check_type.cpp 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  1. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location);
  2. void populate_using_array_index(CheckerContext *ctx, Ast *node, AstField *field, Type *t, String name, i32 idx) {
  3. t = base_type(t);
  4. GB_ASSERT(t->kind == Type_Array);
  5. Entity *e = scope_lookup_current(ctx->scope, name);
  6. if (e != nullptr) {
  7. gbString str = nullptr;
  8. defer (gb_string_free(str));
  9. if (node != nullptr) {
  10. str = expr_to_string(node);
  11. }
  12. if (str != nullptr) {
  13. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  14. } else {
  15. error(e->token, "'%.*s' is already declared", LIT(name));
  16. }
  17. } else {
  18. Token tok = make_token_ident(name);
  19. if (field->names.count > 0) {
  20. tok.pos = ast_token(field->names[0]).pos;
  21. } else {
  22. tok.pos = ast_token(field->type).pos;
  23. }
  24. Entity *f = alloc_entity_array_elem(nullptr, tok, t->Array.elem, idx);
  25. add_entity(ctx->checker, ctx->scope, nullptr, f);
  26. }
  27. }
  28. void populate_using_entity_scope(CheckerContext *ctx, Ast *node, AstField *field, Type *t) {
  29. if (t == nullptr) {
  30. return;
  31. }
  32. t = base_type(type_deref(t));
  33. gbString str = nullptr;
  34. defer (gb_string_free(str));
  35. if (node != nullptr) {
  36. str = expr_to_string(node);
  37. }
  38. if (t->kind == Type_Struct) {
  39. for_array(i, t->Struct.fields) {
  40. Entity *f = t->Struct.fields[i];
  41. GB_ASSERT(f->kind == Entity_Variable);
  42. String name = f->token.string;
  43. Entity *e = scope_lookup_current(ctx->scope, name);
  44. if (e != nullptr && name != "_") {
  45. // TODO(bill): Better type error
  46. if (str != nullptr) {
  47. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  48. } else {
  49. error(e->token, "'%.*s' is already declared", LIT(name));
  50. }
  51. } else {
  52. add_entity(ctx->checker, ctx->scope, nullptr, f);
  53. if (f->flags & EntityFlag_Using) {
  54. populate_using_entity_scope(ctx, node, field, f->type);
  55. }
  56. }
  57. }
  58. } else if (t->kind == Type_BitField) {
  59. for_array(i, t->BitField.fields) {
  60. Entity *f = t->BitField.fields[i];
  61. String name = f->token.string;
  62. Entity *e = scope_lookup_current(ctx->scope, name);
  63. if ((e != nullptr && name != "_") && (e != f)) {
  64. // TODO(bill): Better type error
  65. if (str != nullptr) {
  66. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  67. } else {
  68. error(e->token, "'%.*s' is already declared", LIT(name));
  69. }
  70. } else {
  71. add_entity(ctx->checker, ctx->scope, nullptr, f);
  72. }
  73. }
  74. } else if (t->kind == Type_Array && t->Array.count <= 4) {
  75. Entity *e = nullptr;
  76. String name = {};
  77. i32 idx = 0;
  78. switch (t->Array.count) {
  79. case 4:
  80. populate_using_array_index(ctx, node, field, t, str_lit("w"), 3);
  81. populate_using_array_index(ctx, node, field, t, str_lit("a"), 3);
  82. /*fallthrough*/
  83. case 3:
  84. populate_using_array_index(ctx, node, field, t, str_lit("z"), 2);
  85. populate_using_array_index(ctx, node, field, t, str_lit("b"), 2);
  86. /*fallthrough*/
  87. case 2:
  88. populate_using_array_index(ctx, node, field, t, str_lit("y"), 1);
  89. populate_using_array_index(ctx, node, field, t, str_lit("g"), 1);
  90. /*fallthrough*/
  91. case 1:
  92. populate_using_array_index(ctx, node, field, t, str_lit("x"), 0);
  93. populate_using_array_index(ctx, node, field, t, str_lit("r"), 0);
  94. /*fallthrough*/
  95. default:
  96. break;
  97. }
  98. }
  99. }
  100. bool does_field_type_allow_using(Type *t) {
  101. t = base_type(t);
  102. if (is_type_struct(t)) {
  103. return true;
  104. } else if (is_type_raw_union(t)) {
  105. return true;
  106. } else if (is_type_bit_field(t)) {
  107. return true;
  108. } else if (is_type_array(t)) {
  109. return t->Array.count <= 4;
  110. } else if (is_type_typeid(t)) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. void check_struct_fields(CheckerContext *ctx, Ast *node, Array<Entity *> *fields, Array<String> *tags, Slice<Ast *> const &params,
  116. isize init_field_capacity, Type *struct_type, String context) {
  117. *fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
  118. *tags = array_make<String>(heap_allocator(), 0, init_field_capacity);
  119. GB_ASSERT(node->kind == Ast_StructType);
  120. GB_ASSERT(struct_type->kind == Type_Struct);
  121. isize variable_count = 0;
  122. for_array(i, params) {
  123. Ast *field = params[i];
  124. if (ast_node_expect(field, Ast_Field)) {
  125. ast_node(f, Field, field);
  126. variable_count += gb_max(f->names.count, 1);
  127. }
  128. }
  129. i32 field_src_index = 0;
  130. for_array(i, params) {
  131. Ast *param = params[i];
  132. if (param->kind != Ast_Field) {
  133. continue;
  134. }
  135. ast_node(p, Field, param);
  136. Ast *type_expr = p->type;
  137. Type *type = nullptr;
  138. bool detemine_type_from_operand = false;
  139. if (type_expr != nullptr) {
  140. type = check_type_expr(ctx, type_expr, nullptr);
  141. if (is_type_polymorphic(type)) {
  142. struct_type->Struct.is_polymorphic = true;
  143. type = nullptr;
  144. }
  145. }
  146. if (type == nullptr) {
  147. error(params[i], "Invalid parameter type");
  148. type = t_invalid;
  149. }
  150. if (is_type_untyped(type)) {
  151. if (is_type_untyped_undef(type)) {
  152. error(params[i], "Cannot determine parameter type from ---");
  153. } else {
  154. error(params[i], "Cannot determine parameter type from a nil");
  155. }
  156. type = t_invalid;
  157. }
  158. bool is_using = (p->flags&FieldFlag_using) != 0;
  159. for_array(j, p->names) {
  160. Ast *name = p->names[j];
  161. if (!ast_node_expect(name, Ast_Ident)) {
  162. continue;
  163. }
  164. Token name_token = name->Ident.token;
  165. Entity *field = alloc_entity_field(ctx->scope, name_token, type, is_using, field_src_index);
  166. add_entity(ctx->checker, ctx->scope, name, field);
  167. array_add(fields, field);
  168. array_add(tags, p->tag.string);
  169. field_src_index += 1;
  170. }
  171. if (is_using && p->names.count > 0) {
  172. Type *first_type = (*fields)[fields->count-1]->type;
  173. Type *t = base_type(type_deref(first_type));
  174. if (!does_field_type_allow_using(t) &&
  175. p->names.count >= 1 &&
  176. p->names[0]->kind == Ast_Ident) {
  177. Token name_token = p->names[0]->Ident.token;
  178. gbString type_str = type_to_string(first_type);
  179. error(name_token, "'using' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  180. gb_string_free(type_str);
  181. continue;
  182. }
  183. populate_using_entity_scope(ctx, node, p, type);
  184. }
  185. }
  186. }
  187. Entity *make_names_field_for_struct(CheckerContext *ctx, Scope *scope) {
  188. Entity *e = alloc_entity_field(scope, make_token_ident(str_lit("names")), t_string_slice, false, 0);
  189. e->flags |= EntityFlag_TypeField;
  190. e->flags |= EntityFlag_Value;
  191. return e;
  192. }
  193. bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) {
  194. GB_ASSERT(align_ != nullptr);
  195. Operand o = {};
  196. check_expr(ctx, &o, node);
  197. if (o.mode != Addressing_Constant) {
  198. if (o.mode != Addressing_Invalid) {
  199. error(node, "#align must be a constant");
  200. }
  201. return false;
  202. }
  203. Type *type = base_type(o.type);
  204. if (is_type_untyped(type) || is_type_integer(type)) {
  205. if (o.value.kind == ExactValue_Integer) {
  206. BigInt v = o.value.value_integer;
  207. if (v.len > 1) {
  208. gbAllocator a = heap_allocator();
  209. String str = big_int_to_string(a, &v);
  210. error(node, "#align too large, %.*s", LIT(str));
  211. gb_free(a, str.text);
  212. return false;
  213. }
  214. i64 align = big_int_to_i64(&v);
  215. if (align < 1 || !gb_is_power_of_two(cast(isize)align)) {
  216. error(node, "#align must be a power of 2, got %lld", align);
  217. return false;
  218. }
  219. // NOTE(bill): Success!!!
  220. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  221. if (custom_align < align) {
  222. warning(node, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  223. }
  224. *align_ = custom_align;
  225. return true;
  226. }
  227. }
  228. error(node, "#align must be an integer");
  229. return false;
  230. }
  231. Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure) {
  232. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  233. if (found_gen_types != nullptr) {
  234. for_array(i, *found_gen_types) {
  235. Entity *e = (*found_gen_types)[i];
  236. Type *t = base_type(e->type);
  237. TypeTuple *tuple = get_record_polymorphic_params(t);
  238. GB_ASSERT(param_count == tuple->variables.count);
  239. bool skip = false;
  240. for (isize j = 0; j < param_count; j++) {
  241. Entity *p = tuple->variables[j];
  242. Operand o = ordered_operands[j];
  243. Entity *oe = entity_of_node(o.expr);
  244. if (p == oe) {
  245. // NOTE(bill): This is the same type, make sure that it will be be same thing and use that
  246. // Saves on a lot of checking too below
  247. continue;
  248. }
  249. if (p->kind == Entity_TypeName) {
  250. if (is_type_polymorphic(o.type)) {
  251. // NOTE(bill): Do not add polymorphic version to the gen_types
  252. skip = true;
  253. break;
  254. }
  255. if (!are_types_identical(o.type, p->type)) {
  256. skip = true;
  257. break;
  258. }
  259. } else if (p->kind == Entity_Constant) {
  260. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  261. skip = true;
  262. break;
  263. }
  264. if (!are_types_identical(o.type, p->type)) {
  265. skip = true;
  266. break;
  267. }
  268. } else {
  269. GB_PANIC("Unknown entity kind");
  270. }
  271. }
  272. if (!skip) {
  273. return e;
  274. }
  275. }
  276. }
  277. return nullptr;
  278. }
  279. void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, Type *named_type, Type *original_type) {
  280. GB_ASSERT(is_type_named(named_type));
  281. gbAllocator a = heap_allocator();
  282. Scope *s = ctx->scope->parent;
  283. Entity *e = nullptr;
  284. {
  285. Token token = ast_token(node);
  286. token.kind = Token_String;
  287. token.string = named_type->Named.name;
  288. Ast *node = ast_ident(nullptr, token);
  289. e = alloc_entity_type_name(s, token, named_type);
  290. e->state = EntityState_Resolved;
  291. e->file = ctx->file;
  292. e->pkg = ctx->pkg;
  293. add_entity_use(ctx, node, e);
  294. }
  295. named_type->Named.type_name = e;
  296. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  297. if (found_gen_types) {
  298. array_add(found_gen_types, e);
  299. } else {
  300. auto array = array_make<Entity *>(heap_allocator());
  301. array_add(&array, e);
  302. map_set(&ctx->checker->info.gen_types, hash_pointer(original_type), array);
  303. }
  304. {
  305. Type *dst_bt = base_type(named_type);
  306. Type *src_bt = base_type(original_type);
  307. if ((dst_bt != nullptr && src_bt != nullptr) &&
  308. (dst_bt->kind == src_bt->kind)){
  309. if (dst_bt->kind == Type_Struct) {
  310. if (dst_bt->Struct.atom_op_table == nullptr) {
  311. dst_bt->Struct.atom_op_table = src_bt->Struct.atom_op_table;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  318. GB_ASSERT(is_type_struct(struct_type));
  319. ast_node(st, StructType, node);
  320. String context = str_lit("struct");
  321. isize min_field_count = 0;
  322. for_array(field_index, st->fields) {
  323. Ast *field = st->fields[field_index];
  324. switch (field->kind) {
  325. case_ast_node(f, ValueDecl, field);
  326. min_field_count += f->names.count;
  327. case_end;
  328. case_ast_node(f, Field, field);
  329. min_field_count += f->names.count;
  330. case_end;
  331. }
  332. }
  333. struct_type->Struct.names = make_names_field_for_struct(ctx, ctx->scope);
  334. scope_reserve(ctx->scope, min_field_count);
  335. if (st->is_raw_union && min_field_count > 1) {
  336. struct_type->Struct.is_raw_union = true;
  337. context = str_lit("struct #raw_union");
  338. }
  339. // NOTE(bill): Yes I know it's a non-const reference, what you gonna do?
  340. bool &is_polymorphic = struct_type->Struct.is_polymorphic;
  341. Type *polymorphic_params = nullptr;
  342. bool can_check_fields = true;
  343. bool is_poly_specialized = false;
  344. if (st->polymorphic_params != nullptr) {
  345. ast_node(field_list, FieldList, st->polymorphic_params);
  346. Slice<Ast *> params = field_list->list;
  347. if (params.count != 0) {
  348. isize variable_count = 0;
  349. for_array(i, params) {
  350. Ast *field = params[i];
  351. if (ast_node_expect(field, Ast_Field)) {
  352. ast_node(f, Field, field);
  353. variable_count += gb_max(f->names.count, 1);
  354. }
  355. }
  356. auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  357. for_array(i, params) {
  358. Ast *param = params[i];
  359. if (param->kind != Ast_Field) {
  360. continue;
  361. }
  362. ast_node(p, Field, param);
  363. Ast *type_expr = p->type;
  364. Ast *default_value = unparen_expr(p->default_value);
  365. Type *type = nullptr;
  366. bool is_type_param = false;
  367. bool is_type_polymorphic_type = false;
  368. if (type_expr == nullptr && default_value == nullptr) {
  369. error(param, "Expected a type for this parameter");
  370. continue;
  371. }
  372. if (type_expr != nullptr) {
  373. if (type_expr->kind == Ast_Ellipsis) {
  374. type_expr = type_expr->Ellipsis.expr;
  375. error(param, "A polymorphic parameter cannot be variadic");
  376. }
  377. if (type_expr->kind == Ast_TypeidType) {
  378. is_type_param = true;
  379. Type *specialization = nullptr;
  380. if (type_expr->TypeidType.specialization != nullptr) {
  381. Ast *s = type_expr->TypeidType.specialization;
  382. specialization = check_type(ctx, s);
  383. }
  384. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  385. } else {
  386. type = check_type(ctx, type_expr);
  387. if (is_type_polymorphic(type)) {
  388. is_type_polymorphic_type = true;
  389. }
  390. }
  391. }
  392. ParameterValue param_value = {};
  393. if (default_value != nullptr) {
  394. Type *out_type = nullptr;
  395. param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
  396. if (type == nullptr && out_type != nullptr) {
  397. type = out_type;
  398. }
  399. if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
  400. error(default_value, "Invalid parameter value");
  401. param_value = {};
  402. }
  403. }
  404. if (type == nullptr) {
  405. error(params[i], "Invalid parameter type");
  406. type = t_invalid;
  407. }
  408. if (is_type_untyped(type)) {
  409. if (is_type_untyped_undef(type)) {
  410. error(params[i], "Cannot determine parameter type from ---");
  411. } else {
  412. error(params[i], "Cannot determine parameter type from a nil");
  413. }
  414. type = t_invalid;
  415. }
  416. if (is_type_polymorphic_type) {
  417. gbString str = type_to_string(type);
  418. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  419. gb_string_free(str);
  420. type = t_invalid;
  421. }
  422. if (!is_type_param && !is_type_constant_type(type)) {
  423. gbString str = type_to_string(type);
  424. error(params[i], "A parameter must be a valid constant type, got %s", str);
  425. gb_string_free(str);
  426. }
  427. Scope *scope = ctx->scope;
  428. for_array(j, p->names) {
  429. Ast *name = p->names[j];
  430. if (!ast_node_expect(name, Ast_Ident)) {
  431. continue;
  432. }
  433. Entity *e = nullptr;
  434. Token token = name->Ident.token;
  435. if (poly_operands != nullptr) {
  436. Operand operand = {};
  437. operand.type = t_invalid;
  438. if (entities.count < poly_operands->count) {
  439. operand = (*poly_operands)[entities.count];
  440. } else if (param_value.kind != ParameterValue_Invalid) {
  441. operand.mode = Addressing_Constant;
  442. operand.value = param_value.value;
  443. }
  444. if (is_type_param) {
  445. if (is_type_polymorphic(base_type(operand.type))) {
  446. is_polymorphic = true;
  447. can_check_fields = false;
  448. }
  449. e = alloc_entity_type_name(scope, token, operand.type);
  450. e->TypeName.is_type_alias = true;
  451. } else {
  452. if (is_type_polymorphic(base_type(operand.type))) {
  453. is_polymorphic = true;
  454. can_check_fields = false;
  455. }
  456. if (e == nullptr) {
  457. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  458. e->Constant.param_value = param_value;
  459. }
  460. }
  461. } else {
  462. if (is_type_param) {
  463. e = alloc_entity_type_name(scope, token, type);
  464. e->TypeName.is_type_alias = true;
  465. } else {
  466. e = alloc_entity_constant(scope, token, type, param_value.value);
  467. e->Constant.param_value = param_value;
  468. }
  469. }
  470. e->state = EntityState_Resolved;
  471. add_entity(ctx->checker, scope, name, e);
  472. array_add(&entities, e);
  473. }
  474. }
  475. if (entities.count > 0) {
  476. Type *tuple = alloc_type_tuple();
  477. tuple->Tuple.variables = entities;
  478. polymorphic_params = tuple;
  479. }
  480. }
  481. if (original_type_for_poly != nullptr) {
  482. GB_ASSERT(named_type != nullptr);
  483. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  484. }
  485. }
  486. if (!is_polymorphic) {
  487. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  488. }
  489. if (poly_operands != nullptr) {
  490. is_poly_specialized = true;
  491. for (isize i = 0; i < poly_operands->count; i++) {
  492. Operand o = (*poly_operands)[i];
  493. if (is_type_polymorphic(o.type)) {
  494. is_poly_specialized = false;
  495. break;
  496. }
  497. if (struct_type == o.type) {
  498. // NOTE(bill): Cycle
  499. is_poly_specialized = false;
  500. break;
  501. }
  502. }
  503. }
  504. struct_type->Struct.scope = ctx->scope;
  505. struct_type->Struct.is_packed = st->is_packed;
  506. struct_type->Struct.polymorphic_params = polymorphic_params;
  507. struct_type->Struct.is_poly_specialized = is_poly_specialized;
  508. if (!is_polymorphic) {
  509. if (st->where_clauses.count > 0 && st->polymorphic_params == nullptr) {
  510. error(st->where_clauses[0], "'where' clauses can only be used on structures with polymorphic parameters");
  511. } else {
  512. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &st->where_clauses, true);
  513. }
  514. check_struct_fields(ctx, node, &struct_type->Struct.fields, &struct_type->Struct.tags, st->fields, min_field_count, struct_type, context);
  515. }
  516. if (st->align != nullptr) {
  517. if (st->is_packed) {
  518. syntax_error(st->align, "'#align' cannot be applied with '#packed'");
  519. return;
  520. }
  521. i64 custom_align = 1;
  522. if (check_custom_align(ctx, st->align, &custom_align)) {
  523. struct_type->Struct.custom_align = custom_align;
  524. }
  525. }
  526. }
  527. void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  528. GB_ASSERT(is_type_union(union_type));
  529. ast_node(ut, UnionType, node);
  530. isize variant_count = ut->variants.count;
  531. Entity *using_index_expr = nullptr;
  532. auto variants = array_make<Type *>(permanent_allocator(), 0, variant_count);
  533. union_type->Union.scope = ctx->scope;
  534. Type *polymorphic_params = nullptr;
  535. bool is_polymorphic = false;
  536. bool can_check_fields = true;
  537. bool is_poly_specialized = false;
  538. if (ut->polymorphic_params != nullptr) {
  539. ast_node(field_list, FieldList, ut->polymorphic_params);
  540. Slice<Ast *> params = field_list->list;
  541. if (params.count != 0) {
  542. isize variable_count = 0;
  543. for_array(i, params) {
  544. Ast *field = params[i];
  545. if (ast_node_expect(field, Ast_Field)) {
  546. ast_node(f, Field, field);
  547. variable_count += gb_max(f->names.count, 1);
  548. }
  549. }
  550. auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  551. for_array(i, params) {
  552. Ast *param = params[i];
  553. if (param->kind != Ast_Field) {
  554. continue;
  555. }
  556. ast_node(p, Field, param);
  557. Ast *type_expr = p->type;
  558. Ast *default_value = unparen_expr(p->default_value);
  559. Type *type = nullptr;
  560. bool is_type_param = false;
  561. bool is_type_polymorphic_type = false;
  562. if (type_expr == nullptr && default_value == nullptr) {
  563. error(param, "Expected a type for this parameter");
  564. continue;
  565. }
  566. if (type_expr != nullptr) {
  567. if (type_expr->kind == Ast_Ellipsis) {
  568. type_expr = type_expr->Ellipsis.expr;
  569. error(param, "A polymorphic parameter cannot be variadic");
  570. }
  571. if (type_expr->kind == Ast_TypeidType) {
  572. is_type_param = true;
  573. Type *specialization = nullptr;
  574. if (type_expr->TypeidType.specialization != nullptr) {
  575. Ast *s = type_expr->TypeidType.specialization;
  576. specialization = check_type(ctx, s);
  577. }
  578. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  579. } else {
  580. type = check_type(ctx, type_expr);
  581. if (is_type_polymorphic(type)) {
  582. is_type_polymorphic_type = true;
  583. }
  584. }
  585. }
  586. ParameterValue param_value = {};
  587. if (default_value != nullptr) {
  588. Type *out_type = nullptr;
  589. param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
  590. if (type == nullptr && out_type != nullptr) {
  591. type = out_type;
  592. }
  593. if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
  594. error(default_value, "Invalid parameter value");
  595. param_value = {};
  596. }
  597. }
  598. if (type == nullptr) {
  599. error(params[i], "Invalid parameter type");
  600. type = t_invalid;
  601. }
  602. if (is_type_untyped(type)) {
  603. if (is_type_untyped_undef(type)) {
  604. error(params[i], "Cannot determine parameter type from ---");
  605. } else {
  606. error(params[i], "Cannot determine parameter type from a nil");
  607. }
  608. type = t_invalid;
  609. }
  610. if (is_type_polymorphic_type) {
  611. gbString str = type_to_string(type);
  612. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  613. gb_string_free(str);
  614. type = t_invalid;
  615. }
  616. if (!is_type_param && !is_type_constant_type(type)) {
  617. gbString str = type_to_string(type);
  618. error(params[i], "A parameter must be a valid constant type, got %s", str);
  619. gb_string_free(str);
  620. }
  621. Scope *scope = ctx->scope;
  622. for_array(j, p->names) {
  623. Ast *name = p->names[j];
  624. if (!ast_node_expect(name, Ast_Ident)) {
  625. continue;
  626. }
  627. Entity *e = nullptr;
  628. Token token = name->Ident.token;
  629. if (poly_operands != nullptr) {
  630. Operand operand = {};
  631. operand.type = t_invalid;
  632. if (entities.count < poly_operands->count) {
  633. operand = (*poly_operands)[entities.count];
  634. } else if (param_value.kind != ParameterValue_Invalid) {
  635. operand.mode = Addressing_Constant;
  636. operand.value = param_value.value;
  637. }
  638. if (is_type_param) {
  639. GB_ASSERT(operand.mode == Addressing_Type ||
  640. operand.mode == Addressing_Invalid);
  641. if (is_type_polymorphic(base_type(operand.type))) {
  642. is_polymorphic = true;
  643. can_check_fields = false;
  644. }
  645. e = alloc_entity_type_name(scope, token, operand.type);
  646. e->TypeName.is_type_alias = true;
  647. } else {
  648. // GB_ASSERT(operand.mode == Addressing_Constant);
  649. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  650. e->Constant.param_value = param_value;
  651. }
  652. } else {
  653. if (is_type_param) {
  654. e = alloc_entity_type_name(scope, token, type);
  655. e->TypeName.is_type_alias = true;
  656. } else {
  657. e = alloc_entity_constant(scope, token, type, empty_exact_value);
  658. e->Constant.param_value = param_value;
  659. }
  660. }
  661. e->state = EntityState_Resolved;
  662. add_entity(ctx->checker, scope, name, e);
  663. array_add(&entities, e);
  664. }
  665. }
  666. if (entities.count > 0) {
  667. Type *tuple = alloc_type_tuple();
  668. tuple->Tuple.variables = entities;
  669. polymorphic_params = tuple;
  670. }
  671. }
  672. if (original_type_for_poly != nullptr) {
  673. GB_ASSERT(named_type != nullptr);
  674. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  675. }
  676. }
  677. if (!is_polymorphic) {
  678. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  679. }
  680. if (poly_operands != nullptr) {
  681. is_poly_specialized = true;
  682. for (isize i = 0; i < poly_operands->count; i++) {
  683. Operand o = (*poly_operands)[i];
  684. if (is_type_polymorphic(o.type)) {
  685. is_poly_specialized = false;
  686. break;
  687. }
  688. if (union_type == o.type) {
  689. // NOTE(bill): Cycle
  690. is_poly_specialized = false;
  691. break;
  692. }
  693. }
  694. }
  695. union_type->Union.scope = ctx->scope;
  696. union_type->Union.polymorphic_params = polymorphic_params;
  697. union_type->Union.is_polymorphic = is_polymorphic;
  698. union_type->Union.is_poly_specialized = is_poly_specialized;
  699. if (ut->where_clauses.count > 0 && ut->polymorphic_params == nullptr) {
  700. error(ut->where_clauses[0], "'where' clauses can only be used on unions with polymorphic parameters");
  701. } else {
  702. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &ut->where_clauses, true);
  703. }
  704. for_array(i, ut->variants) {
  705. Ast *node = ut->variants[i];
  706. Type *t = check_type_expr(ctx, node, nullptr);
  707. if (t != nullptr && t != t_invalid) {
  708. bool ok = true;
  709. t = default_type(t);
  710. if (is_type_untyped(t) || is_type_empty_union(t)) {
  711. ok = false;
  712. gbString str = type_to_string(t);
  713. error(node, "Invalid variant type in union '%s'", str);
  714. gb_string_free(str);
  715. } else {
  716. for_array(j, variants) {
  717. if (are_types_identical(t, variants[j])) {
  718. ok = false;
  719. gbString str = type_to_string(t);
  720. error(node, "Duplicate variant type '%s'", str);
  721. gb_string_free(str);
  722. break;
  723. }
  724. }
  725. }
  726. if (ok) {
  727. array_add(&variants, t);
  728. }
  729. }
  730. }
  731. union_type->Union.variants = variants;
  732. union_type->Union.no_nil = ut->no_nil;
  733. union_type->Union.maybe = ut->maybe;
  734. if (union_type->Union.no_nil) {
  735. if (variants.count < 2) {
  736. error(ut->align, "A union with #no_nil must have at least 2 variants");
  737. }
  738. }
  739. if (union_type->Union.maybe) {
  740. if (variants.count != 1) {
  741. error(ut->align, "A union with #maybe must have at 1 variant, got %lld", cast(long long)variants.count);
  742. }
  743. }
  744. if (ut->align != nullptr) {
  745. i64 custom_align = 1;
  746. if (check_custom_align(ctx, ut->align, &custom_align)) {
  747. if (variants.count == 0) {
  748. error(ut->align, "An empty union cannot have a custom alignment");
  749. } else {
  750. union_type->Union.custom_align = custom_align;
  751. }
  752. }
  753. }
  754. }
  755. void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast *node) {
  756. ast_node(et, EnumType, node);
  757. GB_ASSERT(is_type_enum(enum_type));
  758. Type *base_type = t_int;
  759. if (et->base_type != nullptr) {
  760. base_type = check_type(ctx, et->base_type);
  761. }
  762. if (base_type == nullptr || !is_type_integer(base_type)) {
  763. error(node, "Base type for enumeration must be an integer");
  764. return;
  765. }
  766. if (is_type_enum(base_type)) {
  767. error(node, "Base type for enumeration cannot be another enumeration");
  768. return;
  769. }
  770. if (is_type_integer_128bit(base_type)) {
  771. error(node, "Base type for enumeration cannot be a 128-bit integer");
  772. return;
  773. }
  774. // NOTE(bill): Must be up here for the 'check_init_constant' system
  775. enum_type->Enum.base_type = base_type;
  776. enum_type->Enum.scope = ctx->scope;
  777. auto fields = array_make<Entity *>(permanent_allocator(), 0, et->fields.count);
  778. Type *constant_type = enum_type;
  779. if (named_type != nullptr) {
  780. constant_type = named_type;
  781. }
  782. ExactValue iota = exact_value_i64(-1);
  783. ExactValue min_value = exact_value_i64(0);
  784. ExactValue max_value = exact_value_i64(0);
  785. isize min_value_index = 0;
  786. isize max_value_index = 0;
  787. bool min_value_set = false;
  788. bool max_value_set = false;
  789. scope_reserve(ctx->scope, et->fields.count);
  790. for_array(i, et->fields) {
  791. Ast *field = et->fields[i];
  792. Ast *ident = nullptr;
  793. Ast *init = nullptr;
  794. if (field->kind == Ast_FieldValue) {
  795. ast_node(fv, FieldValue, field);
  796. if (fv->field == nullptr || fv->field->kind != Ast_Ident) {
  797. error(field, "An enum field's name must be an identifier");
  798. continue;
  799. }
  800. ident = fv->field;
  801. init = fv->value;
  802. } else if (field->kind == Ast_Ident) {
  803. ident = field;
  804. } else {
  805. error(field, "An enum field's name must be an identifier");
  806. continue;
  807. }
  808. String name = ident->Ident.token.string;
  809. if (init != nullptr) {
  810. Operand o = {};
  811. check_expr(ctx, &o, init);
  812. if (o.mode != Addressing_Constant) {
  813. error(init, "Enumeration value must be a constant");
  814. o.mode = Addressing_Invalid;
  815. }
  816. if (o.mode != Addressing_Invalid) {
  817. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  818. }
  819. if (o.mode != Addressing_Invalid) {
  820. iota = o.value;
  821. } else {
  822. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  823. }
  824. } else {
  825. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  826. }
  827. // NOTE(bill): Skip blank identifiers
  828. if (is_blank_ident(name)) {
  829. continue;
  830. } else if (name == "names") {
  831. error(field, "'names' is a reserved identifier for enumerations");
  832. continue;
  833. }
  834. if (min_value_set) {
  835. if (compare_exact_values(Token_Gt, min_value, iota)) {
  836. min_value_index = i;
  837. min_value = iota;
  838. }
  839. } else {
  840. min_value_index = i;
  841. min_value = iota;
  842. min_value_set = true;
  843. }
  844. if (max_value_set) {
  845. if (compare_exact_values(Token_Lt, max_value, iota)) {
  846. max_value_index = i;
  847. max_value = iota;
  848. }
  849. } else {
  850. max_value_index = i;
  851. max_value = iota;
  852. max_value_set = true;
  853. }
  854. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  855. e->identifier = ident;
  856. e->flags |= EntityFlag_Visited;
  857. e->state = EntityState_Resolved;
  858. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  859. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  860. } else {
  861. add_entity(ctx->checker, ctx->scope, nullptr, e);
  862. array_add(&fields, e);
  863. // TODO(bill): Should I add a use for the enum value?
  864. add_entity_use(ctx, field, e);
  865. }
  866. }
  867. GB_ASSERT(fields.count <= et->fields.count);
  868. enum_type->Enum.fields = fields;
  869. enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope);
  870. enum_type->Enum.min_value = min_value;
  871. enum_type->Enum.max_value = max_value;
  872. enum_type->Enum.min_value_index = min_value_index;
  873. enum_type->Enum.max_value_index = max_value_index;
  874. }
  875. void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Ast *node) {
  876. ast_node(bft, BitFieldType, node);
  877. GB_ASSERT(is_type_bit_field(bit_field_type));
  878. auto fields = array_make<Entity*>(permanent_allocator(), 0, bft->fields.count);
  879. auto sizes = array_make<u32> (permanent_allocator(), 0, bft->fields.count);
  880. auto offsets = array_make<u32> (permanent_allocator(), 0, bft->fields.count);
  881. scope_reserve(ctx->scope, bft->fields.count);
  882. u32 curr_offset = 0;
  883. for_array(i, bft->fields) {
  884. Ast *field = bft->fields[i];
  885. GB_ASSERT(field->kind == Ast_FieldValue);
  886. Ast *ident = field->FieldValue.field;
  887. Ast *value = field->FieldValue.value;
  888. if (ident->kind != Ast_Ident) {
  889. error(field, "A bit field value's name must be an identifier");
  890. continue;
  891. }
  892. String name = ident->Ident.token.string;
  893. Operand o = {};
  894. check_expr(ctx, &o, value);
  895. if (o.mode != Addressing_Constant) {
  896. error(value, "Bit field bit size must be a constant");
  897. continue;
  898. }
  899. ExactValue v = exact_value_to_integer(o.value);
  900. if (v.kind != ExactValue_Integer) {
  901. error(value, "Bit field bit size must be a constant integer");
  902. continue;
  903. }
  904. i64 bits_ = big_int_to_i64(&v.value_integer); // TODO(bill): what if the integer is huge?
  905. if (bits_ < 0 || bits_ > 64) {
  906. error(value, "Bit field's bit size must be within the range 1...64, got %lld", cast(long long)bits_);
  907. continue;
  908. }
  909. u32 bits = cast(u32)bits_;
  910. Type *value_type = alloc_type_bit_field_value(bits);
  911. Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type);
  912. e->identifier = ident;
  913. e->flags |= EntityFlag_BitFieldValue;
  914. if (!is_blank_ident(name) &&
  915. scope_lookup_current(ctx->scope, name) != nullptr) {
  916. error(ident, "'%.*s' is already declared in this bit field", LIT(name));
  917. } else {
  918. add_entity(ctx->checker, ctx->scope, nullptr, e);
  919. // TODO(bill): Should this entity be "used"?
  920. add_entity_use(ctx, field, e);
  921. array_add(&fields, e);
  922. array_add(&offsets, curr_offset);
  923. array_add(&sizes, bits);
  924. curr_offset += bits;
  925. }
  926. }
  927. GB_ASSERT(fields.count <= bft->fields.count);
  928. bit_field_type->BitField.fields = fields;
  929. bit_field_type->BitField.sizes = sizes;
  930. bit_field_type->BitField.offsets = offsets;
  931. if (bft->align != nullptr) {
  932. i64 custom_align = 1;
  933. if (check_custom_align(ctx, bft->align, &custom_align)) {
  934. bit_field_type->BitField.custom_align = custom_align;
  935. }
  936. }
  937. }
  938. bool is_type_valid_bit_set_range(Type *t) {
  939. if (is_type_integer(t)) {
  940. return true;
  941. }
  942. if (is_type_rune(t)) {
  943. return true;
  944. }
  945. return false;
  946. }
  947. void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  948. ast_node(bs, BitSetType, node);
  949. GB_ASSERT(type->kind == Type_BitSet);
  950. i64 const DEFAULT_BITS = cast(i64)(8*build_context.word_size);
  951. i64 const MAX_BITS = 128;
  952. Ast *base = unparen_expr(bs->elem);
  953. if (is_ast_range(base)) {
  954. ast_node(be, BinaryExpr, base);
  955. Operand lhs = {};
  956. Operand rhs = {};
  957. check_expr(c, &lhs, be->left);
  958. check_expr(c, &rhs, be->right);
  959. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  960. return;
  961. }
  962. convert_to_typed(c, &lhs, rhs.type);
  963. if (lhs.mode == Addressing_Invalid) {
  964. return;
  965. }
  966. convert_to_typed(c, &rhs, lhs.type);
  967. if (rhs.mode == Addressing_Invalid) {
  968. return;
  969. }
  970. if (!are_types_identical(lhs.type, rhs.type)) {
  971. if (lhs.type != t_invalid &&
  972. rhs.type != t_invalid) {
  973. gbString xt = type_to_string(lhs.type);
  974. gbString yt = type_to_string(rhs.type);
  975. gbString expr_str = expr_to_string(bs->elem);
  976. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  977. gb_string_free(expr_str);
  978. gb_string_free(yt);
  979. gb_string_free(xt);
  980. }
  981. return;
  982. }
  983. if (!is_type_valid_bit_set_range(lhs.type)) {
  984. gbString str = type_to_string(lhs.type);
  985. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  986. gb_string_free(str);
  987. return;
  988. }
  989. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  990. error(bs->elem, "Intervals must be constant values");
  991. return;
  992. }
  993. ExactValue iv = exact_value_to_integer(lhs.value);
  994. ExactValue jv = exact_value_to_integer(rhs.value);
  995. GB_ASSERT(iv.kind == ExactValue_Integer);
  996. GB_ASSERT(jv.kind == ExactValue_Integer);
  997. BigInt i = iv.value_integer;
  998. BigInt j = jv.value_integer;
  999. if (big_int_cmp(&i, &j) > 0) {
  1000. gbAllocator a = heap_allocator();
  1001. String si = big_int_to_string(a, &i);
  1002. String sj = big_int_to_string(a, &j);
  1003. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  1004. gb_free(a, si.text);
  1005. gb_free(a, sj.text);
  1006. return;
  1007. }
  1008. Type *t = default_type(lhs.type);
  1009. if (bs->underlying != nullptr) {
  1010. Type *u = check_type(c, bs->underlying);
  1011. if (!is_type_integer(u)) {
  1012. gbString ts = type_to_string(u);
  1013. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1014. gb_string_free(ts);
  1015. return;
  1016. }
  1017. type->BitSet.underlying = u;
  1018. }
  1019. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1020. gbAllocator a = heap_allocator();
  1021. String s = big_int_to_string(a, &i);
  1022. gbString ts = type_to_string(t);
  1023. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1024. gb_string_free(ts);
  1025. gb_free(a, s.text);
  1026. return;
  1027. }
  1028. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  1029. gbAllocator a = heap_allocator();
  1030. String s = big_int_to_string(a, &j);
  1031. gbString ts = type_to_string(t);
  1032. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  1033. gb_string_free(ts);
  1034. gb_free(a, s.text);
  1035. return;
  1036. }
  1037. i64 lower = big_int_to_i64(&i);
  1038. i64 upper = big_int_to_i64(&j);
  1039. i64 bits = MAX_BITS;
  1040. if (type->BitSet.underlying != nullptr) {
  1041. bits = 8*type_size_of(type->BitSet.underlying);
  1042. }
  1043. switch (be->op.kind) {
  1044. case Token_Ellipsis:
  1045. if (upper - lower >= bits) {
  1046. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, (upper-lower+1));
  1047. }
  1048. break;
  1049. case Token_RangeHalf:
  1050. if (upper - lower > bits) {
  1051. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, (upper-lower));
  1052. }
  1053. upper -= 1;
  1054. break;
  1055. }
  1056. type->BitSet.elem = t;
  1057. type->BitSet.lower = lower;
  1058. type->BitSet.upper = upper;
  1059. } else {
  1060. Type *elem = check_type_expr(c, bs->elem, nullptr);
  1061. #if 0
  1062. if (named_type != nullptr && named_type->kind == Type_Named &&
  1063. elem->kind == Type_Enum) {
  1064. // NOTE(bill): Anonymous enumeration
  1065. String prefix = named_type->Named.name;
  1066. String enum_name = concatenate_strings(heap_allocator(), prefix, str_lit(".enum"));
  1067. Token token = make_token_ident(enum_name);
  1068. Entity *e = alloc_entity_type_name(nullptr, token, nullptr, EntityState_Resolved);
  1069. Type *named = alloc_type_named(enum_name, elem, e);
  1070. e->type = named;
  1071. e->TypeName.is_type_alias = true;
  1072. elem = named;
  1073. }
  1074. #endif
  1075. type->BitSet.elem = elem;
  1076. if (!is_type_valid_bit_set_elem(elem)) {
  1077. error(bs->elem, "Expected an enum type for a bit_set");
  1078. } else {
  1079. Type *et = base_type(elem);
  1080. if (et->kind == Type_Enum) {
  1081. if (!is_type_integer(et->Enum.base_type)) {
  1082. error(bs->elem, "Enum type for bit_set must be an integer");
  1083. return;
  1084. }
  1085. i64 lower = 0;
  1086. i64 upper = 0;
  1087. for_array(i, et->Enum.fields) {
  1088. Entity *e = et->Enum.fields[i];
  1089. if (e->kind != Entity_Constant) {
  1090. continue;
  1091. }
  1092. ExactValue value = exact_value_to_integer(e->Constant.value);
  1093. GB_ASSERT(value.kind == ExactValue_Integer);
  1094. // NOTE(bill): enum types should be able to store i64 values
  1095. i64 x = big_int_to_i64(&value.value_integer);
  1096. lower = gb_min(lower, x);
  1097. upper = gb_max(upper, x);
  1098. }
  1099. GB_ASSERT(lower <= upper);
  1100. i64 bits = MAX_BITS;
  1101. if (bs->underlying != nullptr) {
  1102. Type *u = check_type(c, bs->underlying);
  1103. if (!is_type_integer(u)) {
  1104. gbString ts = type_to_string(u);
  1105. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  1106. gb_string_free(ts);
  1107. return;
  1108. }
  1109. type->BitSet.underlying = u;
  1110. bits = 8*type_size_of(u);
  1111. }
  1112. if (upper - lower >= MAX_BITS) {
  1113. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", MAX_BITS, (upper-lower+1));
  1114. }
  1115. type->BitSet.lower = lower;
  1116. type->BitSet.upper = upper;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  1122. if (type == nullptr ||
  1123. type == t_invalid) {
  1124. return true;
  1125. }
  1126. Type *t = base_type(type);
  1127. Type *s = base_type(specialization);
  1128. if (t->kind != s->kind) {
  1129. return false;
  1130. }
  1131. if (is_type_untyped(t)) {
  1132. Operand o = {Addressing_Value};
  1133. o.type = default_type(type);
  1134. bool can_convert = check_cast_internal(ctx, &o, specialization);
  1135. return can_convert;
  1136. } else if (t->kind == Type_Struct) {
  1137. if (t->Struct.polymorphic_parent == specialization) {
  1138. return true;
  1139. }
  1140. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  1141. s->Struct.polymorphic_params != nullptr &&
  1142. t->Struct.polymorphic_params != nullptr) {
  1143. TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
  1144. TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
  1145. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1146. for_array(i, s_tuple->variables) {
  1147. Entity *s_e = s_tuple->variables[i];
  1148. Entity *t_e = t_tuple->variables[i];
  1149. Type *st = s_e->type;
  1150. Type *tt = t_e->type;
  1151. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1152. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1153. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1154. GB_ASSERT(e != nullptr);
  1155. if (modify_type) {
  1156. e->kind = Entity_Constant;
  1157. e->Constant.value = t_e->Constant.value;
  1158. e->type = t_e->type;
  1159. }
  1160. } else {
  1161. if (st->kind == Type_Basic && tt->kind == Type_Basic &&
  1162. s_e->kind == Entity_Constant && t_e->kind == Entity_Constant) {
  1163. if (!compare_exact_values(Token_CmpEq, s_e->Constant.value, t_e->Constant.value))
  1164. return false;
  1165. } else {
  1166. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1167. }
  1168. }
  1169. }
  1170. if (modify_type) {
  1171. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1172. gb_memmove(specialization, type, gb_size_of(Type));
  1173. }
  1174. return true;
  1175. }
  1176. } else if (t->kind == Type_Union) {
  1177. if (t->Union.polymorphic_parent == specialization) {
  1178. return true;
  1179. }
  1180. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1181. s->Union.polymorphic_params != nullptr &&
  1182. t->Union.polymorphic_params != nullptr) {
  1183. TypeTuple *s_tuple = &s->Union.polymorphic_params->Tuple;
  1184. TypeTuple *t_tuple = &t->Union.polymorphic_params->Tuple;
  1185. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1186. for_array(i, s_tuple->variables) {
  1187. Entity *s_e = s_tuple->variables[i];
  1188. Entity *t_e = t_tuple->variables[i];
  1189. Type *st = s_e->type;
  1190. Type *tt = t_e->type;
  1191. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1192. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1193. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1194. GB_ASSERT(e != nullptr);
  1195. if (modify_type) {
  1196. e->kind = Entity_Constant;
  1197. e->Constant.value = t_e->Constant.value;
  1198. e->type = t_e->type;
  1199. }
  1200. } else {
  1201. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1202. }
  1203. }
  1204. if (modify_type) {
  1205. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1206. gb_memmove(specialization, type, gb_size_of(Type));
  1207. }
  1208. return true;
  1209. }
  1210. }
  1211. if (specialization->kind == Type_Named &&
  1212. type->kind != Type_Named) {
  1213. return false;
  1214. }
  1215. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1216. return true;
  1217. }
  1218. return false;
  1219. }
  1220. Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand operand) {
  1221. bool modify_type = !ctx->no_polymorphic_errors;
  1222. bool show_error = modify_type && !ctx->hide_polymorphic_errors;
  1223. if (!is_operand_value(operand)) {
  1224. if (show_error) {
  1225. error(operand.expr, "Cannot determine polymorphic type from parameter");
  1226. }
  1227. return t_invalid;
  1228. }
  1229. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1230. if (show_error) {
  1231. set_procedure_abi_types(poly_type);
  1232. }
  1233. return poly_type;
  1234. }
  1235. if (show_error) {
  1236. gbString pts = type_to_string(poly_type);
  1237. gbString ots = type_to_string(operand.type);
  1238. defer (gb_string_free(pts));
  1239. defer (gb_string_free(ots));
  1240. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1241. }
  1242. return t_invalid;
  1243. }
  1244. bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1245. if (expr == nullptr) {
  1246. return false;
  1247. }
  1248. expr = unparen_expr(expr);
  1249. if (expr->kind == Ast_SelectorExpr) {
  1250. Ast *lhs = expr->SelectorExpr.expr;
  1251. return is_expr_from_a_parameter(ctx, lhs);
  1252. } else if (expr->kind == Ast_Ident) {
  1253. Operand x= {};
  1254. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1255. if (e->flags & EntityFlag_Param) {
  1256. return true;
  1257. }
  1258. }
  1259. return false;
  1260. }
  1261. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1262. ParameterValue param_value = {};
  1263. param_value.original_ast_expr = expr;
  1264. if (expr == nullptr) {
  1265. return param_value;
  1266. }
  1267. Operand o = {};
  1268. if (allow_caller_location &&
  1269. expr->kind == Ast_BasicDirective &&
  1270. expr->BasicDirective.name == "caller_location") {
  1271. init_core_source_code_location(ctx->checker);
  1272. param_value.kind = ParameterValue_Location;
  1273. o.type = t_source_code_location;
  1274. } else {
  1275. if (in_type) {
  1276. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1277. } else {
  1278. check_expr(ctx, &o, expr);
  1279. }
  1280. if (is_operand_nil(o)) {
  1281. param_value.kind = ParameterValue_Nil;
  1282. } else if (o.mode != Addressing_Constant) {
  1283. if (expr->kind == Ast_ProcLit) {
  1284. param_value.kind = ParameterValue_Constant;
  1285. param_value.value = exact_value_procedure(expr);
  1286. } else {
  1287. Entity *e = nullptr;
  1288. // if (o.mode == Addressing_Value && is_type_proc(o.type)) {
  1289. if (o.mode == Addressing_Value || o.mode == Addressing_Variable) {
  1290. Operand x = {};
  1291. if (expr->kind == Ast_Ident) {
  1292. e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1293. } else if (expr->kind == Ast_SelectorExpr) {
  1294. e = check_selector(ctx, &x, expr, nullptr);
  1295. }
  1296. }
  1297. if (e != nullptr) {
  1298. if (e->kind == Entity_Procedure) {
  1299. param_value.kind = ParameterValue_Constant;
  1300. param_value.value = exact_value_procedure(e->identifier);
  1301. add_entity_use(ctx, e->identifier, e);
  1302. } else {
  1303. if (e->flags & EntityFlag_Param) {
  1304. error(expr, "Default parameter cannot be another parameter");
  1305. } else {
  1306. if (is_expr_from_a_parameter(ctx, expr)) {
  1307. error(expr, "Default parameter cannot be another parameter");
  1308. } else {
  1309. param_value.kind = ParameterValue_Value;
  1310. param_value.ast_value = expr;
  1311. add_entity_use(ctx, e->identifier, e);
  1312. }
  1313. }
  1314. }
  1315. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1316. param_value.kind = ParameterValue_Value;
  1317. param_value.ast_value = expr;
  1318. } else {
  1319. error(expr, "Default parameter must be a constant");
  1320. }
  1321. }
  1322. } else {
  1323. if (o.value.kind != ExactValue_Invalid) {
  1324. param_value.kind = ParameterValue_Constant;
  1325. param_value.value = o.value;
  1326. } else {
  1327. error(o.expr, "Invalid constant parameter");
  1328. }
  1329. }
  1330. }
  1331. if (in_type) {
  1332. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1333. }
  1334. if (out_type_) *out_type_ = default_type(o.type);
  1335. return param_value;
  1336. }
  1337. Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> *operands) {
  1338. if (_params == nullptr) {
  1339. return nullptr;
  1340. }
  1341. bool allow_polymorphic_types = ctx->allow_polymorphic_types;
  1342. bool success = true;
  1343. ast_node(field_list, FieldList, _params);
  1344. Slice<Ast *> params = field_list->list;
  1345. if (params.count == 0) {
  1346. if (success_) *success_ = success;
  1347. return nullptr;
  1348. }
  1349. isize variable_count = 0;
  1350. for_array(i, params) {
  1351. Ast *field = params[i];
  1352. if (ast_node_expect(field, Ast_Field)) {
  1353. ast_node(f, Field, field);
  1354. variable_count += gb_max(f->names.count, 1);
  1355. }
  1356. }
  1357. isize min_variable_count = variable_count;
  1358. for (isize i = params.count-1; i >= 0; i--) {
  1359. Ast *field = params[i];
  1360. if (field->kind == Ast_Field) {
  1361. ast_node(f, Field, field);
  1362. if (f->default_value == nullptr) {
  1363. break;
  1364. }
  1365. min_variable_count--;
  1366. }
  1367. }
  1368. bool is_variadic = false;
  1369. isize variadic_index = -1;
  1370. bool is_c_vararg = false;
  1371. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1372. for_array(i, params) {
  1373. Ast *param = params[i];
  1374. if (param->kind != Ast_Field) {
  1375. continue;
  1376. }
  1377. ast_node(p, Field, param);
  1378. Ast *type_expr = unparen_expr(p->type);
  1379. Type *type = nullptr;
  1380. Ast *default_value = unparen_expr(p->default_value);
  1381. ParameterValue param_value = {};
  1382. bool is_type_param = false;
  1383. bool is_type_polymorphic_type = false;
  1384. bool detemine_type_from_operand = false;
  1385. Type *specialization = nullptr;
  1386. bool is_using = (p->flags&FieldFlag_using) != 0;
  1387. if (type_expr == nullptr) {
  1388. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1389. } else {
  1390. if (type_expr->kind == Ast_Ellipsis) {
  1391. type_expr = type_expr->Ellipsis.expr;
  1392. is_variadic = true;
  1393. variadic_index = variables.count;
  1394. if (p->names.count != 1) {
  1395. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1396. success = false;
  1397. }
  1398. }
  1399. if (type_expr->kind == Ast_TypeidType) {
  1400. ast_node(tt, TypeidType, type_expr);
  1401. if (tt->specialization) {
  1402. specialization = check_type(ctx, tt->specialization);
  1403. if (specialization == t_invalid){
  1404. specialization = nullptr;
  1405. }
  1406. if (operands != nullptr) {
  1407. detemine_type_from_operand = true;
  1408. type = t_invalid;
  1409. } else {
  1410. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1411. }
  1412. } else {
  1413. type = t_typeid;
  1414. }
  1415. } else {
  1416. bool prev = ctx->allow_polymorphic_types;
  1417. if (operands != nullptr) {
  1418. ctx->allow_polymorphic_types = true;
  1419. }
  1420. type = check_type(ctx, type_expr);
  1421. ctx->allow_polymorphic_types = prev;
  1422. if (is_type_polymorphic(type)) {
  1423. is_type_polymorphic_type = true;
  1424. }
  1425. }
  1426. if (default_value != nullptr) {
  1427. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1428. error(type_expr, "A type parameter may not have a default value");
  1429. } else {
  1430. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1431. }
  1432. }
  1433. }
  1434. if (type == nullptr) {
  1435. error(param, "Invalid parameter type");
  1436. type = t_invalid;
  1437. }
  1438. if (is_type_untyped(type)) {
  1439. if (is_type_untyped_undef(type)) {
  1440. error(param, "Cannot determine parameter type from ---");
  1441. } else {
  1442. error(param, "Cannot determine parameter type from a nil");
  1443. }
  1444. type = t_invalid;
  1445. }
  1446. if (is_type_empty_union(type)) {
  1447. gbString str = type_to_string(type);
  1448. error(param, "Invalid use of an empty union '%s'", str);
  1449. gb_string_free(str);
  1450. type = t_invalid;
  1451. }
  1452. if (p->flags&FieldFlag_c_vararg) {
  1453. if (p->type == nullptr ||
  1454. p->type->kind != Ast_Ellipsis) {
  1455. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1456. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1457. } else {
  1458. is_c_vararg = true;
  1459. }
  1460. }
  1461. for_array(j, p->names) {
  1462. Ast *name = p->names[j];
  1463. bool is_poly_name = false;
  1464. switch (name->kind) {
  1465. case Ast_Ident:
  1466. break;
  1467. case Ast_PolyType:
  1468. GB_ASSERT(name->PolyType.specialization == nullptr);
  1469. is_poly_name = true;
  1470. name = name->PolyType.type;
  1471. break;
  1472. }
  1473. if (!ast_node_expect(name, Ast_Ident)) {
  1474. continue;
  1475. }
  1476. if (is_poly_name) {
  1477. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1478. is_type_param = true;
  1479. } else {
  1480. if (param_value.kind != ParameterValue_Invalid) {
  1481. error(default_value, "Constant parameters cannot have a default value");
  1482. param_value.kind = ParameterValue_Invalid;
  1483. }
  1484. }
  1485. }
  1486. Entity *param = nullptr;
  1487. if (is_type_param) {
  1488. if (operands != nullptr) {
  1489. Operand o = (*operands)[variables.count];
  1490. if (o.mode == Addressing_Type) {
  1491. type = o.type;
  1492. } else {
  1493. if (!ctx->no_polymorphic_errors) {
  1494. error(o.expr, "Expected a type to assign to the type parameter");
  1495. }
  1496. success = false;
  1497. type = t_invalid;
  1498. }
  1499. if (is_type_polymorphic(type)) {
  1500. gbString str = type_to_string(type);
  1501. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1502. gb_string_free(str);
  1503. success = false;
  1504. type = t_invalid;
  1505. }
  1506. if (is_type_untyped(default_type(type))) {
  1507. gbString str = type_to_string(type);
  1508. error(o.expr, "Cannot determine type from the parameter, got '%s'", str);
  1509. gb_string_free(str);
  1510. success = false;
  1511. type = t_invalid;
  1512. }
  1513. bool modify_type = !ctx->no_polymorphic_errors;
  1514. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1515. if (!ctx->no_polymorphic_errors) {
  1516. gbString t = type_to_string(type);
  1517. gbString s = type_to_string(specialization);
  1518. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1519. gb_string_free(s);
  1520. gb_string_free(t);
  1521. }
  1522. success = false;
  1523. type = t_invalid;
  1524. }
  1525. }
  1526. if (p->flags&FieldFlag_auto_cast) {
  1527. error(name, "'auto_cast' can only be applied to variable fields");
  1528. p->flags &= ~FieldFlag_auto_cast;
  1529. }
  1530. if (p->flags&FieldFlag_const) {
  1531. error(name, "'#const' can only be applied to variable fields");
  1532. p->flags &= ~FieldFlag_const;
  1533. }
  1534. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1535. param->TypeName.is_type_alias = true;
  1536. } else {
  1537. ExactValue poly_const = {};
  1538. if (operands != nullptr && variables.count < operands->count) {
  1539. Operand op = (*operands)[variables.count];
  1540. if (op.expr == nullptr) {
  1541. // NOTE(bill): 2019-03-30
  1542. // This is just to add the error message to determine_type_from_polymorphic which
  1543. // depends on valid position information
  1544. op.expr = _params;
  1545. }
  1546. if (is_type_polymorphic_type) {
  1547. type = determine_type_from_polymorphic(ctx, type, op);
  1548. if (type == t_invalid) {
  1549. success = false;
  1550. } else if (!ctx->no_polymorphic_errors) {
  1551. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1552. is_type_polymorphic_type = false;
  1553. }
  1554. }
  1555. if (is_poly_name) {
  1556. bool valid = false;
  1557. if (is_type_proc(op.type)) {
  1558. Entity *proc_entity = entity_from_expr(op.expr);
  1559. valid = proc_entity != nullptr;
  1560. poly_const = exact_value_procedure(proc_entity->identifier ? proc_entity->identifier : op.expr);
  1561. }
  1562. if (!valid) {
  1563. if (op.mode == Addressing_Constant) {
  1564. poly_const = op.value;
  1565. } else {
  1566. error(op.expr, "Expected a constant value for this polymorphic name parameter");
  1567. success = false;
  1568. }
  1569. }
  1570. }
  1571. if (is_type_untyped(default_type(type))) {
  1572. gbString str = type_to_string(type);
  1573. error(op.expr, "Cannot determine type from the parameter, got '%s'", str);
  1574. gb_string_free(str);
  1575. success = false;
  1576. type = t_invalid;
  1577. }
  1578. }
  1579. if (p->flags&FieldFlag_no_alias) {
  1580. if (!is_type_pointer(type)) {
  1581. error(name, "'#no_alias' can only be applied to fields of pointer type");
  1582. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1583. }
  1584. }
  1585. if (is_poly_name) {
  1586. if (p->flags&FieldFlag_no_alias) {
  1587. error(name, "'#no_alias' can only be applied to non constant values");
  1588. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1589. }
  1590. if (p->flags&FieldFlag_auto_cast) {
  1591. error(name, "'auto_cast' can only be applied to variable fields");
  1592. p->flags &= ~FieldFlag_auto_cast;
  1593. }
  1594. if (p->flags&FieldFlag_const) {
  1595. error(name, "'#const' can only be applied to variable fields");
  1596. p->flags &= ~FieldFlag_const;
  1597. }
  1598. if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
  1599. gbString str = type_to_string(type);
  1600. error(params[i], "A parameter must be a valid constant type, got %s", str);
  1601. gb_string_free(str);
  1602. }
  1603. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1604. } else {
  1605. param = alloc_entity_param(scope, name->Ident.token, type, is_using, true);
  1606. param->Variable.param_value = param_value;
  1607. }
  1608. }
  1609. if (p->flags&FieldFlag_no_alias) {
  1610. param->flags |= EntityFlag_NoAlias;
  1611. }
  1612. if (p->flags&FieldFlag_auto_cast) {
  1613. param->flags |= EntityFlag_AutoCast;
  1614. }
  1615. if (p->flags&FieldFlag_const) {
  1616. param->flags |= EntityFlag_ConstInput;
  1617. }
  1618. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1619. add_entity(ctx->checker, scope, name, param);
  1620. if (is_using) {
  1621. add_entity_use(ctx, name, param);
  1622. }
  1623. array_add(&variables, param);
  1624. }
  1625. }
  1626. if (is_variadic) {
  1627. GB_ASSERT(variadic_index >= 0);
  1628. }
  1629. if (is_variadic) {
  1630. GB_ASSERT(params.count > 0);
  1631. // NOTE(bill): Change last variadic parameter to be a slice
  1632. // Custom Calling convention for variadic parameters
  1633. Entity *end = variables[variadic_index];
  1634. end->type = alloc_type_slice(end->type);
  1635. end->flags |= EntityFlag_Ellipsis;
  1636. if (is_c_vararg) {
  1637. end->flags |= EntityFlag_CVarArg;
  1638. }
  1639. }
  1640. isize specialization_count = 0;
  1641. if (scope != nullptr) {
  1642. for_array(i, scope->elements.entries) {
  1643. Entity *e = scope->elements.entries[i].value;
  1644. if (e->kind == Entity_TypeName) {
  1645. Type *t = e->type;
  1646. if (t->kind == Type_Generic &&
  1647. t->Generic.specialized != nullptr) {
  1648. specialization_count += 1;
  1649. }
  1650. }
  1651. }
  1652. }
  1653. Type *tuple = alloc_type_tuple();
  1654. tuple->Tuple.variables = variables;
  1655. if (success_) *success_ = success;
  1656. if (specialization_count_) *specialization_count_ = specialization_count;
  1657. if (is_variadic_) *is_variadic_ = is_variadic;
  1658. if (variadic_index_) *variadic_index_ = variadic_index;
  1659. return tuple;
  1660. }
  1661. Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1662. if (_results == nullptr) {
  1663. return nullptr;
  1664. }
  1665. ast_node(field_list, FieldList, _results);
  1666. Slice<Ast *> results = field_list->list;
  1667. if (results.count == 0) {
  1668. return nullptr;
  1669. }
  1670. Type *tuple = alloc_type_tuple();
  1671. isize variable_count = 0;
  1672. for_array(i, results) {
  1673. Ast *field = results[i];
  1674. if (ast_node_expect(field, Ast_Field)) {
  1675. ast_node(f, Field, field);
  1676. variable_count += gb_max(f->names.count, 1);
  1677. }
  1678. }
  1679. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1680. for_array(i, results) {
  1681. ast_node(field, Field, results[i]);
  1682. Ast *default_value = unparen_expr(field->default_value);
  1683. ParameterValue param_value = {};
  1684. Type *type = nullptr;
  1685. if (field->type == nullptr) {
  1686. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1687. } else {
  1688. type = check_type(ctx, field->type);
  1689. if (default_value != nullptr) {
  1690. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1691. }
  1692. }
  1693. if (type == nullptr) {
  1694. error(results[i], "Invalid parameter type");
  1695. type = t_invalid;
  1696. }
  1697. if (is_type_untyped(type)) {
  1698. error(results[i], "Cannot determine parameter type from a nil");
  1699. type = t_invalid;
  1700. }
  1701. if (field->names.count == 0) {
  1702. Token token = ast_token(field->type);
  1703. token.string = str_lit("");
  1704. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1705. param->Variable.param_value = param_value;
  1706. array_add(&variables, param);
  1707. } else {
  1708. for_array(j, field->names) {
  1709. Token token = ast_token(results[i]);
  1710. if (field->type != nullptr) {
  1711. token = ast_token(field->type);
  1712. }
  1713. token.string = str_lit("");
  1714. Ast *name = field->names[j];
  1715. if (name->kind != Ast_Ident) {
  1716. error(name, "Expected an identifer for as the field name");
  1717. } else {
  1718. token = name->Ident.token;
  1719. }
  1720. if (is_blank_ident(token)) {
  1721. error(name, "Result value cannot be a blank identifer `_`");
  1722. }
  1723. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1724. param->flags |= EntityFlag_Result;
  1725. param->Variable.param_value = param_value;
  1726. array_add(&variables, param);
  1727. add_entity(ctx->checker, scope, name, param);
  1728. // NOTE(bill): Removes `declared but not used` when using -vet
  1729. add_entity_use(ctx, name, param);
  1730. }
  1731. }
  1732. }
  1733. for_array(i, variables) {
  1734. String x = variables[i]->token.string;
  1735. if (x.len == 0 || is_blank_ident(x)) {
  1736. continue;
  1737. }
  1738. for (isize j = i+1; j < variables.count; j++) {
  1739. String y = variables[j]->token.string;
  1740. if (y.len == 0 || is_blank_ident(y)) {
  1741. continue;
  1742. }
  1743. if (x == y) {
  1744. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1745. }
  1746. }
  1747. }
  1748. tuple->Tuple.variables = variables;
  1749. return tuple;
  1750. }
  1751. Array<Type *> systemv_distribute_struct_fields(Type *t) {
  1752. Type *bt = core_type(t);
  1753. isize distributed_cap = 1;
  1754. if (bt->kind == Type_Struct) {
  1755. distributed_cap = bt->Struct.fields.count;
  1756. }
  1757. auto distributed = array_make<Type *>(heap_allocator(), 0, distributed_cap);
  1758. i64 sz = type_size_of(bt);
  1759. switch (bt->kind) {
  1760. case Type_Basic:
  1761. switch (bt->Basic.kind){
  1762. case Basic_complex64:
  1763. array_add(&distributed, t_f32);
  1764. array_add(&distributed, t_f32);
  1765. break;
  1766. case Basic_complex128:
  1767. array_add(&distributed, t_f64);
  1768. array_add(&distributed, t_f64);
  1769. break;
  1770. case Basic_quaternion128:
  1771. array_add(&distributed, t_f32);
  1772. array_add(&distributed, t_f32);
  1773. array_add(&distributed, t_f32);
  1774. array_add(&distributed, t_f32);
  1775. break;
  1776. case Basic_quaternion256:
  1777. goto DEFAULT;
  1778. case Basic_string:
  1779. array_add(&distributed, t_u8_ptr);
  1780. array_add(&distributed, t_int);
  1781. break;
  1782. case Basic_any:
  1783. GB_ASSERT(type_size_of(t_uintptr) == type_size_of(t_typeid));
  1784. array_add(&distributed, t_rawptr);
  1785. array_add(&distributed, t_uintptr);
  1786. break;
  1787. case Basic_u128:
  1788. case Basic_i128:
  1789. if (build_context.ODIN_OS == "windows") {
  1790. array_add(&distributed, alloc_type_simd_vector(2, t_u64));
  1791. } else {
  1792. array_add(&distributed, bt);
  1793. }
  1794. break;
  1795. default:
  1796. goto DEFAULT;
  1797. }
  1798. break;
  1799. case Type_Struct:
  1800. if (bt->Struct.is_raw_union) {
  1801. goto DEFAULT;
  1802. } else {
  1803. // IMPORTANT TOOD(bill): handle #packed structs correctly
  1804. // IMPORTANT TODO(bill): handle #align structs correctly
  1805. for_array(field_index, bt->Struct.fields) {
  1806. Entity *f = bt->Struct.fields[field_index];
  1807. auto nested = systemv_distribute_struct_fields(f->type);
  1808. array_add_elems(&distributed, nested.data, nested.count);
  1809. array_free(&nested);
  1810. }
  1811. }
  1812. break;
  1813. case Type_Array:
  1814. for (i64 i = 0; i < bt->Array.count; i++) {
  1815. array_add(&distributed, bt->Array.elem);
  1816. }
  1817. break;
  1818. case Type_BitSet:
  1819. array_add(&distributed, bit_set_to_int(bt));
  1820. break;
  1821. case Type_Tuple:
  1822. GB_PANIC("Invalid struct field type");
  1823. break;
  1824. case Type_Slice:
  1825. array_add(&distributed, t_rawptr);
  1826. array_add(&distributed, t_int);
  1827. break;
  1828. case Type_Union:
  1829. case Type_DynamicArray:
  1830. case Type_Map:
  1831. case Type_BitField: // TODO(bill): Ignore?
  1832. // NOTE(bill, 2019-10-10): Odin specific, don't worry about C calling convention yet
  1833. goto DEFAULT;
  1834. case Type_Pointer:
  1835. case Type_Proc:
  1836. case Type_SimdVector: // TODO(bill): Is this correct logic?
  1837. default:
  1838. DEFAULT:;
  1839. if (sz > 0) {
  1840. array_add(&distributed, bt);
  1841. }
  1842. break;
  1843. }
  1844. return distributed;
  1845. }
  1846. Type *struct_type_from_systemv_distribute_struct_fields(Type *abi_type) {
  1847. GB_ASSERT(is_type_tuple(abi_type));
  1848. Type *final_type = alloc_type_struct();
  1849. final_type->Struct.fields = abi_type->Tuple.variables;
  1850. return final_type;
  1851. }
  1852. Type *handle_single_distributed_type_parameter(Array<Type *> const &types, bool packed, isize *offset) {
  1853. GB_ASSERT(types.count > 0);
  1854. if (types.count == 1) {
  1855. if (offset) *offset = 1;
  1856. i64 sz = type_size_of(types[0]);
  1857. if (is_type_float(types[0])) {
  1858. return types[0];
  1859. }
  1860. switch (sz) {
  1861. case 0:
  1862. GB_PANIC("Zero sized type found!");
  1863. case 1: return t_u8;
  1864. case 2: return t_u16;
  1865. case 4: return t_u32;
  1866. case 8: return t_u64;
  1867. default:
  1868. return types[0];
  1869. }
  1870. } else if (types.count >= 2) {
  1871. if (types[0] == t_f32 && types[1] == t_f32) {
  1872. if (offset) *offset = 2;
  1873. return alloc_type_simd_vector(2, t_f32);
  1874. } else if (type_size_of(types[0]) == 8) {
  1875. if (offset) *offset = 1;
  1876. return types[0];
  1877. }
  1878. i64 total_size = 0;
  1879. isize i = 0;
  1880. if (packed) {
  1881. for (; i < types.count && total_size < 8; i += 1) {
  1882. Type *t = types[i];
  1883. i64 s = type_size_of(t);
  1884. total_size += s;
  1885. }
  1886. } else {
  1887. for (; i < types.count && total_size < 8; i += 1) {
  1888. Type *t = types[i];
  1889. i64 s = gb_max(type_size_of(t), 0);
  1890. i64 a = gb_max(type_align_of(t), 1);
  1891. isize ts = align_formula(total_size, a);
  1892. if (ts >= 8) {
  1893. break;
  1894. }
  1895. total_size = ts + s;
  1896. }
  1897. }
  1898. if (offset) *offset = i;
  1899. switch (total_size) {
  1900. case 1: return t_u8;
  1901. case 2: return t_u16;
  1902. case 4: return t_u32;
  1903. case 8: return t_u64;
  1904. }
  1905. return t_u64;
  1906. }
  1907. return nullptr;
  1908. }
  1909. Type *handle_struct_system_v_amd64_abi_type(Type *t) {
  1910. if (type_size_of(t) > 16) {
  1911. return alloc_type_pointer(t);
  1912. }
  1913. Type *original_type = t;
  1914. Type *bt = core_type(t);
  1915. t = base_type(t);
  1916. i64 size = type_size_of(bt);
  1917. switch (t->kind) {
  1918. case Type_Slice:
  1919. case Type_Struct:
  1920. break;
  1921. case Type_Basic:
  1922. switch (bt->Basic.kind) {
  1923. case Basic_string:
  1924. case Basic_any:
  1925. case Basic_complex64:
  1926. case Basic_complex128:
  1927. case Basic_quaternion128:
  1928. break;
  1929. default:
  1930. return original_type;
  1931. }
  1932. break;
  1933. default:
  1934. return original_type;
  1935. }
  1936. bool is_packed = false;
  1937. if (is_type_struct(bt)) {
  1938. is_packed = bt->Struct.is_packed;
  1939. }
  1940. if (is_type_raw_union(bt)) {
  1941. // TODO(bill): Handle raw union correctly for
  1942. return t;
  1943. } else {
  1944. auto field_types = systemv_distribute_struct_fields(bt);
  1945. defer (array_free(&field_types));
  1946. GB_ASSERT(field_types.count <= 16);
  1947. Type *final_type = nullptr;
  1948. if (field_types.count == 0) {
  1949. final_type = t;
  1950. } else if (field_types.count == 1) {
  1951. final_type = field_types[0];
  1952. } else {
  1953. if (size <= 8) {
  1954. isize offset = 0;
  1955. final_type = handle_single_distributed_type_parameter(field_types, is_packed, &offset);
  1956. } else {
  1957. isize offset = 0;
  1958. isize next_offset = 0;
  1959. Type *two_types[2] = {};
  1960. two_types[0] = handle_single_distributed_type_parameter(field_types, is_packed, &offset);
  1961. auto remaining = array_slice(field_types, offset, field_types.count);
  1962. two_types[1] = handle_single_distributed_type_parameter(remaining, is_packed, &next_offset);
  1963. GB_ASSERT(offset + next_offset == field_types.count);
  1964. auto variables = array_make<Entity *>(heap_allocator(), 2);
  1965. variables[0] = alloc_entity_param(nullptr, empty_token, two_types[0], false, false);
  1966. variables[1] = alloc_entity_param(nullptr, empty_token, two_types[1], false, false);
  1967. final_type = alloc_type_tuple();
  1968. final_type->Tuple.variables = variables;
  1969. if (t->kind == Type_Struct) {
  1970. // NOTE(bill): Make this packed
  1971. final_type->Tuple.is_packed = t->Struct.is_packed;
  1972. }
  1973. }
  1974. }
  1975. GB_ASSERT(final_type != nullptr);
  1976. i64 ftsz = type_size_of(final_type);
  1977. i64 otsz = type_size_of(original_type);
  1978. if (ftsz != otsz) {
  1979. // TODO(bill): Handle this case which will be caused by #packed most likely
  1980. switch (otsz) {
  1981. case 1:
  1982. case 2:
  1983. case 4:
  1984. case 8:
  1985. GB_PANIC("Incorrectly handled case for handle_struct_system_v_amd64_abi_type, %s %lld vs %s %lld", type_to_string(final_type), ftsz, type_to_string(original_type), otsz);
  1986. }
  1987. }
  1988. return final_type;
  1989. }
  1990. }
  1991. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) {
  1992. Type *new_type = original_type;
  1993. if (is_type_boolean(original_type)) {
  1994. Type *t = core_type(base_type(new_type));
  1995. if (t == t_bool) {
  1996. return t_llvm_bool;
  1997. }
  1998. return new_type;
  1999. }
  2000. if (is_type_proc(original_type)) {
  2001. // NOTE(bill): Force a cast to prevent a possible type cycle
  2002. return t_rawptr;
  2003. }
  2004. if (cc == ProcCC_None || cc == ProcCC_PureNone || cc == ProcCC_InlineAsm) {
  2005. return new_type;
  2006. }
  2007. if (build_context.ODIN_ARCH == "386") {
  2008. return new_type;
  2009. }
  2010. if (is_type_simd_vector(original_type)) {
  2011. return new_type;
  2012. }
  2013. if (build_context.ODIN_ARCH == "amd64") {
  2014. bool is_128 = is_type_integer_128bit(original_type);
  2015. if (!is_128 && is_type_bit_set(original_type) && type_size_of(original_type) == 16) {
  2016. // is_128 = true;
  2017. }
  2018. if (is_128) {
  2019. if (build_context.ODIN_OS == "windows") {
  2020. return alloc_type_simd_vector(2, t_u64);
  2021. } else {
  2022. return original_type;
  2023. }
  2024. }
  2025. }
  2026. if (build_context.ODIN_OS == "windows") {
  2027. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  2028. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  2029. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  2030. Type *bt = core_type(original_type);
  2031. switch (bt->kind) {
  2032. // Okay to pass by value (usually)
  2033. // Especially the only Odin types
  2034. case Type_Basic: {
  2035. i64 sz = bt->Basic.size;
  2036. // if (sz > 8 && build_context.word_size < 8) {
  2037. if (sz > 8) {
  2038. new_type = alloc_type_pointer(original_type);
  2039. }
  2040. break;
  2041. }
  2042. case Type_Pointer: break;
  2043. case Type_Proc: break; // NOTE(bill): Just a pointer
  2044. // Odin specific
  2045. case Type_Slice:
  2046. case Type_Array:
  2047. case Type_DynamicArray:
  2048. case Type_Map:
  2049. case Type_Union:
  2050. // Could be in C too
  2051. case Type_Struct:
  2052. {
  2053. i64 align = type_align_of(original_type);
  2054. i64 size = type_size_of(original_type);
  2055. switch (8*size) {
  2056. case 8: new_type = t_u8; break;
  2057. case 16: new_type = t_u16; break;
  2058. case 32: new_type = t_u32; break;
  2059. case 64: new_type = t_u64; break;
  2060. default:
  2061. new_type = alloc_type_pointer(original_type);
  2062. break;
  2063. }
  2064. break;
  2065. }
  2066. }
  2067. } else if (build_context.ODIN_OS == "linux" ||
  2068. build_context.ODIN_OS == "darwin") {
  2069. Type *bt = core_type(original_type);
  2070. switch (bt->kind) {
  2071. // Okay to pass by value (usually)
  2072. // Especially the only Odin types
  2073. case Type_Basic: {
  2074. i64 sz = bt->Basic.size;
  2075. // if (sz > 8 && build_context.word_size < 8) {
  2076. if (sz > 8) {
  2077. new_type = alloc_type_pointer(original_type);
  2078. }
  2079. break;
  2080. }
  2081. case Type_Pointer: break;
  2082. case Type_Proc: break; // NOTE(bill): Just a pointer
  2083. default: {
  2084. i64 size = type_size_of(original_type);
  2085. if (size > 16) {
  2086. new_type = alloc_type_pointer(original_type);
  2087. } else if (build_context.ODIN_ARCH == "amd64") {
  2088. // NOTE(bill): System V AMD64 ABI
  2089. new_type = handle_struct_system_v_amd64_abi_type(bt);
  2090. if (are_types_identical(core_type(original_type), new_type)) {
  2091. new_type = original_type;
  2092. }
  2093. return new_type;
  2094. }
  2095. break;
  2096. }
  2097. }
  2098. } else {
  2099. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  2100. // their architectures
  2101. }
  2102. return new_type;
  2103. }
  2104. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCallingConvention cc) {
  2105. Type *new_type = original_type;
  2106. if (new_type == nullptr) {
  2107. return nullptr;
  2108. }
  2109. GB_ASSERT(is_type_tuple(original_type));
  2110. Type *single_type = reduce_tuple_to_single_type(original_type);
  2111. if (cc == ProcCC_InlineAsm) {
  2112. return new_type;
  2113. }
  2114. if (is_type_proc(single_type)) {
  2115. // NOTE(bill): Force a cast to prevent a possible type cycle
  2116. return t_rawptr;
  2117. }
  2118. if (is_type_simd_vector(single_type)) {
  2119. return new_type;
  2120. }
  2121. if (build_context.ODIN_OS == "windows") {
  2122. if (build_context.ODIN_ARCH == "amd64") {
  2123. if (is_type_integer_128bit(single_type)) {
  2124. if (cc == ProcCC_None || cc == ProcCC_PureNone) {
  2125. return original_type;
  2126. } else {
  2127. return alloc_type_simd_vector(2, t_u64);
  2128. }
  2129. }
  2130. }
  2131. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  2132. // NOTE(bill): This is just reversed engineered from LLVM IR output
  2133. switch (bt->kind) {
  2134. // Okay to pass by value
  2135. // Especially the only Odin types
  2136. case Type_Pointer: break;
  2137. case Type_Proc: break; // NOTE(bill): Just a pointer
  2138. case Type_Basic: break;
  2139. default: {
  2140. i64 align = type_align_of(original_type);
  2141. i64 size = type_size_of(original_type);
  2142. switch (8*size) {
  2143. #if 1
  2144. case 8: new_type = t_u8; break;
  2145. case 16: new_type = t_u16; break;
  2146. case 32: new_type = t_u32; break;
  2147. case 64: new_type = t_u64; break;
  2148. #endif
  2149. }
  2150. break;
  2151. }
  2152. }
  2153. } else if (build_context.ODIN_OS == "linux" || build_context.ODIN_OS == "darwin") {
  2154. if (build_context.ODIN_ARCH == "amd64") {
  2155. }
  2156. } else {
  2157. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  2158. // their architectures
  2159. }
  2160. if (is_type_integer_128bit(single_type)) {
  2161. if (build_context.word_size == 8) {
  2162. return original_type;
  2163. }
  2164. }
  2165. if (new_type != original_type) {
  2166. Type *tuple = alloc_type_tuple();
  2167. auto variables = array_make<Entity *>(a, 0, 1);
  2168. array_add(&variables, alloc_entity_param(original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false));
  2169. tuple->Tuple.variables = variables;
  2170. new_type = tuple;
  2171. }
  2172. new_type->cached_size = -1;
  2173. new_type->cached_align = -1;
  2174. return new_type;
  2175. }
  2176. bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  2177. if (abi_return_type == nullptr) {
  2178. return false;
  2179. }
  2180. if (cc == ProcCC_None || cc == ProcCC_PureNone || cc == ProcCC_InlineAsm) {
  2181. return false;
  2182. }
  2183. Type *single_type = reduce_tuple_to_single_type(abi_return_type);
  2184. if (is_type_simd_vector(single_type)) {
  2185. return false;
  2186. }
  2187. if (build_context.word_size == 8) {
  2188. if (is_type_integer_128bit(single_type)) {
  2189. return false;
  2190. }
  2191. }
  2192. if (build_context.ODIN_OS == "windows") {
  2193. i64 size = 8*type_size_of(abi_return_type);
  2194. switch (size) {
  2195. case 0:
  2196. case 8:
  2197. case 16:
  2198. case 32:
  2199. case 64:
  2200. return false;
  2201. default:
  2202. return true;
  2203. }
  2204. } else {
  2205. if (is_type_integer_128bit(single_type)) {
  2206. return build_context.word_size < 8;
  2207. }
  2208. }
  2209. return false;
  2210. }
  2211. void set_procedure_abi_types(Type *type) {
  2212. type = base_type(type);
  2213. if (type->kind != Type_Proc) {
  2214. return;
  2215. }
  2216. if (type->Proc.abi_types_set || type->flags & TypeFlag_InProcessOfCheckingABI) {
  2217. return;
  2218. }
  2219. gbAllocator allocator = permanent_allocator();
  2220. u32 flags = type->flags;
  2221. type->flags |= TypeFlag_InProcessOfCheckingABI;
  2222. type->Proc.abi_compat_params = array_make<Type *>(allocator, cast(isize)type->Proc.param_count);
  2223. for (i32 i = 0; i < type->Proc.param_count; i++) {
  2224. Entity *e = type->Proc.params->Tuple.variables[i];
  2225. if (e->kind == Entity_Variable) {
  2226. Type *original_type = e->type;
  2227. Type *new_type = type_to_abi_compat_param_type(allocator, original_type, type->Proc.calling_convention);
  2228. type->Proc.abi_compat_params[i] = new_type;
  2229. switch (type->Proc.calling_convention) {
  2230. case ProcCC_Odin:
  2231. case ProcCC_Contextless:
  2232. case ProcCC_Pure:
  2233. if (is_type_pointer(new_type) && !is_type_pointer(e->type) && !is_type_proc(e->type)) {
  2234. e->flags |= EntityFlag_ImplicitReference;
  2235. }
  2236. break;
  2237. }
  2238. if (build_context.ODIN_OS == "linux" ||
  2239. build_context.ODIN_OS == "darwin") {
  2240. if (is_type_pointer(new_type) & !is_type_pointer(e->type) && !is_type_proc(e->type)) {
  2241. e->flags |= EntityFlag_ByVal;
  2242. }
  2243. }
  2244. }
  2245. }
  2246. for (i32 i = 0; i < type->Proc.param_count; i++) {
  2247. Entity *e = type->Proc.params->Tuple.variables[i];
  2248. if (e->kind == Entity_Variable) {
  2249. set_procedure_abi_types(e->type);
  2250. }
  2251. }
  2252. for (i32 i = 0; i < type->Proc.result_count; i++) {
  2253. Entity *e = type->Proc.results->Tuple.variables[i];
  2254. if (e->kind == Entity_Variable) {
  2255. set_procedure_abi_types(e->type);
  2256. }
  2257. }
  2258. // NOTE(bill): The types are the same
  2259. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(allocator, type->Proc.results, type->Proc.calling_convention);
  2260. type->Proc.return_by_pointer = abi_compat_return_by_pointer(allocator, type->Proc.calling_convention, type->Proc.abi_compat_result_type);
  2261. type->Proc.abi_types_set = true;
  2262. type->flags = flags;
  2263. }
  2264. // NOTE(bill): 'operands' is for generating non generic procedure type
  2265. bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> *operands) {
  2266. ast_node(pt, ProcType, proc_type_node);
  2267. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  2268. ctx->polymorphic_scope = ctx->scope;
  2269. }
  2270. CheckerContext c_ = *ctx;
  2271. CheckerContext *c = &c_;
  2272. c->curr_proc_sig = type;
  2273. c->in_proc_sig = true;
  2274. ProcCallingConvention cc = pt->calling_convention;
  2275. if (cc == ProcCC_ForeignBlockDefault) {
  2276. cc = ProcCC_CDecl;
  2277. if (c->foreign_context.default_cc > 0) {
  2278. cc = c->foreign_context.default_cc;
  2279. }
  2280. }
  2281. GB_ASSERT(cc > 0);
  2282. if (cc == ProcCC_Odin) {
  2283. c->scope->flags |= ScopeFlag_ContextDefined;
  2284. } else {
  2285. c->scope->flags &= ~ScopeFlag_ContextDefined;
  2286. }
  2287. bool variadic = false;
  2288. isize variadic_index = -1;
  2289. bool success = true;
  2290. isize specialization_count = 0;
  2291. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  2292. Type *results = check_get_results(c, c->scope, pt->results);
  2293. isize param_count = 0;
  2294. isize result_count = 0;
  2295. if (params) param_count = params ->Tuple.variables.count;
  2296. if (results) result_count = results->Tuple.variables.count;
  2297. if (param_count > 0) {
  2298. for_array(i, params->Tuple.variables) {
  2299. Entity *param = params->Tuple.variables[i];
  2300. if (param->kind == Entity_Variable) {
  2301. ParameterValue pv = param->Variable.param_value;
  2302. if (pv.kind == ParameterValue_Constant &&
  2303. pv.value.kind == ExactValue_Procedure) {
  2304. type->Proc.has_proc_default_values = true;
  2305. break;
  2306. }
  2307. }
  2308. }
  2309. }
  2310. if (result_count > 0) {
  2311. Entity *first = results->Tuple.variables[0];
  2312. type->Proc.has_named_results = first->token.string != "";
  2313. }
  2314. if (result_count == 0 && cc == ProcCC_Pure) {
  2315. error(proc_type_node, "\"pure\" procedures must have at least 1 return value");
  2316. }
  2317. bool optional_ok = (pt->tags & ProcTag_optional_ok) != 0;
  2318. if (optional_ok) {
  2319. if (result_count != 2) {
  2320. error(proc_type_node, "A procedure type with the #optional_ok tag requires 2 return values, got %td", result_count);
  2321. } else {
  2322. Entity *second = results->Tuple.variables[1];
  2323. if (is_type_polymorphic(second->type)) {
  2324. // ignore
  2325. } else if (is_type_boolean(second->type)) {
  2326. // GOOD
  2327. } else {
  2328. error(second->token, "Second return value of an #optional_ok procedure must be a boolean, got %s", type_to_string(second->type));
  2329. }
  2330. }
  2331. }
  2332. type->Proc.node = proc_type_node;
  2333. type->Proc.scope = c->scope;
  2334. type->Proc.params = params;
  2335. type->Proc.param_count = cast(i32)param_count;
  2336. type->Proc.results = results;
  2337. type->Proc.result_count = cast(i32)result_count;
  2338. type->Proc.variadic = variadic;
  2339. type->Proc.variadic_index = cast(i32)variadic_index;
  2340. type->Proc.calling_convention = cc;
  2341. type->Proc.is_polymorphic = pt->generic;
  2342. type->Proc.specialization_count = specialization_count;
  2343. type->Proc.diverging = pt->diverging;
  2344. type->Proc.optional_ok = optional_ok;
  2345. type->Proc.tags = pt->tags;
  2346. if (param_count > 0) {
  2347. Entity *end = params->Tuple.variables[param_count-1];
  2348. if (end->flags&EntityFlag_CVarArg) {
  2349. if (cc == ProcCC_StdCall || cc == ProcCC_CDecl) {
  2350. type->Proc.c_vararg = true;
  2351. } else {
  2352. error(end->token, "Calling convention does not support #c_vararg");
  2353. }
  2354. }
  2355. }
  2356. bool is_polymorphic = false;
  2357. for (isize i = 0; i < param_count; i++) {
  2358. Entity *e = params->Tuple.variables[i];
  2359. if (e->kind != Entity_Variable) {
  2360. is_polymorphic = true;
  2361. break;
  2362. } else if (is_type_polymorphic(e->type)) {
  2363. is_polymorphic = true;
  2364. break;
  2365. }
  2366. }
  2367. for (isize i = 0; i < result_count; i++) {
  2368. Entity *e = results->Tuple.variables[i];
  2369. if (e->kind != Entity_Variable) {
  2370. is_polymorphic = true;
  2371. break;
  2372. } else if (is_type_polymorphic(e->type)) {
  2373. is_polymorphic = true;
  2374. break;
  2375. }
  2376. }
  2377. type->Proc.is_polymorphic = is_polymorphic;
  2378. return success;
  2379. }
  2380. i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  2381. if (e == nullptr) {
  2382. return 0;
  2383. }
  2384. if (e->kind == Ast_UnaryExpr &&
  2385. e->UnaryExpr.op.kind == Token_Question) {
  2386. return -1;
  2387. }
  2388. check_expr_or_type(ctx, o, e);
  2389. if (o->mode == Addressing_Type && o->type->kind == Type_Generic) {
  2390. if (ctx->allow_polymorphic_types) {
  2391. if (o->type->Generic.specialized) {
  2392. o->type->Generic.specialized = nullptr;
  2393. error(o->expr, "Polymorphic array length cannot have a specialization");
  2394. }
  2395. return 0;
  2396. }
  2397. }
  2398. if (o->mode == Addressing_Type) {
  2399. if (is_type_enum(o->type)) {
  2400. return -1;
  2401. }
  2402. }
  2403. if (o->mode != Addressing_Constant) {
  2404. if (o->mode != Addressing_Invalid) {
  2405. o->mode = Addressing_Invalid;
  2406. error(e, "Array count must be a constant");
  2407. }
  2408. return 0;
  2409. }
  2410. Type *type = core_type(o->type);
  2411. if (is_type_untyped(type) || is_type_integer(type)) {
  2412. if (o->value.kind == ExactValue_Integer) {
  2413. BigInt count = o->value.value_integer;
  2414. if (o->value.value_integer.neg) {
  2415. gbAllocator a = heap_allocator();
  2416. String str = big_int_to_string(a, &count);
  2417. error(e, "Invalid negative array count, %.*s", LIT(str));
  2418. gb_free(a, str.text);
  2419. return 0;
  2420. }
  2421. switch (count.len) {
  2422. case 0: return 0;
  2423. case 1: return count.d.word;
  2424. }
  2425. gbAllocator a = heap_allocator();
  2426. String str = big_int_to_string(a, &count);
  2427. error(e, "Array count too large, %.*s", LIT(str));
  2428. gb_free(a, str.text);
  2429. return 0;
  2430. }
  2431. }
  2432. error(e, "Array count must be an integer");
  2433. return 0;
  2434. }
  2435. Type *make_optional_ok_type(Type *value, bool typed) {
  2436. // LEAK TODO(bill): probably don't reallocate everything here and reuse the same one for the same type if possible
  2437. gbAllocator a = heap_allocator();
  2438. Type *t = alloc_type_tuple();
  2439. array_init(&t->Tuple.variables, a, 0, 2);
  2440. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, value, false, 0));
  2441. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1));
  2442. return t;
  2443. }
  2444. void init_map_entry_type(Type *type) {
  2445. GB_ASSERT(type->kind == Type_Map);
  2446. if (type->Map.entry_type != nullptr) return;
  2447. // NOTE(bill): The preload types may have not been set yet
  2448. GB_ASSERT(t_map_hash != nullptr);
  2449. Type *entry_type = alloc_type_struct();
  2450. /*
  2451. struct {
  2452. hash: runtime.Map_Hash,
  2453. next: int,
  2454. key: Key,
  2455. value: Value,
  2456. }
  2457. */
  2458. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  2459. Scope *s = create_scope(builtin_pkg->scope);
  2460. auto fields = array_make<Entity *>(permanent_allocator(), 0, 4);
  2461. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hash")), t_uintptr, false, cast(i32)fields.count, EntityState_Resolved));
  2462. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, cast(i32)fields.count, EntityState_Resolved));
  2463. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), type->Map.key, false, cast(i32)fields.count, EntityState_Resolved));
  2464. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, cast(i32)fields.count, EntityState_Resolved));
  2465. entry_type->Struct.fields = fields;
  2466. type->Map.entry_type = entry_type;
  2467. }
  2468. void init_map_internal_types(Type *type) {
  2469. GB_ASSERT(type->kind == Type_Map);
  2470. init_map_entry_type(type);
  2471. if (type->Map.internal_type != nullptr) return;
  2472. if (type->Map.generated_struct_type != nullptr) return;
  2473. Type *key = type->Map.key;
  2474. Type *value = type->Map.value;
  2475. GB_ASSERT(key != nullptr);
  2476. GB_ASSERT(value != nullptr);
  2477. Type *generated_struct_type = alloc_type_struct();
  2478. /*
  2479. struct {
  2480. hashes: []int;
  2481. entries: [dynamic]EntryType;
  2482. }
  2483. */
  2484. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  2485. Scope *s = create_scope(builtin_pkg->scope);
  2486. Type *hashes_type = alloc_type_slice(t_int);
  2487. Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
  2488. auto fields = array_make<Entity *>(permanent_allocator(), 0, 2);
  2489. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved));
  2490. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved));
  2491. generated_struct_type->Struct.fields = fields;
  2492. type_set_offsets(generated_struct_type);
  2493. type->Map.generated_struct_type = generated_struct_type;
  2494. type->Map.internal_type = generated_struct_type;
  2495. type->Map.lookup_result_type = make_optional_ok_type(value);
  2496. }
  2497. void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  2498. GB_ASSERT(type->kind == Type_Map);
  2499. ast_node(mt, MapType, node);
  2500. Type *key = check_type(ctx, mt->key);
  2501. Type *value = check_type(ctx, mt->value);
  2502. if (!is_type_valid_for_keys(key)) {
  2503. if (is_type_boolean(key)) {
  2504. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  2505. } else {
  2506. gbString str = type_to_string(key);
  2507. error(node, "Invalid type of a key for a map, got '%s'", str);
  2508. gb_string_free(str);
  2509. }
  2510. }
  2511. type->Map.key = key;
  2512. type->Map.value = value;
  2513. if (is_type_string(key)) {
  2514. add_package_dependency(ctx, "runtime", "default_hash_string");
  2515. } else {
  2516. add_package_dependency(ctx, "runtime", "default_hash_ptr");
  2517. }
  2518. init_core_map_type(ctx->checker);
  2519. init_map_internal_types(type);
  2520. // error(node, "'map' types are not yet implemented");
  2521. }
  2522. Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type) {
  2523. Type *bt_elem = base_type(elem);
  2524. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2525. gbString str = type_to_string(elem);
  2526. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2527. gb_string_free(str);
  2528. return alloc_type_array(elem, count, generic_type);
  2529. }
  2530. Type *soa_struct = nullptr;
  2531. Scope *scope = nullptr;
  2532. if (is_type_array(elem)) {
  2533. Type *old_array = base_type(elem);
  2534. soa_struct = alloc_type_struct();
  2535. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
  2536. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
  2537. soa_struct->Struct.node = array_typ_expr;
  2538. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2539. soa_struct->Struct.soa_elem = elem;
  2540. soa_struct->Struct.soa_count = count;
  2541. scope = create_scope(ctx->scope);
  2542. soa_struct->Struct.scope = scope;
  2543. String params_xyzw[4] = {
  2544. str_lit("x"),
  2545. str_lit("y"),
  2546. str_lit("z"),
  2547. str_lit("w")
  2548. };
  2549. for (i64 i = 0; i < old_array->Array.count; i++) {
  2550. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  2551. Token token = {};
  2552. token.string = params_xyzw[i];
  2553. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2554. soa_struct->Struct.fields[i] = new_field;
  2555. add_entity(ctx->checker, scope, nullptr, new_field);
  2556. add_entity_use(ctx, nullptr, new_field);
  2557. }
  2558. } else {
  2559. GB_ASSERT(is_type_struct(elem));
  2560. Type *old_struct = base_type(elem);
  2561. soa_struct = alloc_type_struct();
  2562. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  2563. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count);
  2564. soa_struct->Struct.node = array_typ_expr;
  2565. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  2566. soa_struct->Struct.soa_elem = elem;
  2567. soa_struct->Struct.soa_count = count;
  2568. scope = create_scope(old_struct->Struct.scope->parent);
  2569. soa_struct->Struct.scope = scope;
  2570. for_array(i, old_struct->Struct.fields) {
  2571. Entity *old_field = old_struct->Struct.fields[i];
  2572. if (old_field->kind == Entity_Variable) {
  2573. Type *array_type = alloc_type_array(old_field->type, count);
  2574. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2575. soa_struct->Struct.fields[i] = new_field;
  2576. add_entity(ctx->checker, scope, nullptr, new_field);
  2577. add_entity_use(ctx, nullptr, new_field);
  2578. } else {
  2579. soa_struct->Struct.fields[i] = old_field;
  2580. }
  2581. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2582. }
  2583. }
  2584. Token token = {};
  2585. token.string = str_lit("Base_Type");
  2586. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2587. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2588. add_type_info_type(ctx, soa_struct);
  2589. return soa_struct;
  2590. }
  2591. Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2592. Type *bt_elem = base_type(elem);
  2593. bool is_polymorphic = is_type_polymorphic(elem);
  2594. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2595. GB_ASSERT(elem_expr != nullptr);
  2596. gbString str = type_to_string(elem);
  2597. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2598. gb_string_free(str);
  2599. return alloc_type_slice(elem);
  2600. }
  2601. Type *soa_struct = nullptr;
  2602. Scope *scope = nullptr;
  2603. isize field_count = 0;
  2604. if (is_polymorphic) {
  2605. field_count = 0;
  2606. soa_struct = alloc_type_struct();
  2607. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2608. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+1);
  2609. soa_struct->Struct.node = array_typ_expr;
  2610. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2611. soa_struct->Struct.soa_elem = elem;
  2612. soa_struct->Struct.soa_count = 0;
  2613. soa_struct->Struct.is_polymorphic = true;
  2614. scope = create_scope(ctx->scope);
  2615. soa_struct->Struct.scope = scope;
  2616. } else if (is_type_array(elem)) {
  2617. Type *old_array = base_type(elem);
  2618. field_count = old_array->Array.count;
  2619. soa_struct = alloc_type_struct();
  2620. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2621. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+1);
  2622. soa_struct->Struct.node = array_typ_expr;
  2623. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2624. soa_struct->Struct.soa_elem = elem;
  2625. soa_struct->Struct.soa_count = 0;
  2626. scope = create_scope(ctx->scope);
  2627. soa_struct->Struct.scope = scope;
  2628. String params_xyzw[4] = {
  2629. str_lit("x"),
  2630. str_lit("y"),
  2631. str_lit("z"),
  2632. str_lit("w")
  2633. };
  2634. for (i64 i = 0; i < field_count; i++) {
  2635. Type *array_type = alloc_type_pointer(old_array->Array.elem);
  2636. Token token = {};
  2637. token.string = params_xyzw[i];
  2638. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2639. new_field->flags |= EntityFlag_SoaPtrField;
  2640. soa_struct->Struct.fields[i] = new_field;
  2641. add_entity(ctx->checker, scope, nullptr, new_field);
  2642. add_entity_use(ctx, nullptr, new_field);
  2643. }
  2644. } else {
  2645. GB_ASSERT(is_type_struct(elem));
  2646. Type *old_struct = base_type(elem);
  2647. field_count = old_struct->Struct.fields.count;
  2648. soa_struct = alloc_type_struct();
  2649. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+1);
  2650. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count+1);
  2651. soa_struct->Struct.node = array_typ_expr;
  2652. soa_struct->Struct.soa_kind = StructSoa_Slice;
  2653. soa_struct->Struct.soa_elem = elem;
  2654. soa_struct->Struct.soa_count = 0;
  2655. scope = create_scope(old_struct->Struct.scope->parent);
  2656. soa_struct->Struct.scope = scope;
  2657. for_array(i, old_struct->Struct.fields) {
  2658. Entity *old_field = old_struct->Struct.fields[i];
  2659. if (old_field->kind == Entity_Variable) {
  2660. Type *array_type = alloc_type_pointer(old_field->type);
  2661. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2662. new_field->flags |= EntityFlag_SoaPtrField;
  2663. soa_struct->Struct.fields[i] = new_field;
  2664. add_entity(ctx->checker, scope, nullptr, new_field);
  2665. add_entity_use(ctx, nullptr, new_field);
  2666. } else {
  2667. soa_struct->Struct.fields[i] = old_field;
  2668. }
  2669. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2670. }
  2671. }
  2672. Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2673. soa_struct->Struct.fields[field_count] = len_field;
  2674. add_entity(ctx->checker, scope, nullptr, len_field);
  2675. add_entity_use(ctx, nullptr, len_field);
  2676. Token token = {};
  2677. token.string = str_lit("Base_Type");
  2678. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2679. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2680. add_type_info_type(ctx, soa_struct);
  2681. return soa_struct;
  2682. }
  2683. Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2684. Type *bt_elem = base_type(elem);
  2685. bool is_polymorphic = is_type_polymorphic(elem);
  2686. if (!is_polymorphic && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2687. GB_ASSERT(elem_expr != nullptr);
  2688. gbString str = type_to_string(elem);
  2689. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2690. gb_string_free(str);
  2691. return alloc_type_dynamic_array(elem);
  2692. }
  2693. Type *soa_struct = nullptr;
  2694. Scope *scope = nullptr;
  2695. isize field_count = 0;
  2696. if (is_polymorphic) {
  2697. field_count = 0;
  2698. soa_struct = alloc_type_struct();
  2699. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2700. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+3);
  2701. soa_struct->Struct.node = array_typ_expr;
  2702. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2703. soa_struct->Struct.soa_elem = elem;
  2704. soa_struct->Struct.soa_count = 0;
  2705. soa_struct->Struct.is_polymorphic = true;
  2706. scope = create_scope(ctx->scope);
  2707. soa_struct->Struct.scope = scope;
  2708. } else if (is_type_array(elem)) {
  2709. Type *old_array = base_type(elem);
  2710. field_count = old_array->Array.count;
  2711. soa_struct = alloc_type_struct();
  2712. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2713. soa_struct->Struct.tags = array_make<String>(heap_allocator(), field_count+3);
  2714. soa_struct->Struct.node = array_typ_expr;
  2715. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2716. soa_struct->Struct.soa_elem = elem;
  2717. soa_struct->Struct.soa_count = 0;
  2718. scope = create_scope(ctx->scope);
  2719. soa_struct->Struct.scope = scope;
  2720. String params_xyzw[4] = {
  2721. str_lit("x"),
  2722. str_lit("y"),
  2723. str_lit("z"),
  2724. str_lit("w")
  2725. };
  2726. for (i64 i = 0; i < field_count; i++) {
  2727. Type *array_type = alloc_type_pointer(old_array->Array.elem);
  2728. Token token = {};
  2729. token.string = params_xyzw[i];
  2730. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  2731. new_field->flags |= EntityFlag_SoaPtrField;
  2732. soa_struct->Struct.fields[i] = new_field;
  2733. add_entity(ctx->checker, scope, nullptr, new_field);
  2734. add_entity_use(ctx, nullptr, new_field);
  2735. }
  2736. } else {
  2737. GB_ASSERT(is_type_struct(elem));
  2738. Type *old_struct = base_type(elem);
  2739. field_count = old_struct->Struct.fields.count;
  2740. soa_struct = alloc_type_struct();
  2741. soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+3);
  2742. soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_struct->Struct.tags.count+3);
  2743. soa_struct->Struct.node = array_typ_expr;
  2744. soa_struct->Struct.soa_kind = StructSoa_Dynamic;
  2745. soa_struct->Struct.soa_elem = elem;
  2746. soa_struct->Struct.soa_count = 0;
  2747. scope = create_scope(old_struct->Struct.scope->parent);
  2748. soa_struct->Struct.scope = scope;
  2749. for_array(i, old_struct->Struct.fields) {
  2750. Entity *old_field = old_struct->Struct.fields[i];
  2751. if (old_field->kind == Entity_Variable) {
  2752. Type *array_type = alloc_type_pointer(old_field->type);
  2753. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_src_index);
  2754. new_field->flags |= EntityFlag_SoaPtrField;
  2755. soa_struct->Struct.fields[i] = new_field;
  2756. add_entity(ctx->checker, scope, nullptr, new_field);
  2757. add_entity_use(ctx, nullptr, new_field);
  2758. } else {
  2759. soa_struct->Struct.fields[i] = old_field;
  2760. }
  2761. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2762. }
  2763. }
  2764. Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2765. soa_struct->Struct.fields[field_count+0] = len_field;
  2766. add_entity(ctx->checker, scope, nullptr, len_field);
  2767. add_entity_use(ctx, nullptr, len_field);
  2768. Entity *cap_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count);
  2769. soa_struct->Struct.fields[field_count+1] = cap_field;
  2770. add_entity(ctx->checker, scope, nullptr, cap_field);
  2771. add_entity_use(ctx, nullptr, cap_field);
  2772. Token token = {};
  2773. token.string = str_lit("allocator");
  2774. Entity *allocator_field = alloc_entity_field(scope, token, t_allocator, false, cast(i32)field_count);
  2775. soa_struct->Struct.fields[field_count+2] = allocator_field;
  2776. add_entity(ctx->checker, scope, nullptr, allocator_field);
  2777. add_entity_use(ctx, nullptr, allocator_field);
  2778. token.string = str_lit("Base_Type");
  2779. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2780. add_entity(ctx->checker, scope, nullptr, base_type_entity);
  2781. add_type_info_type(ctx, soa_struct);
  2782. return soa_struct;
  2783. }
  2784. bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2785. GB_ASSERT_NOT_NULL(type);
  2786. if (e == nullptr) {
  2787. *type = t_invalid;
  2788. return true;
  2789. }
  2790. switch (e->kind) {
  2791. case_ast_node(i, Ident, e);
  2792. Operand o = {};
  2793. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  2794. gbString err_str = nullptr;
  2795. defer (gb_string_free(err_str));
  2796. switch (o.mode) {
  2797. case Addressing_Invalid:
  2798. break;
  2799. case Addressing_Type: {
  2800. *type = o.type;
  2801. if (!ctx->in_polymorphic_specialization) {
  2802. Type *t = base_type(o.type);
  2803. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  2804. err_str = expr_to_string(e);
  2805. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  2806. return true;
  2807. }
  2808. }
  2809. // if (ctx->type_level == 0 && entity->state == EntityState_InProgress) {
  2810. // error(entity->token, "Illegal declaration cycle of `%.*s`", LIT(entity->token.string));
  2811. // for_array(j, *ctx->type_path) {
  2812. // Entity *k = (*ctx->type_path)[j];
  2813. // error(k->token, "\t%.*s refers to", LIT(k->token.string));
  2814. // }
  2815. // error(entity->token, "\t%.*s", LIT(entity->token.string));
  2816. // *type = t_invalid;
  2817. // }
  2818. return true;
  2819. }
  2820. case Addressing_NoValue:
  2821. err_str = expr_to_string(e);
  2822. error(e, "'%s' used as a type", err_str);
  2823. break;
  2824. default:
  2825. err_str = expr_to_string(e);
  2826. error(e, "'%s' used as a type when not a type", err_str);
  2827. break;
  2828. }
  2829. case_end;
  2830. case_ast_node(ht, HelperType, e);
  2831. return check_type_internal(ctx, ht->type, type, named_type);
  2832. case_end;
  2833. case_ast_node(dt, DistinctType, e);
  2834. error(e, "Invalid use of a distinct type");
  2835. // NOTE(bill): Treat it as a HelperType to remove errors
  2836. return check_type_internal(ctx, dt->type, type, named_type);
  2837. case_end;
  2838. case_ast_node(tt, TypeidType, e);
  2839. e->tav.mode = Addressing_Type;
  2840. e->tav.type = t_typeid;
  2841. *type = t_typeid;
  2842. set_base_type(named_type, *type);
  2843. return true;
  2844. case_end;
  2845. case_ast_node(pt, PolyType, e);
  2846. Ast *ident = pt->type;
  2847. if (ident->kind != Ast_Ident) {
  2848. error(ident, "Expected an identifier after the $");
  2849. *type = t_invalid;
  2850. return false;
  2851. }
  2852. Token token = ident->Ident.token;
  2853. Type *specific = nullptr;
  2854. if (pt->specialization != nullptr) {
  2855. CheckerContext c = *ctx;
  2856. c.in_polymorphic_specialization = true;
  2857. Ast *s = pt->specialization;
  2858. specific = check_type(&c, s);
  2859. }
  2860. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2861. if (ctx->allow_polymorphic_types) {
  2862. Scope *ps = ctx->polymorphic_scope;
  2863. Scope *s = ctx->scope;
  2864. Scope *entity_scope = s;
  2865. if (ps != nullptr && ps != s) {
  2866. // TODO(bill): Is this check needed?
  2867. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2868. entity_scope = ps;
  2869. }
  2870. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2871. t->Generic.entity = e;
  2872. e->TypeName.is_type_alias = true;
  2873. e->state = EntityState_Resolved;
  2874. add_entity(ctx->checker, ps, ident, e);
  2875. add_entity(ctx->checker, s, ident, e);
  2876. } else {
  2877. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2878. *type = t_invalid;
  2879. return false;
  2880. }
  2881. *type = t;
  2882. set_base_type(named_type, *type);
  2883. return true;
  2884. case_end;
  2885. case_ast_node(se, SelectorExpr, e);
  2886. Operand o = {};
  2887. check_selector(ctx, &o, e, nullptr);
  2888. gbString err_str;
  2889. switch (o.mode) {
  2890. case Addressing_Invalid:
  2891. break;
  2892. case Addressing_Type:
  2893. GB_ASSERT(o.type != nullptr);
  2894. *type = o.type;
  2895. return true;
  2896. case Addressing_NoValue:
  2897. err_str = expr_to_string(e);
  2898. error(e, "'%s' used as a type", err_str);
  2899. gb_string_free(err_str);
  2900. break;
  2901. default:
  2902. err_str = expr_to_string(e);
  2903. error(e, "'%s' is not a type", err_str);
  2904. gb_string_free(err_str);
  2905. break;
  2906. }
  2907. case_end;
  2908. case_ast_node(pe, ParenExpr, e);
  2909. *type = check_type_expr(ctx, pe->expr, named_type);
  2910. set_base_type(named_type, *type);
  2911. return true;
  2912. case_end;
  2913. case_ast_node(ue, UnaryExpr, e);
  2914. switch (ue->op.kind) {
  2915. case Token_Pointer:
  2916. *type = alloc_type_pointer(check_type(ctx, ue->expr));
  2917. set_base_type(named_type, *type);
  2918. return true;
  2919. }
  2920. case_end;
  2921. case_ast_node(pt, PointerType, e);
  2922. *type = alloc_type_pointer(check_type(ctx, pt->type));
  2923. set_base_type(named_type, *type);
  2924. return true;
  2925. case_end;
  2926. case_ast_node(rt, RelativeType, e);
  2927. GB_ASSERT(rt->tag->kind == Ast_CallExpr);
  2928. ast_node(ce, CallExpr, rt->tag);
  2929. Type *base_integer = nullptr;
  2930. if (ce->args.count != 1) {
  2931. error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count);
  2932. } else {
  2933. base_integer = check_type(ctx, ce->args[0]);
  2934. if (!is_type_integer(base_integer)) {
  2935. error(rt->type, "#relative base types must be an integer");
  2936. base_integer = nullptr;
  2937. } else if (type_size_of(base_integer) > 64) {
  2938. error(rt->type, "#relative base integer types be less than or equal to 64-bits");
  2939. base_integer = nullptr;
  2940. }
  2941. }
  2942. Type *relative_type = nullptr;
  2943. Type *base_type = check_type(ctx, rt->type);
  2944. if (!is_type_pointer(base_type) && !is_type_slice(base_type)) {
  2945. error(rt->type, "#relative types can only be a pointer or slice");
  2946. relative_type = base_type;
  2947. } else if (base_integer == nullptr) {
  2948. relative_type = base_type;
  2949. } else {
  2950. if (is_type_pointer(base_type)) {
  2951. relative_type = alloc_type_relative_pointer(base_type, base_integer);
  2952. } else if (is_type_slice(base_type)) {
  2953. relative_type = alloc_type_relative_slice(base_type, base_integer);
  2954. }
  2955. }
  2956. GB_ASSERT(relative_type != nullptr);
  2957. *type = relative_type;
  2958. set_base_type(named_type, *type);
  2959. return true;
  2960. case_end;
  2961. case_ast_node(ot, OpaqueType, e);
  2962. Type *elem = strip_opaque_type(check_type_expr(ctx, ot->type, nullptr));
  2963. *type = alloc_type_opaque(elem);
  2964. set_base_type(named_type, *type);
  2965. return true;
  2966. case_end;
  2967. case_ast_node(at, ArrayType, e);
  2968. if (at->count != nullptr) {
  2969. Operand o = {};
  2970. i64 count = check_array_count(ctx, &o, at->count);
  2971. Type *generic_type = nullptr;
  2972. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2973. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2974. generic_type = o.type;
  2975. } else if (o.mode == Addressing_Type && is_type_enum(o.type)) {
  2976. Type *index = o.type;
  2977. Type *bt = base_type(index);
  2978. GB_ASSERT(bt->kind == Type_Enum);
  2979. Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, Token_Invalid);
  2980. bool is_partial = false;
  2981. if (at->tag != nullptr) {
  2982. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2983. String name = at->tag->BasicDirective.name;
  2984. if (name == "partial") {
  2985. is_partial = true;
  2986. } else {
  2987. error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
  2988. }
  2989. }
  2990. if (!is_partial && t->EnumeratedArray.count > bt->Enum.fields.count) {
  2991. error(e, "Non-contiguous enumeration used as an index in an enumerated array");
  2992. long long ea_count = cast(long long)t->EnumeratedArray.count;
  2993. long long enum_count = cast(long long)t->Enum.fields.count;
  2994. error_line("\tenumerated array length: %lld\n", ea_count);
  2995. error_line("\tenum field count: %lld\n", enum_count);
  2996. error_line("\tSuggestion: prepend #partial to the enumerated array to allow for non-named elements\n");
  2997. if (2*enum_count < ea_count) {
  2998. error_line("\tWarning: the number of named elements is much smaller than the length of the array, are you sure this is what you want?\n");
  2999. error_line("\t this warning will be removed if #partial is applied\n");
  3000. }
  3001. }
  3002. *type = t;
  3003. goto array_end;
  3004. }
  3005. if (count < 0) {
  3006. error(at->count, "? can only be used in conjuction with compound literals");
  3007. count = 0;
  3008. }
  3009. if (at->tag != nullptr) {
  3010. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  3011. String name = at->tag->BasicDirective.name;
  3012. if (name == "soa") {
  3013. *type = make_soa_struct_fixed(ctx, e, at->elem, elem, count, generic_type);
  3014. } else if (name == "simd") {
  3015. if (!is_type_valid_vector_elem(elem)) {
  3016. gbString str = type_to_string(elem);
  3017. error(at->elem, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  3018. gb_string_free(str);
  3019. *type = alloc_type_array(elem, count, generic_type);
  3020. goto array_end;
  3021. }
  3022. *type = alloc_type_simd_vector(count, elem);
  3023. } else {
  3024. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  3025. *type = alloc_type_array(elem, count, generic_type);
  3026. }
  3027. } else {
  3028. *type = alloc_type_array(elem, count, generic_type);
  3029. }
  3030. } else {
  3031. Type *elem = check_type(ctx, at->elem);
  3032. if (at->tag != nullptr) {
  3033. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  3034. String name = at->tag->BasicDirective.name;
  3035. if (name == "soa") {
  3036. *type = make_soa_struct_slice(ctx, e, at->elem, elem);
  3037. } else {
  3038. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  3039. *type = alloc_type_slice(elem);
  3040. }
  3041. } else {
  3042. *type = alloc_type_slice(elem);
  3043. }
  3044. }
  3045. array_end:
  3046. set_base_type(named_type, *type);
  3047. return true;
  3048. case_end;
  3049. case_ast_node(dat, DynamicArrayType, e);
  3050. Type *elem = check_type(ctx, dat->elem);
  3051. if (dat->tag != nullptr) {
  3052. GB_ASSERT(dat->tag->kind == Ast_BasicDirective);
  3053. String name = dat->tag->BasicDirective.name;
  3054. if (name == "soa") {
  3055. *type = make_soa_struct_dynamic_array(ctx, e, dat->elem, elem);
  3056. } else {
  3057. error(dat->tag, "Invalid tag applied to dynamic array, got #%.*s", LIT(name));
  3058. *type = alloc_type_dynamic_array(elem);
  3059. }
  3060. } else {
  3061. *type = alloc_type_dynamic_array(elem);
  3062. }
  3063. set_base_type(named_type, *type);
  3064. return true;
  3065. case_end;
  3066. case_ast_node(st, StructType, e);
  3067. CheckerContext c = *ctx;
  3068. c.in_polymorphic_specialization = false;
  3069. c.type_level += 1;
  3070. *type = alloc_type_struct();
  3071. set_base_type(named_type, *type);
  3072. check_open_scope(&c, e);
  3073. check_struct_type(&c, *type, e, nullptr, named_type);
  3074. check_close_scope(&c);
  3075. (*type)->Struct.node = e;
  3076. return true;
  3077. case_end;
  3078. case_ast_node(ut, UnionType, e);
  3079. CheckerContext c = *ctx;
  3080. c.in_polymorphic_specialization = false;
  3081. c.type_level += 1;
  3082. *type = alloc_type_union();
  3083. set_base_type(named_type, *type);
  3084. check_open_scope(&c, e);
  3085. check_union_type(&c, *type, e, nullptr, named_type);
  3086. check_close_scope(&c);
  3087. (*type)->Union.node = e;
  3088. return true;
  3089. case_end;
  3090. case_ast_node(et, EnumType, e);
  3091. bool ips = ctx->in_polymorphic_specialization;
  3092. defer (ctx->in_polymorphic_specialization = ips);
  3093. ctx->in_polymorphic_specialization = false;
  3094. ctx->in_enum_type = true;
  3095. *type = alloc_type_enum();
  3096. set_base_type(named_type, *type);
  3097. check_open_scope(ctx, e);
  3098. check_enum_type(ctx, *type, named_type, e);
  3099. check_close_scope(ctx);
  3100. (*type)->Enum.node = e;
  3101. ctx->in_enum_type = false;
  3102. return true;
  3103. case_end;
  3104. case_ast_node(et, BitFieldType, e);
  3105. *type = alloc_type_bit_field();
  3106. set_base_type(named_type, *type);
  3107. check_open_scope(ctx, e);
  3108. check_bit_field_type(ctx, *type, e);
  3109. check_close_scope(ctx);
  3110. return true;
  3111. case_end;
  3112. case_ast_node(bs, BitSetType, e);
  3113. *type = alloc_type_bit_set();
  3114. set_base_type(named_type, *type);
  3115. check_bit_set_type(ctx, *type, named_type, e);
  3116. return true;
  3117. case_end;
  3118. case_ast_node(pt, ProcType, e);
  3119. bool ips = ctx->in_polymorphic_specialization;
  3120. defer (ctx->in_polymorphic_specialization = ips);
  3121. ctx->in_polymorphic_specialization = false;
  3122. *type = alloc_type(Type_Proc);
  3123. set_base_type(named_type, *type);
  3124. check_open_scope(ctx, e);
  3125. check_procedure_type(ctx, *type, e);
  3126. check_close_scope(ctx);
  3127. return true;
  3128. case_end;
  3129. case_ast_node(mt, MapType, e);
  3130. bool ips = ctx->in_polymorphic_specialization;
  3131. defer (ctx->in_polymorphic_specialization = ips);
  3132. ctx->in_polymorphic_specialization = false;
  3133. *type = alloc_type(Type_Map);
  3134. set_base_type(named_type, *type);
  3135. check_map_type(ctx, *type, e);
  3136. return true;
  3137. case_end;
  3138. case_ast_node(ce, CallExpr, e);
  3139. Operand o = {};
  3140. check_expr_or_type(ctx, &o, e);
  3141. if (o.mode == Addressing_Type) {
  3142. *type = o.type;
  3143. set_base_type(named_type, *type);
  3144. return true;
  3145. }
  3146. case_end;
  3147. case_ast_node(te, TernaryExpr, e);
  3148. Operand o = {};
  3149. check_expr_or_type(ctx, &o, e);
  3150. if (o.mode == Addressing_Type) {
  3151. *type = o.type;
  3152. set_base_type(named_type, *type);
  3153. return true;
  3154. }
  3155. case_end;
  3156. case_ast_node(te, TernaryIfExpr, e);
  3157. Operand o = {};
  3158. check_expr_or_type(ctx, &o, e);
  3159. if (o.mode == Addressing_Type) {
  3160. *type = o.type;
  3161. set_base_type(named_type, *type);
  3162. return true;
  3163. }
  3164. case_end;
  3165. case_ast_node(te, TernaryWhenExpr, e);
  3166. Operand o = {};
  3167. check_expr_or_type(ctx, &o, e);
  3168. if (o.mode == Addressing_Type) {
  3169. *type = o.type;
  3170. set_base_type(named_type, *type);
  3171. return true;
  3172. }
  3173. case_end;
  3174. }
  3175. *type = t_invalid;
  3176. return false;
  3177. }
  3178. Type *check_type(CheckerContext *ctx, Ast *e) {
  3179. CheckerContext c = *ctx;
  3180. c.type_path = new_checker_type_path();
  3181. defer (destroy_checker_type_path(c.type_path));
  3182. return check_type_expr(&c, e, nullptr);
  3183. }
  3184. Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  3185. Type *type = nullptr;
  3186. bool ok = check_type_internal(ctx, e, &type, named_type);
  3187. if (!ok) {
  3188. gbString err_str = expr_to_string(e);
  3189. error(e, "'%s' is not a type", err_str);
  3190. gb_string_free(err_str);
  3191. type = t_invalid;
  3192. }
  3193. if (type == nullptr) {
  3194. type = t_invalid;
  3195. }
  3196. if (type->kind == Type_Named &&
  3197. type->Named.base == nullptr) {
  3198. // IMPORTANT TODO(bill): Is this a serious error?!
  3199. #if 0
  3200. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  3201. #endif
  3202. type->Named.base = t_invalid;
  3203. }
  3204. if (is_type_polymorphic(type)) {
  3205. type->flags |= TypeFlag_Polymorphic;
  3206. } else if (is_type_polymorphic(type, true)) {
  3207. type->flags |= TypeFlag_PolySpecialized;
  3208. }
  3209. #if 0
  3210. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  3211. gbString str = type_to_string(type);
  3212. error(e, "Invalid use of a polymorphic type '%s'", str);
  3213. gb_string_free(str);
  3214. type = t_invalid;
  3215. }
  3216. #endif
  3217. if (is_type_typed(type)) {
  3218. add_type_and_value(&ctx->checker->info, e, Addressing_Type, type, empty_exact_value);
  3219. } else {
  3220. gbString name = type_to_string(type);
  3221. error(e, "Invalid type definition of %s", name);
  3222. gb_string_free(name);
  3223. type = t_invalid;
  3224. }
  3225. set_base_type(named_type, type);
  3226. return type;
  3227. }