check_type.cpp 75 KB

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