check_type.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031
  1. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location);
  2. void populate_using_array_index(CheckerContext *ctx, Ast *node, AstField *field, Type *t, String name, i32 idx) {
  3. t = base_type(t);
  4. GB_ASSERT(t->kind == Type_Array);
  5. Entity *e = scope_lookup_current(ctx->scope, name);
  6. if (e != nullptr) {
  7. gbString str = nullptr;
  8. defer (gb_string_free(str));
  9. if (node != nullptr) {
  10. str = expr_to_string(node);
  11. }
  12. if (str != nullptr) {
  13. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  14. } else {
  15. error(e->token, "'%.*s' is already declared", LIT(name));
  16. }
  17. } else {
  18. Token tok = make_token_ident(name);
  19. if (field->names.count > 0) {
  20. tok.pos = ast_token(field->names[0]).pos;
  21. } else {
  22. tok.pos = ast_token(field->type).pos;
  23. }
  24. Entity *f = alloc_entity_array_elem(nullptr, tok, t->Array.elem, idx);
  25. add_entity(ctx, ctx->scope, nullptr, f);
  26. }
  27. }
  28. void populate_using_entity_scope(CheckerContext *ctx, Ast *node, AstField *field, Type *t) {
  29. if (t == nullptr) {
  30. return;
  31. }
  32. t = base_type(type_deref(t));
  33. gbString str = nullptr;
  34. defer (gb_string_free(str));
  35. if (node != nullptr) {
  36. str = expr_to_string(node);
  37. }
  38. if (t->kind == Type_Struct) {
  39. for (Entity *f : t->Struct.fields) {
  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, 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_Array && t->Array.count <= 4) {
  58. switch (t->Array.count) {
  59. case 4:
  60. populate_using_array_index(ctx, node, field, t, str_lit("w"), 3);
  61. populate_using_array_index(ctx, node, field, t, str_lit("a"), 3);
  62. /*fallthrough*/
  63. case 3:
  64. populate_using_array_index(ctx, node, field, t, str_lit("z"), 2);
  65. populate_using_array_index(ctx, node, field, t, str_lit("b"), 2);
  66. /*fallthrough*/
  67. case 2:
  68. populate_using_array_index(ctx, node, field, t, str_lit("y"), 1);
  69. populate_using_array_index(ctx, node, field, t, str_lit("g"), 1);
  70. /*fallthrough*/
  71. case 1:
  72. populate_using_array_index(ctx, node, field, t, str_lit("x"), 0);
  73. populate_using_array_index(ctx, node, field, t, str_lit("r"), 0);
  74. /*fallthrough*/
  75. default:
  76. break;
  77. }
  78. }
  79. }
  80. bool does_field_type_allow_using(Type *t) {
  81. t = base_type(t);
  82. if (is_type_struct(t)) {
  83. return true;
  84. } else if (is_type_array(t)) {
  85. return t->Array.count <= 4;
  86. }
  87. return false;
  88. }
  89. void check_struct_fields(CheckerContext *ctx, Ast *node, Slice<Entity *> *fields, String **tags, Slice<Ast *> const &params,
  90. isize init_field_capacity, Type *struct_type, String context) {
  91. auto fields_array = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
  92. auto tags_array = array_make<String>(heap_allocator(), 0, init_field_capacity);
  93. GB_ASSERT(node->kind == Ast_StructType);
  94. GB_ASSERT(struct_type->kind == Type_Struct);
  95. isize variable_count = 0;
  96. for_array(i, params) {
  97. Ast *field = params[i];
  98. if (ast_node_expect(field, Ast_Field)) {
  99. ast_node(f, Field, field);
  100. variable_count += gb_max(f->names.count, 1);
  101. }
  102. }
  103. i32 field_src_index = 0;
  104. i32 field_group_index = -1;
  105. for_array(i, params) {
  106. Ast *param = params[i];
  107. if (param->kind != Ast_Field) {
  108. continue;
  109. }
  110. field_group_index += 1;
  111. ast_node(p, Field, param);
  112. Ast *type_expr = p->type;
  113. Type *type = nullptr;
  114. CommentGroup *docs = p->docs;
  115. CommentGroup *comment = p->comment;
  116. if (type_expr != nullptr) {
  117. type = check_type_expr(ctx, type_expr, nullptr);
  118. if (is_type_polymorphic(type)) {
  119. struct_type->Struct.is_polymorphic = true;
  120. type = nullptr;
  121. }
  122. }
  123. if (type == nullptr) {
  124. error(params[i], "Invalid parameter type");
  125. type = t_invalid;
  126. }
  127. if (is_type_untyped(type)) {
  128. if (is_type_untyped_undef(type)) {
  129. error(params[i], "Cannot determine parameter type from ---");
  130. } else {
  131. error(params[i], "Cannot determine parameter type from a nil");
  132. }
  133. type = t_invalid;
  134. }
  135. bool is_using = (p->flags&FieldFlag_using) != 0;
  136. bool is_subtype = (p->flags&FieldFlag_subtype) != 0;
  137. for_array(j, p->names) {
  138. Ast *name = p->names[j];
  139. if (!ast_node_expect2(name, Ast_Ident, Ast_PolyType)) {
  140. continue;
  141. }
  142. if (name->kind == Ast_PolyType) {
  143. name = name->PolyType.type;
  144. }
  145. Token name_token = name->Ident.token;
  146. Entity *field = alloc_entity_field(ctx->scope, name_token, type, is_using, field_src_index);
  147. add_entity(ctx, ctx->scope, name, field);
  148. field->Variable.field_group_index = field_group_index;
  149. if (is_subtype) {
  150. field->flags |= EntityFlag_Subtype;
  151. }
  152. if (j == 0) {
  153. field->Variable.docs = docs;
  154. }
  155. if (j+1 == p->names.count) {
  156. field->Variable.comment = comment;
  157. }
  158. array_add(&fields_array, field);
  159. String tag = p->tag.string;
  160. if (tag.len != 0 && !unquote_string(permanent_allocator(), &tag, 0, tag.text[0] == '`')) {
  161. error(p->tag, "Invalid string literal");
  162. tag = {};
  163. }
  164. array_add(&tags_array, tag);
  165. field_src_index += 1;
  166. }
  167. if (is_using && p->names.count > 0) {
  168. Type *first_type = fields_array[fields_array.count-1]->type;
  169. Type *t = base_type(type_deref(first_type));
  170. if (!does_field_type_allow_using(t) &&
  171. p->names.count >= 1 &&
  172. p->names[0]->kind == Ast_Ident) {
  173. Token name_token = p->names[0]->Ident.token;
  174. gbString type_str = type_to_string(first_type);
  175. error(name_token, "'using' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  176. gb_string_free(type_str);
  177. continue;
  178. }
  179. populate_using_entity_scope(ctx, node, p, type);
  180. }
  181. if (is_subtype && p->names.count > 0) {
  182. Type *first_type = fields_array[fields_array.count-1]->type;
  183. Type *t = base_type(type_deref(first_type));
  184. if (!does_field_type_allow_using(t) &&
  185. p->names.count >= 1 &&
  186. p->names[0]->kind == Ast_Ident) {
  187. Token name_token = p->names[0]->Ident.token;
  188. gbString type_str = type_to_string(first_type);
  189. error(name_token, "'subtype' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  190. gb_string_free(type_str);
  191. }
  192. }
  193. }
  194. *fields = slice_from_array(fields_array);
  195. *tags = tags_array.data;
  196. }
  197. bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) {
  198. GB_ASSERT(align_ != nullptr);
  199. Operand o = {};
  200. check_expr(ctx, &o, node);
  201. if (o.mode != Addressing_Constant) {
  202. if (o.mode != Addressing_Invalid) {
  203. error(node, "#align must be a constant");
  204. }
  205. return false;
  206. }
  207. Type *type = base_type(o.type);
  208. if (is_type_untyped(type) || is_type_integer(type)) {
  209. if (o.value.kind == ExactValue_Integer) {
  210. BigInt v = o.value.value_integer;
  211. if (v.used > 1) {
  212. gbAllocator a = heap_allocator();
  213. String str = big_int_to_string(a, &v);
  214. error(node, "#align too large, %.*s", LIT(str));
  215. gb_free(a, str.text);
  216. return false;
  217. }
  218. i64 align = big_int_to_i64(&v);
  219. if (align < 1 || !gb_is_power_of_two(cast(isize)align)) {
  220. error(node, "#align must be a power of 2, got %lld", align);
  221. return false;
  222. }
  223. *align_ = align;
  224. return true;
  225. }
  226. }
  227. error(node, "#align must be an integer");
  228. return false;
  229. }
  230. Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure) {
  231. mutex_lock(&ctx->info->gen_types_mutex);
  232. defer (mutex_unlock(&ctx->info->gen_types_mutex));
  233. auto *found_gen_types = map_get(&ctx->info->gen_types, original_type);
  234. if (found_gen_types != nullptr) {
  235. // GB_ASSERT_MSG(ordered_operands.count >= param_count, "%td >= %td", ordered_operands.count, param_count);
  236. for_array(i, *found_gen_types) {
  237. Entity *e = (*found_gen_types)[i];
  238. Type *t = base_type(e->type);
  239. TypeTuple *tuple = get_record_polymorphic_params(t);
  240. GB_ASSERT(param_count == tuple->variables.count);
  241. bool skip = false;
  242. for (isize j = 0; j < param_count; j++) {
  243. Entity *p = tuple->variables[j];
  244. Operand o = {};
  245. if (j < ordered_operands.count) {
  246. o = ordered_operands[j];
  247. }
  248. if (o.expr == nullptr) {
  249. continue;
  250. }
  251. Entity *oe = entity_of_node(o.expr);
  252. if (p == oe) {
  253. // NOTE(bill): This is the same type, make sure that it will be be same thing and use that
  254. // Saves on a lot of checking too below
  255. continue;
  256. }
  257. if (p->kind == Entity_TypeName) {
  258. if (is_type_polymorphic(o.type)) {
  259. // NOTE(bill): Do not add polymorphic version to the gen_types
  260. skip = true;
  261. break;
  262. }
  263. if (!are_types_identical(o.type, p->type)) {
  264. skip = true;
  265. break;
  266. }
  267. } else if (p->kind == Entity_Constant) {
  268. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  269. skip = true;
  270. break;
  271. }
  272. if (!are_types_identical(o.type, p->type)) {
  273. skip = true;
  274. break;
  275. }
  276. } else {
  277. GB_PANIC("Unknown entity kind");
  278. }
  279. }
  280. if (!skip) {
  281. return e;
  282. }
  283. }
  284. }
  285. return nullptr;
  286. }
  287. void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, Type *named_type, Type *original_type) {
  288. GB_ASSERT(is_type_named(named_type));
  289. gbAllocator a = heap_allocator();
  290. Scope *s = ctx->scope->parent;
  291. Entity *e = nullptr;
  292. {
  293. Token token = ast_token(node);
  294. token.kind = Token_String;
  295. token.string = named_type->Named.name;
  296. Ast *node = ast_ident(nullptr, token);
  297. e = alloc_entity_type_name(s, token, named_type);
  298. e->state = EntityState_Resolved;
  299. e->file = ctx->file;
  300. e->pkg = ctx->pkg;
  301. add_entity_use(ctx, node, e);
  302. }
  303. named_type->Named.type_name = e;
  304. GB_ASSERT(original_type->kind == Type_Named);
  305. e->TypeName.objc_class_name = original_type->Named.type_name->TypeName.objc_class_name;
  306. // TODO(bill): Is this even correct? Or should the metadata be copied?
  307. e->TypeName.objc_metadata = original_type->Named.type_name->TypeName.objc_metadata;
  308. mutex_lock(&ctx->info->gen_types_mutex);
  309. auto *found_gen_types = map_get(&ctx->info->gen_types, original_type);
  310. if (found_gen_types) {
  311. array_add(found_gen_types, e);
  312. } else {
  313. auto array = array_make<Entity *>(heap_allocator());
  314. array_add(&array, e);
  315. map_set(&ctx->info->gen_types, original_type, array);
  316. }
  317. mutex_unlock(&ctx->info->gen_types_mutex);
  318. }
  319. Type *check_record_polymorphic_params(CheckerContext *ctx, Ast *polymorphic_params,
  320. bool *is_polymorphic_,
  321. Ast *node, Array<Operand> *poly_operands,
  322. Type *named_type, Type *original_type_for_poly) {
  323. Type *polymorphic_params_type = nullptr;
  324. bool can_check_fields = true;
  325. GB_ASSERT(is_polymorphic_ != nullptr);
  326. if (polymorphic_params == nullptr) {
  327. if (!*is_polymorphic_) {
  328. *is_polymorphic_ = polymorphic_params != nullptr && poly_operands == nullptr;
  329. }
  330. return polymorphic_params_type;
  331. }
  332. ast_node(field_list, FieldList, polymorphic_params);
  333. Slice<Ast *> params = field_list->list;
  334. if (params.count != 0) {
  335. isize variable_count = 0;
  336. for_array(i, params) {
  337. Ast *field = params[i];
  338. if (ast_node_expect(field, Ast_Field)) {
  339. ast_node(f, Field, field);
  340. variable_count += gb_max(f->names.count, 1);
  341. }
  342. }
  343. auto entities = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  344. for_array(i, params) {
  345. Ast *param = params[i];
  346. if (param->kind != Ast_Field) {
  347. continue;
  348. }
  349. ast_node(p, Field, param);
  350. Ast *type_expr = p->type;
  351. Ast *default_value = unparen_expr(p->default_value);
  352. Type *type = nullptr;
  353. bool is_type_param = false;
  354. bool is_type_polymorphic_type = false;
  355. if (type_expr == nullptr && default_value == nullptr) {
  356. error(param, "Expected a type for this parameter");
  357. continue;
  358. }
  359. if (type_expr != nullptr) {
  360. if (type_expr->kind == Ast_Ellipsis) {
  361. type_expr = type_expr->Ellipsis.expr;
  362. error(param, "A polymorphic parameter cannot be variadic");
  363. }
  364. if (type_expr->kind == Ast_TypeidType) {
  365. is_type_param = true;
  366. Type *specialization = nullptr;
  367. if (type_expr->TypeidType.specialization != nullptr) {
  368. Ast *s = type_expr->TypeidType.specialization;
  369. specialization = check_type(ctx, s);
  370. }
  371. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  372. } else {
  373. type = check_type(ctx, type_expr);
  374. if (is_type_polymorphic(type)) {
  375. is_type_polymorphic_type = true;
  376. }
  377. }
  378. }
  379. ParameterValue param_value = {};
  380. if (default_value != nullptr) {
  381. Type *out_type = nullptr;
  382. param_value = handle_parameter_value(ctx, type, &out_type, default_value, false);
  383. if (type == nullptr && out_type != nullptr) {
  384. type = out_type;
  385. }
  386. if (param_value.kind != ParameterValue_Constant && param_value.kind != ParameterValue_Nil) {
  387. error(default_value, "Invalid parameter value");
  388. param_value = {};
  389. }
  390. }
  391. if (type == nullptr) {
  392. error(params[i], "Invalid parameter type");
  393. type = t_invalid;
  394. }
  395. if (is_type_untyped(type)) {
  396. if (is_type_untyped_undef(type)) {
  397. error(params[i], "Cannot determine parameter type from ---");
  398. } else {
  399. error(params[i], "Cannot determine parameter type from a nil");
  400. }
  401. type = t_invalid;
  402. }
  403. if (is_type_polymorphic_type) {
  404. gbString str = type_to_string(type);
  405. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  406. gb_string_free(str);
  407. type = t_invalid;
  408. }
  409. if (!is_type_param && !is_type_constant_type(type)) {
  410. gbString str = type_to_string(type);
  411. error(params[i], "A parameter must be a valid constant type, got %s", str);
  412. gb_string_free(str);
  413. }
  414. Scope *scope = ctx->scope;
  415. for_array(j, p->names) {
  416. Ast *name = p->names[j];
  417. if (!ast_node_expect2(name, Ast_Ident, Ast_PolyType)) {
  418. continue;
  419. }
  420. if (name->kind == Ast_PolyType) {
  421. name = name->PolyType.type;
  422. }
  423. Entity *e = nullptr;
  424. Token token = name->Ident.token;
  425. if (poly_operands != nullptr) {
  426. Operand operand = {};
  427. operand.type = t_invalid;
  428. if (entities.count < poly_operands->count) {
  429. operand = (*poly_operands)[entities.count];
  430. } else if (param_value.kind != ParameterValue_Invalid) {
  431. operand.mode = Addressing_Constant;
  432. operand.value = param_value.value;
  433. }
  434. if (is_type_param) {
  435. if (is_type_polymorphic(base_type(operand.type))) {
  436. *is_polymorphic_ = true;
  437. can_check_fields = false;
  438. }
  439. e = alloc_entity_type_name(scope, token, operand.type);
  440. e->TypeName.is_type_alias = true;
  441. e->flags |= EntityFlag_PolyConst;
  442. } else {
  443. if (is_type_polymorphic(base_type(operand.type))) {
  444. *is_polymorphic_ = true;
  445. can_check_fields = false;
  446. }
  447. if (e == nullptr) {
  448. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  449. e->Constant.param_value = param_value;
  450. }
  451. }
  452. } else {
  453. if (is_type_param) {
  454. e = alloc_entity_type_name(scope, token, type);
  455. e->TypeName.is_type_alias = true;
  456. e->flags |= EntityFlag_PolyConst;
  457. } else {
  458. e = alloc_entity_constant(scope, token, type, param_value.value);
  459. e->Constant.param_value = param_value;
  460. }
  461. }
  462. e->state = EntityState_Resolved;
  463. add_entity(ctx, scope, name, e);
  464. array_add(&entities, e);
  465. }
  466. }
  467. if (entities.count > 0) {
  468. Type *tuple = alloc_type_tuple();
  469. tuple->Tuple.variables = slice_from_array(entities);
  470. polymorphic_params_type = tuple;
  471. }
  472. }
  473. if (original_type_for_poly != nullptr) {
  474. GB_ASSERT(named_type != nullptr);
  475. add_polymorphic_record_entity(ctx, node, named_type, original_type_for_poly);
  476. }
  477. if (!*is_polymorphic_) {
  478. *is_polymorphic_ = polymorphic_params != nullptr && poly_operands == nullptr;
  479. }
  480. return polymorphic_params_type;
  481. }
  482. bool check_record_poly_operand_specialization(CheckerContext *ctx, Type *record_type, Array<Operand> *poly_operands, bool *is_polymorphic_) {
  483. if (poly_operands == nullptr) {
  484. return false;
  485. }
  486. for (isize i = 0; i < poly_operands->count; i++) {
  487. Operand o = (*poly_operands)[i];
  488. if (is_type_polymorphic(o.type)) {
  489. return false;
  490. }
  491. if (record_type == o.type) {
  492. // NOTE(bill): Cycle
  493. return false;
  494. }
  495. if (o.mode == Addressing_Type) {
  496. // NOTE(bill): ANNOYING EDGE CASE FOR `where` clauses
  497. // TODO(bill, 2021-03-27): Is this even a valid HACK?!
  498. Entity *entity = entity_of_node(o.expr);
  499. if (entity != nullptr &&
  500. entity->kind == Entity_TypeName &&
  501. entity->type == t_typeid) {
  502. *is_polymorphic_ = true;
  503. return false;
  504. }
  505. }
  506. }
  507. return true;
  508. }
  509. void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  510. GB_ASSERT(is_type_struct(struct_type));
  511. ast_node(st, StructType, node);
  512. String context = str_lit("struct");
  513. isize min_field_count = 0;
  514. for_array(field_index, st->fields) {
  515. Ast *field = st->fields[field_index];
  516. switch (field->kind) {
  517. case_ast_node(f, ValueDecl, field);
  518. min_field_count += f->names.count;
  519. case_end;
  520. case_ast_node(f, Field, field);
  521. min_field_count += f->names.count;
  522. case_end;
  523. }
  524. }
  525. scope_reserve(ctx->scope, min_field_count);
  526. if (st->is_raw_union && min_field_count > 1) {
  527. struct_type->Struct.is_raw_union = true;
  528. context = str_lit("struct #raw_union");
  529. }
  530. struct_type->Struct.scope = ctx->scope;
  531. struct_type->Struct.is_packed = st->is_packed;
  532. struct_type->Struct.polymorphic_params = check_record_polymorphic_params(
  533. ctx, st->polymorphic_params,
  534. &struct_type->Struct.is_polymorphic,
  535. node, poly_operands,
  536. named_type, original_type_for_poly
  537. );;
  538. struct_type->Struct.is_poly_specialized = check_record_poly_operand_specialization(ctx, struct_type, poly_operands, &struct_type->Struct.is_polymorphic);
  539. if (!struct_type->Struct.is_polymorphic) {
  540. if (st->where_clauses.count > 0 && st->polymorphic_params == nullptr) {
  541. error(st->where_clauses[0], "'where' clauses can only be used on structures with polymorphic parameters");
  542. } else {
  543. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &st->where_clauses, true);
  544. gb_unused(where_clause_ok);
  545. }
  546. check_struct_fields(ctx, node, &struct_type->Struct.fields, &struct_type->Struct.tags, st->fields, min_field_count, struct_type, context);
  547. }
  548. if (st->align != nullptr) {
  549. if (st->is_packed) {
  550. syntax_error(st->align, "'#align' cannot be applied with '#packed'");
  551. return;
  552. }
  553. i64 custom_align = 1;
  554. if (check_custom_align(ctx, st->align, &custom_align)) {
  555. struct_type->Struct.custom_align = custom_align;
  556. }
  557. }
  558. }
  559. void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  560. GB_ASSERT(is_type_union(union_type));
  561. ast_node(ut, UnionType, node);
  562. union_type->Union.scope = ctx->scope;
  563. union_type->Union.polymorphic_params = check_record_polymorphic_params(
  564. ctx, ut->polymorphic_params,
  565. &union_type->Union.is_polymorphic,
  566. node, poly_operands,
  567. named_type, original_type_for_poly
  568. );
  569. union_type->Union.is_poly_specialized = check_record_poly_operand_specialization(ctx, union_type, poly_operands, &union_type->Union.is_polymorphic);
  570. if (!union_type->Union.is_polymorphic) {
  571. if (ut->where_clauses.count > 0 && ut->polymorphic_params == nullptr) {
  572. error(ut->where_clauses[0], "'where' clauses can only be used on unions with polymorphic parameters");
  573. } else {
  574. bool where_clause_ok = evaluate_where_clauses(ctx, node, ctx->scope, &ut->where_clauses, true);
  575. gb_unused(where_clause_ok);
  576. }
  577. }
  578. auto variants = array_make<Type *>(permanent_allocator(), 0, ut->variants.count);
  579. for_array(i, ut->variants) {
  580. Ast *node = ut->variants[i];
  581. Type *t = check_type_expr(ctx, node, nullptr);
  582. if (t != nullptr && t != t_invalid) {
  583. bool ok = true;
  584. t = default_type(t);
  585. if (is_type_untyped(t) || is_type_empty_union(t)) {
  586. ok = false;
  587. gbString str = type_to_string(t);
  588. error(node, "Invalid variant type in union '%s'", str);
  589. gb_string_free(str);
  590. } else {
  591. for_array(j, variants) {
  592. if (are_types_identical(t, variants[j])) {
  593. ok = false;
  594. gbString str = type_to_string(t);
  595. error(node, "Duplicate variant type '%s'", str);
  596. gb_string_free(str);
  597. break;
  598. }
  599. }
  600. }
  601. if (ok) {
  602. array_add(&variants, t);
  603. }
  604. }
  605. }
  606. union_type->Union.variants = slice_from_array(variants);
  607. union_type->Union.no_nil = ut->no_nil;
  608. union_type->Union.maybe = ut->maybe;
  609. if (union_type->Union.no_nil) {
  610. if (variants.count < 2) {
  611. error(ut->align, "A union with #no_nil must have at least 2 variants");
  612. }
  613. }
  614. if (union_type->Union.maybe) {
  615. if (variants.count != 1) {
  616. error(ut->align, "A union with #maybe must have at 1 variant, got %lld", cast(long long)variants.count);
  617. }
  618. }
  619. if (ut->align != nullptr) {
  620. i64 custom_align = 1;
  621. if (check_custom_align(ctx, ut->align, &custom_align)) {
  622. if (variants.count == 0) {
  623. error(ut->align, "An empty union cannot have a custom alignment");
  624. } else {
  625. union_type->Union.custom_align = custom_align;
  626. }
  627. }
  628. }
  629. }
  630. void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast *node) {
  631. ast_node(et, EnumType, node);
  632. GB_ASSERT(is_type_enum(enum_type));
  633. Type *base_type = t_int;
  634. if (et->base_type != nullptr) {
  635. base_type = check_type(ctx, et->base_type);
  636. }
  637. if (base_type == nullptr || !is_type_integer(base_type)) {
  638. error(node, "Base type for enumeration must be an integer");
  639. return;
  640. }
  641. if (is_type_enum(base_type)) {
  642. error(node, "Base type for enumeration cannot be another enumeration");
  643. return;
  644. }
  645. if (is_type_integer_128bit(base_type)) {
  646. error(node, "Base type for enumeration cannot be a 128-bit integer");
  647. return;
  648. }
  649. // NOTE(bill): Must be up here for the 'check_init_constant' system
  650. enum_type->Enum.base_type = base_type;
  651. enum_type->Enum.scope = ctx->scope;
  652. auto fields = array_make<Entity *>(permanent_allocator(), 0, et->fields.count);
  653. Type *constant_type = enum_type;
  654. if (named_type != nullptr) {
  655. constant_type = named_type;
  656. }
  657. ExactValue iota = exact_value_i64(-1);
  658. ExactValue min_value = exact_value_i64(0);
  659. ExactValue max_value = exact_value_i64(0);
  660. isize min_value_index = 0;
  661. isize max_value_index = 0;
  662. bool min_value_set = false;
  663. bool max_value_set = false;
  664. scope_reserve(ctx->scope, et->fields.count);
  665. for_array(i, et->fields) {
  666. Ast *field = et->fields[i];
  667. Ast *ident = nullptr;
  668. Ast *init = nullptr;
  669. u32 entity_flags = 0;
  670. if (field->kind != Ast_EnumFieldValue) {
  671. error(field, "An enum field's name must be an identifier");
  672. continue;
  673. }
  674. ident = field->EnumFieldValue.name;
  675. init = field->EnumFieldValue.value;
  676. if (ident == nullptr || ident->kind != Ast_Ident) {
  677. error(field, "An enum field's name must be an identifier");
  678. continue;
  679. }
  680. CommentGroup *docs = field->EnumFieldValue.docs;
  681. CommentGroup *comment = field->EnumFieldValue.comment;
  682. String name = ident->Ident.token.string;
  683. if (init != nullptr) {
  684. Operand o = {};
  685. check_expr(ctx, &o, init);
  686. if (o.mode != Addressing_Constant) {
  687. error(init, "Enumeration value must be a constant");
  688. o.mode = Addressing_Invalid;
  689. }
  690. if (o.mode != Addressing_Invalid) {
  691. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  692. }
  693. if (o.mode != Addressing_Invalid) {
  694. iota = o.value;
  695. } else {
  696. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  697. }
  698. } else {
  699. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  700. entity_flags |= EntityConstantFlag_ImplicitEnumValue;
  701. }
  702. // NOTE(bill): Skip blank identifiers
  703. if (is_blank_ident(name)) {
  704. continue;
  705. } else if (name == "names") {
  706. error(field, "'names' is a reserved identifier for enumerations");
  707. continue;
  708. }
  709. if (min_value_set) {
  710. if (compare_exact_values(Token_Gt, min_value, iota)) {
  711. min_value_index = i;
  712. min_value = iota;
  713. }
  714. } else {
  715. min_value_index = i;
  716. min_value = iota;
  717. min_value_set = true;
  718. }
  719. if (max_value_set) {
  720. if (compare_exact_values(Token_Lt, max_value, iota)) {
  721. max_value_index = i;
  722. max_value = iota;
  723. }
  724. } else {
  725. max_value_index = i;
  726. max_value = iota;
  727. max_value_set = true;
  728. }
  729. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  730. e->identifier = ident;
  731. e->flags |= EntityFlag_Visited;
  732. e->state = EntityState_Resolved;
  733. e->Constant.flags |= entity_flags;
  734. e->Constant.docs = docs;
  735. e->Constant.comment = comment;
  736. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  737. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  738. } else {
  739. add_entity(ctx, ctx->scope, nullptr, e);
  740. array_add(&fields, e);
  741. // TODO(bill): Should I add a use for the enum value?
  742. add_entity_use(ctx, field, e);
  743. }
  744. }
  745. GB_ASSERT(fields.count <= et->fields.count);
  746. enum_type->Enum.fields = fields;
  747. *enum_type->Enum.min_value = min_value;
  748. *enum_type->Enum.max_value = max_value;
  749. enum_type->Enum.min_value_index = min_value_index;
  750. enum_type->Enum.max_value_index = max_value_index;
  751. }
  752. bool is_type_valid_bit_set_range(Type *t) {
  753. if (is_type_integer(t)) {
  754. return true;
  755. }
  756. if (is_type_rune(t)) {
  757. return true;
  758. }
  759. return false;
  760. }
  761. void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  762. ast_node(bs, BitSetType, node);
  763. GB_ASSERT(type->kind == Type_BitSet);
  764. type->BitSet.node = node;
  765. /* i64 const DEFAULT_BITS = cast(i64)(8*build_context.word_size); */
  766. i64 const MAX_BITS = 128;
  767. Ast *base = unparen_expr(bs->elem);
  768. if (is_ast_range(base)) {
  769. ast_node(be, BinaryExpr, base);
  770. Operand lhs = {};
  771. Operand rhs = {};
  772. check_expr(c, &lhs, be->left);
  773. check_expr(c, &rhs, be->right);
  774. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  775. return;
  776. }
  777. convert_to_typed(c, &lhs, rhs.type);
  778. if (lhs.mode == Addressing_Invalid) {
  779. return;
  780. }
  781. convert_to_typed(c, &rhs, lhs.type);
  782. if (rhs.mode == Addressing_Invalid) {
  783. return;
  784. }
  785. if (!are_types_identical(lhs.type, rhs.type)) {
  786. if (lhs.type != t_invalid &&
  787. rhs.type != t_invalid) {
  788. gbString xt = type_to_string(lhs.type);
  789. gbString yt = type_to_string(rhs.type);
  790. gbString expr_str = expr_to_string(bs->elem);
  791. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  792. gb_string_free(expr_str);
  793. gb_string_free(yt);
  794. gb_string_free(xt);
  795. }
  796. return;
  797. }
  798. if (!is_type_valid_bit_set_range(lhs.type)) {
  799. gbString str = type_to_string(lhs.type);
  800. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  801. gb_string_free(str);
  802. return;
  803. }
  804. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  805. error(bs->elem, "Intervals must be constant values");
  806. return;
  807. }
  808. ExactValue iv = exact_value_to_integer(lhs.value);
  809. ExactValue jv = exact_value_to_integer(rhs.value);
  810. GB_ASSERT(iv.kind == ExactValue_Integer);
  811. GB_ASSERT(jv.kind == ExactValue_Integer);
  812. BigInt i = iv.value_integer;
  813. BigInt j = jv.value_integer;
  814. if (big_int_cmp(&i, &j) > 0) {
  815. gbAllocator a = heap_allocator();
  816. String si = big_int_to_string(a, &i);
  817. String sj = big_int_to_string(a, &j);
  818. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  819. gb_free(a, si.text);
  820. gb_free(a, sj.text);
  821. return;
  822. }
  823. Type *t = default_type(lhs.type);
  824. if (bs->underlying != nullptr) {
  825. Type *u = check_type(c, bs->underlying);
  826. if (!is_type_integer(u)) {
  827. gbString ts = type_to_string(u);
  828. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  829. gb_string_free(ts);
  830. return;
  831. }
  832. type->BitSet.underlying = u;
  833. }
  834. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  835. gbAllocator a = heap_allocator();
  836. String s = big_int_to_string(a, &i);
  837. gbString ts = type_to_string(t);
  838. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  839. gb_string_free(ts);
  840. gb_free(a, s.text);
  841. return;
  842. }
  843. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  844. gbAllocator a = heap_allocator();
  845. String s = big_int_to_string(a, &j);
  846. gbString ts = type_to_string(t);
  847. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  848. gb_string_free(ts);
  849. gb_free(a, s.text);
  850. return;
  851. }
  852. i64 lower = big_int_to_i64(&i);
  853. i64 upper = big_int_to_i64(&j);
  854. i64 actual_lower = lower;
  855. i64 bits = MAX_BITS;
  856. if (type->BitSet.underlying != nullptr) {
  857. bits = 8*type_size_of(type->BitSet.underlying);
  858. if (lower > 0) {
  859. actual_lower = 0;
  860. } else if (lower < 0) {
  861. error(bs->elem, "bit_set does not allow a negative lower bound (%lld) when an underlying type is set", lower);
  862. }
  863. }
  864. i64 bits_required = upper-actual_lower;
  865. switch (be->op.kind) {
  866. case Token_Ellipsis:
  867. case Token_RangeFull:
  868. bits_required += 1;
  869. break;
  870. }
  871. bool is_valid = true;
  872. switch (be->op.kind) {
  873. case Token_Ellipsis:
  874. case Token_RangeFull:
  875. if (upper - lower >= bits) {
  876. is_valid = false;
  877. }
  878. break;
  879. case Token_RangeHalf:
  880. if (upper - lower > bits) {
  881. is_valid = false;
  882. }
  883. upper -= 1;
  884. break;
  885. }
  886. if (!is_valid) {
  887. if (actual_lower != lower) {
  888. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required (internal the lower changed was changed 0 as an underlying type was set)", bits, bits_required);
  889. } else {
  890. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  891. }
  892. }
  893. type->BitSet.elem = t;
  894. type->BitSet.lower = lower;
  895. type->BitSet.upper = upper;
  896. } else {
  897. Type *elem = check_type_expr(c, bs->elem, nullptr);
  898. #if 0
  899. if (named_type != nullptr && named_type->kind == Type_Named &&
  900. elem->kind == Type_Enum) {
  901. // NOTE(bill): Anonymous enumeration
  902. String prefix = named_type->Named.name;
  903. String enum_name = concatenate_strings(heap_allocator(), prefix, str_lit(".enum"));
  904. Token token = make_token_ident(enum_name);
  905. Entity *e = alloc_entity_type_name(nullptr, token, nullptr, EntityState_Resolved);
  906. Type *named = alloc_type_named(enum_name, elem, e);
  907. e->type = named;
  908. e->TypeName.is_type_alias = true;
  909. elem = named;
  910. }
  911. #endif
  912. type->BitSet.elem = elem;
  913. if (!is_type_valid_bit_set_elem(elem)) {
  914. error(bs->elem, "Expected an enum type for a bit_set");
  915. } else {
  916. Type *et = base_type(elem);
  917. if (et->kind == Type_Enum) {
  918. if (!is_type_integer(et->Enum.base_type)) {
  919. error(bs->elem, "Enum type for bit_set must be an integer");
  920. return;
  921. }
  922. i64 lower = I64_MAX;
  923. i64 upper = I64_MIN;
  924. for_array(i, et->Enum.fields) {
  925. Entity *e = et->Enum.fields[i];
  926. if (e->kind != Entity_Constant) {
  927. continue;
  928. }
  929. ExactValue value = exact_value_to_integer(e->Constant.value);
  930. GB_ASSERT(value.kind == ExactValue_Integer);
  931. // NOTE(bill): enum types should be able to store i64 values
  932. i64 x = big_int_to_i64(&value.value_integer);
  933. lower = gb_min(lower, x);
  934. upper = gb_max(upper, x);
  935. }
  936. if (et->Enum.fields.count == 0) {
  937. lower = 0;
  938. upper = 0;
  939. }
  940. GB_ASSERT(lower <= upper);
  941. bool lower_changed = false;
  942. i64 bits = MAX_BITS
  943. ; if (bs->underlying != nullptr) {
  944. Type *u = check_type(c, bs->underlying);
  945. if (!is_type_integer(u)) {
  946. gbString ts = type_to_string(u);
  947. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  948. gb_string_free(ts);
  949. return;
  950. }
  951. type->BitSet.underlying = u;
  952. bits = 8*type_size_of(u);
  953. if (lower > 0) {
  954. lower = 0;
  955. lower_changed = true;
  956. } else if (lower < 0) {
  957. gbString s = type_to_string(elem);
  958. error(bs->elem, "bit_set does not allow a negative lower bound (%lld) of the element type '%s' when an underlying type is set", lower, s);
  959. gb_string_free(s);
  960. }
  961. }
  962. if (upper - lower >= bits) {
  963. i64 bits_required = upper-lower+1;
  964. if (lower_changed) {
  965. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required (internal the lower changed was changed 0 as an underlying type was set)", bits, bits_required);
  966. } else {
  967. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, bits_required);
  968. }
  969. }
  970. type->BitSet.lower = lower;
  971. type->BitSet.upper = upper;
  972. }
  973. }
  974. }
  975. }
  976. bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  977. if (type == nullptr ||
  978. type == t_invalid) {
  979. return true;
  980. }
  981. Type *t = base_type(type);
  982. Type *s = base_type(specialization);
  983. if (t->kind != s->kind) {
  984. if (t->kind == Type_EnumeratedArray && s->kind == Type_Array) {
  985. // Might be okay, check later
  986. } else {
  987. return false;
  988. }
  989. }
  990. if (is_type_untyped(t)) {
  991. Operand o = {Addressing_Value};
  992. o.type = default_type(type);
  993. bool can_convert = check_cast_internal(ctx, &o, specialization);
  994. return can_convert;
  995. } else if (t->kind == Type_Struct) {
  996. if (t->Struct.polymorphic_parent == specialization) {
  997. return true;
  998. }
  999. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  1000. s->Struct.polymorphic_params != nullptr &&
  1001. t->Struct.polymorphic_params != nullptr) {
  1002. TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
  1003. TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
  1004. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1005. for_array(i, s_tuple->variables) {
  1006. Entity *s_e = s_tuple->variables[i];
  1007. Entity *t_e = t_tuple->variables[i];
  1008. Type *st = s_e->type;
  1009. Type *tt = t_e->type;
  1010. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1011. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1012. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1013. GB_ASSERT(e != nullptr);
  1014. if (modify_type) {
  1015. e->kind = Entity_Constant;
  1016. e->Constant.value = t_e->Constant.value;
  1017. e->type = t_e->type;
  1018. }
  1019. } else {
  1020. if (st->kind == Type_Basic && tt->kind == Type_Basic &&
  1021. s_e->kind == Entity_Constant && t_e->kind == Entity_Constant) {
  1022. if (!compare_exact_values(Token_CmpEq, s_e->Constant.value, t_e->Constant.value))
  1023. return false;
  1024. } else {
  1025. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1026. if (!ok) {
  1027. // TODO(bill, 2021-08-19): is this logic correct?
  1028. return false;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. if (modify_type) {
  1034. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1035. gb_memmove(specialization, type, gb_size_of(Type));
  1036. }
  1037. return true;
  1038. }
  1039. } else if (t->kind == Type_Union) {
  1040. if (t->Union.polymorphic_parent == specialization) {
  1041. return true;
  1042. }
  1043. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1044. s->Union.polymorphic_params != nullptr &&
  1045. t->Union.polymorphic_params != nullptr) {
  1046. TypeTuple *s_tuple = &s->Union.polymorphic_params->Tuple;
  1047. TypeTuple *t_tuple = &t->Union.polymorphic_params->Tuple;
  1048. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1049. for_array(i, s_tuple->variables) {
  1050. Entity *s_e = s_tuple->variables[i];
  1051. Entity *t_e = t_tuple->variables[i];
  1052. Type *st = s_e->type;
  1053. Type *tt = t_e->type;
  1054. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1055. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1056. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1057. GB_ASSERT(e != nullptr);
  1058. if (modify_type) {
  1059. e->kind = Entity_Constant;
  1060. e->Constant.value = t_e->Constant.value;
  1061. e->type = t_e->type;
  1062. }
  1063. } else {
  1064. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1065. if (!ok) {
  1066. // TODO(bill, 2021-08-19): is this logic correct?
  1067. return false;
  1068. }
  1069. }
  1070. }
  1071. if (modify_type) {
  1072. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1073. gb_memmove(specialization, type, gb_size_of(Type));
  1074. }
  1075. return true;
  1076. }
  1077. }
  1078. if (specialization->kind == Type_Named &&
  1079. type->kind != Type_Named) {
  1080. return false;
  1081. }
  1082. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1083. return true;
  1084. }
  1085. return false;
  1086. }
  1087. Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand operand) {
  1088. bool modify_type = !ctx->no_polymorphic_errors;
  1089. bool show_error = modify_type && !ctx->hide_polymorphic_errors;
  1090. if (!is_operand_value(operand)) {
  1091. if (show_error) {
  1092. gbString pts = type_to_string(poly_type);
  1093. gbString ots = type_to_string(operand.type);
  1094. defer (gb_string_free(pts));
  1095. defer (gb_string_free(ots));
  1096. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1097. }
  1098. return t_invalid;
  1099. }
  1100. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1101. return poly_type;
  1102. }
  1103. if (show_error) {
  1104. gbString pts = type_to_string(poly_type);
  1105. gbString ots = type_to_string(operand.type);
  1106. defer (gb_string_free(pts));
  1107. defer (gb_string_free(ots));
  1108. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1109. }
  1110. return t_invalid;
  1111. }
  1112. bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1113. if (expr == nullptr) {
  1114. return false;
  1115. }
  1116. expr = unparen_expr(expr);
  1117. if (expr->kind == Ast_SelectorExpr) {
  1118. Ast *lhs = expr->SelectorExpr.expr;
  1119. return is_expr_from_a_parameter(ctx, lhs);
  1120. } else if (expr->kind == Ast_Ident) {
  1121. Operand x= {};
  1122. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1123. if (e->flags & EntityFlag_Param) {
  1124. return true;
  1125. }
  1126. }
  1127. return false;
  1128. }
  1129. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1130. ParameterValue param_value = {};
  1131. param_value.original_ast_expr = expr;
  1132. if (expr == nullptr) {
  1133. return param_value;
  1134. }
  1135. Operand o = {};
  1136. if (allow_caller_location &&
  1137. expr->kind == Ast_BasicDirective &&
  1138. expr->BasicDirective.name.string == "caller_location") {
  1139. init_core_source_code_location(ctx->checker);
  1140. param_value.kind = ParameterValue_Location;
  1141. o.type = t_source_code_location;
  1142. if (in_type) {
  1143. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1144. }
  1145. } else {
  1146. if (in_type) {
  1147. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1148. } else {
  1149. check_expr(ctx, &o, expr);
  1150. }
  1151. if (in_type) {
  1152. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1153. }
  1154. if (is_operand_nil(o)) {
  1155. param_value.kind = ParameterValue_Nil;
  1156. } else if (o.mode != Addressing_Constant) {
  1157. if (expr->kind == Ast_ProcLit) {
  1158. param_value.kind = ParameterValue_Constant;
  1159. param_value.value = exact_value_procedure(expr);
  1160. } else {
  1161. Entity *e = entity_from_expr(o.expr);
  1162. if (e != nullptr) {
  1163. if (e->kind == Entity_Procedure) {
  1164. param_value.kind = ParameterValue_Constant;
  1165. param_value.value = exact_value_procedure(e->identifier);
  1166. add_entity_use(ctx, e->identifier, e);
  1167. } else {
  1168. if (e->flags & EntityFlag_Param) {
  1169. error(expr, "Default parameter cannot be another parameter");
  1170. } else {
  1171. if (is_expr_from_a_parameter(ctx, expr)) {
  1172. error(expr, "Default parameter cannot be another parameter");
  1173. } else {
  1174. param_value.kind = ParameterValue_Value;
  1175. param_value.ast_value = expr;
  1176. add_entity_use(ctx, e->identifier, e);
  1177. }
  1178. }
  1179. }
  1180. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1181. param_value.kind = ParameterValue_Value;
  1182. param_value.ast_value = expr;
  1183. } else if (o.value.kind != ExactValue_Invalid) {
  1184. param_value.kind = ParameterValue_Constant;
  1185. param_value.value = o.value;
  1186. } else {
  1187. error(expr, "Default parameter must be a constant, %d", o.mode);
  1188. }
  1189. }
  1190. } else {
  1191. if (o.value.kind != ExactValue_Invalid) {
  1192. param_value.kind = ParameterValue_Constant;
  1193. param_value.value = o.value;
  1194. } else {
  1195. gbString s = expr_to_string(o.expr);
  1196. error(o.expr, "Invalid constant parameter, got '%s'", s);
  1197. // error(o.expr, "Invalid constant parameter, got '%s' %d %d", s, o.mode, o.value.kind);
  1198. gb_string_free(s);
  1199. }
  1200. }
  1201. }
  1202. if (out_type_) {
  1203. if (in_type != nullptr) {
  1204. *out_type_ = in_type;
  1205. } else {
  1206. *out_type_ = default_type(o.type);
  1207. }
  1208. }
  1209. return param_value;
  1210. }
  1211. Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> *operands) {
  1212. if (_params == nullptr) {
  1213. return nullptr;
  1214. }
  1215. bool success = true;
  1216. ast_node(field_list, FieldList, _params);
  1217. Slice<Ast *> params = field_list->list;
  1218. if (params.count == 0) {
  1219. if (success_) *success_ = success;
  1220. return nullptr;
  1221. }
  1222. isize variable_count = 0;
  1223. for_array(i, params) {
  1224. Ast *field = params[i];
  1225. if (ast_node_expect(field, Ast_Field)) {
  1226. ast_node(f, Field, field);
  1227. variable_count += gb_max(f->names.count, 1);
  1228. }
  1229. }
  1230. isize min_variable_count = variable_count;
  1231. for (isize i = params.count-1; i >= 0; i--) {
  1232. Ast *field = params[i];
  1233. if (field->kind == Ast_Field) {
  1234. ast_node(f, Field, field);
  1235. if (f->default_value == nullptr) {
  1236. break;
  1237. }
  1238. min_variable_count--;
  1239. }
  1240. }
  1241. bool is_variadic = false;
  1242. isize variadic_index = -1;
  1243. bool is_c_vararg = false;
  1244. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1245. i32 field_group_index = -1;
  1246. for_array(i, params) {
  1247. Ast *param = params[i];
  1248. if (param->kind != Ast_Field) {
  1249. continue;
  1250. }
  1251. field_group_index += 1;
  1252. ast_node(p, Field, param);
  1253. Ast *type_expr = unparen_expr(p->type);
  1254. Type *type = nullptr;
  1255. Ast *default_value = unparen_expr(p->default_value);
  1256. ParameterValue param_value = {};
  1257. bool is_type_param = false;
  1258. bool is_type_polymorphic_type = false;
  1259. bool detemine_type_from_operand = false;
  1260. Type *specialization = nullptr;
  1261. bool is_using = (p->flags&FieldFlag_using) != 0;
  1262. if (type_expr == nullptr) {
  1263. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1264. } else {
  1265. if (type_expr->kind == Ast_Ellipsis) {
  1266. type_expr = type_expr->Ellipsis.expr;
  1267. is_variadic = true;
  1268. variadic_index = variables.count;
  1269. if (p->names.count != 1) {
  1270. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1271. success = false;
  1272. }
  1273. }
  1274. if (type_expr->kind == Ast_TypeidType) {
  1275. ast_node(tt, TypeidType, type_expr);
  1276. if (tt->specialization) {
  1277. specialization = check_type(ctx, tt->specialization);
  1278. if (specialization == t_invalid){
  1279. specialization = nullptr;
  1280. }
  1281. if (operands != nullptr) {
  1282. detemine_type_from_operand = true;
  1283. type = t_invalid;
  1284. } else {
  1285. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1286. }
  1287. } else {
  1288. type = t_typeid;
  1289. }
  1290. } else {
  1291. bool prev = ctx->allow_polymorphic_types;
  1292. if (operands != nullptr) {
  1293. ctx->allow_polymorphic_types = true;
  1294. }
  1295. type = check_type(ctx, type_expr);
  1296. ctx->allow_polymorphic_types = prev;
  1297. if (is_type_polymorphic(type)) {
  1298. is_type_polymorphic_type = true;
  1299. }
  1300. }
  1301. if (default_value != nullptr) {
  1302. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1303. error(type_expr, "A type parameter may not have a default value");
  1304. } else {
  1305. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1306. }
  1307. }
  1308. }
  1309. if (type == nullptr) {
  1310. error(param, "Invalid parameter type");
  1311. type = t_invalid;
  1312. }
  1313. if (is_type_untyped(type)) {
  1314. if (is_type_untyped_undef(type)) {
  1315. error(param, "Cannot determine parameter type from ---");
  1316. } else {
  1317. error(param, "Cannot determine parameter type from a nil");
  1318. }
  1319. type = t_invalid;
  1320. }
  1321. if (is_type_empty_union(type)) {
  1322. gbString str = type_to_string(type);
  1323. error(param, "Invalid use of an empty union '%s'", str);
  1324. gb_string_free(str);
  1325. type = t_invalid;
  1326. }
  1327. if (is_type_polymorphic(type)) {
  1328. switch (param_value.kind) {
  1329. case ParameterValue_Invalid:
  1330. case ParameterValue_Constant:
  1331. case ParameterValue_Nil:
  1332. break;
  1333. case ParameterValue_Location:
  1334. case ParameterValue_Value:
  1335. gbString str = type_to_string(type);
  1336. error(params[i], "A default value for a parameter must not be a polymorphic constant type, got %s", str);
  1337. gb_string_free(str);
  1338. break;
  1339. }
  1340. }
  1341. if (p->flags&FieldFlag_c_vararg) {
  1342. if (p->type == nullptr ||
  1343. p->type->kind != Ast_Ellipsis) {
  1344. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1345. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1346. } else {
  1347. is_c_vararg = true;
  1348. }
  1349. }
  1350. for_array(j, p->names) {
  1351. Ast *name = p->names[j];
  1352. bool is_poly_name = false;
  1353. switch (name->kind) {
  1354. case Ast_Ident:
  1355. break;
  1356. case Ast_PolyType:
  1357. GB_ASSERT(name->PolyType.specialization == nullptr);
  1358. is_poly_name = true;
  1359. name = name->PolyType.type;
  1360. break;
  1361. }
  1362. if (!ast_node_expect(name, Ast_Ident)) {
  1363. continue;
  1364. }
  1365. if (is_poly_name) {
  1366. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1367. is_type_param = true;
  1368. } else {
  1369. if (param_value.kind != ParameterValue_Invalid) {
  1370. error(default_value, "Constant parameters cannot have a default value");
  1371. param_value.kind = ParameterValue_Invalid;
  1372. }
  1373. }
  1374. }
  1375. Entity *param = nullptr;
  1376. if (is_type_param) {
  1377. if (operands != nullptr) {
  1378. Operand o = (*operands)[variables.count];
  1379. if (o.mode == Addressing_Type) {
  1380. type = o.type;
  1381. } else {
  1382. if (!ctx->no_polymorphic_errors) {
  1383. error(o.expr, "Expected a type to assign to the type parameter");
  1384. }
  1385. success = false;
  1386. type = t_invalid;
  1387. }
  1388. if (is_type_polymorphic(type)) {
  1389. gbString str = type_to_string(type);
  1390. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1391. gb_string_free(str);
  1392. success = false;
  1393. type = t_invalid;
  1394. }
  1395. if (is_type_untyped(default_type(type))) {
  1396. gbString str = type_to_string(type);
  1397. error(o.expr, "Cannot determine type from the parameter, got '%s'", str);
  1398. gb_string_free(str);
  1399. success = false;
  1400. type = t_invalid;
  1401. }
  1402. bool modify_type = !ctx->no_polymorphic_errors;
  1403. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1404. if (!ctx->no_polymorphic_errors) {
  1405. gbString t = type_to_string(type);
  1406. gbString s = type_to_string(specialization);
  1407. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1408. gb_string_free(s);
  1409. gb_string_free(t);
  1410. }
  1411. success = false;
  1412. type = t_invalid;
  1413. }
  1414. }
  1415. if (p->flags&FieldFlag_auto_cast) {
  1416. error(name, "'auto_cast' can only be applied to variable fields");
  1417. p->flags &= ~FieldFlag_auto_cast;
  1418. }
  1419. if (p->flags&FieldFlag_const) {
  1420. error(name, "'#const' can only be applied to variable fields");
  1421. p->flags &= ~FieldFlag_const;
  1422. }
  1423. if (p->flags&FieldFlag_any_int) {
  1424. error(name, "'#any_int' can only be applied to variable fields");
  1425. p->flags &= ~FieldFlag_any_int;
  1426. }
  1427. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1428. param->TypeName.is_type_alias = true;
  1429. } else {
  1430. ExactValue poly_const = {};
  1431. if (operands != nullptr && variables.count < operands->count) {
  1432. Operand op = (*operands)[variables.count];
  1433. if (op.expr == nullptr) {
  1434. // NOTE(bill): 2019-03-30
  1435. // This is just to add the error message to determine_type_from_polymorphic which
  1436. // depends on valid position information
  1437. op.expr = _params;
  1438. }
  1439. if (is_type_polymorphic_type) {
  1440. type = determine_type_from_polymorphic(ctx, type, op);
  1441. if (type == t_invalid) {
  1442. success = false;
  1443. } else if (!ctx->no_polymorphic_errors) {
  1444. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1445. is_type_polymorphic_type = false;
  1446. }
  1447. }
  1448. if (is_poly_name) {
  1449. bool valid = false;
  1450. if (is_type_proc(op.type)) {
  1451. Entity *proc_entity = entity_from_expr(op.expr);
  1452. valid = proc_entity != nullptr;
  1453. poly_const = exact_value_procedure(proc_entity->identifier.load() ? proc_entity->identifier.load() : op.expr);
  1454. }
  1455. if (!valid) {
  1456. if (op.mode == Addressing_Constant) {
  1457. poly_const = op.value;
  1458. } else {
  1459. error(op.expr, "Expected a constant value for this polymorphic name parameter");
  1460. success = false;
  1461. }
  1462. }
  1463. }
  1464. if (type != t_invalid && !check_is_assignable_to(ctx, &op, type)) {
  1465. bool ok = true;
  1466. if (p->flags&FieldFlag_auto_cast) {
  1467. if (!check_is_castable_to(ctx, &op, type)) {
  1468. ok = false;
  1469. }
  1470. } else if (p->flags&FieldFlag_any_int) {
  1471. if ((!is_type_integer(op.type) && !is_type_enum(op.type)) || (!is_type_integer(type) && !is_type_enum(type))) {
  1472. ok = false;
  1473. } else if (!check_is_castable_to(ctx, &op, type)) {
  1474. ok = false;
  1475. }
  1476. }
  1477. if (!ok) {
  1478. success = false;
  1479. #if 0
  1480. gbString got = type_to_string(op.type);
  1481. gbString expected = type_to_string(type);
  1482. error(op.expr, "Cannot assigned type to parameter, got type '%s', expected '%s'", got, expected);
  1483. gb_string_free(expected);
  1484. gb_string_free(got);
  1485. #endif
  1486. }
  1487. }
  1488. if (is_type_untyped(default_type(type))) {
  1489. gbString str = type_to_string(type);
  1490. error(op.expr, "Cannot determine type from the parameter, got '%s'", str);
  1491. gb_string_free(str);
  1492. success = false;
  1493. type = t_invalid;
  1494. }
  1495. }
  1496. if (p->flags&FieldFlag_no_alias) {
  1497. if (!is_type_pointer(type)) {
  1498. error(name, "'#no_alias' can only be applied to fields of pointer type");
  1499. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1500. }
  1501. }
  1502. if (is_poly_name) {
  1503. if (p->flags&FieldFlag_no_alias) {
  1504. error(name, "'#no_alias' can only be applied to non constant values");
  1505. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1506. }
  1507. if (p->flags&FieldFlag_auto_cast) {
  1508. error(name, "'auto_cast' can only be applied to variable fields");
  1509. p->flags &= ~FieldFlag_auto_cast;
  1510. }
  1511. if (p->flags&FieldFlag_any_int) {
  1512. error(name, "'#any_int' can only be applied to variable fields");
  1513. p->flags &= ~FieldFlag_any_int;
  1514. }
  1515. if (p->flags&FieldFlag_const) {
  1516. error(name, "'#const' can only be applied to variable fields");
  1517. p->flags &= ~FieldFlag_const;
  1518. }
  1519. if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
  1520. gbString str = type_to_string(type);
  1521. error(params[i], "A parameter must be a valid constant type, got %s", str);
  1522. gb_string_free(str);
  1523. }
  1524. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1525. param->Constant.field_group_index = field_group_index;
  1526. } else {
  1527. param = alloc_entity_param(scope, name->Ident.token, type, is_using, true);
  1528. param->Variable.param_value = param_value;
  1529. param->Variable.field_group_index = field_group_index;
  1530. }
  1531. }
  1532. if (p->flags&FieldFlag_no_alias) {
  1533. param->flags |= EntityFlag_NoAlias;
  1534. }
  1535. if (p->flags&FieldFlag_auto_cast) {
  1536. param->flags |= EntityFlag_AutoCast;
  1537. }
  1538. if (p->flags&FieldFlag_any_int) {
  1539. if (!is_type_integer(param->type) && !is_type_enum(param->type)) {
  1540. gbString str = type_to_string(param->type);
  1541. error(name, "A parameter with '#any_int' must be an integer, got %s", str);
  1542. gb_string_free(str);
  1543. }
  1544. param->flags |= EntityFlag_AnyInt;
  1545. }
  1546. if (p->flags&FieldFlag_const) {
  1547. param->flags |= EntityFlag_ConstInput;
  1548. }
  1549. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1550. add_entity(ctx, scope, name, param);
  1551. if (is_using) {
  1552. add_entity_use(ctx, name, param);
  1553. }
  1554. array_add(&variables, param);
  1555. }
  1556. }
  1557. if (is_variadic) {
  1558. GB_ASSERT(variadic_index >= 0);
  1559. }
  1560. if (is_variadic) {
  1561. GB_ASSERT(params.count > 0);
  1562. // NOTE(bill): Change last variadic parameter to be a slice
  1563. // Custom Calling convention for variadic parameters
  1564. Entity *end = variables[variadic_index];
  1565. end->type = alloc_type_slice(end->type);
  1566. end->flags |= EntityFlag_Ellipsis;
  1567. if (is_c_vararg) {
  1568. end->flags |= EntityFlag_CVarArg;
  1569. }
  1570. }
  1571. isize specialization_count = 0;
  1572. if (scope != nullptr) {
  1573. for_array(i, scope->elements.entries) {
  1574. Entity *e = scope->elements.entries[i].value;
  1575. if (e->kind == Entity_TypeName) {
  1576. Type *t = e->type;
  1577. if (t->kind == Type_Generic &&
  1578. t->Generic.specialized != nullptr) {
  1579. specialization_count += 1;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. Type *tuple = alloc_type_tuple();
  1585. tuple->Tuple.variables = slice_from_array(variables);
  1586. if (success_) *success_ = success;
  1587. if (specialization_count_) *specialization_count_ = specialization_count;
  1588. if (is_variadic_) *is_variadic_ = is_variadic;
  1589. if (variadic_index_) *variadic_index_ = variadic_index;
  1590. return tuple;
  1591. }
  1592. Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1593. if (_results == nullptr) {
  1594. return nullptr;
  1595. }
  1596. ast_node(field_list, FieldList, _results);
  1597. Slice<Ast *> results = field_list->list;
  1598. if (results.count == 0) {
  1599. return nullptr;
  1600. }
  1601. Type *tuple = alloc_type_tuple();
  1602. isize variable_count = 0;
  1603. for_array(i, results) {
  1604. Ast *field = results[i];
  1605. if (ast_node_expect(field, Ast_Field)) {
  1606. ast_node(f, Field, field);
  1607. variable_count += gb_max(f->names.count, 1);
  1608. }
  1609. }
  1610. auto variables = array_make<Entity *>(permanent_allocator(), 0, variable_count);
  1611. i32 field_group_index = -1;
  1612. for_array(i, results) {
  1613. field_group_index += 1;
  1614. ast_node(field, Field, results[i]);
  1615. Ast *default_value = unparen_expr(field->default_value);
  1616. ParameterValue param_value = {};
  1617. Type *type = nullptr;
  1618. if (field->type == nullptr) {
  1619. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1620. } else {
  1621. type = check_type(ctx, field->type);
  1622. if (default_value != nullptr) {
  1623. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1624. }
  1625. }
  1626. if (type == nullptr) {
  1627. error(results[i], "Invalid parameter type");
  1628. type = t_invalid;
  1629. }
  1630. if (is_type_untyped(type)) {
  1631. error(results[i], "Cannot determine parameter type from a nil");
  1632. type = t_invalid;
  1633. }
  1634. if (field->names.count == 0) {
  1635. Token token = ast_token(field->type);
  1636. token.string = str_lit("");
  1637. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1638. param->Variable.param_value = param_value;
  1639. param->Variable.field_group_index = -1;
  1640. array_add(&variables, param);
  1641. } else {
  1642. for_array(j, field->names) {
  1643. Token token = ast_token(results[i]);
  1644. if (field->type != nullptr) {
  1645. token = ast_token(field->type);
  1646. }
  1647. token.string = str_lit("");
  1648. Ast *name = field->names[j];
  1649. if (name->kind != Ast_Ident) {
  1650. error(name, "Expected an identifer for as the field name");
  1651. } else {
  1652. token = name->Ident.token;
  1653. }
  1654. if (is_blank_ident(token)) {
  1655. error(name, "Result value cannot be a blank identifer `_`");
  1656. }
  1657. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1658. param->flags |= EntityFlag_Result;
  1659. param->Variable.param_value = param_value;
  1660. param->Variable.field_group_index = field_group_index;
  1661. array_add(&variables, param);
  1662. add_entity(ctx, scope, name, param);
  1663. // NOTE(bill): Removes `declared but not used` when using -vet
  1664. add_entity_use(ctx, name, param);
  1665. }
  1666. }
  1667. }
  1668. for_array(i, variables) {
  1669. String x = variables[i]->token.string;
  1670. if (x.len == 0 || is_blank_ident(x)) {
  1671. continue;
  1672. }
  1673. for (isize j = i+1; j < variables.count; j++) {
  1674. String y = variables[j]->token.string;
  1675. if (y.len == 0 || is_blank_ident(y)) {
  1676. continue;
  1677. }
  1678. if (x == y) {
  1679. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1680. }
  1681. }
  1682. }
  1683. tuple->Tuple.variables = slice_from_array(variables);
  1684. return tuple;
  1685. }
  1686. // NOTE(bill): 'operands' is for generating non generic procedure type
  1687. bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> *operands) {
  1688. ast_node(pt, ProcType, proc_type_node);
  1689. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  1690. ctx->polymorphic_scope = ctx->scope;
  1691. }
  1692. CheckerContext c_ = *ctx;
  1693. CheckerContext *c = &c_;
  1694. c->curr_proc_sig = type;
  1695. c->in_proc_sig = true;
  1696. ProcCallingConvention cc = pt->calling_convention;
  1697. if (cc == ProcCC_ForeignBlockDefault) {
  1698. cc = ProcCC_CDecl;
  1699. if (c->foreign_context.default_cc > 0) {
  1700. cc = c->foreign_context.default_cc;
  1701. }
  1702. }
  1703. GB_ASSERT(cc > 0);
  1704. if (cc == ProcCC_Odin) {
  1705. c->scope->flags |= ScopeFlag_ContextDefined;
  1706. } else {
  1707. c->scope->flags &= ~ScopeFlag_ContextDefined;
  1708. }
  1709. TargetArchKind arch = build_context.metrics.arch;
  1710. switch (cc) {
  1711. case ProcCC_StdCall:
  1712. case ProcCC_FastCall:
  1713. if (arch != TargetArch_i386 || arch != TargetArch_amd64) {
  1714. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected either i386 or amd64, got %.*s",
  1715. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  1716. }
  1717. break;
  1718. case ProcCC_Win64:
  1719. case ProcCC_SysV:
  1720. if (arch != TargetArch_amd64) {
  1721. error(proc_type_node, "Invalid procedure calling convention \"%s\" for target architecture, expected amd64, got %.*s",
  1722. proc_calling_convention_strings[cc], LIT(target_arch_names[arch]));
  1723. }
  1724. break;
  1725. }
  1726. bool variadic = false;
  1727. isize variadic_index = -1;
  1728. bool success = true;
  1729. isize specialization_count = 0;
  1730. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  1731. Type *results = check_get_results(c, c->scope, pt->results);
  1732. isize param_count = 0;
  1733. isize result_count = 0;
  1734. if (params) param_count = params ->Tuple.variables.count;
  1735. if (results) result_count = results->Tuple.variables.count;
  1736. if (param_count > 0) {
  1737. for_array(i, params->Tuple.variables) {
  1738. Entity *param = params->Tuple.variables[i];
  1739. if (param->kind == Entity_Variable) {
  1740. ParameterValue pv = param->Variable.param_value;
  1741. if (pv.kind == ParameterValue_Constant &&
  1742. pv.value.kind == ExactValue_Procedure) {
  1743. type->Proc.has_proc_default_values = true;
  1744. break;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. if (result_count > 0) {
  1750. Entity *first = results->Tuple.variables[0];
  1751. type->Proc.has_named_results = first->token.string != "";
  1752. }
  1753. bool optional_ok = (pt->tags & ProcTag_optional_ok) != 0;
  1754. if (optional_ok) {
  1755. if (result_count != 2) {
  1756. error(proc_type_node, "A procedure type with the #optional_ok tag requires 2 return values, got %td", result_count);
  1757. } else {
  1758. Entity *second = results->Tuple.variables[1];
  1759. if (is_type_polymorphic(second->type)) {
  1760. // ignore
  1761. } else if (is_type_boolean(second->type)) {
  1762. // GOOD
  1763. } else {
  1764. error(second->token, "Second return value of an #optional_ok procedure must be a boolean, got %s", type_to_string(second->type));
  1765. }
  1766. }
  1767. }
  1768. if (pt->tags & ProcTag_optional_second) {
  1769. if (optional_ok) {
  1770. error(proc_type_node, "A procedure type cannot have both an #optional_ok tag and #optional_second");
  1771. }
  1772. optional_ok = true;
  1773. if (result_count != 2) {
  1774. error(proc_type_node, "A procedure type with the #optional_second tag requires 2 return values, got %td", result_count);
  1775. } else {
  1776. bool ok = false;
  1777. AstFile *file = proc_type_node->file();
  1778. if (file && file->pkg) {
  1779. ok = file->pkg->scope == ctx->info->runtime_package->scope;
  1780. }
  1781. if (!ok) {
  1782. error(proc_type_node, "A procedure type with the #optional_second may only be allowed within 'package runtime'");
  1783. }
  1784. }
  1785. }
  1786. type->Proc.node = proc_type_node;
  1787. type->Proc.scope = c->scope;
  1788. type->Proc.params = params;
  1789. type->Proc.param_count = cast(i32)param_count;
  1790. type->Proc.results = results;
  1791. type->Proc.result_count = cast(i32)result_count;
  1792. type->Proc.variadic = variadic;
  1793. type->Proc.variadic_index = cast(i32)variadic_index;
  1794. type->Proc.calling_convention = cc;
  1795. type->Proc.is_polymorphic = pt->generic;
  1796. type->Proc.specialization_count = specialization_count;
  1797. type->Proc.diverging = pt->diverging;
  1798. type->Proc.optional_ok = optional_ok;
  1799. if (param_count > 0) {
  1800. Entity *end = params->Tuple.variables[param_count-1];
  1801. if (end->flags&EntityFlag_CVarArg) {
  1802. if (cc == ProcCC_StdCall || cc == ProcCC_CDecl) {
  1803. type->Proc.c_vararg = true;
  1804. } else {
  1805. error(end->token, "Calling convention does not support #c_vararg");
  1806. }
  1807. }
  1808. }
  1809. bool is_polymorphic = false;
  1810. for (isize i = 0; i < param_count; i++) {
  1811. Entity *e = params->Tuple.variables[i];
  1812. if (e->kind != Entity_Variable) {
  1813. is_polymorphic = true;
  1814. break;
  1815. } else if (is_type_polymorphic(e->type)) {
  1816. is_polymorphic = true;
  1817. break;
  1818. }
  1819. }
  1820. for (isize i = 0; i < result_count; i++) {
  1821. Entity *e = results->Tuple.variables[i];
  1822. if (e->kind != Entity_Variable) {
  1823. is_polymorphic = true;
  1824. break;
  1825. } else if (is_type_polymorphic(e->type)) {
  1826. is_polymorphic = true;
  1827. break;
  1828. }
  1829. }
  1830. type->Proc.is_polymorphic = is_polymorphic;
  1831. return success;
  1832. }
  1833. i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  1834. if (e == nullptr) {
  1835. return 0;
  1836. }
  1837. if (e->kind == Ast_UnaryExpr &&
  1838. e->UnaryExpr.op.kind == Token_Question) {
  1839. return -1;
  1840. }
  1841. check_expr_or_type(ctx, o, e);
  1842. if (o->mode == Addressing_Type) {
  1843. Type *ot = base_type(o->type);
  1844. if (ot->kind == Type_Generic) {
  1845. if (ctx->allow_polymorphic_types) {
  1846. if (ot->Generic.specialized) {
  1847. ot->Generic.specialized = nullptr;
  1848. error(o->expr, "Polymorphic array length cannot have a specialization");
  1849. }
  1850. return 0;
  1851. }
  1852. }
  1853. if (is_type_enum(ot)) {
  1854. return -1;
  1855. }
  1856. }
  1857. if (o->mode != Addressing_Constant) {
  1858. if (o->mode != Addressing_Invalid) {
  1859. Entity *entity = entity_of_node(o->expr);
  1860. bool is_poly_type = false;
  1861. if (entity != nullptr) {
  1862. is_poly_type = \
  1863. entity->kind == Entity_TypeName &&
  1864. entity->type == t_typeid &&
  1865. entity->flags&EntityFlag_PolyConst;
  1866. }
  1867. // NOTE(bill, 2021-03-27): Improve error message for parametric polymorphic parameters which want to generate
  1868. // and enumerated array but cannot determine what it ought to be yet
  1869. if (ctx->allow_polymorphic_types && is_poly_type) {
  1870. return 0;
  1871. }
  1872. gbString s = expr_to_string(o->expr);
  1873. error(e, "Array count must be a constant integer, got %s", s);
  1874. gb_string_free(s);
  1875. if (is_poly_type) {
  1876. error_line("\tSuggestion: 'where' clause may be required to restrict the enumerated array index type to an enum\n");
  1877. error_line("\t 'where intrinsics.type_is_enum(%.*s)'\n", LIT(entity->token.string));
  1878. }
  1879. o->mode = Addressing_Invalid;
  1880. o->type = t_invalid;
  1881. }
  1882. return 0;
  1883. }
  1884. Type *type = core_type(o->type);
  1885. if (is_type_untyped(type) || is_type_integer(type)) {
  1886. if (o->value.kind == ExactValue_Integer) {
  1887. BigInt count = o->value.value_integer;
  1888. if (big_int_is_neg(&o->value.value_integer)) {
  1889. gbAllocator a = heap_allocator();
  1890. String str = big_int_to_string(a, &count);
  1891. error(e, "Invalid negative array count, %.*s", LIT(str));
  1892. gb_free(a, str.text);
  1893. return 0;
  1894. }
  1895. switch (count.used) {
  1896. case 0: return 0;
  1897. case 1: return big_int_to_u64(&count);
  1898. }
  1899. gbAllocator a = heap_allocator();
  1900. String str = big_int_to_string(a, &count);
  1901. error(e, "Array count too large, %.*s", LIT(str));
  1902. gb_free(a, str.text);
  1903. return 0;
  1904. }
  1905. }
  1906. error(e, "Array count must be a constant integer");
  1907. return 0;
  1908. }
  1909. Type *make_optional_ok_type(Type *value, bool typed) {
  1910. gbAllocator a = permanent_allocator();
  1911. Type *t = alloc_type_tuple();
  1912. slice_init(&t->Tuple.variables, a, 2);
  1913. t->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, value, false, 0);
  1914. t->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1);
  1915. return t;
  1916. }
  1917. void init_map_entry_type(Type *type) {
  1918. GB_ASSERT(type->kind == Type_Map);
  1919. if (type->Map.entry_type != nullptr) return;
  1920. // NOTE(bill): The preload types may have not been set yet
  1921. GB_ASSERT(t_map_hash != nullptr);
  1922. /*
  1923. struct {
  1924. hash: runtime.Map_Hash,
  1925. next: int,
  1926. key: Key,
  1927. value: Value,
  1928. }
  1929. */
  1930. Scope *s = create_scope(nullptr, builtin_pkg->scope);
  1931. auto fields = slice_make<Entity *>(permanent_allocator(), 4);
  1932. fields[0] = alloc_entity_field(s, make_token_ident(str_lit("hash")), t_uintptr, false, 0, EntityState_Resolved);
  1933. fields[1] = alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, EntityState_Resolved);
  1934. fields[2] = alloc_entity_field(s, make_token_ident(str_lit("key")), type->Map.key, false, 2, EntityState_Resolved);
  1935. fields[3] = alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, 3, EntityState_Resolved);
  1936. Type *entry_type = alloc_type_struct();
  1937. entry_type->Struct.fields = fields;
  1938. entry_type->Struct.tags = gb_alloc_array(permanent_allocator(), String, fields.count);
  1939. type_set_offsets(entry_type);
  1940. type->Map.entry_type = entry_type;
  1941. }
  1942. void init_map_internal_types(Type *type) {
  1943. GB_ASSERT(type->kind == Type_Map);
  1944. init_map_entry_type(type);
  1945. if (type->Map.internal_type != nullptr) return;
  1946. if (type->Map.generated_struct_type != nullptr) return;
  1947. Type *key = type->Map.key;
  1948. Type *value = type->Map.value;
  1949. GB_ASSERT(key != nullptr);
  1950. GB_ASSERT(value != nullptr);
  1951. Type *generated_struct_type = alloc_type_struct();
  1952. /*
  1953. struct {
  1954. hashes: []int;
  1955. entries: [dynamic]EntryType;
  1956. }
  1957. */
  1958. Scope *s = create_scope(nullptr, builtin_pkg->scope);
  1959. Type *hashes_type = alloc_type_slice(t_int);
  1960. Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
  1961. auto fields = slice_make<Entity *>(permanent_allocator(), 2);
  1962. fields[0] = alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved);
  1963. fields[1] = alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved);
  1964. generated_struct_type->Struct.fields = fields;
  1965. type_set_offsets(generated_struct_type);
  1966. type->Map.generated_struct_type = generated_struct_type;
  1967. type->Map.internal_type = generated_struct_type;
  1968. type->Map.lookup_result_type = make_optional_ok_type(value);
  1969. }
  1970. void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
  1971. key = core_type(key);
  1972. if (is_type_cstring(key)) {
  1973. add_package_dependency(ctx, "runtime", "default_hasher_cstring");
  1974. } else if (is_type_string(key)) {
  1975. add_package_dependency(ctx, "runtime", "default_hasher_string");
  1976. } else if (!is_type_polymorphic(key)) {
  1977. if (!is_type_comparable(key)) {
  1978. return;
  1979. }
  1980. if (is_type_simple_compare(key)) {
  1981. i64 sz = type_size_of(key);
  1982. if (1 <= sz && sz <= 16) {
  1983. char buf[20] = {};
  1984. gb_snprintf(buf, 20, "default_hasher%d", cast(i32)sz);
  1985. add_package_dependency(ctx, "runtime", buf);
  1986. return;
  1987. } else {
  1988. add_package_dependency(ctx, "runtime", "default_hasher_n");
  1989. return;
  1990. }
  1991. }
  1992. if (key->kind == Type_Struct) {
  1993. add_package_dependency(ctx, "runtime", "default_hasher_n");
  1994. for_array(i, key->Struct.fields) {
  1995. Entity *field = key->Struct.fields[i];
  1996. add_map_key_type_dependencies(ctx, field->type);
  1997. }
  1998. } else if (key->kind == Type_Union) {
  1999. add_package_dependency(ctx, "runtime", "default_hasher_n");
  2000. for_array(i, key->Union.variants) {
  2001. Type *v = key->Union.variants[i];
  2002. add_map_key_type_dependencies(ctx, v);
  2003. }
  2004. } else if (key->kind == Type_EnumeratedArray) {
  2005. add_package_dependency(ctx, "runtime", "default_hasher_n");
  2006. add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
  2007. } else if (key->kind == Type_Array) {
  2008. add_package_dependency(ctx, "runtime", "default_hasher_n");
  2009. add_map_key_type_dependencies(ctx, key->Array.elem);
  2010. }
  2011. }
  2012. }
  2013. void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  2014. GB_ASSERT(type->kind == Type_Map);
  2015. ast_node(mt, MapType, node);
  2016. Type *key = check_type(ctx, mt->key);
  2017. Type *value = check_type(ctx, mt->value);
  2018. if (!is_type_valid_for_keys(key)) {
  2019. if (is_type_boolean(key)) {
  2020. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  2021. } else {
  2022. gbString str = type_to_string(key);
  2023. error(node, "Invalid type of a key for a map, got '%s'", str);
  2024. gb_string_free(str);
  2025. }
  2026. }
  2027. if (type_size_of(key) == 0) {
  2028. gbString str = type_to_string(key);
  2029. error(node, "Invalid type of a key for a map of size 0, got '%s'", str);
  2030. gb_string_free(str);
  2031. }
  2032. type->Map.key = key;
  2033. type->Map.value = value;
  2034. add_map_key_type_dependencies(ctx, key);
  2035. init_core_map_type(ctx->checker);
  2036. init_map_internal_types(type);
  2037. // error(node, "'map' types are not yet implemented");
  2038. }
  2039. void check_matrix_type(CheckerContext *ctx, Type **type, Ast *node) {
  2040. ast_node(mt, MatrixType, node);
  2041. Operand row = {};
  2042. Operand column = {};
  2043. i64 row_count = check_array_count(ctx, &row, mt->row_count);
  2044. i64 column_count = check_array_count(ctx, &column, mt->column_count);
  2045. Type *elem = check_type_expr(ctx, mt->elem, nullptr);
  2046. Type *generic_row = nullptr;
  2047. Type *generic_column = nullptr;
  2048. if (row.mode == Addressing_Type && row.type->kind == Type_Generic) {
  2049. generic_row = row.type;
  2050. }
  2051. if (column.mode == Addressing_Type && column.type->kind == Type_Generic) {
  2052. generic_column = column.type;
  2053. }
  2054. if (row_count < MATRIX_ELEMENT_COUNT_MIN && generic_row == nullptr) {
  2055. gbString s = expr_to_string(row.expr);
  2056. error(row.expr, "Invalid matrix row count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2057. gb_string_free(s);
  2058. }
  2059. if (column_count < MATRIX_ELEMENT_COUNT_MIN && generic_column == nullptr) {
  2060. gbString s = expr_to_string(column.expr);
  2061. error(column.expr, "Invalid matrix column count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
  2062. gb_string_free(s);
  2063. }
  2064. if (row_count*column_count > MATRIX_ELEMENT_COUNT_MAX) {
  2065. i64 element_count = row_count*column_count;
  2066. error(column.expr, "Matrix types are limited to a maximum of %d elements, got %lld", MATRIX_ELEMENT_COUNT_MAX, cast(long long)element_count);
  2067. }
  2068. if (!is_type_valid_for_matrix_elems(elem)) {
  2069. if (elem == t_typeid) {
  2070. Entity *e = entity_of_node(mt->elem);
  2071. if (e && e->kind == Entity_TypeName && e->TypeName.is_type_alias) {
  2072. // HACK TODO(bill): This is to allow polymorphic parameters for matrix elements
  2073. // proc($T: typeid) -> matrix[2, 2]T
  2074. //
  2075. // THIS IS NEEDS TO BE FIXED AND NOT USE THIS HACK
  2076. goto type_assign;
  2077. }
  2078. }
  2079. gbString s = type_to_string(elem);
  2080. error(column.expr, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
  2081. gb_string_free(s);
  2082. }
  2083. type_assign:;
  2084. *type = alloc_type_matrix(elem, row_count, column_count, generic_row, generic_column);
  2085. return;
  2086. }
  2087. Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type, StructSoaKind soa_kind) {
  2088. Type *bt_elem = base_type(elem);
  2089. bool is_polymorphic = is_type_polymorphic(elem);
  2090. if ((!is_polymorphic || soa_kind == StructSoa_Fixed) && !is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  2091. gbString str = type_to_string(elem);
  2092. error(elem_expr, "Invalid type for an #soa array, expected a struct or array of length 4 or below, got '%s'", str);
  2093. gb_string_free(str);
  2094. return alloc_type_array(elem, count, generic_type);
  2095. }
  2096. Type *soa_struct = nullptr;
  2097. Scope *scope = nullptr;
  2098. isize field_count = 0;
  2099. i32 extra_field_count = 0;
  2100. switch (soa_kind) {
  2101. case StructSoa_Fixed: extra_field_count = 0; break;
  2102. case StructSoa_Slice: extra_field_count = 1; break;
  2103. case StructSoa_Dynamic: extra_field_count = 3; break;
  2104. }
  2105. if (is_polymorphic && soa_kind != StructSoa_Fixed) {
  2106. field_count = 0;
  2107. soa_struct = alloc_type_struct();
  2108. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2109. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2110. soa_struct->Struct.node = array_typ_expr;
  2111. soa_struct->Struct.soa_kind = soa_kind;
  2112. soa_struct->Struct.soa_elem = elem;
  2113. soa_struct->Struct.soa_count = 0;
  2114. soa_struct->Struct.is_polymorphic = true;
  2115. scope = create_scope(ctx->info, ctx->scope);
  2116. soa_struct->Struct.scope = scope;
  2117. } else if (is_type_array(elem)) {
  2118. Type *old_array = base_type(elem);
  2119. field_count = cast(isize)old_array->Array.count;
  2120. soa_struct = alloc_type_struct();
  2121. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2122. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2123. soa_struct->Struct.node = array_typ_expr;
  2124. soa_struct->Struct.soa_kind = soa_kind;
  2125. soa_struct->Struct.soa_elem = elem;
  2126. if (count > I32_MAX) {
  2127. count = I32_MAX;
  2128. error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  2129. }
  2130. soa_struct->Struct.soa_count = cast(i32)count;
  2131. scope = create_scope(ctx->info, ctx->scope, 8);
  2132. soa_struct->Struct.scope = scope;
  2133. String params_xyzw[4] = {
  2134. str_lit("x"),
  2135. str_lit("y"),
  2136. str_lit("z"),
  2137. str_lit("w")
  2138. };
  2139. for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
  2140. Type *field_type = nullptr;
  2141. if (soa_kind == StructSoa_Fixed) {
  2142. GB_ASSERT(count >= 0);
  2143. field_type = alloc_type_array(old_array->Array.elem, count);
  2144. } else {
  2145. field_type = alloc_type_pointer(old_array->Array.elem);
  2146. }
  2147. Token token = {};
  2148. token.string = params_xyzw[i];
  2149. Entity *new_field = alloc_entity_field(scope, token, field_type, false, cast(i32)i);
  2150. soa_struct->Struct.fields[i] = new_field;
  2151. add_entity(ctx, scope, nullptr, new_field);
  2152. add_entity_use(ctx, nullptr, new_field);
  2153. }
  2154. } else {
  2155. GB_ASSERT(is_type_struct(elem));
  2156. Type *old_struct = base_type(elem);
  2157. field_count = old_struct->Struct.fields.count;
  2158. soa_struct = alloc_type_struct();
  2159. soa_struct->Struct.fields = slice_make<Entity *>(permanent_allocator(), field_count+extra_field_count);
  2160. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, field_count+extra_field_count);
  2161. soa_struct->Struct.node = array_typ_expr;
  2162. soa_struct->Struct.soa_kind = soa_kind;
  2163. soa_struct->Struct.soa_elem = elem;
  2164. if (count > I32_MAX) {
  2165. count = I32_MAX;
  2166. error(array_typ_expr, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  2167. }
  2168. soa_struct->Struct.soa_count = cast(i32)count;
  2169. scope = create_scope(ctx->info, old_struct->Struct.scope->parent);
  2170. soa_struct->Struct.scope = scope;
  2171. for_array(i, old_struct->Struct.fields) {
  2172. Entity *old_field = old_struct->Struct.fields[i];
  2173. if (old_field->kind == Entity_Variable) {
  2174. Type *field_type = nullptr;
  2175. if (soa_kind == StructSoa_Fixed) {
  2176. GB_ASSERT(count >= 0);
  2177. field_type = alloc_type_array(old_field->type, count);
  2178. } else {
  2179. field_type = alloc_type_pointer(old_field->type);
  2180. }
  2181. Entity *new_field = alloc_entity_field(scope, old_field->token, field_type, false, old_field->Variable.field_index);
  2182. soa_struct->Struct.fields[i] = new_field;
  2183. add_entity(ctx, scope, nullptr, new_field);
  2184. add_entity_use(ctx, nullptr, new_field);
  2185. } else {
  2186. soa_struct->Struct.fields[i] = old_field;
  2187. }
  2188. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  2189. }
  2190. }
  2191. if (soa_kind != StructSoa_Fixed) {
  2192. Entity *len_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count+0);
  2193. soa_struct->Struct.fields[field_count+0] = len_field;
  2194. add_entity(ctx, scope, nullptr, len_field);
  2195. add_entity_use(ctx, nullptr, len_field);
  2196. if (soa_kind == StructSoa_Dynamic) {
  2197. Entity *cap_field = alloc_entity_field(scope, empty_token, t_int, false, cast(i32)field_count+1);
  2198. soa_struct->Struct.fields[field_count+1] = cap_field;
  2199. add_entity(ctx, scope, nullptr, cap_field);
  2200. add_entity_use(ctx, nullptr, cap_field);
  2201. Token token = {};
  2202. token.string = str_lit("allocator");
  2203. init_mem_allocator(ctx->checker);
  2204. Entity *allocator_field = alloc_entity_field(scope, token, t_allocator, false, cast(i32)field_count+2);
  2205. soa_struct->Struct.fields[field_count+2] = allocator_field;
  2206. add_entity(ctx, scope, nullptr, allocator_field);
  2207. add_entity_use(ctx, nullptr, allocator_field);
  2208. }
  2209. }
  2210. Token token = {};
  2211. token.string = str_lit("Base_Type");
  2212. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  2213. add_entity(ctx, scope, nullptr, base_type_entity);
  2214. add_type_info_type(ctx, soa_struct);
  2215. return soa_struct;
  2216. }
  2217. Type *make_soa_struct_fixed(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type) {
  2218. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, count, generic_type, StructSoa_Fixed);
  2219. }
  2220. Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2221. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Slice);
  2222. }
  2223. Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem) {
  2224. return make_soa_struct_internal(ctx, array_typ_expr, elem_expr, elem, -1, nullptr, StructSoa_Dynamic);
  2225. }
  2226. bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  2227. GB_ASSERT_NOT_NULL(type);
  2228. if (e == nullptr) {
  2229. *type = t_invalid;
  2230. return true;
  2231. }
  2232. switch (e->kind) {
  2233. case_ast_node(i, Ident, e);
  2234. Operand o = {};
  2235. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  2236. gb_unused(entity);
  2237. gbString err_str = nullptr;
  2238. defer (gb_string_free(err_str));
  2239. switch (o.mode) {
  2240. case Addressing_Invalid:
  2241. break;
  2242. case Addressing_Type: {
  2243. *type = o.type;
  2244. if (!ctx->in_polymorphic_specialization) {
  2245. Type *t = base_type(o.type);
  2246. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  2247. err_str = expr_to_string(e);
  2248. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  2249. return true;
  2250. }
  2251. }
  2252. return true;
  2253. }
  2254. case Addressing_NoValue:
  2255. err_str = expr_to_string(e);
  2256. error(e, "'%s' used as a type", err_str);
  2257. break;
  2258. default:
  2259. err_str = expr_to_string(e);
  2260. error(e, "'%s' used as a type when not a type", err_str);
  2261. break;
  2262. }
  2263. case_end;
  2264. case_ast_node(ht, HelperType, e);
  2265. return check_type_internal(ctx, ht->type, type, named_type);
  2266. case_end;
  2267. case_ast_node(dt, DistinctType, e);
  2268. error(e, "Invalid use of a distinct type");
  2269. // NOTE(bill): Treat it as a HelperType to remove errors
  2270. return check_type_internal(ctx, dt->type, type, named_type);
  2271. case_end;
  2272. case_ast_node(tt, TypeidType, e);
  2273. e->tav.mode = Addressing_Type;
  2274. e->tav.type = t_typeid;
  2275. *type = t_typeid;
  2276. set_base_type(named_type, *type);
  2277. return true;
  2278. case_end;
  2279. case_ast_node(pt, PolyType, e);
  2280. Ast *ident = pt->type;
  2281. if (ident->kind != Ast_Ident) {
  2282. error(ident, "Expected an identifier after the $");
  2283. *type = t_invalid;
  2284. return false;
  2285. }
  2286. Token token = ident->Ident.token;
  2287. Type *specific = nullptr;
  2288. if (pt->specialization != nullptr) {
  2289. CheckerContext c = *ctx;
  2290. c.in_polymorphic_specialization = true;
  2291. Ast *s = pt->specialization;
  2292. specific = check_type(&c, s);
  2293. }
  2294. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2295. if (ctx->allow_polymorphic_types) {
  2296. Scope *ps = ctx->polymorphic_scope;
  2297. Scope *s = ctx->scope;
  2298. Scope *entity_scope = s;
  2299. if (ps != nullptr && ps != s) {
  2300. // TODO(bill): Is this check needed?
  2301. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2302. entity_scope = ps;
  2303. }
  2304. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2305. t->Generic.entity = e;
  2306. e->TypeName.is_type_alias = true;
  2307. e->state = EntityState_Resolved;
  2308. add_entity(ctx, ps, ident, e);
  2309. add_entity(ctx, s, ident, e);
  2310. } else {
  2311. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2312. *type = t_invalid;
  2313. return false;
  2314. }
  2315. *type = t;
  2316. set_base_type(named_type, *type);
  2317. return true;
  2318. case_end;
  2319. case_ast_node(se, SelectorExpr, e);
  2320. Operand o = {};
  2321. check_selector(ctx, &o, e, nullptr);
  2322. gbString err_str;
  2323. switch (o.mode) {
  2324. case Addressing_Invalid:
  2325. break;
  2326. case Addressing_Type:
  2327. GB_ASSERT(o.type != nullptr);
  2328. *type = o.type;
  2329. return true;
  2330. case Addressing_NoValue:
  2331. err_str = expr_to_string(e);
  2332. error(e, "'%s' used as a type", err_str);
  2333. gb_string_free(err_str);
  2334. break;
  2335. default:
  2336. err_str = expr_to_string(e);
  2337. error(e, "'%s' is not a type", err_str);
  2338. gb_string_free(err_str);
  2339. break;
  2340. }
  2341. case_end;
  2342. case_ast_node(pe, ParenExpr, e);
  2343. *type = check_type_expr(ctx, pe->expr, named_type);
  2344. set_base_type(named_type, *type);
  2345. return true;
  2346. case_end;
  2347. case_ast_node(ue, UnaryExpr, e);
  2348. switch (ue->op.kind) {
  2349. case Token_Pointer:
  2350. *type = alloc_type_pointer(check_type(ctx, ue->expr));
  2351. set_base_type(named_type, *type);
  2352. return true;
  2353. }
  2354. case_end;
  2355. case_ast_node(pt, PointerType, e);
  2356. *type = alloc_type_pointer(check_type(ctx, pt->type));
  2357. set_base_type(named_type, *type);
  2358. return true;
  2359. case_end;
  2360. case_ast_node(pt, MultiPointerType, e);
  2361. *type = alloc_type_multi_pointer(check_type(ctx, pt->type));
  2362. set_base_type(named_type, *type);
  2363. return true;
  2364. case_end;
  2365. case_ast_node(rt, RelativeType, e);
  2366. GB_ASSERT(rt->tag->kind == Ast_CallExpr);
  2367. ast_node(ce, CallExpr, rt->tag);
  2368. Type *base_integer = nullptr;
  2369. if (ce->args.count != 1) {
  2370. error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count);
  2371. } else {
  2372. base_integer = check_type(ctx, ce->args[0]);
  2373. if (!is_type_integer(base_integer)) {
  2374. error(rt->type, "#relative base types must be an integer");
  2375. base_integer = nullptr;
  2376. } else if (type_size_of(base_integer) > 64) {
  2377. error(rt->type, "#relative base integer types be less than or equal to 64-bits");
  2378. base_integer = nullptr;
  2379. }
  2380. }
  2381. Type *relative_type = nullptr;
  2382. Type *base_type = check_type(ctx, rt->type);
  2383. if (!is_type_pointer(base_type) && !is_type_slice(base_type)) {
  2384. error(rt->type, "#relative types can only be a pointer or slice");
  2385. relative_type = base_type;
  2386. } else if (base_integer == nullptr) {
  2387. relative_type = base_type;
  2388. } else {
  2389. if (is_type_pointer(base_type)) {
  2390. relative_type = alloc_type_relative_pointer(base_type, base_integer);
  2391. } else if (is_type_slice(base_type)) {
  2392. relative_type = alloc_type_relative_slice(base_type, base_integer);
  2393. }
  2394. }
  2395. GB_ASSERT(relative_type != nullptr);
  2396. *type = relative_type;
  2397. set_base_type(named_type, *type);
  2398. return true;
  2399. case_end;
  2400. case_ast_node(at, ArrayType, e);
  2401. if (at->count != nullptr) {
  2402. Operand o = {};
  2403. i64 count = check_array_count(ctx, &o, at->count);
  2404. Type *generic_type = nullptr;
  2405. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2406. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2407. generic_type = o.type;
  2408. } else if (o.mode == Addressing_Type && is_type_enum(o.type)) {
  2409. Type *index = o.type;
  2410. Type *bt = base_type(index);
  2411. GB_ASSERT(bt->kind == Type_Enum);
  2412. Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, Token_Invalid);
  2413. bool is_sparse = false;
  2414. if (at->tag != nullptr) {
  2415. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2416. String name = at->tag->BasicDirective.name.string;
  2417. if (name == "sparse") {
  2418. is_sparse = true;
  2419. } else {
  2420. error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
  2421. }
  2422. }
  2423. if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count) {
  2424. error(e, "Non-contiguous enumeration used as an index in an enumerated array");
  2425. long long ea_count = cast(long long)t->EnumeratedArray.count;
  2426. long long enum_count = cast(long long)bt->Enum.fields.count;
  2427. error_line("\tenumerated array length: %lld\n", ea_count);
  2428. error_line("\tenum field count: %lld\n", enum_count);
  2429. error_line("\tSuggestion: prepend #sparse to the enumerated array to allow for non-contiguous elements\n");
  2430. if (2*enum_count < ea_count) {
  2431. 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");
  2432. error_line("\t this warning will be removed if #sparse is applied\n");
  2433. }
  2434. }
  2435. t->EnumeratedArray.is_sparse = is_sparse;
  2436. *type = t;
  2437. goto array_end;
  2438. }
  2439. if (count < 0) {
  2440. error(at->count, "? can only be used in conjuction with compound literals");
  2441. count = 0;
  2442. }
  2443. if (at->tag != nullptr) {
  2444. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2445. String name = at->tag->BasicDirective.name.string;
  2446. if (name == "soa") {
  2447. *type = make_soa_struct_fixed(ctx, e, at->elem, elem, count, generic_type);
  2448. } else if (name == "simd") {
  2449. if (!is_type_valid_vector_elem(elem)) {
  2450. gbString str = type_to_string(elem);
  2451. error(at->elem, "Invalid element type for 'intrinsics.simd_vector', expected an integer or float with no specific endianness, got '%s'", str);
  2452. gb_string_free(str);
  2453. *type = alloc_type_array(elem, count, generic_type);
  2454. goto array_end;
  2455. }
  2456. *type = alloc_type_simd_vector(count, elem);
  2457. } else {
  2458. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2459. *type = alloc_type_array(elem, count, generic_type);
  2460. }
  2461. } else {
  2462. *type = alloc_type_array(elem, count, generic_type);
  2463. }
  2464. } else {
  2465. Type *elem = check_type(ctx, at->elem);
  2466. if (at->tag != nullptr) {
  2467. GB_ASSERT(at->tag->kind == Ast_BasicDirective);
  2468. String name = at->tag->BasicDirective.name.string;
  2469. if (name == "soa") {
  2470. *type = make_soa_struct_slice(ctx, e, at->elem, elem);
  2471. } else {
  2472. error(at->tag, "Invalid tag applied to array, got #%.*s", LIT(name));
  2473. *type = alloc_type_slice(elem);
  2474. }
  2475. } else {
  2476. *type = alloc_type_slice(elem);
  2477. }
  2478. }
  2479. array_end:
  2480. set_base_type(named_type, *type);
  2481. return true;
  2482. case_end;
  2483. case_ast_node(dat, DynamicArrayType, e);
  2484. Type *elem = check_type(ctx, dat->elem);
  2485. if (dat->tag != nullptr) {
  2486. GB_ASSERT(dat->tag->kind == Ast_BasicDirective);
  2487. String name = dat->tag->BasicDirective.name.string;
  2488. if (name == "soa") {
  2489. *type = make_soa_struct_dynamic_array(ctx, e, dat->elem, elem);
  2490. } else {
  2491. error(dat->tag, "Invalid tag applied to dynamic array, got #%.*s", LIT(name));
  2492. *type = alloc_type_dynamic_array(elem);
  2493. }
  2494. } else {
  2495. *type = alloc_type_dynamic_array(elem);
  2496. }
  2497. set_base_type(named_type, *type);
  2498. return true;
  2499. case_end;
  2500. case_ast_node(st, StructType, e);
  2501. CheckerContext c = *ctx;
  2502. c.in_polymorphic_specialization = false;
  2503. c.type_level += 1;
  2504. *type = alloc_type_struct();
  2505. set_base_type(named_type, *type);
  2506. check_open_scope(&c, e);
  2507. check_struct_type(&c, *type, e, nullptr, named_type);
  2508. check_close_scope(&c);
  2509. (*type)->Struct.node = e;
  2510. return true;
  2511. case_end;
  2512. case_ast_node(ut, UnionType, e);
  2513. CheckerContext c = *ctx;
  2514. c.in_polymorphic_specialization = false;
  2515. c.type_level += 1;
  2516. *type = alloc_type_union();
  2517. set_base_type(named_type, *type);
  2518. check_open_scope(&c, e);
  2519. check_union_type(&c, *type, e, nullptr, named_type);
  2520. check_close_scope(&c);
  2521. (*type)->Union.node = e;
  2522. return true;
  2523. case_end;
  2524. case_ast_node(et, EnumType, e);
  2525. bool ips = ctx->in_polymorphic_specialization;
  2526. defer (ctx->in_polymorphic_specialization = ips);
  2527. ctx->in_polymorphic_specialization = false;
  2528. ctx->in_enum_type = true;
  2529. *type = alloc_type_enum();
  2530. set_base_type(named_type, *type);
  2531. check_open_scope(ctx, e);
  2532. check_enum_type(ctx, *type, named_type, e);
  2533. check_close_scope(ctx);
  2534. (*type)->Enum.node = e;
  2535. ctx->in_enum_type = false;
  2536. return true;
  2537. case_end;
  2538. case_ast_node(bs, BitSetType, e);
  2539. *type = alloc_type_bit_set();
  2540. set_base_type(named_type, *type);
  2541. check_bit_set_type(ctx, *type, named_type, e);
  2542. return true;
  2543. case_end;
  2544. case_ast_node(pt, ProcType, e);
  2545. bool ips = ctx->in_polymorphic_specialization;
  2546. defer (ctx->in_polymorphic_specialization = ips);
  2547. ctx->in_polymorphic_specialization = false;
  2548. *type = alloc_type(Type_Proc);
  2549. set_base_type(named_type, *type);
  2550. check_open_scope(ctx, e);
  2551. check_procedure_type(ctx, *type, e);
  2552. check_close_scope(ctx);
  2553. return true;
  2554. case_end;
  2555. case_ast_node(mt, MapType, e);
  2556. bool ips = ctx->in_polymorphic_specialization;
  2557. defer (ctx->in_polymorphic_specialization = ips);
  2558. ctx->in_polymorphic_specialization = false;
  2559. *type = alloc_type(Type_Map);
  2560. set_base_type(named_type, *type);
  2561. check_map_type(ctx, *type, e);
  2562. return true;
  2563. case_end;
  2564. case_ast_node(ce, CallExpr, e);
  2565. Operand o = {};
  2566. check_expr_or_type(ctx, &o, e);
  2567. if (o.mode == Addressing_Type) {
  2568. *type = o.type;
  2569. set_base_type(named_type, *type);
  2570. return true;
  2571. }
  2572. case_end;
  2573. case_ast_node(te, TernaryIfExpr, e);
  2574. Operand o = {};
  2575. check_expr_or_type(ctx, &o, e);
  2576. if (o.mode == Addressing_Type) {
  2577. *type = o.type;
  2578. set_base_type(named_type, *type);
  2579. return true;
  2580. }
  2581. case_end;
  2582. case_ast_node(te, TernaryWhenExpr, e);
  2583. Operand o = {};
  2584. check_expr_or_type(ctx, &o, e);
  2585. if (o.mode == Addressing_Type) {
  2586. *type = o.type;
  2587. set_base_type(named_type, *type);
  2588. return true;
  2589. }
  2590. case_end;
  2591. case_ast_node(mt, MatrixType, e);
  2592. check_matrix_type(ctx, type, e);
  2593. set_base_type(named_type, *type);
  2594. return true;
  2595. case_end;
  2596. }
  2597. *type = t_invalid;
  2598. return false;
  2599. }
  2600. Type *check_type(CheckerContext *ctx, Ast *e) {
  2601. CheckerContext c = *ctx;
  2602. c.type_path = new_checker_type_path();
  2603. defer (destroy_checker_type_path(c.type_path));
  2604. return check_type_expr(&c, e, nullptr);
  2605. }
  2606. Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  2607. Type *type = nullptr;
  2608. bool ok = check_type_internal(ctx, e, &type, named_type);
  2609. if (!ok) {
  2610. gbString err_str = expr_to_string(e);
  2611. error(e, "'%s' is not a type", err_str);
  2612. gb_string_free(err_str);
  2613. type = t_invalid;
  2614. }
  2615. if (type == nullptr) {
  2616. type = t_invalid;
  2617. }
  2618. if (type->kind == Type_Named &&
  2619. type->Named.base == nullptr) {
  2620. // IMPORTANT TODO(bill): Is this a serious error?!
  2621. #if 0
  2622. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  2623. #endif
  2624. type->Named.base = t_invalid;
  2625. }
  2626. if (is_type_polymorphic(type)) {
  2627. type->flags |= TypeFlag_Polymorphic;
  2628. } else if (is_type_polymorphic(type, true)) {
  2629. type->flags |= TypeFlag_PolySpecialized;
  2630. }
  2631. #if 0
  2632. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  2633. gbString str = type_to_string(type);
  2634. error(e, "Invalid use of a polymorphic type '%s'", str);
  2635. gb_string_free(str);
  2636. type = t_invalid;
  2637. }
  2638. #endif
  2639. if (is_type_typed(type)) {
  2640. add_type_and_value(ctx->info, e, Addressing_Type, type, empty_exact_value);
  2641. } else {
  2642. gbString name = type_to_string(type);
  2643. error(e, "Invalid type definition of %s", name);
  2644. gb_string_free(name);
  2645. type = t_invalid;
  2646. }
  2647. set_base_type(named_type, type);
  2648. return type;
  2649. }