types.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  1. struct Scope;
  2. struct AstNode;
  3. enum BasicKind {
  4. Basic_Invalid,
  5. Basic_bool,
  6. Basic_i8,
  7. Basic_u8,
  8. Basic_i16,
  9. Basic_u16,
  10. Basic_i32,
  11. Basic_u32,
  12. Basic_i64,
  13. Basic_u64,
  14. Basic_i128,
  15. Basic_u128,
  16. Basic_rune,
  17. // Basic_f16,
  18. Basic_f32,
  19. Basic_f64,
  20. // Basic_complex32,
  21. Basic_complex64,
  22. Basic_complex128,
  23. Basic_int,
  24. Basic_uint,
  25. Basic_rawptr,
  26. Basic_string, // ^u8 + int
  27. Basic_any, // ^Type_Info + rawptr
  28. Basic_UntypedBool,
  29. Basic_UntypedInteger,
  30. Basic_UntypedFloat,
  31. Basic_UntypedComplex,
  32. Basic_UntypedString,
  33. Basic_UntypedRune,
  34. Basic_UntypedNil,
  35. Basic_UntypedUndef,
  36. Basic_COUNT,
  37. Basic_byte = Basic_u8,
  38. };
  39. enum BasicFlag {
  40. BasicFlag_Boolean = GB_BIT(0),
  41. BasicFlag_Integer = GB_BIT(1),
  42. BasicFlag_Unsigned = GB_BIT(2),
  43. BasicFlag_Float = GB_BIT(3),
  44. BasicFlag_Complex = GB_BIT(4),
  45. BasicFlag_Pointer = GB_BIT(5),
  46. BasicFlag_String = GB_BIT(6),
  47. BasicFlag_Rune = GB_BIT(7),
  48. BasicFlag_Untyped = GB_BIT(8),
  49. BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Complex,
  50. BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  51. BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
  52. };
  53. struct BasicType {
  54. BasicKind kind;
  55. u32 flags;
  56. i64 size; // -1 if arch. dep.
  57. String name;
  58. };
  59. struct TypeStruct {
  60. Array<Entity *> fields;
  61. Array<Entity *> fields_in_src_order;
  62. AstNode *node;
  63. Scope * scope;
  64. i64 * offsets; // == fields.count
  65. bool are_offsets_set;
  66. bool are_offsets_being_processed;
  67. bool is_packed;
  68. bool is_ordered;
  69. bool is_raw_union;
  70. bool is_polymorphic;
  71. bool is_poly_specialized;
  72. Type * polymorphic_params; // Type_Tuple
  73. Type * polymorphic_parent;
  74. i64 custom_align; // NOTE(bill): Only used in structs at the moment
  75. Entity * names;
  76. };
  77. #define TYPE_KINDS \
  78. TYPE_KIND(Basic, BasicType) \
  79. TYPE_KIND(Generic, struct { \
  80. i64 id; \
  81. String name; \
  82. Type * specialized; \
  83. }) \
  84. TYPE_KIND(Pointer, struct { Type *elem; }) \
  85. TYPE_KIND(Array, struct { Type *elem; i64 count; }) \
  86. TYPE_KIND(DynamicArray, struct { Type *elem; }) \
  87. TYPE_KIND(Vector, struct { Type *elem; i64 count; }) \
  88. TYPE_KIND(Slice, struct { Type *elem; }) \
  89. TYPE_KIND(Struct, TypeStruct) \
  90. TYPE_KIND(Enum, struct { \
  91. Entity **fields; \
  92. i32 field_count; \
  93. AstNode *node; \
  94. Scope * scope; \
  95. Entity * names; \
  96. Type * base_type; \
  97. Entity * count; \
  98. Entity * min_value; \
  99. Entity * max_value; \
  100. }) \
  101. TYPE_KIND(Union, struct { \
  102. Array<Type *> variants; \
  103. AstNode *node; \
  104. Scope * scope; \
  105. Entity * union__type_info; \
  106. i64 variant_block_size; \
  107. i64 custom_align; \
  108. }) \
  109. TYPE_KIND(Named, struct { \
  110. String name; \
  111. Type * base; \
  112. Entity *type_name; /* Entity_TypeName */ \
  113. }) \
  114. TYPE_KIND(Tuple, struct { \
  115. Array<Entity *> variables; /* Entity_Variable */ \
  116. bool are_offsets_set; \
  117. i64 * offsets; \
  118. }) \
  119. TYPE_KIND(Proc, struct { \
  120. AstNode *node; \
  121. Scope * scope; \
  122. Type * params; /* Type_Tuple */ \
  123. Type * results; /* Type_Tuple */ \
  124. i32 param_count; \
  125. i32 result_count; \
  126. bool return_by_pointer; \
  127. Type ** abi_compat_params; \
  128. Type * abi_compat_result_type; \
  129. bool variadic; \
  130. bool require_results; \
  131. bool c_vararg; \
  132. bool is_polymorphic; \
  133. bool is_poly_specialized; \
  134. ProcCallingConvention calling_convention; \
  135. }) \
  136. TYPE_KIND(Map, struct { \
  137. i64 count; /* 0 if dynamic */ \
  138. Type *key; \
  139. Type *value; \
  140. Type *entry_type; \
  141. Type *generated_struct_type; \
  142. Type *lookup_result_type; \
  143. }) \
  144. TYPE_KIND(BitFieldValue, struct { u32 bits; }) \
  145. TYPE_KIND(BitField, struct { \
  146. Scope * scope; \
  147. Entity **fields; \
  148. i32 field_count; \
  149. u32 * offsets; \
  150. u32 * sizes; \
  151. i64 custom_align; \
  152. }) \
  153. enum TypeKind {
  154. Type_Invalid,
  155. #define TYPE_KIND(k, ...) GB_JOIN2(Type_, k),
  156. TYPE_KINDS
  157. #undef TYPE_KIND
  158. Type_Count,
  159. };
  160. String const type_strings[] = {
  161. {cast(u8 *)"Invalid", gb_size_of("Invalid")},
  162. #define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1},
  163. TYPE_KINDS
  164. #undef TYPE_KIND
  165. };
  166. #define TYPE_KIND(k, ...) typedef __VA_ARGS__ GB_JOIN2(Type, k);
  167. TYPE_KINDS
  168. #undef TYPE_KIND
  169. struct Type {
  170. TypeKind kind;
  171. union {
  172. #define TYPE_KIND(k, ...) GB_JOIN2(Type, k) k;
  173. TYPE_KINDS
  174. #undef TYPE_KIND
  175. };
  176. bool failure;
  177. };
  178. // TODO(bill): Should I add extra information here specifying the kind of selection?
  179. // e.g. field, constant, vector field, type field, etc.
  180. struct Selection {
  181. Entity * entity;
  182. Array<i32> index;
  183. bool indirect; // Set if there was a pointer deref anywhere down the line
  184. };
  185. Selection empty_selection = {0};
  186. Selection make_selection(Entity *entity, Array<i32> index, bool indirect) {
  187. Selection s = {entity, index, indirect};
  188. return s;
  189. }
  190. void selection_add_index(Selection *s, isize index) {
  191. // IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
  192. // of heap allocation
  193. // TODO(bill): Find a way to use a backing buffer for initial use as the general case is probably .count<3
  194. if (s->index.data == nullptr) {
  195. array_init(&s->index, heap_allocator());
  196. }
  197. array_add(&s->index, cast(i32)index);
  198. }
  199. gb_global Type basic_types[] = {
  200. {Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}},
  201. {Type_Basic, {Basic_bool, BasicFlag_Boolean, 1, STR_LIT("bool")}},
  202. {Type_Basic, {Basic_i8, BasicFlag_Integer, 1, STR_LIT("i8")}},
  203. {Type_Basic, {Basic_u8, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("u8")}},
  204. {Type_Basic, {Basic_i16, BasicFlag_Integer, 2, STR_LIT("i16")}},
  205. {Type_Basic, {Basic_u16, BasicFlag_Integer | BasicFlag_Unsigned, 2, STR_LIT("u16")}},
  206. {Type_Basic, {Basic_i32, BasicFlag_Integer, 4, STR_LIT("i32")}},
  207. {Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, 4, STR_LIT("u32")}},
  208. {Type_Basic, {Basic_i64, BasicFlag_Integer, 8, STR_LIT("i64")}},
  209. {Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, 8, STR_LIT("u64")}},
  210. {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}},
  211. {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}},
  212. {Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}},
  213. // {Type_Basic, {Basic_f16, BasicFlag_Float, 2, STR_LIT("f16")}},
  214. {Type_Basic, {Basic_f32, BasicFlag_Float, 4, STR_LIT("f32")}},
  215. {Type_Basic, {Basic_f64, BasicFlag_Float, 8, STR_LIT("f64")}},
  216. // {Type_Basic, {Basic_complex32, BasicFlag_Complex, 4, STR_LIT("complex32")}},
  217. {Type_Basic, {Basic_complex64, BasicFlag_Complex, 8, STR_LIT("complex64")}},
  218. {Type_Basic, {Basic_complex128, BasicFlag_Complex, 16, STR_LIT("complex128")}},
  219. {Type_Basic, {Basic_int, BasicFlag_Integer, -1, STR_LIT("int")}},
  220. {Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uint")}},
  221. {Type_Basic, {Basic_rawptr, BasicFlag_Pointer, -1, STR_LIT("rawptr")}},
  222. {Type_Basic, {Basic_string, BasicFlag_String, -1, STR_LIT("string")}},
  223. {Type_Basic, {Basic_any, 0, -1, STR_LIT("any")}},
  224. {Type_Basic, {Basic_UntypedBool, BasicFlag_Boolean | BasicFlag_Untyped, 0, STR_LIT("untyped bool")}},
  225. {Type_Basic, {Basic_UntypedInteger, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped integer")}},
  226. {Type_Basic, {Basic_UntypedFloat, BasicFlag_Float | BasicFlag_Untyped, 0, STR_LIT("untyped float")}},
  227. {Type_Basic, {Basic_UntypedComplex, BasicFlag_Complex | BasicFlag_Untyped, 0, STR_LIT("untyped complex")}},
  228. {Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}},
  229. {Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}},
  230. {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}},
  231. {Type_Basic, {Basic_UntypedUndef, BasicFlag_Untyped, 0, STR_LIT("untyped undefined")}},
  232. };
  233. // gb_global Type basic_type_aliases[] = {
  234. // // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}},
  235. // // {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}},
  236. // };
  237. gb_global Type *t_invalid = &basic_types[Basic_Invalid];
  238. gb_global Type *t_bool = &basic_types[Basic_bool];
  239. gb_global Type *t_i8 = &basic_types[Basic_i8];
  240. gb_global Type *t_u8 = &basic_types[Basic_u8];
  241. gb_global Type *t_i16 = &basic_types[Basic_i16];
  242. gb_global Type *t_u16 = &basic_types[Basic_u16];
  243. gb_global Type *t_i32 = &basic_types[Basic_i32];
  244. gb_global Type *t_u32 = &basic_types[Basic_u32];
  245. gb_global Type *t_i64 = &basic_types[Basic_i64];
  246. gb_global Type *t_u64 = &basic_types[Basic_u64];
  247. gb_global Type *t_i128 = &basic_types[Basic_i128];
  248. gb_global Type *t_u128 = &basic_types[Basic_u128];
  249. gb_global Type *t_rune = &basic_types[Basic_rune];
  250. // gb_global Type *t_f16 = &basic_types[Basic_f16];
  251. gb_global Type *t_f32 = &basic_types[Basic_f32];
  252. gb_global Type *t_f64 = &basic_types[Basic_f64];
  253. // gb_global Type *t_complex32 = &basic_types[Basic_complex32];
  254. gb_global Type *t_complex64 = &basic_types[Basic_complex64];
  255. gb_global Type *t_complex128 = &basic_types[Basic_complex128];
  256. gb_global Type *t_int = &basic_types[Basic_int];
  257. gb_global Type *t_uint = &basic_types[Basic_uint];
  258. gb_global Type *t_rawptr = &basic_types[Basic_rawptr];
  259. gb_global Type *t_string = &basic_types[Basic_string];
  260. gb_global Type *t_any = &basic_types[Basic_any];
  261. gb_global Type *t_untyped_bool = &basic_types[Basic_UntypedBool];
  262. gb_global Type *t_untyped_integer = &basic_types[Basic_UntypedInteger];
  263. gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat];
  264. gb_global Type *t_untyped_complex = &basic_types[Basic_UntypedComplex];
  265. gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString];
  266. gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
  267. gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil];
  268. gb_global Type *t_untyped_undef = &basic_types[Basic_UntypedUndef];
  269. gb_global Type *t_u8_ptr = nullptr;
  270. gb_global Type *t_int_ptr = nullptr;
  271. gb_global Type *t_i64_ptr = nullptr;
  272. gb_global Type *t_i128_ptr = nullptr;
  273. gb_global Type *t_f64_ptr = nullptr;
  274. gb_global Type *t_u8_slice = nullptr;
  275. gb_global Type *t_string_slice = nullptr;
  276. // Type generated for the "preload" file
  277. gb_global Type *t_type_info = nullptr;
  278. gb_global Type *t_type_info_enum_value = nullptr;
  279. gb_global Type *t_type_info_ptr = nullptr;
  280. gb_global Type *t_type_info_enum_value_ptr = nullptr;
  281. gb_global Type *t_type_info_named = nullptr;
  282. gb_global Type *t_type_info_integer = nullptr;
  283. gb_global Type *t_type_info_rune = nullptr;
  284. gb_global Type *t_type_info_float = nullptr;
  285. gb_global Type *t_type_info_complex = nullptr;
  286. gb_global Type *t_type_info_any = nullptr;
  287. gb_global Type *t_type_info_string = nullptr;
  288. gb_global Type *t_type_info_boolean = nullptr;
  289. gb_global Type *t_type_info_pointer = nullptr;
  290. gb_global Type *t_type_info_procedure = nullptr;
  291. gb_global Type *t_type_info_array = nullptr;
  292. gb_global Type *t_type_info_dynamic_array = nullptr;
  293. gb_global Type *t_type_info_slice = nullptr;
  294. gb_global Type *t_type_info_vector = nullptr;
  295. gb_global Type *t_type_info_tuple = nullptr;
  296. gb_global Type *t_type_info_struct = nullptr;
  297. gb_global Type *t_type_info_union = nullptr;
  298. gb_global Type *t_type_info_enum = nullptr;
  299. gb_global Type *t_type_info_map = nullptr;
  300. gb_global Type *t_type_info_bit_field = nullptr;
  301. gb_global Type *t_type_info_named_ptr = nullptr;
  302. gb_global Type *t_type_info_integer_ptr = nullptr;
  303. gb_global Type *t_type_info_rune_ptr = nullptr;
  304. gb_global Type *t_type_info_float_ptr = nullptr;
  305. gb_global Type *t_type_info_complex_ptr = nullptr;
  306. gb_global Type *t_type_info_quaternion_ptr = nullptr;
  307. gb_global Type *t_type_info_any_ptr = nullptr;
  308. gb_global Type *t_type_info_string_ptr = nullptr;
  309. gb_global Type *t_type_info_boolean_ptr = nullptr;
  310. gb_global Type *t_type_info_pointer_ptr = nullptr;
  311. gb_global Type *t_type_info_procedure_ptr = nullptr;
  312. gb_global Type *t_type_info_array_ptr = nullptr;
  313. gb_global Type *t_type_info_dynamic_array_ptr = nullptr;
  314. gb_global Type *t_type_info_slice_ptr = nullptr;
  315. gb_global Type *t_type_info_vector_ptr = nullptr;
  316. gb_global Type *t_type_info_tuple_ptr = nullptr;
  317. gb_global Type *t_type_info_struct_ptr = nullptr;
  318. gb_global Type *t_type_info_union_ptr = nullptr;
  319. gb_global Type *t_type_info_enum_ptr = nullptr;
  320. gb_global Type *t_type_info_map_ptr = nullptr;
  321. gb_global Type *t_type_info_bit_field_ptr = nullptr;
  322. gb_global Type *t_allocator = nullptr;
  323. gb_global Type *t_allocator_ptr = nullptr;
  324. gb_global Type *t_context = nullptr;
  325. gb_global Type *t_context_ptr = nullptr;
  326. gb_global Type *t_source_code_location = nullptr;
  327. gb_global Type *t_source_code_location_ptr = nullptr;
  328. gb_global Type *t_map_key = nullptr;
  329. gb_global Type *t_map_header = nullptr;
  330. i64 type_size_of (gbAllocator allocator, Type *t);
  331. i64 type_align_of (gbAllocator allocator, Type *t);
  332. i64 type_offset_of (gbAllocator allocator, Type *t, i32 index);
  333. gbString type_to_string(Type *type);
  334. Type *base_type(Type *t) {
  335. for (;;) {
  336. if (t == nullptr) {
  337. break;
  338. }
  339. if (t->kind != Type_Named) {
  340. break;
  341. }
  342. if (t == t->Named.base) {
  343. return t_invalid;
  344. }
  345. t = t->Named.base;
  346. }
  347. return t;
  348. }
  349. Type *base_enum_type(Type *t) {
  350. Type *bt = base_type(t);
  351. if (bt != nullptr &&
  352. bt->kind == Type_Enum) {
  353. return bt->Enum.base_type;
  354. }
  355. return t;
  356. }
  357. Type *core_type(Type *t) {
  358. for (;;) {
  359. if (t == nullptr) {
  360. break;
  361. }
  362. switch (t->kind) {
  363. case Type_Named:
  364. if (t == t->Named.base) {
  365. return t_invalid;
  366. }
  367. t = t->Named.base;
  368. continue;
  369. case Type_Enum:
  370. t = t->Enum.base_type;
  371. continue;
  372. }
  373. break;
  374. }
  375. return t;
  376. }
  377. void set_base_type(Type *t, Type *base) {
  378. if (t && t->kind == Type_Named) {
  379. t->Named.base = base;
  380. }
  381. }
  382. Type *alloc_type(gbAllocator a, TypeKind kind) {
  383. Type *t = gb_alloc_item(a, Type);
  384. gb_zero_item(t);
  385. t->kind = kind;
  386. return t;
  387. }
  388. Type *make_type_basic(gbAllocator a, BasicType basic) {
  389. Type *t = alloc_type(a, Type_Basic);
  390. t->Basic = basic;
  391. return t;
  392. }
  393. Type *make_type_generic(gbAllocator a, i64 id, String name, Type *specialized) {
  394. Type *t = alloc_type(a, Type_Generic);
  395. t->Generic.id = id;
  396. t->Generic.name = name;
  397. t->Generic.specialized = specialized;
  398. return t;
  399. }
  400. Type *make_type_pointer(gbAllocator a, Type *elem) {
  401. Type *t = alloc_type(a, Type_Pointer);
  402. t->Pointer.elem = elem;
  403. return t;
  404. }
  405. Type *make_type_array(gbAllocator a, Type *elem, i64 count) {
  406. Type *t = alloc_type(a, Type_Array);
  407. t->Array.elem = elem;
  408. t->Array.count = count;
  409. return t;
  410. }
  411. Type *make_type_dynamic_array(gbAllocator a, Type *elem) {
  412. Type *t = alloc_type(a, Type_DynamicArray);
  413. t->DynamicArray.elem = elem;
  414. return t;
  415. }
  416. Type *make_type_vector(gbAllocator a, Type *elem, i64 count) {
  417. Type *t = alloc_type(a, Type_Vector);
  418. t->Vector.elem = elem;
  419. t->Vector.count = count;
  420. return t;
  421. }
  422. Type *make_type_slice(gbAllocator a, Type *elem) {
  423. Type *t = alloc_type(a, Type_Slice);
  424. t->Array.elem = elem;
  425. return t;
  426. }
  427. Type *make_type_struct(gbAllocator a) {
  428. Type *t = alloc_type(a, Type_Struct);
  429. return t;
  430. }
  431. Type *make_type_union(gbAllocator a) {
  432. Type *t = alloc_type(a, Type_Union);
  433. return t;
  434. }
  435. Type *make_type_enum(gbAllocator a) {
  436. Type *t = alloc_type(a, Type_Enum);
  437. return t;
  438. }
  439. Type *make_type_named(gbAllocator a, String name, Type *base, Entity *type_name) {
  440. Type *t = alloc_type(a, Type_Named);
  441. t->Named.name = name;
  442. t->Named.base = base;
  443. t->Named.type_name = type_name;
  444. return t;
  445. }
  446. Type *make_type_tuple(gbAllocator a) {
  447. Type *t = alloc_type(a, Type_Tuple);
  448. return t;
  449. }
  450. Type *make_type_proc(gbAllocator a, Scope *scope, Type *params, isize param_count, Type *results, isize result_count, bool variadic, ProcCallingConvention calling_convention) {
  451. Type *t = alloc_type(a, Type_Proc);
  452. if (variadic) {
  453. if (param_count == 0) {
  454. GB_PANIC("variadic procedure must have at least one parameter");
  455. }
  456. GB_ASSERT(params != nullptr && params->kind == Type_Tuple);
  457. Entity *e = params->Tuple.variables[param_count-1];
  458. if (base_type(e->type)->kind != Type_Slice) {
  459. // NOTE(bill): For custom calling convention
  460. GB_PANIC("variadic parameter must be of type slice");
  461. }
  462. }
  463. t->Proc.scope = scope;
  464. t->Proc.params = params;
  465. t->Proc.param_count = param_count;
  466. t->Proc.results = results;
  467. t->Proc.result_count = result_count;
  468. t->Proc.variadic = variadic;
  469. t->Proc.calling_convention = calling_convention;
  470. return t;
  471. }
  472. bool is_type_valid_for_keys(Type *t);
  473. Type *make_type_map(gbAllocator a, i64 count, Type *key, Type *value) {
  474. Type *t = alloc_type(a, Type_Map);
  475. if (key != nullptr) {
  476. GB_ASSERT(is_type_valid_for_keys(key));
  477. }
  478. t->Map.count = count;
  479. t->Map.key = key;
  480. t->Map.value = value;
  481. return t;
  482. }
  483. Type *make_type_bit_field_value(gbAllocator a, u32 bits) {
  484. Type *t = alloc_type(a, Type_BitFieldValue);
  485. t->BitFieldValue.bits = bits;
  486. return t;
  487. }
  488. Type *make_type_bit_field(gbAllocator a) {
  489. Type *t = alloc_type(a, Type_BitField);
  490. return t;
  491. }
  492. ////////////////////////////////////////////////////////////////
  493. Type *type_deref(Type *t) {
  494. if (t != nullptr) {
  495. Type *bt = base_type(t);
  496. if (bt == nullptr)
  497. return nullptr;
  498. if (bt != nullptr && bt->kind == Type_Pointer)
  499. return bt->Pointer.elem;
  500. }
  501. return t;
  502. }
  503. bool is_type_named(Type *t) {
  504. if (t->kind == Type_Basic) {
  505. return true;
  506. }
  507. return t->kind == Type_Named;
  508. }
  509. bool is_type_named_alias(Type *t) {
  510. if (!is_type_named(t)) {
  511. return false;
  512. }
  513. Entity *e = t->Named.type_name;
  514. if (e == nullptr) {
  515. return false;
  516. }
  517. if (e->kind != Entity_TypeName) {
  518. return false;
  519. }
  520. return e->TypeName.is_type_alias;
  521. }
  522. bool is_type_boolean(Type *t) {
  523. t = core_type(t);
  524. if (t->kind == Type_Basic) {
  525. return (t->Basic.flags & BasicFlag_Boolean) != 0;
  526. }
  527. return false;
  528. }
  529. bool is_type_integer(Type *t) {
  530. t = core_type(t);
  531. if (t->kind == Type_Basic) {
  532. return (t->Basic.flags & BasicFlag_Integer) != 0;
  533. }
  534. return false;
  535. }
  536. bool is_type_unsigned(Type *t) {
  537. t = core_type(t);
  538. if (t->kind == Type_Basic) {
  539. return (t->Basic.flags & BasicFlag_Unsigned) != 0;
  540. }
  541. return false;
  542. }
  543. bool is_type_rune(Type *t) {
  544. t = core_type(t);
  545. if (t->kind == Type_Basic) {
  546. return (t->Basic.flags & BasicFlag_Rune) != 0;
  547. }
  548. return false;
  549. }
  550. bool is_type_numeric(Type *t) {
  551. t = core_type(t);
  552. if (t->kind == Type_Basic) {
  553. return (t->Basic.flags & BasicFlag_Numeric) != 0;
  554. }
  555. // TODO(bill): Should this be here?
  556. if (t->kind == Type_Vector) {
  557. return is_type_numeric(t->Vector.elem);
  558. }
  559. return false;
  560. }
  561. bool is_type_string(Type *t) {
  562. t = base_type(t);
  563. if (t->kind == Type_Basic) {
  564. return (t->Basic.flags & BasicFlag_String) != 0;
  565. }
  566. return false;
  567. }
  568. bool is_type_typed(Type *t) {
  569. t = base_type(t);
  570. if (t == nullptr) {
  571. return false;
  572. }
  573. if (t->kind == Type_Basic) {
  574. return (t->Basic.flags & BasicFlag_Untyped) == 0;
  575. }
  576. return true;
  577. }
  578. bool is_type_untyped(Type *t) {
  579. t = base_type(t);
  580. if (t->kind == Type_Basic) {
  581. return (t->Basic.flags & BasicFlag_Untyped) != 0;
  582. }
  583. return false;
  584. }
  585. bool is_type_ordered(Type *t) {
  586. t = core_type(t);
  587. switch (t->kind) {
  588. case Type_Basic:
  589. return (t->Basic.flags & BasicFlag_Ordered) != 0;
  590. case Type_Pointer:
  591. return true;
  592. case Type_Vector:
  593. return is_type_ordered(t->Vector.elem);
  594. }
  595. return false;
  596. }
  597. bool is_type_constant_type(Type *t) {
  598. t = core_type(t);
  599. if (t->kind == Type_Basic) {
  600. return (t->Basic.flags & BasicFlag_ConstantType) != 0;
  601. }
  602. return false;
  603. }
  604. bool is_type_float(Type *t) {
  605. t = core_type(t);
  606. if (t->kind == Type_Basic) {
  607. return (t->Basic.flags & BasicFlag_Float) != 0;
  608. }
  609. return false;
  610. }
  611. bool is_type_complex(Type *t) {
  612. t = core_type(t);
  613. if (t->kind == Type_Basic) {
  614. return (t->Basic.flags & BasicFlag_Complex) != 0;
  615. }
  616. return false;
  617. }
  618. bool is_type_f32(Type *t) {
  619. t = core_type(t);
  620. if (t->kind == Type_Basic) {
  621. return t->Basic.kind == Basic_f32;
  622. }
  623. return false;
  624. }
  625. bool is_type_f64(Type *t) {
  626. t = core_type(t);
  627. if (t->kind == Type_Basic) {
  628. return t->Basic.kind == Basic_f64;
  629. }
  630. return false;
  631. }
  632. bool is_type_pointer(Type *t) {
  633. t = base_type(t);
  634. if (t->kind == Type_Basic) {
  635. return (t->Basic.flags & BasicFlag_Pointer) != 0;
  636. }
  637. return t->kind == Type_Pointer;
  638. }
  639. bool is_type_tuple(Type *t) {
  640. t = base_type(t);
  641. return t->kind == Type_Tuple;
  642. }
  643. bool is_type_int_or_uint(Type *t) {
  644. if (t->kind == Type_Basic) {
  645. return (t->Basic.kind == Basic_int) || (t->Basic.kind == Basic_uint);
  646. }
  647. return false;
  648. }
  649. bool is_type_i128_or_u128(Type *t) {
  650. if (t->kind == Type_Basic) {
  651. return (t->Basic.kind == Basic_i128) || (t->Basic.kind == Basic_u128);
  652. }
  653. return false;
  654. }
  655. bool is_type_rawptr(Type *t) {
  656. if (t->kind == Type_Basic) {
  657. return t->Basic.kind == Basic_rawptr;
  658. }
  659. return false;
  660. }
  661. bool is_type_u8(Type *t) {
  662. if (t->kind == Type_Basic) {
  663. return t->Basic.kind == Basic_u8;
  664. }
  665. return false;
  666. }
  667. bool is_type_array(Type *t) {
  668. t = base_type(t);
  669. return t->kind == Type_Array;
  670. }
  671. bool is_type_dynamic_array(Type *t) {
  672. t = base_type(t);
  673. return t->kind == Type_DynamicArray;
  674. }
  675. bool is_type_slice(Type *t) {
  676. t = base_type(t);
  677. return t->kind == Type_Slice;
  678. }
  679. bool is_type_u8_slice(Type *t) {
  680. t = base_type(t);
  681. if (t->kind == Type_Slice) {
  682. return is_type_u8(t->Slice.elem);
  683. }
  684. return false;
  685. }
  686. bool is_type_vector(Type *t) {
  687. t = base_type(t);
  688. return t->kind == Type_Vector;
  689. }
  690. bool is_type_proc(Type *t) {
  691. t = base_type(t);
  692. return t->kind == Type_Proc;
  693. }
  694. bool is_type_poly_proc(Type *t) {
  695. t = base_type(t);
  696. return t->kind == Type_Proc && t->Proc.is_polymorphic;
  697. }
  698. Type *base_vector_type(Type *t) {
  699. if (is_type_vector(t)) {
  700. t = base_type(t);
  701. return t->Vector.elem;
  702. }
  703. return t;
  704. }
  705. Type *base_complex_elem_type(Type *t) {
  706. t = core_type(t);
  707. if (is_type_complex(t)) {
  708. switch (t->Basic.kind) {
  709. // case Basic_complex32: return t_f16;
  710. case Basic_complex64: return t_f32;
  711. case Basic_complex128: return t_f64;
  712. case Basic_UntypedComplex: return t_untyped_float;
  713. }
  714. }
  715. GB_PANIC("Invalid complex type");
  716. return t_invalid;
  717. }
  718. bool is_type_struct(Type *t) {
  719. t = base_type(t);
  720. return (t->kind == Type_Struct && !t->Struct.is_raw_union);
  721. }
  722. bool is_type_union(Type *t) {
  723. t = base_type(t);
  724. return t->kind == Type_Union;
  725. }
  726. bool is_type_raw_union(Type *t) {
  727. t = base_type(t);
  728. return (t->kind == Type_Struct && t->Struct.is_raw_union);
  729. }
  730. bool is_type_enum(Type *t) {
  731. t = base_type(t);
  732. return (t->kind == Type_Enum);
  733. }
  734. bool is_type_bit_field(Type *t) {
  735. t = base_type(t);
  736. return (t->kind == Type_BitField);
  737. }
  738. bool is_type_bit_field_value(Type *t) {
  739. t = base_type(t);
  740. return (t->kind == Type_BitFieldValue);
  741. }
  742. bool is_type_map(Type *t) {
  743. t = base_type(t);
  744. return t->kind == Type_Map;
  745. }
  746. bool is_type_fixed_map(Type *t) {
  747. t = base_type(t);
  748. return t->kind == Type_Map && t->Map.count > 0;
  749. }
  750. bool is_type_dynamic_map(Type *t) {
  751. t = base_type(t); return t->kind == Type_Map && t->Map.count == 0;
  752. }
  753. bool is_type_any(Type *t) {
  754. t = base_type(t);
  755. return (t->kind == Type_Basic && t->Basic.kind == Basic_any);
  756. }
  757. bool is_type_untyped_nil(Type *t) {
  758. t = base_type(t);
  759. return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedNil);
  760. }
  761. bool is_type_untyped_undef(Type *t) {
  762. t = base_type(t);
  763. return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUndef);
  764. }
  765. bool is_type_valid_for_keys(Type *t) {
  766. t = core_type(t);
  767. if (t->kind == Type_Generic) {
  768. return true;
  769. }
  770. if (is_type_untyped(t)) {
  771. return false;
  772. }
  773. if (is_type_integer(t)) {
  774. return true;
  775. }
  776. if (is_type_float(t)) {
  777. return true;
  778. }
  779. if (is_type_string(t)) {
  780. return true;
  781. }
  782. if (is_type_pointer(t)) {
  783. return true;
  784. }
  785. return false;
  786. }
  787. bool is_type_indexable(Type *t) {
  788. return is_type_array(t) || is_type_slice(t) || is_type_vector(t) || is_type_string(t);
  789. }
  790. bool is_type_polymorphic_struct(Type *t) {
  791. t = base_type(t);
  792. if (t->kind == Type_Struct) {
  793. return t->Struct.is_polymorphic;
  794. }
  795. return false;
  796. }
  797. bool is_type_polymorphic_struct_specialized(Type *t) {
  798. t = base_type(t);
  799. if (t->kind == Type_Struct) {
  800. return t->Struct.is_polymorphic && t->Struct.is_poly_specialized;
  801. }
  802. return false;
  803. }
  804. bool is_type_polymorphic(Type *t) {
  805. switch (t->kind) {
  806. case Type_Generic:
  807. return true;
  808. case Type_Named:
  809. return is_type_polymorphic_struct(t->Named.base);
  810. case Type_Pointer:
  811. return is_type_polymorphic(t->Pointer.elem);
  812. case Type_Array:
  813. return is_type_polymorphic(t->Array.elem);
  814. case Type_DynamicArray:
  815. return is_type_polymorphic(t->DynamicArray.elem);
  816. case Type_Vector:
  817. return is_type_polymorphic(t->Vector.elem);
  818. case Type_Slice:
  819. return is_type_polymorphic(t->Slice.elem);
  820. case Type_Tuple:
  821. for_array(i, t->Tuple.variables) {
  822. if (is_type_polymorphic(t->Tuple.variables[i]->type)) {
  823. return true;
  824. }
  825. }
  826. break;
  827. case Type_Proc:
  828. if (t->Proc.is_polymorphic) {
  829. return true;
  830. }
  831. #if 1
  832. if (t->Proc.param_count > 0 &&
  833. is_type_polymorphic(t->Proc.params)) {
  834. return true;
  835. }
  836. if (t->Proc.result_count > 0 &&
  837. is_type_polymorphic(t->Proc.results)) {
  838. return true;
  839. }
  840. #endif
  841. break;
  842. case Type_Enum:
  843. if (t->kind == Type_Enum) {
  844. if (t->Enum.base_type != nullptr) {
  845. return is_type_polymorphic(t->Enum.base_type);
  846. }
  847. return false;
  848. }
  849. break;
  850. case Type_Union:
  851. for_array(i, t->Union.variants) {
  852. if (is_type_polymorphic(t->Union.variants[i])) {
  853. return true;
  854. }
  855. }
  856. break;
  857. case Type_Struct:
  858. if (t->Struct.is_polymorphic) {
  859. return true;
  860. }
  861. for_array(i, t->Struct.fields) {
  862. if (is_type_polymorphic(t->Struct.fields[i]->type)) {
  863. return true;
  864. }
  865. }
  866. break;
  867. case Type_Map:
  868. if (is_type_polymorphic(t->Map.key)) {
  869. return true;
  870. }
  871. if (is_type_polymorphic(t->Map.value)) {
  872. return true;
  873. }
  874. break;
  875. }
  876. return false;
  877. }
  878. bool type_has_undef(Type *t) {
  879. t = base_type(t);
  880. return true;
  881. }
  882. bool type_has_nil(Type *t) {
  883. t = base_type(t);
  884. switch (t->kind) {
  885. case Type_Basic: {
  886. switch (t->Basic.kind) {
  887. case Basic_rawptr:
  888. case Basic_any:
  889. return true;
  890. }
  891. return false;
  892. } break;
  893. case Type_Slice:
  894. case Type_Proc:
  895. case Type_Pointer:
  896. case Type_DynamicArray:
  897. case Type_Map:
  898. return true;
  899. case Type_Union:
  900. return true;
  901. case Type_Struct:
  902. return false;
  903. }
  904. return false;
  905. }
  906. bool is_type_comparable(Type *t) {
  907. t = base_type(t);
  908. switch (t->kind) {
  909. case Type_Basic:
  910. switch (t->Basic.kind) {
  911. case Basic_UntypedNil:
  912. case Basic_any:
  913. return false;
  914. case Basic_rune:
  915. return true;
  916. }
  917. return true;
  918. case Type_Pointer:
  919. return true;
  920. case Type_Enum:
  921. return is_type_comparable(core_type(t));
  922. case Type_Array:
  923. return false;
  924. case Type_Vector:
  925. return is_type_comparable(t->Vector.elem);
  926. case Type_Proc:
  927. return true;
  928. }
  929. return false;
  930. }
  931. bool are_types_identical(Type *x, Type *y) {
  932. if (x == y) {
  933. return true;
  934. }
  935. if ((x == nullptr && y != nullptr) ||
  936. (x != nullptr && y == nullptr)) {
  937. return false;
  938. }
  939. switch (x->kind) {
  940. case Type_Generic:
  941. if (y->kind == Type_Generic) {
  942. return are_types_identical(x->Generic.specialized, y->Generic.specialized);
  943. }
  944. break;
  945. case Type_Basic:
  946. if (y->kind == Type_Basic) {
  947. return x->Basic.kind == y->Basic.kind;
  948. }
  949. break;
  950. case Type_Array:
  951. if (y->kind == Type_Array) {
  952. return (x->Array.count == y->Array.count) && are_types_identical(x->Array.elem, y->Array.elem);
  953. }
  954. break;
  955. case Type_DynamicArray:
  956. if (y->kind == Type_DynamicArray) {
  957. return are_types_identical(x->DynamicArray.elem, y->DynamicArray.elem);
  958. }
  959. break;
  960. case Type_Vector:
  961. if (y->kind == Type_Vector) {
  962. return (x->Vector.count == y->Vector.count) && are_types_identical(x->Vector.elem, y->Vector.elem);
  963. }
  964. break;
  965. case Type_Slice:
  966. if (y->kind == Type_Slice) {
  967. return are_types_identical(x->Slice.elem, y->Slice.elem);
  968. }
  969. break;
  970. case Type_Enum:
  971. return x == y; // NOTE(bill): All enums are unique
  972. case Type_Union:
  973. if (y->kind == Type_Union) {
  974. if (x->Union.variants.count == y->Union.variants.count &&
  975. x->Union.custom_align == y->Union.custom_align) {
  976. // NOTE(bill): zeroth variant is nullptr
  977. for_array(i, x->Union.variants) {
  978. if (!are_types_identical(x->Union.variants[i], y->Union.variants[i])) {
  979. return false;
  980. }
  981. }
  982. return true;
  983. }
  984. }
  985. break;
  986. case Type_Struct:
  987. if (y->kind == Type_Struct) {
  988. if (x->Struct.is_raw_union == y->Struct.is_raw_union &&
  989. x->Struct.fields.count == y->Struct.fields.count &&
  990. x->Struct.is_packed == y->Struct.is_packed &&
  991. x->Struct.is_ordered == y->Struct.is_ordered &&
  992. x->Struct.custom_align == y->Struct.custom_align) {
  993. // TODO(bill); Fix the custom alignment rule
  994. for_array(i, x->Struct.fields) {
  995. Entity *xf = x->Struct.fields[i];
  996. Entity *yf = y->Struct.fields[i];
  997. if (!are_types_identical(xf->type, yf->type)) {
  998. return false;
  999. }
  1000. if (xf->token.string != yf->token.string) {
  1001. return false;
  1002. }
  1003. bool xf_is_using = (xf->flags&EntityFlag_Using) != 0;
  1004. bool yf_is_using = (yf->flags&EntityFlag_Using) != 0;
  1005. if (xf_is_using ^ yf_is_using) {
  1006. return false;
  1007. }
  1008. }
  1009. return true;
  1010. }
  1011. }
  1012. break;
  1013. case Type_Pointer:
  1014. if (y->kind == Type_Pointer) {
  1015. return are_types_identical(x->Pointer.elem, y->Pointer.elem);
  1016. }
  1017. break;
  1018. case Type_Named:
  1019. if (y->kind == Type_Named) {
  1020. return x->Named.type_name == y->Named.type_name;
  1021. }
  1022. break;
  1023. case Type_Tuple:
  1024. if (y->kind == Type_Tuple) {
  1025. if (x->Tuple.variables.count == y->Tuple.variables.count) {
  1026. for_array(i, x->Tuple.variables) {
  1027. Entity *xe = x->Tuple.variables[i];
  1028. Entity *ye = y->Tuple.variables[i];
  1029. if (xe->kind != ye->kind || !are_types_identical(xe->type, ye->type)) {
  1030. return false;
  1031. }
  1032. }
  1033. return true;
  1034. }
  1035. }
  1036. break;
  1037. case Type_Proc:
  1038. if (y->kind == Type_Proc) {
  1039. return x->Proc.calling_convention == y->Proc.calling_convention &&
  1040. x->Proc.c_vararg == y->Proc.c_vararg &&
  1041. x->Proc.variadic == y->Proc.variadic &&
  1042. are_types_identical(x->Proc.params, y->Proc.params) &&
  1043. are_types_identical(x->Proc.results, y->Proc.results);
  1044. }
  1045. break;
  1046. case Type_Map:
  1047. if (y->kind == Type_Map) {
  1048. return x->Map.count == y->Map.count &&
  1049. are_types_identical(x->Map.key, y->Map.key) &&
  1050. are_types_identical(x->Map.value, y->Map.value);
  1051. }
  1052. break;
  1053. }
  1054. return false;
  1055. }
  1056. Type *default_bit_field_value_type(Type *type) {
  1057. if (type == nullptr) {
  1058. return t_invalid;
  1059. }
  1060. Type *t = base_type(type);
  1061. if (t->kind == Type_BitFieldValue) {
  1062. i32 bits = t->BitFieldValue.bits;
  1063. i32 size = 8*next_pow2((bits+7)/8);
  1064. switch (size) {
  1065. case 8: return t_u8;
  1066. case 16: return t_u16;
  1067. case 32: return t_u32;
  1068. case 64: return t_u64;
  1069. case 128: return t_u128;
  1070. default: GB_PANIC("Too big of a bit size!"); break;
  1071. }
  1072. }
  1073. return type;
  1074. }
  1075. Type *default_type(Type *type) {
  1076. if (type == nullptr) {
  1077. return t_invalid;
  1078. }
  1079. if (type->kind == Type_Basic) {
  1080. switch (type->Basic.kind) {
  1081. case Basic_UntypedBool: return t_bool;
  1082. case Basic_UntypedInteger: return t_int;
  1083. case Basic_UntypedFloat: return t_f64;
  1084. case Basic_UntypedComplex: return t_complex128;
  1085. case Basic_UntypedString: return t_string;
  1086. case Basic_UntypedRune: return t_rune;
  1087. }
  1088. }
  1089. if (type->kind == Type_BitFieldValue) {
  1090. return default_bit_field_value_type(type);
  1091. }
  1092. return type;
  1093. }
  1094. // NOTE(bill): Valid Compile time execution #run type
  1095. bool is_type_cte_safe(Type *type) {
  1096. type = default_type(base_type(type));
  1097. switch (type->kind) {
  1098. case Type_Basic:
  1099. switch (type->Basic.kind) {
  1100. case Basic_rawptr:
  1101. case Basic_any:
  1102. return false;
  1103. }
  1104. return true;
  1105. case Type_Pointer:
  1106. return false;
  1107. case Type_Array:
  1108. return is_type_cte_safe(type->Array.elem);
  1109. case Type_DynamicArray:
  1110. return false;
  1111. case Type_Map:
  1112. return false;
  1113. case Type_Vector: // NOTE(bill): This should always to be true but this is for sanity reasons
  1114. return is_type_cte_safe(type->Vector.elem);
  1115. case Type_Slice:
  1116. return false;
  1117. case Type_Struct: {
  1118. if (type->Struct.is_raw_union) {
  1119. return false;
  1120. }
  1121. for_array(i, type->Struct.fields) {
  1122. Entity *v = type->Struct.fields[i];
  1123. if (!is_type_cte_safe(v->type)) {
  1124. return false;
  1125. }
  1126. }
  1127. return true;
  1128. }
  1129. case Type_Tuple: {
  1130. for_array(i, type->Tuple.variables) {
  1131. Entity *v = type->Tuple.variables[i];
  1132. if (!is_type_cte_safe(v->type)) {
  1133. return false;
  1134. }
  1135. }
  1136. return true;
  1137. }
  1138. case Type_Proc:
  1139. // TODO(bill): How should I handle procedures in the CTE stage?
  1140. // return type->Proc.calling_convention == ProcCC_Odin;
  1141. return false;
  1142. }
  1143. return false;
  1144. }
  1145. enum ProcTypeOverloadKind {
  1146. ProcOverload_Identical, // The types are identical
  1147. ProcOverload_CallingConvention,
  1148. ProcOverload_ParamCount,
  1149. ProcOverload_ParamVariadic,
  1150. ProcOverload_ParamTypes,
  1151. ProcOverload_ResultCount,
  1152. ProcOverload_ResultTypes,
  1153. ProcOverload_Polymorphic,
  1154. ProcOverload_NotProcedure,
  1155. };
  1156. ProcTypeOverloadKind are_proc_types_overload_safe(Type *x, Type *y) {
  1157. if (x == nullptr && y == nullptr) return ProcOverload_NotProcedure;
  1158. if (x == nullptr && y != nullptr) return ProcOverload_NotProcedure;
  1159. if (x != nullptr && y == nullptr) return ProcOverload_NotProcedure;
  1160. if (!is_type_proc(x)) return ProcOverload_NotProcedure;
  1161. if (!is_type_proc(y)) return ProcOverload_NotProcedure;
  1162. TypeProc px = base_type(x)->Proc;
  1163. TypeProc py = base_type(y)->Proc;
  1164. // if (px.calling_convention != py.calling_convention) {
  1165. // return ProcOverload_CallingConvention;
  1166. // }
  1167. // if (px.is_polymorphic != py.is_polymorphic) {
  1168. // return ProcOverload_Polymorphic;
  1169. // }
  1170. if (px.param_count != py.param_count) {
  1171. return ProcOverload_ParamCount;
  1172. }
  1173. for (isize i = 0; i < px.param_count; i++) {
  1174. Entity *ex = px.params->Tuple.variables[i];
  1175. Entity *ey = py.params->Tuple.variables[i];
  1176. if (!are_types_identical(ex->type, ey->type)) {
  1177. return ProcOverload_ParamTypes;
  1178. }
  1179. }
  1180. // IMPORTANT TODO(bill): Determine the rules for overloading procedures with variadic parameters
  1181. if (px.variadic != py.variadic) {
  1182. return ProcOverload_ParamVariadic;
  1183. }
  1184. if (px.is_polymorphic != py.is_polymorphic) {
  1185. return ProcOverload_Polymorphic;
  1186. }
  1187. if (px.result_count != py.result_count) {
  1188. return ProcOverload_ResultCount;
  1189. }
  1190. for (isize i = 0; i < px.result_count; i++) {
  1191. Entity *ex = px.results->Tuple.variables[i];
  1192. Entity *ey = py.results->Tuple.variables[i];
  1193. if (!are_types_identical(ex->type, ey->type)) {
  1194. return ProcOverload_ResultTypes;
  1195. }
  1196. }
  1197. if (px.params != nullptr && py.params != nullptr) {
  1198. Entity *ex = px.params->Tuple.variables[0];
  1199. Entity *ey = py.params->Tuple.variables[0];
  1200. bool ok = are_types_identical(ex->type, ey->type);
  1201. if (ok) {
  1202. }
  1203. }
  1204. return ProcOverload_Identical;
  1205. }
  1206. Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_name, bool is_type, Selection sel);
  1207. Selection lookup_field(gbAllocator a, Type *type_, String field_name, bool is_type) {
  1208. return lookup_field_with_selection(a, type_, field_name, is_type, empty_selection);
  1209. }
  1210. Selection lookup_field_from_index(gbAllocator a, Type *type, i64 index) {
  1211. GB_ASSERT(is_type_struct(type) || is_type_union(type) || is_type_tuple(type));
  1212. type = base_type(type);
  1213. i64 max_count = 0;
  1214. switch (type->kind) {
  1215. case Type_Struct: max_count = type->Struct.fields.count; break;
  1216. case Type_Tuple: max_count = type->Tuple.variables.count; break;
  1217. case Type_BitField: max_count = type->BitField.field_count; break;
  1218. }
  1219. if (index >= max_count) {
  1220. return empty_selection;
  1221. }
  1222. switch (type->kind) {
  1223. case Type_Struct:
  1224. for (isize i = 0; i < max_count; i++) {
  1225. Entity *f = type->Struct.fields[i];
  1226. if (f->kind == Entity_Variable) {
  1227. if (f->Variable.field_src_index == index) {
  1228. Array<i32> sel_array = {0};
  1229. array_init_count(&sel_array, a, 1);
  1230. sel_array[0] = i;
  1231. return make_selection(f, sel_array, false);
  1232. }
  1233. }
  1234. }
  1235. break;
  1236. case Type_Tuple:
  1237. for (isize i = 0; i < max_count; i++) {
  1238. Entity *f = type->Tuple.variables[i];
  1239. if (i == index) {
  1240. Array<i32> sel_array = {0};
  1241. array_init_count(&sel_array, a, 1);
  1242. sel_array[0] = i;
  1243. return make_selection(f, sel_array, false);
  1244. }
  1245. }
  1246. break;
  1247. case Type_BitField: {
  1248. Array<i32> sel_array = {0};
  1249. array_init_count(&sel_array, a, 1);
  1250. sel_array[0] = cast(i32)index;
  1251. return make_selection(type->BitField.fields[index], sel_array, false);
  1252. } break;
  1253. }
  1254. GB_PANIC("Illegal index");
  1255. return empty_selection;
  1256. }
  1257. gb_global Entity *entity__any_data = nullptr;
  1258. gb_global Entity *entity__any_type_info = nullptr;
  1259. Entity *current_scope_lookup_entity(Scope *s, String name);
  1260. Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_name, bool is_type, Selection sel) {
  1261. GB_ASSERT(type_ != nullptr);
  1262. if (is_blank_ident(field_name)) {
  1263. return empty_selection;
  1264. }
  1265. Type *type = type_deref(type_);
  1266. bool is_ptr = type != type_;
  1267. sel.indirect = sel.indirect || is_ptr;
  1268. type = base_type(type);
  1269. if (type->kind == Type_Basic) {
  1270. switch (type->Basic.kind) {
  1271. case Basic_any: {
  1272. #if 1
  1273. // IMPORTANT TODO(bill): Should these members be available to should I only allow them with
  1274. // `Raw_Any` type?
  1275. String data_str = str_lit("data");
  1276. String type_info_str = str_lit("type_info");
  1277. if (entity__any_data == nullptr) {
  1278. entity__any_data = make_entity_field(a, nullptr, make_token_ident(data_str), t_rawptr, false, 0);
  1279. }
  1280. if (entity__any_type_info == nullptr) {
  1281. entity__any_type_info = make_entity_field(a, nullptr, make_token_ident(type_info_str), t_type_info_ptr, false, 1);
  1282. }
  1283. if (field_name == data_str) {
  1284. selection_add_index(&sel, 0);
  1285. sel.entity = entity__any_data;;
  1286. return sel;
  1287. } else if (field_name == type_info_str) {
  1288. selection_add_index(&sel, 1);
  1289. sel.entity = entity__any_type_info;
  1290. return sel;
  1291. }
  1292. #endif
  1293. } break;
  1294. }
  1295. return sel;
  1296. } else if (type->kind == Type_Vector) {
  1297. if (type->Vector.count <= 4 && !is_type_boolean(type->Vector.elem)) {
  1298. // HACK(bill): Memory leak
  1299. switch (type->Vector.count) {
  1300. #define _VECTOR_FIELD_CASE(_length, _name) \
  1301. case (_length): \
  1302. if (field_name == _name) { \
  1303. selection_add_index(&sel, (_length)-1); \
  1304. sel.entity = make_entity_vector_elem(a, nullptr, make_token_ident(str_lit(_name)), type->Vector.elem, (_length)-1); \
  1305. return sel; \
  1306. } \
  1307. /*fallthrough*/
  1308. _VECTOR_FIELD_CASE(4, "w");
  1309. _VECTOR_FIELD_CASE(3, "z");
  1310. _VECTOR_FIELD_CASE(2, "y");
  1311. _VECTOR_FIELD_CASE(1, "x");
  1312. default: break;
  1313. #undef _VECTOR_FIELD_CASE
  1314. }
  1315. }
  1316. }
  1317. if (is_type) {
  1318. switch (type->kind) {
  1319. case Type_Struct:
  1320. if (type->Struct.names != nullptr &&
  1321. field_name == "names") {
  1322. sel.entity = type->Struct.names;
  1323. return sel;
  1324. }
  1325. break;
  1326. case Type_Enum:
  1327. if (type->Enum.names != nullptr &&
  1328. field_name == "names") {
  1329. sel.entity = type->Enum.names;
  1330. return sel;
  1331. }
  1332. break;
  1333. }
  1334. if (is_type_enum(type)) {
  1335. // NOTE(bill): These may not have been added yet, so check in case
  1336. if (type->Enum.count != nullptr) {
  1337. if (field_name == "count") {
  1338. sel.entity = type->Enum.count;
  1339. return sel;
  1340. }
  1341. if (field_name == "min_value") {
  1342. sel.entity = type->Enum.min_value;
  1343. return sel;
  1344. }
  1345. if (field_name == "max_value") {
  1346. sel.entity = type->Enum.max_value;
  1347. return sel;
  1348. }
  1349. }
  1350. for (isize i = 0; i < type->Enum.field_count; i++) {
  1351. Entity *f = type->Enum.fields[i];
  1352. GB_ASSERT(f->kind == Entity_Constant);
  1353. String str = f->token.string;
  1354. if (field_name == str) {
  1355. sel.entity = f;
  1356. // selection_add_index(&sel, i);
  1357. return sel;
  1358. }
  1359. }
  1360. }
  1361. if (type->kind == Type_Struct) {
  1362. Scope *s = type->Struct.scope;
  1363. if (s != nullptr) {
  1364. Entity *found = current_scope_lookup_entity(s, field_name);
  1365. if (found != nullptr && found->kind != Entity_Variable) {
  1366. sel.entity = found;
  1367. return sel;
  1368. }
  1369. }
  1370. }
  1371. if (type->kind == Type_Generic && type->Generic.specialized != nullptr) {
  1372. Type *specialized = type->Generic.specialized;
  1373. return lookup_field_with_selection(a, specialized, field_name, is_type, sel);
  1374. }
  1375. } else if (type->kind == Type_Union) {
  1376. if (field_name == "__type_info") {
  1377. Entity *e = type->Union.union__type_info;
  1378. if (e == nullptr) {
  1379. Entity *__type_info = make_entity_field(a, nullptr, make_token_ident(str_lit("__type_info")), t_type_info_ptr, false, -1);
  1380. type->Union.union__type_info = __type_info;
  1381. e = __type_info;
  1382. }
  1383. GB_ASSERT(e != nullptr);
  1384. selection_add_index(&sel, -1); // HACK(bill): Leaky memory
  1385. sel.entity = e;
  1386. return sel;
  1387. }
  1388. } else if (type->kind == Type_Struct) {
  1389. for_array(i, type->Struct.fields) {
  1390. Entity *f = type->Struct.fields[i];
  1391. if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
  1392. continue;
  1393. }
  1394. String str = f->token.string;
  1395. if (field_name == str) {
  1396. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  1397. sel.entity = f;
  1398. return sel;
  1399. }
  1400. if (f->flags & EntityFlag_Using) {
  1401. isize prev_count = sel.index.count;
  1402. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  1403. sel = lookup_field_with_selection(a, f->type, field_name, is_type, sel);
  1404. if (sel.entity != nullptr) {
  1405. if (is_type_pointer(f->type)) {
  1406. sel.indirect = true;
  1407. }
  1408. return sel;
  1409. }
  1410. sel.index.count = prev_count;
  1411. }
  1412. }
  1413. } else if (type->kind == Type_BitField) {
  1414. for (isize i = 0; i < type->BitField.field_count; i++) {
  1415. Entity *f = type->BitField.fields[i];
  1416. if (f->kind != Entity_Variable ||
  1417. (f->flags & EntityFlag_BitFieldValue) == 0) {
  1418. continue;
  1419. }
  1420. String str = f->token.string;
  1421. if (field_name == str) {
  1422. selection_add_index(&sel, i); // HACK(bill): Leaky memory
  1423. sel.entity = f;
  1424. return sel;
  1425. }
  1426. }
  1427. }
  1428. return sel;
  1429. }
  1430. struct TypePath {
  1431. Array<Type *> path; // Entity_TypeName;
  1432. bool failure;
  1433. };
  1434. void type_path_init(TypePath *tp) {
  1435. // TODO(bill): Use an allocator that uses a backing array if it can and then use alternative allocator when exhausted
  1436. array_init(&tp->path, heap_allocator());
  1437. }
  1438. void type_path_free(TypePath *tp) {
  1439. array_free(&tp->path);
  1440. }
  1441. void type_path_print_illegal_cycle(TypePath *tp, isize start_index) {
  1442. GB_ASSERT(tp != nullptr);
  1443. GB_ASSERT(start_index < tp->path.count);
  1444. Type *t = tp->path[start_index];
  1445. GB_ASSERT(t != nullptr);
  1446. GB_ASSERT_MSG(is_type_named(t), "%s", type_to_string(t));
  1447. Entity *e = t->Named.type_name;
  1448. error(e->token, "Illegal declaration cycle of `%.*s`", LIT(t->Named.name));
  1449. // NOTE(bill): Print cycle, if it's deep enough
  1450. for (isize j = start_index; j < tp->path.count; j++) {
  1451. Type *t = tp->path[j];
  1452. GB_ASSERT_MSG(is_type_named(t), "%s", type_to_string(t));
  1453. Entity *e = t->Named.type_name;
  1454. error(e->token, "\t%.*s refers to", LIT(t->Named.name));
  1455. }
  1456. // NOTE(bill): This will only print if the path count > 1
  1457. error(e->token, "\t%.*s", LIT(t->Named.name));
  1458. tp->failure = true;
  1459. t->failure = true;
  1460. }
  1461. TypePath *type_path_push(TypePath *tp, Type *t) {
  1462. GB_ASSERT(tp != nullptr);
  1463. for (isize i = 0; i < tp->path.count; i++) {
  1464. if (tp->path[i] == t) {
  1465. type_path_print_illegal_cycle(tp, i);
  1466. }
  1467. }
  1468. if (!tp->failure && is_type_named(t)) {
  1469. array_add(&tp->path, t);
  1470. }
  1471. return tp;
  1472. }
  1473. void type_path_pop(TypePath *tp) {
  1474. if (tp != nullptr && tp->path.count > 0) {
  1475. array_pop(&tp->path);
  1476. }
  1477. }
  1478. #define FAILURE_SIZE 0
  1479. #define FAILURE_ALIGNMENT 0
  1480. i64 type_size_of_internal (gbAllocator allocator, Type *t, TypePath *path);
  1481. i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path);
  1482. i64 align_formula(i64 size, i64 align) {
  1483. if (align > 0) {
  1484. i64 result = size + align-1;
  1485. return result - result%align;
  1486. }
  1487. return size;
  1488. }
  1489. i64 type_size_of(gbAllocator allocator, Type *t) {
  1490. if (t == nullptr) {
  1491. return 0;
  1492. }
  1493. i64 size;
  1494. TypePath path = {0};
  1495. type_path_init(&path);
  1496. size = type_size_of_internal(allocator, t, &path);
  1497. type_path_free(&path);
  1498. return size;
  1499. }
  1500. i64 type_align_of(gbAllocator allocator, Type *t) {
  1501. if (t == nullptr) {
  1502. return 1;
  1503. }
  1504. i64 align;
  1505. TypePath path = {0};
  1506. type_path_init(&path);
  1507. align = type_align_of_internal(allocator, t, &path);
  1508. type_path_free(&path);
  1509. return align;
  1510. }
  1511. i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
  1512. if (t->failure) {
  1513. return FAILURE_ALIGNMENT;
  1514. }
  1515. t = base_type(t);
  1516. switch (t->kind) {
  1517. case Type_Basic: {
  1518. GB_ASSERT(is_type_typed(t));
  1519. switch (t->Basic.kind) {
  1520. case Basic_string: return build_context.word_size;
  1521. case Basic_any: return build_context.word_size;
  1522. case Basic_int: case Basic_uint: case Basic_rawptr:
  1523. return build_context.word_size;
  1524. case Basic_complex64: case Basic_complex128:
  1525. return type_size_of_internal(allocator, t, path) / 2;
  1526. }
  1527. } break;
  1528. case Type_Array: {
  1529. Type *elem = t->Array.elem;
  1530. type_path_push(path, elem);
  1531. if (path->failure) {
  1532. return FAILURE_ALIGNMENT;
  1533. }
  1534. i64 align = type_align_of_internal(allocator, t->Array.elem, path);
  1535. type_path_pop(path);
  1536. return align;
  1537. }
  1538. case Type_DynamicArray:
  1539. // data, count, capacity, allocator
  1540. return build_context.word_size;
  1541. case Type_Slice:
  1542. return build_context.word_size;
  1543. case Type_Vector: {
  1544. Type *elem = t->Vector.elem;
  1545. type_path_push(path, elem);
  1546. if (path->failure) {
  1547. return FAILURE_ALIGNMENT;
  1548. }
  1549. i64 size = type_size_of_internal(allocator, t->Vector.elem, path);
  1550. type_path_pop(path);
  1551. i64 count = gb_max(prev_pow2(t->Vector.count), 1);
  1552. i64 total = size * count;
  1553. return gb_clamp(total, 1, build_context.max_align);
  1554. } break;
  1555. case Type_Tuple: {
  1556. i64 max = 1;
  1557. for_array(i, t->Tuple.variables) {
  1558. i64 align = type_align_of_internal(allocator, t->Tuple.variables[i]->type, path);
  1559. if (max < align) {
  1560. max = align;
  1561. }
  1562. }
  1563. return max;
  1564. } break;
  1565. case Type_Map: {
  1566. if (t->Map.count == 0) { // Dynamic
  1567. return type_align_of_internal(allocator, t->Map.generated_struct_type, path);
  1568. }
  1569. GB_PANIC("TODO(bill): Fixed map alignment");
  1570. } break;
  1571. case Type_Enum:
  1572. return type_align_of_internal(allocator, t->Enum.base_type, path);
  1573. case Type_Union: {
  1574. i64 max = build_context.word_size;
  1575. for_array(i, t->Union.variants) {
  1576. Type *variant = t->Union.variants[i];
  1577. type_path_push(path, variant);
  1578. if (path->failure) {
  1579. return FAILURE_ALIGNMENT;
  1580. }
  1581. i64 align = type_align_of_internal(allocator, variant, path);
  1582. type_path_pop(path);
  1583. if (max < align) {
  1584. max = align;
  1585. }
  1586. }
  1587. return max;
  1588. } break;
  1589. case Type_Struct: {
  1590. if (t->Struct.is_raw_union) {
  1591. i64 max = 1;
  1592. for_array(i, t->Struct.fields) {
  1593. Type *field_type = t->Struct.fields[i]->type;
  1594. type_path_push(path, field_type);
  1595. if (path->failure) {
  1596. return FAILURE_ALIGNMENT;
  1597. }
  1598. i64 align = type_align_of_internal(allocator, field_type, path);
  1599. type_path_pop(path);
  1600. if (max < align) {
  1601. max = align;
  1602. }
  1603. }
  1604. return max;
  1605. } else {
  1606. if (t->Struct.custom_align > 0) {
  1607. return gb_clamp(t->Struct.custom_align, 1, build_context.max_align);
  1608. }
  1609. if (t->Struct.fields.count > 0) {
  1610. i64 max = 1;
  1611. if (t->Struct.is_packed) {
  1612. max = build_context.word_size;
  1613. }
  1614. for_array(i, t->Struct.fields) {
  1615. Type *field_type = t->Struct.fields[i]->type;
  1616. type_path_push(path, field_type);
  1617. if (path->failure) {
  1618. return FAILURE_ALIGNMENT;
  1619. }
  1620. i64 align = type_align_of_internal(allocator, field_type, path);
  1621. type_path_pop(path);
  1622. if (max < align) {
  1623. max = align;
  1624. }
  1625. }
  1626. return max;
  1627. }
  1628. }
  1629. } break;
  1630. case Type_BitField: {
  1631. i64 align = 1;
  1632. if (t->BitField.custom_align > 0) {
  1633. align = t->BitField.custom_align;
  1634. }
  1635. return gb_clamp(next_pow2(align), 1, build_context.max_align);
  1636. } break;
  1637. }
  1638. // return gb_clamp(next_pow2(type_size_of(allocator, t)), 1, build_context.max_align);
  1639. // NOTE(bill): Things that are bigger than build_context.word_size, are actually comprised of smaller types
  1640. // TODO(bill): Is this correct for 128-bit types (integers)?
  1641. return gb_clamp(next_pow2(type_size_of_internal(allocator, t, path)), 1, build_context.word_size);
  1642. }
  1643. i64 *type_set_offsets_of(gbAllocator allocator, Array<Entity *> fields, bool is_packed, bool is_raw_union) {
  1644. i64 *offsets = gb_alloc_array(allocator, i64, fields.count);
  1645. i64 curr_offset = 0;
  1646. if (is_raw_union) {
  1647. for_array(i, fields) {
  1648. offsets[i] = 0;
  1649. }
  1650. } else if (is_packed) {
  1651. for_array(i, fields) {
  1652. i64 size = type_size_of(allocator, fields[i]->type);
  1653. offsets[i] = curr_offset;
  1654. curr_offset += size;
  1655. }
  1656. } else {
  1657. for_array(i, fields) {
  1658. i64 align = gb_max(type_align_of(allocator, fields[i]->type), 1);
  1659. i64 size = gb_max(type_size_of(allocator, fields[i]->type), 0);
  1660. curr_offset = align_formula(curr_offset, align);
  1661. offsets[i] = curr_offset;
  1662. curr_offset += size;
  1663. }
  1664. }
  1665. return offsets;
  1666. }
  1667. bool type_set_offsets(gbAllocator allocator, Type *t) {
  1668. t = base_type(t);
  1669. if (t->kind == Type_Struct) {
  1670. if (!t->Struct.are_offsets_set) {
  1671. t->Struct.are_offsets_being_processed = true;
  1672. t->Struct.offsets = type_set_offsets_of(allocator, t->Struct.fields, t->Struct.is_packed, t->Struct.is_raw_union);
  1673. t->Struct.are_offsets_set = true;
  1674. return true;
  1675. }
  1676. } else if (is_type_tuple(t)) {
  1677. if (!t->Tuple.are_offsets_set) {
  1678. t->Struct.are_offsets_being_processed = true;
  1679. t->Tuple.offsets = type_set_offsets_of(allocator, t->Tuple.variables, false, false);
  1680. t->Tuple.are_offsets_set = true;
  1681. return true;
  1682. }
  1683. } else {
  1684. GB_PANIC("Invalid type for setting offsets");
  1685. }
  1686. return false;
  1687. }
  1688. i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
  1689. if (t->failure) {
  1690. return FAILURE_SIZE;
  1691. }
  1692. switch (t->kind) {
  1693. case Type_Named: {
  1694. type_path_push(path, t);
  1695. if (path->failure) {
  1696. return FAILURE_ALIGNMENT;
  1697. }
  1698. i64 size = type_size_of_internal(allocator, t->Named.base, path);
  1699. type_path_pop(path);
  1700. return size;
  1701. } break;
  1702. case Type_Basic: {
  1703. GB_ASSERT_MSG(is_type_typed(t), "%s", type_to_string(t));
  1704. BasicKind kind = t->Basic.kind;
  1705. i64 size = t->Basic.size;
  1706. if (size > 0) {
  1707. return size;
  1708. }
  1709. switch (kind) {
  1710. case Basic_string: return 2*build_context.word_size;
  1711. case Basic_any: return 2*build_context.word_size;
  1712. case Basic_int: case Basic_uint: case Basic_rawptr:
  1713. return build_context.word_size;
  1714. }
  1715. } break;
  1716. case Type_Array: {
  1717. i64 count, align, size, alignment;
  1718. count = t->Array.count;
  1719. if (count == 0) {
  1720. return 0;
  1721. }
  1722. align = type_align_of_internal(allocator, t->Array.elem, path);
  1723. if (path->failure) {
  1724. return FAILURE_SIZE;
  1725. }
  1726. size = type_size_of_internal( allocator, t->Array.elem, path);
  1727. alignment = align_formula(size, align);
  1728. return alignment*(count-1) + size;
  1729. } break;
  1730. case Type_DynamicArray:
  1731. // data + len + cap + allocator(procedure+data)
  1732. return 3*build_context.word_size + 2*build_context.word_size;
  1733. case Type_Vector: {
  1734. #if 0
  1735. i64 count, bit_size, total_size_in_bits, total_size;
  1736. count = t->Vector.count;
  1737. if (count == 0) {
  1738. return 0;
  1739. }
  1740. type_path_push(path, t->Vector.elem);
  1741. if (path->failure) {
  1742. return FAILURE_SIZE;
  1743. }
  1744. bit_size = 8*type_size_of_internal(allocator, t->Vector.elem, path);
  1745. type_path_pop(path);
  1746. if (is_type_boolean(t->Vector.elem)) {
  1747. bit_size = 1; // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
  1748. // Silly LLVM spec
  1749. }
  1750. total_size_in_bits = bit_size * count;
  1751. total_size = (total_size_in_bits+7)/8;
  1752. return total_size;
  1753. #else
  1754. i64 count = t->Vector.count;
  1755. if (count == 0) {
  1756. return 0;
  1757. }
  1758. i64 elem_align = type_align_of_internal(allocator, t->Vector.elem, path);
  1759. if (path->failure) {
  1760. return FAILURE_SIZE;
  1761. }
  1762. i64 vector_align = type_align_of_internal(allocator, t, path);
  1763. i64 elem_size = type_size_of_internal(allocator, t->Vector.elem, path);
  1764. i64 alignment = align_formula(elem_size, elem_align);
  1765. return align_formula(alignment*(count-1) + elem_size, vector_align);
  1766. #endif
  1767. } break;
  1768. case Type_Slice: // ptr + count
  1769. return 3 * build_context.word_size;
  1770. case Type_Map: {
  1771. if (t->Map.count == 0) { // Dynamic
  1772. return type_size_of_internal(allocator, t->Map.generated_struct_type, path);
  1773. }
  1774. GB_PANIC("TODO(bill): Fixed map size");
  1775. }
  1776. case Type_Tuple: {
  1777. i64 count, align, size;
  1778. count = t->Tuple.variables.count;
  1779. if (count == 0) {
  1780. return 0;
  1781. }
  1782. align = type_align_of_internal(allocator, t, path);
  1783. type_set_offsets(allocator, t);
  1784. size = t->Tuple.offsets[count-1] + type_size_of_internal(allocator, t->Tuple.variables[count-1]->type, path);
  1785. return align_formula(size, align);
  1786. } break;
  1787. case Type_Enum:
  1788. return type_size_of_internal(allocator, t->Enum.base_type, path);
  1789. case Type_Union: {
  1790. i64 align = type_align_of_internal(allocator, t, path);
  1791. if (path->failure) {
  1792. return FAILURE_SIZE;
  1793. }
  1794. i64 max = 0;
  1795. i64 field_size = 0;
  1796. for_array(i, t->Union.variants) {
  1797. Type *variant_type = t->Union.variants[i];
  1798. i64 size = type_size_of_internal(allocator, variant_type, path);
  1799. if (max < size) {
  1800. max = size;
  1801. }
  1802. }
  1803. // NOTE(bill): Align to int
  1804. i64 size = align_formula(max, build_context.word_size);
  1805. // NOTE(bill): Calculate the padding between the common fields and the tag
  1806. t->Union.variant_block_size = size - field_size;
  1807. size += type_size_of(allocator, t_int);
  1808. size = align_formula(size, align);
  1809. return size;
  1810. } break;
  1811. case Type_Struct: {
  1812. if (t->Struct.is_raw_union) {
  1813. i64 count = t->Struct.fields.count;
  1814. i64 align = type_align_of_internal(allocator, t, path);
  1815. if (path->failure) {
  1816. return FAILURE_SIZE;
  1817. }
  1818. i64 max = 0;
  1819. for (isize i = 0; i < count; i++) {
  1820. i64 size = type_size_of_internal(allocator, t->Struct.fields[i]->type, path);
  1821. if (max < size) {
  1822. max = size;
  1823. }
  1824. }
  1825. // TODO(bill): Is this how it should work?
  1826. return align_formula(max, align);
  1827. } else {
  1828. i64 count = t->Struct.fields.count;
  1829. if (count == 0) {
  1830. return 0;
  1831. }
  1832. i64 align = type_align_of_internal(allocator, t, path);
  1833. if (path->failure) {
  1834. return FAILURE_SIZE;
  1835. }
  1836. if (t->Struct.are_offsets_being_processed && t->Struct.offsets == nullptr) {
  1837. type_path_print_illegal_cycle(path, path->path.count-1);
  1838. return FAILURE_SIZE;
  1839. }
  1840. type_set_offsets(allocator, t);
  1841. i64 size = t->Struct.offsets[count-1] + type_size_of_internal(allocator, t->Struct.fields[count-1]->type, path);
  1842. return align_formula(size, align);
  1843. }
  1844. } break;
  1845. case Type_BitField: {
  1846. i64 align = 8*type_align_of_internal(allocator, t, path);
  1847. i64 end = 0;
  1848. if (t->BitField.field_count > 0) {
  1849. i64 last = t->BitField.field_count-1;
  1850. end = t->BitField.offsets[last] + t->BitField.sizes[last];
  1851. }
  1852. i64 bits = align_formula(end, align);
  1853. GB_ASSERT((bits%8) == 0);
  1854. return bits/8;
  1855. } break;
  1856. }
  1857. // Catch all
  1858. return build_context.word_size;
  1859. }
  1860. i64 type_offset_of(gbAllocator allocator, Type *t, i32 index) {
  1861. t = base_type(t);
  1862. if (t->kind == Type_Struct && !t->Struct.is_raw_union) {
  1863. type_set_offsets(allocator, t);
  1864. if (gb_is_between(index, 0, t->Struct.fields.count-1)) {
  1865. return t->Struct.offsets[index];
  1866. }
  1867. } else if (t->kind == Type_Tuple) {
  1868. type_set_offsets(allocator, t);
  1869. if (gb_is_between(index, 0, t->Tuple.variables.count-1)) {
  1870. return t->Tuple.offsets[index];
  1871. }
  1872. } else if (t->kind == Type_Basic) {
  1873. if (t->Basic.kind == Basic_string) {
  1874. switch (index) {
  1875. case 0: return 0; // data
  1876. case 1: return build_context.word_size; // count
  1877. }
  1878. } else if (t->Basic.kind == Basic_any) {
  1879. switch (index) {
  1880. case 0: return 0; // type_info
  1881. case 1: return build_context.word_size; // data
  1882. }
  1883. }
  1884. } else if (t->kind == Type_Slice) {
  1885. switch (index) {
  1886. case 0: return 0; // data
  1887. case 1: return 1*build_context.word_size; // count
  1888. case 2: return 2*build_context.word_size; // capacity
  1889. }
  1890. } else if (t->kind == Type_DynamicArray) {
  1891. switch (index) {
  1892. case 0: return 0; // data
  1893. case 1: return 1*build_context.word_size; // count
  1894. case 2: return 2*build_context.word_size; // capacity
  1895. case 3: return 3*build_context.word_size; // allocator
  1896. }
  1897. }
  1898. return 0;
  1899. }
  1900. i64 type_offset_of_from_selection(gbAllocator allocator, Type *type, Selection sel) {
  1901. GB_ASSERT(sel.indirect == false);
  1902. Type *t = type;
  1903. i64 offset = 0;
  1904. for_array(i, sel.index) {
  1905. isize index = sel.index[i];
  1906. t = base_type(t);
  1907. offset += type_offset_of(allocator, t, index);
  1908. if (t->kind == Type_Struct && !t->Struct.is_raw_union) {
  1909. t = t->Struct.fields[index]->type;
  1910. } else {
  1911. // NOTE(bill): No need to worry about custom types, just need the alignment
  1912. switch (t->kind) {
  1913. case Type_Basic:
  1914. if (t->Basic.kind == Basic_string) {
  1915. switch (index) {
  1916. case 0: t = t_rawptr; break;
  1917. case 1: t = t_int; break;
  1918. }
  1919. } else if (t->Basic.kind == Basic_any) {
  1920. switch (index) {
  1921. case 0: t = t_type_info_ptr; break;
  1922. case 1: t = t_rawptr; break;
  1923. }
  1924. }
  1925. break;
  1926. case Type_Slice:
  1927. switch (index) {
  1928. case 0: t = t_rawptr; break;
  1929. case 1: t = t_int; break;
  1930. case 2: t = t_int; break;
  1931. }
  1932. break;
  1933. case Type_DynamicArray:
  1934. switch (index) {
  1935. case 0: t = t_rawptr; break;
  1936. case 1: t = t_int; break;
  1937. case 2: t = t_int; break;
  1938. case 3: t = t_allocator; break;
  1939. }
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. return offset;
  1945. }
  1946. gbString write_type_to_string(gbString str, Type *type) {
  1947. if (type == nullptr) {
  1948. return gb_string_appendc(str, "<no type>");
  1949. }
  1950. switch (type->kind) {
  1951. case Type_Basic:
  1952. str = gb_string_append_length(str, type->Basic.name.text, type->Basic.name.len);
  1953. break;
  1954. case Type_Generic:
  1955. if (type->Generic.name.len == 0) {
  1956. str = gb_string_appendc(str, "type");
  1957. } else {
  1958. String name = type->Generic.name;
  1959. str = gb_string_appendc(str, "$");
  1960. str = gb_string_append_length(str, name.text, name.len);
  1961. if (type->Generic.specialized != nullptr) {
  1962. str = gb_string_appendc(str, "/");
  1963. str = write_type_to_string(str, type->Generic.specialized);
  1964. }
  1965. }
  1966. break;
  1967. case Type_Pointer:
  1968. str = gb_string_appendc(str, "^");
  1969. str = write_type_to_string(str, type->Pointer.elem);
  1970. break;
  1971. case Type_Array:
  1972. str = gb_string_appendc(str, gb_bprintf("[%d]", cast(int)type->Array.count));
  1973. str = write_type_to_string(str, type->Array.elem);
  1974. break;
  1975. case Type_Vector:
  1976. str = gb_string_appendc(str, gb_bprintf("[vector %d]", cast(int)type->Vector.count));
  1977. str = write_type_to_string(str, type->Vector.elem);
  1978. break;
  1979. case Type_Slice:
  1980. str = gb_string_appendc(str, "[]");
  1981. str = write_type_to_string(str, type->Array.elem);
  1982. break;
  1983. case Type_DynamicArray:
  1984. str = gb_string_appendc(str, "[dynamic]");
  1985. str = write_type_to_string(str, type->DynamicArray.elem);
  1986. break;
  1987. case Type_Enum:
  1988. str = gb_string_appendc(str, "enum");
  1989. if (type->Enum.base_type != nullptr) {
  1990. str = gb_string_appendc(str, " ");
  1991. str = write_type_to_string(str, type->Enum.base_type);
  1992. }
  1993. str = gb_string_appendc(str, " {");
  1994. for (isize i = 0; i < type->Enum.field_count; i++) {
  1995. Entity *f = type->Enum.fields[i];
  1996. GB_ASSERT(f->kind == Entity_Constant);
  1997. if (i > 0) {
  1998. str = gb_string_appendc(str, ", ");
  1999. }
  2000. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2001. // str = gb_string_appendc(str, " = ");
  2002. }
  2003. str = gb_string_appendc(str, "}");
  2004. break;
  2005. case Type_Union:
  2006. str = gb_string_appendc(str, "union{");
  2007. for_array(i, type->Union.variants) {
  2008. Type *t = type->Union.variants[i];
  2009. if (i > 0) str = gb_string_appendc(str, ", ");
  2010. str = write_type_to_string(str, t);
  2011. }
  2012. str = gb_string_appendc(str, "}");
  2013. break;
  2014. case Type_Struct: {
  2015. if (type->Struct.is_raw_union) {
  2016. str = gb_string_appendc(str, "raw_union{");
  2017. for_array(i, type->Struct.fields) {
  2018. Entity *f = type->Struct.fields[i];
  2019. GB_ASSERT(f->kind == Entity_Variable);
  2020. if (i > 0) {
  2021. str = gb_string_appendc(str, ", ");
  2022. }
  2023. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2024. str = gb_string_appendc(str, ": ");
  2025. str = write_type_to_string(str, f->type);
  2026. }
  2027. str = gb_string_appendc(str, "}");
  2028. } else {
  2029. str = gb_string_appendc(str, "struct");
  2030. if (type->Struct.is_packed) {
  2031. str = gb_string_appendc(str, " #packed");
  2032. }
  2033. if (type->Struct.is_ordered) {
  2034. str = gb_string_appendc(str, " #ordered");
  2035. }
  2036. str = gb_string_appendc(str, " {");
  2037. for_array(i, type->Struct.fields) {
  2038. Entity *f = type->Struct.fields[i];
  2039. GB_ASSERT(f->kind == Entity_Variable);
  2040. if (i > 0) {
  2041. str = gb_string_appendc(str, ", ");
  2042. }
  2043. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2044. str = gb_string_appendc(str, ": ");
  2045. str = write_type_to_string(str, f->type);
  2046. }
  2047. str = gb_string_appendc(str, "}");
  2048. }
  2049. } break;
  2050. case Type_Map: {
  2051. str = gb_string_appendc(str, "map[");
  2052. if (type->Map.count > 0) {
  2053. str = gb_string_appendc(str, gb_bprintf("%d, ", cast(int)type->Map.count));
  2054. }
  2055. str = write_type_to_string(str, type->Map.key);
  2056. str = gb_string_appendc(str, "]");
  2057. str = write_type_to_string(str, type->Map.value);
  2058. } break;
  2059. case Type_Named:
  2060. if (type->Named.type_name != nullptr) {
  2061. str = gb_string_append_length(str, type->Named.name.text, type->Named.name.len);
  2062. } else {
  2063. // NOTE(bill): Just in case
  2064. str = gb_string_appendc(str, "<named type>");
  2065. }
  2066. break;
  2067. case Type_Tuple:
  2068. if (type->Tuple.variables.count > 0) {
  2069. for_array(i, type->Tuple.variables) {
  2070. Entity *var = type->Tuple.variables[i];
  2071. if (var != nullptr) {
  2072. if (i > 0) {
  2073. str = gb_string_appendc(str, ", ");
  2074. }
  2075. if (var->kind == Entity_Variable) {
  2076. if (var->flags&EntityFlag_CVarArg) {
  2077. str = gb_string_appendc(str, "#c_vararg ");
  2078. }
  2079. if (var->flags&EntityFlag_Ellipsis) {
  2080. Type *slice = base_type(var->type);
  2081. str = gb_string_appendc(str, "..");
  2082. GB_ASSERT(var->type->kind == Type_Slice);
  2083. str = write_type_to_string(str, slice->Slice.elem);
  2084. } else {
  2085. str = write_type_to_string(str, var->type);
  2086. }
  2087. } else {
  2088. GB_ASSERT(var->kind == Entity_TypeName);
  2089. if (var->type->kind == Type_Generic) {
  2090. str = gb_string_appendc(str, "type/");
  2091. str = write_type_to_string(str, var->type);
  2092. } else {
  2093. str = gb_string_appendc(str, "type");
  2094. }
  2095. }
  2096. }
  2097. }
  2098. }
  2099. break;
  2100. case Type_Proc:
  2101. str = gb_string_appendc(str, "proc(");
  2102. if (type->Proc.params) {
  2103. str = write_type_to_string(str, type->Proc.params);
  2104. }
  2105. str = gb_string_appendc(str, ")");
  2106. if (type->Proc.results) {
  2107. str = gb_string_appendc(str, " -> ");
  2108. str = write_type_to_string(str, type->Proc.results);
  2109. }
  2110. switch (type->Proc.calling_convention) {
  2111. case ProcCC_Odin:
  2112. // str = gb_string_appendc(str, " #cc_odin");
  2113. break;
  2114. case ProcCC_C:
  2115. str = gb_string_appendc(str, " #cc_c");
  2116. break;
  2117. case ProcCC_Std:
  2118. str = gb_string_appendc(str, " #cc_std");
  2119. break;
  2120. case ProcCC_Fast:
  2121. str = gb_string_appendc(str, " #cc_fast");
  2122. break;
  2123. }
  2124. break;
  2125. case Type_BitField:
  2126. str = gb_string_appendc(str, "bit_field ");
  2127. if (type->BitField.custom_align != 0) {
  2128. str = gb_string_appendc(str, gb_bprintf("#align %d ", cast(int)type->BitField.custom_align));
  2129. }
  2130. str = gb_string_appendc(str, "{");
  2131. for (isize i = 0; i < type->BitField.field_count; i++) {
  2132. Entity *f = type->BitField.fields[i];
  2133. GB_ASSERT(f->kind == Entity_Variable);
  2134. GB_ASSERT(f->type != nullptr && f->type->kind == Type_BitFieldValue);
  2135. str = gb_string_appendc(str, "{");
  2136. if (i > 0) {
  2137. str = gb_string_appendc(str, ", ");
  2138. }
  2139. str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
  2140. str = gb_string_appendc(str, " : ");
  2141. str = gb_string_appendc(str, gb_bprintf("%lld", cast(long long)f->type->BitFieldValue.bits));
  2142. }
  2143. str = gb_string_appendc(str, "}");
  2144. break;
  2145. case Type_BitFieldValue:
  2146. str = gb_string_appendc(str, gb_bprintf("(bit field value with %d bits)", cast(int)type->BitFieldValue.bits));
  2147. break;
  2148. }
  2149. return str;
  2150. }
  2151. gbString type_to_string(Type *type) {
  2152. return write_type_to_string(gb_string_make(heap_allocator(), ""), type);
  2153. }