check_type.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  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. scope_reserve(ctx->scope, et->fields.count);
  676. for_array(i, et->fields) {
  677. Ast *field = et->fields[i];
  678. Ast *ident = nullptr;
  679. Ast *init = nullptr;
  680. if (field->kind == Ast_FieldValue) {
  681. ast_node(fv, FieldValue, field);
  682. if (fv->field == nullptr || fv->field->kind != Ast_Ident) {
  683. error(field, "An enum field's name must be an identifier");
  684. continue;
  685. }
  686. ident = fv->field;
  687. init = fv->value;
  688. } else if (field->kind == Ast_Ident) {
  689. ident = field;
  690. } else {
  691. error(field, "An enum field's name must be an identifier");
  692. continue;
  693. }
  694. String name = ident->Ident.token.string;
  695. if (init != nullptr) {
  696. Operand o = {};
  697. check_expr(ctx, &o, init);
  698. if (o.mode != Addressing_Constant) {
  699. error(init, "Enumeration value must be a constant");
  700. o.mode = Addressing_Invalid;
  701. }
  702. if (o.mode != Addressing_Invalid) {
  703. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  704. }
  705. if (o.mode != Addressing_Invalid) {
  706. iota = o.value;
  707. } else {
  708. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  709. }
  710. } else {
  711. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  712. }
  713. // NOTE(bill): Skip blank identifiers
  714. if (is_blank_ident(name)) {
  715. continue;
  716. } else if (name == "names") {
  717. error(field, "'names' is a reserved identifier for enumerations");
  718. continue;
  719. }
  720. if (compare_exact_values(Token_Gt, min_value, iota)) {
  721. min_value = iota;
  722. }
  723. if (compare_exact_values(Token_Lt, max_value, iota)) {
  724. max_value = iota;
  725. }
  726. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  727. e->identifier = ident;
  728. e->flags |= EntityFlag_Visited;
  729. e->state = EntityState_Resolved;
  730. if (scope_lookup_current(ctx->scope, name) != nullptr) {
  731. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  732. } else {
  733. add_entity(ctx->checker, ctx->scope, nullptr, e);
  734. array_add(&fields, e);
  735. // TODO(bill): Should I add a use for the enum value?
  736. add_entity_use(ctx, field, e);
  737. }
  738. }
  739. GB_ASSERT(fields.count <= et->fields.count);
  740. enum_type->Enum.fields = fields;
  741. enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope);
  742. enum_type->Enum.min_value = min_value;
  743. enum_type->Enum.max_value = max_value;
  744. }
  745. void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, Ast *node) {
  746. ast_node(bft, BitFieldType, node);
  747. GB_ASSERT(is_type_bit_field(bit_field_type));
  748. auto fields = array_make<Entity*>(ctx->allocator, 0, bft->fields.count);
  749. auto sizes = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  750. auto offsets = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  751. scope_reserve(ctx->scope, bft->fields.count);
  752. u32 curr_offset = 0;
  753. for_array(i, bft->fields) {
  754. Ast *field = bft->fields[i];
  755. GB_ASSERT(field->kind == Ast_FieldValue);
  756. Ast *ident = field->FieldValue.field;
  757. Ast *value = field->FieldValue.value;
  758. if (ident->kind != Ast_Ident) {
  759. error(field, "A bit field value's name must be an identifier");
  760. continue;
  761. }
  762. String name = ident->Ident.token.string;
  763. Operand o = {};
  764. check_expr(ctx, &o, value);
  765. if (o.mode != Addressing_Constant) {
  766. error(value, "Bit field bit size must be a constant");
  767. continue;
  768. }
  769. ExactValue v = exact_value_to_integer(o.value);
  770. if (v.kind != ExactValue_Integer) {
  771. error(value, "Bit field bit size must be a constant integer");
  772. continue;
  773. }
  774. i64 bits_ = big_int_to_i64(&v.value_integer); // TODO(bill): what if the integer is huge?
  775. if (bits_ < 0 || bits_ > 64) {
  776. error(value, "Bit field's bit size must be within the range 1...64, got %lld", cast(long long)bits_);
  777. continue;
  778. }
  779. u32 bits = cast(u32)bits_;
  780. Type *value_type = alloc_type_bit_field_value(bits);
  781. Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type, false);
  782. e->identifier = ident;
  783. e->flags |= EntityFlag_BitFieldValue;
  784. if (!is_blank_ident(name) &&
  785. scope_lookup_current(ctx->scope, name) != nullptr) {
  786. error(ident, "'%.*s' is already declared in this bit field", LIT(name));
  787. } else {
  788. add_entity(ctx->checker, ctx->scope, nullptr, e);
  789. // TODO(bill): Should this entity be "used"?
  790. add_entity_use(ctx, field, e);
  791. array_add(&fields, e);
  792. array_add(&offsets, curr_offset);
  793. array_add(&sizes, bits);
  794. curr_offset += bits;
  795. }
  796. }
  797. GB_ASSERT(fields.count <= bft->fields.count);
  798. bit_field_type->BitField.fields = fields;
  799. bit_field_type->BitField.sizes = sizes;
  800. bit_field_type->BitField.offsets = offsets;
  801. if (bft->align != nullptr) {
  802. i64 custom_align = 1;
  803. if (check_custom_align(ctx, bft->align, &custom_align)) {
  804. bit_field_type->BitField.custom_align = custom_align;
  805. }
  806. }
  807. }
  808. bool is_type_valid_bit_set_range(Type *t) {
  809. if (is_type_integer(t)) {
  810. return true;
  811. }
  812. if (is_type_rune(t)) {
  813. return true;
  814. }
  815. return false;
  816. }
  817. void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *node) {
  818. ast_node(bs, BitSetType, node);
  819. GB_ASSERT(type->kind == Type_BitSet);
  820. i64 const MAX_BITS = 64;
  821. Ast *base = unparen_expr(bs->elem);
  822. if (is_ast_range(base)) {
  823. ast_node(be, BinaryExpr, base);
  824. Operand lhs = {};
  825. Operand rhs = {};
  826. check_expr(c, &lhs, be->left);
  827. check_expr(c, &rhs, be->right);
  828. if (lhs.mode == Addressing_Invalid || rhs.mode == Addressing_Invalid) {
  829. return;
  830. }
  831. convert_to_typed(c, &lhs, rhs.type);
  832. if (lhs.mode == Addressing_Invalid) {
  833. return;
  834. }
  835. convert_to_typed(c, &rhs, lhs.type);
  836. if (rhs.mode == Addressing_Invalid) {
  837. return;
  838. }
  839. if (!are_types_identical(lhs.type, rhs.type)) {
  840. if (lhs.type != t_invalid &&
  841. rhs.type != t_invalid) {
  842. gbString xt = type_to_string(lhs.type);
  843. gbString yt = type_to_string(rhs.type);
  844. gbString expr_str = expr_to_string(bs->elem);
  845. error(bs->elem, "Mismatched types in range '%s' : '%s' vs '%s'", expr_str, xt, yt);
  846. gb_string_free(expr_str);
  847. gb_string_free(yt);
  848. gb_string_free(xt);
  849. }
  850. return;
  851. }
  852. if (!is_type_valid_bit_set_range(lhs.type)) {
  853. gbString str = type_to_string(lhs.type);
  854. error(bs->elem, "'%s' is invalid for an interval expression, expected an integer or rune", str);
  855. gb_string_free(str);
  856. return;
  857. }
  858. if (lhs.mode != Addressing_Constant || rhs.mode != Addressing_Constant) {
  859. error(bs->elem, "Intervals must be constant values");
  860. return;
  861. }
  862. ExactValue iv = exact_value_to_integer(lhs.value);
  863. ExactValue jv = exact_value_to_integer(rhs.value);
  864. GB_ASSERT(iv.kind == ExactValue_Integer);
  865. GB_ASSERT(jv.kind == ExactValue_Integer);
  866. BigInt i = iv.value_integer;
  867. BigInt j = jv.value_integer;
  868. if (big_int_cmp(&i, &j) > 0) {
  869. gbAllocator a = heap_allocator();
  870. String si = big_int_to_string(a, &i);
  871. String sj = big_int_to_string(a, &j);
  872. error(bs->elem, "Lower interval bound larger than upper bound, %.*s .. %.*s", LIT(si), LIT(sj));
  873. gb_free(a, si.text);
  874. gb_free(a, sj.text);
  875. return;
  876. }
  877. Type *t = default_type(lhs.type);
  878. if (bs->underlying != nullptr) {
  879. Type *u = check_type(c, bs->underlying);
  880. if (!is_type_integer(u)) {
  881. gbString ts = type_to_string(u);
  882. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  883. gb_string_free(ts);
  884. return;
  885. }
  886. type->BitSet.underlying = u;
  887. }
  888. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  889. gbAllocator a = heap_allocator();
  890. String s = big_int_to_string(a, &i);
  891. gbString ts = type_to_string(t);
  892. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  893. gb_string_free(ts);
  894. gb_free(a, s.text);
  895. return;
  896. }
  897. if (!check_representable_as_constant(c, iv, t, nullptr)) {
  898. gbAllocator a = heap_allocator();
  899. String s = big_int_to_string(a, &j);
  900. gbString ts = type_to_string(t);
  901. error(bs->elem, "%.*s is not representable by %s", LIT(s), ts);
  902. gb_string_free(ts);
  903. gb_free(a, s.text);
  904. return;
  905. }
  906. i64 lower = big_int_to_i64(&i);
  907. i64 upper = big_int_to_i64(&j);
  908. i64 bits = MAX_BITS;
  909. if (type->BitSet.underlying != nullptr) {
  910. bits = 8*type_size_of(type->BitSet.underlying);
  911. }
  912. if (upper - lower >= bits) {
  913. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", bits, (upper-lower+1));
  914. }
  915. type->BitSet.elem = t;
  916. type->BitSet.lower = lower;
  917. type->BitSet.upper = upper;
  918. } else {
  919. Type *elem = check_type_expr(c, bs->elem, nullptr);
  920. #if 1
  921. if (named_type != nullptr && named_type->kind == Type_Named &&
  922. elem->kind == Type_Enum) {
  923. // NOTE(bill): Anonymous enumeration
  924. String prefix = named_type->Named.name;
  925. String enum_name = concatenate_strings(heap_allocator(), prefix, str_lit(".enum"));
  926. Token token = make_token_ident(enum_name);
  927. Entity *e = alloc_entity_type_name(nullptr, token, nullptr, EntityState_Resolved);
  928. Type *named = alloc_type_named(enum_name, elem, e);
  929. e->type = named;
  930. e->TypeName.is_type_alias = true;
  931. elem = named;
  932. }
  933. #endif
  934. type->BitSet.elem = elem;
  935. if (!is_type_valid_bit_set_elem(elem)) {
  936. error(bs->elem, "Expected an enum type for a bit_set");
  937. } else {
  938. Type *et = base_type(elem);
  939. if (et->kind == Type_Enum) {
  940. if (!is_type_integer(et->Enum.base_type)) {
  941. error(bs->elem, "Enum type for bit_set must be an integer");
  942. return;
  943. }
  944. i64 lower = 0;
  945. i64 upper = 0;
  946. for_array(i, et->Enum.fields) {
  947. Entity *e = et->Enum.fields[i];
  948. if (e->kind != Entity_Constant) {
  949. continue;
  950. }
  951. ExactValue value = exact_value_to_integer(e->Constant.value);
  952. GB_ASSERT(value.kind == ExactValue_Integer);
  953. // NOTE(bill): enum types should be able to store i64 values
  954. i64 x = big_int_to_i64(&value.value_integer);
  955. lower = gb_min(lower, x);
  956. upper = gb_max(upper, x);
  957. }
  958. GB_ASSERT(lower <= upper);
  959. i64 bits = MAX_BITS;
  960. if (bs->underlying != nullptr) {
  961. Type *u = check_type(c, bs->underlying);
  962. if (!is_type_integer(u)) {
  963. gbString ts = type_to_string(u);
  964. error(bs->underlying, "Expected an underlying integer for the bit set, got %s", ts);
  965. gb_string_free(ts);
  966. return;
  967. }
  968. type->BitSet.underlying = u;
  969. bits = 8*type_size_of(u);
  970. }
  971. if (upper - lower >= MAX_BITS) {
  972. error(bs->elem, "bit_set range is greater than %lld bits, %lld bits are required", MAX_BITS, (upper-lower+1));
  973. }
  974. type->BitSet.lower = lower;
  975. type->BitSet.upper = upper;
  976. }
  977. }
  978. }
  979. }
  980. bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  981. if (type == nullptr ||
  982. type == t_invalid) {
  983. return true;
  984. }
  985. Type *t = base_type(type);
  986. Type *s = base_type(specialization);
  987. if (t->kind != s->kind) {
  988. return false;
  989. }
  990. if (t->kind == Type_Struct) {
  991. if (t->Struct.polymorphic_parent == specialization) {
  992. return true;
  993. }
  994. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  995. s->Struct.polymorphic_params != nullptr &&
  996. t->Struct.polymorphic_params != nullptr) {
  997. TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
  998. TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
  999. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1000. for_array(i, s_tuple->variables) {
  1001. Entity *s_e = s_tuple->variables[i];
  1002. Entity *t_e = t_tuple->variables[i];
  1003. Type *st = s_e->type;
  1004. Type *tt = t_e->type;
  1005. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1006. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1007. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1008. GB_ASSERT(e != nullptr);
  1009. if (modify_type) {
  1010. e->kind = Entity_Constant;
  1011. e->Constant.value = t_e->Constant.value;
  1012. e->type = t_e->type;
  1013. }
  1014. } else {
  1015. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1016. }
  1017. }
  1018. if (modify_type) {
  1019. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1020. gb_memmove(specialization, type, gb_size_of(Type));
  1021. }
  1022. return true;
  1023. }
  1024. } else if (t->kind == Type_Union) {
  1025. if (t->Union.polymorphic_parent == specialization) {
  1026. return true;
  1027. }
  1028. if (t->Union.polymorphic_parent == s->Union.polymorphic_parent &&
  1029. s->Union.polymorphic_params != nullptr &&
  1030. t->Union.polymorphic_params != nullptr) {
  1031. TypeTuple *s_tuple = &s->Union.polymorphic_params->Tuple;
  1032. TypeTuple *t_tuple = &t->Union.polymorphic_params->Tuple;
  1033. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  1034. for_array(i, s_tuple->variables) {
  1035. Entity *s_e = s_tuple->variables[i];
  1036. Entity *t_e = t_tuple->variables[i];
  1037. Type *st = s_e->type;
  1038. Type *tt = t_e->type;
  1039. // NOTE(bill, 2018-12-14): This is needed to override polymorphic named constants in types
  1040. if (st->kind == Type_Generic && t_e->kind == Entity_Constant) {
  1041. Entity *e = scope_lookup(st->Generic.scope, st->Generic.name);
  1042. GB_ASSERT(e != nullptr);
  1043. if (modify_type) {
  1044. e->kind = Entity_Constant;
  1045. e->Constant.value = t_e->Constant.value;
  1046. e->type = t_e->type;
  1047. }
  1048. } else {
  1049. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  1050. }
  1051. }
  1052. if (modify_type) {
  1053. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  1054. gb_memmove(specialization, type, gb_size_of(Type));
  1055. }
  1056. return true;
  1057. }
  1058. }
  1059. if (specialization->kind == Type_Named &&
  1060. type->kind != Type_Named) {
  1061. return false;
  1062. }
  1063. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  1064. return true;
  1065. }
  1066. return false;
  1067. }
  1068. Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand operand) {
  1069. bool modify_type = !ctx->no_polymorphic_errors;
  1070. if (!is_operand_value(operand)) {
  1071. if (modify_type) {
  1072. error(operand.expr, "Cannot determine polymorphic type from parameter");
  1073. }
  1074. return t_invalid;
  1075. }
  1076. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  1077. return poly_type;
  1078. }
  1079. if (modify_type) {
  1080. gbString pts = type_to_string(poly_type);
  1081. gbString ots = type_to_string(operand.type);
  1082. defer (gb_string_free(pts));
  1083. defer (gb_string_free(ots));
  1084. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  1085. }
  1086. return t_invalid;
  1087. }
  1088. bool is_expr_from_a_parameter(CheckerContext *ctx, Ast *expr) {
  1089. if (expr == nullptr) {
  1090. return false;
  1091. }
  1092. expr = unparen_expr(expr);
  1093. if (expr->kind == Ast_SelectorExpr) {
  1094. Ast *lhs = expr->SelectorExpr.expr;
  1095. return is_expr_from_a_parameter(ctx, lhs);
  1096. } else if (expr->kind == Ast_Ident) {
  1097. Operand x= {};
  1098. Entity *e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1099. if (e->flags & EntityFlag_Param) {
  1100. return true;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type **out_type_, Ast *expr, bool allow_caller_location) {
  1106. ParameterValue param_value = {};
  1107. if (expr == nullptr) {
  1108. return param_value;
  1109. }
  1110. Operand o = {};
  1111. if (allow_caller_location &&
  1112. expr->kind == Ast_BasicDirective &&
  1113. expr->BasicDirective.name == "caller_location") {
  1114. init_core_source_code_location(ctx->checker);
  1115. param_value.kind = ParameterValue_Location;
  1116. o.type = t_source_code_location;
  1117. } else {
  1118. if (in_type) {
  1119. check_expr_with_type_hint(ctx, &o, expr, in_type);
  1120. } else {
  1121. check_expr(ctx, &o, expr);
  1122. }
  1123. if (is_operand_nil(o)) {
  1124. param_value.kind = ParameterValue_Nil;
  1125. } else if (o.mode != Addressing_Constant) {
  1126. if (expr->kind == Ast_ProcLit) {
  1127. param_value.kind = ParameterValue_Constant;
  1128. param_value.value = exact_value_procedure(expr);
  1129. } else {
  1130. Entity *e = nullptr;
  1131. // if (o.mode == Addressing_Value && is_type_proc(o.type)) {
  1132. if (o.mode == Addressing_Value || o.mode == Addressing_Variable) {
  1133. Operand x = {};
  1134. if (expr->kind == Ast_Ident) {
  1135. e = check_ident(ctx, &x, expr, nullptr, nullptr, false);
  1136. } else if (expr->kind == Ast_SelectorExpr) {
  1137. e = check_selector(ctx, &x, expr, nullptr);
  1138. }
  1139. }
  1140. if (e != nullptr) {
  1141. if (e->kind == Entity_Procedure) {
  1142. param_value.kind = ParameterValue_Constant;
  1143. param_value.value = exact_value_procedure(e->identifier);
  1144. add_entity_use(ctx, e->identifier, e);
  1145. } else {
  1146. if (e->flags & EntityFlag_Param) {
  1147. error(expr, "Default parameter cannot be another parameter");
  1148. } else {
  1149. if (is_expr_from_a_parameter(ctx, expr)) {
  1150. error(expr, "Default parameter cannot be another parameter");
  1151. } else {
  1152. param_value.kind = ParameterValue_Value;
  1153. param_value.ast_value = expr;
  1154. add_entity_use(ctx, e->identifier, e);
  1155. }
  1156. }
  1157. }
  1158. } else if (allow_caller_location && o.mode == Addressing_Context) {
  1159. param_value.kind = ParameterValue_Value;
  1160. param_value.ast_value = expr;
  1161. } else {
  1162. error(expr, "Default parameter must be a constant");
  1163. }
  1164. }
  1165. } else {
  1166. if (o.value.kind != ExactValue_Invalid) {
  1167. param_value.kind = ParameterValue_Constant;
  1168. param_value.value = o.value;
  1169. } else {
  1170. error(o.expr, "Invalid constant parameter");
  1171. }
  1172. }
  1173. }
  1174. if (in_type) {
  1175. check_assignment(ctx, &o, in_type, str_lit("parameter value"));
  1176. }
  1177. if (out_type_) *out_type_ = default_type(o.type);
  1178. return param_value;
  1179. }
  1180. Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> *operands) {
  1181. if (_params == nullptr) {
  1182. return nullptr;
  1183. }
  1184. bool allow_polymorphic_types = ctx->allow_polymorphic_types;
  1185. bool success = true;
  1186. ast_node(field_list, FieldList, _params);
  1187. Array<Ast *> params = field_list->list;
  1188. if (params.count == 0) {
  1189. if (success_) *success_ = success;
  1190. return nullptr;
  1191. }
  1192. isize variable_count = 0;
  1193. for_array(i, params) {
  1194. Ast *field = params[i];
  1195. if (ast_node_expect(field, Ast_Field)) {
  1196. ast_node(f, Field, field);
  1197. variable_count += gb_max(f->names.count, 1);
  1198. }
  1199. }
  1200. isize min_variable_count = variable_count;
  1201. for (isize i = params.count-1; i >= 0; i--) {
  1202. Ast *field = params[i];
  1203. if (field->kind == Ast_Field) {
  1204. ast_node(f, Field, field);
  1205. if (f->default_value == nullptr) {
  1206. break;
  1207. }
  1208. min_variable_count--;
  1209. }
  1210. }
  1211. bool is_variadic = false;
  1212. isize variadic_index = -1;
  1213. bool is_c_vararg = false;
  1214. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  1215. for_array(i, params) {
  1216. Ast *param = params[i];
  1217. if (param->kind != Ast_Field) {
  1218. continue;
  1219. }
  1220. ast_node(p, Field, param);
  1221. Ast *type_expr = unparen_expr(p->type);
  1222. Type *type = nullptr;
  1223. Ast *default_value = unparen_expr(p->default_value);
  1224. ParameterValue param_value = {};
  1225. bool is_type_param = false;
  1226. bool is_type_polymorphic_type = false;
  1227. bool detemine_type_from_operand = false;
  1228. Type *specialization = nullptr;
  1229. bool is_using = (p->flags&FieldFlag_using) != 0;
  1230. if (type_expr == nullptr) {
  1231. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, true);
  1232. } else {
  1233. if (type_expr->kind == Ast_Ellipsis) {
  1234. type_expr = type_expr->Ellipsis.expr;
  1235. is_variadic = true;
  1236. variadic_index = variables.count;
  1237. if (p->names.count != 1) {
  1238. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  1239. success = false;
  1240. }
  1241. }
  1242. if (type_expr->kind == Ast_TypeidType) {
  1243. ast_node(tt, TypeidType, type_expr);
  1244. if (tt->specialization) {
  1245. specialization = check_type(ctx, tt->specialization);
  1246. if (specialization == t_invalid){
  1247. specialization = nullptr;
  1248. }
  1249. if (operands != nullptr) {
  1250. detemine_type_from_operand = true;
  1251. type = t_invalid;
  1252. } else {
  1253. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  1254. }
  1255. } else {
  1256. type = t_typeid;
  1257. }
  1258. } else {
  1259. bool prev = ctx->allow_polymorphic_types;
  1260. if (operands != nullptr) {
  1261. ctx->allow_polymorphic_types = true;
  1262. }
  1263. type = check_type(ctx, type_expr);
  1264. ctx->allow_polymorphic_types = prev;
  1265. if (is_type_polymorphic(type)) {
  1266. is_type_polymorphic_type = true;
  1267. }
  1268. }
  1269. if (default_value != nullptr) {
  1270. if (type_expr != nullptr && type_expr->kind == Ast_TypeidType) {
  1271. error(type_expr, "A type parameter may not have a default value");
  1272. } else {
  1273. param_value = handle_parameter_value(ctx, type, nullptr, default_value, true);
  1274. }
  1275. }
  1276. }
  1277. if (type == nullptr) {
  1278. error(param, "Invalid parameter type");
  1279. type = t_invalid;
  1280. }
  1281. if (is_type_untyped(type)) {
  1282. if (is_type_untyped_undef(type)) {
  1283. error(param, "Cannot determine parameter type from ---");
  1284. } else {
  1285. error(param, "Cannot determine parameter type from a nil");
  1286. }
  1287. type = t_invalid;
  1288. }
  1289. if (is_type_empty_union(type)) {
  1290. gbString str = type_to_string(type);
  1291. error(param, "Invalid use of an empty union '%s'", str);
  1292. gb_string_free(str);
  1293. type = t_invalid;
  1294. }
  1295. if (p->flags&FieldFlag_c_vararg) {
  1296. if (p->type == nullptr ||
  1297. p->type->kind != Ast_Ellipsis) {
  1298. error(param, "'#c_vararg' can only be applied to variadic type fields");
  1299. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  1300. } else {
  1301. is_c_vararg = true;
  1302. }
  1303. }
  1304. if (p->flags&FieldFlag_in) {
  1305. if (is_type_param) {
  1306. error(param, "'in' cannot be applied to a type parameter");
  1307. p->flags &= ~FieldFlag_in;
  1308. } else if (is_variadic) {
  1309. error(param, "'in' cannot be applied to a variadic parameter");
  1310. p->flags &= ~FieldFlag_in;
  1311. }
  1312. }
  1313. bool is_in = (p->flags&FieldFlag_in) != 0;
  1314. for_array(j, p->names) {
  1315. Ast *name = p->names[j];
  1316. bool is_poly_name = false;
  1317. switch (name->kind) {
  1318. case Ast_Ident:
  1319. break;
  1320. case Ast_PolyType:
  1321. GB_ASSERT(name->PolyType.specialization == nullptr);
  1322. is_poly_name = true;
  1323. name = name->PolyType.type;
  1324. break;
  1325. }
  1326. if (!ast_node_expect(name, Ast_Ident)) {
  1327. continue;
  1328. }
  1329. if (is_poly_name) {
  1330. if (type != nullptr && type_expr->kind == Ast_TypeidType) {
  1331. is_type_param = true;
  1332. } else {
  1333. if (param_value.kind != ParameterValue_Invalid) {
  1334. error(default_value, "Constant parameters cannot have a default value");
  1335. param_value.kind = ParameterValue_Invalid;
  1336. }
  1337. }
  1338. }
  1339. Entity *param = nullptr;
  1340. if (is_type_param) {
  1341. if (operands != nullptr) {
  1342. Operand o = (*operands)[variables.count];
  1343. if (o.mode == Addressing_Type) {
  1344. type = o.type;
  1345. } else {
  1346. if (!ctx->no_polymorphic_errors) {
  1347. error(o.expr, "Expected a type to assign to the type parameter");
  1348. }
  1349. success = false;
  1350. type = t_invalid;
  1351. }
  1352. if (is_type_polymorphic(type)) {
  1353. gbString str = type_to_string(type);
  1354. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  1355. gb_string_free(str);
  1356. success = false;
  1357. type = t_invalid;
  1358. }
  1359. bool modify_type = !ctx->no_polymorphic_errors;
  1360. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  1361. if (!ctx->no_polymorphic_errors) {
  1362. gbString t = type_to_string(type);
  1363. gbString s = type_to_string(specialization);
  1364. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  1365. gb_string_free(s);
  1366. gb_string_free(t);
  1367. }
  1368. success = false;
  1369. type = t_invalid;
  1370. }
  1371. }
  1372. if (p->flags&FieldFlag_auto_cast) {
  1373. error(name, "'auto_cast' can only be applied to variable fields");
  1374. p->flags &= ~FieldFlag_auto_cast;
  1375. }
  1376. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  1377. param->TypeName.is_type_alias = true;
  1378. } else {
  1379. ExactValue poly_const = {};
  1380. if (operands != nullptr && variables.count < operands->count) {
  1381. Operand op = (*operands)[variables.count];
  1382. if (is_type_polymorphic_type) {
  1383. type = determine_type_from_polymorphic(ctx, type, op);
  1384. if (type == t_invalid) {
  1385. success = false;
  1386. } else if (!ctx->no_polymorphic_errors) {
  1387. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  1388. is_type_polymorphic_type = false;
  1389. }
  1390. }
  1391. if (is_poly_name) {
  1392. if (op.mode == Addressing_Constant) {
  1393. poly_const = op.value;
  1394. } else {
  1395. success = false;
  1396. }
  1397. }
  1398. }
  1399. if (p->flags&FieldFlag_no_alias) {
  1400. if (!is_type_pointer(type)) {
  1401. error(name, "'#no_alias' can only be applied to fields of pointer type");
  1402. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1403. }
  1404. }
  1405. if (is_poly_name) {
  1406. if (p->flags&FieldFlag_no_alias) {
  1407. error(name, "'#no_alias' can only be applied to non constant values");
  1408. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  1409. }
  1410. if (p->flags&FieldFlag_auto_cast) {
  1411. error(name, "'auto_cast' can only be applied to variable fields");
  1412. p->flags &= ~FieldFlag_auto_cast;
  1413. }
  1414. if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
  1415. gbString str = type_to_string(type);
  1416. error(params[i], "A parameter must be a valid constant type, got %s", str);
  1417. gb_string_free(str);
  1418. }
  1419. param = alloc_entity_const_param(scope, name->Ident.token, type, poly_const, is_type_polymorphic(type));
  1420. } else {
  1421. param = alloc_entity_param(scope, name->Ident.token, type, is_using, is_in);
  1422. param->Variable.param_value = param_value;
  1423. }
  1424. }
  1425. if (p->flags&FieldFlag_no_alias) {
  1426. param->flags |= EntityFlag_NoAlias;
  1427. }
  1428. if (p->flags&FieldFlag_auto_cast) {
  1429. param->flags |= EntityFlag_AutoCast;
  1430. }
  1431. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  1432. add_entity(ctx->checker, scope, name, param);
  1433. if (is_using) {
  1434. add_entity_use(ctx, name, param);
  1435. }
  1436. array_add(&variables, param);
  1437. }
  1438. }
  1439. if (is_variadic) {
  1440. GB_ASSERT(variadic_index >= 0);
  1441. }
  1442. if (is_variadic) {
  1443. GB_ASSERT(params.count > 0);
  1444. // NOTE(bill): Change last variadic parameter to be a slice
  1445. // Custom Calling convention for variadic parameters
  1446. Entity *end = variables[variadic_index];
  1447. end->type = alloc_type_slice(end->type);
  1448. end->flags |= EntityFlag_Ellipsis;
  1449. if (is_c_vararg) {
  1450. end->flags |= EntityFlag_CVarArg;
  1451. }
  1452. }
  1453. isize specialization_count = 0;
  1454. if (scope != nullptr) {
  1455. for_array(i, scope->elements.entries) {
  1456. Entity *e = scope->elements.entries[i].value;
  1457. if (e->kind == Entity_TypeName) {
  1458. Type *t = e->type;
  1459. if (t->kind == Type_Generic &&
  1460. t->Generic.specialized != nullptr) {
  1461. specialization_count += 1;
  1462. }
  1463. }
  1464. }
  1465. }
  1466. Type *tuple = alloc_type_tuple();
  1467. tuple->Tuple.variables = variables;
  1468. if (success_) *success_ = success;
  1469. if (specialization_count_) *specialization_count_ = specialization_count;
  1470. if (is_variadic_) *is_variadic_ = is_variadic;
  1471. if (variadic_index_) *variadic_index_ = variadic_index;
  1472. return tuple;
  1473. }
  1474. Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_results) {
  1475. if (_results == nullptr) {
  1476. return nullptr;
  1477. }
  1478. ast_node(field_list, FieldList, _results);
  1479. Array<Ast *> results = field_list->list;
  1480. if (results.count == 0) {
  1481. return nullptr;
  1482. }
  1483. Type *tuple = alloc_type_tuple();
  1484. isize variable_count = 0;
  1485. for_array(i, results) {
  1486. Ast *field = results[i];
  1487. if (ast_node_expect(field, Ast_Field)) {
  1488. ast_node(f, Field, field);
  1489. variable_count += gb_max(f->names.count, 1);
  1490. }
  1491. }
  1492. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  1493. for_array(i, results) {
  1494. ast_node(field, Field, results[i]);
  1495. Ast *default_value = unparen_expr(field->default_value);
  1496. ParameterValue param_value = {};
  1497. Type *type = nullptr;
  1498. if (field->type == nullptr) {
  1499. param_value = handle_parameter_value(ctx, nullptr, &type, default_value, false);
  1500. } else {
  1501. type = check_type(ctx, field->type);
  1502. if (default_value != nullptr) {
  1503. param_value = handle_parameter_value(ctx, type, nullptr, default_value, false);
  1504. }
  1505. }
  1506. if (type == nullptr) {
  1507. error(results[i], "Invalid parameter type");
  1508. type = t_invalid;
  1509. }
  1510. if (is_type_untyped(type)) {
  1511. error(results[i], "Cannot determine parameter type from a nil");
  1512. type = t_invalid;
  1513. }
  1514. if (field->names.count == 0) {
  1515. Token token = ast_token(field->type);
  1516. token.string = str_lit("");
  1517. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1518. param->Variable.param_value = param_value;
  1519. array_add(&variables, param);
  1520. } else {
  1521. for_array(j, field->names) {
  1522. Token token = ast_token(results[i]);
  1523. if (field->type != nullptr) {
  1524. token = ast_token(field->type);
  1525. }
  1526. token.string = str_lit("");
  1527. Ast *name = field->names[j];
  1528. if (name->kind != Ast_Ident) {
  1529. error(name, "Expected an identifer for as the field name");
  1530. } else {
  1531. token = name->Ident.token;
  1532. }
  1533. if (is_blank_ident(token)) {
  1534. error(name, "Result value cannot be a blank identifer `_`");
  1535. }
  1536. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1537. param->flags |= EntityFlag_Result;
  1538. param->Variable.param_value = param_value;
  1539. array_add(&variables, param);
  1540. add_entity(ctx->checker, scope, name, param);
  1541. // NOTE(bill): Removes `declared but not used` when using -vet
  1542. add_entity_use(ctx, name, param);
  1543. }
  1544. }
  1545. }
  1546. for_array(i, variables) {
  1547. String x = variables[i]->token.string;
  1548. if (x.len == 0 || is_blank_ident(x)) {
  1549. continue;
  1550. }
  1551. for (isize j = i+1; j < variables.count; j++) {
  1552. String y = variables[j]->token.string;
  1553. if (y.len == 0 || is_blank_ident(y)) {
  1554. continue;
  1555. }
  1556. if (x == y) {
  1557. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1558. }
  1559. }
  1560. }
  1561. tuple->Tuple.variables = variables;
  1562. return tuple;
  1563. }
  1564. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
  1565. Type *new_type = original_type;
  1566. if (is_type_boolean(original_type)) {
  1567. Type *t = core_type(base_type(new_type));
  1568. if (t == t_bool) {
  1569. return t_llvm_bool;
  1570. }
  1571. return new_type;
  1572. }
  1573. if (build_context.ODIN_ARCH == "386") {
  1574. return new_type;
  1575. }
  1576. if (is_type_simd_vector(original_type)) {
  1577. return new_type;
  1578. }
  1579. if (build_context.ODIN_OS == "windows") {
  1580. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  1581. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  1582. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  1583. Type *bt = core_type(original_type);
  1584. switch (bt->kind) {
  1585. // Okay to pass by value (usually)
  1586. // Especially the only Odin types
  1587. case Type_Basic: {
  1588. i64 sz = bt->Basic.size;
  1589. // if (sz > 8 && build_context.word_size < 8) {
  1590. if (sz > 8) {
  1591. new_type = alloc_type_pointer(original_type);
  1592. }
  1593. break;
  1594. }
  1595. case Type_Pointer: break;
  1596. case Type_Proc: break; // NOTE(bill): Just a pointer
  1597. // Odin specific
  1598. case Type_Slice:
  1599. case Type_Array:
  1600. case Type_DynamicArray:
  1601. case Type_Map:
  1602. case Type_Union:
  1603. // Could be in C too
  1604. case Type_Struct:
  1605. {
  1606. i64 align = type_align_of(original_type);
  1607. i64 size = type_size_of(original_type);
  1608. switch (8*size) {
  1609. case 8: new_type = t_u8; break;
  1610. case 16: new_type = t_u16; break;
  1611. case 32: new_type = t_u32; break;
  1612. case 64: new_type = t_u64; break;
  1613. default:
  1614. new_type = alloc_type_pointer(original_type);
  1615. break;
  1616. }
  1617. break;
  1618. }
  1619. }
  1620. } else if (build_context.ODIN_OS == "linux" ||
  1621. build_context.ODIN_OS == "osx") {
  1622. Type *bt = core_type(original_type);
  1623. switch (bt->kind) {
  1624. // Okay to pass by value (usually)
  1625. // Especially the only Odin types
  1626. case Type_Basic: {
  1627. i64 sz = bt->Basic.size;
  1628. // if (sz > 8 && build_context.word_size < 8) {
  1629. if (sz > 8) {
  1630. new_type = alloc_type_pointer(original_type);
  1631. }
  1632. break;
  1633. }
  1634. case Type_Pointer: break;
  1635. case Type_Proc: break; // NOTE(bill): Just a pointer
  1636. // Odin specific
  1637. case Type_Slice:
  1638. case Type_Array:
  1639. case Type_DynamicArray:
  1640. case Type_Map:
  1641. case Type_Union:
  1642. // Could be in C too
  1643. case Type_Struct: {
  1644. i64 align = type_align_of(original_type);
  1645. i64 size = type_size_of(original_type);
  1646. if (8*size > 16) {
  1647. new_type = alloc_type_pointer(original_type);
  1648. }
  1649. break;
  1650. }
  1651. }
  1652. } else {
  1653. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1654. // their architectures
  1655. }
  1656. return new_type;
  1657. }
  1658. Type *reduce_tuple_to_single_type(Type *original_type) {
  1659. if (original_type != nullptr) {
  1660. Type *t = core_type(original_type);
  1661. if (t->kind == Type_Tuple && t->Tuple.variables.count == 1) {
  1662. return t->Tuple.variables[0]->type;
  1663. }
  1664. }
  1665. return original_type;
  1666. }
  1667. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type) {
  1668. Type *new_type = original_type;
  1669. if (new_type == nullptr) {
  1670. return nullptr;
  1671. }
  1672. GB_ASSERT(is_type_tuple(original_type));
  1673. Type *single_type = reduce_tuple_to_single_type(original_type);
  1674. if (is_type_simd_vector(single_type)) {
  1675. return new_type;
  1676. }
  1677. if (build_context.ODIN_OS == "windows") {
  1678. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  1679. // NOTE(bill): This is just reversed engineered from LLVM IR output
  1680. switch (bt->kind) {
  1681. // Okay to pass by value
  1682. // Especially the only Odin types
  1683. case Type_Pointer: break;
  1684. case Type_Proc: break; // NOTE(bill): Just a pointer
  1685. case Type_Basic: break;
  1686. default: {
  1687. i64 align = type_align_of(original_type);
  1688. i64 size = type_size_of(original_type);
  1689. switch (8*size) {
  1690. #if 1
  1691. case 8: new_type = t_u8; break;
  1692. case 16: new_type = t_u16; break;
  1693. case 32: new_type = t_u32; break;
  1694. case 64: new_type = t_u64; break;
  1695. #endif
  1696. }
  1697. break;
  1698. }
  1699. }
  1700. } else if (build_context.ODIN_OS == "linux") {
  1701. } else {
  1702. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1703. // their architectures
  1704. }
  1705. if (new_type != original_type) {
  1706. Type *tuple = alloc_type_tuple();
  1707. auto variables = array_make<Entity *>(a, 0, 1);
  1708. array_add(&variables, alloc_entity_param(original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false));
  1709. tuple->Tuple.variables = variables;
  1710. new_type = tuple;
  1711. }
  1712. // return reduce_tuple_to_single_type(new_type);
  1713. return new_type;
  1714. }
  1715. bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  1716. if (abi_return_type == nullptr) {
  1717. return false;
  1718. }
  1719. Type *single_type = reduce_tuple_to_single_type(abi_return_type);
  1720. if (is_type_simd_vector(single_type)) {
  1721. return false;
  1722. }
  1723. if (build_context.ODIN_OS == "windows") {
  1724. i64 size = 8*type_size_of(abi_return_type);
  1725. switch (size) {
  1726. case 0:
  1727. case 8:
  1728. case 16:
  1729. case 32:
  1730. case 64:
  1731. return false;
  1732. default:
  1733. return true;
  1734. }
  1735. }
  1736. return false;
  1737. }
  1738. // NOTE(bill): 'operands' is for generating non generic procedure type
  1739. bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, Array<Operand> *operands) {
  1740. ast_node(pt, ProcType, proc_type_node);
  1741. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  1742. ctx->polymorphic_scope = ctx->scope;
  1743. }
  1744. CheckerContext c_ = *ctx;
  1745. CheckerContext *c = &c_;
  1746. c->curr_proc_sig = type;
  1747. c->in_proc_sig = true;
  1748. bool variadic = false;
  1749. isize variadic_index = -1;
  1750. bool success = true;
  1751. isize specialization_count = 0;
  1752. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  1753. Type *results = check_get_results(c, c->scope, pt->results);
  1754. isize param_count = 0;
  1755. isize result_count = 0;
  1756. if (params) param_count = params ->Tuple.variables.count;
  1757. if (results) result_count = results->Tuple.variables.count;
  1758. if (param_count > 0) {
  1759. for_array(i, params->Tuple.variables) {
  1760. Entity *param = params->Tuple.variables[i];
  1761. if (param->kind == Entity_Variable) {
  1762. ParameterValue pv = param->Variable.param_value;
  1763. if (pv.kind == ParameterValue_Constant &&
  1764. pv.value.kind == ExactValue_Procedure) {
  1765. type->Proc.has_proc_default_values = true;
  1766. break;
  1767. }
  1768. }
  1769. }
  1770. }
  1771. if (result_count > 0) {
  1772. Entity *first = results->Tuple.variables[0];
  1773. type->Proc.has_named_results = first->token.string != "";
  1774. }
  1775. ProcCallingConvention cc = pt->calling_convention;
  1776. if (cc == ProcCC_ForeignBlockDefault) {
  1777. cc = ProcCC_CDecl;
  1778. if (c->foreign_context.default_cc > 0) {
  1779. cc = c->foreign_context.default_cc;
  1780. }
  1781. }
  1782. GB_ASSERT(cc > 0);
  1783. type->Proc.node = proc_type_node;
  1784. type->Proc.scope = c->scope;
  1785. type->Proc.params = params;
  1786. type->Proc.param_count = cast(i32)param_count;
  1787. type->Proc.results = results;
  1788. type->Proc.result_count = cast(i32)result_count;
  1789. type->Proc.variadic = variadic;
  1790. type->Proc.variadic_index = cast(i32)variadic_index;
  1791. type->Proc.calling_convention = cc;
  1792. type->Proc.is_polymorphic = pt->generic;
  1793. type->Proc.specialization_count = specialization_count;
  1794. type->Proc.diverging = pt->diverging;
  1795. type->Proc.tags = pt->tags;
  1796. if (param_count > 0) {
  1797. Entity *end = params->Tuple.variables[param_count-1];
  1798. if (end->flags&EntityFlag_CVarArg) {
  1799. if (cc == ProcCC_StdCall || cc == ProcCC_CDecl) {
  1800. type->Proc.c_vararg = true;
  1801. } else {
  1802. error(end->token, "Calling convention does not support #c_vararg");
  1803. }
  1804. }
  1805. }
  1806. bool is_polymorphic = false;
  1807. for (isize i = 0; i < param_count; i++) {
  1808. Entity *e = params->Tuple.variables[i];
  1809. if (e->kind != Entity_Variable) {
  1810. is_polymorphic = true;
  1811. break;
  1812. } else if (is_type_polymorphic(e->type)) {
  1813. is_polymorphic = true;
  1814. break;
  1815. }
  1816. }
  1817. type->Proc.is_polymorphic = is_polymorphic;
  1818. type->Proc.abi_compat_params = array_make<Type *>(c->allocator, param_count);
  1819. for (isize i = 0; i < param_count; i++) {
  1820. Entity *e = type->Proc.params->Tuple.variables[i];
  1821. if (e->kind == Entity_Variable) {
  1822. Type *original_type = e->type;
  1823. Type *new_type = type_to_abi_compat_param_type(c->allocator, original_type);
  1824. type->Proc.abi_compat_params[i] = new_type;
  1825. }
  1826. }
  1827. // NOTE(bill): The types are the same
  1828. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(c->allocator, type->Proc.results);
  1829. type->Proc.return_by_pointer = abi_compat_return_by_pointer(c->allocator, pt->calling_convention, type->Proc.abi_compat_result_type);
  1830. return success;
  1831. }
  1832. i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
  1833. if (e == nullptr) {
  1834. return 0;
  1835. }
  1836. if (e->kind == Ast_UnaryExpr &&
  1837. e->UnaryExpr.op.kind == Token_Question) {
  1838. return -1;
  1839. }
  1840. check_expr_or_type(ctx, o, e);
  1841. if (o->mode == Addressing_Type && o->type->kind == Type_Generic) {
  1842. if (ctx->allow_polymorphic_types) {
  1843. if (o->type->Generic.specialized) {
  1844. o->type->Generic.specialized = nullptr;
  1845. error(o->expr, "Polymorphic array length cannot have a specialization");
  1846. }
  1847. return 0;
  1848. }
  1849. }
  1850. if (o->mode != Addressing_Constant) {
  1851. if (o->mode != Addressing_Invalid) {
  1852. o->mode = Addressing_Invalid;
  1853. error(e, "Array count must be a constant");
  1854. }
  1855. return 0;
  1856. }
  1857. Type *type = core_type(o->type);
  1858. if (is_type_untyped(type) || is_type_integer(type)) {
  1859. if (o->value.kind == ExactValue_Integer) {
  1860. BigInt count = o->value.value_integer;
  1861. if (o->value.value_integer.neg) {
  1862. gbAllocator a = heap_allocator();
  1863. String str = big_int_to_string(a, &count);
  1864. error(e, "Invalid negative array count, %.*s", LIT(str));
  1865. gb_free(a, str.text);
  1866. return 0;
  1867. }
  1868. switch (count.len) {
  1869. case 0: return 0;
  1870. case 1: return count.d.word;
  1871. }
  1872. gbAllocator a = heap_allocator();
  1873. String str = big_int_to_string(a, &count);
  1874. error(e, "Array count too large, %.*s", LIT(str));
  1875. gb_free(a, str.text);
  1876. return 0;
  1877. }
  1878. }
  1879. error(e, "Array count must be an integer");
  1880. return 0;
  1881. }
  1882. Type *make_optional_ok_type(Type *value) {
  1883. // LEAK TODO(bill): probably don't reallocate everything here and reuse the same one for the same type if possible
  1884. gbAllocator a = heap_allocator();
  1885. bool typed = true;
  1886. Type *t = alloc_type_tuple();
  1887. array_init(&t->Tuple.variables, a, 0, 2);
  1888. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, value, false, 0));
  1889. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1));
  1890. return t;
  1891. }
  1892. void init_map_entry_type(Type *type) {
  1893. GB_ASSERT(type->kind == Type_Map);
  1894. if (type->Map.entry_type != nullptr) return;
  1895. // NOTE(bill): The preload types may have not been set yet
  1896. GB_ASSERT(t_map_key != nullptr);
  1897. gbAllocator a = heap_allocator();
  1898. Type *entry_type = alloc_type_struct();
  1899. /*
  1900. struct {
  1901. hash: __MapKey;
  1902. next: int;
  1903. key: Key;
  1904. value: Value;
  1905. }
  1906. */
  1907. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  1908. Scope *s = create_scope(builtin_pkg->scope, a);
  1909. auto fields = array_make<Entity *>(a, 0, 3);
  1910. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_key, false, 0, EntityState_Resolved));
  1911. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, EntityState_Resolved));
  1912. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, 2, EntityState_Resolved));
  1913. entry_type->Struct.fields = fields;
  1914. // type_set_offsets(a, entry_type);
  1915. type->Map.entry_type = entry_type;
  1916. }
  1917. void init_map_internal_types(Type *type) {
  1918. GB_ASSERT(type->kind == Type_Map);
  1919. init_map_entry_type(type);
  1920. if (type->Map.internal_type != nullptr) return;
  1921. if (type->Map.generated_struct_type != nullptr) return;
  1922. Type *key = type->Map.key;
  1923. Type *value = type->Map.value;
  1924. GB_ASSERT(key != nullptr);
  1925. GB_ASSERT(value != nullptr);
  1926. Type *generated_struct_type = alloc_type_struct();
  1927. /*
  1928. struct {
  1929. hashes: []int;
  1930. entries: [dynamic]EntryType;
  1931. }
  1932. */
  1933. gbAllocator a = heap_allocator();
  1934. Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
  1935. Scope *s = create_scope(builtin_pkg->scope, a);
  1936. Type *hashes_type = alloc_type_slice(t_int);
  1937. Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
  1938. auto fields = array_make<Entity *>(a, 0, 2);
  1939. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved));
  1940. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved));
  1941. generated_struct_type->Struct.fields = fields;
  1942. type_set_offsets(generated_struct_type);
  1943. type->Map.generated_struct_type = generated_struct_type;
  1944. type->Map.internal_type = generated_struct_type;
  1945. type->Map.lookup_result_type = make_optional_ok_type(value);
  1946. }
  1947. void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
  1948. GB_ASSERT(type->kind == Type_Map);
  1949. ast_node(mt, MapType, node);
  1950. Type *key = check_type(ctx, mt->key);
  1951. Type *value = check_type(ctx, mt->value);
  1952. if (!is_type_valid_for_keys(key)) {
  1953. if (is_type_boolean(key)) {
  1954. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  1955. } else {
  1956. gbString str = type_to_string(key);
  1957. error(node, "Invalid type of a key for a map, got '%s'", str);
  1958. gb_string_free(str);
  1959. }
  1960. }
  1961. type->Map.key = key;
  1962. type->Map.value = value;
  1963. if (is_type_string(key)) {
  1964. add_package_dependency(ctx, "runtime", "default_hash_string");
  1965. }
  1966. init_core_map_type(ctx->checker);
  1967. init_map_internal_types(type);
  1968. // error(node, "'map' types are not yet implemented");
  1969. }
  1970. bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_type) {
  1971. GB_ASSERT_NOT_NULL(type);
  1972. if (e == nullptr) {
  1973. *type = t_invalid;
  1974. return true;
  1975. }
  1976. switch (e->kind) {
  1977. case_ast_node(i, Ident, e);
  1978. Operand o = {};
  1979. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  1980. gbString err_str = nullptr;
  1981. defer (gb_string_free(err_str));
  1982. switch (o.mode) {
  1983. case Addressing_Invalid:
  1984. break;
  1985. case Addressing_Type: {
  1986. *type = o.type;
  1987. if (!ctx->in_polymorphic_specialization) {
  1988. Type *t = base_type(o.type);
  1989. if (t != nullptr && is_type_polymorphic_record_unspecialized(t)) {
  1990. err_str = expr_to_string(e);
  1991. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  1992. return true;
  1993. }
  1994. }
  1995. // if (ctx->type_level == 0 && entity->state == EntityState_InProgress) {
  1996. // error(entity->token, "Illegal declaration cycle of `%.*s`", LIT(entity->token.string));
  1997. // for_array(j, *ctx->type_path) {
  1998. // Entity *k = (*ctx->type_path)[j];
  1999. // error(k->token, "\t%.*s refers to", LIT(k->token.string));
  2000. // }
  2001. // error(entity->token, "\t%.*s", LIT(entity->token.string));
  2002. // *type = t_invalid;
  2003. // }
  2004. return true;
  2005. }
  2006. case Addressing_NoValue:
  2007. err_str = expr_to_string(e);
  2008. error(e, "'%s' used as a type", err_str);
  2009. break;
  2010. default:
  2011. err_str = expr_to_string(e);
  2012. error(e, "'%s' used as a type when not a type", err_str);
  2013. break;
  2014. }
  2015. case_end;
  2016. case_ast_node(ht, HelperType, e);
  2017. return check_type_internal(ctx, ht->type, type, named_type);
  2018. case_end;
  2019. case_ast_node(dt, DistinctType, e);
  2020. error(e, "Invalid use of a distinct type");
  2021. // NOTE(bill): Treat it as a HelperType to remove errors
  2022. return check_type_internal(ctx, dt->type, type, named_type);
  2023. case_end;
  2024. case_ast_node(tt, TypeidType, e);
  2025. e->tav.mode = Addressing_Type;
  2026. e->tav.type = t_typeid;
  2027. *type = t_typeid;
  2028. set_base_type(named_type, *type);
  2029. return true;
  2030. case_end;
  2031. case_ast_node(pt, PolyType, e);
  2032. Ast *ident = pt->type;
  2033. if (ident->kind != Ast_Ident) {
  2034. error(ident, "Expected an identifier after the $");
  2035. *type = t_invalid;
  2036. return false;
  2037. }
  2038. Token token = ident->Ident.token;
  2039. Type *specific = nullptr;
  2040. if (pt->specialization != nullptr) {
  2041. CheckerContext c = *ctx;
  2042. c.in_polymorphic_specialization = true;
  2043. Ast *s = pt->specialization;
  2044. specific = check_type(&c, s);
  2045. }
  2046. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  2047. if (ctx->allow_polymorphic_types) {
  2048. Scope *ps = ctx->polymorphic_scope;
  2049. Scope *s = ctx->scope;
  2050. Scope *entity_scope = s;
  2051. if (ps != nullptr && ps != s) {
  2052. // TODO(bill): Is this check needed?
  2053. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  2054. entity_scope = ps;
  2055. }
  2056. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  2057. t->Generic.entity = e;
  2058. e->TypeName.is_type_alias = true;
  2059. e->state = EntityState_Resolved;
  2060. add_entity(ctx->checker, ps, ident, e);
  2061. add_entity(ctx->checker, s, ident, e);
  2062. } else {
  2063. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  2064. *type = t_invalid;
  2065. return false;
  2066. }
  2067. *type = t;
  2068. set_base_type(named_type, *type);
  2069. return true;
  2070. case_end;
  2071. case_ast_node(se, SelectorExpr, e);
  2072. Operand o = {};
  2073. check_selector(ctx, &o, e, nullptr);
  2074. gbString err_str;
  2075. switch (o.mode) {
  2076. case Addressing_Invalid:
  2077. break;
  2078. case Addressing_Type:
  2079. GB_ASSERT(o.type != nullptr);
  2080. *type = o.type;
  2081. return true;
  2082. case Addressing_NoValue:
  2083. err_str = expr_to_string(e);
  2084. error(e, "'%s' used as a type", err_str);
  2085. gb_string_free(err_str);
  2086. break;
  2087. default:
  2088. err_str = expr_to_string(e);
  2089. error(e, "'%s' is not a type", err_str);
  2090. gb_string_free(err_str);
  2091. break;
  2092. }
  2093. case_end;
  2094. case_ast_node(pe, ParenExpr, e);
  2095. *type = check_type_expr(ctx, pe->expr, named_type);
  2096. set_base_type(named_type, *type);
  2097. return true;
  2098. case_end;
  2099. case_ast_node(ue, UnaryExpr, e);
  2100. switch (ue->op.kind) {
  2101. case Token_Pointer:
  2102. *type = alloc_type_pointer(check_type(ctx, ue->expr));
  2103. set_base_type(named_type, *type);
  2104. return true;
  2105. }
  2106. case_end;
  2107. case_ast_node(pt, PointerType, e);
  2108. *type = alloc_type_pointer(check_type(ctx, pt->type));
  2109. set_base_type(named_type, *type);
  2110. return true;
  2111. case_end;
  2112. case_ast_node(ot, OpaqueType, e);
  2113. Type *elem = strip_opaque_type(check_type(ctx, ot->type));
  2114. *type = alloc_type_opaque(elem);
  2115. set_base_type(named_type, *type);
  2116. return true;
  2117. case_end;
  2118. case_ast_node(at, ArrayType, e);
  2119. if (at->count != nullptr) {
  2120. Operand o = {};
  2121. i64 count = check_array_count(ctx, &o, at->count);
  2122. Type *generic_type = nullptr;
  2123. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  2124. generic_type = o.type;
  2125. }
  2126. if (count < 0) {
  2127. error(at->count, "... can only be used in conjuction with compound literals");
  2128. count = 0;
  2129. }
  2130. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  2131. *type = alloc_type_array(elem, count, generic_type);
  2132. } else {
  2133. Type *elem = check_type(ctx, at->elem);
  2134. *type = alloc_type_slice(elem);
  2135. }
  2136. set_base_type(named_type, *type);
  2137. return true;
  2138. case_end;
  2139. case_ast_node(dat, DynamicArrayType, e);
  2140. Type *elem = check_type(ctx, dat->elem);
  2141. *type = alloc_type_dynamic_array(elem);
  2142. set_base_type(named_type, *type);
  2143. return true;
  2144. case_end;
  2145. case_ast_node(st, StructType, e);
  2146. CheckerContext c = *ctx;
  2147. c.in_polymorphic_specialization = false;
  2148. c.type_level += 1;
  2149. *type = alloc_type_struct();
  2150. set_base_type(named_type, *type);
  2151. check_open_scope(&c, e);
  2152. check_struct_type(&c, *type, e, nullptr, named_type);
  2153. check_close_scope(&c);
  2154. (*type)->Struct.node = e;
  2155. return true;
  2156. case_end;
  2157. case_ast_node(ut, UnionType, e);
  2158. CheckerContext c = *ctx;
  2159. c.in_polymorphic_specialization = false;
  2160. c.type_level += 1;
  2161. *type = alloc_type_union();
  2162. set_base_type(named_type, *type);
  2163. check_open_scope(&c, e);
  2164. check_union_type(&c, *type, e, nullptr, named_type);
  2165. check_close_scope(&c);
  2166. (*type)->Union.node = e;
  2167. return true;
  2168. case_end;
  2169. case_ast_node(et, EnumType, e);
  2170. bool ips = ctx->in_polymorphic_specialization;
  2171. defer (ctx->in_polymorphic_specialization = ips);
  2172. ctx->in_polymorphic_specialization = false;
  2173. ctx->in_enum_type = true;
  2174. *type = alloc_type_enum();
  2175. set_base_type(named_type, *type);
  2176. check_open_scope(ctx, e);
  2177. check_enum_type(ctx, *type, named_type, e);
  2178. check_close_scope(ctx);
  2179. (*type)->Enum.node = e;
  2180. ctx->in_enum_type = false;
  2181. return true;
  2182. case_end;
  2183. case_ast_node(et, BitFieldType, e);
  2184. *type = alloc_type_bit_field();
  2185. set_base_type(named_type, *type);
  2186. check_open_scope(ctx, e);
  2187. check_bit_field_type(ctx, *type, e);
  2188. check_close_scope(ctx);
  2189. return true;
  2190. case_end;
  2191. case_ast_node(bs, BitSetType, e);
  2192. *type = alloc_type_bit_set();
  2193. set_base_type(named_type, *type);
  2194. check_bit_set_type(ctx, *type, named_type, e);
  2195. return true;
  2196. case_end;
  2197. case_ast_node(pt, ProcType, e);
  2198. bool ips = ctx->in_polymorphic_specialization;
  2199. defer (ctx->in_polymorphic_specialization = ips);
  2200. ctx->in_polymorphic_specialization = false;
  2201. *type = alloc_type(Type_Proc);
  2202. set_base_type(named_type, *type);
  2203. check_open_scope(ctx, e);
  2204. check_procedure_type(ctx, *type, e);
  2205. check_close_scope(ctx);
  2206. return true;
  2207. case_end;
  2208. case_ast_node(mt, MapType, e);
  2209. bool ips = ctx->in_polymorphic_specialization;
  2210. defer (ctx->in_polymorphic_specialization = ips);
  2211. ctx->in_polymorphic_specialization = false;
  2212. *type = alloc_type(Type_Map);
  2213. set_base_type(named_type, *type);
  2214. check_map_type(ctx, *type, e);
  2215. return true;
  2216. case_end;
  2217. case_ast_node(ce, CallExpr, e);
  2218. Operand o = {};
  2219. check_expr_or_type(ctx, &o, e);
  2220. if (o.mode == Addressing_Type) {
  2221. *type = o.type;
  2222. set_base_type(named_type, *type);
  2223. return true;
  2224. }
  2225. case_end;
  2226. case_ast_node(te, TernaryExpr, e);
  2227. Operand o = {};
  2228. check_expr_or_type(ctx, &o, e);
  2229. if (o.mode == Addressing_Type) {
  2230. *type = o.type;
  2231. set_base_type(named_type, *type);
  2232. return true;
  2233. }
  2234. case_end;
  2235. }
  2236. *type = t_invalid;
  2237. return false;
  2238. }
  2239. Type *check_type(CheckerContext *ctx, Ast *e) {
  2240. CheckerContext c = *ctx;
  2241. c.type_path = new_checker_type_path();
  2242. defer (destroy_checker_type_path(c.type_path));
  2243. return check_type_expr(&c, e, nullptr);
  2244. }
  2245. Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) {
  2246. Type *type = nullptr;
  2247. bool ok = check_type_internal(ctx, e, &type, named_type);
  2248. if (!ok) {
  2249. gbString err_str = expr_to_string(e);
  2250. error(e, "'%s' is not a type", err_str);
  2251. gb_string_free(err_str);
  2252. type = t_invalid;
  2253. }
  2254. if (type == nullptr) {
  2255. type = t_invalid;
  2256. }
  2257. if (type->kind == Type_Named &&
  2258. type->Named.base == nullptr) {
  2259. // IMPORTANT TODO(bill): Is this a serious error?!
  2260. #if 0
  2261. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  2262. #endif
  2263. type->Named.base = t_invalid;
  2264. }
  2265. if (is_type_polymorphic(type)) {
  2266. type->flags |= TypeFlag_Polymorphic;
  2267. } else if (is_type_polymorphic(type, true)) {
  2268. type->flags |= TypeFlag_PolySpecialized;
  2269. }
  2270. #if 0
  2271. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  2272. gbString str = type_to_string(type);
  2273. error(e, "Invalid use of a polymorphic type '%s'", str);
  2274. gb_string_free(str);
  2275. type = t_invalid;
  2276. }
  2277. #endif
  2278. if (is_type_typed(type)) {
  2279. add_type_and_value(&ctx->checker->info, e, Addressing_Type, type, empty_exact_value);
  2280. } else {
  2281. gbString name = type_to_string(type);
  2282. error(e, "Invalid type definition of %s", name);
  2283. gb_string_free(name);
  2284. type = t_invalid;
  2285. }
  2286. set_base_type(named_type, type);
  2287. return type;
  2288. }