types.cpp 112 KB

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