types.cpp 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523
  1. struct Scope;
  2. struct Ast;
  3. struct Entity;
  4. enum BasicKind {
  5. Basic_Invalid,
  6. Basic_llvm_bool,
  7. Basic_bool,
  8. Basic_b8,
  9. Basic_b16,
  10. Basic_b32,
  11. Basic_b64,
  12. Basic_i8,
  13. Basic_u8,
  14. Basic_i16,
  15. Basic_u16,
  16. Basic_i32,
  17. Basic_u32,
  18. Basic_i64,
  19. Basic_u64,
  20. Basic_i128,
  21. Basic_u128,
  22. Basic_rune,
  23. Basic_f16,
  24. Basic_f32,
  25. Basic_f64,
  26. Basic_complex32,
  27. Basic_complex64,
  28. Basic_complex128,
  29. Basic_quaternion64,
  30. Basic_quaternion128,
  31. Basic_quaternion256,
  32. Basic_int,
  33. Basic_uint,
  34. Basic_uintptr,
  35. Basic_rawptr,
  36. Basic_string, // ^u8 + int
  37. Basic_cstring, // ^u8
  38. Basic_any, // rawptr + ^Type_Info
  39. Basic_typeid,
  40. // Endian Specific Types
  41. Basic_i16le,
  42. Basic_u16le,
  43. Basic_i32le,
  44. Basic_u32le,
  45. Basic_i64le,
  46. Basic_u64le,
  47. Basic_i128le,
  48. Basic_u128le,
  49. Basic_i16be,
  50. Basic_u16be,
  51. Basic_i32be,
  52. Basic_u32be,
  53. Basic_i64be,
  54. Basic_u64be,
  55. Basic_i128be,
  56. Basic_u128be,
  57. Basic_f16le,
  58. Basic_f32le,
  59. Basic_f64le,
  60. Basic_f16be,
  61. Basic_f32be,
  62. Basic_f64be,
  63. // Untyped types
  64. Basic_UntypedBool,
  65. Basic_UntypedInteger,
  66. Basic_UntypedFloat,
  67. Basic_UntypedComplex,
  68. Basic_UntypedQuaternion,
  69. Basic_UntypedString,
  70. Basic_UntypedRune,
  71. Basic_UntypedNil,
  72. Basic_UntypedUninit,
  73. Basic_COUNT,
  74. Basic_byte = Basic_u8,
  75. };
  76. enum BasicFlag {
  77. BasicFlag_Boolean = GB_BIT(0),
  78. BasicFlag_Integer = GB_BIT(1),
  79. BasicFlag_Unsigned = GB_BIT(2),
  80. BasicFlag_Float = GB_BIT(3),
  81. BasicFlag_Complex = GB_BIT(4),
  82. BasicFlag_Quaternion = GB_BIT(5),
  83. BasicFlag_Pointer = GB_BIT(6),
  84. BasicFlag_String = GB_BIT(7),
  85. BasicFlag_Rune = GB_BIT(8),
  86. BasicFlag_Untyped = GB_BIT(9),
  87. BasicFlag_LLVM = GB_BIT(11),
  88. BasicFlag_EndianLittle = GB_BIT(13),
  89. BasicFlag_EndianBig = GB_BIT(14),
  90. BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Complex | BasicFlag_Quaternion,
  91. BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  92. BasicFlag_OrderedNumeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Rune,
  93. BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  94. BasicFlag_SimpleCompare = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_Rune,
  95. };
  96. struct BasicType {
  97. BasicKind kind;
  98. u32 flags;
  99. i64 size; // -1 if arch. dep.
  100. String name;
  101. };
  102. enum StructSoaKind : u8 {
  103. StructSoa_None = 0,
  104. StructSoa_Fixed = 1,
  105. StructSoa_Slice = 2,
  106. StructSoa_Dynamic = 3,
  107. };
  108. struct TypeStruct {
  109. Slice<Entity *> fields;
  110. String * tags; // count == fields.count
  111. i64 * offsets; // count == fields.count
  112. Ast * node;
  113. Scope * scope;
  114. i64 custom_align;
  115. Type * polymorphic_params; // Type_Tuple
  116. Type * polymorphic_parent;
  117. Type * soa_elem;
  118. i32 soa_count;
  119. StructSoaKind soa_kind;
  120. RwMutex fields_mutex;
  121. BlockingMutex offset_mutex; // for settings offsets
  122. bool is_polymorphic;
  123. bool are_offsets_set : 1;
  124. bool are_offsets_being_processed : 1;
  125. bool is_packed : 1;
  126. bool is_raw_union : 1;
  127. bool is_no_copy : 1;
  128. bool is_poly_specialized : 1;
  129. };
  130. struct TypeUnion {
  131. Slice<Type *> variants;
  132. Ast * node;
  133. Scope * scope;
  134. i64 variant_block_size;
  135. i64 custom_align;
  136. Type * polymorphic_params; // Type_Tuple
  137. Type * polymorphic_parent;
  138. i16 tag_size;
  139. bool is_polymorphic;
  140. bool is_poly_specialized;
  141. UnionTypeKind kind;
  142. };
  143. struct TypeProc {
  144. Ast *node;
  145. Scope * scope;
  146. Type * params; // Type_Tuple
  147. Type * results; // Type_Tuple
  148. i32 param_count;
  149. i32 result_count;
  150. isize specialization_count;
  151. ProcCallingConvention calling_convention;
  152. i32 variadic_index;
  153. // TODO(bill): Make this a flag set rather than bools
  154. bool variadic;
  155. bool require_results;
  156. bool c_vararg;
  157. bool is_polymorphic;
  158. bool is_poly_specialized;
  159. bool has_named_results;
  160. bool diverging; // no return
  161. bool return_by_pointer;
  162. bool optional_ok;
  163. };
  164. #define TYPE_KINDS \
  165. TYPE_KIND(Basic, BasicType) \
  166. TYPE_KIND(Named, struct { \
  167. String name; \
  168. Type * base; \
  169. Entity *type_name; /* Entity_TypeName */ \
  170. }) \
  171. TYPE_KIND(Generic, struct { \
  172. i64 id; \
  173. String name; \
  174. Type * specialized; \
  175. Scope * scope; \
  176. Entity *entity; \
  177. }) \
  178. TYPE_KIND(Pointer, struct { Type *elem; }) \
  179. TYPE_KIND(MultiPointer, struct { Type *elem; }) \
  180. TYPE_KIND(Array, struct { \
  181. Type *elem; \
  182. i64 count; \
  183. Type *generic_count; \
  184. }) \
  185. TYPE_KIND(EnumeratedArray, struct { \
  186. Type *elem; \
  187. Type *index; \
  188. ExactValue *min_value; \
  189. ExactValue *max_value; \
  190. i64 count; \
  191. TokenKind op; \
  192. bool is_sparse; \
  193. }) \
  194. TYPE_KIND(Slice, struct { Type *elem; }) \
  195. TYPE_KIND(DynamicArray, struct { Type *elem; }) \
  196. TYPE_KIND(Map, struct { \
  197. Type *key; \
  198. Type *value; \
  199. Type *lookup_result_type; \
  200. }) \
  201. TYPE_KIND(Struct, TypeStruct) \
  202. TYPE_KIND(Union, TypeUnion) \
  203. TYPE_KIND(Enum, struct { \
  204. Array<Entity *> fields; \
  205. Ast *node; \
  206. Scope * scope; \
  207. Type * base_type; \
  208. ExactValue *min_value; \
  209. ExactValue *max_value; \
  210. isize min_value_index; \
  211. isize max_value_index; \
  212. }) \
  213. TYPE_KIND(Tuple, struct { \
  214. Slice<Entity *> variables; /* Entity_Variable */ \
  215. i64 * offsets; \
  216. BlockingMutex mutex; /* for settings offsets */ \
  217. bool are_offsets_being_processed; \
  218. bool are_offsets_set; \
  219. bool is_packed; \
  220. }) \
  221. TYPE_KIND(Proc, TypeProc) \
  222. TYPE_KIND(BitSet, struct { \
  223. Type *elem; \
  224. Type *underlying; \
  225. i64 lower; \
  226. i64 upper; \
  227. Ast * node; \
  228. }) \
  229. TYPE_KIND(SimdVector, struct { \
  230. i64 count; \
  231. Type *elem; \
  232. Type *generic_count; \
  233. }) \
  234. TYPE_KIND(RelativePointer, struct { \
  235. Type *pointer_type; \
  236. Type *base_integer; \
  237. }) \
  238. TYPE_KIND(RelativeMultiPointer, struct { \
  239. Type *pointer_type; \
  240. Type *base_integer; \
  241. }) \
  242. TYPE_KIND(Matrix, struct { \
  243. Type *elem; \
  244. i64 row_count; \
  245. i64 column_count; \
  246. Type *generic_row_count; \
  247. Type *generic_column_count; \
  248. i64 stride_in_bytes; \
  249. }) \
  250. TYPE_KIND(SoaPointer, struct { Type *elem; })
  251. enum TypeKind {
  252. Type_Invalid,
  253. #define TYPE_KIND(k, ...) GB_JOIN2(Type_, k),
  254. TYPE_KINDS
  255. #undef TYPE_KIND
  256. Type_Count,
  257. };
  258. gb_global String const type_strings[] = {
  259. {cast(u8 *)"Invalid", gb_size_of("Invalid")},
  260. #define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1},
  261. TYPE_KINDS
  262. #undef TYPE_KIND
  263. };
  264. #define TYPE_KIND(k, ...) typedef __VA_ARGS__ GB_JOIN2(Type, k);
  265. TYPE_KINDS
  266. #undef TYPE_KIND
  267. enum TypeFlag : u32 {
  268. TypeFlag_Polymorphic = 1<<1,
  269. TypeFlag_PolySpecialized = 1<<2,
  270. TypeFlag_InProcessOfCheckingPolymorphic = 1<<3,
  271. };
  272. struct Type {
  273. TypeKind kind;
  274. union {
  275. #define TYPE_KIND(k, ...) GB_JOIN2(Type, k) k;
  276. TYPE_KINDS
  277. #undef TYPE_KIND
  278. };
  279. // NOTE(bill): These need to be at the end to not affect the unionized data
  280. std::atomic<i64> cached_size;
  281. std::atomic<i64> cached_align;
  282. std::atomic<u32> flags; // TypeFlag
  283. bool failure;
  284. };
  285. // IMPORTANT NOTE(bill): This must match the same as the in core.odin
  286. enum Typeid_Kind : u8 {
  287. Typeid_Invalid,
  288. Typeid_Integer,
  289. Typeid_Rune,
  290. Typeid_Float,
  291. Typeid_Complex,
  292. Typeid_Quaternion,
  293. Typeid_String,
  294. Typeid_Boolean,
  295. Typeid_Any,
  296. Typeid_Type_Id,
  297. Typeid_Pointer,
  298. Typeid_Multi_Pointer,
  299. Typeid_Procedure,
  300. Typeid_Array,
  301. Typeid_Enumerated_Array,
  302. Typeid_Dynamic_Array,
  303. Typeid_Slice,
  304. Typeid_Tuple,
  305. Typeid_Struct,
  306. Typeid_Union,
  307. Typeid_Enum,
  308. Typeid_Map,
  309. Typeid_Bit_Set,
  310. Typeid_Simd_Vector,
  311. Typeid_Relative_Pointer,
  312. Typeid_Relative_Multi_Pointer,
  313. Typeid_Matrix,
  314. Typeid_SoaPointer,
  315. };
  316. // IMPORTANT NOTE(bill): This must match the same as the in core.odin
  317. enum TypeInfoFlag : u32 {
  318. TypeInfoFlag_Comparable = 1<<0,
  319. TypeInfoFlag_Simple_Compare = 1<<1,
  320. };
  321. enum : int {
  322. MATRIX_ELEMENT_COUNT_MIN = 1,
  323. MATRIX_ELEMENT_COUNT_MAX = 16,
  324. MATRIX_ELEMENT_MAX_SIZE = MATRIX_ELEMENT_COUNT_MAX * (2 * 8), // complex128
  325. SIMD_ELEMENT_COUNT_MIN = 1,
  326. SIMD_ELEMENT_COUNT_MAX = 64,
  327. };
  328. gb_internal bool is_type_comparable(Type *t);
  329. gb_internal bool is_type_simple_compare(Type *t);
  330. gb_internal u32 type_info_flags_of_type(Type *type) {
  331. if (type == nullptr) {
  332. return 0;
  333. }
  334. u32 flags = 0;
  335. if (is_type_comparable(type)) {
  336. flags |= TypeInfoFlag_Comparable;
  337. }
  338. if (is_type_simple_compare(type)) {
  339. flags |= TypeInfoFlag_Comparable;
  340. }
  341. return flags;
  342. }
  343. // TODO(bill): Should I add extra information here specifying the kind of selection?
  344. // e.g. field, constant, array field, type field, etc.
  345. struct Selection {
  346. Entity * entity;
  347. Array<i32> index;
  348. bool indirect; // Set if there was a pointer deref anywhere down the line
  349. u8 swizzle_count; // maximum components = 4
  350. u8 swizzle_indices; // 2 bits per component, representing which swizzle index
  351. bool pseudo_field;
  352. };
  353. gb_global Selection const empty_selection = {0};
  354. gb_internal Selection make_selection(Entity *entity, Array<i32> index, bool indirect) {
  355. Selection s = {entity, index, indirect};
  356. return s;
  357. }
  358. gb_internal void selection_add_index(Selection *s, isize index) {
  359. // IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
  360. // of heap allocation
  361. // TODO(bill): Find a way to use a backing buffer for initial use as the general case is probably .count<3
  362. if (s->index.data == nullptr) {
  363. array_init(&s->index, heap_allocator());
  364. }
  365. array_add(&s->index, cast(i32)index);
  366. }
  367. gb_internal Selection selection_combine(Selection const &lhs, Selection const &rhs) {
  368. Selection new_sel = lhs;
  369. new_sel.indirect = lhs.indirect || rhs.indirect;
  370. new_sel.index = array_make<i32>(heap_allocator(), lhs.index.count+rhs.index.count);
  371. array_copy(&new_sel.index, lhs.index, 0);
  372. array_copy(&new_sel.index, rhs.index, lhs.index.count);
  373. return new_sel;
  374. }
  375. gb_internal Selection sub_selection(Selection const &sel, isize offset) {
  376. Selection res = {};
  377. res.index.data = sel.index.data + offset;
  378. res.index.count = gb_max(sel.index.count - offset, 0);
  379. res.index.capacity = res.index.count;
  380. return res;
  381. }
  382. gb_global Type basic_types[] = {
  383. {Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}},
  384. {Type_Basic, {Basic_llvm_bool, BasicFlag_Boolean | BasicFlag_LLVM, 1, STR_LIT("llvm bool")}},
  385. {Type_Basic, {Basic_bool, BasicFlag_Boolean, 1, STR_LIT("bool")}},
  386. {Type_Basic, {Basic_b8, BasicFlag_Boolean, 1, STR_LIT("b8")}},
  387. {Type_Basic, {Basic_b16, BasicFlag_Boolean, 2, STR_LIT("b16")}},
  388. {Type_Basic, {Basic_b32, BasicFlag_Boolean, 4, STR_LIT("b32")}},
  389. {Type_Basic, {Basic_b64, BasicFlag_Boolean, 8, STR_LIT("b64")}},
  390. {Type_Basic, {Basic_i8, BasicFlag_Integer, 1, STR_LIT("i8")}},
  391. {Type_Basic, {Basic_u8, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("u8")}},
  392. {Type_Basic, {Basic_i16, BasicFlag_Integer, 2, STR_LIT("i16")}},
  393. {Type_Basic, {Basic_u16, BasicFlag_Integer | BasicFlag_Unsigned, 2, STR_LIT("u16")}},
  394. {Type_Basic, {Basic_i32, BasicFlag_Integer, 4, STR_LIT("i32")}},
  395. {Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, 4, STR_LIT("u32")}},
  396. {Type_Basic, {Basic_i64, BasicFlag_Integer, 8, STR_LIT("i64")}},
  397. {Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, 8, STR_LIT("u64")}},
  398. {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}},
  399. {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}},
  400. {Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}},
  401. {Type_Basic, {Basic_f16, BasicFlag_Float, 2, STR_LIT("f16")}},
  402. {Type_Basic, {Basic_f32, BasicFlag_Float, 4, STR_LIT("f32")}},
  403. {Type_Basic, {Basic_f64, BasicFlag_Float, 8, STR_LIT("f64")}},
  404. {Type_Basic, {Basic_complex32, BasicFlag_Complex, 4, STR_LIT("complex32")}},
  405. {Type_Basic, {Basic_complex64, BasicFlag_Complex, 8, STR_LIT("complex64")}},
  406. {Type_Basic, {Basic_complex128, BasicFlag_Complex, 16, STR_LIT("complex128")}},
  407. {Type_Basic, {Basic_quaternion64, BasicFlag_Quaternion, 8, STR_LIT("quaternion64")}},
  408. {Type_Basic, {Basic_quaternion128, BasicFlag_Quaternion, 16, STR_LIT("quaternion128")}},
  409. {Type_Basic, {Basic_quaternion256, BasicFlag_Quaternion, 32, STR_LIT("quaternion256")}},
  410. {Type_Basic, {Basic_int, BasicFlag_Integer, -1, STR_LIT("int")}},
  411. {Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uint")}},
  412. {Type_Basic, {Basic_uintptr, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uintptr")}},
  413. {Type_Basic, {Basic_rawptr, BasicFlag_Pointer, -1, STR_LIT("rawptr")}},
  414. {Type_Basic, {Basic_string, BasicFlag_String, -1, STR_LIT("string")}},
  415. {Type_Basic, {Basic_cstring, BasicFlag_String, -1, STR_LIT("cstring")}},
  416. {Type_Basic, {Basic_any, 0, -1, STR_LIT("any")}},
  417. {Type_Basic, {Basic_typeid, 0, -1, STR_LIT("typeid")}},
  418. // Endian
  419. {Type_Basic, {Basic_i16le, BasicFlag_Integer | BasicFlag_EndianLittle, 2, STR_LIT("i16le")}},
  420. {Type_Basic, {Basic_u16le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 2, STR_LIT("u16le")}},
  421. {Type_Basic, {Basic_i32le, BasicFlag_Integer | BasicFlag_EndianLittle, 4, STR_LIT("i32le")}},
  422. {Type_Basic, {Basic_u32le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 4, STR_LIT("u32le")}},
  423. {Type_Basic, {Basic_i64le, BasicFlag_Integer | BasicFlag_EndianLittle, 8, STR_LIT("i64le")}},
  424. {Type_Basic, {Basic_u64le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 8, STR_LIT("u64le")}},
  425. {Type_Basic, {Basic_i128le, BasicFlag_Integer | BasicFlag_EndianLittle, 16, STR_LIT("i128le")}},
  426. {Type_Basic, {Basic_u128le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 16, STR_LIT("u128le")}},
  427. {Type_Basic, {Basic_i16be, BasicFlag_Integer | BasicFlag_EndianBig, 2, STR_LIT("i16be")}},
  428. {Type_Basic, {Basic_u16be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 2, STR_LIT("u16be")}},
  429. {Type_Basic, {Basic_i32be, BasicFlag_Integer | BasicFlag_EndianBig, 4, STR_LIT("i32be")}},
  430. {Type_Basic, {Basic_u32be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 4, STR_LIT("u32be")}},
  431. {Type_Basic, {Basic_i64be, BasicFlag_Integer | BasicFlag_EndianBig, 8, STR_LIT("i64be")}},
  432. {Type_Basic, {Basic_u64be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 8, STR_LIT("u64be")}},
  433. {Type_Basic, {Basic_i128be, BasicFlag_Integer | BasicFlag_EndianBig, 16, STR_LIT("i128be")}},
  434. {Type_Basic, {Basic_u128be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 16, STR_LIT("u128be")}},
  435. {Type_Basic, {Basic_f16le, BasicFlag_Float | BasicFlag_EndianLittle, 2, STR_LIT("f16le")}},
  436. {Type_Basic, {Basic_f32le, BasicFlag_Float | BasicFlag_EndianLittle, 4, STR_LIT("f32le")}},
  437. {Type_Basic, {Basic_f64le, BasicFlag_Float | BasicFlag_EndianLittle, 8, STR_LIT("f64le")}},
  438. {Type_Basic, {Basic_f16be, BasicFlag_Float | BasicFlag_EndianBig, 2, STR_LIT("f16be")}},
  439. {Type_Basic, {Basic_f32be, BasicFlag_Float | BasicFlag_EndianBig, 4, STR_LIT("f32be")}},
  440. {Type_Basic, {Basic_f64be, BasicFlag_Float | BasicFlag_EndianBig, 8, STR_LIT("f64be")}},
  441. // Untyped types
  442. {Type_Basic, {Basic_UntypedBool, BasicFlag_Boolean | BasicFlag_Untyped, 0, STR_LIT("untyped bool")}},
  443. {Type_Basic, {Basic_UntypedInteger, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped integer")}},
  444. {Type_Basic, {Basic_UntypedFloat, BasicFlag_Float | BasicFlag_Untyped, 0, STR_LIT("untyped float")}},
  445. {Type_Basic, {Basic_UntypedComplex, BasicFlag_Complex | BasicFlag_Untyped, 0, STR_LIT("untyped complex")}},
  446. {Type_Basic, {Basic_UntypedQuaternion, BasicFlag_Quaternion | BasicFlag_Untyped, 0, STR_LIT("untyped quaternion")}},
  447. {Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}},
  448. {Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}},
  449. {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}},
  450. {Type_Basic, {Basic_UntypedUninit, BasicFlag_Untyped, 0, STR_LIT("untyped uninitialized")}},
  451. };
  452. // gb_global Type basic_type_aliases[] = {
  453. // // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}},
  454. // // {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}},
  455. // };
  456. gb_global Type *t_invalid = &basic_types[Basic_Invalid];
  457. gb_global Type *t_llvm_bool = &basic_types[Basic_llvm_bool];
  458. gb_global Type *t_bool = &basic_types[Basic_bool];
  459. gb_global Type *t_i8 = &basic_types[Basic_i8];
  460. gb_global Type *t_u8 = &basic_types[Basic_u8];
  461. gb_global Type *t_i16 = &basic_types[Basic_i16];
  462. gb_global Type *t_u16 = &basic_types[Basic_u16];
  463. gb_global Type *t_i32 = &basic_types[Basic_i32];
  464. gb_global Type *t_u32 = &basic_types[Basic_u32];
  465. gb_global Type *t_i64 = &basic_types[Basic_i64];
  466. gb_global Type *t_u64 = &basic_types[Basic_u64];
  467. gb_global Type *t_i128 = &basic_types[Basic_i128];
  468. gb_global Type *t_u128 = &basic_types[Basic_u128];
  469. gb_global Type *t_rune = &basic_types[Basic_rune];
  470. gb_global Type *t_f16 = &basic_types[Basic_f16];
  471. gb_global Type *t_f32 = &basic_types[Basic_f32];
  472. gb_global Type *t_f64 = &basic_types[Basic_f64];
  473. gb_global Type *t_complex32 = &basic_types[Basic_complex32];
  474. gb_global Type *t_complex64 = &basic_types[Basic_complex64];
  475. gb_global Type *t_complex128 = &basic_types[Basic_complex128];
  476. gb_global Type *t_quaternion64 = &basic_types[Basic_quaternion64];
  477. gb_global Type *t_quaternion128 = &basic_types[Basic_quaternion128];
  478. gb_global Type *t_quaternion256 = &basic_types[Basic_quaternion256];
  479. gb_global Type *t_int = &basic_types[Basic_int];
  480. gb_global Type *t_uint = &basic_types[Basic_uint];
  481. gb_global Type *t_uintptr = &basic_types[Basic_uintptr];
  482. gb_global Type *t_rawptr = &basic_types[Basic_rawptr];
  483. gb_global Type *t_string = &basic_types[Basic_string];
  484. gb_global Type *t_cstring = &basic_types[Basic_cstring];
  485. gb_global Type *t_any = &basic_types[Basic_any];
  486. gb_global Type *t_typeid = &basic_types[Basic_typeid];
  487. gb_global Type *t_i16le = &basic_types[Basic_i16le];
  488. gb_global Type *t_u16le = &basic_types[Basic_u16le];
  489. gb_global Type *t_i32le = &basic_types[Basic_i32le];
  490. gb_global Type *t_u32le = &basic_types[Basic_u32le];
  491. gb_global Type *t_i64le = &basic_types[Basic_i64le];
  492. gb_global Type *t_u64le = &basic_types[Basic_u64le];
  493. gb_global Type *t_i128le = &basic_types[Basic_i128le];
  494. gb_global Type *t_u128le = &basic_types[Basic_u128le];
  495. gb_global Type *t_i16be = &basic_types[Basic_i16be];
  496. gb_global Type *t_u16be = &basic_types[Basic_u16be];
  497. gb_global Type *t_i32be = &basic_types[Basic_i32be];
  498. gb_global Type *t_u32be = &basic_types[Basic_u32be];
  499. gb_global Type *t_i64be = &basic_types[Basic_i64be];
  500. gb_global Type *t_u64be = &basic_types[Basic_u64be];
  501. gb_global Type *t_i128be = &basic_types[Basic_i128be];
  502. gb_global Type *t_u128be = &basic_types[Basic_u128be];
  503. gb_global Type *t_untyped_bool = &basic_types[Basic_UntypedBool];
  504. gb_global Type *t_untyped_integer = &basic_types[Basic_UntypedInteger];
  505. gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat];
  506. gb_global Type *t_untyped_complex = &basic_types[Basic_UntypedComplex];
  507. gb_global Type *t_untyped_quaternion = &basic_types[Basic_UntypedQuaternion];
  508. gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString];
  509. gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
  510. gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil];
  511. gb_global Type *t_untyped_uninit = &basic_types[Basic_UntypedUninit];
  512. gb_global Type *t_u8_ptr = nullptr;
  513. gb_global Type *t_u8_multi_ptr = nullptr;
  514. gb_global Type *t_int_ptr = nullptr;
  515. gb_global Type *t_i64_ptr = nullptr;
  516. gb_global Type *t_f64_ptr = nullptr;
  517. gb_global Type *t_u8_slice = nullptr;
  518. gb_global Type *t_string_slice = nullptr;
  519. // Type generated for the "preload" file
  520. gb_global Type *t_type_info = nullptr;
  521. gb_global Type *t_type_info_enum_value = nullptr;
  522. gb_global Type *t_type_info_ptr = nullptr;
  523. gb_global Type *t_type_info_enum_value_ptr = nullptr;
  524. gb_global Type *t_type_info_named = nullptr;
  525. gb_global Type *t_type_info_integer = nullptr;
  526. gb_global Type *t_type_info_rune = nullptr;
  527. gb_global Type *t_type_info_float = nullptr;
  528. gb_global Type *t_type_info_complex = nullptr;
  529. gb_global Type *t_type_info_quaternion = nullptr;
  530. gb_global Type *t_type_info_any = nullptr;
  531. gb_global Type *t_type_info_typeid = nullptr;
  532. gb_global Type *t_type_info_string = nullptr;
  533. gb_global Type *t_type_info_boolean = nullptr;
  534. gb_global Type *t_type_info_pointer = nullptr;
  535. gb_global Type *t_type_info_multi_pointer = nullptr;
  536. gb_global Type *t_type_info_procedure = nullptr;
  537. gb_global Type *t_type_info_array = nullptr;
  538. gb_global Type *t_type_info_enumerated_array = nullptr;
  539. gb_global Type *t_type_info_dynamic_array = nullptr;
  540. gb_global Type *t_type_info_slice = nullptr;
  541. gb_global Type *t_type_info_parameters = nullptr;
  542. gb_global Type *t_type_info_struct = nullptr;
  543. gb_global Type *t_type_info_union = nullptr;
  544. gb_global Type *t_type_info_enum = nullptr;
  545. gb_global Type *t_type_info_map = nullptr;
  546. gb_global Type *t_type_info_bit_set = nullptr;
  547. gb_global Type *t_type_info_simd_vector = nullptr;
  548. gb_global Type *t_type_info_relative_pointer = nullptr;
  549. gb_global Type *t_type_info_relative_multi_pointer = nullptr;
  550. gb_global Type *t_type_info_matrix = nullptr;
  551. gb_global Type *t_type_info_soa_pointer = nullptr;
  552. gb_global Type *t_type_info_named_ptr = nullptr;
  553. gb_global Type *t_type_info_integer_ptr = nullptr;
  554. gb_global Type *t_type_info_rune_ptr = nullptr;
  555. gb_global Type *t_type_info_float_ptr = nullptr;
  556. gb_global Type *t_type_info_complex_ptr = nullptr;
  557. gb_global Type *t_type_info_quaternion_ptr = nullptr;
  558. gb_global Type *t_type_info_any_ptr = nullptr;
  559. gb_global Type *t_type_info_typeid_ptr = nullptr;
  560. gb_global Type *t_type_info_string_ptr = nullptr;
  561. gb_global Type *t_type_info_boolean_ptr = nullptr;
  562. gb_global Type *t_type_info_pointer_ptr = nullptr;
  563. gb_global Type *t_type_info_multi_pointer_ptr = nullptr;
  564. gb_global Type *t_type_info_procedure_ptr = nullptr;
  565. gb_global Type *t_type_info_array_ptr = nullptr;
  566. gb_global Type *t_type_info_enumerated_array_ptr = nullptr;
  567. gb_global Type *t_type_info_dynamic_array_ptr = nullptr;
  568. gb_global Type *t_type_info_slice_ptr = nullptr;
  569. gb_global Type *t_type_info_parameters_ptr = nullptr;
  570. gb_global Type *t_type_info_struct_ptr = nullptr;
  571. gb_global Type *t_type_info_union_ptr = nullptr;
  572. gb_global Type *t_type_info_enum_ptr = nullptr;
  573. gb_global Type *t_type_info_map_ptr = nullptr;
  574. gb_global Type *t_type_info_bit_set_ptr = nullptr;
  575. gb_global Type *t_type_info_simd_vector_ptr = nullptr;
  576. gb_global Type *t_type_info_relative_pointer_ptr = nullptr;
  577. gb_global Type *t_type_info_relative_multi_pointer_ptr = nullptr;
  578. gb_global Type *t_type_info_matrix_ptr = nullptr;
  579. gb_global Type *t_type_info_soa_pointer_ptr = nullptr;
  580. gb_global Type *t_allocator = nullptr;
  581. gb_global Type *t_allocator_ptr = nullptr;
  582. gb_global Type *t_context = nullptr;
  583. gb_global Type *t_context_ptr = nullptr;
  584. gb_global Type *t_allocator_error = nullptr;
  585. gb_global Type *t_source_code_location = nullptr;
  586. gb_global Type *t_source_code_location_ptr = nullptr;
  587. gb_global Type *t_map_info = nullptr;
  588. gb_global Type *t_map_cell_info = nullptr;
  589. gb_global Type *t_raw_map = nullptr;
  590. gb_global Type *t_map_info_ptr = nullptr;
  591. gb_global Type *t_map_cell_info_ptr = nullptr;
  592. gb_global Type *t_raw_map_ptr = nullptr;
  593. gb_global Type *t_equal_proc = nullptr;
  594. gb_global Type *t_hasher_proc = nullptr;
  595. gb_global Type *t_map_get_proc = nullptr;
  596. gb_global Type *t_map_set_proc = nullptr;
  597. gb_global Type *t_objc_object = nullptr;
  598. gb_global Type *t_objc_selector = nullptr;
  599. gb_global Type *t_objc_class = nullptr;
  600. gb_global Type *t_objc_id = nullptr;
  601. gb_global Type *t_objc_SEL = nullptr;
  602. gb_global Type *t_objc_Class = nullptr;
  603. enum OdinAtomicMemoryOrder : i32 {
  604. OdinAtomicMemoryOrder_relaxed = 0, // unordered
  605. OdinAtomicMemoryOrder_consume = 1, // monotonic
  606. OdinAtomicMemoryOrder_acquire = 2,
  607. OdinAtomicMemoryOrder_release = 3,
  608. OdinAtomicMemoryOrder_acq_rel = 4,
  609. OdinAtomicMemoryOrder_seq_cst = 5,
  610. OdinAtomicMemoryOrder_COUNT,
  611. };
  612. char const *OdinAtomicMemoryOrder_strings[OdinAtomicMemoryOrder_COUNT] = {
  613. "Relaxed",
  614. "Consume",
  615. "Acquire",
  616. "Release",
  617. "Acq_Rel",
  618. "Seq_Cst",
  619. };
  620. gb_global Type *t_atomic_memory_order = nullptr;
  621. gb_global RecursiveMutex g_type_mutex;
  622. struct TypePath;
  623. gb_internal i64 type_size_of (Type *t);
  624. gb_internal i64 type_align_of (Type *t);
  625. gb_internal i64 type_offset_of (Type *t, i64 index, Type **field_type_=nullptr);
  626. gb_internal gbString type_to_string (Type *type, bool shorthand=true);
  627. gb_internal gbString type_to_string (Type *type, gbAllocator allocator, bool shorthand=true);
  628. gb_internal i64 type_size_of_internal(Type *t, TypePath *path);
  629. gb_internal void init_map_internal_types(Type *type);
  630. gb_internal Type * bit_set_to_int(Type *t);
  631. gb_internal bool are_types_identical(Type *x, Type *y);
  632. gb_internal bool is_type_pointer(Type *t);
  633. gb_internal bool is_type_multi_pointer(Type *t);
  634. gb_internal bool is_type_soa_pointer(Type *t);
  635. gb_internal bool is_type_proc(Type *t);
  636. gb_internal bool is_type_slice(Type *t);
  637. gb_internal bool is_type_integer(Type *t);
  638. gb_internal bool type_set_offsets(Type *t);
  639. gb_internal Type *base_type(Type *t);
  640. gb_internal i64 type_size_of_internal(Type *t, TypePath *path);
  641. gb_internal i64 type_align_of_internal(Type *t, TypePath *path);
  642. // IMPORTANT TODO(bill): SHould this TypePath code be removed since type cycle checking is handled much earlier on?
  643. struct TypePath {
  644. RecursiveMutex mutex;
  645. Array<Entity *> path; // Entity_TypeName;
  646. bool failure;
  647. };
  648. gb_internal void type_path_init(TypePath *tp) {
  649. tp->path.allocator = heap_allocator();
  650. }
  651. gb_internal void type_path_free(TypePath *tp) {
  652. mutex_lock(&tp->mutex);
  653. array_free(&tp->path);
  654. mutex_unlock(&tp->mutex);
  655. }
  656. gb_internal void type_path_print_illegal_cycle(TypePath *tp, isize start_index) {
  657. GB_ASSERT(tp != nullptr);
  658. GB_ASSERT(start_index < tp->path.count);
  659. Entity *e = tp->path[start_index];
  660. GB_ASSERT(e != nullptr);
  661. error(e->token, "Illegal type declaration cycle of `%.*s`", LIT(e->token.string));
  662. // NOTE(bill): Print cycle, if it's deep enough
  663. for (isize j = start_index; j < tp->path.count; j++) {
  664. Entity *e = tp->path[j];
  665. error(e->token, "\t%.*s refers to", LIT(e->token.string));
  666. }
  667. // NOTE(bill): This will only print if the path count > 1
  668. error(e->token, "\t%.*s", LIT(e->token.string));
  669. tp->failure = true;
  670. e->type->failure = true;
  671. base_type(e->type)->failure = true;
  672. }
  673. gb_internal bool type_path_push(TypePath *tp, Type *t) {
  674. GB_ASSERT(tp != nullptr);
  675. if (t->kind != Type_Named) {
  676. return false;
  677. }
  678. Entity *e = t->Named.type_name;
  679. mutex_lock(&tp->mutex);
  680. for (isize i = 0; i < tp->path.count; i++) {
  681. Entity *p = tp->path[i];
  682. if (p == e) {
  683. type_path_print_illegal_cycle(tp, i);
  684. }
  685. }
  686. array_add(&tp->path, e);
  687. mutex_unlock(&tp->mutex);
  688. return true;
  689. }
  690. gb_internal void type_path_pop(TypePath *tp) {
  691. if (tp != nullptr) {
  692. mutex_lock(&tp->mutex);
  693. if (tp->path.count > 0) {
  694. array_pop(&tp->path);
  695. }
  696. mutex_unlock(&tp->mutex);
  697. }
  698. }
  699. #define FAILURE_SIZE 0
  700. #define FAILURE_ALIGNMENT 0
  701. gb_internal bool type_ptr_set_update(PtrSet<Type *> *s, Type *t) {
  702. if (t == nullptr) {
  703. return true;
  704. }
  705. if (ptr_set_exists(s, t)) {
  706. return true;
  707. }
  708. ptr_set_add(s, t);
  709. return false;
  710. }
  711. gb_internal bool type_ptr_set_exists(PtrSet<Type *> *s, Type *t) {
  712. if (t == nullptr) {
  713. return true;
  714. }
  715. if (ptr_set_exists(s, t)) {
  716. return true;
  717. }
  718. // TODO(bill, 2019-10-05): This is very slow and it's probably a lot
  719. // faster to cache types correctly
  720. for (Type *f : *s) {
  721. if (are_types_identical(t, f)) {
  722. ptr_set_add(s, t);
  723. return true;
  724. }
  725. }
  726. return false;
  727. }
  728. gb_internal Type *base_type(Type *t) {
  729. for (;;) {
  730. if (t == nullptr) {
  731. break;
  732. }
  733. if (t->kind != Type_Named) {
  734. break;
  735. }
  736. if (t == t->Named.base) {
  737. return t_invalid;
  738. }
  739. t = t->Named.base;
  740. }
  741. return t;
  742. }
  743. gb_internal Type *base_enum_type(Type *t) {
  744. Type *bt = base_type(t);
  745. if (bt != nullptr &&
  746. bt->kind == Type_Enum) {
  747. return bt->Enum.base_type;
  748. }
  749. return t;
  750. }
  751. gb_internal Type *core_type(Type *t) {
  752. for (;;) {
  753. if (t == nullptr) {
  754. break;
  755. }
  756. switch (t->kind) {
  757. case Type_Named:
  758. if (t == t->Named.base) {
  759. return t_invalid;
  760. }
  761. t = t->Named.base;
  762. continue;
  763. case Type_Enum:
  764. t = t->Enum.base_type;
  765. continue;
  766. }
  767. break;
  768. }
  769. return t;
  770. }
  771. gb_internal void set_base_type(Type *t, Type *base) {
  772. if (t && t->kind == Type_Named) {
  773. t->Named.base = base;
  774. }
  775. }
  776. gb_internal Type *alloc_type(TypeKind kind) {
  777. // gbAllocator a = heap_allocator();
  778. gbAllocator a = permanent_allocator();
  779. Type *t = gb_alloc_item(a, Type);
  780. zero_item(t);
  781. t->kind = kind;
  782. t->cached_size = -1;
  783. t->cached_align = -1;
  784. return t;
  785. }
  786. gb_internal Type *alloc_type_generic(Scope *scope, i64 id, String name, Type *specialized) {
  787. Type *t = alloc_type(Type_Generic);
  788. t->Generic.id = id;
  789. t->Generic.name = name;
  790. t->Generic.specialized = specialized;
  791. t->Generic.scope = scope;
  792. return t;
  793. }
  794. gb_internal Type *alloc_type_pointer(Type *elem) {
  795. Type *t = alloc_type(Type_Pointer);
  796. t->Pointer.elem = elem;
  797. return t;
  798. }
  799. gb_internal Type *alloc_type_multi_pointer(Type *elem) {
  800. Type *t = alloc_type(Type_MultiPointer);
  801. t->MultiPointer.elem = elem;
  802. return t;
  803. }
  804. gb_internal Type *alloc_type_soa_pointer(Type *elem) {
  805. Type *t = alloc_type(Type_SoaPointer);
  806. t->SoaPointer.elem = elem;
  807. return t;
  808. }
  809. gb_internal Type *alloc_type_array(Type *elem, i64 count, Type *generic_count = nullptr) {
  810. if (generic_count != nullptr) {
  811. Type *t = alloc_type(Type_Array);
  812. t->Array.elem = elem;
  813. t->Array.count = count;
  814. t->Array.generic_count = generic_count;
  815. return t;
  816. }
  817. Type *t = alloc_type(Type_Array);
  818. t->Array.elem = elem;
  819. t->Array.count = count;
  820. return t;
  821. }
  822. gb_internal Type *alloc_type_matrix(Type *elem, i64 row_count, i64 column_count, Type *generic_row_count = nullptr, Type *generic_column_count = nullptr) {
  823. if (generic_row_count != nullptr || generic_column_count != nullptr) {
  824. Type *t = alloc_type(Type_Matrix);
  825. t->Matrix.elem = elem;
  826. t->Matrix.row_count = row_count;
  827. t->Matrix.column_count = column_count;
  828. t->Matrix.generic_row_count = generic_row_count;
  829. t->Matrix.generic_column_count = generic_column_count;
  830. return t;
  831. }
  832. Type *t = alloc_type(Type_Matrix);
  833. t->Matrix.elem = elem;
  834. t->Matrix.row_count = row_count;
  835. t->Matrix.column_count = column_count;
  836. return t;
  837. }
  838. gb_internal Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue const *min_value, ExactValue const *max_value, isize count, TokenKind op) {
  839. Type *t = alloc_type(Type_EnumeratedArray);
  840. t->EnumeratedArray.elem = elem;
  841. t->EnumeratedArray.index = index;
  842. t->EnumeratedArray.min_value = gb_alloc_item(permanent_allocator(), ExactValue);
  843. t->EnumeratedArray.max_value = gb_alloc_item(permanent_allocator(), ExactValue);
  844. gb_memmove(t->EnumeratedArray.min_value, min_value, gb_size_of(ExactValue));
  845. gb_memmove(t->EnumeratedArray.max_value, max_value, gb_size_of(ExactValue));
  846. t->EnumeratedArray.op = op;
  847. if (count == 0) {
  848. t->EnumeratedArray.count = 0;
  849. } else {
  850. t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(*max_value, *min_value));
  851. }
  852. return t;
  853. }
  854. gb_internal Type *alloc_type_slice(Type *elem) {
  855. Type *t = alloc_type(Type_Slice);
  856. t->Slice.elem = elem;
  857. return t;
  858. }
  859. gb_internal Type *alloc_type_dynamic_array(Type *elem) {
  860. Type *t = alloc_type(Type_DynamicArray);
  861. t->DynamicArray.elem = elem;
  862. return t;
  863. }
  864. gb_internal Type *alloc_type_struct() {
  865. Type *t = alloc_type(Type_Struct);
  866. return t;
  867. }
  868. gb_internal Type *alloc_type_union() {
  869. Type *t = alloc_type(Type_Union);
  870. return t;
  871. }
  872. gb_internal Type *alloc_type_enum() {
  873. Type *t = alloc_type(Type_Enum);
  874. t->Enum.min_value = gb_alloc_item(permanent_allocator(), ExactValue);
  875. t->Enum.max_value = gb_alloc_item(permanent_allocator(), ExactValue);
  876. return t;
  877. }
  878. gb_internal Type *alloc_type_relative_pointer(Type *pointer_type, Type *base_integer) {
  879. GB_ASSERT(is_type_pointer(pointer_type));
  880. GB_ASSERT(is_type_integer(base_integer));
  881. Type *t = alloc_type(Type_RelativePointer);
  882. t->RelativePointer.pointer_type = pointer_type;
  883. t->RelativePointer.base_integer = base_integer;
  884. return t;
  885. }
  886. gb_internal Type *alloc_type_relative_multi_pointer(Type *pointer_type, Type *base_integer) {
  887. GB_ASSERT(is_type_multi_pointer(pointer_type));
  888. GB_ASSERT(is_type_integer(base_integer));
  889. Type *t = alloc_type(Type_RelativeMultiPointer);
  890. t->RelativeMultiPointer.pointer_type = pointer_type;
  891. t->RelativeMultiPointer.base_integer = base_integer;
  892. return t;
  893. }
  894. gb_internal Type *alloc_type_named(String name, Type *base, Entity *type_name) {
  895. Type *t = alloc_type(Type_Named);
  896. t->Named.name = name;
  897. t->Named.base = base;
  898. if (base != t) {
  899. t->Named.base = base_type(base);
  900. }
  901. t->Named.type_name = type_name;
  902. return t;
  903. }
  904. gb_internal bool is_calling_convention_none(ProcCallingConvention calling_convention) {
  905. switch (calling_convention) {
  906. case ProcCC_None:
  907. case ProcCC_InlineAsm:
  908. return true;
  909. }
  910. return false;
  911. }
  912. gb_internal bool is_calling_convention_odin(ProcCallingConvention calling_convention) {
  913. switch (calling_convention) {
  914. case ProcCC_Odin:
  915. case ProcCC_Contextless:
  916. return true;
  917. }
  918. return false;
  919. }
  920. gb_internal Type *alloc_type_tuple() {
  921. Type *t = alloc_type(Type_Tuple);
  922. return t;
  923. }
  924. gb_internal Type *alloc_type_proc(Scope *scope, Type *params, isize param_count, Type *results, isize result_count, bool variadic, ProcCallingConvention calling_convention) {
  925. Type *t = alloc_type(Type_Proc);
  926. if (variadic) {
  927. if (param_count == 0) {
  928. GB_PANIC("variadic procedure must have at least one parameter");
  929. }
  930. GB_ASSERT(params != nullptr && params->kind == Type_Tuple);
  931. Entity *e = params->Tuple.variables[param_count-1];
  932. if (base_type(e->type)->kind != Type_Slice) {
  933. // NOTE(bill): For custom calling convention
  934. GB_PANIC("variadic parameter must be of type slice");
  935. }
  936. }
  937. t->Proc.scope = scope;
  938. t->Proc.params = params;
  939. t->Proc.param_count = cast(i32)param_count;
  940. t->Proc.results = results;
  941. t->Proc.result_count = cast(i32)result_count;
  942. t->Proc.variadic = variadic;
  943. t->Proc.calling_convention = calling_convention;
  944. return t;
  945. }
  946. gb_internal bool is_type_valid_for_keys(Type *t);
  947. gb_internal Type *alloc_type_bit_set() {
  948. Type *t = alloc_type(Type_BitSet);
  949. return t;
  950. }
  951. gb_internal Type *alloc_type_simd_vector(i64 count, Type *elem, Type *generic_count=nullptr) {
  952. Type *t = alloc_type(Type_SimdVector);
  953. t->SimdVector.count = count;
  954. t->SimdVector.elem = elem;
  955. t->SimdVector.generic_count = generic_count;
  956. return t;
  957. }
  958. ////////////////////////////////////////////////////////////////
  959. gb_internal Type *type_deref(Type *t, bool allow_multi_pointer=false) {
  960. if (t != nullptr) {
  961. Type *bt = base_type(t);
  962. if (bt == nullptr) {
  963. return nullptr;
  964. }
  965. switch (bt->kind) {
  966. case Type_Pointer:
  967. return bt->Pointer.elem;
  968. case Type_RelativePointer:
  969. return type_deref(bt->RelativePointer.pointer_type);
  970. case Type_SoaPointer:
  971. {
  972. Type *elem = base_type(bt->SoaPointer.elem);
  973. GB_ASSERT(elem->kind == Type_Struct && elem->Struct.soa_kind != StructSoa_None);
  974. return elem->Struct.soa_elem;
  975. }
  976. case Type_MultiPointer:
  977. if (allow_multi_pointer) {
  978. return bt->MultiPointer.elem;
  979. }
  980. break;
  981. }
  982. }
  983. return t;
  984. }
  985. gb_internal bool is_type_named(Type *t) {
  986. if (t->kind == Type_Basic) {
  987. return true;
  988. }
  989. return t->kind == Type_Named;
  990. }
  991. gb_internal bool is_type_boolean(Type *t) {
  992. // t = core_type(t);
  993. t = base_type(t);
  994. if (t->kind == Type_Basic) {
  995. return (t->Basic.flags & BasicFlag_Boolean) != 0;
  996. }
  997. return false;
  998. }
  999. gb_internal bool is_type_integer(Type *t) {
  1000. // t = core_type(t);
  1001. t = base_type(t);
  1002. if (t->kind == Type_Basic) {
  1003. return (t->Basic.flags & BasicFlag_Integer) != 0;
  1004. }
  1005. return false;
  1006. }
  1007. gb_internal bool is_type_integer_like(Type *t) {
  1008. t = core_type(t);
  1009. if (t->kind == Type_Basic) {
  1010. return (t->Basic.flags & (BasicFlag_Integer|BasicFlag_Boolean)) != 0;
  1011. }
  1012. if (t->kind == Type_BitSet) {
  1013. if (t->BitSet.underlying) {
  1014. return is_type_integer_like(t->BitSet.underlying);
  1015. }
  1016. return true;
  1017. }
  1018. return false;
  1019. }
  1020. gb_internal bool is_type_unsigned(Type *t) {
  1021. t = base_type(t);
  1022. // t = core_type(t);
  1023. if (t->kind == Type_Basic) {
  1024. return (t->Basic.flags & BasicFlag_Unsigned) != 0;
  1025. }
  1026. return false;
  1027. }
  1028. gb_internal bool is_type_integer_128bit(Type *t) {
  1029. // t = core_type(t);
  1030. t = base_type(t);
  1031. if (t->kind == Type_Basic) {
  1032. return (t->Basic.flags & BasicFlag_Integer) != 0 && t->Basic.size == 16;
  1033. }
  1034. return false;
  1035. }
  1036. gb_internal bool is_type_rune(Type *t) {
  1037. // t = core_type(t);
  1038. t = base_type(t);
  1039. if (t->kind == Type_Basic) {
  1040. return (t->Basic.flags & BasicFlag_Rune) != 0;
  1041. }
  1042. return false;
  1043. }
  1044. gb_internal bool is_type_numeric(Type *t) {
  1045. // t = core_type(t);
  1046. t = base_type(t);
  1047. if (t->kind == Type_Basic) {
  1048. return (t->Basic.flags & BasicFlag_Numeric) != 0;
  1049. } else if (t->kind == Type_Enum) {
  1050. return is_type_numeric(t->Enum.base_type);
  1051. }
  1052. // TODO(bill): Should this be here?
  1053. if (t->kind == Type_Array) {
  1054. return is_type_numeric(t->Array.elem);
  1055. }
  1056. return false;
  1057. }
  1058. gb_internal bool is_type_string(Type *t) {
  1059. t = base_type(t);
  1060. if (t->kind == Type_Basic) {
  1061. return (t->Basic.flags & BasicFlag_String) != 0;
  1062. }
  1063. return false;
  1064. }
  1065. gb_internal bool is_type_cstring(Type *t) {
  1066. t = base_type(t);
  1067. if (t->kind == Type_Basic) {
  1068. return t->Basic.kind == Basic_cstring;
  1069. }
  1070. return false;
  1071. }
  1072. gb_internal bool is_type_typed(Type *t) {
  1073. t = base_type(t);
  1074. if (t == nullptr) {
  1075. return false;
  1076. }
  1077. if (t->kind == Type_Basic) {
  1078. return (t->Basic.flags & BasicFlag_Untyped) == 0;
  1079. }
  1080. return true;
  1081. }
  1082. gb_internal bool is_type_untyped(Type *t) {
  1083. t = base_type(t);
  1084. if (t == nullptr) {
  1085. return false;
  1086. }
  1087. if (t->kind == Type_Basic) {
  1088. return (t->Basic.flags & BasicFlag_Untyped) != 0;
  1089. }
  1090. return false;
  1091. }
  1092. gb_internal bool is_type_ordered(Type *t) {
  1093. t = core_type(t);
  1094. switch (t->kind) {
  1095. case Type_Basic:
  1096. return (t->Basic.flags & BasicFlag_Ordered) != 0;
  1097. case Type_Pointer:
  1098. return true;
  1099. case Type_MultiPointer:
  1100. return true;
  1101. }
  1102. return false;
  1103. }
  1104. gb_internal bool is_type_ordered_numeric(Type *t) {
  1105. t = core_type(t);
  1106. switch (t->kind) {
  1107. case Type_Basic:
  1108. return (t->Basic.flags & BasicFlag_OrderedNumeric) != 0;
  1109. }
  1110. return false;
  1111. }
  1112. gb_internal bool is_type_constant_type(Type *t) {
  1113. t = core_type(t);
  1114. if (t->kind == Type_Basic) {
  1115. return (t->Basic.flags & BasicFlag_ConstantType) != 0;
  1116. }
  1117. if (t->kind == Type_BitSet) {
  1118. return true;
  1119. }
  1120. if (t->kind == Type_Proc) {
  1121. return true;
  1122. }
  1123. return false;
  1124. }
  1125. gb_internal bool is_type_float(Type *t) {
  1126. t = core_type(t);
  1127. if (t->kind == Type_Basic) {
  1128. return (t->Basic.flags & BasicFlag_Float) != 0;
  1129. }
  1130. return false;
  1131. }
  1132. gb_internal bool is_type_complex(Type *t) {
  1133. t = core_type(t);
  1134. if (t->kind == Type_Basic) {
  1135. return (t->Basic.flags & BasicFlag_Complex) != 0;
  1136. }
  1137. return false;
  1138. }
  1139. gb_internal bool is_type_quaternion(Type *t) {
  1140. t = core_type(t);
  1141. if (t->kind == Type_Basic) {
  1142. return (t->Basic.flags & BasicFlag_Quaternion) != 0;
  1143. }
  1144. return false;
  1145. }
  1146. gb_internal bool is_type_complex_or_quaternion(Type *t) {
  1147. t = core_type(t);
  1148. if (t->kind == Type_Basic) {
  1149. return (t->Basic.flags & (BasicFlag_Complex|BasicFlag_Quaternion)) != 0;
  1150. }
  1151. return false;
  1152. }
  1153. gb_internal bool is_type_pointer(Type *t) {
  1154. t = base_type(t);
  1155. if (t->kind == Type_Basic) {
  1156. return (t->Basic.flags & BasicFlag_Pointer) != 0;
  1157. }
  1158. return t->kind == Type_Pointer;
  1159. }
  1160. gb_internal bool is_type_soa_pointer(Type *t) {
  1161. t = base_type(t);
  1162. return t->kind == Type_SoaPointer;
  1163. }
  1164. gb_internal bool is_type_multi_pointer(Type *t) {
  1165. t = base_type(t);
  1166. return t->kind == Type_MultiPointer;
  1167. }
  1168. gb_internal bool is_type_internally_pointer_like(Type *t) {
  1169. return is_type_pointer(t) || is_type_multi_pointer(t) || is_type_cstring(t) || is_type_proc(t);
  1170. }
  1171. gb_internal bool is_type_tuple(Type *t) {
  1172. t = base_type(t);
  1173. return t->kind == Type_Tuple;
  1174. }
  1175. gb_internal bool is_type_uintptr(Type *t) {
  1176. if (t->kind == Type_Basic) {
  1177. return (t->Basic.kind == Basic_uintptr);
  1178. }
  1179. return false;
  1180. }
  1181. gb_internal bool is_type_rawptr(Type *t) {
  1182. if (t->kind == Type_Basic) {
  1183. return t->Basic.kind == Basic_rawptr;
  1184. }
  1185. return false;
  1186. }
  1187. gb_internal bool is_type_u8(Type *t) {
  1188. if (t->kind == Type_Basic) {
  1189. return t->Basic.kind == Basic_u8;
  1190. }
  1191. return false;
  1192. }
  1193. gb_internal bool is_type_array(Type *t) {
  1194. t = base_type(t);
  1195. return t->kind == Type_Array;
  1196. }
  1197. gb_internal bool is_type_enumerated_array(Type *t) {
  1198. t = base_type(t);
  1199. return t->kind == Type_EnumeratedArray;
  1200. }
  1201. gb_internal bool is_type_matrix(Type *t) {
  1202. t = base_type(t);
  1203. return t->kind == Type_Matrix;
  1204. }
  1205. gb_internal i64 matrix_align_of(Type *t, struct TypePath *tp) {
  1206. t = base_type(t);
  1207. GB_ASSERT(t->kind == Type_Matrix);
  1208. Type *elem = t->Matrix.elem;
  1209. i64 row_count = gb_max(t->Matrix.row_count, 1);
  1210. bool pop = type_path_push(tp, elem);
  1211. if (tp->failure) {
  1212. return FAILURE_ALIGNMENT;
  1213. }
  1214. i64 elem_align = type_align_of_internal(elem, tp);
  1215. if (pop) type_path_pop(tp);
  1216. i64 elem_size = type_size_of(elem);
  1217. // NOTE(bill, 2021-10-25): The alignment strategy here is to have zero padding
  1218. // It would be better for performance to pad each column so that each column
  1219. // could be maximally aligned but as a compromise, having no padding will be
  1220. // beneficial to third libraries that assume no padding
  1221. i64 total_expected_size = row_count*t->Matrix.column_count*elem_size;
  1222. // i64 min_alignment = prev_pow2(elem_align * row_count);
  1223. i64 min_alignment = prev_pow2(total_expected_size);
  1224. while ((total_expected_size % min_alignment) != 0) {
  1225. min_alignment >>= 1;
  1226. }
  1227. GB_ASSERT(min_alignment >= elem_align);
  1228. i64 align = gb_min(min_alignment, build_context.max_simd_align);
  1229. return align;
  1230. }
  1231. gb_internal i64 matrix_type_stride_in_bytes(Type *t, struct TypePath *tp) {
  1232. t = base_type(t);
  1233. GB_ASSERT(t->kind == Type_Matrix);
  1234. if (t->Matrix.stride_in_bytes != 0) {
  1235. return t->Matrix.stride_in_bytes;
  1236. } else if (t->Matrix.row_count == 0) {
  1237. return 0;
  1238. }
  1239. i64 elem_size;
  1240. if (tp != nullptr) {
  1241. elem_size = type_size_of_internal(t->Matrix.elem, tp);
  1242. } else {
  1243. elem_size = type_size_of(t->Matrix.elem);
  1244. }
  1245. i64 stride_in_bytes = 0;
  1246. // NOTE(bill, 2021-10-25): The alignment strategy here is to have zero padding
  1247. // It would be better for performance to pad each column so that each column
  1248. // could be maximally aligned but as a compromise, having no padding will be
  1249. // beneficial to third libraries that assume no padding
  1250. i64 row_count = t->Matrix.row_count;
  1251. stride_in_bytes = elem_size*row_count;
  1252. t->Matrix.stride_in_bytes = stride_in_bytes;
  1253. return stride_in_bytes;
  1254. }
  1255. gb_internal i64 matrix_type_stride_in_elems(Type *t) {
  1256. t = base_type(t);
  1257. GB_ASSERT(t->kind == Type_Matrix);
  1258. i64 stride = matrix_type_stride_in_bytes(t, nullptr);
  1259. return stride/gb_max(1, type_size_of(t->Matrix.elem));
  1260. }
  1261. gb_internal i64 matrix_type_total_internal_elems(Type *t) {
  1262. t = base_type(t);
  1263. GB_ASSERT(t->kind == Type_Matrix);
  1264. i64 size = type_size_of(t);
  1265. i64 elem_size = type_size_of(t->Matrix.elem);
  1266. return size/gb_max(elem_size, 1);
  1267. }
  1268. gb_internal i64 matrix_indices_to_offset(Type *t, i64 row_index, i64 column_index) {
  1269. t = base_type(t);
  1270. GB_ASSERT(t->kind == Type_Matrix);
  1271. GB_ASSERT(0 <= row_index && row_index < t->Matrix.row_count);
  1272. GB_ASSERT(0 <= column_index && column_index < t->Matrix.column_count);
  1273. i64 stride_elems = matrix_type_stride_in_elems(t);
  1274. // NOTE(bill): Column-major layout internally
  1275. return row_index + stride_elems*column_index;
  1276. }
  1277. gb_internal i64 matrix_row_major_index_to_offset(Type *t, i64 index) {
  1278. t = base_type(t);
  1279. GB_ASSERT(t->kind == Type_Matrix);
  1280. i64 row_index = index/t->Matrix.column_count;
  1281. i64 column_index = index%t->Matrix.column_count;
  1282. return matrix_indices_to_offset(t, row_index, column_index);
  1283. }
  1284. gb_internal i64 matrix_column_major_index_to_offset(Type *t, i64 index) {
  1285. t = base_type(t);
  1286. GB_ASSERT(t->kind == Type_Matrix);
  1287. i64 row_index = index%t->Matrix.row_count;
  1288. i64 column_index = index/t->Matrix.row_count;
  1289. return matrix_indices_to_offset(t, row_index, column_index);
  1290. }
  1291. gb_internal bool is_matrix_square(Type *t) {
  1292. t = base_type(t);
  1293. GB_ASSERT(t->kind == Type_Matrix);
  1294. return t->Matrix.row_count == t->Matrix.column_count;
  1295. }
  1296. gb_internal bool is_type_valid_for_matrix_elems(Type *t) {
  1297. t = base_type(t);
  1298. if (is_type_integer(t)) {
  1299. return true;
  1300. } else if (is_type_float(t)) {
  1301. return true;
  1302. } else if (is_type_complex(t)) {
  1303. return true;
  1304. }
  1305. if (t->kind == Type_Generic) {
  1306. return true;
  1307. }
  1308. return false;
  1309. }
  1310. gb_internal bool is_type_dynamic_array(Type *t) {
  1311. t = base_type(t);
  1312. return t->kind == Type_DynamicArray;
  1313. }
  1314. gb_internal bool is_type_slice(Type *t) {
  1315. t = base_type(t);
  1316. return t->kind == Type_Slice;
  1317. }
  1318. gb_internal bool is_type_proc(Type *t) {
  1319. t = base_type(t);
  1320. return t->kind == Type_Proc;
  1321. }
  1322. gb_internal bool is_type_asm_proc(Type *t) {
  1323. t = base_type(t);
  1324. return t->kind == Type_Proc && t->Proc.calling_convention == ProcCC_InlineAsm;
  1325. }
  1326. gb_internal bool is_type_simd_vector(Type *t) {
  1327. t = base_type(t);
  1328. return t->kind == Type_SimdVector;
  1329. }
  1330. gb_internal Type *base_array_type(Type *t) {
  1331. Type *bt = base_type(t);
  1332. if (is_type_array(bt)) {
  1333. return bt->Array.elem;
  1334. } else if (is_type_enumerated_array(bt)) {
  1335. return bt->EnumeratedArray.elem;
  1336. } else if (is_type_simd_vector(bt)) {
  1337. return bt->SimdVector.elem;
  1338. } else if (is_type_matrix(bt)) {
  1339. return bt->Matrix.elem;
  1340. }
  1341. return t;
  1342. }
  1343. gb_internal bool is_type_generic(Type *t) {
  1344. t = base_type(t);
  1345. return t->kind == Type_Generic;
  1346. }
  1347. gb_internal bool is_type_relative_pointer(Type *t) {
  1348. t = base_type(t);
  1349. return t->kind == Type_RelativePointer;
  1350. }
  1351. gb_internal bool is_type_relative_multi_pointer(Type *t) {
  1352. t = base_type(t);
  1353. return t->kind == Type_RelativeMultiPointer;
  1354. }
  1355. gb_internal bool is_type_u8_slice(Type *t) {
  1356. t = base_type(t);
  1357. if (t->kind == Type_Slice) {
  1358. return is_type_u8(t->Slice.elem);
  1359. }
  1360. return false;
  1361. }
  1362. gb_internal bool is_type_u8_array(Type *t) {
  1363. t = base_type(t);
  1364. if (t->kind == Type_Array) {
  1365. return is_type_u8(t->Array.elem);
  1366. }
  1367. return false;
  1368. }
  1369. gb_internal bool is_type_u8_ptr(Type *t) {
  1370. t = base_type(t);
  1371. if (t->kind == Type_Pointer) {
  1372. return is_type_u8(t->Slice.elem);
  1373. }
  1374. return false;
  1375. }
  1376. gb_internal bool is_type_u8_multi_ptr(Type *t) {
  1377. t = base_type(t);
  1378. if (t->kind == Type_MultiPointer) {
  1379. return is_type_u8(t->Slice.elem);
  1380. }
  1381. return false;
  1382. }
  1383. gb_internal bool is_type_rune_array(Type *t) {
  1384. t = base_type(t);
  1385. if (t->kind == Type_Array) {
  1386. return is_type_rune(t->Array.elem);
  1387. }
  1388. return false;
  1389. }
  1390. gb_internal bool is_type_array_like(Type *t) {
  1391. return is_type_array(t) || is_type_enumerated_array(t);
  1392. }
  1393. gb_internal i64 get_array_type_count(Type *t) {
  1394. Type *bt = base_type(t);
  1395. if (bt->kind == Type_Array) {
  1396. return bt->Array.count;
  1397. } else if (bt->kind == Type_EnumeratedArray) {
  1398. return bt->EnumeratedArray.count;
  1399. } else if (bt->kind == Type_SimdVector) {
  1400. return bt->SimdVector.count;
  1401. }
  1402. GB_ASSERT(is_type_array_like(t));
  1403. return -1;
  1404. }
  1405. gb_internal Type *core_array_type(Type *t) {
  1406. for (;;) {
  1407. t = base_array_type(t);
  1408. switch (t->kind) {
  1409. case Type_Array:
  1410. case Type_EnumeratedArray:
  1411. case Type_SimdVector:
  1412. case Type_Matrix:
  1413. break;
  1414. default:
  1415. return t;
  1416. }
  1417. }
  1418. }
  1419. gb_internal i32 type_math_rank(Type *t) {
  1420. i32 rank = 0;
  1421. for (;;) {
  1422. t = base_type(t);
  1423. switch (t->kind) {
  1424. case Type_Array:
  1425. rank += 1;
  1426. t = t->Array.elem;
  1427. break;
  1428. case Type_Matrix:
  1429. rank += 2;
  1430. t = t->Matrix.elem;
  1431. break;
  1432. default:
  1433. return rank;
  1434. }
  1435. }
  1436. }
  1437. gb_internal Type *base_complex_elem_type(Type *t) {
  1438. t = core_type(t);
  1439. if (t->kind == Type_Basic) {
  1440. switch (t->Basic.kind) {
  1441. case Basic_complex32: return t_f16;
  1442. case Basic_complex64: return t_f32;
  1443. case Basic_complex128: return t_f64;
  1444. case Basic_quaternion64: return t_f16;
  1445. case Basic_quaternion128: return t_f32;
  1446. case Basic_quaternion256: return t_f64;
  1447. case Basic_UntypedComplex: return t_untyped_float;
  1448. case Basic_UntypedQuaternion: return t_untyped_float;
  1449. }
  1450. }
  1451. GB_PANIC("Invalid complex type");
  1452. return t_invalid;
  1453. }
  1454. gb_internal bool is_type_struct(Type *t) {
  1455. t = base_type(t);
  1456. return t->kind == Type_Struct;
  1457. }
  1458. gb_internal bool is_type_union(Type *t) {
  1459. t = base_type(t);
  1460. return t->kind == Type_Union;
  1461. }
  1462. gb_internal bool is_type_soa_struct(Type *t) {
  1463. t = base_type(t);
  1464. return t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None;
  1465. }
  1466. gb_internal bool is_type_raw_union(Type *t) {
  1467. t = base_type(t);
  1468. return (t->kind == Type_Struct && t->Struct.is_raw_union);
  1469. }
  1470. gb_internal bool is_type_no_copy(Type *t) {
  1471. t = base_type(t);
  1472. return (t->kind == Type_Struct && t->Struct.is_no_copy);
  1473. }
  1474. gb_internal bool is_type_enum(Type *t) {
  1475. t = base_type(t);
  1476. return (t->kind == Type_Enum);
  1477. }
  1478. gb_internal bool is_type_bit_set(Type *t) {
  1479. t = base_type(t);
  1480. return (t->kind == Type_BitSet);
  1481. }
  1482. gb_internal bool is_type_map(Type *t) {
  1483. t = base_type(t);
  1484. return t->kind == Type_Map;
  1485. }
  1486. gb_internal bool is_type_union_maybe_pointer(Type *t) {
  1487. t = base_type(t);
  1488. if (t->kind == Type_Union && t->Union.variants.count == 1) {
  1489. Type *v = t->Union.variants[0];
  1490. return is_type_internally_pointer_like(v);
  1491. }
  1492. return false;
  1493. }
  1494. gb_internal bool is_type_union_maybe_pointer_original_alignment(Type *t) {
  1495. t = base_type(t);
  1496. if (t->kind == Type_Union && t->Union.variants.count == 1) {
  1497. Type *v = t->Union.variants[0];
  1498. if (is_type_internally_pointer_like(v)) {
  1499. return type_align_of(v) == type_align_of(t);
  1500. }
  1501. }
  1502. return false;
  1503. }
  1504. gb_internal bool is_type_endian_big(Type *t) {
  1505. t = core_type(t);
  1506. if (t->kind == Type_Basic) {
  1507. if (t->Basic.flags & BasicFlag_EndianBig) {
  1508. return true;
  1509. } else if (t->Basic.flags & BasicFlag_EndianLittle) {
  1510. return false;
  1511. }
  1512. return build_context.endian_kind == TargetEndian_Big;
  1513. } else if (t->kind == Type_BitSet) {
  1514. return is_type_endian_big(bit_set_to_int(t));
  1515. } else if (t->kind == Type_Pointer) {
  1516. return is_type_endian_big(&basic_types[Basic_uintptr]);
  1517. }
  1518. return build_context.endian_kind == TargetEndian_Big;
  1519. }
  1520. gb_internal bool is_type_endian_little(Type *t) {
  1521. t = core_type(t);
  1522. if (t->kind == Type_Basic) {
  1523. if (t->Basic.flags & BasicFlag_EndianLittle) {
  1524. return true;
  1525. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  1526. return false;
  1527. }
  1528. return build_context.endian_kind == TargetEndian_Little;
  1529. } else if (t->kind == Type_BitSet) {
  1530. return is_type_endian_little(bit_set_to_int(t));
  1531. } else if (t->kind == Type_Pointer) {
  1532. return is_type_endian_little(&basic_types[Basic_uintptr]);
  1533. }
  1534. return build_context.endian_kind == TargetEndian_Little;
  1535. }
  1536. gb_internal bool is_type_endian_platform(Type *t) {
  1537. t = core_type(t);
  1538. if (t->kind == Type_Basic) {
  1539. return (t->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) == 0;
  1540. } else if (t->kind == Type_BitSet) {
  1541. return is_type_endian_platform(bit_set_to_int(t));
  1542. } else if (t->kind == Type_Pointer) {
  1543. return is_type_endian_platform(&basic_types[Basic_uintptr]);
  1544. }
  1545. return false;
  1546. }
  1547. gb_internal bool types_have_same_internal_endian(Type *a, Type *b) {
  1548. return is_type_endian_little(a) == is_type_endian_little(b);
  1549. }
  1550. gb_internal bool is_type_endian_specific(Type *t) {
  1551. t = core_type(t);
  1552. if (t->kind == Type_BitSet) {
  1553. t = bit_set_to_int(t);
  1554. }
  1555. if (t->kind == Type_Basic) {
  1556. switch (t->Basic.kind) {
  1557. case Basic_i16le:
  1558. case Basic_u16le:
  1559. case Basic_i32le:
  1560. case Basic_u32le:
  1561. case Basic_i64le:
  1562. case Basic_u64le:
  1563. case Basic_u128le:
  1564. return true;
  1565. case Basic_i16be:
  1566. case Basic_u16be:
  1567. case Basic_i32be:
  1568. case Basic_u32be:
  1569. case Basic_i64be:
  1570. case Basic_u64be:
  1571. case Basic_u128be:
  1572. return true;
  1573. case Basic_f16le:
  1574. case Basic_f16be:
  1575. case Basic_f32le:
  1576. case Basic_f32be:
  1577. case Basic_f64le:
  1578. case Basic_f64be:
  1579. return true;
  1580. }
  1581. }
  1582. return false;
  1583. }
  1584. gb_internal bool is_type_dereferenceable(Type *t) {
  1585. if (is_type_rawptr(t)) {
  1586. return false;
  1587. }
  1588. return is_type_pointer(t) || is_type_soa_pointer(t);
  1589. }
  1590. gb_internal bool is_type_different_to_arch_endianness(Type *t) {
  1591. switch (build_context.endian_kind) {
  1592. case TargetEndian_Little:
  1593. return !is_type_endian_little(t);
  1594. case TargetEndian_Big:
  1595. return !is_type_endian_big(t);
  1596. }
  1597. return false;
  1598. }
  1599. gb_internal Type *integer_endian_type_to_platform_type(Type *t) {
  1600. t = core_type(t);
  1601. if (t->kind == Type_BitSet) {
  1602. t = bit_set_to_int(t);
  1603. }
  1604. GB_ASSERT_MSG(t->kind == Type_Basic, "%s", type_to_string(t));
  1605. switch (t->Basic.kind) {
  1606. // Endian Specific Types
  1607. case Basic_i16le: return t_i16;
  1608. case Basic_u16le: return t_u16;
  1609. case Basic_i32le: return t_i32;
  1610. case Basic_u32le: return t_u32;
  1611. case Basic_i64le: return t_i64;
  1612. case Basic_u64le: return t_u64;
  1613. case Basic_i128le: return t_i128;
  1614. case Basic_u128le: return t_u128;
  1615. case Basic_i16be: return t_i16;
  1616. case Basic_u16be: return t_u16;
  1617. case Basic_i32be: return t_i32;
  1618. case Basic_u32be: return t_u32;
  1619. case Basic_i64be: return t_i64;
  1620. case Basic_u64be: return t_u64;
  1621. case Basic_i128be: return t_i128;
  1622. case Basic_u128be: return t_u128;
  1623. case Basic_f16le: return t_f16;
  1624. case Basic_f16be: return t_f16;
  1625. case Basic_f32le: return t_f32;
  1626. case Basic_f32be: return t_f32;
  1627. case Basic_f64le: return t_f64;
  1628. case Basic_f64be: return t_f64;
  1629. }
  1630. return t;
  1631. }
  1632. gb_internal bool is_type_any(Type *t) {
  1633. t = base_type(t);
  1634. return (t->kind == Type_Basic && t->Basic.kind == Basic_any);
  1635. }
  1636. gb_internal bool is_type_typeid(Type *t) {
  1637. t = base_type(t);
  1638. return (t->kind == Type_Basic && t->Basic.kind == Basic_typeid);
  1639. }
  1640. gb_internal bool is_type_untyped_nil(Type *t) {
  1641. t = base_type(t);
  1642. // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling
  1643. return (t->kind == Type_Basic && (t->Basic.kind == Basic_UntypedNil || t->Basic.kind == Basic_UntypedUninit));
  1644. }
  1645. gb_internal bool is_type_untyped_uninit(Type *t) {
  1646. t = base_type(t);
  1647. // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling
  1648. return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUninit);
  1649. }
  1650. gb_internal bool is_type_empty_union(Type *t) {
  1651. t = base_type(t);
  1652. return t->kind == Type_Union && t->Union.variants.count == 0;
  1653. }
  1654. gb_internal bool is_type_valid_for_keys(Type *t) {
  1655. t = core_type(t);
  1656. if (t->kind == Type_Generic) {
  1657. return true;
  1658. }
  1659. if (is_type_untyped(t)) {
  1660. return false;
  1661. }
  1662. return type_size_of(t) > 0 && is_type_comparable(t);
  1663. }
  1664. gb_internal bool is_type_valid_bit_set_elem(Type *t) {
  1665. if (is_type_enum(t)) {
  1666. return true;
  1667. }
  1668. t = core_type(t);
  1669. if (t->kind == Type_Generic) {
  1670. return true;
  1671. }
  1672. return false;
  1673. }
  1674. gb_internal Type *bit_set_to_int(Type *t) {
  1675. GB_ASSERT(is_type_bit_set(t));
  1676. Type *bt = base_type(t);
  1677. Type *underlying = bt->BitSet.underlying;
  1678. if (underlying != nullptr && is_type_integer(underlying)) {
  1679. return underlying;
  1680. }
  1681. i64 sz = type_size_of(t);
  1682. switch (sz) {
  1683. case 0: return t_u8;
  1684. case 1: return t_u8;
  1685. case 2: return t_u16;
  1686. case 4: return t_u32;
  1687. case 8: return t_u64;
  1688. case 16: return t_u128;
  1689. }
  1690. GB_PANIC("Unknown bit_set size");
  1691. return nullptr;
  1692. }
  1693. gb_internal bool is_type_valid_vector_elem(Type *t) {
  1694. t = base_type(t);
  1695. if (t->kind == Type_Basic) {
  1696. if (t->Basic.flags & BasicFlag_EndianLittle) {
  1697. return false;
  1698. }
  1699. if (t->Basic.flags & BasicFlag_EndianBig) {
  1700. return false;
  1701. }
  1702. if (is_type_integer(t)) {
  1703. return !is_type_integer_128bit(t);
  1704. }
  1705. if (is_type_float(t)) {
  1706. return true;
  1707. }
  1708. if (is_type_boolean(t)) {
  1709. return true;
  1710. }
  1711. }
  1712. return false;
  1713. }
  1714. gb_internal bool is_type_indexable(Type *t) {
  1715. Type *bt = base_type(t);
  1716. switch (bt->kind) {
  1717. case Type_Basic:
  1718. return bt->Basic.kind == Basic_string;
  1719. case Type_Array:
  1720. case Type_Slice:
  1721. case Type_DynamicArray:
  1722. case Type_Map:
  1723. return true;
  1724. case Type_MultiPointer:
  1725. return true;
  1726. case Type_EnumeratedArray:
  1727. return true;
  1728. case Type_RelativeMultiPointer:
  1729. return true;
  1730. case Type_Matrix:
  1731. return true;
  1732. }
  1733. return false;
  1734. }
  1735. gb_internal bool is_type_sliceable(Type *t) {
  1736. Type *bt = base_type(t);
  1737. switch (bt->kind) {
  1738. case Type_Basic:
  1739. return bt->Basic.kind == Basic_string;
  1740. case Type_Array:
  1741. case Type_Slice:
  1742. case Type_DynamicArray:
  1743. return true;
  1744. case Type_EnumeratedArray:
  1745. return false;
  1746. case Type_RelativeMultiPointer:
  1747. return true;
  1748. case Type_Matrix:
  1749. return false;
  1750. }
  1751. return false;
  1752. }
  1753. gb_internal bool is_type_polymorphic_record(Type *t) {
  1754. t = base_type(t);
  1755. if (t->kind == Type_Struct) {
  1756. return t->Struct.is_polymorphic;
  1757. } else if (t->kind == Type_Union) {
  1758. return t->Union.is_polymorphic;
  1759. }
  1760. return false;
  1761. }
  1762. gb_internal Scope *polymorphic_record_parent_scope(Type *t) {
  1763. t = base_type(t);
  1764. if (is_type_polymorphic_record(t)) {
  1765. if (t->kind == Type_Struct) {
  1766. return t->Struct.scope->parent;
  1767. } else if (t->kind == Type_Union) {
  1768. return t->Union.scope->parent;
  1769. }
  1770. }
  1771. return nullptr;
  1772. }
  1773. gb_internal bool is_type_polymorphic_record_specialized(Type *t) {
  1774. t = base_type(t);
  1775. if (t->kind == Type_Struct) {
  1776. return t->Struct.is_poly_specialized;
  1777. } else if (t->kind == Type_Union) {
  1778. return t->Union.is_poly_specialized;
  1779. }
  1780. return false;
  1781. }
  1782. gb_internal bool is_type_polymorphic_record_unspecialized(Type *t) {
  1783. t = base_type(t);
  1784. if (t->kind == Type_Struct) {
  1785. return t->Struct.is_polymorphic && !t->Struct.is_poly_specialized;
  1786. } else if (t->kind == Type_Struct) {
  1787. return t->Struct.is_polymorphic && !t->Struct.is_poly_specialized;
  1788. }
  1789. return false;
  1790. }
  1791. gb_internal TypeTuple *get_record_polymorphic_params(Type *t) {
  1792. t = base_type(t);
  1793. switch (t->kind) {
  1794. case Type_Struct:
  1795. if (t->Struct.polymorphic_params) {
  1796. return &t->Struct.polymorphic_params->Tuple;
  1797. }
  1798. break;
  1799. case Type_Union:
  1800. if (t->Union.polymorphic_params) {
  1801. return &t->Union.polymorphic_params->Tuple;
  1802. }
  1803. break;
  1804. }
  1805. return nullptr;
  1806. }
  1807. gb_internal bool is_type_polymorphic(Type *t, bool or_specialized=false) {
  1808. if (t == nullptr) {
  1809. return false;
  1810. }
  1811. if (t->flags & TypeFlag_InProcessOfCheckingPolymorphic) {
  1812. return false;
  1813. }
  1814. switch (t->kind) {
  1815. case Type_Generic:
  1816. return true;
  1817. case Type_Named:
  1818. {
  1819. u32 flags = t->flags;
  1820. t->flags |= TypeFlag_InProcessOfCheckingPolymorphic;
  1821. bool ok = is_type_polymorphic(t->Named.base, or_specialized);
  1822. t->flags = flags;
  1823. return ok;
  1824. }
  1825. case Type_Pointer:
  1826. return is_type_polymorphic(t->Pointer.elem, or_specialized);
  1827. case Type_MultiPointer:
  1828. return is_type_polymorphic(t->MultiPointer.elem, or_specialized);
  1829. case Type_SoaPointer:
  1830. return is_type_polymorphic(t->SoaPointer.elem, or_specialized);
  1831. case Type_EnumeratedArray:
  1832. if (is_type_polymorphic(t->EnumeratedArray.index, or_specialized)) {
  1833. return true;
  1834. }
  1835. return is_type_polymorphic(t->EnumeratedArray.elem, or_specialized);
  1836. case Type_Array:
  1837. if (t->Array.generic_count != nullptr) {
  1838. return true;
  1839. }
  1840. return is_type_polymorphic(t->Array.elem, or_specialized);
  1841. case Type_SimdVector:
  1842. if (t->SimdVector.generic_count != nullptr) {
  1843. return true;
  1844. }
  1845. return is_type_polymorphic(t->SimdVector.elem, or_specialized);
  1846. case Type_DynamicArray:
  1847. return is_type_polymorphic(t->DynamicArray.elem, or_specialized);
  1848. case Type_Slice:
  1849. return is_type_polymorphic(t->Slice.elem, or_specialized);
  1850. case Type_Matrix:
  1851. if (t->Matrix.generic_row_count != nullptr) {
  1852. return true;
  1853. }
  1854. if (t->Matrix.generic_column_count != nullptr) {
  1855. return true;
  1856. }
  1857. return is_type_polymorphic(t->Matrix.elem, or_specialized);
  1858. case Type_Tuple:
  1859. for (Entity *e : t->Tuple.variables) {
  1860. if (e->kind == Entity_Constant) {
  1861. if (e->Constant.value.kind != ExactValue_Invalid) {
  1862. return or_specialized;
  1863. }
  1864. } else if (is_type_polymorphic(e->type, or_specialized)) {
  1865. return true;
  1866. }
  1867. }
  1868. break;
  1869. case Type_Proc:
  1870. if (t->Proc.is_polymorphic) {
  1871. return true;
  1872. }
  1873. if (t->Proc.param_count > 0 &&
  1874. is_type_polymorphic(t->Proc.params, or_specialized)) {
  1875. return true;
  1876. }
  1877. if (t->Proc.result_count > 0 &&
  1878. is_type_polymorphic(t->Proc.results, or_specialized)) {
  1879. return true;
  1880. }
  1881. break;
  1882. case Type_Enum:
  1883. if (t->kind == Type_Enum) {
  1884. if (t->Enum.base_type != nullptr) {
  1885. return is_type_polymorphic(t->Enum.base_type, or_specialized);
  1886. }
  1887. return false;
  1888. }
  1889. break;
  1890. case Type_Union:
  1891. if (t->Union.is_polymorphic) {
  1892. return true;
  1893. }
  1894. if (or_specialized && t->Union.is_poly_specialized) {
  1895. return true;
  1896. }
  1897. // for_array(i, t->Union.variants) {
  1898. // if (is_type_polymorphic(t->Union.variants[i], or_specialized)) {
  1899. // return true;
  1900. // }
  1901. // }
  1902. break;
  1903. case Type_Struct:
  1904. if (t->Struct.is_polymorphic) {
  1905. return true;
  1906. }
  1907. if (or_specialized && t->Struct.is_poly_specialized) {
  1908. return true;
  1909. }
  1910. break;
  1911. case Type_Map:
  1912. if (t->Map.key == nullptr || t->Map.value == nullptr) {
  1913. return false;
  1914. }
  1915. if (is_type_polymorphic(t->Map.key, or_specialized)) {
  1916. return true;
  1917. }
  1918. if (is_type_polymorphic(t->Map.value, or_specialized)) {
  1919. return true;
  1920. }
  1921. break;
  1922. case Type_BitSet:
  1923. if (is_type_polymorphic(t->BitSet.elem, or_specialized)) {
  1924. return true;
  1925. }
  1926. if (t->BitSet.underlying != nullptr &&
  1927. is_type_polymorphic(t->BitSet.underlying, or_specialized)) {
  1928. return true;
  1929. }
  1930. break;
  1931. case Type_RelativeMultiPointer:
  1932. if (is_type_polymorphic(t->RelativeMultiPointer.pointer_type, or_specialized)) {
  1933. return true;
  1934. }
  1935. if (t->RelativeMultiPointer.base_integer != nullptr &&
  1936. is_type_polymorphic(t->RelativeMultiPointer.base_integer, or_specialized)) {
  1937. return true;
  1938. }
  1939. break;
  1940. case Type_RelativePointer:
  1941. if (is_type_polymorphic(t->RelativePointer.pointer_type, or_specialized)) {
  1942. return true;
  1943. }
  1944. if (t->RelativePointer.base_integer != nullptr &&
  1945. is_type_polymorphic(t->RelativePointer.base_integer, or_specialized)) {
  1946. return true;
  1947. }
  1948. break;
  1949. }
  1950. return false;
  1951. }
  1952. gb_internal bool type_has_nil(Type *t) {
  1953. t = base_type(t);
  1954. switch (t->kind) {
  1955. case Type_Basic: {
  1956. switch (t->Basic.kind) {
  1957. case Basic_rawptr:
  1958. case Basic_any:
  1959. return true;
  1960. case Basic_cstring:
  1961. return true;
  1962. case Basic_typeid:
  1963. return true;
  1964. }
  1965. return false;
  1966. } break;
  1967. case Type_Enum:
  1968. case Type_BitSet:
  1969. return true;
  1970. case Type_Slice:
  1971. case Type_Proc:
  1972. case Type_Pointer:
  1973. case Type_SoaPointer:
  1974. case Type_MultiPointer:
  1975. case Type_DynamicArray:
  1976. case Type_Map:
  1977. return true;
  1978. case Type_Union:
  1979. return t->Union.kind != UnionType_no_nil;
  1980. case Type_Struct:
  1981. if (is_type_soa_struct(t)) {
  1982. switch (t->Struct.soa_kind) {
  1983. case StructSoa_Fixed: return false;
  1984. case StructSoa_Slice: return true;
  1985. case StructSoa_Dynamic: return true;
  1986. }
  1987. }
  1988. return false;
  1989. case Type_RelativePointer:
  1990. case Type_RelativeMultiPointer:
  1991. return true;
  1992. }
  1993. return false;
  1994. }
  1995. gb_internal bool elem_type_can_be_constant(Type *t) {
  1996. t = base_type(t);
  1997. if (t == t_invalid) {
  1998. return false;
  1999. }
  2000. if (is_type_any(t) || is_type_union(t) || is_type_raw_union(t)) {
  2001. return false;
  2002. }
  2003. return true;
  2004. }
  2005. gb_internal bool is_type_lock_free(Type *t) {
  2006. t = core_type(t);
  2007. if (t == t_invalid) {
  2008. return false;
  2009. }
  2010. i64 sz = type_size_of(t);
  2011. // TODO(bill): Figure this out correctly
  2012. return sz <= build_context.max_align;
  2013. }
  2014. gb_internal bool is_type_comparable(Type *t) {
  2015. t = base_type(t);
  2016. switch (t->kind) {
  2017. case Type_Basic:
  2018. switch (t->Basic.kind) {
  2019. case Basic_UntypedNil:
  2020. case Basic_any:
  2021. return false;
  2022. case Basic_rune:
  2023. return true;
  2024. case Basic_string:
  2025. return true;
  2026. case Basic_cstring:
  2027. return true;
  2028. case Basic_typeid:
  2029. return true;
  2030. }
  2031. return true;
  2032. case Type_Pointer:
  2033. return true;
  2034. case Type_SoaPointer:
  2035. return true;
  2036. case Type_MultiPointer:
  2037. return true;
  2038. case Type_Enum:
  2039. return is_type_comparable(core_type(t));
  2040. case Type_EnumeratedArray:
  2041. return is_type_comparable(t->EnumeratedArray.elem);
  2042. case Type_Array:
  2043. return is_type_comparable(t->Array.elem);
  2044. case Type_Proc:
  2045. return true;
  2046. case Type_Matrix:
  2047. return is_type_comparable(t->Matrix.elem);
  2048. case Type_BitSet:
  2049. return true;
  2050. case Type_Struct:
  2051. if (t->Struct.soa_kind != StructSoa_None) {
  2052. return false;
  2053. }
  2054. if (t->Struct.is_raw_union) {
  2055. return is_type_simple_compare(t);
  2056. }
  2057. for_array(i, t->Struct.fields) {
  2058. Entity *f = t->Struct.fields[i];
  2059. if (!is_type_comparable(f->type)) {
  2060. return false;
  2061. }
  2062. }
  2063. return true;
  2064. case Type_Union:
  2065. for_array(i, t->Union.variants) {
  2066. Type *v = t->Union.variants[i];
  2067. if (!is_type_comparable(v)) {
  2068. return false;
  2069. }
  2070. }
  2071. return true;
  2072. case Type_SimdVector:
  2073. return true;
  2074. }
  2075. return false;
  2076. }
  2077. // NOTE(bill): type can be easily compared using memcmp
  2078. gb_internal bool is_type_simple_compare(Type *t) {
  2079. t = core_type(t);
  2080. switch (t->kind) {
  2081. case Type_Array:
  2082. return is_type_simple_compare(t->Array.elem);
  2083. case Type_EnumeratedArray:
  2084. return is_type_simple_compare(t->EnumeratedArray.elem);
  2085. case Type_Basic:
  2086. if (t->Basic.flags & BasicFlag_SimpleCompare) {
  2087. return true;
  2088. }
  2089. if (t->Basic.kind == Basic_typeid) {
  2090. return true;
  2091. }
  2092. return false;
  2093. case Type_Pointer:
  2094. case Type_MultiPointer:
  2095. case Type_SoaPointer:
  2096. case Type_Proc:
  2097. case Type_BitSet:
  2098. return true;
  2099. case Type_Matrix:
  2100. return is_type_simple_compare(t->Matrix.elem);
  2101. case Type_Struct:
  2102. for_array(i, t->Struct.fields) {
  2103. Entity *f = t->Struct.fields[i];
  2104. if (!is_type_simple_compare(f->type)) {
  2105. return false;
  2106. }
  2107. }
  2108. return true;
  2109. case Type_Union:
  2110. for_array(i, t->Union.variants) {
  2111. Type *v = t->Union.variants[i];
  2112. if (!is_type_simple_compare(v)) {
  2113. return false;
  2114. }
  2115. }
  2116. // make it dumb on purpose
  2117. return t->Union.variants.count == 1;
  2118. case Type_SimdVector:
  2119. return is_type_simple_compare(t->SimdVector.elem);
  2120. }
  2121. return false;
  2122. }
  2123. gb_internal bool is_type_load_safe(Type *type) {
  2124. GB_ASSERT(type != nullptr);
  2125. type = core_type(core_array_type(type));
  2126. switch (type->kind) {
  2127. case Type_Basic:
  2128. return (type->Basic.flags & (BasicFlag_Boolean|BasicFlag_Numeric|BasicFlag_Rune)) != 0;
  2129. case Type_BitSet:
  2130. if (type->BitSet.underlying) {
  2131. return is_type_load_safe(type->BitSet.underlying);
  2132. }
  2133. return true;
  2134. case Type_RelativePointer:
  2135. case Type_RelativeMultiPointer:
  2136. return true;
  2137. case Type_Pointer:
  2138. case Type_MultiPointer:
  2139. case Type_Slice:
  2140. case Type_DynamicArray:
  2141. case Type_Proc:
  2142. case Type_SoaPointer:
  2143. return false;
  2144. case Type_Enum:
  2145. case Type_EnumeratedArray:
  2146. case Type_Array:
  2147. case Type_SimdVector:
  2148. case Type_Matrix:
  2149. GB_PANIC("should never be hit");
  2150. return false;
  2151. case Type_Struct:
  2152. for_array(i, type->Struct.fields) {
  2153. if (!is_type_load_safe(type->Struct.fields[i]->type)) {
  2154. return false;
  2155. }
  2156. }
  2157. return type_size_of(type) > 0;
  2158. case Type_Union:
  2159. for_array(i, type->Union.variants) {
  2160. if (!is_type_load_safe(type->Union.variants[i])) {
  2161. return false;
  2162. }
  2163. }
  2164. return type_size_of(type) > 0;
  2165. }
  2166. return false;
  2167. }
  2168. gb_internal String lookup_subtype_polymorphic_field(Type *dst, Type *src) {
  2169. Type *prev_src = src;
  2170. // Type *prev_dst = dst;
  2171. src = base_type(type_deref(src));
  2172. // dst = base_type(type_deref(dst));
  2173. bool src_is_ptr = src != prev_src;
  2174. // bool dst_is_ptr = dst != prev_dst;
  2175. GB_ASSERT(is_type_struct(src) || is_type_union(src));
  2176. for_array(i, src->Struct.fields) {
  2177. Entity *f = src->Struct.fields[i];
  2178. if (f->kind == Entity_Variable && f->flags & EntityFlags_IsSubtype) {
  2179. if (are_types_identical(dst, f->type)) {
  2180. return f->token.string;
  2181. }
  2182. if (src_is_ptr && is_type_pointer(dst)) {
  2183. if (are_types_identical(type_deref(dst), f->type)) {
  2184. return f->token.string;
  2185. }
  2186. }
  2187. if ((f->flags & EntityFlag_Using) != 0 && is_type_struct(f->type)) {
  2188. String name = lookup_subtype_polymorphic_field(dst, f->type);
  2189. if (name.len > 0) {
  2190. return name;
  2191. }
  2192. }
  2193. }
  2194. }
  2195. return str_lit("");
  2196. }
  2197. gb_internal bool lookup_subtype_polymorphic_selection(Type *dst, Type *src, Selection *sel) {
  2198. Type *prev_src = src;
  2199. // Type *prev_dst = dst;
  2200. src = base_type(type_deref(src));
  2201. // dst = base_type(type_deref(dst));
  2202. bool src_is_ptr = src != prev_src;
  2203. // bool dst_is_ptr = dst != prev_dst;
  2204. GB_ASSERT(is_type_struct(src) || is_type_union(src));
  2205. for_array(i, src->Struct.fields) {
  2206. Entity *f = src->Struct.fields[i];
  2207. if (f->kind == Entity_Variable && f->flags & EntityFlags_IsSubtype) {
  2208. if (are_types_identical(dst, f->type)) {
  2209. array_add(&sel->index, cast(i32)i);
  2210. sel->entity = f;
  2211. return true;
  2212. }
  2213. if (src_is_ptr && is_type_pointer(dst)) {
  2214. if (are_types_identical(type_deref(dst), f->type)) {
  2215. array_add(&sel->index, cast(i32)i);
  2216. sel->indirect = true;
  2217. sel->entity = f;
  2218. return true;
  2219. }
  2220. }
  2221. if ((f->flags & EntityFlag_Using) != 0 && is_type_struct(f->type)) {
  2222. String name = lookup_subtype_polymorphic_field(dst, f->type);
  2223. if (name.len > 0) {
  2224. array_add(&sel->index, cast(i32)i);
  2225. return lookup_subtype_polymorphic_selection(dst, f->type, sel);
  2226. }
  2227. }
  2228. }
  2229. }
  2230. return false;
  2231. }
  2232. gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names);
  2233. gb_internal bool are_types_identical(Type *x, Type *y) {
  2234. if (x == y) {
  2235. return true;
  2236. }
  2237. if ((x == nullptr && y != nullptr) ||
  2238. (x != nullptr && y == nullptr)) {
  2239. return false;
  2240. }
  2241. if (x->kind == Type_Named) {
  2242. Entity *e = x->Named.type_name;
  2243. if (e->TypeName.is_type_alias) {
  2244. x = x->Named.base;
  2245. }
  2246. }
  2247. if (y->kind == Type_Named) {
  2248. Entity *e = y->Named.type_name;
  2249. if (e->TypeName.is_type_alias) {
  2250. y = y->Named.base;
  2251. }
  2252. }
  2253. if (x->kind != y->kind) {
  2254. return false;
  2255. }
  2256. return are_types_identical_internal(x, y, false);
  2257. }
  2258. gb_internal bool are_types_identical_unique_tuples(Type *x, Type *y) {
  2259. if (x == y) {
  2260. return true;
  2261. }
  2262. if (!x | !y) {
  2263. return false;
  2264. }
  2265. if (x->kind == Type_Named) {
  2266. Entity *e = x->Named.type_name;
  2267. if (e->TypeName.is_type_alias) {
  2268. x = x->Named.base;
  2269. }
  2270. }
  2271. if (y->kind == Type_Named) {
  2272. Entity *e = y->Named.type_name;
  2273. if (e->TypeName.is_type_alias) {
  2274. y = y->Named.base;
  2275. }
  2276. }
  2277. if (x->kind != y->kind) {
  2278. return false;
  2279. }
  2280. return are_types_identical_internal(x, y, true);
  2281. }
  2282. gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names) {
  2283. if (x == y) {
  2284. return true;
  2285. }
  2286. if (!x | !y) {
  2287. return false;
  2288. }
  2289. #if 0
  2290. if (x->kind == Type_Named) {
  2291. Entity *e = x->Named.type_name;
  2292. if (e->TypeName.is_type_alias) {
  2293. x = x->Named.base;
  2294. }
  2295. }
  2296. if (y->kind == Type_Named) {
  2297. Entity *e = y->Named.type_name;
  2298. if (e->TypeName.is_type_alias) {
  2299. y = y->Named.base;
  2300. }
  2301. }
  2302. if (x->kind != y->kind) {
  2303. return false;
  2304. }
  2305. #endif
  2306. switch (x->kind) {
  2307. case Type_Generic:
  2308. return are_types_identical(x->Generic.specialized, y->Generic.specialized);
  2309. case Type_Basic:
  2310. return x->Basic.kind == y->Basic.kind;
  2311. case Type_EnumeratedArray:
  2312. return are_types_identical(x->EnumeratedArray.index, y->EnumeratedArray.index) &&
  2313. are_types_identical(x->EnumeratedArray.elem, y->EnumeratedArray.elem);
  2314. case Type_Array:
  2315. return (x->Array.count == y->Array.count) && are_types_identical(x->Array.elem, y->Array.elem);
  2316. case Type_Matrix:
  2317. return x->Matrix.row_count == y->Matrix.row_count &&
  2318. x->Matrix.column_count == y->Matrix.column_count &&
  2319. are_types_identical(x->Matrix.elem, y->Matrix.elem);
  2320. case Type_DynamicArray:
  2321. return are_types_identical(x->DynamicArray.elem, y->DynamicArray.elem);
  2322. case Type_Slice:
  2323. return are_types_identical(x->Slice.elem, y->Slice.elem);
  2324. case Type_BitSet:
  2325. if (are_types_identical(x->BitSet.elem, y->BitSet.elem) &&
  2326. are_types_identical(x->BitSet.underlying, y->BitSet.underlying)) {
  2327. if (is_type_enum(x->BitSet.elem)) {
  2328. return true;
  2329. }
  2330. return x->BitSet.lower == y->BitSet.lower && x->BitSet.upper == y->BitSet.upper;
  2331. }
  2332. return false;
  2333. case Type_Enum:
  2334. return x == y; // NOTE(bill): All enums are unique
  2335. case Type_Union:
  2336. if (x->Union.variants.count == y->Union.variants.count &&
  2337. x->Union.custom_align == y->Union.custom_align &&
  2338. x->Union.kind == y->Union.kind) {
  2339. // NOTE(bill): zeroth variant is nullptr
  2340. for_array(i, x->Union.variants) {
  2341. if (!are_types_identical(x->Union.variants[i], y->Union.variants[i])) {
  2342. return false;
  2343. }
  2344. }
  2345. return true;
  2346. }
  2347. break;
  2348. case Type_Struct:
  2349. if (x->Struct.is_raw_union == y->Struct.is_raw_union &&
  2350. x->Struct.is_no_copy == y->Struct.is_no_copy &&
  2351. x->Struct.fields.count == y->Struct.fields.count &&
  2352. x->Struct.is_packed == y->Struct.is_packed &&
  2353. x->Struct.custom_align == y->Struct.custom_align &&
  2354. x->Struct.soa_kind == y->Struct.soa_kind &&
  2355. x->Struct.soa_count == y->Struct.soa_count &&
  2356. are_types_identical(x->Struct.soa_elem, y->Struct.soa_elem)) {
  2357. for_array(i, x->Struct.fields) {
  2358. Entity *xf = x->Struct.fields[i];
  2359. Entity *yf = y->Struct.fields[i];
  2360. if (xf->kind != yf->kind) {
  2361. return false;
  2362. }
  2363. if (!are_types_identical(xf->type, yf->type)) {
  2364. return false;
  2365. }
  2366. if (xf->token.string != yf->token.string) {
  2367. return false;
  2368. }
  2369. if (x->Struct.tags[i] != y->Struct.tags[i]) {
  2370. return false;
  2371. }
  2372. u64 xf_flags = (xf->flags&EntityFlags_IsSubtype);
  2373. u64 yf_flags = (yf->flags&EntityFlags_IsSubtype);
  2374. if (xf_flags != yf_flags) {
  2375. return false;
  2376. }
  2377. }
  2378. return are_types_identical(x->Struct.polymorphic_params, y->Struct.polymorphic_params);
  2379. }
  2380. break;
  2381. case Type_Pointer:
  2382. return are_types_identical(x->Pointer.elem, y->Pointer.elem);
  2383. case Type_MultiPointer:
  2384. return are_types_identical(x->MultiPointer.elem, y->MultiPointer.elem);
  2385. case Type_SoaPointer:
  2386. return are_types_identical(x->SoaPointer.elem, y->SoaPointer.elem);
  2387. case Type_Named:
  2388. return x->Named.type_name == y->Named.type_name;
  2389. case Type_Tuple:
  2390. if (x->Tuple.variables.count == y->Tuple.variables.count &&
  2391. x->Tuple.is_packed == y->Tuple.is_packed) {
  2392. for_array(i, x->Tuple.variables) {
  2393. Entity *xe = x->Tuple.variables[i];
  2394. Entity *ye = y->Tuple.variables[i];
  2395. if (xe->kind != ye->kind || !are_types_identical(xe->type, ye->type)) {
  2396. return false;
  2397. }
  2398. if (check_tuple_names) {
  2399. if (xe->token.string != ye->token.string) {
  2400. return false;
  2401. }
  2402. }
  2403. if (xe->kind == Entity_Constant && !compare_exact_values(Token_CmpEq, xe->Constant.value, ye->Constant.value)) {
  2404. // NOTE(bill): This is needed for polymorphic procedures
  2405. return false;
  2406. }
  2407. }
  2408. return true;
  2409. }
  2410. break;
  2411. case Type_Proc:
  2412. return x->Proc.calling_convention == y->Proc.calling_convention &&
  2413. x->Proc.c_vararg == y->Proc.c_vararg &&
  2414. x->Proc.variadic == y->Proc.variadic &&
  2415. x->Proc.diverging == y->Proc.diverging &&
  2416. x->Proc.optional_ok == y->Proc.optional_ok &&
  2417. are_types_identical_internal(x->Proc.params, y->Proc.params, check_tuple_names) &&
  2418. are_types_identical_internal(x->Proc.results, y->Proc.results, check_tuple_names);
  2419. case Type_Map:
  2420. return are_types_identical(x->Map.key, y->Map.key) &&
  2421. are_types_identical(x->Map.value, y->Map.value);
  2422. case Type_SimdVector:
  2423. if (x->SimdVector.count == y->SimdVector.count) {
  2424. return are_types_identical(x->SimdVector.elem, y->SimdVector.elem);
  2425. }
  2426. break;
  2427. }
  2428. return false;
  2429. }
  2430. gb_internal Type *default_type(Type *type) {
  2431. if (type == nullptr) {
  2432. return t_invalid;
  2433. }
  2434. if (type->kind == Type_Basic) {
  2435. switch (type->Basic.kind) {
  2436. case Basic_UntypedBool: return t_bool;
  2437. case Basic_UntypedInteger: return t_int;
  2438. case Basic_UntypedFloat: return t_f64;
  2439. case Basic_UntypedComplex: return t_complex128;
  2440. case Basic_UntypedQuaternion: return t_quaternion256;
  2441. case Basic_UntypedString: return t_string;
  2442. case Basic_UntypedRune: return t_rune;
  2443. }
  2444. }
  2445. return type;
  2446. }
  2447. gb_internal bool union_variant_index_types_equal(Type *v, Type *vt) {
  2448. if (are_types_identical(v, vt)) {
  2449. return true;
  2450. }
  2451. if (is_type_proc(v) && is_type_proc(vt)) {
  2452. return are_types_identical(base_type(v), base_type(vt));
  2453. }
  2454. return false;
  2455. }
  2456. gb_internal i64 union_variant_index(Type *u, Type *v) {
  2457. u = base_type(u);
  2458. GB_ASSERT(u->kind == Type_Union);
  2459. for_array(i, u->Union.variants) {
  2460. Type *vt = u->Union.variants[i];
  2461. if (union_variant_index_types_equal(v, vt)) {
  2462. if (u->Union.kind == UnionType_no_nil) {
  2463. return cast(i64)(i+0);
  2464. } else {
  2465. return cast(i64)(i+1);
  2466. }
  2467. }
  2468. }
  2469. return 0;
  2470. }
  2471. gb_internal i64 union_tag_size(Type *u) {
  2472. u = base_type(u);
  2473. GB_ASSERT(u->kind == Type_Union);
  2474. if (u->Union.tag_size > 0) {
  2475. return u->Union.tag_size;
  2476. }
  2477. u64 n = cast(u64)u->Union.variants.count;
  2478. if (n == 0) {
  2479. return 0;
  2480. }
  2481. i64 max_align = 1;
  2482. if (u->Union.variants.count < 1ull<<8) {
  2483. max_align = 1;
  2484. } else if (u->Union.variants.count < 1ull<<16) {
  2485. max_align = 2;
  2486. } else if (u->Union.variants.count < 1ull<<32) {
  2487. max_align = 4;
  2488. } else {
  2489. compiler_error("how many variants do you have?! %lld", cast(long long)u->Union.variants.count);
  2490. }
  2491. for_array(i, u->Union.variants) {
  2492. Type *variant_type = u->Union.variants[i];
  2493. i64 align = type_align_of(variant_type);
  2494. if (max_align < align) {
  2495. max_align = align;
  2496. }
  2497. }
  2498. u->Union.tag_size = cast(i16)gb_min3(max_align, build_context.max_align, 8);
  2499. return u->Union.tag_size;
  2500. }
  2501. gb_internal Type *union_tag_type(Type *u) {
  2502. i64 s = union_tag_size(u);
  2503. switch (s) {
  2504. case 0: return t_u8;
  2505. case 1: return t_u8;
  2506. case 2: return t_u16;
  2507. case 4: return t_u32;
  2508. case 8: return t_u64;
  2509. }
  2510. GB_PANIC("Invalid union_tag_size");
  2511. return t_uint;
  2512. }
  2513. enum ProcTypeOverloadKind {
  2514. ProcOverload_Identical, // The types are identical
  2515. ProcOverload_CallingConvention,
  2516. ProcOverload_ParamCount,
  2517. ProcOverload_ParamVariadic,
  2518. ProcOverload_ParamTypes,
  2519. ProcOverload_ResultCount,
  2520. ProcOverload_ResultTypes,
  2521. ProcOverload_Polymorphic,
  2522. ProcOverload_NotProcedure,
  2523. };
  2524. gb_internal ProcTypeOverloadKind are_proc_types_overload_safe(Type *x, Type *y) {
  2525. if (x == nullptr && y == nullptr) return ProcOverload_NotProcedure;
  2526. if (x == nullptr && y != nullptr) return ProcOverload_NotProcedure;
  2527. if (x != nullptr && y == nullptr) return ProcOverload_NotProcedure;
  2528. if (!is_type_proc(x)) return ProcOverload_NotProcedure;
  2529. if (!is_type_proc(y)) return ProcOverload_NotProcedure;
  2530. TypeProc px = base_type(x)->Proc;
  2531. TypeProc py = base_type(y)->Proc;
  2532. // if (px.calling_convention != py.calling_convention) {
  2533. // return ProcOverload_CallingConvention;
  2534. // }
  2535. // if (px.is_polymorphic != py.is_polymorphic) {
  2536. // return ProcOverload_Polymorphic;
  2537. // }
  2538. if (px.param_count != py.param_count) {
  2539. return ProcOverload_ParamCount;
  2540. }
  2541. for (isize i = 0; i < px.param_count; i++) {
  2542. Entity *ex = px.params->Tuple.variables[i];
  2543. Entity *ey = py.params->Tuple.variables[i];
  2544. if (!are_types_identical(ex->type, ey->type)) {
  2545. return ProcOverload_ParamTypes;
  2546. }
  2547. }
  2548. // IMPORTANT TODO(bill): Determine the rules for overloading procedures with variadic parameters
  2549. if (px.variadic != py.variadic) {
  2550. return ProcOverload_ParamVariadic;
  2551. }
  2552. if (px.is_polymorphic != py.is_polymorphic) {
  2553. return ProcOverload_Polymorphic;
  2554. }
  2555. if (px.result_count != py.result_count) {
  2556. return ProcOverload_ResultCount;
  2557. }
  2558. for (isize i = 0; i < px.result_count; i++) {
  2559. Entity *ex = px.results->Tuple.variables[i];
  2560. Entity *ey = py.results->Tuple.variables[i];
  2561. if (!are_types_identical(ex->type, ey->type)) {
  2562. return ProcOverload_ResultTypes;
  2563. }
  2564. }
  2565. if (px.params != nullptr && py.params != nullptr) {
  2566. Entity *ex = px.params->Tuple.variables[0];
  2567. Entity *ey = py.params->Tuple.variables[0];
  2568. bool ok = are_types_identical(ex->type, ey->type);
  2569. if (ok) {
  2570. }
  2571. }
  2572. return ProcOverload_Identical;
  2573. }
  2574. gb_internal Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident=false);
  2575. gb_internal Selection lookup_field(Type *type_, String field_name, bool is_type, bool allow_blank_ident=false) {
  2576. return lookup_field_with_selection(type_, field_name, is_type, empty_selection, allow_blank_ident);
  2577. }
  2578. gb_internal Selection lookup_field_from_index(Type *type, i64 index) {
  2579. GB_ASSERT(is_type_struct(type) || is_type_union(type) || is_type_tuple(type));
  2580. type = base_type(type);
  2581. gbAllocator a = permanent_allocator();
  2582. isize max_count = 0;
  2583. switch (type->kind) {
  2584. case Type_Struct:
  2585. rw_mutex_shared_lock(&type->Struct.fields_mutex);
  2586. max_count = type->Struct.fields.count;
  2587. rw_mutex_shared_unlock(&type->Struct.fields_mutex);
  2588. break;
  2589. case Type_Tuple: max_count = type->Tuple.variables.count; break;
  2590. }
  2591. if (index >= max_count) {
  2592. return empty_selection;
  2593. }
  2594. switch (type->kind) {
  2595. case Type_Struct: {
  2596. rw_mutex_shared_lock(&type->Struct.fields_mutex);
  2597. defer (rw_mutex_shared_unlock(&type->Struct.fields_mutex));
  2598. for (isize i = 0; i < max_count; i++) {
  2599. Entity *f = type->Struct.fields[i];
  2600. if (f->kind == Entity_Variable) {
  2601. if (f->Variable.field_index == index) {
  2602. auto sel_array = array_make<i32>(a, 1);
  2603. sel_array[0] = cast(i32)i;
  2604. return make_selection(f, sel_array, false);
  2605. }
  2606. }
  2607. }
  2608. } break;
  2609. case Type_Tuple:
  2610. for (isize i = 0; i < max_count; i++) {
  2611. Entity *f = type->Tuple.variables[i];
  2612. if (i == index) {
  2613. auto sel_array = array_make<i32>(a, 1);
  2614. sel_array[0] = cast(i32)i;
  2615. return make_selection(f, sel_array, false);
  2616. }
  2617. }
  2618. break;
  2619. }
  2620. GB_PANIC("Illegal index");
  2621. return empty_selection;
  2622. }
  2623. gb_internal Entity *scope_lookup_current(Scope *s, String const &name);
  2624. gb_internal bool has_type_got_objc_class_attribute(Type *t);
  2625. gb_internal Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident) {
  2626. GB_ASSERT(type_ != nullptr);
  2627. if (!allow_blank_ident && is_blank_ident(field_name)) {
  2628. return empty_selection;
  2629. }
  2630. Type *type = type_deref(type_);
  2631. bool is_ptr = type != type_;
  2632. sel.indirect = sel.indirect || is_ptr;
  2633. Type *original_type = type;
  2634. type = base_type(type);
  2635. if (is_type) {
  2636. if (has_type_got_objc_class_attribute(original_type) && original_type->kind == Type_Named) {
  2637. Entity *e = original_type->Named.type_name;
  2638. GB_ASSERT(e->kind == Entity_TypeName);
  2639. if (e->TypeName.objc_metadata) {
  2640. auto *md = e->TypeName.objc_metadata;
  2641. mutex_lock(md->mutex);
  2642. defer (mutex_unlock(md->mutex));
  2643. for (TypeNameObjCMetadataEntry const &entry : md->type_entries) {
  2644. GB_ASSERT(entry.entity->kind == Entity_Procedure);
  2645. if (entry.name == field_name) {
  2646. sel.entity = entry.entity;
  2647. sel.pseudo_field = true;
  2648. return sel;
  2649. }
  2650. }
  2651. }
  2652. if (type->kind == Type_Struct) {
  2653. rw_mutex_shared_lock(&type->Struct.fields_mutex);
  2654. isize field_count = type->Struct.fields.count;
  2655. rw_mutex_shared_unlock(&type->Struct.fields_mutex);
  2656. if (field_count != 0) for_array(i, type->Struct.fields) {
  2657. Entity *f = type->Struct.fields[i];
  2658. if (f->flags&EntityFlag_Using) {
  2659. sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
  2660. if (sel.entity) {
  2661. return sel;
  2662. }
  2663. }
  2664. }
  2665. }
  2666. }
  2667. if (is_type_enum(type)) {
  2668. // NOTE(bill): These may not have been added yet, so check in case
  2669. for_array(i, type->Enum.fields) {
  2670. Entity *f = type->Enum.fields[i];
  2671. GB_ASSERT(f->kind == Entity_Constant);
  2672. String str = f->token.string;
  2673. if (field_name == str) {
  2674. sel.entity = f;
  2675. // selection_add_index(&sel, i);
  2676. return sel;
  2677. }
  2678. }
  2679. }
  2680. if (type->kind == Type_Struct) {
  2681. rw_mutex_shared_lock(&type->Struct.fields_mutex);
  2682. Scope *s = type->Struct.scope;
  2683. rw_mutex_shared_unlock(&type->Struct.fields_mutex);
  2684. if (s != nullptr) {
  2685. Entity *found = scope_lookup_current(s, field_name);
  2686. if (found != nullptr && found->kind != Entity_Variable) {
  2687. sel.entity = found;
  2688. return sel;
  2689. }
  2690. }
  2691. } else if (type->kind == Type_Union) {
  2692. Scope *s = type->Union.scope;
  2693. if (s != nullptr) {
  2694. Entity *found = scope_lookup_current(s, field_name);
  2695. if (found != nullptr && found->kind != Entity_Variable) {
  2696. sel.entity = found;
  2697. return sel;
  2698. }
  2699. }
  2700. } else if (type->kind == Type_BitSet) {
  2701. return lookup_field_with_selection(type->BitSet.elem, field_name, true, sel, allow_blank_ident);
  2702. }
  2703. if (type->kind == Type_Generic && type->Generic.specialized != nullptr) {
  2704. Type *specialized = type->Generic.specialized;
  2705. return lookup_field_with_selection(specialized, field_name, is_type, sel, allow_blank_ident);
  2706. }
  2707. } else if (type->kind == Type_Union) {
  2708. } else if (type->kind == Type_Struct) {
  2709. if (has_type_got_objc_class_attribute(original_type) && original_type->kind == Type_Named) {
  2710. Entity *e = original_type->Named.type_name;
  2711. GB_ASSERT(e->kind == Entity_TypeName);
  2712. if (e->TypeName.objc_metadata) {
  2713. auto *md = e->TypeName.objc_metadata;
  2714. mutex_lock(md->mutex);
  2715. defer (mutex_unlock(md->mutex));
  2716. for (TypeNameObjCMetadataEntry const &entry : md->value_entries) {
  2717. GB_ASSERT(entry.entity->kind == Entity_Procedure || entry.entity->kind == Entity_ProcGroup);
  2718. if (entry.name == field_name) {
  2719. sel.entity = entry.entity;
  2720. sel.pseudo_field = true;
  2721. return sel;
  2722. }
  2723. }
  2724. }
  2725. }
  2726. rw_mutex_shared_lock(&type->Struct.fields_mutex);
  2727. isize field_count = type->Struct.fields.count;
  2728. rw_mutex_shared_unlock(&type->Struct.fields_mutex);
  2729. if (field_count != 0) for_array(i, type->Struct.fields) {
  2730. Entity *f = type->Struct.fields[i];
  2731. if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
  2732. continue;
  2733. }
  2734. String str = f->token.string;
  2735. if (field_name == str) {
  2736. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  2737. sel.entity = f;
  2738. return sel;
  2739. }
  2740. if (f->flags & EntityFlag_Using) {
  2741. isize prev_count = sel.index.count;
  2742. bool prev_indirect = sel.indirect;
  2743. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  2744. sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
  2745. if (sel.entity != nullptr) {
  2746. if (is_type_pointer(f->type)) {
  2747. sel.indirect = true;
  2748. }
  2749. return sel;
  2750. }
  2751. sel.index.count = prev_count;
  2752. sel.indirect = prev_indirect;
  2753. }
  2754. }
  2755. bool is_soa = type->Struct.soa_kind != StructSoa_None;
  2756. bool is_soa_of_array = is_soa && is_type_array(type->Struct.soa_elem);
  2757. if (is_soa_of_array) {
  2758. String mapped_field_name = {};
  2759. if (field_name == "r") mapped_field_name = str_lit("x");
  2760. else if (field_name == "g") mapped_field_name = str_lit("y");
  2761. else if (field_name == "b") mapped_field_name = str_lit("z");
  2762. else if (field_name == "a") mapped_field_name = str_lit("w");
  2763. return lookup_field_with_selection(type, mapped_field_name, is_type, sel, allow_blank_ident);
  2764. }
  2765. } else if (type->kind == Type_Basic) {
  2766. switch (type->Basic.kind) {
  2767. case Basic_any: {
  2768. #if 1
  2769. String data_str = str_lit("data");
  2770. String id_str = str_lit("id");
  2771. gb_local_persist Entity *entity__any_data = alloc_entity_field(nullptr, make_token_ident(data_str), t_rawptr, false, 0);
  2772. gb_local_persist Entity *entity__any_id = alloc_entity_field(nullptr, make_token_ident(id_str), t_typeid, false, 1);
  2773. if (field_name == data_str) {
  2774. selection_add_index(&sel, 0);
  2775. sel.entity = entity__any_data;
  2776. return sel;
  2777. } else if (field_name == id_str) {
  2778. selection_add_index(&sel, 1);
  2779. sel.entity = entity__any_id;
  2780. return sel;
  2781. }
  2782. #endif
  2783. } break;
  2784. case Basic_quaternion64: {
  2785. // @QuaternionLayout
  2786. gb_local_persist String w = str_lit("w");
  2787. gb_local_persist String x = str_lit("x");
  2788. gb_local_persist String y = str_lit("y");
  2789. gb_local_persist String z = str_lit("z");
  2790. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_f16, false, 3);
  2791. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_f16, false, 0);
  2792. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_f16, false, 1);
  2793. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_f16, false, 2);
  2794. if (field_name == w) {
  2795. selection_add_index(&sel, 3);
  2796. sel.entity = entity__w;
  2797. return sel;
  2798. } else if (field_name == x) {
  2799. selection_add_index(&sel, 0);
  2800. sel.entity = entity__x;
  2801. return sel;
  2802. } else if (field_name == y) {
  2803. selection_add_index(&sel, 1);
  2804. sel.entity = entity__y;
  2805. return sel;
  2806. } else if (field_name == z) {
  2807. selection_add_index(&sel, 2);
  2808. sel.entity = entity__z;
  2809. return sel;
  2810. }
  2811. } break;
  2812. case Basic_quaternion128: {
  2813. // @QuaternionLayout
  2814. gb_local_persist String w = str_lit("w");
  2815. gb_local_persist String x = str_lit("x");
  2816. gb_local_persist String y = str_lit("y");
  2817. gb_local_persist String z = str_lit("z");
  2818. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_f32, false, 3);
  2819. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_f32, false, 0);
  2820. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_f32, false, 1);
  2821. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_f32, false, 2);
  2822. if (field_name == w) {
  2823. selection_add_index(&sel, 3);
  2824. sel.entity = entity__w;
  2825. return sel;
  2826. } else if (field_name == x) {
  2827. selection_add_index(&sel, 0);
  2828. sel.entity = entity__x;
  2829. return sel;
  2830. } else if (field_name == y) {
  2831. selection_add_index(&sel, 1);
  2832. sel.entity = entity__y;
  2833. return sel;
  2834. } else if (field_name == z) {
  2835. selection_add_index(&sel, 2);
  2836. sel.entity = entity__z;
  2837. return sel;
  2838. }
  2839. } break;
  2840. case Basic_quaternion256: {
  2841. // @QuaternionLayout
  2842. gb_local_persist String w = str_lit("w");
  2843. gb_local_persist String x = str_lit("x");
  2844. gb_local_persist String y = str_lit("y");
  2845. gb_local_persist String z = str_lit("z");
  2846. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_f64, false, 3);
  2847. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_f64, false, 0);
  2848. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_f64, false, 1);
  2849. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_f64, false, 2);
  2850. if (field_name == w) {
  2851. selection_add_index(&sel, 3);
  2852. sel.entity = entity__w;
  2853. return sel;
  2854. } else if (field_name == x) {
  2855. selection_add_index(&sel, 0);
  2856. sel.entity = entity__x;
  2857. return sel;
  2858. } else if (field_name == y) {
  2859. selection_add_index(&sel, 1);
  2860. sel.entity = entity__y;
  2861. return sel;
  2862. } else if (field_name == z) {
  2863. selection_add_index(&sel, 2);
  2864. sel.entity = entity__z;
  2865. return sel;
  2866. }
  2867. } break;
  2868. case Basic_UntypedQuaternion: {
  2869. // @QuaternionLayout
  2870. gb_local_persist String w = str_lit("w");
  2871. gb_local_persist String x = str_lit("x");
  2872. gb_local_persist String y = str_lit("y");
  2873. gb_local_persist String z = str_lit("z");
  2874. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_untyped_float, false, 3);
  2875. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_untyped_float, false, 0);
  2876. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_untyped_float, false, 1);
  2877. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_untyped_float, false, 2);
  2878. if (field_name == w) {
  2879. selection_add_index(&sel, 3);
  2880. sel.entity = entity__w;
  2881. return sel;
  2882. } else if (field_name == x) {
  2883. selection_add_index(&sel, 0);
  2884. sel.entity = entity__x;
  2885. return sel;
  2886. } else if (field_name == y) {
  2887. selection_add_index(&sel, 1);
  2888. sel.entity = entity__y;
  2889. return sel;
  2890. } else if (field_name == z) {
  2891. selection_add_index(&sel, 2);
  2892. sel.entity = entity__z;
  2893. return sel;
  2894. }
  2895. } break;
  2896. }
  2897. return sel;
  2898. } else if (type->kind == Type_Array) {
  2899. if (type->Array.count <= 4) {
  2900. // HACK(bill): Memory leak
  2901. switch (type->Array.count) {
  2902. #define _ARRAY_FIELD_CASE_IF(_length, _name) \
  2903. if (field_name == (_name)) { \
  2904. selection_add_index(&sel, (_length)-1); \
  2905. sel.entity = alloc_entity_array_elem(nullptr, make_token_ident(str_lit(_name)), type->Array.elem, (_length)-1); \
  2906. return sel; \
  2907. }
  2908. #define _ARRAY_FIELD_CASE(_length, _name0, _name1) \
  2909. case (_length): \
  2910. _ARRAY_FIELD_CASE_IF(_length, _name0); \
  2911. _ARRAY_FIELD_CASE_IF(_length, _name1); \
  2912. /*fallthrough*/
  2913. _ARRAY_FIELD_CASE(4, "w", "a");
  2914. _ARRAY_FIELD_CASE(3, "z", "b");
  2915. _ARRAY_FIELD_CASE(2, "y", "g");
  2916. _ARRAY_FIELD_CASE(1, "x", "r");
  2917. default: break;
  2918. #undef _ARRAY_FIELD_CASE
  2919. }
  2920. }
  2921. } else if (type->kind == Type_DynamicArray) {
  2922. GB_ASSERT(t_allocator != nullptr);
  2923. String allocator_str = str_lit("allocator");
  2924. gb_local_persist Entity *entity__allocator = alloc_entity_field(nullptr, make_token_ident(allocator_str), t_allocator, false, 3);
  2925. if (field_name == allocator_str) {
  2926. selection_add_index(&sel, 3);
  2927. sel.entity = entity__allocator;
  2928. return sel;
  2929. }
  2930. } else if (type->kind == Type_Map) {
  2931. GB_ASSERT(t_allocator != nullptr);
  2932. String allocator_str = str_lit("allocator");
  2933. gb_local_persist Entity *entity__allocator = alloc_entity_field(nullptr, make_token_ident(allocator_str), t_allocator, false, 2);
  2934. if (field_name == allocator_str) {
  2935. selection_add_index(&sel, 2);
  2936. sel.entity = entity__allocator;
  2937. return sel;
  2938. }
  2939. }
  2940. return sel;
  2941. }
  2942. gb_internal bool are_struct_fields_reordered(Type *type) {
  2943. type = base_type(type);
  2944. GB_ASSERT(type->kind == Type_Struct);
  2945. type_set_offsets(type);
  2946. if (type->Struct.fields.count == 0) {
  2947. return false;
  2948. }
  2949. GB_ASSERT(type->Struct.offsets != nullptr);
  2950. i64 prev_offset = 0;
  2951. for_array(i, type->Struct.fields) {
  2952. i64 offset = type->Struct.offsets[i];
  2953. if (prev_offset > offset) {
  2954. return true;
  2955. }
  2956. prev_offset = offset;
  2957. }
  2958. return false;
  2959. }
  2960. gb_internal Slice<i32> struct_fields_index_by_increasing_offset(gbAllocator allocator, Type *type) {
  2961. type = base_type(type);
  2962. GB_ASSERT(type->kind == Type_Struct);
  2963. type_set_offsets(type);
  2964. if (type->Struct.fields.count == 0) {
  2965. return {};
  2966. }
  2967. GB_ASSERT(type->Struct.offsets != nullptr);
  2968. auto indices = slice_make<i32>(allocator, type->Struct.fields.count);
  2969. i64 prev_offset = 0;
  2970. bool is_ordered = true;
  2971. for_array(i, indices) {
  2972. indices.data[i] = cast(i32)i;
  2973. i64 offset = type->Struct.offsets[i];
  2974. if (is_ordered && prev_offset > offset) {
  2975. is_ordered = false;
  2976. }
  2977. prev_offset = offset;
  2978. }
  2979. if (!is_ordered) {
  2980. isize n = indices.count;
  2981. for (isize i = 1; i < n; i++) {
  2982. isize j = i;
  2983. while (j > 0 && type->Struct.offsets[indices[j-1]] > type->Struct.offsets[indices[j]]) {
  2984. gb_swap(i32, indices[j-1], indices[j]);
  2985. j -= 1;
  2986. }
  2987. }
  2988. }
  2989. return indices;
  2990. }
  2991. gb_internal i64 type_size_of_internal (Type *t, TypePath *path);
  2992. gb_internal i64 type_align_of_internal(Type *t, TypePath *path);
  2993. gb_internal i64 type_size_of(Type *t);
  2994. gb_internal i64 type_align_of(Type *t);
  2995. gb_internal i64 type_size_of_struct_pretend_is_packed(Type *ot) {
  2996. if (ot == nullptr) {
  2997. return 0;
  2998. }
  2999. Type *t = core_type(ot);
  3000. if (t->kind != Type_Struct) {
  3001. return type_size_of(ot);
  3002. }
  3003. if (t->Struct.is_packed) {
  3004. return type_size_of(ot);
  3005. }
  3006. i64 count = 0, size = 0, align = 1;
  3007. auto const &fields = t->Struct.fields;
  3008. count = fields.count;
  3009. if (count == 0) {
  3010. return 0;
  3011. }
  3012. for_array(i, fields) {
  3013. size += type_size_of(fields[i]->type);
  3014. }
  3015. return align_formula(size, align);
  3016. }
  3017. gb_internal i64 type_size_of(Type *t) {
  3018. if (t == nullptr) {
  3019. return 0;
  3020. }
  3021. i64 size = -1;
  3022. if (t->kind == Type_Basic) {
  3023. GB_ASSERT_MSG(is_type_typed(t), "%s", type_to_string(t));
  3024. switch (t->Basic.kind) {
  3025. case Basic_string: size = 2*build_context.int_size; break;
  3026. case Basic_cstring: size = build_context.ptr_size; break;
  3027. case Basic_any: size = 2*build_context.ptr_size; break;
  3028. case Basic_typeid: size = build_context.ptr_size; break;
  3029. case Basic_int: case Basic_uint:
  3030. size = build_context.int_size;
  3031. break;
  3032. case Basic_uintptr: case Basic_rawptr:
  3033. size = build_context.ptr_size;
  3034. break;
  3035. default:
  3036. size = t->Basic.size;
  3037. break;
  3038. }
  3039. t->cached_size.store(size);
  3040. return size;
  3041. } else if (t->kind != Type_Named && t->cached_size >= 0) {
  3042. return t->cached_size.load();
  3043. } else {
  3044. TypePath path{};
  3045. type_path_init(&path);
  3046. {
  3047. MUTEX_GUARD(&g_type_mutex);
  3048. size = type_size_of_internal(t, &path);
  3049. t->cached_size.store(size);
  3050. }
  3051. type_path_free(&path);
  3052. return size;
  3053. }
  3054. }
  3055. gb_internal i64 type_align_of(Type *t) {
  3056. if (t == nullptr) {
  3057. return 1;
  3058. }
  3059. if (t->kind != Type_Named && t->cached_align > 0) {
  3060. return t->cached_align.load();
  3061. }
  3062. TypePath path{};
  3063. type_path_init(&path);
  3064. {
  3065. MUTEX_GUARD(&g_type_mutex);
  3066. t->cached_align.store(type_align_of_internal(t, &path));
  3067. }
  3068. type_path_free(&path);
  3069. return t->cached_align.load();
  3070. }
  3071. gb_internal i64 type_align_of_internal(Type *t, TypePath *path) {
  3072. GB_ASSERT(path != nullptr);
  3073. if (t->failure) {
  3074. return FAILURE_ALIGNMENT;
  3075. }
  3076. t = base_type(t);
  3077. switch (t->kind) {
  3078. case Type_Basic: {
  3079. GB_ASSERT(is_type_typed(t));
  3080. switch (t->Basic.kind) {
  3081. case Basic_string: return build_context.int_size;
  3082. case Basic_cstring: return build_context.ptr_size;
  3083. case Basic_any: return build_context.ptr_size;
  3084. case Basic_typeid: return build_context.ptr_size;
  3085. case Basic_int: case Basic_uint:
  3086. return build_context.int_size;
  3087. case Basic_uintptr: case Basic_rawptr:
  3088. return build_context.ptr_size;
  3089. case Basic_complex32: case Basic_complex64: case Basic_complex128:
  3090. return type_size_of_internal(t, path) / 2;
  3091. case Basic_quaternion64: case Basic_quaternion128: case Basic_quaternion256:
  3092. return type_size_of_internal(t, path) / 4;
  3093. }
  3094. } break;
  3095. case Type_Array: {
  3096. Type *elem = t->Array.elem;
  3097. bool pop = type_path_push(path, elem);
  3098. if (path->failure) {
  3099. return FAILURE_ALIGNMENT;
  3100. }
  3101. i64 align = type_align_of_internal(elem, path);
  3102. if (pop) type_path_pop(path);
  3103. return align;
  3104. }
  3105. case Type_EnumeratedArray: {
  3106. Type *elem = t->EnumeratedArray.elem;
  3107. bool pop = type_path_push(path, elem);
  3108. if (path->failure) {
  3109. return FAILURE_ALIGNMENT;
  3110. }
  3111. i64 align = type_align_of_internal(elem, path);
  3112. if (pop) type_path_pop(path);
  3113. return align;
  3114. }
  3115. case Type_DynamicArray:
  3116. // data, count, capacity, allocator
  3117. return build_context.int_size;
  3118. case Type_Slice:
  3119. return build_context.int_size;
  3120. case Type_Tuple: {
  3121. i64 max = 1;
  3122. for_array(i, t->Tuple.variables) {
  3123. i64 align = type_align_of_internal(t->Tuple.variables[i]->type, path);
  3124. if (max < align) {
  3125. max = align;
  3126. }
  3127. }
  3128. return max;
  3129. } break;
  3130. case Type_Map:
  3131. return build_context.ptr_size;
  3132. case Type_Enum:
  3133. return type_align_of_internal(t->Enum.base_type, path);
  3134. case Type_Union: {
  3135. if (t->Union.variants.count == 0) {
  3136. return 1;
  3137. }
  3138. if (t->Union.custom_align > 0) {
  3139. return gb_max(t->Union.custom_align, 1);
  3140. }
  3141. i64 max = 1;
  3142. for_array(i, t->Union.variants) {
  3143. Type *variant = t->Union.variants[i];
  3144. bool pop = type_path_push(path, variant);
  3145. if (path->failure) {
  3146. return FAILURE_ALIGNMENT;
  3147. }
  3148. i64 align = type_align_of_internal(variant, path);
  3149. if (pop) type_path_pop(path);
  3150. if (max < align) {
  3151. max = align;
  3152. }
  3153. }
  3154. return max;
  3155. } break;
  3156. case Type_Struct: {
  3157. if (t->Struct.custom_align > 0) {
  3158. return gb_max(t->Struct.custom_align, 1);
  3159. }
  3160. if (t->Struct.is_packed) {
  3161. return 1;
  3162. }
  3163. i64 max = 1;
  3164. for_array(i, t->Struct.fields) {
  3165. Type *field_type = t->Struct.fields[i]->type;
  3166. bool pop = type_path_push(path, field_type);
  3167. if (path->failure) {
  3168. return FAILURE_ALIGNMENT;
  3169. }
  3170. i64 align = type_align_of_internal(field_type, path);
  3171. if (pop) type_path_pop(path);
  3172. if (max < align) {
  3173. max = align;
  3174. }
  3175. }
  3176. return max;
  3177. } break;
  3178. case Type_BitSet: {
  3179. if (t->BitSet.underlying != nullptr) {
  3180. return type_align_of(t->BitSet.underlying);
  3181. }
  3182. i64 bits = t->BitSet.upper - t->BitSet.lower + 1;
  3183. if (bits <= 8) return 1;
  3184. if (bits <= 16) return 2;
  3185. if (bits <= 32) return 4;
  3186. if (bits <= 64) return 8;
  3187. if (bits <= 128) return 16;
  3188. return 8; // NOTE(bill): Could be an invalid range so limit it for now
  3189. }
  3190. case Type_SimdVector: {
  3191. // IMPORTANT TODO(bill): Figure out the alignment of vector types
  3192. return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.max_simd_align*2);
  3193. }
  3194. case Type_Matrix:
  3195. return matrix_align_of(t, path);
  3196. case Type_RelativePointer:
  3197. return type_align_of_internal(t->RelativePointer.base_integer, path);
  3198. case Type_RelativeMultiPointer:
  3199. return type_align_of_internal(t->RelativeMultiPointer.base_integer, path);
  3200. case Type_SoaPointer:
  3201. return build_context.int_size;
  3202. }
  3203. // NOTE(bill): Things that are bigger than build_context.ptr_size, are actually comprised of smaller types
  3204. // TODO(bill): Is this correct for 128-bit types (integers)?
  3205. return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.max_align);
  3206. }
  3207. gb_internal i64 *type_set_offsets_of(Slice<Entity *> const &fields, bool is_packed, bool is_raw_union) {
  3208. gbAllocator a = permanent_allocator();
  3209. auto offsets = gb_alloc_array(a, i64, fields.count);
  3210. i64 curr_offset = 0;
  3211. if (is_raw_union) {
  3212. for_array(i, fields) {
  3213. offsets[i] = 0;
  3214. }
  3215. } else if (is_packed) {
  3216. for_array(i, fields) {
  3217. if (fields[i]->kind != Entity_Variable) {
  3218. offsets[i] = -1;
  3219. } else {
  3220. i64 size = type_size_of(fields[i]->type);
  3221. offsets[i] = curr_offset;
  3222. curr_offset += size;
  3223. }
  3224. }
  3225. } else {
  3226. for_array(i, fields) {
  3227. if (fields[i]->kind != Entity_Variable) {
  3228. offsets[i] = -1;
  3229. } else {
  3230. Type *t = fields[i]->type;
  3231. i64 align = gb_max(type_align_of(t), 1);
  3232. i64 size = gb_max(type_size_of( t), 0);
  3233. curr_offset = align_formula(curr_offset, align);
  3234. offsets[i] = curr_offset;
  3235. curr_offset += size;
  3236. }
  3237. }
  3238. }
  3239. return offsets;
  3240. }
  3241. gb_internal bool type_set_offsets(Type *t) {
  3242. t = base_type(t);
  3243. if (t->kind == Type_Struct) {
  3244. MUTEX_GUARD(&t->Struct.offset_mutex);
  3245. if (!t->Struct.are_offsets_set) {
  3246. t->Struct.are_offsets_being_processed = true;
  3247. t->Struct.offsets = type_set_offsets_of(t->Struct.fields, t->Struct.is_packed, t->Struct.is_raw_union);
  3248. t->Struct.are_offsets_being_processed = false;
  3249. t->Struct.are_offsets_set = true;
  3250. return true;
  3251. }
  3252. } else if (is_type_tuple(t)) {
  3253. MUTEX_GUARD(&t->Tuple.mutex);
  3254. if (!t->Tuple.are_offsets_set) {
  3255. t->Tuple.are_offsets_being_processed = true;
  3256. t->Tuple.offsets = type_set_offsets_of(t->Tuple.variables, t->Tuple.is_packed, false);
  3257. t->Tuple.are_offsets_being_processed = false;
  3258. t->Tuple.are_offsets_set = true;
  3259. return true;
  3260. }
  3261. } else {
  3262. GB_PANIC("Invalid type for setting offsets");
  3263. }
  3264. return false;
  3265. }
  3266. gb_internal i64 type_size_of_internal(Type *t, TypePath *path) {
  3267. if (t->failure) {
  3268. return FAILURE_SIZE;
  3269. }
  3270. switch (t->kind) {
  3271. case Type_Named: {
  3272. bool pop = type_path_push(path, t);
  3273. if (path->failure) {
  3274. return FAILURE_ALIGNMENT;
  3275. }
  3276. i64 size = type_size_of_internal(t->Named.base, path);
  3277. if (pop) type_path_pop(path);
  3278. return size;
  3279. } break;
  3280. case Type_Basic: {
  3281. GB_ASSERT_MSG(is_type_typed(t), "%s", type_to_string(t));
  3282. BasicKind kind = t->Basic.kind;
  3283. i64 size = t->Basic.size;
  3284. if (size > 0) {
  3285. return size;
  3286. }
  3287. switch (kind) {
  3288. case Basic_string: return 2*build_context.int_size;
  3289. case Basic_cstring: return build_context.ptr_size;
  3290. case Basic_any: return 2*build_context.ptr_size;
  3291. case Basic_typeid: return build_context.ptr_size;
  3292. case Basic_int: case Basic_uint:
  3293. return build_context.int_size;
  3294. case Basic_uintptr: case Basic_rawptr:
  3295. return build_context.ptr_size;
  3296. }
  3297. } break;
  3298. case Type_Pointer:
  3299. return build_context.ptr_size;
  3300. case Type_MultiPointer:
  3301. return build_context.ptr_size;
  3302. case Type_SoaPointer:
  3303. return build_context.int_size*2;
  3304. case Type_Array: {
  3305. i64 count, align, size, alignment;
  3306. count = t->Array.count;
  3307. if (count == 0) {
  3308. return 0;
  3309. }
  3310. align = type_align_of_internal(t->Array.elem, path);
  3311. if (path->failure) {
  3312. return FAILURE_SIZE;
  3313. }
  3314. size = type_size_of_internal( t->Array.elem, path);
  3315. alignment = align_formula(size, align);
  3316. return alignment*(count-1) + size;
  3317. } break;
  3318. case Type_EnumeratedArray: {
  3319. i64 count, align, size, alignment;
  3320. count = t->EnumeratedArray.count;
  3321. if (count == 0) {
  3322. return 0;
  3323. }
  3324. align = type_align_of_internal(t->EnumeratedArray.elem, path);
  3325. if (path->failure) {
  3326. return FAILURE_SIZE;
  3327. }
  3328. size = type_size_of_internal( t->EnumeratedArray.elem, path);
  3329. alignment = align_formula(size, align);
  3330. return alignment*(count-1) + size;
  3331. } break;
  3332. case Type_Slice: // ptr + len
  3333. return 2 * build_context.int_size;
  3334. case Type_DynamicArray:
  3335. // data + len + cap + allocator(procedure+data)
  3336. return 3*build_context.int_size + 2*build_context.ptr_size;
  3337. case Type_Map:
  3338. /*
  3339. struct {
  3340. data: uintptr, // 1 word
  3341. size: uintptr, // 1 word
  3342. allocator: runtime.Allocator, // 2 words
  3343. }
  3344. */
  3345. return (1 + 1 + 2)*build_context.ptr_size;
  3346. case Type_Tuple: {
  3347. i64 count, align, size;
  3348. count = t->Tuple.variables.count;
  3349. if (count == 0) {
  3350. return 0;
  3351. }
  3352. align = type_align_of_internal(t, path);
  3353. type_set_offsets(t);
  3354. size = t->Tuple.offsets[cast(isize)count-1] + type_size_of_internal(t->Tuple.variables[cast(isize)count-1]->type, path);
  3355. return align_formula(size, align);
  3356. } break;
  3357. case Type_Enum:
  3358. return type_size_of_internal(t->Enum.base_type, path);
  3359. case Type_Union: {
  3360. if (t->Union.variants.count == 0) {
  3361. return 0;
  3362. }
  3363. i64 align = type_align_of_internal(t, path);
  3364. if (path->failure) {
  3365. return FAILURE_SIZE;
  3366. }
  3367. i64 max = 0;
  3368. i64 field_size = 0;
  3369. for_array(i, t->Union.variants) {
  3370. Type *variant_type = t->Union.variants[i];
  3371. i64 size = type_size_of_internal(variant_type, path);
  3372. if (max < size) {
  3373. max = size;
  3374. }
  3375. }
  3376. i64 size = 0;
  3377. if (is_type_union_maybe_pointer(t)) {
  3378. size = max;
  3379. t->Union.tag_size = 0;
  3380. t->Union.variant_block_size = size;
  3381. } else {
  3382. // NOTE(bill): Align to tag
  3383. i64 tag_size = union_tag_size(t);
  3384. size = align_formula(max, tag_size);
  3385. // NOTE(bill): Calculate the padding between the common fields and the tag
  3386. t->Union.tag_size = cast(i16)tag_size;
  3387. t->Union.variant_block_size = size - field_size;
  3388. size += tag_size;
  3389. }
  3390. return align_formula(size, align);
  3391. } break;
  3392. case Type_Struct: {
  3393. if (t->Struct.is_raw_union) {
  3394. i64 count = t->Struct.fields.count;
  3395. i64 align = type_align_of_internal(t, path);
  3396. if (path->failure) {
  3397. return FAILURE_SIZE;
  3398. }
  3399. i64 max = 0;
  3400. for (isize i = 0; i < count; i++) {
  3401. i64 size = type_size_of_internal(t->Struct.fields[i]->type, path);
  3402. if (max < size) {
  3403. max = size;
  3404. }
  3405. }
  3406. return align_formula(max, align);
  3407. } else {
  3408. i64 count = 0, size = 0, align = 0;
  3409. count = t->Struct.fields.count;
  3410. if (count == 0) {
  3411. return 0;
  3412. }
  3413. align = type_align_of_internal(t, path);
  3414. if (path->failure) {
  3415. return FAILURE_SIZE;
  3416. }
  3417. if (t->Struct.are_offsets_being_processed && t->Struct.offsets == nullptr) {
  3418. type_path_print_illegal_cycle(path, path->path.count-1);
  3419. return FAILURE_SIZE;
  3420. }
  3421. type_set_offsets(t);
  3422. GB_ASSERT(t->Struct.fields.count == 0 || t->Struct.offsets != nullptr);
  3423. size = t->Struct.offsets[cast(isize)count-1] + type_size_of_internal(t->Struct.fields[cast(isize)count-1]->type, path);
  3424. return align_formula(size, align);
  3425. }
  3426. } break;
  3427. case Type_BitSet: {
  3428. if (t->BitSet.underlying != nullptr) {
  3429. return type_size_of(t->BitSet.underlying);
  3430. }
  3431. i64 bits = t->BitSet.upper - t->BitSet.lower + 1;
  3432. if (bits <= 8) return 1;
  3433. if (bits <= 16) return 2;
  3434. if (bits <= 32) return 4;
  3435. if (bits <= 64) return 8;
  3436. if (bits <= 128) return 16;
  3437. return 8; // NOTE(bill): Could be an invalid range so limit it for now
  3438. }
  3439. case Type_SimdVector: {
  3440. i64 count = t->SimdVector.count;
  3441. Type *elem = t->SimdVector.elem;
  3442. return count * type_size_of_internal(elem, path);
  3443. }
  3444. case Type_Matrix: {
  3445. i64 stride_in_bytes = matrix_type_stride_in_bytes(t, path);
  3446. return stride_in_bytes * t->Matrix.column_count;
  3447. }
  3448. case Type_RelativePointer:
  3449. return type_size_of_internal(t->RelativePointer.base_integer, path);
  3450. case Type_RelativeMultiPointer:
  3451. return type_size_of_internal(t->RelativeMultiPointer.base_integer, path);
  3452. }
  3453. // Catch all
  3454. return build_context.ptr_size;
  3455. }
  3456. gb_internal i64 type_offset_of(Type *t, i64 index, Type **field_type_) {
  3457. t = base_type(t);
  3458. switch (t->kind) {
  3459. case Type_Struct:
  3460. type_set_offsets(t);
  3461. if (gb_is_between(index, 0, t->Struct.fields.count-1)) {
  3462. GB_ASSERT(t->Struct.offsets != nullptr);
  3463. if (field_type_) *field_type_ = t->Struct.fields[index]->type;
  3464. return t->Struct.offsets[index];
  3465. }
  3466. break;
  3467. case Type_Tuple:
  3468. type_set_offsets(t);
  3469. if (gb_is_between(index, 0, t->Tuple.variables.count-1)) {
  3470. GB_ASSERT(t->Tuple.offsets != nullptr);
  3471. if (field_type_) *field_type_ = t->Tuple.variables[index]->type;
  3472. i64 offset = t->Tuple.offsets[index];
  3473. GB_ASSERT(offset >= 0);
  3474. return offset;
  3475. }
  3476. break;
  3477. case Type_Array:
  3478. GB_ASSERT(0 <= index && index < t->Array.count);
  3479. return index * type_size_of(t->Array.elem);
  3480. case Type_Basic:
  3481. if (t->Basic.kind == Basic_string) {
  3482. switch (index) {
  3483. case 0:
  3484. if (field_type_) *field_type_ = t_u8_ptr;
  3485. return 0; // data
  3486. case 1:
  3487. if (field_type_) *field_type_ = t_int;
  3488. return build_context.int_size; // len
  3489. }
  3490. } else if (t->Basic.kind == Basic_any) {
  3491. switch (index) {
  3492. case 0:
  3493. if (field_type_) *field_type_ = t_rawptr;
  3494. return 0; // data
  3495. case 1:
  3496. if (field_type_) *field_type_ = t_typeid;
  3497. return build_context.ptr_size; // id
  3498. }
  3499. }
  3500. break;
  3501. case Type_Slice:
  3502. switch (index) {
  3503. case 0:
  3504. if (field_type_) *field_type_ = alloc_type_multi_pointer(t->Slice.elem);
  3505. return 0; // data
  3506. case 1:
  3507. if (field_type_) *field_type_ = t_int;
  3508. return 1*build_context.int_size; // len
  3509. }
  3510. break;
  3511. case Type_DynamicArray:
  3512. switch (index) {
  3513. case 0:
  3514. if (field_type_) *field_type_ = alloc_type_multi_pointer(t->DynamicArray.elem);
  3515. return 0; // data
  3516. case 1:
  3517. if (field_type_) *field_type_ = t_int;
  3518. return 1*build_context.int_size; // len
  3519. case 2:
  3520. if (field_type_) *field_type_ = t_int;
  3521. return 2*build_context.int_size; // cap
  3522. case 3:
  3523. if (field_type_) *field_type_ = t_allocator;
  3524. return 3*build_context.int_size; // allocator
  3525. }
  3526. break;
  3527. case Type_Union:
  3528. if (!is_type_union_maybe_pointer(t)) {
  3529. /* i64 s = */ type_size_of(t);
  3530. switch (index) {
  3531. case -1:
  3532. if (field_type_) *field_type_ = union_tag_type(t);
  3533. union_tag_size(t);
  3534. return t->Union.variant_block_size;
  3535. }
  3536. }
  3537. break;
  3538. }
  3539. GB_ASSERT(index == 0);
  3540. return 0;
  3541. }
  3542. gb_internal i64 type_offset_of_from_selection(Type *type, Selection sel) {
  3543. GB_ASSERT(sel.indirect == false);
  3544. Type *t = type;
  3545. i64 offset = 0;
  3546. for_array(i, sel.index) {
  3547. i32 index = sel.index[i];
  3548. t = base_type(t);
  3549. offset += type_offset_of(t, index);
  3550. if (t->kind == Type_Struct) {
  3551. t = t->Struct.fields[index]->type;
  3552. } else if (t->kind == Type_Array) {
  3553. t = t->Array.elem;
  3554. } else {
  3555. // NOTE(bill): No need to worry about custom types, just need the alignment
  3556. switch (t->kind) {
  3557. case Type_Basic:
  3558. if (t->Basic.kind == Basic_string) {
  3559. switch (index) {
  3560. case 0: t = t_rawptr; break;
  3561. case 1: t = t_int; break;
  3562. }
  3563. } else if (t->Basic.kind == Basic_any) {
  3564. switch (index) {
  3565. case 0: t = t_type_info_ptr; break;
  3566. case 1: t = t_rawptr; break;
  3567. }
  3568. }
  3569. break;
  3570. case Type_Slice:
  3571. switch (index) {
  3572. case 0: t = t_rawptr; break;
  3573. case 1: t = t_int; break;
  3574. case 2: t = t_int; break;
  3575. }
  3576. break;
  3577. case Type_DynamicArray:
  3578. switch (index) {
  3579. case 0: t = t_rawptr; break;
  3580. case 1: t = t_int; break;
  3581. case 2: t = t_int; break;
  3582. case 3: t = t_allocator; break;
  3583. }
  3584. break;
  3585. }
  3586. }
  3587. }
  3588. return offset;
  3589. }
  3590. gb_internal isize check_is_assignable_to_using_subtype(Type *src, Type *dst, isize level = 0, bool src_is_ptr = false) {
  3591. Type *prev_src = src;
  3592. src = type_deref(src);
  3593. if (!src_is_ptr) {
  3594. src_is_ptr = src != prev_src;
  3595. }
  3596. src = base_type(src);
  3597. if (!is_type_struct(src)) {
  3598. return 0;
  3599. }
  3600. for_array(i, src->Struct.fields) {
  3601. Entity *f = src->Struct.fields[i];
  3602. if (f->kind != Entity_Variable || (f->flags&EntityFlags_IsSubtype) == 0) {
  3603. continue;
  3604. }
  3605. if (are_types_identical(f->type, dst)) {
  3606. return level+1;
  3607. }
  3608. if (src_is_ptr && is_type_pointer(dst)) {
  3609. if (are_types_identical(f->type, type_deref(dst))) {
  3610. return level+1;
  3611. }
  3612. }
  3613. isize nested_level = check_is_assignable_to_using_subtype(f->type, dst, level+1, src_is_ptr);
  3614. if (nested_level > 0) {
  3615. return nested_level;
  3616. }
  3617. }
  3618. return 0;
  3619. }
  3620. gb_internal bool is_type_subtype_of(Type *src, Type *dst) {
  3621. if (are_types_identical(src, dst)) {
  3622. return true;
  3623. }
  3624. return 0 < check_is_assignable_to_using_subtype(src, dst, 0, is_type_pointer(src));
  3625. }
  3626. gb_internal bool has_type_got_objc_class_attribute(Type *t) {
  3627. return t->kind == Type_Named && t->Named.type_name != nullptr && t->Named.type_name->TypeName.objc_class_name != "";
  3628. }
  3629. gb_internal bool internal_check_is_assignable_to(Type *src, Type *dst);
  3630. gb_internal bool is_type_objc_object(Type *t) {
  3631. return internal_check_is_assignable_to(t, t_objc_object);
  3632. }
  3633. gb_internal Type *get_struct_field_type(Type *t, isize index) {
  3634. t = base_type(type_deref(t));
  3635. GB_ASSERT(t->kind == Type_Struct);
  3636. return t->Struct.fields[index]->type;
  3637. }
  3638. gb_internal Type *reduce_tuple_to_single_type(Type *original_type) {
  3639. if (original_type != nullptr) {
  3640. Type *t = core_type(original_type);
  3641. if (t->kind == Type_Tuple && t->Tuple.variables.count == 1) {
  3642. return t->Tuple.variables[0]->type;
  3643. }
  3644. }
  3645. return original_type;
  3646. }
  3647. gb_internal Type *alloc_type_tuple_from_field_types(Type **field_types, isize field_count, bool is_packed, bool must_be_tuple) {
  3648. if (field_count == 0) {
  3649. return nullptr;
  3650. }
  3651. if (!must_be_tuple && field_count == 1) {
  3652. return field_types[0];
  3653. }
  3654. Type *t = alloc_type_tuple();
  3655. t->Tuple.variables = slice_make<Entity *>(heap_allocator(), field_count);
  3656. Scope *scope = nullptr;
  3657. for_array(i, t->Tuple.variables) {
  3658. t->Tuple.variables[i] = alloc_entity_param(scope, blank_token, field_types[i], false, false);
  3659. }
  3660. t->Tuple.is_packed = is_packed;
  3661. return t;
  3662. }
  3663. gb_internal Type *alloc_type_proc_from_types(Type **param_types, unsigned param_count, Type *results, bool is_c_vararg, ProcCallingConvention calling_convention) {
  3664. Type *params = alloc_type_tuple_from_field_types(param_types, param_count, false, true);
  3665. isize results_count = 0;
  3666. if (results != nullptr) {
  3667. if (results->kind != Type_Tuple) {
  3668. results = alloc_type_tuple_from_field_types(&results, 1, false, true);
  3669. }
  3670. results_count = results->Tuple.variables.count;
  3671. }
  3672. Scope *scope = nullptr;
  3673. Type *t = alloc_type_proc(scope, params, param_count, results, results_count, false, calling_convention);
  3674. t->Proc.c_vararg = is_c_vararg;
  3675. return t;
  3676. }
  3677. gb_internal gbString write_type_to_string(gbString str, Type *type, bool shorthand=false) {
  3678. if (type == nullptr) {
  3679. return gb_string_appendc(str, "<no type>");
  3680. }
  3681. switch (type->kind) {
  3682. case Type_Basic:
  3683. str = gb_string_append_length(str, type->Basic.name.text, type->Basic.name.len);
  3684. break;
  3685. case Type_Generic:
  3686. if (type->Generic.name.len == 0) {
  3687. if (type->Generic.entity != nullptr) {
  3688. String name = type->Generic.entity->token.string;
  3689. str = gb_string_append_rune(str, '$');
  3690. str = gb_string_append_length(str, name.text, name.len);
  3691. } else {
  3692. str = gb_string_appendc(str, "type");
  3693. }
  3694. } else {
  3695. String name = type->Generic.name;
  3696. str = gb_string_append_rune(str, '$');
  3697. str = gb_string_append_length(str, name.text, name.len);
  3698. if (type->Generic.specialized != nullptr) {
  3699. str = gb_string_append_rune(str, '/');
  3700. str = write_type_to_string(str, type->Generic.specialized);
  3701. }
  3702. }
  3703. break;
  3704. case Type_Pointer:
  3705. str = gb_string_append_rune(str, '^');
  3706. str = write_type_to_string(str, type->Pointer.elem);
  3707. break;
  3708. case Type_SoaPointer:
  3709. str = gb_string_appendc(str, "#soa ^");
  3710. str = write_type_to_string(str, type->SoaPointer.elem);
  3711. break;
  3712. case Type_MultiPointer:
  3713. str = gb_string_appendc(str, "[^]");
  3714. str = write_type_to_string(str, type->Pointer.elem);
  3715. break;
  3716. case Type_EnumeratedArray:
  3717. if (type->EnumeratedArray.is_sparse) {
  3718. str = gb_string_appendc(str, "#sparse");
  3719. }
  3720. str = gb_string_append_rune(str, '[');
  3721. str = write_type_to_string(str, type->EnumeratedArray.index);
  3722. str = gb_string_append_rune(str, ']');
  3723. str = write_type_to_string(str, type->EnumeratedArray.elem);
  3724. break;
  3725. case Type_Array:
  3726. str = gb_string_appendc(str, gb_bprintf("[%d]", cast(int)type->Array.count));
  3727. str = write_type_to_string(str, type->Array.elem);
  3728. break;
  3729. case Type_Slice:
  3730. str = gb_string_appendc(str, "[]");
  3731. str = write_type_to_string(str, type->Array.elem);
  3732. break;
  3733. case Type_DynamicArray:
  3734. str = gb_string_appendc(str, "[dynamic]");
  3735. str = write_type_to_string(str, type->DynamicArray.elem);
  3736. break;
  3737. case Type_Enum:
  3738. str = gb_string_appendc(str, "enum");
  3739. if (type->Enum.base_type != nullptr) {
  3740. str = gb_string_appendc(str, " ");
  3741. str = write_type_to_string(str, type->Enum.base_type);
  3742. }
  3743. str = gb_string_appendc(str, " {");
  3744. for_array(i, type->Enum.fields) {
  3745. Entity *f = type->Enum.fields[i];
  3746. GB_ASSERT(f->kind == Entity_Constant);
  3747. if (i > 0) {
  3748. str = gb_string_appendc(str, ", ");
  3749. }
  3750. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  3751. // str = gb_string_appendc(str, " = ");
  3752. }
  3753. str = gb_string_append_rune(str, '}');
  3754. break;
  3755. case Type_Union:
  3756. str = gb_string_appendc(str, "union");
  3757. switch (type->Union.kind) {
  3758. case UnionType_no_nil: str = gb_string_appendc(str, " #no_nil"); break;
  3759. case UnionType_shared_nil: str = gb_string_appendc(str, " #shared_nil"); break;
  3760. }
  3761. if (type->Union.custom_align != 0) str = gb_string_append_fmt(str, " #align %d", cast(int)type->Union.custom_align);
  3762. str = gb_string_appendc(str, " {");
  3763. for_array(i, type->Union.variants) {
  3764. Type *t = type->Union.variants[i];
  3765. if (i > 0) str = gb_string_appendc(str, ", ");
  3766. str = write_type_to_string(str, t);
  3767. }
  3768. str = gb_string_append_rune(str, '}');
  3769. break;
  3770. case Type_Struct: {
  3771. if (type->Struct.soa_kind != StructSoa_None) {
  3772. switch (type->Struct.soa_kind) {
  3773. case StructSoa_Fixed: str = gb_string_append_fmt(str, "#soa[%d]", cast(int)type->Struct.soa_count); break;
  3774. case StructSoa_Slice: str = gb_string_appendc(str, "#soa[]"); break;
  3775. case StructSoa_Dynamic: str = gb_string_appendc(str, "#soa[dynamic]"); break;
  3776. default: GB_PANIC("Unknown StructSoaKind"); break;
  3777. }
  3778. str = write_type_to_string(str, type->Struct.soa_elem);
  3779. break;
  3780. }
  3781. str = gb_string_appendc(str, "struct");
  3782. if (type->Struct.is_packed) str = gb_string_appendc(str, " #packed");
  3783. if (type->Struct.is_raw_union) str = gb_string_appendc(str, " #raw_union");
  3784. if (type->Struct.is_no_copy) str = gb_string_appendc(str, " #no_copy");
  3785. if (type->Struct.custom_align != 0) str = gb_string_append_fmt(str, " #align %d", cast(int)type->Struct.custom_align);
  3786. str = gb_string_appendc(str, " {");
  3787. if (shorthand && type->Struct.fields.count > 16) {
  3788. str = gb_string_append_fmt(str, "%lld fields...", cast(long long)type->Struct.fields.count);
  3789. } else {
  3790. for_array(i, type->Struct.fields) {
  3791. Entity *f = type->Struct.fields[i];
  3792. GB_ASSERT(f->kind == Entity_Variable);
  3793. if (i > 0) {
  3794. str = gb_string_appendc(str, ", ");
  3795. }
  3796. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  3797. str = gb_string_appendc(str, ": ");
  3798. str = write_type_to_string(str, f->type);
  3799. }
  3800. }
  3801. str = gb_string_append_rune(str, '}');
  3802. } break;
  3803. case Type_Map: {
  3804. str = gb_string_appendc(str, "map[");
  3805. str = write_type_to_string(str, type->Map.key);
  3806. str = gb_string_append_rune(str, ']');
  3807. str = write_type_to_string(str, type->Map.value);
  3808. } break;
  3809. case Type_Named:
  3810. if (type->Named.type_name != nullptr) {
  3811. str = gb_string_append_length(str, type->Named.name.text, type->Named.name.len);
  3812. } else {
  3813. // NOTE(bill): Just in case
  3814. str = gb_string_appendc(str, "<named type>");
  3815. }
  3816. break;
  3817. case Type_Tuple:
  3818. if (type->Tuple.variables.count > 0) {
  3819. isize comma_index = 0;
  3820. for_array(i, type->Tuple.variables) {
  3821. Entity *var = type->Tuple.variables[i];
  3822. if (var == nullptr) {
  3823. continue;
  3824. }
  3825. if (comma_index++ > 0) {
  3826. str = gb_string_appendc(str, ", ");
  3827. }
  3828. String name = var->token.string;
  3829. if (var->kind == Entity_Constant) {
  3830. str = gb_string_appendc(str, "$");
  3831. str = gb_string_append_length(str, name.text, name.len);
  3832. if (!is_type_untyped(var->type)) {
  3833. str = gb_string_appendc(str, ": ");
  3834. str = write_type_to_string(str, var->type);
  3835. str = gb_string_appendc(str, " = ");
  3836. str = write_exact_value_to_string(str, var->Constant.value);
  3837. } else {
  3838. str = gb_string_appendc(str, " := ");
  3839. str = write_exact_value_to_string(str, var->Constant.value);
  3840. }
  3841. continue;
  3842. }
  3843. if (var->kind == Entity_Variable) {
  3844. if (var->flags&EntityFlag_CVarArg) {
  3845. str = gb_string_appendc(str, "#c_vararg ");
  3846. }
  3847. if (var->flags&EntityFlag_Ellipsis) {
  3848. Type *slice = base_type(var->type);
  3849. str = gb_string_appendc(str, "..");
  3850. GB_ASSERT(var->type->kind == Type_Slice);
  3851. str = write_type_to_string(str, slice->Slice.elem);
  3852. } else {
  3853. str = write_type_to_string(str, var->type);
  3854. }
  3855. } else {
  3856. GB_ASSERT(var->kind == Entity_TypeName);
  3857. if (var->type->kind == Type_Generic) {
  3858. str = gb_string_appendc(str, "typeid/");
  3859. str = write_type_to_string(str, var->type);
  3860. } else {
  3861. str = gb_string_appendc(str, "$");
  3862. str = gb_string_append_length(str, name.text, name.len);
  3863. str = gb_string_appendc(str, "=");
  3864. str = write_type_to_string(str, var->type);
  3865. }
  3866. }
  3867. }
  3868. }
  3869. break;
  3870. case Type_Proc:
  3871. str = gb_string_appendc(str, "proc");
  3872. switch (type->Proc.calling_convention) {
  3873. case ProcCC_Odin:
  3874. if (default_calling_convention() != ProcCC_Odin) {
  3875. str = gb_string_appendc(str, " \"odin\" ");
  3876. }
  3877. break;
  3878. case ProcCC_Contextless:
  3879. if (default_calling_convention() != ProcCC_Contextless) {
  3880. str = gb_string_appendc(str, " \"contextless\" ");
  3881. }
  3882. break;
  3883. case ProcCC_CDecl:
  3884. str = gb_string_appendc(str, " \"cdecl\" ");
  3885. break;
  3886. case ProcCC_StdCall:
  3887. str = gb_string_appendc(str, " \"stdcall\" ");
  3888. break;
  3889. case ProcCC_FastCall:
  3890. str = gb_string_appendc(str, " \"fastcall\" ");
  3891. break;
  3892. break;
  3893. case ProcCC_None:
  3894. str = gb_string_appendc(str, " \"none\" ");
  3895. break;
  3896. case ProcCC_Naked:
  3897. str = gb_string_appendc(str, " \"naked\" ");
  3898. break;
  3899. // case ProcCC_VectorCall:
  3900. // str = gb_string_appendc(str, " \"vectorcall\" ");
  3901. // break;
  3902. // case ProcCC_ClrCall:
  3903. // str = gb_string_appendc(str, " \"clrcall\" ");
  3904. // break;
  3905. }
  3906. str = gb_string_appendc(str, "(");
  3907. if (type->Proc.params) {
  3908. str = write_type_to_string(str, type->Proc.params);
  3909. }
  3910. str = gb_string_appendc(str, ")");
  3911. if (type->Proc.results) {
  3912. str = gb_string_appendc(str, " -> ");
  3913. if (type->Proc.results->Tuple.variables.count > 1) {
  3914. str = gb_string_appendc(str, "(");
  3915. }
  3916. str = write_type_to_string(str, type->Proc.results);
  3917. if (type->Proc.results->Tuple.variables.count > 1) {
  3918. str = gb_string_appendc(str, ")");
  3919. }
  3920. }
  3921. break;
  3922. case Type_BitSet:
  3923. str = gb_string_appendc(str, "bit_set[");
  3924. if (is_type_enum(type->BitSet.elem)) {
  3925. str = write_type_to_string(str, type->BitSet.elem);
  3926. } else {
  3927. str = gb_string_append_fmt(str, "%lld", type->BitSet.lower);
  3928. str = gb_string_append_fmt(str, "..=");
  3929. str = gb_string_append_fmt(str, "%lld", type->BitSet.upper);
  3930. }
  3931. if (type->BitSet.underlying != nullptr) {
  3932. str = gb_string_appendc(str, "; ");
  3933. str = write_type_to_string(str, type->BitSet.underlying);
  3934. }
  3935. str = gb_string_appendc(str, "]");
  3936. break;
  3937. case Type_SimdVector:
  3938. str = gb_string_append_fmt(str, "#simd[%d]", cast(int)type->SimdVector.count);
  3939. str = write_type_to_string(str, type->SimdVector.elem);
  3940. break;
  3941. case Type_RelativePointer:
  3942. str = gb_string_append_fmt(str, "#relative(");
  3943. str = write_type_to_string(str, type->RelativePointer.base_integer);
  3944. str = gb_string_append_fmt(str, ") ");
  3945. str = write_type_to_string(str, type->RelativePointer.pointer_type);
  3946. break;
  3947. case Type_RelativeMultiPointer:
  3948. str = gb_string_append_fmt(str, "#relative(");
  3949. str = write_type_to_string(str, type->RelativePointer.base_integer);
  3950. str = gb_string_append_fmt(str, ") ");
  3951. str = write_type_to_string(str, type->RelativePointer.pointer_type);
  3952. break;
  3953. case Type_Matrix:
  3954. str = gb_string_appendc(str, gb_bprintf("matrix[%d, %d]", cast(int)type->Matrix.row_count, cast(int)type->Matrix.column_count));
  3955. str = write_type_to_string(str, type->Matrix.elem);
  3956. break;
  3957. }
  3958. return str;
  3959. }
  3960. gb_internal gbString type_to_string(Type *type, gbAllocator allocator, bool shorthand) {
  3961. return write_type_to_string(gb_string_make(allocator, ""), type, shorthand);
  3962. }
  3963. gb_internal gbString type_to_string(Type *type, bool shorthand) {
  3964. return write_type_to_string(gb_string_make(heap_allocator(), ""), type, shorthand);
  3965. }
  3966. gb_internal gbString type_to_string_shorthand(Type *type) {
  3967. return type_to_string(type, true);
  3968. }