check_type.cpp 83 KB

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