types.cpp 69 KB

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