types.cpp 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495
  1. #include "llvm-c/Types.h"
  2. struct Scope;
  3. struct Ast;
  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_quaternion128,
  30. Basic_quaternion256,
  31. Basic_int,
  32. Basic_uint,
  33. Basic_uintptr,
  34. Basic_rawptr,
  35. Basic_string, // ^u8 + int
  36. Basic_cstring, // ^u8
  37. Basic_any, // rawptr + ^Type_Info
  38. Basic_typeid,
  39. // Endian Specific Types
  40. Basic_i16le,
  41. Basic_u16le,
  42. Basic_i32le,
  43. Basic_u32le,
  44. Basic_i64le,
  45. Basic_u64le,
  46. Basic_i128le,
  47. Basic_u128le,
  48. Basic_i16be,
  49. Basic_u16be,
  50. Basic_i32be,
  51. Basic_u32be,
  52. Basic_i64be,
  53. Basic_u64be,
  54. Basic_i128be,
  55. Basic_u128be,
  56. // Untyped types
  57. Basic_UntypedBool,
  58. Basic_UntypedInteger,
  59. Basic_UntypedFloat,
  60. Basic_UntypedComplex,
  61. Basic_UntypedQuaternion,
  62. Basic_UntypedString,
  63. Basic_UntypedRune,
  64. Basic_UntypedNil,
  65. Basic_UntypedUndef,
  66. Basic_COUNT,
  67. Basic_byte = Basic_u8,
  68. };
  69. enum BasicFlag {
  70. BasicFlag_Boolean = GB_BIT(0),
  71. BasicFlag_Integer = GB_BIT(1),
  72. BasicFlag_Unsigned = GB_BIT(2),
  73. BasicFlag_Float = GB_BIT(3),
  74. BasicFlag_Complex = GB_BIT(4),
  75. BasicFlag_Quaternion = GB_BIT(5),
  76. BasicFlag_Pointer = GB_BIT(6),
  77. BasicFlag_String = GB_BIT(7),
  78. BasicFlag_Rune = GB_BIT(8),
  79. BasicFlag_Untyped = GB_BIT(9),
  80. BasicFlag_LLVM = GB_BIT(11),
  81. BasicFlag_EndianLittle = GB_BIT(13),
  82. BasicFlag_EndianBig = GB_BIT(14),
  83. BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Complex | BasicFlag_Quaternion,
  84. BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  85. BasicFlag_OrderedNumeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Rune,
  86. BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  87. };
  88. struct BasicType {
  89. BasicKind kind;
  90. u32 flags;
  91. i64 size; // -1 if arch. dep.
  92. String name;
  93. };
  94. enum StructSoaKind {
  95. StructSoa_None = 0,
  96. StructSoa_Fixed = 1,
  97. StructSoa_Slice = 2,
  98. StructSoa_Dynamic = 3,
  99. };
  100. struct TypeStruct {
  101. Array<Entity *> fields;
  102. Array<String> tags;
  103. Array<i64> offsets;
  104. Ast * node;
  105. Scope * scope;
  106. Type * polymorphic_params; // Type_Tuple
  107. Type * polymorphic_parent;
  108. i64 custom_align;
  109. Entity * names;
  110. bool are_offsets_set;
  111. bool are_offsets_being_processed;
  112. bool is_packed;
  113. bool is_raw_union;
  114. bool is_polymorphic;
  115. bool is_poly_specialized;
  116. StructSoaKind soa_kind;
  117. Type * soa_elem;
  118. i64 soa_count;
  119. };
  120. struct TypeUnion {
  121. Array<Type *> variants;
  122. Ast * node;
  123. Scope * scope;
  124. i64 variant_block_size;
  125. i64 custom_align;
  126. i64 tag_size;
  127. Type * polymorphic_params; // Type_Tuple
  128. Type * polymorphic_parent;
  129. bool no_nil;
  130. bool maybe;
  131. bool is_polymorphic;
  132. bool is_poly_specialized;
  133. };
  134. #define TYPE_KINDS \
  135. TYPE_KIND(Basic, BasicType) \
  136. TYPE_KIND(Named, struct { \
  137. String name; \
  138. Type * base; \
  139. Entity *type_name; /* Entity_TypeName */ \
  140. }) \
  141. TYPE_KIND(Generic, struct { \
  142. i64 id; \
  143. String name; \
  144. Type * specialized; \
  145. Scope * scope; \
  146. Entity *entity; \
  147. }) \
  148. TYPE_KIND(Pointer, struct { Type *elem; }) \
  149. TYPE_KIND(Opaque, struct { Type *elem; }) \
  150. TYPE_KIND(Array, struct { \
  151. Type *elem; \
  152. i64 count; \
  153. Type *generic_count; \
  154. }) \
  155. TYPE_KIND(EnumeratedArray, struct { \
  156. Type *elem; \
  157. Type *index; \
  158. ExactValue min_value; \
  159. ExactValue max_value; \
  160. i64 count; \
  161. TokenKind op; \
  162. }) \
  163. TYPE_KIND(Slice, struct { Type *elem; }) \
  164. TYPE_KIND(DynamicArray, struct { Type *elem; }) \
  165. TYPE_KIND(Map, struct { \
  166. Type *key; \
  167. Type *value; \
  168. Type *entry_type; \
  169. Type *generated_struct_type; \
  170. Type *internal_type; \
  171. Type *lookup_result_type; \
  172. }) \
  173. TYPE_KIND(Struct, TypeStruct) \
  174. TYPE_KIND(Union, TypeUnion) \
  175. TYPE_KIND(Enum, struct { \
  176. Array<Entity *> fields; \
  177. Ast *node; \
  178. Scope * scope; \
  179. Entity * names; \
  180. Type * base_type; \
  181. ExactValue min_value; \
  182. ExactValue max_value; \
  183. isize min_value_index; \
  184. isize max_value_index; \
  185. }) \
  186. TYPE_KIND(Tuple, struct { \
  187. Array<Entity *> variables; /* Entity_Variable */ \
  188. Array<i64> offsets; \
  189. bool are_offsets_being_processed; \
  190. bool are_offsets_set; \
  191. bool is_packed; \
  192. }) \
  193. TYPE_KIND(Proc, struct { \
  194. Ast *node; \
  195. Scope * scope; \
  196. Type * params; /* Type_Tuple */ \
  197. Type * results; /* Type_Tuple */ \
  198. i32 param_count; \
  199. i32 result_count; \
  200. Array<Type *> abi_compat_params; \
  201. Type * abi_compat_result_type; \
  202. i32 variadic_index; \
  203. bool variadic; \
  204. bool abi_types_set; \
  205. bool require_results; \
  206. bool c_vararg; \
  207. bool is_polymorphic; \
  208. bool is_poly_specialized; \
  209. bool has_proc_default_values; \
  210. bool has_named_results; \
  211. bool diverging; /* no return */ \
  212. bool return_by_pointer; \
  213. u64 tags; \
  214. isize specialization_count; \
  215. ProcCallingConvention calling_convention; \
  216. }) \
  217. TYPE_KIND(BitFieldValue, struct { u32 bits; }) \
  218. TYPE_KIND(BitField, struct { \
  219. Array<Entity *> fields; \
  220. Array<u32> offsets; \
  221. Array<u32> sizes; \
  222. Scope * scope; \
  223. i64 custom_align; \
  224. }) \
  225. TYPE_KIND(BitSet, struct { \
  226. Type *elem; \
  227. Type *underlying; \
  228. i64 lower; \
  229. i64 upper; \
  230. }) \
  231. TYPE_KIND(SimdVector, struct { \
  232. i64 count; \
  233. Type *elem; \
  234. bool is_x86_mmx; \
  235. }) \
  236. enum TypeKind {
  237. Type_Invalid,
  238. #define TYPE_KIND(k, ...) GB_JOIN2(Type_, k),
  239. TYPE_KINDS
  240. #undef TYPE_KIND
  241. Type_Count,
  242. };
  243. String const type_strings[] = {
  244. {cast(u8 *)"Invalid", gb_size_of("Invalid")},
  245. #define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1},
  246. TYPE_KINDS
  247. #undef TYPE_KIND
  248. };
  249. #define TYPE_KIND(k, ...) typedef __VA_ARGS__ GB_JOIN2(Type, k);
  250. TYPE_KINDS
  251. #undef TYPE_KIND
  252. enum TypeFlag : u32 {
  253. TypeFlag_Polymorphic = 1<<1,
  254. TypeFlag_PolySpecialized = 1<<2,
  255. };
  256. struct Type {
  257. TypeKind kind;
  258. union {
  259. #define TYPE_KIND(k, ...) GB_JOIN2(Type, k) k;
  260. TYPE_KINDS
  261. #undef TYPE_KIND
  262. };
  263. // NOTE(bill): These need to be at the end to not affect the unionized data
  264. i64 cached_size;
  265. i64 cached_align;
  266. u32 flags; // TypeFlag
  267. bool failure;
  268. };
  269. // IMPORTANT NOTE(bill): This must match the same as the in core.odin
  270. enum Typeid_Kind : u8 {
  271. Typeid_Invalid,
  272. Typeid_Integer,
  273. Typeid_Rune,
  274. Typeid_Float,
  275. Typeid_Complex,
  276. Typeid_Quaternion,
  277. Typeid_String,
  278. Typeid_Boolean,
  279. Typeid_Any,
  280. Typeid_Type_Id,
  281. Typeid_Pointer,
  282. Typeid_Procedure,
  283. Typeid_Array,
  284. Typeid_Enumerated_Array,
  285. Typeid_Dynamic_Array,
  286. Typeid_Slice,
  287. Typeid_Tuple,
  288. Typeid_Struct,
  289. Typeid_Union,
  290. Typeid_Enum,
  291. Typeid_Map,
  292. Typeid_Bit_Field,
  293. Typeid_Bit_Set,
  294. };
  295. // TODO(bill): Should I add extra information here specifying the kind of selection?
  296. // e.g. field, constant, array field, type field, etc.
  297. struct Selection {
  298. Entity * entity;
  299. Array<i32> index;
  300. bool indirect; // Set if there was a pointer deref anywhere down the line
  301. };
  302. Selection empty_selection = {0};
  303. Selection make_selection(Entity *entity, Array<i32> index, bool indirect) {
  304. Selection s = {entity, index, indirect};
  305. return s;
  306. }
  307. void selection_add_index(Selection *s, isize index) {
  308. // IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
  309. // of heap allocation
  310. // TODO(bill): Find a way to use a backing buffer for initial use as the general case is probably .count<3
  311. if (s->index.data == nullptr) {
  312. array_init(&s->index, heap_allocator());
  313. }
  314. array_add(&s->index, cast(i32)index);
  315. }
  316. Selection selection_combine(Selection const &lhs, Selection const &rhs) {
  317. Selection new_sel = lhs;
  318. new_sel.indirect = lhs.indirect || rhs.indirect;
  319. new_sel.index = array_make<i32>(heap_allocator(), lhs.index.count+rhs.index.count);
  320. array_copy(&new_sel.index, lhs.index, 0);
  321. array_copy(&new_sel.index, rhs.index, lhs.index.count);
  322. return new_sel;
  323. }
  324. Selection sub_selection(Selection const &sel, isize offset) {
  325. Selection res = {};
  326. res.index.data = sel.index.data + offset;
  327. res.index.count = gb_max(sel.index.count - offset, 0);
  328. res.index.capacity = res.index.count;
  329. return res;
  330. }
  331. Selection sub_selection_with_length(Selection const &sel, isize offset, isize len) {
  332. Selection res = {};
  333. res.index.data = sel.index.data + offset;
  334. res.index.count = gb_max(len, gb_max(sel.index.count - offset, 0));
  335. res.index.capacity = res.index.count;
  336. return res;
  337. }
  338. gb_global Type basic_types[] = {
  339. {Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}},
  340. {Type_Basic, {Basic_llvm_bool, BasicFlag_Boolean | BasicFlag_LLVM, 1, STR_LIT("llvm bool")}},
  341. {Type_Basic, {Basic_bool, BasicFlag_Boolean, 1, STR_LIT("bool")}},
  342. {Type_Basic, {Basic_b8, BasicFlag_Boolean, 1, STR_LIT("b8")}},
  343. {Type_Basic, {Basic_b16, BasicFlag_Boolean, 2, STR_LIT("b16")}},
  344. {Type_Basic, {Basic_b32, BasicFlag_Boolean, 4, STR_LIT("b32")}},
  345. {Type_Basic, {Basic_b64, BasicFlag_Boolean, 8, STR_LIT("b64")}},
  346. {Type_Basic, {Basic_i8, BasicFlag_Integer, 1, STR_LIT("i8")}},
  347. {Type_Basic, {Basic_u8, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("u8")}},
  348. {Type_Basic, {Basic_i16, BasicFlag_Integer, 2, STR_LIT("i16")}},
  349. {Type_Basic, {Basic_u16, BasicFlag_Integer | BasicFlag_Unsigned, 2, STR_LIT("u16")}},
  350. {Type_Basic, {Basic_i32, BasicFlag_Integer, 4, STR_LIT("i32")}},
  351. {Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, 4, STR_LIT("u32")}},
  352. {Type_Basic, {Basic_i64, BasicFlag_Integer, 8, STR_LIT("i64")}},
  353. {Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, 8, STR_LIT("u64")}},
  354. {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}},
  355. {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}},
  356. {Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}},
  357. // {Type_Basic, {Basic_f16, BasicFlag_Float, 2, STR_LIT("f16")}},
  358. {Type_Basic, {Basic_f32, BasicFlag_Float, 4, STR_LIT("f32")}},
  359. {Type_Basic, {Basic_f64, BasicFlag_Float, 8, STR_LIT("f64")}},
  360. // {Type_Basic, {Basic_complex32, BasicFlag_Complex, 4, STR_LIT("complex32")}},
  361. {Type_Basic, {Basic_complex64, BasicFlag_Complex, 8, STR_LIT("complex64")}},
  362. {Type_Basic, {Basic_complex128, BasicFlag_Complex, 16, STR_LIT("complex128")}},
  363. {Type_Basic, {Basic_quaternion128, BasicFlag_Quaternion, 16, STR_LIT("quaternion128")}},
  364. {Type_Basic, {Basic_quaternion256, BasicFlag_Quaternion, 32, STR_LIT("quaternion256")}},
  365. {Type_Basic, {Basic_int, BasicFlag_Integer, -1, STR_LIT("int")}},
  366. {Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uint")}},
  367. {Type_Basic, {Basic_uintptr, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uintptr")}},
  368. {Type_Basic, {Basic_rawptr, BasicFlag_Pointer, -1, STR_LIT("rawptr")}},
  369. {Type_Basic, {Basic_string, BasicFlag_String, -1, STR_LIT("string")}},
  370. {Type_Basic, {Basic_cstring, BasicFlag_String, -1, STR_LIT("cstring")}},
  371. {Type_Basic, {Basic_any, 0, -1, STR_LIT("any")}},
  372. {Type_Basic, {Basic_typeid, 0, -1, STR_LIT("typeid")}},
  373. // Endian
  374. {Type_Basic, {Basic_i16le, BasicFlag_Integer | BasicFlag_EndianLittle, 2, STR_LIT("i16le")}},
  375. {Type_Basic, {Basic_u16le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 2, STR_LIT("u16le")}},
  376. {Type_Basic, {Basic_i32le, BasicFlag_Integer | BasicFlag_EndianLittle, 4, STR_LIT("i32le")}},
  377. {Type_Basic, {Basic_u32le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 4, STR_LIT("u32le")}},
  378. {Type_Basic, {Basic_i64le, BasicFlag_Integer | BasicFlag_EndianLittle, 8, STR_LIT("i64le")}},
  379. {Type_Basic, {Basic_u64le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 8, STR_LIT("u64le")}},
  380. {Type_Basic, {Basic_i128le, BasicFlag_Integer | BasicFlag_EndianLittle, 16, STR_LIT("i128le")}},
  381. {Type_Basic, {Basic_u128le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 16, STR_LIT("u128le")}},
  382. {Type_Basic, {Basic_i16be, BasicFlag_Integer | BasicFlag_EndianBig, 2, STR_LIT("i16be")}},
  383. {Type_Basic, {Basic_u16be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 2, STR_LIT("u16be")}},
  384. {Type_Basic, {Basic_i32be, BasicFlag_Integer | BasicFlag_EndianBig, 4, STR_LIT("i32be")}},
  385. {Type_Basic, {Basic_u32be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 4, STR_LIT("u32be")}},
  386. {Type_Basic, {Basic_i64be, BasicFlag_Integer | BasicFlag_EndianBig, 8, STR_LIT("i64be")}},
  387. {Type_Basic, {Basic_u64be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 8, STR_LIT("u64be")}},
  388. {Type_Basic, {Basic_i128be, BasicFlag_Integer | BasicFlag_EndianBig, 16, STR_LIT("i128be")}},
  389. {Type_Basic, {Basic_u128be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 16, STR_LIT("u128be")}},
  390. // Untyped types
  391. {Type_Basic, {Basic_UntypedBool, BasicFlag_Boolean | BasicFlag_Untyped, 0, STR_LIT("untyped bool")}},
  392. {Type_Basic, {Basic_UntypedInteger, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped integer")}},
  393. {Type_Basic, {Basic_UntypedFloat, BasicFlag_Float | BasicFlag_Untyped, 0, STR_LIT("untyped float")}},
  394. {Type_Basic, {Basic_UntypedComplex, BasicFlag_Complex | BasicFlag_Untyped, 0, STR_LIT("untyped complex")}},
  395. {Type_Basic, {Basic_UntypedQuaternion, BasicFlag_Quaternion | BasicFlag_Untyped, 0, STR_LIT("untyped quaternion")}},
  396. {Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}},
  397. {Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}},
  398. {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}},
  399. {Type_Basic, {Basic_UntypedUndef, BasicFlag_Untyped, 0, STR_LIT("untyped undefined")}},
  400. };
  401. // gb_global Type basic_type_aliases[] = {
  402. // // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}},
  403. // // {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}},
  404. // };
  405. gb_global Type *t_invalid = &basic_types[Basic_Invalid];
  406. gb_global Type *t_llvm_bool = &basic_types[Basic_llvm_bool];
  407. gb_global Type *t_bool = &basic_types[Basic_bool];
  408. gb_global Type *t_i8 = &basic_types[Basic_i8];
  409. gb_global Type *t_u8 = &basic_types[Basic_u8];
  410. gb_global Type *t_i16 = &basic_types[Basic_i16];
  411. gb_global Type *t_u16 = &basic_types[Basic_u16];
  412. gb_global Type *t_i32 = &basic_types[Basic_i32];
  413. gb_global Type *t_u32 = &basic_types[Basic_u32];
  414. gb_global Type *t_i64 = &basic_types[Basic_i64];
  415. gb_global Type *t_u64 = &basic_types[Basic_u64];
  416. gb_global Type *t_i128 = &basic_types[Basic_i128];
  417. gb_global Type *t_u128 = &basic_types[Basic_u128];
  418. gb_global Type *t_rune = &basic_types[Basic_rune];
  419. // gb_global Type *t_f16 = &basic_types[Basic_f16];
  420. gb_global Type *t_f32 = &basic_types[Basic_f32];
  421. gb_global Type *t_f64 = &basic_types[Basic_f64];
  422. // gb_global Type *t_complex32 = &basic_types[Basic_complex32];
  423. gb_global Type *t_complex64 = &basic_types[Basic_complex64];
  424. gb_global Type *t_complex128 = &basic_types[Basic_complex128];
  425. gb_global Type *t_quaternion128 = &basic_types[Basic_quaternion128];
  426. gb_global Type *t_quaternion256 = &basic_types[Basic_quaternion256];
  427. gb_global Type *t_int = &basic_types[Basic_int];
  428. gb_global Type *t_uint = &basic_types[Basic_uint];
  429. gb_global Type *t_uintptr = &basic_types[Basic_uintptr];
  430. gb_global Type *t_rawptr = &basic_types[Basic_rawptr];
  431. gb_global Type *t_string = &basic_types[Basic_string];
  432. gb_global Type *t_cstring = &basic_types[Basic_cstring];
  433. gb_global Type *t_any = &basic_types[Basic_any];
  434. gb_global Type *t_typeid = &basic_types[Basic_typeid];
  435. gb_global Type *t_i16le = &basic_types[Basic_i16le];
  436. gb_global Type *t_u16le = &basic_types[Basic_u16le];
  437. gb_global Type *t_i32le = &basic_types[Basic_i32le];
  438. gb_global Type *t_u32le = &basic_types[Basic_u32le];
  439. gb_global Type *t_i64le = &basic_types[Basic_i64le];
  440. gb_global Type *t_u64le = &basic_types[Basic_u64le];
  441. gb_global Type *t_i128le = &basic_types[Basic_i128le];
  442. gb_global Type *t_u128le = &basic_types[Basic_u128le];
  443. gb_global Type *t_i16be = &basic_types[Basic_i16be];
  444. gb_global Type *t_u16be = &basic_types[Basic_u16be];
  445. gb_global Type *t_i32be = &basic_types[Basic_i32be];
  446. gb_global Type *t_u32be = &basic_types[Basic_u32be];
  447. gb_global Type *t_i64be = &basic_types[Basic_i64be];
  448. gb_global Type *t_u64be = &basic_types[Basic_u64be];
  449. gb_global Type *t_i128be = &basic_types[Basic_i128be];
  450. gb_global Type *t_u128be = &basic_types[Basic_u128be];
  451. gb_global Type *t_untyped_bool = &basic_types[Basic_UntypedBool];
  452. gb_global Type *t_untyped_integer = &basic_types[Basic_UntypedInteger];
  453. gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat];
  454. gb_global Type *t_untyped_complex = &basic_types[Basic_UntypedComplex];
  455. gb_global Type *t_untyped_quaternion = &basic_types[Basic_UntypedQuaternion];
  456. gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString];
  457. gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
  458. gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil];
  459. gb_global Type *t_untyped_undef = &basic_types[Basic_UntypedUndef];
  460. gb_global Type *t_u8_ptr = nullptr;
  461. gb_global Type *t_int_ptr = nullptr;
  462. gb_global Type *t_i64_ptr = nullptr;
  463. gb_global Type *t_f64_ptr = nullptr;
  464. gb_global Type *t_u8_slice = nullptr;
  465. gb_global Type *t_string_slice = nullptr;
  466. // Type generated for the "preload" file
  467. gb_global Type *t_type_info = nullptr;
  468. gb_global Type *t_type_info_enum_value = nullptr;
  469. gb_global Type *t_type_info_ptr = nullptr;
  470. gb_global Type *t_type_info_enum_value_ptr = nullptr;
  471. gb_global Type *t_type_info_named = nullptr;
  472. gb_global Type *t_type_info_integer = nullptr;
  473. gb_global Type *t_type_info_rune = nullptr;
  474. gb_global Type *t_type_info_float = nullptr;
  475. gb_global Type *t_type_info_complex = nullptr;
  476. gb_global Type *t_type_info_quaternion = nullptr;
  477. gb_global Type *t_type_info_any = nullptr;
  478. gb_global Type *t_type_info_typeid = nullptr;
  479. gb_global Type *t_type_info_string = nullptr;
  480. gb_global Type *t_type_info_boolean = nullptr;
  481. gb_global Type *t_type_info_pointer = nullptr;
  482. gb_global Type *t_type_info_procedure = nullptr;
  483. gb_global Type *t_type_info_array = nullptr;
  484. gb_global Type *t_type_info_enumerated_array = nullptr;
  485. gb_global Type *t_type_info_dynamic_array = nullptr;
  486. gb_global Type *t_type_info_slice = nullptr;
  487. gb_global Type *t_type_info_tuple = nullptr;
  488. gb_global Type *t_type_info_struct = nullptr;
  489. gb_global Type *t_type_info_union = nullptr;
  490. gb_global Type *t_type_info_enum = nullptr;
  491. gb_global Type *t_type_info_map = nullptr;
  492. gb_global Type *t_type_info_bit_field = nullptr;
  493. gb_global Type *t_type_info_bit_set = nullptr;
  494. gb_global Type *t_type_info_opaque = nullptr;
  495. gb_global Type *t_type_info_simd_vector = nullptr;
  496. gb_global Type *t_type_info_named_ptr = nullptr;
  497. gb_global Type *t_type_info_integer_ptr = nullptr;
  498. gb_global Type *t_type_info_rune_ptr = nullptr;
  499. gb_global Type *t_type_info_float_ptr = nullptr;
  500. gb_global Type *t_type_info_complex_ptr = nullptr;
  501. gb_global Type *t_type_info_quaternion_ptr = nullptr;
  502. gb_global Type *t_type_info_any_ptr = nullptr;
  503. gb_global Type *t_type_info_typeid_ptr = nullptr;
  504. gb_global Type *t_type_info_string_ptr = nullptr;
  505. gb_global Type *t_type_info_boolean_ptr = nullptr;
  506. gb_global Type *t_type_info_pointer_ptr = nullptr;
  507. gb_global Type *t_type_info_procedure_ptr = nullptr;
  508. gb_global Type *t_type_info_array_ptr = nullptr;
  509. gb_global Type *t_type_info_enumerated_array_ptr = nullptr;
  510. gb_global Type *t_type_info_dynamic_array_ptr = nullptr;
  511. gb_global Type *t_type_info_slice_ptr = nullptr;
  512. gb_global Type *t_type_info_tuple_ptr = nullptr;
  513. gb_global Type *t_type_info_struct_ptr = nullptr;
  514. gb_global Type *t_type_info_union_ptr = nullptr;
  515. gb_global Type *t_type_info_enum_ptr = nullptr;
  516. gb_global Type *t_type_info_map_ptr = nullptr;
  517. gb_global Type *t_type_info_bit_field_ptr = nullptr;
  518. gb_global Type *t_type_info_bit_set_ptr = nullptr;
  519. gb_global Type *t_type_info_opaque_ptr = nullptr;
  520. gb_global Type *t_type_info_simd_vector_ptr = nullptr;
  521. gb_global Type *t_allocator = nullptr;
  522. gb_global Type *t_allocator_ptr = nullptr;
  523. gb_global Type *t_context = nullptr;
  524. gb_global Type *t_context_ptr = nullptr;
  525. gb_global Type *t_source_code_location = nullptr;
  526. gb_global Type *t_source_code_location_ptr = nullptr;
  527. gb_global Type *t_map_key = nullptr;
  528. gb_global Type *t_map_header = nullptr;
  529. gb_global Type *t_vector_x86_mmx = nullptr;
  530. i64 type_size_of (Type *t);
  531. i64 type_align_of (Type *t);
  532. i64 type_offset_of (Type *t, i32 index);
  533. gbString type_to_string (Type *type);
  534. void init_map_internal_types(Type *type);
  535. Type * bit_set_to_int(Type *t);
  536. bool are_types_identical(Type *x, Type *y);
  537. bool type_ptr_set_exists(PtrSet<Type *> *s, Type *t) {
  538. if (ptr_set_exists(s, t)) {
  539. return true;
  540. }
  541. // TODO(bill, 2019-10-05): This is very slow and it's probably a lot
  542. // faster to cache types correctly
  543. for_array(i, s->entries) {
  544. Type *f = s->entries[i].ptr;
  545. if (are_types_identical(t, f)) {
  546. ptr_set_add(s, t);
  547. return true;
  548. }
  549. }
  550. return false;
  551. }
  552. Type *base_type(Type *t) {
  553. for (;;) {
  554. if (t == nullptr) {
  555. break;
  556. }
  557. if (t->kind != Type_Named) {
  558. break;
  559. }
  560. if (t == t->Named.base) {
  561. return t_invalid;
  562. }
  563. t = t->Named.base;
  564. }
  565. return t;
  566. }
  567. Type *strip_opaque_type(Type *t) {
  568. for (;;) {
  569. if (t == nullptr) {
  570. break;
  571. }
  572. if (t->kind != Type_Opaque) {
  573. break;
  574. }
  575. t = t->Opaque.elem;
  576. }
  577. return t;
  578. }
  579. Type *base_enum_type(Type *t) {
  580. Type *bt = base_type(t);
  581. if (bt != nullptr &&
  582. bt->kind == Type_Enum) {
  583. return bt->Enum.base_type;
  584. }
  585. return t;
  586. }
  587. Type *core_type(Type *t) {
  588. for (;;) {
  589. if (t == nullptr) {
  590. break;
  591. }
  592. switch (t->kind) {
  593. case Type_Named:
  594. if (t == t->Named.base) {
  595. return t_invalid;
  596. }
  597. t = t->Named.base;
  598. continue;
  599. case Type_Enum:
  600. t = t->Enum.base_type;
  601. continue;
  602. case Type_Opaque:
  603. t = t->Opaque.elem;
  604. continue;
  605. }
  606. break;
  607. }
  608. return t;
  609. }
  610. void set_base_type(Type *t, Type *base) {
  611. if (t && t->kind == Type_Named) {
  612. t->Named.base = base;
  613. }
  614. }
  615. Type *alloc_type(TypeKind kind) {
  616. gbAllocator a = heap_allocator();
  617. Type *t = gb_alloc_item(a, Type);
  618. gb_zero_item(t);
  619. t->kind = kind;
  620. t->cached_size = -1;
  621. t->cached_align = -1;
  622. return t;
  623. }
  624. Type *alloc_type_generic(Scope *scope, i64 id, String name, Type *specialized) {
  625. Type *t = alloc_type(Type_Generic);
  626. t->Generic.id = id;
  627. t->Generic.name = name;
  628. t->Generic.specialized = specialized;
  629. t->Generic.scope = scope;
  630. return t;
  631. }
  632. Type *alloc_type_opaque(Type *elem) {
  633. Type *t = alloc_type(Type_Opaque);
  634. t->Opaque.elem = elem;
  635. return t;
  636. }
  637. Type *alloc_type_pointer(Type *elem) {
  638. Type *t = alloc_type(Type_Pointer);
  639. t->Pointer.elem = elem;
  640. return t;
  641. }
  642. Type *alloc_type_array(Type *elem, i64 count, Type *generic_count = nullptr) {
  643. if (generic_count != nullptr) {
  644. Type *t = alloc_type(Type_Array);
  645. t->Array.elem = elem;
  646. t->Array.count = count;
  647. t->Array.generic_count = generic_count;
  648. return t;
  649. }
  650. Type *t = alloc_type(Type_Array);
  651. t->Array.elem = elem;
  652. t->Array.count = count;
  653. return t;
  654. }
  655. Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue min_value, ExactValue max_value, TokenKind op) {
  656. Type *t = alloc_type(Type_EnumeratedArray);
  657. t->EnumeratedArray.elem = elem;
  658. t->EnumeratedArray.index = index;
  659. t->EnumeratedArray.min_value = min_value;
  660. t->EnumeratedArray.max_value = max_value;
  661. t->EnumeratedArray.op = op;
  662. t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(max_value, min_value));
  663. return t;
  664. }
  665. Type *alloc_type_slice(Type *elem) {
  666. Type *t = alloc_type(Type_Slice);
  667. t->Array.elem = elem;
  668. return t;
  669. }
  670. Type *alloc_type_dynamic_array(Type *elem) {
  671. Type *t = alloc_type(Type_DynamicArray);
  672. t->DynamicArray.elem = elem;
  673. return t;
  674. }
  675. Type *alloc_type_struct() {
  676. Type *t = alloc_type(Type_Struct);
  677. return t;
  678. }
  679. Type *alloc_type_union() {
  680. Type *t = alloc_type(Type_Union);
  681. return t;
  682. }
  683. Type *alloc_type_enum() {
  684. Type *t = alloc_type(Type_Enum);
  685. return t;
  686. }
  687. Type *alloc_type_named(String name, Type *base, Entity *type_name) {
  688. Type *t = alloc_type(Type_Named);
  689. t->Named.name = name;
  690. t->Named.base = base;
  691. t->Named.type_name = type_name;
  692. return t;
  693. }
  694. Type *alloc_type_tuple() {
  695. Type *t = alloc_type(Type_Tuple);
  696. return t;
  697. }
  698. Type *alloc_type_proc(Scope *scope, Type *params, isize param_count, Type *results, isize result_count, bool variadic, ProcCallingConvention calling_convention) {
  699. Type *t = alloc_type(Type_Proc);
  700. if (variadic) {
  701. if (param_count == 0) {
  702. GB_PANIC("variadic procedure must have at least one parameter");
  703. }
  704. GB_ASSERT(params != nullptr && params->kind == Type_Tuple);
  705. Entity *e = params->Tuple.variables[param_count-1];
  706. if (base_type(e->type)->kind != Type_Slice) {
  707. // NOTE(bill): For custom calling convention
  708. GB_PANIC("variadic parameter must be of type slice");
  709. }
  710. }
  711. t->Proc.scope = scope;
  712. t->Proc.params = params;
  713. t->Proc.param_count = cast(i32)param_count;
  714. t->Proc.results = results;
  715. t->Proc.result_count = cast(i32)result_count;
  716. t->Proc.variadic = variadic;
  717. t->Proc.calling_convention = calling_convention;
  718. return t;
  719. }
  720. bool is_type_valid_for_keys(Type *t);
  721. Type *alloc_type_map(i64 count, Type *key, Type *value) {
  722. if (key != nullptr) {
  723. GB_ASSERT(is_type_valid_for_keys(key));
  724. GB_ASSERT(value != nullptr);
  725. }
  726. Type *t = alloc_type(Type_Map);
  727. t->Map.key = key;
  728. t->Map.value = value;
  729. return t;
  730. }
  731. Type *alloc_type_bit_field_value(u32 bits) {
  732. Type *t = alloc_type(Type_BitFieldValue);
  733. t->BitFieldValue.bits = bits;
  734. return t;
  735. }
  736. Type *alloc_type_bit_field() {
  737. Type *t = alloc_type(Type_BitField);
  738. return t;
  739. }
  740. Type *alloc_type_bit_set() {
  741. Type *t = alloc_type(Type_BitSet);
  742. return t;
  743. }
  744. Type *alloc_type_simd_vector(i64 count, Type *elem) {
  745. Type *t = alloc_type(Type_SimdVector);
  746. t->SimdVector.count = count;
  747. t->SimdVector.elem = elem;
  748. return t;
  749. }
  750. ////////////////////////////////////////////////////////////////
  751. Type *type_deref(Type *t) {
  752. if (t != nullptr) {
  753. Type *bt = base_type(t);
  754. if (bt == nullptr)
  755. return nullptr;
  756. if (bt != nullptr && bt->kind == Type_Pointer)
  757. return bt->Pointer.elem;
  758. }
  759. return t;
  760. }
  761. bool is_type_named(Type *t) {
  762. if (t->kind == Type_Basic) {
  763. return true;
  764. }
  765. return t->kind == Type_Named;
  766. }
  767. bool is_type_named_alias(Type *t) {
  768. if (!is_type_named(t)) {
  769. return false;
  770. }
  771. Entity *e = t->Named.type_name;
  772. if (e == nullptr) {
  773. return false;
  774. }
  775. if (e->kind != Entity_TypeName) {
  776. return false;
  777. }
  778. return e->TypeName.is_type_alias;
  779. }
  780. bool is_type_boolean(Type *t) {
  781. // t = core_type(t);
  782. t = base_type(t);
  783. if (t->kind == Type_Basic) {
  784. return (t->Basic.flags & BasicFlag_Boolean) != 0;
  785. }
  786. return false;
  787. }
  788. bool is_type_integer(Type *t) {
  789. // t = core_type(t);
  790. t = base_type(t);
  791. if (t->kind == Type_Basic) {
  792. return (t->Basic.flags & BasicFlag_Integer) != 0;
  793. }
  794. return false;
  795. }
  796. bool is_type_unsigned(Type *t) {
  797. t = base_type(t);
  798. // t = core_type(t);
  799. if (t->kind == Type_Basic) {
  800. return (t->Basic.flags & BasicFlag_Unsigned) != 0;
  801. }
  802. return false;
  803. }
  804. bool is_type_integer_128bit(Type *t) {
  805. // t = core_type(t);
  806. t = base_type(t);
  807. if (t->kind == Type_Basic) {
  808. return (t->Basic.flags & BasicFlag_Integer) != 0 && t->Basic.size == 16;
  809. }
  810. return false;
  811. }
  812. bool is_type_rune(Type *t) {
  813. // t = core_type(t);
  814. t = base_type(t);
  815. if (t->kind == Type_Basic) {
  816. return (t->Basic.flags & BasicFlag_Rune) != 0;
  817. }
  818. return false;
  819. }
  820. bool is_type_numeric(Type *t) {
  821. // t = core_type(t);
  822. t = base_type(t);
  823. if (t->kind == Type_Basic) {
  824. return (t->Basic.flags & BasicFlag_Numeric) != 0;
  825. } else if (t->kind == Type_Enum) {
  826. return is_type_numeric(t->Enum.base_type);
  827. }
  828. // TODO(bill): Should this be here?
  829. if (t->kind == Type_Array) {
  830. return is_type_numeric(t->Array.elem);
  831. }
  832. return false;
  833. }
  834. bool is_type_string(Type *t) {
  835. t = base_type(t);
  836. if (t->kind == Type_Basic) {
  837. return (t->Basic.flags & BasicFlag_String) != 0;
  838. }
  839. return false;
  840. }
  841. bool is_type_cstring(Type *t) {
  842. t = base_type(t);
  843. if (t->kind == Type_Basic) {
  844. return t->Basic.kind == Basic_cstring;
  845. }
  846. return false;
  847. }
  848. bool is_type_typed(Type *t) {
  849. t = base_type(t);
  850. if (t == nullptr) {
  851. return false;
  852. }
  853. if (t->kind == Type_Basic) {
  854. return (t->Basic.flags & BasicFlag_Untyped) == 0;
  855. }
  856. return true;
  857. }
  858. bool is_type_untyped(Type *t) {
  859. t = base_type(t);
  860. if (t->kind == Type_Basic) {
  861. return (t->Basic.flags & BasicFlag_Untyped) != 0;
  862. }
  863. return false;
  864. }
  865. bool is_type_ordered(Type *t) {
  866. t = core_type(t);
  867. switch (t->kind) {
  868. case Type_Basic:
  869. return (t->Basic.flags & BasicFlag_Ordered) != 0;
  870. case Type_Pointer:
  871. return true;
  872. }
  873. return false;
  874. }
  875. bool is_type_ordered_numeric(Type *t) {
  876. t = core_type(t);
  877. switch (t->kind) {
  878. case Type_Basic:
  879. return (t->Basic.flags & BasicFlag_OrderedNumeric) != 0;
  880. }
  881. return false;
  882. }
  883. bool is_type_constant_type(Type *t) {
  884. t = core_type(t);
  885. if (t->kind == Type_Basic) {
  886. return (t->Basic.flags & BasicFlag_ConstantType) != 0;
  887. }
  888. if (t->kind == Type_BitSet) {
  889. return true;
  890. }
  891. return false;
  892. }
  893. bool is_type_float(Type *t) {
  894. t = core_type(t);
  895. if (t->kind == Type_Basic) {
  896. return (t->Basic.flags & BasicFlag_Float) != 0;
  897. }
  898. return false;
  899. }
  900. bool is_type_complex(Type *t) {
  901. t = core_type(t);
  902. if (t->kind == Type_Basic) {
  903. return (t->Basic.flags & BasicFlag_Complex) != 0;
  904. }
  905. return false;
  906. }
  907. bool is_type_quaternion(Type *t) {
  908. t = core_type(t);
  909. if (t->kind == Type_Basic) {
  910. return (t->Basic.flags & BasicFlag_Quaternion) != 0;
  911. }
  912. return false;
  913. }
  914. bool is_type_f32(Type *t) {
  915. t = core_type(t);
  916. if (t->kind == Type_Basic) {
  917. return t->Basic.kind == Basic_f32;
  918. }
  919. return false;
  920. }
  921. bool is_type_f64(Type *t) {
  922. t = core_type(t);
  923. if (t->kind == Type_Basic) {
  924. return t->Basic.kind == Basic_f64;
  925. }
  926. return false;
  927. }
  928. bool is_type_pointer(Type *t) {
  929. t = base_type(t);
  930. if (t->kind == Type_Basic) {
  931. return (t->Basic.flags & BasicFlag_Pointer) != 0;
  932. }
  933. return t->kind == Type_Pointer;
  934. }
  935. bool is_type_tuple(Type *t) {
  936. t = base_type(t);
  937. return t->kind == Type_Tuple;
  938. }
  939. bool is_type_opaque(Type *t) {
  940. t = base_type(t);
  941. return t->kind == Type_Opaque;
  942. }
  943. bool is_type_uintptr(Type *t) {
  944. if (t->kind == Type_Basic) {
  945. return (t->Basic.kind == Basic_uintptr);
  946. }
  947. return false;
  948. }
  949. bool is_type_rawptr(Type *t) {
  950. if (t->kind == Type_Basic) {
  951. return t->Basic.kind == Basic_rawptr;
  952. }
  953. return false;
  954. }
  955. bool is_type_u8(Type *t) {
  956. if (t->kind == Type_Basic) {
  957. return t->Basic.kind == Basic_u8;
  958. }
  959. return false;
  960. }
  961. bool is_type_array(Type *t) {
  962. t = base_type(t);
  963. return t->kind == Type_Array;
  964. }
  965. bool is_type_enumerated_array(Type *t) {
  966. t = base_type(t);
  967. return t->kind == Type_EnumeratedArray;
  968. }
  969. bool is_type_dynamic_array(Type *t) {
  970. t = base_type(t);
  971. return t->kind == Type_DynamicArray;
  972. }
  973. bool is_type_slice(Type *t) {
  974. t = base_type(t);
  975. return t->kind == Type_Slice;
  976. }
  977. bool is_type_u8_slice(Type *t) {
  978. t = base_type(t);
  979. if (t->kind == Type_Slice) {
  980. return is_type_u8(t->Slice.elem);
  981. }
  982. return false;
  983. }
  984. bool is_type_u8_ptr(Type *t) {
  985. t = base_type(t);
  986. if (t->kind == Type_Pointer) {
  987. return is_type_u8(t->Slice.elem);
  988. }
  989. return false;
  990. }
  991. bool is_type_proc(Type *t) {
  992. t = base_type(t);
  993. return t->kind == Type_Proc;
  994. }
  995. bool is_type_poly_proc(Type *t) {
  996. t = base_type(t);
  997. return t->kind == Type_Proc && t->Proc.is_polymorphic;
  998. }
  999. bool is_type_simd_vector(Type *t) {
  1000. t = base_type(t);
  1001. return t->kind == Type_SimdVector;
  1002. }
  1003. Type *base_array_type(Type *t) {
  1004. Type *bt = base_type(t);
  1005. if (is_type_array(bt)) {
  1006. return bt->Array.elem;
  1007. } else if (is_type_enumerated_array(bt)) {
  1008. return bt->EnumeratedArray.elem;
  1009. } else if (is_type_simd_vector(bt)) {
  1010. return bt->SimdVector.elem;
  1011. }
  1012. return t;
  1013. }
  1014. bool is_type_generic(Type *t) {
  1015. t = base_type(t);
  1016. return t->kind == Type_Generic;
  1017. }
  1018. Type *core_array_type(Type *t) {
  1019. for (;;) {
  1020. Type *prev = t;
  1021. t = base_array_type(t);
  1022. if (t->kind != Type_Array && t->kind != Type_SimdVector) {
  1023. break;
  1024. }
  1025. }
  1026. return t;
  1027. }
  1028. // NOTE(bill): type can be easily compared using memcmp
  1029. bool is_type_simple_compare(Type *t) {
  1030. t = core_type(t);
  1031. switch (t->kind) {
  1032. case Type_Array:
  1033. return is_type_simple_compare(t->Array.elem);
  1034. case Type_Basic:
  1035. if (t->Basic.flags & (BasicFlag_Integer|BasicFlag_Float|BasicFlag_Complex|BasicFlag_Rune|BasicFlag_Pointer)) {
  1036. return true;
  1037. }
  1038. return false;
  1039. case Type_Pointer:
  1040. case Type_Proc:
  1041. case Type_BitSet:
  1042. case Type_BitField:
  1043. return true;
  1044. }
  1045. return false;
  1046. }
  1047. Type *base_complex_elem_type(Type *t) {
  1048. t = core_type(t);
  1049. if (t->kind == Type_Basic) {
  1050. switch (t->Basic.kind) {
  1051. // case Basic_complex32: return t_f16;
  1052. case Basic_complex64: return t_f32;
  1053. case Basic_complex128: return t_f64;
  1054. case Basic_quaternion128: return t_f32;
  1055. case Basic_quaternion256: return t_f64;
  1056. case Basic_UntypedComplex: return t_untyped_float;
  1057. case Basic_UntypedQuaternion: return t_untyped_float;
  1058. }
  1059. }
  1060. GB_PANIC("Invalid complex type");
  1061. return t_invalid;
  1062. }
  1063. bool is_type_struct(Type *t) {
  1064. t = base_type(t);
  1065. return t->kind == Type_Struct;
  1066. }
  1067. bool is_type_union(Type *t) {
  1068. t = base_type(t);
  1069. return t->kind == Type_Union;
  1070. }
  1071. bool is_type_soa_struct(Type *t) {
  1072. t = base_type(t);
  1073. return t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None;
  1074. }
  1075. bool is_type_raw_union(Type *t) {
  1076. t = base_type(t);
  1077. return (t->kind == Type_Struct && t->Struct.is_raw_union);
  1078. }
  1079. bool is_type_enum(Type *t) {
  1080. t = base_type(t);
  1081. return (t->kind == Type_Enum);
  1082. }
  1083. bool is_type_bit_field(Type *t) {
  1084. t = base_type(t);
  1085. return (t->kind == Type_BitField);
  1086. }
  1087. bool is_type_bit_field_value(Type *t) {
  1088. t = base_type(t);
  1089. return (t->kind == Type_BitFieldValue);
  1090. }
  1091. bool is_type_bit_set(Type *t) {
  1092. t = base_type(t);
  1093. return (t->kind == Type_BitSet);
  1094. }
  1095. bool is_type_map(Type *t) {
  1096. t = base_type(t);
  1097. return t->kind == Type_Map;
  1098. }
  1099. bool is_type_union_maybe_pointer(Type *t) {
  1100. t = base_type(t);
  1101. if (t->kind == Type_Union && t->Union.maybe) {
  1102. if (t->Union.variants.count == 1) {
  1103. return is_type_pointer(t->Union.variants[0]);
  1104. }
  1105. }
  1106. return false;
  1107. }
  1108. bool is_type_union_maybe_pointer_original_alignment(Type *t) {
  1109. t = base_type(t);
  1110. if (t->kind == Type_Union && t->Union.maybe) {
  1111. if (t->Union.variants.count == 1) {
  1112. Type *v = t->Union.variants[0];
  1113. if (is_type_pointer(v)) {
  1114. return type_align_of(v) == type_align_of(t);
  1115. }
  1116. }
  1117. }
  1118. return false;
  1119. }
  1120. bool is_type_integer_endian_big(Type *t) {
  1121. t = core_type(t);
  1122. if (t->kind == Type_Basic) {
  1123. if (t->Basic.flags & BasicFlag_EndianBig) {
  1124. return true;
  1125. } else if (t->Basic.flags & BasicFlag_EndianLittle) {
  1126. return false;
  1127. }
  1128. return build_context.endian_kind == TargetEndian_Big;
  1129. } else if (t->kind == Type_BitSet) {
  1130. return is_type_integer_endian_big(bit_set_to_int(t));
  1131. } else if (t->kind == Type_Pointer) {
  1132. return is_type_integer_endian_big(&basic_types[Basic_uintptr]);
  1133. }
  1134. return build_context.endian_kind == TargetEndian_Big;
  1135. }
  1136. bool is_type_integer_endian_little(Type *t) {
  1137. t = core_type(t);
  1138. if (t->kind == Type_Basic) {
  1139. if (t->Basic.flags & BasicFlag_EndianLittle) {
  1140. return true;
  1141. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  1142. return false;
  1143. }
  1144. return build_context.endian_kind == TargetEndian_Little;
  1145. } else if (t->kind == Type_BitSet) {
  1146. return is_type_integer_endian_little(bit_set_to_int(t));
  1147. } else if (t->kind == Type_Pointer) {
  1148. return is_type_integer_endian_little(&basic_types[Basic_uintptr]);
  1149. }
  1150. return build_context.endian_kind == TargetEndian_Little;
  1151. }
  1152. bool is_type_endian_big(Type *t) {
  1153. return is_type_integer_endian_big(t);
  1154. }
  1155. bool is_type_endian_little(Type *t) {
  1156. return is_type_integer_endian_little(t);
  1157. }
  1158. bool is_type_dereferenceable(Type *t) {
  1159. if (is_type_rawptr(t)) {
  1160. return false;
  1161. }
  1162. return is_type_pointer(t);
  1163. }
  1164. bool is_type_different_to_arch_endianness(Type *t) {
  1165. switch (build_context.endian_kind) {
  1166. case TargetEndian_Little:
  1167. return !is_type_integer_endian_little(t);
  1168. case TargetEndian_Big:
  1169. return !is_type_integer_endian_big(t);
  1170. }
  1171. return false;
  1172. }
  1173. Type *integer_endian_type_to_platform_type(Type *t) {
  1174. t = core_type(t);
  1175. if (t->kind == Type_BitSet) {
  1176. t = bit_set_to_int(t);
  1177. }
  1178. GB_ASSERT(t->kind == Type_Basic);
  1179. switch (t->Basic.kind) {
  1180. // Endian Specific Types
  1181. case Basic_i16le: return t_i16;
  1182. case Basic_u16le: return t_u16;
  1183. case Basic_i32le: return t_i32;
  1184. case Basic_u32le: return t_u32;
  1185. case Basic_i64le: return t_i64;
  1186. case Basic_u64le: return t_u64;
  1187. case Basic_i16be: return t_i16;
  1188. case Basic_u16be: return t_u16;
  1189. case Basic_i32be: return t_i32;
  1190. case Basic_u32be: return t_u32;
  1191. case Basic_i64be: return t_i64;
  1192. case Basic_u64be: return t_u64;
  1193. }
  1194. return t;
  1195. }
  1196. bool is_type_any(Type *t) {
  1197. t = base_type(t);
  1198. return (t->kind == Type_Basic && t->Basic.kind == Basic_any);
  1199. }
  1200. bool is_type_typeid(Type *t) {
  1201. t = base_type(t);
  1202. return (t->kind == Type_Basic && t->Basic.kind == Basic_typeid);
  1203. }
  1204. bool is_type_untyped_nil(Type *t) {
  1205. t = base_type(t);
  1206. return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedNil);
  1207. }
  1208. bool is_type_untyped_undef(Type *t) {
  1209. t = base_type(t);
  1210. return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUndef);
  1211. }
  1212. bool is_type_empty_union(Type *t) {
  1213. t = base_type(t);
  1214. return t->kind == Type_Union && t->Union.variants.count == 0;
  1215. }
  1216. bool is_type_empty_struct(Type *t) {
  1217. t = base_type(t);
  1218. return t->kind == Type_Struct && !t->Struct.is_raw_union && t->Struct.fields.count == 0;
  1219. }
  1220. bool is_type_valid_for_keys(Type *t) {
  1221. t = core_type(t);
  1222. if (t->kind == Type_Generic) {
  1223. return true;
  1224. }
  1225. if (is_type_untyped(t)) {
  1226. return false;
  1227. }
  1228. if (is_type_integer(t)) {
  1229. return true;
  1230. }
  1231. if (is_type_float(t)) {
  1232. return true;
  1233. }
  1234. if (is_type_string(t)) {
  1235. return true;
  1236. }
  1237. if (is_type_pointer(t)) {
  1238. return true;
  1239. }
  1240. if (is_type_typeid(t)) {
  1241. return true;
  1242. }
  1243. return false;
  1244. }
  1245. bool is_type_valid_bit_set_elem(Type *t) {
  1246. if (is_type_enum(t)) {
  1247. return true;
  1248. }
  1249. t = core_type(t);
  1250. if (t->kind == Type_Generic) {
  1251. return true;
  1252. }
  1253. return false;
  1254. }
  1255. Type *bit_set_to_int(Type *t) {
  1256. GB_ASSERT(is_type_bit_set(t));
  1257. Type *bt = base_type(t);
  1258. Type *underlying = bt->BitSet.underlying;
  1259. if (underlying != nullptr && is_type_integer(underlying)) {
  1260. return underlying;
  1261. }
  1262. i64 sz = type_size_of(t);
  1263. switch (sz) {
  1264. case 0: return t_u8;
  1265. case 1: return t_u8;
  1266. case 2: return t_u16;
  1267. case 4: return t_u32;
  1268. case 8: return t_u64;
  1269. case 16: return t_u128;
  1270. }
  1271. GB_PANIC("Unknown bit_set size");
  1272. return nullptr;
  1273. }
  1274. bool is_type_valid_vector_elem(Type *t) {
  1275. t = base_type(t);
  1276. if (t->kind == Type_Basic) {
  1277. if (t->Basic.flags & BasicFlag_EndianLittle) {
  1278. return false;
  1279. }
  1280. if (t->Basic.flags & BasicFlag_EndianBig) {
  1281. return false;
  1282. }
  1283. if (is_type_integer(t)) {
  1284. return true;
  1285. }
  1286. if (is_type_float(t)) {
  1287. return true;
  1288. }
  1289. }
  1290. return false;
  1291. }
  1292. bool is_type_indexable(Type *t) {
  1293. Type *bt = base_type(t);
  1294. switch (bt->kind) {
  1295. case Type_Basic:
  1296. return bt->Basic.kind == Basic_string;
  1297. case Type_Array:
  1298. case Type_Slice:
  1299. case Type_DynamicArray:
  1300. case Type_Map:
  1301. return true;
  1302. case Type_EnumeratedArray:
  1303. return true;
  1304. }
  1305. return false;
  1306. }
  1307. bool is_type_sliceable(Type *t) {
  1308. Type *bt = base_type(t);
  1309. switch (bt->kind) {
  1310. case Type_Basic:
  1311. return bt->Basic.kind == Basic_string;
  1312. case Type_Array:
  1313. case Type_Slice:
  1314. case Type_DynamicArray:
  1315. return true;
  1316. case Type_EnumeratedArray:
  1317. return false;
  1318. }
  1319. return false;
  1320. }
  1321. bool is_type_polymorphic_record(Type *t) {
  1322. t = base_type(t);
  1323. if (t->kind == Type_Struct) {
  1324. return t->Struct.is_polymorphic;
  1325. } else if (t->kind == Type_Union) {
  1326. return t->Union.is_polymorphic;
  1327. }
  1328. return false;
  1329. }
  1330. Scope *polymorphic_record_parent_scope(Type *t) {
  1331. t = base_type(t);
  1332. if (is_type_polymorphic_record(t)) {
  1333. if (t->kind == Type_Struct) {
  1334. return t->Struct.scope->parent;
  1335. } else if (t->kind == Type_Union) {
  1336. return t->Union.scope->parent;
  1337. }
  1338. }
  1339. return nullptr;
  1340. }
  1341. bool is_type_polymorphic_record_specialized(Type *t) {
  1342. Type *original_type = t;
  1343. t = base_type(t);
  1344. if (t->kind == Type_Struct) {
  1345. return t->Struct.is_poly_specialized;
  1346. } else if (t->kind == Type_Union) {
  1347. return t->Union.is_poly_specialized;
  1348. }
  1349. return false;
  1350. }
  1351. bool is_type_polymorphic_record_unspecialized(Type *t) {
  1352. t = base_type(t);
  1353. if (t->kind == Type_Struct) {
  1354. return t->Struct.is_polymorphic && !t->Struct.is_poly_specialized;
  1355. } else if (t->kind == Type_Struct) {
  1356. return t->Struct.is_polymorphic && !t->Struct.is_poly_specialized;
  1357. }
  1358. return false;
  1359. }
  1360. TypeTuple *get_record_polymorphic_params(Type *t) {
  1361. t = base_type(t);
  1362. switch (t->kind) {
  1363. case Type_Struct:
  1364. if (t->Struct.polymorphic_params) {
  1365. return &t->Struct.polymorphic_params->Tuple;
  1366. }
  1367. break;
  1368. case Type_Union:
  1369. if (t->Union.polymorphic_params) {
  1370. return &t->Union.polymorphic_params->Tuple;
  1371. }
  1372. break;
  1373. }
  1374. return nullptr;
  1375. }
  1376. bool is_type_polymorphic(Type *t, bool or_specialized=false) {
  1377. switch (t->kind) {
  1378. case Type_Generic:
  1379. return true;
  1380. case Type_Named:
  1381. return is_type_polymorphic(t->Named.base, or_specialized);
  1382. case Type_Opaque:
  1383. return is_type_polymorphic(t->Opaque.elem, or_specialized);
  1384. case Type_Pointer:
  1385. return is_type_polymorphic(t->Pointer.elem, or_specialized);
  1386. case Type_EnumeratedArray:
  1387. if (is_type_polymorphic(t->EnumeratedArray.index, or_specialized)) {
  1388. return true;
  1389. }
  1390. return is_type_polymorphic(t->EnumeratedArray.elem, or_specialized);
  1391. case Type_Array:
  1392. if (t->Array.generic_count != nullptr) {
  1393. return true;
  1394. }
  1395. return is_type_polymorphic(t->Array.elem, or_specialized);
  1396. case Type_DynamicArray:
  1397. return is_type_polymorphic(t->DynamicArray.elem, or_specialized);
  1398. case Type_Slice:
  1399. return is_type_polymorphic(t->Slice.elem, or_specialized);
  1400. case Type_Tuple:
  1401. for_array(i, t->Tuple.variables) {
  1402. if (is_type_polymorphic(t->Tuple.variables[i]->type, or_specialized)) {
  1403. return true;
  1404. }
  1405. }
  1406. break;
  1407. case Type_Proc:
  1408. if (t->Proc.is_polymorphic) {
  1409. return true;
  1410. }
  1411. #if 1
  1412. if (t->Proc.param_count > 0 &&
  1413. is_type_polymorphic(t->Proc.params, or_specialized)) {
  1414. return true;
  1415. }
  1416. if (t->Proc.result_count > 0 &&
  1417. is_type_polymorphic(t->Proc.results, or_specialized)) {
  1418. return true;
  1419. }
  1420. #endif
  1421. break;
  1422. case Type_Enum:
  1423. if (t->kind == Type_Enum) {
  1424. if (t->Enum.base_type != nullptr) {
  1425. return is_type_polymorphic(t->Enum.base_type, or_specialized);
  1426. }
  1427. return false;
  1428. }
  1429. break;
  1430. case Type_Union:
  1431. if (t->Union.is_polymorphic) {
  1432. return true;
  1433. }
  1434. if (or_specialized && t->Union.is_poly_specialized) {
  1435. return true;
  1436. }
  1437. // for_array(i, t->Union.variants) {
  1438. // if (is_type_polymorphic(t->Union.variants[i], or_specialized)) {
  1439. // return true;
  1440. // }
  1441. // }
  1442. break;
  1443. case Type_Struct:
  1444. if (t->Struct.is_polymorphic) {
  1445. return true;
  1446. }
  1447. if (or_specialized && t->Struct.is_poly_specialized) {
  1448. return true;
  1449. }
  1450. break;
  1451. case Type_Map:
  1452. if (t->Map.key == nullptr || t->Map.value == nullptr) {
  1453. return false;
  1454. }
  1455. if (is_type_polymorphic(t->Map.key, or_specialized)) {
  1456. return true;
  1457. }
  1458. if (is_type_polymorphic(t->Map.value, or_specialized)) {
  1459. return true;
  1460. }
  1461. break;
  1462. }
  1463. return false;
  1464. }
  1465. bool type_has_undef(Type *t) {
  1466. // t = base_type(t);
  1467. return true;
  1468. }
  1469. bool type_has_nil(Type *t) {
  1470. t = base_type(t);
  1471. switch (t->kind) {
  1472. case Type_Basic: {
  1473. switch (t->Basic.kind) {
  1474. case Basic_rawptr:
  1475. case Basic_any:
  1476. return true;
  1477. case Basic_cstring:
  1478. return true;
  1479. case Basic_typeid:
  1480. return true;
  1481. }
  1482. return false;
  1483. } break;
  1484. case Type_Enum:
  1485. case Type_BitSet:
  1486. case Type_BitField:
  1487. return true;
  1488. case Type_Slice:
  1489. case Type_Proc:
  1490. case Type_Pointer:
  1491. case Type_DynamicArray:
  1492. case Type_Map:
  1493. return true;
  1494. case Type_Union:
  1495. return !t->Union.no_nil;
  1496. case Type_Struct:
  1497. if (is_type_soa_struct(t)) {
  1498. switch (t->Struct.soa_kind) {
  1499. case StructSoa_Fixed: return false;
  1500. case StructSoa_Slice: return true;
  1501. case StructSoa_Dynamic: return true;
  1502. }
  1503. }
  1504. return false;
  1505. case Type_Opaque:
  1506. return true;
  1507. }
  1508. return false;
  1509. }
  1510. bool elem_type_can_be_constant(Type *t) {
  1511. t = base_type(t);
  1512. if (t == t_invalid) {
  1513. return false;
  1514. }
  1515. if (is_type_any(t) || is_type_union(t)) {
  1516. return false;
  1517. }
  1518. return true;
  1519. }
  1520. bool is_type_comparable(Type *t) {
  1521. t = base_type(t);
  1522. switch (t->kind) {
  1523. case Type_Basic:
  1524. switch (t->Basic.kind) {
  1525. case Basic_UntypedNil:
  1526. case Basic_any:
  1527. return false;
  1528. case Basic_rune:
  1529. return true;
  1530. case Basic_string:
  1531. return true;
  1532. case Basic_cstring:
  1533. return true;
  1534. case Basic_typeid:
  1535. return true;
  1536. }
  1537. return true;
  1538. case Type_Pointer:
  1539. return true;
  1540. case Type_Enum:
  1541. return is_type_comparable(core_type(t));
  1542. case Type_EnumeratedArray:
  1543. return is_type_comparable(t->EnumeratedArray.elem);
  1544. case Type_Array:
  1545. return is_type_comparable(t->Array.elem);
  1546. case Type_Proc:
  1547. return true;
  1548. case Type_BitSet:
  1549. return true;
  1550. case Type_BitFieldValue:
  1551. return true;
  1552. case Type_Opaque:
  1553. return is_type_comparable(t->Opaque.elem);
  1554. }
  1555. return false;
  1556. }
  1557. Type *strip_type_aliasing(Type *x) {
  1558. if (x == nullptr) {
  1559. return x;
  1560. }
  1561. if (x->kind == Type_Named) {
  1562. Entity *e = x->Named.type_name;
  1563. if (e != nullptr && e->kind == Entity_TypeName && e->TypeName.is_type_alias) {
  1564. return x->Named.base;
  1565. }
  1566. }
  1567. return x;
  1568. }
  1569. bool are_types_identical(Type *x, Type *y) {
  1570. if (x == y) {
  1571. return true;
  1572. }
  1573. if ((x == nullptr && y != nullptr) ||
  1574. (x != nullptr && y == nullptr)) {
  1575. return false;
  1576. }
  1577. x = strip_type_aliasing(x);
  1578. y = strip_type_aliasing(y);
  1579. switch (x->kind) {
  1580. case Type_Generic:
  1581. if (y->kind == Type_Generic) {
  1582. return are_types_identical(x->Generic.specialized, y->Generic.specialized);
  1583. }
  1584. break;
  1585. case Type_Opaque:
  1586. if (y->kind == Type_Opaque) {
  1587. return are_types_identical(x->Opaque.elem, y->Opaque.elem);
  1588. }
  1589. break;
  1590. case Type_Basic:
  1591. if (y->kind == Type_Basic) {
  1592. return x->Basic.kind == y->Basic.kind;
  1593. }
  1594. break;
  1595. case Type_EnumeratedArray:
  1596. if (y->kind == Type_EnumeratedArray) {
  1597. return are_types_identical(x->EnumeratedArray.index, y->EnumeratedArray.index) &&
  1598. are_types_identical(x->EnumeratedArray.elem, y->EnumeratedArray.elem);
  1599. }
  1600. break;
  1601. case Type_Array:
  1602. if (y->kind == Type_Array) {
  1603. return (x->Array.count == y->Array.count) && are_types_identical(x->Array.elem, y->Array.elem);
  1604. }
  1605. break;
  1606. case Type_DynamicArray:
  1607. if (y->kind == Type_DynamicArray) {
  1608. return are_types_identical(x->DynamicArray.elem, y->DynamicArray.elem);
  1609. }
  1610. break;
  1611. case Type_Slice:
  1612. if (y->kind == Type_Slice) {
  1613. return are_types_identical(x->Slice.elem, y->Slice.elem);
  1614. }
  1615. break;
  1616. case Type_BitField:
  1617. if (y->kind == Type_BitField) {
  1618. if (x->BitField.fields.count == y->BitField.fields.count &&
  1619. x->BitField.custom_align == y->BitField.custom_align) {
  1620. for (i32 i = 0; i < x->BitField.fields.count; i++) {
  1621. if (x->BitField.offsets[i] != y->BitField.offsets[i]) {
  1622. return false;
  1623. }
  1624. if (x->BitField.sizes[i] != y->BitField.sizes[i]) {
  1625. return false;
  1626. }
  1627. }
  1628. return true;
  1629. }
  1630. }
  1631. break;
  1632. case Type_BitSet:
  1633. if (y->kind == Type_BitSet) {
  1634. return are_types_identical(x->BitSet.elem, y->BitSet.elem) &&
  1635. are_types_identical(x->BitSet.underlying, y->BitSet.underlying) &&
  1636. x->BitSet.lower == y->BitSet.lower &&
  1637. x->BitSet.upper == y->BitSet.upper;
  1638. }
  1639. break;
  1640. case Type_Enum:
  1641. return x == y; // NOTE(bill): All enums are unique
  1642. case Type_Union:
  1643. if (y->kind == Type_Union) {
  1644. if (x->Union.variants.count == y->Union.variants.count &&
  1645. x->Union.custom_align == y->Union.custom_align &&
  1646. x->Union.no_nil == y->Union.no_nil) {
  1647. // NOTE(bill): zeroth variant is nullptr
  1648. for_array(i, x->Union.variants) {
  1649. if (!are_types_identical(x->Union.variants[i], y->Union.variants[i])) {
  1650. return false;
  1651. }
  1652. }
  1653. return true;
  1654. }
  1655. }
  1656. break;
  1657. case Type_Struct:
  1658. if (y->kind == Type_Struct) {
  1659. if (x->Struct.is_raw_union == y->Struct.is_raw_union &&
  1660. x->Struct.fields.count == y->Struct.fields.count &&
  1661. x->Struct.is_packed == y->Struct.is_packed &&
  1662. x->Struct.custom_align == y->Struct.custom_align &&
  1663. x->Struct.soa_kind == y->Struct.soa_kind &&
  1664. x->Struct.soa_count == y->Struct.soa_count &&
  1665. are_types_identical(x->Struct.soa_elem, y->Struct.soa_elem)) {
  1666. // TODO(bill); Fix the custom alignment rule
  1667. for_array(i, x->Struct.fields) {
  1668. Entity *xf = x->Struct.fields[i];
  1669. Entity *yf = y->Struct.fields[i];
  1670. if (xf->kind != yf->kind) {
  1671. return false;
  1672. }
  1673. if (!are_types_identical(xf->type, yf->type)) {
  1674. return false;
  1675. }
  1676. if (xf->token.string != yf->token.string) {
  1677. return false;
  1678. }
  1679. bool xf_is_using = (xf->flags&EntityFlag_Using) != 0;
  1680. bool yf_is_using = (yf->flags&EntityFlag_Using) != 0;
  1681. if (xf_is_using ^ yf_is_using) {
  1682. return false;
  1683. }
  1684. if (x->Struct.tags.count != y->Struct.tags.count) {
  1685. return false;
  1686. }
  1687. if (x->Struct.tags.count > 0 && x->Struct.tags[i] != y->Struct.tags[i]) {
  1688. return false;
  1689. }
  1690. }
  1691. return true;
  1692. }
  1693. }
  1694. break;
  1695. case Type_Pointer:
  1696. if (y->kind == Type_Pointer) {
  1697. return are_types_identical(x->Pointer.elem, y->Pointer.elem);
  1698. }
  1699. break;
  1700. case Type_Named:
  1701. if (y->kind == Type_Named) {
  1702. return x->Named.type_name == y->Named.type_name;
  1703. }
  1704. break;
  1705. case Type_Tuple:
  1706. if (y->kind == Type_Tuple) {
  1707. if (x->Tuple.variables.count == y->Tuple.variables.count &&
  1708. x->Tuple.is_packed == y->Tuple.is_packed) {
  1709. for_array(i, x->Tuple.variables) {
  1710. Entity *xe = x->Tuple.variables[i];
  1711. Entity *ye = y->Tuple.variables[i];
  1712. if (xe->kind != ye->kind || !are_types_identical(xe->type, ye->type)) {
  1713. return false;
  1714. }
  1715. if (xe->kind == Entity_Constant && !compare_exact_values(Token_CmpEq, xe->Constant.value, ye->Constant.value)) {
  1716. // NOTE(bill): This is needed for polymorphic procedures
  1717. return false;
  1718. }
  1719. }
  1720. return true;
  1721. }
  1722. }
  1723. break;
  1724. case Type_Proc:
  1725. if (y->kind == Type_Proc) {
  1726. return x->Proc.calling_convention == y->Proc.calling_convention &&
  1727. x->Proc.c_vararg == y->Proc.c_vararg &&
  1728. x->Proc.variadic == y->Proc.variadic &&
  1729. x->Proc.diverging == y->Proc.diverging &&
  1730. are_types_identical(x->Proc.params, y->Proc.params) &&
  1731. are_types_identical(x->Proc.results, y->Proc.results);
  1732. }
  1733. break;
  1734. case Type_Map:
  1735. if (y->kind == Type_Map) {
  1736. return are_types_identical(x->Map.key, y->Map.key) &&
  1737. are_types_identical(x->Map.value, y->Map.value);
  1738. }
  1739. break;
  1740. case Type_SimdVector:
  1741. if (y->kind == Type_SimdVector) {
  1742. if (x->SimdVector.is_x86_mmx == y->SimdVector.is_x86_mmx) {
  1743. if (x->SimdVector.is_x86_mmx) {
  1744. return true;
  1745. } else if (x->SimdVector.count == y->SimdVector.count) {
  1746. return are_types_identical(x->SimdVector.elem, y->SimdVector.elem);
  1747. }
  1748. }
  1749. }
  1750. break;
  1751. }
  1752. return false;
  1753. }
  1754. Type *default_bit_field_value_type(Type *type) {
  1755. if (type == nullptr) {
  1756. return t_invalid;
  1757. }
  1758. Type *t = base_type(type);
  1759. if (t->kind == Type_BitFieldValue) {
  1760. i32 bits = t->BitFieldValue.bits;
  1761. i32 size = 8*next_pow2((bits+7)/8);
  1762. switch (size) {
  1763. case 8: return t_u8;
  1764. case 16: return t_u16;
  1765. case 32: return t_u32;
  1766. case 64: return t_u64;
  1767. default: GB_PANIC("Too big of a bit size!"); break;
  1768. }
  1769. }
  1770. return type;
  1771. }
  1772. Type *default_type(Type *type) {
  1773. if (type == nullptr) {
  1774. return t_invalid;
  1775. }
  1776. if (type->kind == Type_Basic) {
  1777. switch (type->Basic.kind) {
  1778. case Basic_UntypedBool: return t_bool;
  1779. case Basic_UntypedInteger: return t_int;
  1780. case Basic_UntypedFloat: return t_f64;
  1781. case Basic_UntypedComplex: return t_complex128;
  1782. case Basic_UntypedQuaternion: return t_quaternion256;
  1783. case Basic_UntypedString: return t_string;
  1784. case Basic_UntypedRune: return t_rune;
  1785. }
  1786. }
  1787. if (type->kind == Type_BitFieldValue) {
  1788. return default_bit_field_value_type(type);
  1789. }
  1790. return type;
  1791. }
  1792. i64 union_variant_index(Type *u, Type *v) {
  1793. u = base_type(u);
  1794. GB_ASSERT(u->kind == Type_Union);
  1795. for_array(i, u->Union.variants) {
  1796. Type *vt = u->Union.variants[i];
  1797. if (are_types_identical(v, vt)) {
  1798. if (u->Union.no_nil) {
  1799. return cast(i64)(i+0);
  1800. } else {
  1801. return cast(i64)(i+1);
  1802. }
  1803. }
  1804. }
  1805. return 0;
  1806. }
  1807. i64 union_tag_size(Type *u) {
  1808. u = base_type(u);
  1809. GB_ASSERT(u->kind == Type_Union);
  1810. if (u->Union.tag_size > 0) {
  1811. return u->Union.tag_size;
  1812. }
  1813. u64 n = cast(u64)u->Union.variants.count;
  1814. if (n == 0) {
  1815. return 0;
  1816. }
  1817. #if 1
  1818. // TODO(bill): Is this an okay approach?
  1819. i64 max_align = 1;
  1820. for_array(i, u->Union.variants) {
  1821. Type *variant_type = u->Union.variants[i];
  1822. i64 align = type_align_of(variant_type);
  1823. if (max_align < align) {
  1824. max_align = align;
  1825. }
  1826. }
  1827. u->Union.tag_size = gb_min(max_align, build_context.max_align);
  1828. return max_align;
  1829. #else
  1830. i64 bytes = next_pow2(cast(i64)(floor_log2(n)/8 + 1));
  1831. i64 tag_size = gb_max(bytes, 1);
  1832. u->Union.tag_size = tag_size;
  1833. return tag_size;
  1834. #endif
  1835. }
  1836. Type *union_tag_type(Type *u) {
  1837. i64 s = union_tag_size(u);
  1838. switch (s) {
  1839. case 0: return t_u8;
  1840. case 1: return t_u8;
  1841. case 2: return t_u16;
  1842. case 4: return t_u32;
  1843. case 8: return t_u64;
  1844. }
  1845. GB_PANIC("Invalid union_tag_size");
  1846. return t_uint;
  1847. }
  1848. enum ProcTypeOverloadKind {
  1849. ProcOverload_Identical, // The types are identical
  1850. ProcOverload_CallingConvention,
  1851. ProcOverload_ParamCount,
  1852. ProcOverload_ParamVariadic,
  1853. ProcOverload_ParamTypes,
  1854. ProcOverload_ResultCount,
  1855. ProcOverload_ResultTypes,
  1856. ProcOverload_Polymorphic,
  1857. ProcOverload_NotProcedure,
  1858. };
  1859. ProcTypeOverloadKind are_proc_types_overload_safe(Type *x, Type *y) {
  1860. if (x == nullptr && y == nullptr) return ProcOverload_NotProcedure;
  1861. if (x == nullptr && y != nullptr) return ProcOverload_NotProcedure;
  1862. if (x != nullptr && y == nullptr) return ProcOverload_NotProcedure;
  1863. if (!is_type_proc(x)) return ProcOverload_NotProcedure;
  1864. if (!is_type_proc(y)) return ProcOverload_NotProcedure;
  1865. TypeProc px = base_type(x)->Proc;
  1866. TypeProc py = base_type(y)->Proc;
  1867. // if (px.calling_convention != py.calling_convention) {
  1868. // return ProcOverload_CallingConvention;
  1869. // }
  1870. // if (px.is_polymorphic != py.is_polymorphic) {
  1871. // return ProcOverload_Polymorphic;
  1872. // }
  1873. if (px.param_count != py.param_count) {
  1874. return ProcOverload_ParamCount;
  1875. }
  1876. for (isize i = 0; i < px.param_count; i++) {
  1877. Entity *ex = px.params->Tuple.variables[i];
  1878. Entity *ey = py.params->Tuple.variables[i];
  1879. if (!are_types_identical(ex->type, ey->type)) {
  1880. return ProcOverload_ParamTypes;
  1881. }
  1882. }
  1883. // IMPORTANT TODO(bill): Determine the rules for overloading procedures with variadic parameters
  1884. if (px.variadic != py.variadic) {
  1885. return ProcOverload_ParamVariadic;
  1886. }
  1887. if (px.is_polymorphic != py.is_polymorphic) {
  1888. return ProcOverload_Polymorphic;
  1889. }
  1890. if (px.result_count != py.result_count) {
  1891. return ProcOverload_ResultCount;
  1892. }
  1893. for (isize i = 0; i < px.result_count; i++) {
  1894. Entity *ex = px.results->Tuple.variables[i];
  1895. Entity *ey = py.results->Tuple.variables[i];
  1896. if (!are_types_identical(ex->type, ey->type)) {
  1897. return ProcOverload_ResultTypes;
  1898. }
  1899. }
  1900. if (px.params != nullptr && py.params != nullptr) {
  1901. Entity *ex = px.params->Tuple.variables[0];
  1902. Entity *ey = py.params->Tuple.variables[0];
  1903. bool ok = are_types_identical(ex->type, ey->type);
  1904. if (ok) {
  1905. }
  1906. }
  1907. return ProcOverload_Identical;
  1908. }
  1909. Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident=false);
  1910. Selection lookup_field(Type *type_, String field_name, bool is_type, bool allow_blank_ident=false) {
  1911. return lookup_field_with_selection(type_, field_name, is_type, empty_selection, allow_blank_ident);
  1912. }
  1913. Selection lookup_field_from_index(Type *type, i64 index) {
  1914. GB_ASSERT(is_type_struct(type) || is_type_union(type) || is_type_tuple(type));
  1915. type = base_type(type);
  1916. gbAllocator a = heap_allocator();
  1917. isize max_count = 0;
  1918. switch (type->kind) {
  1919. case Type_Struct: max_count = type->Struct.fields.count; break;
  1920. case Type_Tuple: max_count = type->Tuple.variables.count; break;
  1921. case Type_BitField: max_count = type->BitField.fields.count; break;
  1922. }
  1923. if (index >= max_count) {
  1924. return empty_selection;
  1925. }
  1926. switch (type->kind) {
  1927. case Type_Struct:
  1928. for (isize i = 0; i < max_count; i++) {
  1929. Entity *f = type->Struct.fields[i];
  1930. if (f->kind == Entity_Variable) {
  1931. if (f->Variable.field_src_index == index) {
  1932. auto sel_array = array_make<i32>(a, 1);
  1933. sel_array[0] = cast(i32)i;
  1934. return make_selection(f, sel_array, false);
  1935. }
  1936. }
  1937. }
  1938. break;
  1939. case Type_Tuple:
  1940. for (isize i = 0; i < max_count; i++) {
  1941. Entity *f = type->Tuple.variables[i];
  1942. if (i == index) {
  1943. auto sel_array = array_make<i32>(a, 1);
  1944. sel_array[0] = cast(i32)i;
  1945. return make_selection(f, sel_array, false);
  1946. }
  1947. }
  1948. break;
  1949. case Type_BitField: {
  1950. auto sel_array = array_make<i32>(a, 1);
  1951. sel_array[0] = cast(i32)index;
  1952. return make_selection(type->BitField.fields[cast(isize)index], sel_array, false);
  1953. } break;
  1954. }
  1955. GB_PANIC("Illegal index");
  1956. return empty_selection;
  1957. }
  1958. Entity *scope_lookup_current(Scope *s, String name);
  1959. Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident) {
  1960. GB_ASSERT(type_ != nullptr);
  1961. if (!allow_blank_ident && is_blank_ident(field_name)) {
  1962. return empty_selection;
  1963. }
  1964. gbAllocator a = heap_allocator();
  1965. Type *type = type_deref(type_);
  1966. bool is_ptr = type != type_;
  1967. sel.indirect = sel.indirect || is_ptr;
  1968. type = base_type(type);
  1969. if (is_type) {
  1970. switch (type->kind) {
  1971. case Type_Struct:
  1972. if (type->Struct.names != nullptr &&
  1973. field_name == "names") {
  1974. sel.entity = type->Struct.names;
  1975. return sel;
  1976. }
  1977. break;
  1978. case Type_Enum:
  1979. if (type->Enum.names != nullptr &&
  1980. field_name == "names") {
  1981. sel.entity = type->Enum.names;
  1982. return sel;
  1983. }
  1984. break;
  1985. }
  1986. if (is_type_enum(type)) {
  1987. // NOTE(bill): These may not have been added yet, so check in case
  1988. for_array(i, type->Enum.fields) {
  1989. Entity *f = type->Enum.fields[i];
  1990. GB_ASSERT(f->kind == Entity_Constant);
  1991. String str = f->token.string;
  1992. if (field_name == str) {
  1993. sel.entity = f;
  1994. // selection_add_index(&sel, i);
  1995. return sel;
  1996. }
  1997. }
  1998. }
  1999. if (type->kind == Type_Struct) {
  2000. Scope *s = type->Struct.scope;
  2001. if (s != nullptr) {
  2002. Entity *found = scope_lookup_current(s, field_name);
  2003. if (found != nullptr && found->kind != Entity_Variable) {
  2004. sel.entity = found;
  2005. return sel;
  2006. }
  2007. }
  2008. } else if (type->kind == Type_Union) {
  2009. Scope *s = type->Union.scope;
  2010. if (s != nullptr) {
  2011. Entity *found = scope_lookup_current(s, field_name);
  2012. if (found != nullptr && found->kind != Entity_Variable) {
  2013. sel.entity = found;
  2014. return sel;
  2015. }
  2016. }
  2017. } else if (type->kind == Type_BitSet) {
  2018. return lookup_field_with_selection(type->BitSet.elem, field_name, true, sel, allow_blank_ident);
  2019. }
  2020. if (type->kind == Type_Generic && type->Generic.specialized != nullptr) {
  2021. Type *specialized = type->Generic.specialized;
  2022. return lookup_field_with_selection(specialized, field_name, is_type, sel, allow_blank_ident);
  2023. }
  2024. } else if (type->kind == Type_Union) {
  2025. } else if (type->kind == Type_Struct) {
  2026. for_array(i, type->Struct.fields) {
  2027. Entity *f = type->Struct.fields[i];
  2028. if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
  2029. continue;
  2030. }
  2031. String str = f->token.string;
  2032. if (field_name == str) {
  2033. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  2034. sel.entity = f;
  2035. return sel;
  2036. }
  2037. if (f->flags & EntityFlag_Using) {
  2038. isize prev_count = sel.index.count;
  2039. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  2040. sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
  2041. if (sel.entity != nullptr) {
  2042. if (is_type_pointer(f->type)) {
  2043. sel.indirect = true;
  2044. }
  2045. return sel;
  2046. }
  2047. sel.index.count = prev_count;
  2048. }
  2049. }
  2050. bool is_soa = type->Struct.soa_kind != StructSoa_None;
  2051. bool is_soa_of_array = is_soa && is_type_array(type->Struct.soa_elem);
  2052. if (is_soa_of_array) {
  2053. String mapped_field_name = {};
  2054. if (field_name == "r") mapped_field_name = str_lit("x");
  2055. else if (field_name == "g") mapped_field_name = str_lit("y");
  2056. else if (field_name == "b") mapped_field_name = str_lit("z");
  2057. else if (field_name == "a") mapped_field_name = str_lit("w");
  2058. return lookup_field_with_selection(type, mapped_field_name, is_type, sel, allow_blank_ident);
  2059. }
  2060. } else if (type->kind == Type_BitField) {
  2061. for_array(i, type->BitField.fields) {
  2062. Entity *f = type->BitField.fields[i];
  2063. if (f->kind != Entity_Variable ||
  2064. (f->flags & EntityFlag_BitFieldValue) == 0) {
  2065. continue;
  2066. }
  2067. String str = f->token.string;
  2068. if (field_name == str) {
  2069. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  2070. sel.entity = f;
  2071. return sel;
  2072. }
  2073. }
  2074. } else if (type->kind == Type_Basic) {
  2075. switch (type->Basic.kind) {
  2076. case Basic_any: {
  2077. #if 1
  2078. // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
  2079. // `Raw_Any` type?
  2080. String data_str = str_lit("data");
  2081. String id_str = str_lit("id");
  2082. gb_local_persist Entity *entity__any_data = alloc_entity_field(nullptr, make_token_ident(data_str), t_rawptr, false, 0);
  2083. gb_local_persist Entity *entity__any_id = alloc_entity_field(nullptr, make_token_ident(id_str), t_typeid, false, 1);
  2084. if (field_name == data_str) {
  2085. selection_add_index(&sel, 0);
  2086. sel.entity = entity__any_data;
  2087. return sel;
  2088. } else if (field_name == id_str) {
  2089. selection_add_index(&sel, 1);
  2090. sel.entity = entity__any_id;
  2091. return sel;
  2092. }
  2093. #endif
  2094. } break;
  2095. case Basic_quaternion128: {
  2096. // @QuaternionLayout
  2097. gb_local_persist String w = str_lit("w");
  2098. gb_local_persist String x = str_lit("x");
  2099. gb_local_persist String y = str_lit("y");
  2100. gb_local_persist String z = str_lit("z");
  2101. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_f32, false, 3);
  2102. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_f32, false, 0);
  2103. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_f32, false, 1);
  2104. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_f32, false, 2);
  2105. if (field_name == w) {
  2106. selection_add_index(&sel, 3);
  2107. sel.entity = entity__w;
  2108. return sel;
  2109. } else if (field_name == x) {
  2110. selection_add_index(&sel, 0);
  2111. sel.entity = entity__x;
  2112. return sel;
  2113. } else if (field_name == y) {
  2114. selection_add_index(&sel, 1);
  2115. sel.entity = entity__y;
  2116. return sel;
  2117. } else if (field_name == z) {
  2118. selection_add_index(&sel, 2);
  2119. sel.entity = entity__z;
  2120. return sel;
  2121. }
  2122. } break;
  2123. case Basic_quaternion256: {
  2124. // @QuaternionLayout
  2125. gb_local_persist String w = str_lit("w");
  2126. gb_local_persist String x = str_lit("x");
  2127. gb_local_persist String y = str_lit("y");
  2128. gb_local_persist String z = str_lit("z");
  2129. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_f64, false, 3);
  2130. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_f64, false, 0);
  2131. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_f64, false, 1);
  2132. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_f64, false, 2);
  2133. if (field_name == w) {
  2134. selection_add_index(&sel, 3);
  2135. sel.entity = entity__w;
  2136. return sel;
  2137. } else if (field_name == x) {
  2138. selection_add_index(&sel, 0);
  2139. sel.entity = entity__x;
  2140. return sel;
  2141. } else if (field_name == y) {
  2142. selection_add_index(&sel, 1);
  2143. sel.entity = entity__y;
  2144. return sel;
  2145. } else if (field_name == z) {
  2146. selection_add_index(&sel, 2);
  2147. sel.entity = entity__z;
  2148. return sel;
  2149. }
  2150. } break;
  2151. case Basic_UntypedQuaternion: {
  2152. // @QuaternionLayout
  2153. gb_local_persist String w = str_lit("w");
  2154. gb_local_persist String x = str_lit("x");
  2155. gb_local_persist String y = str_lit("y");
  2156. gb_local_persist String z = str_lit("z");
  2157. gb_local_persist Entity *entity__w = alloc_entity_field(nullptr, make_token_ident(w), t_untyped_float, false, 3);
  2158. gb_local_persist Entity *entity__x = alloc_entity_field(nullptr, make_token_ident(x), t_untyped_float, false, 0);
  2159. gb_local_persist Entity *entity__y = alloc_entity_field(nullptr, make_token_ident(y), t_untyped_float, false, 1);
  2160. gb_local_persist Entity *entity__z = alloc_entity_field(nullptr, make_token_ident(z), t_untyped_float, false, 2);
  2161. if (field_name == w) {
  2162. selection_add_index(&sel, 3);
  2163. sel.entity = entity__w;
  2164. return sel;
  2165. } else if (field_name == x) {
  2166. selection_add_index(&sel, 0);
  2167. sel.entity = entity__x;
  2168. return sel;
  2169. } else if (field_name == y) {
  2170. selection_add_index(&sel, 1);
  2171. sel.entity = entity__y;
  2172. return sel;
  2173. } else if (field_name == z) {
  2174. selection_add_index(&sel, 2);
  2175. sel.entity = entity__z;
  2176. return sel;
  2177. }
  2178. } break;
  2179. }
  2180. return sel;
  2181. } else if (type->kind == Type_Array) {
  2182. if (type->Array.count <= 4) {
  2183. // HACK(bill): Memory leak
  2184. switch (type->Array.count) {
  2185. #define _ARRAY_FIELD_CASE_IF(_length, _name) \
  2186. if (field_name == (_name)) { \
  2187. selection_add_index(&sel, (_length)-1); \
  2188. sel.entity = alloc_entity_array_elem(nullptr, make_token_ident(str_lit(_name)), type->Array.elem, (_length)-1); \
  2189. return sel; \
  2190. }
  2191. #define _ARRAY_FIELD_CASE(_length, _name0, _name1) \
  2192. case (_length): \
  2193. _ARRAY_FIELD_CASE_IF(_length, _name0); \
  2194. _ARRAY_FIELD_CASE_IF(_length, _name1); \
  2195. /*fallthrough*/
  2196. _ARRAY_FIELD_CASE(4, "w", "a");
  2197. _ARRAY_FIELD_CASE(3, "z", "b");
  2198. _ARRAY_FIELD_CASE(2, "y", "g");
  2199. _ARRAY_FIELD_CASE(1, "x", "r");
  2200. default: break;
  2201. #undef _ARRAY_FIELD_CASE
  2202. }
  2203. }
  2204. } else if (type->kind == Type_DynamicArray) {
  2205. // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
  2206. // `Raw_Dynamic_Array` type?
  2207. GB_ASSERT(t_allocator != nullptr);
  2208. String allocator_str = str_lit("allocator");
  2209. gb_local_persist Entity *entity__allocator = alloc_entity_field(nullptr, make_token_ident(allocator_str), t_allocator, false, 3);
  2210. if (field_name == allocator_str) {
  2211. selection_add_index(&sel, 3);
  2212. sel.entity = entity__allocator;
  2213. return sel;
  2214. }
  2215. } else if (type->kind == Type_Map) {
  2216. // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
  2217. // `Raw_Map` type?
  2218. GB_ASSERT(t_allocator != nullptr);
  2219. String allocator_str = str_lit("allocator");
  2220. gb_local_persist Entity *entity__allocator = alloc_entity_field(nullptr, make_token_ident(allocator_str), t_allocator, false, 3);
  2221. if (field_name == allocator_str) {
  2222. selection_add_index(&sel, 1);
  2223. selection_add_index(&sel, 3);
  2224. sel.entity = entity__allocator;
  2225. return sel;
  2226. }
  2227. }
  2228. return sel;
  2229. }
  2230. // IMPORTANT TODO(bill): SHould this TypePath code be removed since type cycle checking is handled much earlier on?
  2231. struct TypePath {
  2232. Array<Entity *> path; // Entity_TypeName;
  2233. bool failure;
  2234. };
  2235. void type_path_init(TypePath *tp) {
  2236. tp->path.allocator = heap_allocator();
  2237. }
  2238. void type_path_free(TypePath *tp) {
  2239. array_free(&tp->path);
  2240. }
  2241. void type_path_print_illegal_cycle(TypePath *tp, isize start_index) {
  2242. GB_ASSERT(tp != nullptr);
  2243. GB_ASSERT(start_index < tp->path.count);
  2244. Entity *e = tp->path[start_index];
  2245. GB_ASSERT(e != nullptr);
  2246. error(e->token, "Illegal declaration cycle of `%.*s`", LIT(e->token.string));
  2247. // NOTE(bill): Print cycle, if it's deep enough
  2248. for (isize j = start_index; j < tp->path.count; j++) {
  2249. Entity *e = tp->path[j];
  2250. error(e->token, "\t%.*s refers to", LIT(e->token.string));
  2251. }
  2252. // NOTE(bill): This will only print if the path count > 1
  2253. error(e->token, "\t%.*s", LIT(e->token.string));
  2254. tp->failure = true;
  2255. e->type->failure = true;
  2256. base_type(e->type)->failure = true;
  2257. }
  2258. bool type_path_push(TypePath *tp, Type *t) {
  2259. GB_ASSERT(tp != nullptr);
  2260. if (t->kind != Type_Named) {
  2261. return false;
  2262. }
  2263. Entity *e = t->Named.type_name;
  2264. for (isize i = 0; i < tp->path.count; i++) {
  2265. Entity *p = tp->path[i];
  2266. if (p == e) {
  2267. type_path_print_illegal_cycle(tp, i);
  2268. }
  2269. }
  2270. array_add(&tp->path, e);
  2271. return true;
  2272. }
  2273. void type_path_pop(TypePath *tp) {
  2274. if (tp != nullptr && tp->path.count > 0) {
  2275. array_pop(&tp->path);
  2276. }
  2277. }
  2278. #define FAILURE_SIZE 0
  2279. #define FAILURE_ALIGNMENT 0
  2280. i64 type_size_of_internal (Type *t, TypePath *path);
  2281. i64 type_align_of_internal(Type *t, TypePath *path);
  2282. i64 type_size_of(Type *t) {
  2283. if (t == nullptr) {
  2284. return 0;
  2285. }
  2286. // NOTE(bill): Always calculate the size when it is a Type_Basic
  2287. if (t->kind == Type_Named && t->cached_size >= 0) {
  2288. } else if (t->kind != Type_Basic && t->cached_size >= 0) {
  2289. return t->cached_size;
  2290. }
  2291. TypePath path = {0};
  2292. type_path_init(&path);
  2293. t->cached_size = type_size_of_internal(t, &path);
  2294. type_path_free(&path);
  2295. return t->cached_size;
  2296. }
  2297. i64 type_align_of(Type *t) {
  2298. if (t == nullptr) {
  2299. return 1;
  2300. }
  2301. // NOTE(bill): Always calculate the size when it is a Type_Basic
  2302. if (t->kind == Type_Named && t->cached_align >= 0) {
  2303. } if (t->kind != Type_Basic && t->cached_align > 0) {
  2304. return t->cached_align;
  2305. }
  2306. TypePath path = {0};
  2307. type_path_init(&path);
  2308. t->cached_align = type_align_of_internal(t, &path);
  2309. type_path_free(&path);
  2310. return t->cached_align;
  2311. }
  2312. i64 type_align_of_internal(Type *t, TypePath *path) {
  2313. GB_ASSERT(path != nullptr);
  2314. if (t->failure) {
  2315. return FAILURE_ALIGNMENT;
  2316. }
  2317. t = base_type(t);
  2318. switch (t->kind) {
  2319. case Type_Basic: {
  2320. GB_ASSERT(is_type_typed(t));
  2321. switch (t->Basic.kind) {
  2322. case Basic_string: return build_context.word_size;
  2323. case Basic_cstring: return build_context.word_size;
  2324. case Basic_any: return build_context.word_size;
  2325. case Basic_typeid: return build_context.word_size;
  2326. case Basic_int: case Basic_uint: case Basic_uintptr: case Basic_rawptr:
  2327. return build_context.word_size;
  2328. case Basic_complex64: case Basic_complex128:
  2329. return type_size_of_internal(t, path) / 2;
  2330. case Basic_quaternion128: case Basic_quaternion256:
  2331. return type_size_of_internal(t, path) / 4;
  2332. }
  2333. } break;
  2334. case Type_Array: {
  2335. Type *elem = t->Array.elem;
  2336. bool pop = type_path_push(path, elem);
  2337. if (path->failure) {
  2338. return FAILURE_ALIGNMENT;
  2339. }
  2340. i64 align = type_align_of_internal(t->Array.elem, path);
  2341. if (pop) type_path_pop(path);
  2342. return align;
  2343. }
  2344. case Type_EnumeratedArray: {
  2345. Type *elem = t->EnumeratedArray.elem;
  2346. bool pop = type_path_push(path, elem);
  2347. if (path->failure) {
  2348. return FAILURE_ALIGNMENT;
  2349. }
  2350. i64 align = type_align_of_internal(t->EnumeratedArray.elem, path);
  2351. if (pop) type_path_pop(path);
  2352. return align;
  2353. }
  2354. case Type_Opaque:
  2355. return type_align_of_internal(t->Opaque.elem, path);
  2356. case Type_DynamicArray:
  2357. // data, count, capacity, allocator
  2358. return build_context.word_size;
  2359. case Type_Slice:
  2360. return build_context.word_size;
  2361. case Type_Tuple: {
  2362. i64 max = 1;
  2363. for_array(i, t->Tuple.variables) {
  2364. i64 align = type_align_of_internal(t->Tuple.variables[i]->type, path);
  2365. if (max < align) {
  2366. max = align;
  2367. }
  2368. }
  2369. return max;
  2370. } break;
  2371. case Type_Map:
  2372. init_map_internal_types(t);
  2373. return type_align_of_internal(t->Map.internal_type, path);
  2374. case Type_Enum:
  2375. return type_align_of_internal(t->Enum.base_type, path);
  2376. case Type_Union: {
  2377. if (t->Union.variants.count == 0) {
  2378. return 1;
  2379. }
  2380. if (t->Union.custom_align > 0) {
  2381. return gb_clamp(t->Union.custom_align, 1, build_context.max_align);
  2382. }
  2383. i64 max = 1;
  2384. for_array(i, t->Union.variants) {
  2385. Type *variant = t->Union.variants[i];
  2386. bool pop = type_path_push(path, variant);
  2387. if (path->failure) {
  2388. return FAILURE_ALIGNMENT;
  2389. }
  2390. i64 align = type_align_of_internal(variant, path);
  2391. if (pop) type_path_pop(path);
  2392. if (max < align) {
  2393. max = align;
  2394. }
  2395. }
  2396. return max;
  2397. } break;
  2398. case Type_Struct: {
  2399. if (t->Struct.custom_align > 0) {
  2400. return gb_clamp(t->Struct.custom_align, 1, build_context.max_align);
  2401. }
  2402. if (t->Struct.is_raw_union) {
  2403. i64 max = 1;
  2404. for_array(i, t->Struct.fields) {
  2405. Type *field_type = t->Struct.fields[i]->type;
  2406. bool pop = type_path_push(path, field_type);
  2407. if (path->failure) {
  2408. return FAILURE_ALIGNMENT;
  2409. }
  2410. i64 align = type_align_of_internal(field_type, path);
  2411. if (pop) type_path_pop(path);
  2412. if (max < align) {
  2413. max = align;
  2414. }
  2415. }
  2416. return max;
  2417. } else if (t->Struct.fields.count > 0) {
  2418. i64 max = 1;
  2419. // NOTE(bill): Check the fields to check for cyclic definitions
  2420. for_array(i, t->Struct.fields) {
  2421. Type *field_type = t->Struct.fields[i]->type;
  2422. bool pop = type_path_push(path, field_type);
  2423. if (path->failure) return FAILURE_ALIGNMENT;
  2424. i64 align = type_align_of_internal(field_type, path);
  2425. if (pop) type_path_pop(path);
  2426. if (max < align) {
  2427. max = align;
  2428. }
  2429. }
  2430. if (t->Struct.is_packed) {
  2431. return 1;
  2432. }
  2433. return max;
  2434. }
  2435. } break;
  2436. case Type_BitField: {
  2437. i64 align = 1;
  2438. if (t->BitField.custom_align > 0) {
  2439. align = t->BitField.custom_align;
  2440. }
  2441. return gb_clamp(next_pow2(align), 1, build_context.max_align);
  2442. } break;
  2443. case Type_BitSet: {
  2444. if (t->BitSet.underlying != nullptr) {
  2445. return type_align_of(t->BitSet.underlying);
  2446. }
  2447. i64 bits = t->BitSet.upper - t->BitSet.lower + 1;
  2448. if (bits <= 8) return 1;
  2449. if (bits <= 16) return 2;
  2450. if (bits <= 32) return 4;
  2451. if (bits <= 64) return 8;
  2452. if (bits <= 128) return 16;
  2453. return 8; // NOTE(bill): Could be an invalid range so limit it for now
  2454. }
  2455. case Type_SimdVector: {
  2456. if (t->SimdVector.is_x86_mmx) {
  2457. return 8;
  2458. }
  2459. // align of
  2460. i64 count = t->SimdVector.count;
  2461. Type *elem = t->SimdVector.elem;
  2462. i64 size = count * type_size_of_internal(elem, path);
  2463. // IMPORTANT TODO(bill): Figure out the alignment of vector types
  2464. return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.max_align);
  2465. }
  2466. }
  2467. // return gb_clamp(next_pow2(type_size_of(t)), 1, build_context.max_align);
  2468. // NOTE(bill): Things that are bigger than build_context.word_size, are actually comprised of smaller types
  2469. // TODO(bill): Is this correct for 128-bit types (integers)?
  2470. return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.word_size);
  2471. }
  2472. Array<i64> type_set_offsets_of(Array<Entity *> const &fields, bool is_packed, bool is_raw_union) {
  2473. gbAllocator a = heap_allocator();
  2474. auto offsets = array_make<i64>(a, fields.count);
  2475. i64 curr_offset = 0;
  2476. if (is_raw_union) {
  2477. for_array(i, fields) {
  2478. offsets[i] = 0;
  2479. }
  2480. } else if (is_packed) {
  2481. for_array(i, fields) {
  2482. i64 size = type_size_of(fields[i]->type);
  2483. offsets[i] = curr_offset;
  2484. curr_offset += size;
  2485. }
  2486. } else {
  2487. for_array(i, fields) {
  2488. Type *t = fields[i]->type;
  2489. i64 align = gb_max(type_align_of(t), 1);
  2490. i64 size = gb_max(type_size_of( t), 0);
  2491. curr_offset = align_formula(curr_offset, align);
  2492. offsets[i] = curr_offset;
  2493. curr_offset += size;
  2494. }
  2495. }
  2496. return offsets;
  2497. }
  2498. bool type_set_offsets(Type *t) {
  2499. t = base_type(t);
  2500. if (t->kind == Type_Struct) {
  2501. if (!t->Struct.are_offsets_set) {
  2502. t->Struct.are_offsets_being_processed = true;
  2503. t->Struct.offsets = type_set_offsets_of(t->Struct.fields, t->Struct.is_packed, t->Struct.is_raw_union);
  2504. GB_ASSERT(t->Struct.offsets.count == t->Struct.fields.count);
  2505. t->Struct.are_offsets_being_processed = false;
  2506. t->Struct.are_offsets_set = true;
  2507. return true;
  2508. }
  2509. } else if (is_type_tuple(t)) {
  2510. if (!t->Tuple.are_offsets_set) {
  2511. t->Tuple.are_offsets_being_processed = true;
  2512. t->Tuple.offsets = type_set_offsets_of(t->Tuple.variables, t->Tuple.is_packed, false);
  2513. t->Tuple.are_offsets_being_processed = false;
  2514. t->Tuple.are_offsets_set = true;
  2515. return true;
  2516. }
  2517. } else {
  2518. GB_PANIC("Invalid type for setting offsets");
  2519. }
  2520. return false;
  2521. }
  2522. i64 type_size_of_internal(Type *t, TypePath *path) {
  2523. if (t->failure) {
  2524. return FAILURE_SIZE;
  2525. }
  2526. switch (t->kind) {
  2527. case Type_Named: {
  2528. bool pop = type_path_push(path, t);
  2529. if (path->failure) {
  2530. return FAILURE_ALIGNMENT;
  2531. }
  2532. i64 size = type_size_of_internal(t->Named.base, path);
  2533. if (pop) type_path_pop(path);
  2534. return size;
  2535. } break;
  2536. case Type_Basic: {
  2537. GB_ASSERT_MSG(is_type_typed(t), "%s", type_to_string(t));
  2538. BasicKind kind = t->Basic.kind;
  2539. i64 size = t->Basic.size;
  2540. if (size > 0) {
  2541. return size;
  2542. }
  2543. switch (kind) {
  2544. case Basic_string: return 2*build_context.word_size;
  2545. case Basic_cstring: return build_context.word_size;
  2546. case Basic_any: return 2*build_context.word_size;
  2547. case Basic_typeid: return build_context.word_size;
  2548. case Basic_int: case Basic_uint: case Basic_uintptr: case Basic_rawptr:
  2549. return build_context.word_size;
  2550. }
  2551. } break;
  2552. case Type_Pointer:
  2553. return build_context.word_size;
  2554. case Type_Opaque:
  2555. return type_size_of_internal(t->Opaque.elem, path);
  2556. case Type_Array: {
  2557. i64 count, align, size, alignment;
  2558. count = t->Array.count;
  2559. if (count == 0) {
  2560. return 0;
  2561. }
  2562. align = type_align_of_internal(t->Array.elem, path);
  2563. if (path->failure) {
  2564. return FAILURE_SIZE;
  2565. }
  2566. size = type_size_of_internal( t->Array.elem, path);
  2567. alignment = align_formula(size, align);
  2568. return alignment*(count-1) + size;
  2569. } break;
  2570. case Type_EnumeratedArray: {
  2571. i64 count, align, size, alignment;
  2572. count = t->EnumeratedArray.count;
  2573. if (count == 0) {
  2574. return 0;
  2575. }
  2576. align = type_align_of_internal(t->EnumeratedArray.elem, path);
  2577. if (path->failure) {
  2578. return FAILURE_SIZE;
  2579. }
  2580. size = type_size_of_internal( t->EnumeratedArray.elem, path);
  2581. alignment = align_formula(size, align);
  2582. return alignment*(count-1) + size;
  2583. } break;
  2584. case Type_Slice: // ptr + len
  2585. return 2 * build_context.word_size;
  2586. case Type_DynamicArray:
  2587. // data + len + cap + allocator(procedure+data)
  2588. return 3*build_context.word_size + 2*build_context.word_size;
  2589. case Type_Map:
  2590. init_map_internal_types(t);
  2591. return type_size_of_internal(t->Map.internal_type, path);
  2592. case Type_Tuple: {
  2593. i64 count, align, size;
  2594. count = t->Tuple.variables.count;
  2595. if (count == 0) {
  2596. return 0;
  2597. }
  2598. align = type_align_of_internal(t, path);
  2599. type_set_offsets(t);
  2600. size = t->Tuple.offsets[cast(isize)count-1] + type_size_of_internal(t->Tuple.variables[cast(isize)count-1]->type, path);
  2601. return align_formula(size, align);
  2602. } break;
  2603. case Type_Enum:
  2604. return type_size_of_internal(t->Enum.base_type, path);
  2605. case Type_Union: {
  2606. if (t->Union.variants.count == 0) {
  2607. return 0;
  2608. }
  2609. i64 align = type_align_of_internal(t, path);
  2610. if (path->failure) {
  2611. return FAILURE_SIZE;
  2612. }
  2613. i64 max = 0;
  2614. i64 field_size = 0;
  2615. for_array(i, t->Union.variants) {
  2616. Type *variant_type = t->Union.variants[i];
  2617. i64 size = type_size_of_internal(variant_type, path);
  2618. if (max < size) {
  2619. max = size;
  2620. }
  2621. }
  2622. i64 size = 0;
  2623. if (is_type_union_maybe_pointer(t)) {
  2624. size = max;
  2625. t->Union.tag_size = 0;
  2626. t->Union.variant_block_size = size;
  2627. } else {
  2628. // NOTE(bill): Align to tag
  2629. i64 tag_size = union_tag_size(t);
  2630. size = align_formula(max, tag_size);
  2631. // NOTE(bill): Calculate the padding between the common fields and the tag
  2632. t->Union.tag_size = tag_size;
  2633. t->Union.variant_block_size = size - field_size;
  2634. size += tag_size;
  2635. }
  2636. return align_formula(size, align);
  2637. } break;
  2638. case Type_Struct: {
  2639. if (t->Struct.is_raw_union) {
  2640. i64 count = t->Struct.fields.count;
  2641. i64 align = type_align_of_internal(t, path);
  2642. if (path->failure) {
  2643. return FAILURE_SIZE;
  2644. }
  2645. i64 max = 0;
  2646. for (isize i = 0; i < count; i++) {
  2647. i64 size = type_size_of_internal(t->Struct.fields[i]->type, path);
  2648. if (max < size) {
  2649. max = size;
  2650. }
  2651. }
  2652. // TODO(bill): Is this how it should work?
  2653. return align_formula(max, align);
  2654. } else {
  2655. i64 count = 0, size = 0, align = 0;
  2656. count = t->Struct.fields.count;
  2657. if (count == 0) {
  2658. return 0;
  2659. }
  2660. align = type_align_of_internal(t, path);
  2661. if (path->failure) {
  2662. return FAILURE_SIZE;
  2663. }
  2664. if (t->Struct.are_offsets_being_processed && t->Struct.offsets.data == nullptr) {
  2665. type_path_print_illegal_cycle(path, path->path.count-1);
  2666. return FAILURE_SIZE;
  2667. }
  2668. if (t->Struct.are_offsets_set && t->Struct.offsets.count != t->Struct.fields.count) {
  2669. // TODO(bill, 2019-04-28): Determine exactly why the offsets length is different thatn the field length
  2670. // Are the the same at some point and then the struct length is increased?
  2671. // Why is this not handled by the type cycle checker?
  2672. t->Struct.are_offsets_set = false;
  2673. }
  2674. type_set_offsets(t);
  2675. GB_ASSERT_MSG(t->Struct.offsets.count == t->Struct.fields.count, "%s", type_to_string(t));
  2676. size = t->Struct.offsets[cast(isize)count-1] + type_size_of_internal(t->Struct.fields[cast(isize)count-1]->type, path);
  2677. return align_formula(size, align);
  2678. }
  2679. } break;
  2680. case Type_BitField: {
  2681. i64 align = 8*type_align_of_internal(t, path);
  2682. i64 end = 0;
  2683. if (t->BitField.fields.count > 0) {
  2684. i64 last = t->BitField.fields.count-1;
  2685. end = t->BitField.offsets[cast(isize)last] + t->BitField.sizes[cast(isize)last];
  2686. }
  2687. i64 bits = align_formula(end, align);
  2688. GB_ASSERT((bits%8) == 0);
  2689. return bits/8;
  2690. } break;
  2691. case Type_BitSet: {
  2692. if (t->BitSet.underlying != nullptr) {
  2693. return type_size_of(t->BitSet.underlying);
  2694. }
  2695. i64 bits = t->BitSet.upper - t->BitSet.lower + 1;
  2696. if (bits <= 8) return 1;
  2697. if (bits <= 16) return 2;
  2698. if (bits <= 32) return 4;
  2699. if (bits <= 64) return 8;
  2700. if (bits <= 128) return 16;
  2701. return 8; // NOTE(bill): Could be an invalid range so limit it for now
  2702. }
  2703. case Type_SimdVector: {
  2704. if (t->SimdVector.is_x86_mmx) {
  2705. return 8;
  2706. }
  2707. i64 count = t->SimdVector.count;
  2708. Type *elem = t->SimdVector.elem;
  2709. return count * type_size_of_internal(elem, path);
  2710. }
  2711. }
  2712. // Catch all
  2713. return build_context.word_size;
  2714. }
  2715. i64 type_offset_of(Type *t, i32 index) {
  2716. t = base_type(t);
  2717. if (t->kind == Type_Struct) {
  2718. type_set_offsets(t);
  2719. if (gb_is_between(index, 0, t->Struct.fields.count-1)) {
  2720. return t->Struct.offsets[index];
  2721. }
  2722. } else if (t->kind == Type_Tuple) {
  2723. type_set_offsets(t);
  2724. if (gb_is_between(index, 0, t->Tuple.variables.count-1)) {
  2725. return t->Tuple.offsets[index];
  2726. }
  2727. } else if (t->kind == Type_Basic) {
  2728. if (t->Basic.kind == Basic_string) {
  2729. switch (index) {
  2730. case 0: return 0; // data
  2731. case 1: return build_context.word_size; // len
  2732. }
  2733. } else if (t->Basic.kind == Basic_any) {
  2734. switch (index) {
  2735. case 0: return 0; // type_info
  2736. case 1: return build_context.word_size; // data
  2737. }
  2738. }
  2739. } else if (t->kind == Type_Slice) {
  2740. switch (index) {
  2741. case 0: return 0; // data
  2742. case 1: return 1*build_context.word_size; // len
  2743. case 2: return 2*build_context.word_size; // cap
  2744. }
  2745. } else if (t->kind == Type_DynamicArray) {
  2746. switch (index) {
  2747. case 0: return 0; // data
  2748. case 1: return 1*build_context.word_size; // len
  2749. case 2: return 2*build_context.word_size; // cap
  2750. case 3: return 3*build_context.word_size; // allocator
  2751. }
  2752. } else if (t->kind == Type_Union) {
  2753. /* i64 s = */ type_size_of(t);
  2754. switch (index) {
  2755. case -1: return align_formula(t->Union.variant_block_size, build_context.word_size); // __type_info
  2756. }
  2757. }
  2758. return 0;
  2759. }
  2760. i64 type_offset_of_from_selection(Type *type, Selection sel) {
  2761. GB_ASSERT(sel.indirect == false);
  2762. Type *t = type;
  2763. i64 offset = 0;
  2764. for_array(i, sel.index) {
  2765. i32 index = sel.index[i];
  2766. t = base_type(t);
  2767. offset += type_offset_of(t, index);
  2768. if (t->kind == Type_Struct && !t->Struct.is_raw_union) {
  2769. t = t->Struct.fields[index]->type;
  2770. } else {
  2771. // NOTE(bill): No need to worry about custom types, just need the alignment
  2772. switch (t->kind) {
  2773. case Type_Basic:
  2774. if (t->Basic.kind == Basic_string) {
  2775. switch (index) {
  2776. case 0: t = t_rawptr; break;
  2777. case 1: t = t_int; break;
  2778. }
  2779. } else if (t->Basic.kind == Basic_any) {
  2780. switch (index) {
  2781. case 0: t = t_type_info_ptr; break;
  2782. case 1: t = t_rawptr; break;
  2783. }
  2784. }
  2785. break;
  2786. case Type_Slice:
  2787. switch (index) {
  2788. case 0: t = t_rawptr; break;
  2789. case 1: t = t_int; break;
  2790. case 2: t = t_int; break;
  2791. }
  2792. break;
  2793. case Type_DynamicArray:
  2794. switch (index) {
  2795. case 0: t = t_rawptr; break;
  2796. case 1: t = t_int; break;
  2797. case 2: t = t_int; break;
  2798. case 3: t = t_allocator; break;
  2799. }
  2800. break;
  2801. }
  2802. }
  2803. }
  2804. return offset;
  2805. }
  2806. Type *get_struct_field_type(Type *t, isize index) {
  2807. t = base_type(type_deref(t));
  2808. GB_ASSERT(t->kind == Type_Struct);
  2809. return t->Struct.fields[index]->type;
  2810. }
  2811. gbString write_type_to_string(gbString str, Type *type) {
  2812. if (type == nullptr) {
  2813. return gb_string_appendc(str, "<no type>");
  2814. }
  2815. switch (type->kind) {
  2816. case Type_Basic:
  2817. str = gb_string_append_length(str, type->Basic.name.text, type->Basic.name.len);
  2818. break;
  2819. case Type_Generic:
  2820. if (type->Generic.name.len == 0) {
  2821. if (type->Generic.entity != nullptr) {
  2822. String name = type->Generic.entity->token.string;
  2823. str = gb_string_append_rune(str, '$');
  2824. str = gb_string_append_length(str, name.text, name.len);
  2825. } else {
  2826. str = gb_string_appendc(str, "type");
  2827. }
  2828. } else {
  2829. String name = type->Generic.name;
  2830. str = gb_string_append_rune(str, '$');
  2831. str = gb_string_append_length(str, name.text, name.len);
  2832. if (type->Generic.specialized != nullptr) {
  2833. str = gb_string_append_rune(str, '/');
  2834. str = write_type_to_string(str, type->Generic.specialized);
  2835. }
  2836. }
  2837. break;
  2838. case Type_Pointer:
  2839. str = gb_string_append_rune(str, '^');
  2840. str = write_type_to_string(str, type->Pointer.elem);
  2841. break;
  2842. case Type_Opaque:
  2843. str = gb_string_appendc(str, "opaque ");
  2844. str = write_type_to_string(str, type->Opaque.elem);
  2845. break;
  2846. case Type_EnumeratedArray:
  2847. str = gb_string_append_rune(str, '[');
  2848. str = write_type_to_string(str, type->EnumeratedArray.index);
  2849. str = gb_string_append_rune(str, ']');
  2850. str = write_type_to_string(str, type->EnumeratedArray.elem);
  2851. break;
  2852. case Type_Array:
  2853. str = gb_string_appendc(str, gb_bprintf("[%d]", cast(int)type->Array.count));
  2854. str = write_type_to_string(str, type->Array.elem);
  2855. break;
  2856. case Type_Slice:
  2857. str = gb_string_appendc(str, "[]");
  2858. str = write_type_to_string(str, type->Array.elem);
  2859. break;
  2860. case Type_DynamicArray:
  2861. str = gb_string_appendc(str, "[dynamic]");
  2862. str = write_type_to_string(str, type->DynamicArray.elem);
  2863. break;
  2864. case Type_Enum:
  2865. str = gb_string_appendc(str, "enum");
  2866. if (type->Enum.base_type != nullptr) {
  2867. str = gb_string_appendc(str, " ");
  2868. str = write_type_to_string(str, type->Enum.base_type);
  2869. }
  2870. str = gb_string_appendc(str, " {");
  2871. for_array(i, type->Enum.fields) {
  2872. Entity *f = type->Enum.fields[i];
  2873. GB_ASSERT(f->kind == Entity_Constant);
  2874. if (i > 0) {
  2875. str = gb_string_appendc(str, ", ");
  2876. }
  2877. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2878. // str = gb_string_appendc(str, " = ");
  2879. }
  2880. str = gb_string_append_rune(str, '}');
  2881. break;
  2882. case Type_Union:
  2883. str = gb_string_appendc(str, "union {");
  2884. for_array(i, type->Union.variants) {
  2885. Type *t = type->Union.variants[i];
  2886. if (i > 0) str = gb_string_appendc(str, ", ");
  2887. str = write_type_to_string(str, t);
  2888. }
  2889. str = gb_string_append_rune(str, '}');
  2890. break;
  2891. case Type_Struct: {
  2892. if (type->Struct.soa_kind != StructSoa_None) {
  2893. switch (type->Struct.soa_kind) {
  2894. case StructSoa_Fixed: str = gb_string_append_fmt(str, "#soa[%d]", cast(int)type->Struct.soa_count); break;
  2895. case StructSoa_Slice: str = gb_string_appendc(str, "#soa[]"); break;
  2896. case StructSoa_Dynamic: str = gb_string_appendc(str, "#soa[dynamic]"); break;
  2897. default: GB_PANIC("Unknown StructSoaKind"); break;
  2898. }
  2899. str = write_type_to_string(str, type->Struct.soa_elem);
  2900. break;
  2901. }
  2902. str = gb_string_appendc(str, "struct");
  2903. if (type->Struct.is_packed) str = gb_string_appendc(str, " #packed");
  2904. if (type->Struct.is_raw_union) str = gb_string_appendc(str, " #raw_union");
  2905. str = gb_string_appendc(str, " {");
  2906. for_array(i, type->Struct.fields) {
  2907. Entity *f = type->Struct.fields[i];
  2908. GB_ASSERT(f->kind == Entity_Variable);
  2909. if (i > 0) {
  2910. str = gb_string_appendc(str, ", ");
  2911. }
  2912. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2913. str = gb_string_appendc(str, ": ");
  2914. str = write_type_to_string(str, f->type);
  2915. }
  2916. str = gb_string_append_rune(str, '}');
  2917. } break;
  2918. case Type_Map: {
  2919. str = gb_string_appendc(str, "map[");
  2920. str = write_type_to_string(str, type->Map.key);
  2921. str = gb_string_append_rune(str, ']');
  2922. str = write_type_to_string(str, type->Map.value);
  2923. } break;
  2924. case Type_Named:
  2925. if (type->Named.type_name != nullptr) {
  2926. str = gb_string_append_length(str, type->Named.name.text, type->Named.name.len);
  2927. } else {
  2928. // NOTE(bill): Just in case
  2929. str = gb_string_appendc(str, "<named type>");
  2930. }
  2931. break;
  2932. case Type_Tuple:
  2933. if (type->Tuple.variables.count > 0) {
  2934. isize comma_index = 0;
  2935. for_array(i, type->Tuple.variables) {
  2936. Entity *var = type->Tuple.variables[i];
  2937. if (var == nullptr) {
  2938. continue;
  2939. }
  2940. String name = var->token.string;
  2941. if (var->kind == Entity_Constant) {
  2942. str = gb_string_appendc(str, "$");
  2943. str = gb_string_append_length(str, name.text, name.len);
  2944. if (!is_type_untyped(var->type)) {
  2945. str = gb_string_appendc(str, ": ");
  2946. str = write_type_to_string(str, var->type);
  2947. str = gb_string_appendc(str, " = ");
  2948. str = write_exact_value_to_string(str, var->Constant.value);
  2949. } else {
  2950. str = gb_string_appendc(str, "=");
  2951. str = write_exact_value_to_string(str, var->Constant.value);
  2952. }
  2953. continue;
  2954. }
  2955. if (comma_index++ > 0) {
  2956. str = gb_string_appendc(str, ", ");
  2957. }
  2958. if (var->kind == Entity_Variable) {
  2959. if (var->flags&EntityFlag_CVarArg) {
  2960. str = gb_string_appendc(str, "#c_vararg ");
  2961. }
  2962. if (var->flags&EntityFlag_Ellipsis) {
  2963. Type *slice = base_type(var->type);
  2964. str = gb_string_appendc(str, "..");
  2965. GB_ASSERT(var->type->kind == Type_Slice);
  2966. str = write_type_to_string(str, slice->Slice.elem);
  2967. } else {
  2968. str = write_type_to_string(str, var->type);
  2969. }
  2970. } else {
  2971. GB_ASSERT(var->kind == Entity_TypeName);
  2972. if (var->type->kind == Type_Generic) {
  2973. str = gb_string_appendc(str, "typeid/");
  2974. str = write_type_to_string(str, var->type);
  2975. } else {
  2976. if (var->kind == Entity_TypeName) {
  2977. str = gb_string_appendc(str, "$");
  2978. str = gb_string_append_length(str, name.text, name.len);
  2979. str = gb_string_appendc(str, "=");
  2980. str = write_type_to_string(str, var->type);
  2981. } else {
  2982. str = gb_string_appendc(str, "typeid");
  2983. }
  2984. }
  2985. }
  2986. }
  2987. }
  2988. break;
  2989. case Type_Proc:
  2990. str = gb_string_appendc(str, "proc");
  2991. switch (type->Proc.calling_convention) {
  2992. case ProcCC_Odin:
  2993. break;
  2994. case ProcCC_Contextless:
  2995. str = gb_string_appendc(str, " \"contextless\" ");
  2996. break;
  2997. case ProcCC_CDecl:
  2998. str = gb_string_appendc(str, " \"cdecl\" ");
  2999. break;
  3000. case ProcCC_StdCall:
  3001. str = gb_string_appendc(str, " \"stdcall\" ");
  3002. break;
  3003. case ProcCC_FastCall:
  3004. str = gb_string_appendc(str, " \"fastcall\" ");
  3005. break;
  3006. case ProcCC_None:
  3007. str = gb_string_appendc(str, " \"none\" ");
  3008. break;
  3009. // case ProcCC_VectorCall:
  3010. // str = gb_string_appendc(str, " \"vectorcall\" ");
  3011. // break;
  3012. // case ProcCC_ClrCall:
  3013. // str = gb_string_appendc(str, " \"clrcall\" ");
  3014. // break;
  3015. }
  3016. str = gb_string_appendc(str, "(");
  3017. if (type->Proc.params) {
  3018. str = write_type_to_string(str, type->Proc.params);
  3019. }
  3020. str = gb_string_appendc(str, ")");
  3021. if (type->Proc.results) {
  3022. str = gb_string_appendc(str, " -> ");
  3023. if (type->Proc.results->Tuple.variables.count > 1) {
  3024. str = gb_string_appendc(str, "(");
  3025. }
  3026. str = write_type_to_string(str, type->Proc.results);
  3027. if (type->Proc.results->Tuple.variables.count > 1) {
  3028. str = gb_string_appendc(str, ")");
  3029. }
  3030. }
  3031. break;
  3032. case Type_BitField:
  3033. str = gb_string_appendc(str, "bit_field ");
  3034. if (type->BitField.custom_align != 0) {
  3035. str = gb_string_append_fmt(str, "#align %d ", cast(int)type->BitField.custom_align);
  3036. }
  3037. str = gb_string_append_rune(str, '{');
  3038. for_array(i, type->BitField.fields) {
  3039. Entity *f = type->BitField.fields[i];
  3040. GB_ASSERT(f->kind == Entity_Variable);
  3041. GB_ASSERT(f->type != nullptr && f->type->kind == Type_BitFieldValue);
  3042. if (i > 0) {
  3043. str = gb_string_appendc(str, ", ");
  3044. }
  3045. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  3046. str = gb_string_appendc(str, ": ");
  3047. str = gb_string_append_fmt(str, "%lld", cast(long long)f->type->BitFieldValue.bits);
  3048. }
  3049. str = gb_string_append_rune(str, '}');
  3050. break;
  3051. case Type_BitFieldValue:
  3052. str = gb_string_append_fmt(str, "(bit field value with %d bits)", cast(int)type->BitFieldValue.bits);
  3053. break;
  3054. case Type_BitSet:
  3055. str = gb_string_appendc(str, "bit_set[");
  3056. str = write_type_to_string(str, type->BitSet.elem);
  3057. if (type->BitSet.underlying != nullptr) {
  3058. str = gb_string_appendc(str, "; ");
  3059. str = write_type_to_string(str, type->BitSet.underlying);
  3060. }
  3061. str = gb_string_appendc(str, "]");
  3062. break;
  3063. case Type_SimdVector:
  3064. if (type->SimdVector.is_x86_mmx) {
  3065. return gb_string_appendc(str, "intrinsics.x86_mmx");
  3066. } else {
  3067. str = gb_string_append_fmt(str, "#simd[%d]", cast(int)type->SimdVector.count);
  3068. str = write_type_to_string(str, type->SimdVector.elem);
  3069. }
  3070. break;
  3071. }
  3072. return str;
  3073. }
  3074. gbString type_to_string(Type *type) {
  3075. return write_type_to_string(gb_string_make(heap_allocator(), ""), type);
  3076. }