check_type.cpp 104 KB

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