types.cpp 87 KB

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