check_type.cpp 71 KB

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