types.cpp 83 KB

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