check_type.cpp 76 KB

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