types.cpp 66 KB

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