types.cpp 103 KB

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