types.cpp 124 KB

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