types.cpp 124 KB

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