types.cpp 70 KB

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