types.cpp 87 KB

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