check_type.cpp 74 KB

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