check_type.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. void populate_using_entity_scope(CheckerContext *ctx, AstNode *node, Type *t) {
  2. t = base_type(type_deref(t));
  3. gbString str = nullptr;
  4. defer (gb_string_free(str));
  5. if (node != nullptr) {
  6. str = expr_to_string(node);
  7. }
  8. if (t->kind == Type_Struct) {
  9. for_array(i, t->Struct.fields) {
  10. Entity *f = t->Struct.fields[i];
  11. GB_ASSERT(f->kind == Entity_Variable);
  12. String name = f->token.string;;
  13. Entity *e = current_scope_lookup_entity(ctx->scope, name);
  14. if (e != nullptr && name != "_") {
  15. // TODO(bill): Better type error
  16. if (str != nullptr) {
  17. error(e->token, "'%.*s' is already declared in '%s'", LIT(name), str);
  18. } else {
  19. error(e->token, "'%.*s' is already declared", LIT(name));
  20. }
  21. } else {
  22. add_entity(ctx->checker, ctx->scope, nullptr, f);
  23. if (f->flags & EntityFlag_Using) {
  24. populate_using_entity_scope(ctx, node, f->type);
  25. }
  26. }
  27. }
  28. }
  29. }
  30. void check_struct_fields(CheckerContext *ctx, AstNode *node, Array<Entity *> *fields, Array<AstNode *> const &params,
  31. isize init_field_capacity, Type *named_type, String context) {
  32. *fields = array_make<Entity *>(heap_allocator(), 0, init_field_capacity);
  33. GB_ASSERT(node->kind == AstNode_StructType);
  34. isize variable_count = 0;
  35. for_array(i, params) {
  36. AstNode *field = params[i];
  37. if (ast_node_expect(field, AstNode_Field)) {
  38. ast_node(f, Field, field);
  39. variable_count += gb_max(f->names.count, 1);
  40. }
  41. }
  42. i32 field_src_index = 0;
  43. for_array(i, params) {
  44. AstNode *param = params[i];
  45. if (param->kind != AstNode_Field) {
  46. continue;
  47. }
  48. ast_node(p, Field, param);
  49. AstNode *type_expr = p->type;
  50. Type *type = nullptr;
  51. bool detemine_type_from_operand = false;
  52. if (type_expr != nullptr) {
  53. type = check_type_expr(ctx, type_expr, nullptr);
  54. if (is_type_polymorphic(type)) {
  55. type = nullptr;
  56. }
  57. }
  58. if (type == nullptr) {
  59. error(params[i], "Invalid parameter type");
  60. type = t_invalid;
  61. }
  62. if (is_type_untyped(type)) {
  63. if (is_type_untyped_undef(type)) {
  64. error(params[i], "Cannot determine parameter type from ---");
  65. } else {
  66. error(params[i], "Cannot determine parameter type from a nil");
  67. }
  68. type = t_invalid;
  69. }
  70. bool is_using = (p->flags&FieldFlag_using) != 0;
  71. for_array(j, p->names) {
  72. AstNode *name = p->names[j];
  73. if (!ast_node_expect(name, AstNode_Ident)) {
  74. continue;
  75. }
  76. Token name_token = name->Ident.token;
  77. Entity *field = alloc_entity_field(ctx->scope, name_token, type, is_using, field_src_index);
  78. add_entity(ctx->checker, ctx->scope, name, field);
  79. array_add(fields, field);
  80. field_src_index += 1;
  81. }
  82. if (is_using && p->names.count > 0) {
  83. Type *first_type = (*fields)[fields->count-1]->type;
  84. Type *t = base_type(type_deref(first_type));
  85. if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
  86. p->names.count >= 1 &&
  87. p->names[0]->kind == AstNode_Ident) {
  88. Token name_token = p->names[0]->Ident.token;
  89. gbString type_str = type_to_string(first_type);
  90. error(name_token, "'using' cannot be applied to the field '%.*s' of type '%s'", LIT(name_token.string), type_str);
  91. gb_string_free(type_str);
  92. continue;
  93. }
  94. populate_using_entity_scope(ctx, node, type);
  95. }
  96. }
  97. }
  98. Entity *make_names_field_for_struct(CheckerContext *ctx, Scope *scope) {
  99. Entity *e = alloc_entity_field(scope, make_token_ident(str_lit("names")), t_string_slice, false, 0);
  100. e->Variable.is_immutable = true;
  101. e->flags |= EntityFlag_TypeField;
  102. return e;
  103. }
  104. bool check_custom_align(CheckerContext *ctx, AstNode *node, i64 *align_) {
  105. GB_ASSERT(align_ != nullptr);
  106. Operand o = {};
  107. check_expr(ctx, &o, node);
  108. if (o.mode != Addressing_Constant) {
  109. if (o.mode != Addressing_Invalid) {
  110. error(node, "#align must be a constant");
  111. }
  112. return false;
  113. }
  114. Type *type = base_type(o.type);
  115. if (is_type_untyped(type) || is_type_integer(type)) {
  116. if (o.value.kind == ExactValue_Integer) {
  117. i64 align = o.value.value_integer;
  118. if (align < 1 || !gb_is_power_of_two(align)) {
  119. error(node, "#align must be a power of 2, got %lld", align);
  120. return false;
  121. }
  122. // NOTE(bill): Success!!!
  123. i64 custom_align = gb_clamp(align, 1, build_context.max_align);
  124. if (custom_align < align) {
  125. warning(node, "Custom alignment has been clamped to %lld from %lld", align, custom_align);
  126. }
  127. *align_ = custom_align;
  128. return true;
  129. }
  130. }
  131. error(node, "#align must be an integer");
  132. return false;
  133. }
  134. Entity *find_polymorphic_struct_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array<Operand> ordered_operands) {
  135. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  136. if (found_gen_types != nullptr) {
  137. for_array(i, *found_gen_types) {
  138. Entity *e = (*found_gen_types)[i];
  139. Type *t = base_type(e->type);
  140. TypeTuple *tuple = &t->Struct.polymorphic_params->Tuple;
  141. bool ok = true;
  142. GB_ASSERT(param_count == tuple->variables.count);
  143. for (isize j = 0; j < param_count; j++) {
  144. Entity *p = tuple->variables[j];
  145. Operand o = ordered_operands[j];
  146. if (p->kind == Entity_TypeName) {
  147. if (is_type_polymorphic(o.type)) {
  148. // NOTE(bill): Do not add polymorphic version to the gen_types
  149. ok = false;
  150. }
  151. if (!are_types_identical(o.type, p->type)) {
  152. ok = false;
  153. }
  154. } else if (p->kind == Entity_Constant) {
  155. if (!are_types_identical(o.type, p->type)) {
  156. ok = false;
  157. }
  158. if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
  159. ok = false;
  160. }
  161. } else {
  162. GB_PANIC("Unknown entity kind");
  163. }
  164. }
  165. if (ok) {
  166. return e;
  167. }
  168. }
  169. }
  170. return nullptr;
  171. }
  172. void add_polymorphic_struct_entity(CheckerContext *ctx, AstNode *node, Type *named_type, Type *original_type) {
  173. GB_ASSERT(is_type_named(named_type));
  174. gbAllocator a = heap_allocator();
  175. Scope *s = ctx->scope->parent;
  176. Entity *e = nullptr;
  177. {
  178. Token token = ast_node_token(node);
  179. token.kind = Token_String;
  180. token.string = named_type->Named.name;
  181. AstNode *node = gb_alloc_item(a, AstNode);
  182. node->kind = AstNode_Ident;
  183. node->Ident.token = token;
  184. e = alloc_entity_type_name(s, token, named_type);
  185. e->state = EntityState_Resolved;
  186. e->pkg = ctx->pkg;
  187. add_entity_use(ctx, node, e);
  188. }
  189. named_type->Named.type_name = e;
  190. auto *found_gen_types = map_get(&ctx->checker->info.gen_types, hash_pointer(original_type));
  191. if (found_gen_types) {
  192. array_add(found_gen_types, e);
  193. } else {
  194. auto array = array_make<Entity *>(heap_allocator());
  195. array_add(&array, e);
  196. map_set(&ctx->checker->info.gen_types, hash_pointer(original_type), array);
  197. }
  198. }
  199. void check_struct_type(CheckerContext *ctx, Type *struct_type, AstNode *node, Array<Operand> *poly_operands, Type *named_type, Type *original_type_for_poly) {
  200. GB_ASSERT(is_type_struct(struct_type));
  201. ast_node(st, StructType, node);
  202. String context = str_lit("struct");
  203. isize min_field_count = 0;
  204. for_array(field_index, st->fields) {
  205. AstNode *field = st->fields[field_index];
  206. switch (field->kind) {
  207. case_ast_node(f, ValueDecl, field);
  208. min_field_count += f->names.count;
  209. case_end;
  210. }
  211. }
  212. struct_type->Struct.names = make_names_field_for_struct(ctx, ctx->scope);
  213. scope_reserve(ctx->scope, min_field_count);
  214. if (st->is_raw_union) {
  215. struct_type->Struct.is_raw_union = true;
  216. context = str_lit("struct #raw_union");
  217. }
  218. Type *polymorphic_params = nullptr;
  219. bool is_polymorphic = false;
  220. bool can_check_fields = true;
  221. bool is_poly_specialized = false;
  222. if (st->polymorphic_params != nullptr) {
  223. ast_node(field_list, FieldList, st->polymorphic_params);
  224. Array<AstNode *> params = field_list->list;
  225. if (params.count != 0) {
  226. isize variable_count = 0;
  227. for_array(i, params) {
  228. AstNode *field = params[i];
  229. if (ast_node_expect(field, AstNode_Field)) {
  230. ast_node(f, Field, field);
  231. variable_count += gb_max(f->names.count, 1);
  232. }
  233. }
  234. auto entities = array_make<Entity *>(ctx->allocator, 0, variable_count);
  235. for_array(i, params) {
  236. AstNode *param = params[i];
  237. if (param->kind != AstNode_Field) {
  238. continue;
  239. }
  240. ast_node(p, Field, param);
  241. AstNode *type_expr = p->type;
  242. Type *type = nullptr;
  243. bool is_type_param = false;
  244. bool is_type_polymorphic_type = false;
  245. if (type_expr == nullptr) {
  246. error(param, "Expected a type for this parameter");
  247. continue;
  248. }
  249. if (type_expr->kind == AstNode_Ellipsis) {
  250. type_expr = type_expr->Ellipsis.expr;
  251. error(param, "A polymorphic parameter cannot be variadic");
  252. }
  253. if (type_expr->kind == AstNode_TypeType) {
  254. is_type_param = true;
  255. Type *specialization = nullptr;
  256. if (type_expr->TypeType.specialization != nullptr) {
  257. AstNode *s = type_expr->TypeType.specialization;
  258. specialization = check_type(ctx, s);
  259. // if (!is_type_polymorphic_struct(specialization)) {
  260. // gbString str = type_to_string(specialization);
  261. // defer (gb_string_free(str));
  262. // error(s, "Expected a polymorphic struct, got %s", str);
  263. // specialization = nullptr;
  264. // }
  265. }
  266. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  267. } else {
  268. type = check_type(ctx, type_expr);
  269. if (is_type_polymorphic(type)) {
  270. is_type_polymorphic_type = true;
  271. }
  272. }
  273. if (type == nullptr) {
  274. error(params[i], "Invalid parameter type");
  275. type = t_invalid;
  276. }
  277. if (is_type_untyped(type)) {
  278. if (is_type_untyped_undef(type)) {
  279. error(params[i], "Cannot determine parameter type from ---");
  280. } else {
  281. error(params[i], "Cannot determine parameter type from a nil");
  282. }
  283. type = t_invalid;
  284. }
  285. if (is_type_polymorphic_type) {
  286. gbString str = type_to_string(type);
  287. error(params[i], "Parameter types cannot be polymorphic, got %s", str);
  288. gb_string_free(str);
  289. type = t_invalid;
  290. }
  291. if (!is_type_param && !is_type_constant_type(type)) {
  292. gbString str = type_to_string(type);
  293. error(params[i], "A parameter must be a valid constant type, got %s", str);
  294. gb_string_free(str);
  295. }
  296. Scope *scope = ctx->scope;
  297. for_array(j, p->names) {
  298. AstNode *name = p->names[j];
  299. if (!ast_node_expect(name, AstNode_Ident)) {
  300. continue;
  301. }
  302. Entity *e = nullptr;
  303. Token token = name->Ident.token;
  304. if (poly_operands != nullptr) {
  305. Operand operand = (*poly_operands)[entities.count];
  306. if (is_type_param) {
  307. GB_ASSERT(operand.mode == Addressing_Type ||
  308. operand.mode == Addressing_Invalid);
  309. if (is_type_polymorphic(base_type(operand.type))) {
  310. is_polymorphic = true;
  311. can_check_fields = false;
  312. }
  313. e = alloc_entity_type_name(scope, token, operand.type);
  314. e->TypeName.is_type_alias = true;
  315. } else {
  316. GB_ASSERT(operand.mode == Addressing_Constant);
  317. e = alloc_entity_constant(scope, token, operand.type, operand.value);
  318. }
  319. } else {
  320. if (is_type_param) {
  321. e = alloc_entity_type_name(scope, token, type);
  322. e->TypeName.is_type_alias = true;
  323. } else {
  324. e = alloc_entity_constant(scope, token, type, empty_exact_value);
  325. }
  326. }
  327. e->state = EntityState_Resolved;
  328. add_entity(ctx->checker, scope, name, e);
  329. array_add(&entities, e);
  330. }
  331. }
  332. if (entities.count > 0) {
  333. Type *tuple = alloc_type_tuple();
  334. tuple->Tuple.variables = entities;
  335. polymorphic_params = tuple;
  336. }
  337. }
  338. if (original_type_for_poly != nullptr) {
  339. GB_ASSERT(named_type != nullptr);
  340. add_polymorphic_struct_entity(ctx, node, named_type, original_type_for_poly);
  341. }
  342. }
  343. if (!is_polymorphic) {
  344. is_polymorphic = polymorphic_params != nullptr && poly_operands == nullptr;
  345. }
  346. if (poly_operands != nullptr) {
  347. is_poly_specialized = true;
  348. for (isize i = 0; i < poly_operands->count; i++) {
  349. Operand o = (*poly_operands)[i];
  350. if (is_type_polymorphic(o.type)) {
  351. is_poly_specialized = false;
  352. break;
  353. }
  354. }
  355. }
  356. struct_type->Struct.scope = ctx->scope;
  357. struct_type->Struct.is_packed = st->is_packed;
  358. struct_type->Struct.polymorphic_params = polymorphic_params;
  359. struct_type->Struct.is_polymorphic = is_polymorphic;
  360. struct_type->Struct.is_poly_specialized = is_poly_specialized;
  361. if (!is_polymorphic) {
  362. check_struct_fields(ctx, node, &struct_type->Struct.fields, st->fields, min_field_count, named_type, context);
  363. }
  364. if (st->align != nullptr) {
  365. if (st->is_packed) {
  366. syntax_error(st->align, "'#align' cannot be applied with '#packed'");
  367. return;
  368. }
  369. i64 custom_align = 1;
  370. if (check_custom_align(ctx, st->align, &custom_align)) {
  371. struct_type->Struct.custom_align = custom_align;
  372. }
  373. }
  374. }
  375. void check_union_type(CheckerContext *ctx, Type *union_type, AstNode *node) {
  376. GB_ASSERT(is_type_union(union_type));
  377. ast_node(ut, UnionType, node);
  378. isize variant_count = ut->variants.count;
  379. Entity *using_index_expr = nullptr;
  380. auto variants = array_make<Type *>(ctx->allocator, 0, variant_count);
  381. union_type->Union.scope = ctx->scope;
  382. for_array(i, ut->variants) {
  383. AstNode *node = ut->variants[i];
  384. Type *t = check_type_expr(ctx, node, nullptr);
  385. if (t != nullptr && t != t_invalid) {
  386. bool ok = true;
  387. t = default_type(t);
  388. if (is_type_untyped(t) || is_type_empty_union(t)) {
  389. ok = false;
  390. gbString str = type_to_string(t);
  391. error(node, "Invalid variant type in union '%s'", str);
  392. gb_string_free(str);
  393. } else {
  394. for_array(j, variants) {
  395. if (are_types_identical(t, variants[j])) {
  396. ok = false;
  397. gbString str = type_to_string(t);
  398. error(node, "Duplicate variant type '%s'", str);
  399. gb_string_free(str);
  400. break;
  401. }
  402. }
  403. }
  404. if (ok) {
  405. array_add(&variants, t);
  406. }
  407. }
  408. }
  409. union_type->Union.variants = variants;
  410. if (ut->align != nullptr) {
  411. i64 custom_align = 1;
  412. if (check_custom_align(ctx, ut->align, &custom_align)) {
  413. if (variants.count == 0) {
  414. error(ut->align, "An empty union cannot have a custom alignment");
  415. } else {
  416. union_type->Union.custom_align = custom_align;
  417. }
  418. }
  419. }
  420. }
  421. void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, AstNode *node) {
  422. ast_node(et, EnumType, node);
  423. GB_ASSERT(is_type_enum(enum_type));
  424. Type *base_type = t_int;
  425. if (et->base_type != nullptr) {
  426. base_type = check_type(ctx, et->base_type);
  427. }
  428. if (base_type == nullptr || !(is_type_integer(base_type) || is_type_float(base_type))) {
  429. error(node, "Base type for enumeration must be numeric");
  430. return;
  431. }
  432. if (is_type_enum(base_type)) {
  433. error(node, "Base type for enumeration cannot be another enumeration");
  434. return;
  435. }
  436. // NOTE(bill): Must be up here for the 'check_init_constant' system
  437. enum_type->Enum.base_type = base_type;
  438. enum_type->Enum.scope = ctx->scope;
  439. auto fields = array_make<Entity *>(ctx->allocator, 0, et->fields.count);
  440. Type *constant_type = enum_type;
  441. if (named_type != nullptr) {
  442. constant_type = named_type;
  443. }
  444. ExactValue iota = exact_value_i64(-1);
  445. ExactValue min_value = exact_value_i64(0);
  446. ExactValue max_value = exact_value_i64(0);
  447. scope_reserve(ctx->scope, et->fields.count);
  448. for_array(i, et->fields) {
  449. AstNode *field = et->fields[i];
  450. AstNode *ident = nullptr;
  451. AstNode *init = nullptr;
  452. if (field->kind == AstNode_FieldValue) {
  453. ast_node(fv, FieldValue, field);
  454. if (fv->field == nullptr || fv->field->kind != AstNode_Ident) {
  455. error(field, "An enum field's name must be an identifier");
  456. continue;
  457. }
  458. ident = fv->field;
  459. init = fv->value;
  460. } else if (field->kind == AstNode_Ident) {
  461. ident = field;
  462. } else {
  463. error(field, "An enum field's name must be an identifier");
  464. continue;
  465. }
  466. String name = ident->Ident.token.string;
  467. if (init != nullptr) {
  468. Operand o = {};
  469. check_expr(ctx, &o, init);
  470. if (o.mode != Addressing_Constant) {
  471. error(init, "Enumeration value must be a constant");
  472. o.mode = Addressing_Invalid;
  473. }
  474. if (o.mode != Addressing_Invalid) {
  475. check_assignment(ctx, &o, constant_type, str_lit("enumeration"));
  476. }
  477. if (o.mode != Addressing_Invalid) {
  478. iota = o.value;
  479. } else {
  480. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  481. }
  482. } else {
  483. iota = exact_binary_operator_value(Token_Add, iota, exact_value_i64(1));
  484. }
  485. // NOTE(bill): Skip blank identifiers
  486. if (is_blank_ident(name)) {
  487. continue;
  488. } else if (name == "count") {
  489. error(field, "'count' is a reserved identifier for enumerations");
  490. continue;
  491. } else if (name == "min_value") {
  492. error(field, "'min_value' is a reserved identifier for enumerations");
  493. continue;
  494. } else if (name == "max_value") {
  495. error(field, "'max_value' is a reserved identifier for enumerations");
  496. continue;
  497. } else if (name == "names") {
  498. error(field, "'names' is a reserved identifier for enumerations");
  499. continue;
  500. }/* else if (name == "base_type") {
  501. error(field, "'base_type' is a reserved identifier for enumerations");
  502. continue;
  503. } */
  504. if (compare_exact_values(Token_Gt, min_value, iota)) {
  505. min_value = iota;
  506. }
  507. if (compare_exact_values(Token_Lt, max_value, iota)) {
  508. max_value = iota;
  509. }
  510. Entity *e = alloc_entity_constant(ctx->scope, ident->Ident.token, constant_type, iota);
  511. e->identifier = ident;
  512. e->flags |= EntityFlag_Visited;
  513. e->state = EntityState_Resolved;
  514. if (current_scope_lookup_entity(ctx->scope, name) != nullptr) {
  515. error(ident, "'%.*s' is already declared in this enumeration", LIT(name));
  516. } else {
  517. add_entity(ctx->checker, ctx->scope, nullptr, e);
  518. array_add(&fields, e);
  519. // TODO(bill): Should I add a use for the enum value?
  520. add_entity_use(ctx, field, e);
  521. }
  522. }
  523. GB_ASSERT(fields.count <= et->fields.count);
  524. enum_type->Enum.fields = fields;
  525. enum_type->Enum.is_export = et->is_export;
  526. if (et->is_export) {
  527. Scope *parent = ctx->scope->parent;
  528. for_array(i, fields) {
  529. Entity *f = fields[i];
  530. if (f->kind != Entity_Constant) {
  531. continue;
  532. }
  533. String name = f->token.string;
  534. if (is_blank_ident(name)) {
  535. continue;
  536. }
  537. add_entity(ctx->checker, parent, nullptr, f);
  538. }
  539. }
  540. Scope *s = ctx->scope;
  541. enum_type->Enum.count = alloc_entity_constant(s, make_token_ident(str_lit("count")), t_int, exact_value_i64(fields.count));
  542. enum_type->Enum.min_value = alloc_entity_constant(s, make_token_ident(str_lit("min_value")), constant_type, min_value);
  543. enum_type->Enum.max_value = alloc_entity_constant(s, make_token_ident(str_lit("max_value")), constant_type, max_value);
  544. enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope);
  545. }
  546. void check_bit_field_type(CheckerContext *ctx, Type *bit_field_type, AstNode *node) {
  547. ast_node(bft, BitFieldType, node);
  548. GB_ASSERT(is_type_bit_field(bit_field_type));
  549. auto fields = array_make<Entity*>(ctx->allocator, 0, bft->fields.count);
  550. auto sizes = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  551. auto offsets = array_make<u32> (ctx->allocator, 0, bft->fields.count);
  552. scope_reserve(ctx->scope, bft->fields.count);
  553. u32 curr_offset = 0;
  554. for_array(i, bft->fields) {
  555. AstNode *field = bft->fields[i];
  556. GB_ASSERT(field->kind == AstNode_FieldValue);
  557. AstNode *ident = field->FieldValue.field;
  558. AstNode *value = field->FieldValue.value;
  559. if (ident->kind != AstNode_Ident) {
  560. error(field, "A bit field value's name must be an identifier");
  561. continue;
  562. }
  563. String name = ident->Ident.token.string;
  564. Operand o = {};
  565. check_expr(ctx, &o, value);
  566. if (o.mode != Addressing_Constant) {
  567. error(value, "Bit field bit size must be a constant");
  568. continue;
  569. }
  570. ExactValue v = exact_value_to_integer(o.value);
  571. if (v.kind != ExactValue_Integer) {
  572. error(value, "Bit field bit size must be a constant integer");
  573. continue;
  574. }
  575. i64 bits_ = v.value_integer;
  576. if (bits_ < 0 || bits_ > 64) {
  577. error(value, "Bit field's bit size must be within the range 1...64, got %lld", cast(long long)bits_);
  578. continue;
  579. }
  580. u32 bits = cast(u32)bits_;
  581. Type *value_type = alloc_type_bit_field_value(bits);
  582. Entity *e = alloc_entity_variable(bit_field_type->BitField.scope, ident->Ident.token, value_type, false);
  583. e->identifier = ident;
  584. e->flags |= EntityFlag_BitFieldValue;
  585. if (!is_blank_ident(name) &&
  586. current_scope_lookup_entity(ctx->scope, name) != nullptr) {
  587. error(ident, "'%.*s' is already declared in this bit field", LIT(name));
  588. } else {
  589. add_entity(ctx->checker, ctx->scope, nullptr, e);
  590. // TODO(bill): Should this entity be "used"?
  591. add_entity_use(ctx, field, e);
  592. array_add(&fields, e);
  593. array_add(&offsets, curr_offset);
  594. array_add(&sizes, bits);
  595. curr_offset += bits;
  596. }
  597. }
  598. GB_ASSERT(fields.count <= bft->fields.count);
  599. bit_field_type->BitField.fields = fields;
  600. bit_field_type->BitField.sizes = sizes;
  601. bit_field_type->BitField.offsets = offsets;
  602. if (bft->align != nullptr) {
  603. i64 custom_align = 1;
  604. if (check_custom_align(ctx, bft->align, &custom_align)) {
  605. bit_field_type->BitField.custom_align = custom_align;
  606. }
  607. }
  608. }
  609. bool check_type_specialization_to(CheckerContext *ctx, Type *specialization, Type *type, bool compound, bool modify_type) {
  610. if (type == nullptr ||
  611. type == t_invalid) {
  612. return true;
  613. }
  614. Type *t = base_type(type);
  615. Type *s = base_type(specialization);
  616. if (t->kind != s->kind) {
  617. return false;
  618. }
  619. // gb_printf_err("#1 %s %s\n", type_to_string(type), type_to_string(specialization));
  620. if (t->kind == Type_Struct) {
  621. if (t->Struct.polymorphic_parent == specialization) {
  622. return true;
  623. }
  624. if (t->Struct.polymorphic_parent == s->Struct.polymorphic_parent &&
  625. s->Struct.polymorphic_params != nullptr &&
  626. t->Struct.polymorphic_params != nullptr) {
  627. TypeTuple *s_tuple = &s->Struct.polymorphic_params->Tuple;
  628. TypeTuple *t_tuple = &t->Struct.polymorphic_params->Tuple;
  629. GB_ASSERT(t_tuple->variables.count == s_tuple->variables.count);
  630. for_array(i, s_tuple->variables) {
  631. Entity *s_e = s_tuple->variables[i];
  632. Entity *t_e = t_tuple->variables[i];
  633. Type *st = s_e->type;
  634. Type *tt = t_e->type;
  635. bool ok = is_polymorphic_type_assignable(ctx, st, tt, true, modify_type);
  636. }
  637. if (modify_type) {
  638. // NOTE(bill): This is needed in order to change the actual type but still have the types defined within it
  639. gb_memmove(specialization, type, gb_size_of(Type));
  640. }
  641. return true;
  642. }
  643. }
  644. if (specialization->kind == Type_Named &&
  645. type->kind != Type_Named) {
  646. return false;
  647. }
  648. if (is_polymorphic_type_assignable(ctx, base_type(specialization), base_type(type), compound, modify_type)) {
  649. return true;
  650. }
  651. return false;
  652. }
  653. Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Operand operand) {
  654. bool modify_type = !ctx->no_polymorphic_errors;
  655. if (!is_operand_value(operand)) {
  656. if (modify_type) {
  657. error(operand.expr, "Cannot determine polymorphic type from parameter");
  658. }
  659. return t_invalid;
  660. }
  661. if (is_polymorphic_type_assignable(ctx, poly_type, operand.type, false, modify_type)) {
  662. return poly_type;
  663. }
  664. if (modify_type) {
  665. gbString pts = type_to_string(poly_type);
  666. gbString ots = type_to_string(operand.type);
  667. defer (gb_string_free(pts));
  668. defer (gb_string_free(ots));
  669. error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
  670. }
  671. return t_invalid;
  672. }
  673. Type *check_get_params(CheckerContext *ctx, Scope *scope, AstNode *_params, bool *is_variadic_, isize *variadic_index_, bool *success_, isize *specialization_count_, Array<Operand> *operands) {
  674. if (_params == nullptr) {
  675. return nullptr;
  676. }
  677. bool allow_polymorphic_types = ctx->allow_polymorphic_types;
  678. bool success = true;
  679. ast_node(field_list, FieldList, _params);
  680. Array<AstNode *> params = field_list->list;
  681. if (params.count == 0) {
  682. if (success_) *success_ = success;
  683. return nullptr;
  684. }
  685. isize variable_count = 0;
  686. for_array(i, params) {
  687. AstNode *field = params[i];
  688. if (ast_node_expect(field, AstNode_Field)) {
  689. ast_node(f, Field, field);
  690. variable_count += gb_max(f->names.count, 1);
  691. }
  692. }
  693. isize min_variable_count = variable_count;
  694. for (isize i = params.count-1; i >= 0; i--) {
  695. AstNode *field = params[i];
  696. if (field->kind == AstNode_Field) {
  697. ast_node(f, Field, field);
  698. if (f->default_value == nullptr) {
  699. break;
  700. }
  701. min_variable_count--;
  702. }
  703. }
  704. bool is_variadic = false;
  705. isize variadic_index = -1;
  706. bool is_c_vararg = false;
  707. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  708. for_array(i, params) {
  709. AstNode *param = params[i];
  710. if (param->kind != AstNode_Field) {
  711. continue;
  712. }
  713. ast_node(p, Field, param);
  714. AstNode *type_expr = p->type;
  715. Type *type = nullptr;
  716. AstNode *default_value = unparen_expr(p->default_value);
  717. ExactValue value = {};
  718. bool default_is_nil = false;
  719. bool default_is_location = false;
  720. bool is_type_param = false;
  721. bool is_type_polymorphic_type = false;
  722. bool detemine_type_from_operand = false;
  723. Type *specialization = nullptr;
  724. bool is_using = (p->flags&FieldFlag_using) != 0;
  725. if (type_expr == nullptr) {
  726. if (default_value->kind == AstNode_BasicDirective &&
  727. default_value->BasicDirective.name == "caller_location") {
  728. init_preload(ctx->checker);
  729. default_is_location = true;
  730. type = t_source_code_location;
  731. } else {
  732. Operand o = {};
  733. check_expr_or_type(ctx, &o, default_value);
  734. if (is_operand_nil(o)) {
  735. default_is_nil = true;
  736. } else if (o.mode != Addressing_Constant) {
  737. if (default_value->kind == AstNode_ProcLit) {
  738. value = exact_value_procedure(default_value);
  739. } else {
  740. Entity *e = nullptr;
  741. if (o.mode == Addressing_Value && is_type_proc(o.type)) {
  742. Operand x = {};
  743. if (default_value->kind == AstNode_Ident) {
  744. e = check_ident(ctx, &x, default_value, nullptr, nullptr, false);
  745. } else if (default_value->kind == AstNode_SelectorExpr) {
  746. e = check_selector(ctx, &x, default_value, nullptr);
  747. }
  748. }
  749. if (e != nullptr && e->kind == Entity_Procedure) {
  750. value = exact_value_procedure(e->identifier);
  751. add_entity_use(ctx, e->identifier, e);
  752. } else {
  753. error(default_value, "Default parameter must be a constant");
  754. continue;
  755. }
  756. }
  757. } else {
  758. value = o.value;
  759. }
  760. type = default_type(o.type);
  761. }
  762. } else {
  763. if (type_expr->kind == AstNode_Ellipsis) {
  764. type_expr = type_expr->Ellipsis.expr;
  765. #if 1
  766. is_variadic = true;
  767. variadic_index = variables.count;
  768. if (p->names.count != 1) {
  769. error(param, "Invalid AST: Invalid variadic parameter with multiple names");
  770. success = false;
  771. }
  772. #else
  773. if (i+1 == params.count) {
  774. is_variadic = true;
  775. } else {
  776. error(param, "Invalid AST: Invalid variadic parameter");
  777. success = false;
  778. }
  779. #endif
  780. }
  781. if (type_expr->kind == AstNode_TypeType) {
  782. ast_node(tt, TypeType, type_expr);
  783. is_type_param = true;
  784. specialization = check_type(ctx, tt->specialization);
  785. if (specialization == t_invalid){
  786. specialization = nullptr;
  787. }
  788. // if (specialization) {
  789. // if (!is_type_polymorphic(specialization)) {
  790. // gbString str = type_to_string(specialization);
  791. // error(tt->specialization, "Type specialization requires a polymorphic type, got %s", str);
  792. // gb_string_free(str);
  793. // }
  794. // }
  795. if (operands != nullptr) {
  796. detemine_type_from_operand = true;
  797. type = t_invalid;
  798. } else {
  799. type = alloc_type_generic(ctx->scope, 0, str_lit(""), specialization);
  800. }
  801. } else {
  802. bool prev = ctx->allow_polymorphic_types;
  803. if (operands != nullptr) {
  804. ctx->allow_polymorphic_types = true;
  805. }
  806. type = check_type(ctx, type_expr);
  807. ctx->allow_polymorphic_types = prev;
  808. if (is_type_polymorphic(type)) {
  809. is_type_polymorphic_type = true;
  810. }
  811. }
  812. if (default_value != nullptr) {
  813. if (type_expr->kind == AstNode_TypeType) {
  814. error(default_value, "A type parameter may not have a default value");
  815. continue;
  816. } else {
  817. Operand o = {};
  818. if (default_value->kind == AstNode_BasicDirective &&
  819. default_value->BasicDirective.name == "caller_location") {
  820. init_preload(ctx->checker);
  821. default_is_location = true;
  822. o.type = t_source_code_location;
  823. o.mode = Addressing_Value;
  824. } else {
  825. check_expr_with_type_hint(ctx, &o, default_value, type);
  826. if (is_operand_nil(o)) {
  827. default_is_nil = true;
  828. } else if (o.mode != Addressing_Constant) {
  829. if (default_value->kind == AstNode_ProcLit) {
  830. value = exact_value_procedure(default_value);
  831. } else {
  832. Entity *e = nullptr;
  833. if (o.mode == Addressing_Value && is_type_proc(o.type)) {
  834. Operand x = {};
  835. if (default_value->kind == AstNode_Ident) {
  836. e = check_ident(ctx, &x, default_value, nullptr, nullptr, false);
  837. } else if (default_value->kind == AstNode_SelectorExpr) {
  838. e = check_selector(ctx, &x, default_value, nullptr);
  839. }
  840. }
  841. if (e != nullptr && e->kind == Entity_Procedure) {
  842. value = exact_value_procedure(e->identifier);
  843. add_entity_use(ctx, e->identifier, e);
  844. } else {
  845. error(default_value, "Default parameter must be a constant");
  846. }
  847. }
  848. } else {
  849. value = o.value;
  850. }
  851. }
  852. check_is_assignable_to(ctx, &o, type);
  853. }
  854. }
  855. }
  856. if (type == nullptr) {
  857. error(param, "Invalid parameter type");
  858. type = t_invalid;
  859. }
  860. if (is_type_untyped(type)) {
  861. if (is_type_untyped_undef(type)) {
  862. error(param, "Cannot determine parameter type from ---");
  863. } else {
  864. error(param, "Cannot determine parameter type from a nil");
  865. }
  866. type = t_invalid;
  867. }
  868. if (is_type_empty_union(type)) {
  869. gbString str = type_to_string(type);
  870. error(param, "Invalid use of an empty union '%s'", str);
  871. gb_string_free(str);
  872. type = t_invalid;
  873. }
  874. if (p->flags&FieldFlag_c_vararg) {
  875. if (p->type == nullptr ||
  876. p->type->kind != AstNode_Ellipsis) {
  877. error(param, "'#c_vararg' can only be applied to variadic type fields");
  878. p->flags &= ~FieldFlag_c_vararg; // Remove the flag
  879. } else {
  880. is_c_vararg = true;
  881. }
  882. }
  883. if (p->flags&FieldFlag_in) {
  884. if (is_type_param) {
  885. error(param, "'in' cannot be applied to a type parameter");
  886. p->flags &= ~FieldFlag_in;
  887. } else if (is_variadic) {
  888. error(param, "'in' cannot be applied to a variadic parameter");
  889. p->flags &= ~FieldFlag_in;
  890. }
  891. }
  892. bool is_in = (p->flags&FieldFlag_in) != 0;
  893. for_array(j, p->names) {
  894. AstNode *name = p->names[j];
  895. if (!ast_node_expect(name, AstNode_Ident)) {
  896. continue;
  897. }
  898. Entity *param = nullptr;
  899. if (is_type_param) {
  900. if (operands != nullptr) {
  901. Operand o = (*operands)[variables.count];
  902. if (o.mode == Addressing_Type) {
  903. type = o.type;
  904. } else {
  905. if (!ctx->no_polymorphic_errors) {
  906. error(o.expr, "Expected a type to assign to the type parameter");
  907. }
  908. success = false;
  909. type = t_invalid;
  910. }
  911. if (is_type_polymorphic(type)) {
  912. gbString str = type_to_string(type);
  913. error(o.expr, "Cannot pass polymorphic type as a parameter, got '%s'", str);
  914. gb_string_free(str);
  915. success = false;
  916. type = t_invalid;
  917. }
  918. bool modify_type = !ctx->no_polymorphic_errors;
  919. if (specialization != nullptr && !check_type_specialization_to(ctx, specialization, type, false, modify_type)) {
  920. if (!ctx->no_polymorphic_errors) {
  921. gbString t = type_to_string(type);
  922. gbString s = type_to_string(specialization);
  923. error(o.expr, "Cannot convert type '%s' to the specialization '%s'", t, s);
  924. gb_string_free(s);
  925. gb_string_free(t);
  926. }
  927. success = false;
  928. type = t_invalid;
  929. }
  930. }
  931. param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
  932. param->TypeName.is_type_alias = true;
  933. } else {
  934. if (operands != nullptr && variables.count < operands->count) {
  935. if (is_type_polymorphic_type) {
  936. Operand op = (*operands)[variables.count];
  937. type = determine_type_from_polymorphic(ctx, type, op);
  938. if (type == t_invalid) {
  939. success = false;
  940. } else if (!ctx->no_polymorphic_errors) {
  941. // NOTE(bill): The type should be determined now and thus, no need to determine the type any more
  942. is_type_polymorphic_type = false;
  943. }
  944. }
  945. }
  946. if (p->flags&FieldFlag_no_alias) {
  947. if (!is_type_pointer(type)) {
  948. error(name, "'#no_alias' can only be applied to fields of pointer type");
  949. p->flags &= ~FieldFlag_no_alias; // Remove the flag
  950. }
  951. }
  952. param = alloc_entity_param(scope, name->Ident.token, type, is_using, is_in);
  953. param->Variable.default_value = value;
  954. param->Variable.default_is_nil = default_is_nil;
  955. param->Variable.default_is_location = default_is_location;
  956. }
  957. if (p->flags&FieldFlag_no_alias) {
  958. param->flags |= EntityFlag_NoAlias;
  959. }
  960. param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
  961. add_entity(ctx->checker, scope, name, param);
  962. array_add(&variables, param);
  963. }
  964. }
  965. if (is_variadic) {
  966. GB_ASSERT(variadic_index >= 0);
  967. }
  968. if (is_variadic) {
  969. GB_ASSERT(params.count > 0);
  970. // NOTE(bill): Change last variadic parameter to be a slice
  971. // Custom Calling convention for variadic parameters
  972. Entity *end = variables[variadic_index];
  973. end->type = alloc_type_slice(end->type);
  974. end->flags |= EntityFlag_Ellipsis;
  975. if (is_c_vararg) {
  976. end->flags |= EntityFlag_CVarArg;
  977. }
  978. }
  979. isize specialization_count = 0;
  980. if (scope != nullptr) {
  981. for_array(i, scope->elements.entries) {
  982. Entity *e = scope->elements.entries[i].value;
  983. if (e->kind == Entity_TypeName) {
  984. Type *t = e->type;
  985. if (t->kind == Type_Generic &&
  986. t->Generic.specialized != nullptr) {
  987. specialization_count += 1;
  988. }
  989. }
  990. }
  991. }
  992. Type *tuple = alloc_type_tuple();
  993. tuple->Tuple.variables = variables;
  994. if (success_) *success_ = success;
  995. if (specialization_count_) *specialization_count_ = specialization_count;
  996. if (is_variadic_) *is_variadic_ = is_variadic;
  997. if (variadic_index_) *variadic_index_ = variadic_index;
  998. return tuple;
  999. }
  1000. Type *check_get_results(CheckerContext *ctx, Scope *scope, AstNode *_results) {
  1001. if (_results == nullptr) {
  1002. return nullptr;
  1003. }
  1004. ast_node(field_list, FieldList, _results);
  1005. Array<AstNode *> results = field_list->list;
  1006. if (results.count == 0) {
  1007. return nullptr;
  1008. }
  1009. Type *tuple = alloc_type_tuple();
  1010. isize variable_count = 0;
  1011. for_array(i, results) {
  1012. AstNode *field = results[i];
  1013. if (ast_node_expect(field, AstNode_Field)) {
  1014. ast_node(f, Field, field);
  1015. variable_count += gb_max(f->names.count, 1);
  1016. }
  1017. }
  1018. auto variables = array_make<Entity *>(ctx->allocator, 0, variable_count);
  1019. for_array(i, results) {
  1020. ast_node(field, Field, results[i]);
  1021. AstNode *default_value = unparen_expr(field->default_value);
  1022. ExactValue value = {};
  1023. bool default_is_nil = false;
  1024. Type *type = nullptr;
  1025. if (field->type == nullptr) {
  1026. Operand o = {};
  1027. check_expr(ctx, &o, default_value);
  1028. if (is_operand_nil(o)) {
  1029. default_is_nil = true;
  1030. } else if (o.mode != Addressing_Constant) {
  1031. error(default_value, "Default parameter must be a constant");
  1032. } else {
  1033. value = o.value;
  1034. }
  1035. type = default_type(o.type);
  1036. } else {
  1037. type = check_type(ctx, field->type);
  1038. if (default_value != nullptr) {
  1039. Operand o = {};
  1040. check_expr_with_type_hint(ctx, &o, default_value, type);
  1041. if (is_operand_nil(o)) {
  1042. default_is_nil = true;
  1043. } else if (o.mode != Addressing_Constant) {
  1044. error(default_value, "Default parameter must be a constant");
  1045. } else {
  1046. value = o.value;
  1047. }
  1048. check_is_assignable_to(ctx, &o, type);
  1049. }
  1050. }
  1051. if (type == nullptr) {
  1052. error(results[i], "Invalid parameter type");
  1053. type = t_invalid;
  1054. }
  1055. if (is_type_untyped(type)) {
  1056. error(results[i], "Cannot determine parameter type from a nil");
  1057. type = t_invalid;
  1058. }
  1059. if (field->names.count == 0) {
  1060. Token token = ast_node_token(field->type);
  1061. token.string = str_lit("");
  1062. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1063. param->Variable.default_value = value;
  1064. param->Variable.default_is_nil = default_is_nil;
  1065. array_add(&variables, param);
  1066. } else {
  1067. for_array(j, field->names) {
  1068. Token token = ast_node_token(results[i]);
  1069. if (field->type != nullptr) {
  1070. token = ast_node_token(field->type);
  1071. }
  1072. token.string = str_lit("");
  1073. AstNode *name = field->names[j];
  1074. if (name->kind != AstNode_Ident) {
  1075. error(name, "Expected an identifer for as the field name");
  1076. } else {
  1077. token = name->Ident.token;
  1078. }
  1079. if (is_blank_ident(token)) {
  1080. error(name, "Result value cannot be a blank identifer `_`");
  1081. }
  1082. Entity *param = alloc_entity_param(scope, token, type, false, false);
  1083. param->flags |= EntityFlag_Result;
  1084. param->Variable.default_value = value;
  1085. param->Variable.default_is_nil = default_is_nil;
  1086. array_add(&variables, param);
  1087. add_entity(ctx->checker, scope, name, param);
  1088. }
  1089. }
  1090. }
  1091. for_array(i, variables) {
  1092. String x = variables[i]->token.string;
  1093. if (x.len == 0 || is_blank_ident(x)) {
  1094. continue;
  1095. }
  1096. for (isize j = i+1; j < variables.count; j++) {
  1097. String y = variables[j]->token.string;
  1098. if (y.len == 0 || is_blank_ident(y)) {
  1099. continue;
  1100. }
  1101. if (x == y) {
  1102. error(variables[j]->token, "Duplicate return value name '%.*s'", LIT(y));
  1103. }
  1104. }
  1105. }
  1106. tuple->Tuple.variables = variables;
  1107. return tuple;
  1108. }
  1109. Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) {
  1110. Type *new_type = original_type;
  1111. if (is_type_boolean(original_type)) {
  1112. return t_llvm_bool;
  1113. }
  1114. if (build_context.ODIN_ARCH == "x86") {
  1115. return new_type;
  1116. }
  1117. if (build_context.ODIN_OS == "windows") {
  1118. // NOTE(bill): Changing the passing parameter value type is to match C's ABI
  1119. // IMPORTANT TODO(bill): This only matches the ABI on MSVC at the moment
  1120. // SEE: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
  1121. Type *bt = core_type(original_type);
  1122. switch (bt->kind) {
  1123. // Okay to pass by value (usually)
  1124. // Especially the only Odin types
  1125. case Type_Basic: {
  1126. i64 sz = bt->Basic.size;
  1127. // if (sz > 8 && build_context.word_size < 8) {
  1128. if (sz > 8) {
  1129. new_type = alloc_type_pointer(original_type);
  1130. }
  1131. break;
  1132. }
  1133. case Type_Pointer: break;
  1134. case Type_Proc: break; // NOTE(bill): Just a pointer
  1135. // Odin specific
  1136. case Type_Slice:
  1137. case Type_Array:
  1138. case Type_DynamicArray:
  1139. case Type_Map:
  1140. case Type_Union:
  1141. // Could be in C too
  1142. case Type_Struct:
  1143. {
  1144. i64 align = type_align_of(original_type);
  1145. i64 size = type_size_of(original_type);
  1146. switch (8*size) {
  1147. case 8: new_type = t_u8; break;
  1148. case 16: new_type = t_u16; break;
  1149. case 32: new_type = t_u32; break;
  1150. case 64: new_type = t_u64; break;
  1151. default:
  1152. new_type = alloc_type_pointer(original_type);
  1153. break;
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. } else if (build_context.ODIN_OS == "linux" ||
  1159. build_context.ODIN_OS == "osx") {
  1160. Type *bt = core_type(original_type);
  1161. switch (bt->kind) {
  1162. // Okay to pass by value (usually)
  1163. // Especially the only Odin types
  1164. case Type_Basic: {
  1165. i64 sz = bt->Basic.size;
  1166. // if (sz > 8 && build_context.word_size < 8) {
  1167. if (sz > 8) {
  1168. new_type = alloc_type_pointer(original_type);
  1169. }
  1170. break;
  1171. }
  1172. case Type_Pointer: break;
  1173. case Type_Proc: break; // NOTE(bill): Just a pointer
  1174. // Odin specific
  1175. case Type_Slice:
  1176. case Type_Array:
  1177. case Type_DynamicArray:
  1178. case Type_Map:
  1179. case Type_Union:
  1180. // Could be in C too
  1181. case Type_Struct: {
  1182. i64 align = type_align_of(original_type);
  1183. i64 size = type_size_of(original_type);
  1184. if (8*size > 16) {
  1185. new_type = alloc_type_pointer(original_type);
  1186. }
  1187. break;
  1188. }
  1189. }
  1190. } else {
  1191. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1192. // their architectures
  1193. }
  1194. return new_type;
  1195. }
  1196. Type *reduce_tuple_to_single_type(Type *original_type) {
  1197. if (original_type != nullptr) {
  1198. Type *t = core_type(original_type);
  1199. if (t->kind == Type_Tuple && t->Tuple.variables.count == 1) {
  1200. return t->Tuple.variables[0]->type;
  1201. }
  1202. }
  1203. return original_type;
  1204. }
  1205. Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type) {
  1206. Type *new_type = original_type;
  1207. if (new_type == nullptr) {
  1208. return nullptr;
  1209. }
  1210. GB_ASSERT(is_type_tuple(original_type));
  1211. if (build_context.ODIN_OS == "windows") {
  1212. Type *bt = core_type(reduce_tuple_to_single_type(original_type));
  1213. // NOTE(bill): This is just reversed engineered from LLVM IR output
  1214. switch (bt->kind) {
  1215. // Okay to pass by value
  1216. // Especially the only Odin types
  1217. case Type_Pointer: break;
  1218. case Type_Proc: break; // NOTE(bill): Just a pointer
  1219. case Type_Basic: break;
  1220. default: {
  1221. i64 align = type_align_of(original_type);
  1222. i64 size = type_size_of(original_type);
  1223. switch (8*size) {
  1224. #if 1
  1225. case 8: new_type = t_u8; break;
  1226. case 16: new_type = t_u16; break;
  1227. case 32: new_type = t_u32; break;
  1228. case 64: new_type = t_u64; break;
  1229. #endif
  1230. }
  1231. break;
  1232. }
  1233. }
  1234. } else if (build_context.ODIN_OS == "linux") {
  1235. } else {
  1236. // IMPORTANT TODO(bill): figure out the ABI settings for Linux, OSX etc. for
  1237. // their architectures
  1238. }
  1239. if (new_type != original_type) {
  1240. Type *tuple = alloc_type_tuple();
  1241. auto variables = array_make<Entity *>(a, 0, 1);
  1242. array_add(&variables, alloc_entity_param(original_type->Tuple.variables[0]->scope, empty_token, new_type, false, false));
  1243. tuple->Tuple.variables = variables;
  1244. new_type = tuple;
  1245. }
  1246. // return reduce_tuple_to_single_type(new_type);
  1247. return new_type;
  1248. }
  1249. bool abi_compat_return_by_value(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type) {
  1250. if (abi_return_type == nullptr) {
  1251. return false;
  1252. }
  1253. switch (cc) {
  1254. case ProcCC_Odin:
  1255. case ProcCC_Contextless:
  1256. return false;
  1257. }
  1258. if (build_context.ODIN_OS == "windows") {
  1259. i64 size = 8*type_size_of(abi_return_type);
  1260. switch (size) {
  1261. case 0:
  1262. case 8:
  1263. case 16:
  1264. case 32:
  1265. case 64:
  1266. return false;
  1267. default:
  1268. return true;
  1269. }
  1270. }
  1271. return false;
  1272. }
  1273. // NOTE(bill): 'operands' is for generating non generic procedure type
  1274. bool check_procedure_type(CheckerContext *ctx, Type *type, AstNode *proc_type_node, Array<Operand> *operands) {
  1275. ast_node(pt, ProcType, proc_type_node);
  1276. if (ctx->polymorphic_scope == nullptr && ctx->allow_polymorphic_types) {
  1277. ctx->polymorphic_scope = ctx->scope;
  1278. }
  1279. CheckerContext c_ = *ctx;
  1280. CheckerContext *c = &c_;
  1281. c->curr_proc_sig = type;
  1282. bool variadic = false;
  1283. isize variadic_index = -1;
  1284. bool success = true;
  1285. isize specialization_count = 0;
  1286. Type *params = check_get_params(c, c->scope, pt->params, &variadic, &variadic_index, &success, &specialization_count, operands);
  1287. Type *results = check_get_results(c, c->scope, pt->results);
  1288. isize param_count = 0;
  1289. isize result_count = 0;
  1290. if (params) param_count = params ->Tuple.variables.count;
  1291. if (results) result_count = results->Tuple.variables.count;
  1292. if (param_count > 0) {
  1293. for_array(i, params->Tuple.variables) {
  1294. Entity *param = params->Tuple.variables[i];
  1295. if (param->kind == Entity_Variable && param->Variable.default_value.kind == ExactValue_Procedure) {
  1296. type->Proc.has_proc_default_values = true;
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. if (result_count > 0) {
  1302. Entity *first = results->Tuple.variables[0];
  1303. type->Proc.has_named_results = first->token.string != "";
  1304. }
  1305. ProcCallingConvention cc = pt->calling_convention;
  1306. if (cc == ProcCC_ForeignBlockDefault) {
  1307. cc = ProcCC_CDecl;
  1308. if (c->foreign_context.default_cc > 0) {
  1309. cc = c->foreign_context.default_cc;
  1310. }
  1311. }
  1312. GB_ASSERT(cc > 0);
  1313. type->Proc.node = proc_type_node;
  1314. type->Proc.scope = c->scope;
  1315. type->Proc.params = params;
  1316. type->Proc.param_count = cast(i32)param_count;
  1317. type->Proc.results = results;
  1318. type->Proc.result_count = cast(i32)result_count;
  1319. type->Proc.variadic = variadic;
  1320. type->Proc.variadic_index = cast(i32)variadic_index;
  1321. type->Proc.calling_convention = cc;
  1322. type->Proc.is_polymorphic = pt->generic;
  1323. type->Proc.specialization_count = specialization_count;
  1324. if (param_count > 0) {
  1325. Entity *end = params->Tuple.variables[param_count-1];
  1326. if (end->flags&EntityFlag_CVarArg) {
  1327. if (cc == ProcCC_StdCall || cc == ProcCC_CDecl) {
  1328. type->Proc.c_vararg = true;
  1329. } else {
  1330. error(end->token, "Calling convention does not support #c_vararg");
  1331. }
  1332. }
  1333. }
  1334. bool is_polymorphic = false;
  1335. for (isize i = 0; i < param_count; i++) {
  1336. Entity *e = params->Tuple.variables[i];
  1337. if (e->kind != Entity_Variable) {
  1338. is_polymorphic = true;
  1339. break;
  1340. } else if (is_type_polymorphic(e->type)) {
  1341. is_polymorphic = true;
  1342. break;
  1343. }
  1344. }
  1345. type->Proc.is_polymorphic = is_polymorphic;
  1346. type->Proc.abi_compat_params = array_make<Type *>(c->allocator, param_count);
  1347. for (isize i = 0; i < param_count; i++) {
  1348. Entity *e = type->Proc.params->Tuple.variables[i];
  1349. if (e->kind == Entity_Variable) {
  1350. Type *original_type = e->type;
  1351. Type *new_type = type_to_abi_compat_param_type(c->allocator, original_type);
  1352. type->Proc.abi_compat_params[i] = new_type;
  1353. }
  1354. }
  1355. // NOTE(bill): The types are the same
  1356. type->Proc.abi_compat_result_type = type_to_abi_compat_result_type(c->allocator, type->Proc.results);
  1357. type->Proc.return_by_pointer = abi_compat_return_by_value(c->allocator, pt->calling_convention, type->Proc.abi_compat_result_type);
  1358. return success;
  1359. }
  1360. i64 check_array_count(CheckerContext *ctx, Operand *o, AstNode *e) {
  1361. if (e == nullptr) {
  1362. return 0;
  1363. }
  1364. if (e->kind == AstNode_UnaryExpr &&
  1365. e->UnaryExpr.op.kind == Token_Ellipsis) {
  1366. return -1;
  1367. }
  1368. check_expr_or_type(ctx, o, e);
  1369. if (o->mode == Addressing_Type && o->type->kind == Type_Generic) {
  1370. if (ctx->allow_polymorphic_types) {
  1371. if (o->type->Generic.specialized) {
  1372. o->type->Generic.specialized = nullptr;
  1373. error(o->expr, "Polymorphic array length cannot have a specialization");
  1374. }
  1375. return 0;
  1376. }
  1377. }
  1378. if (o->mode != Addressing_Constant) {
  1379. if (o->mode != Addressing_Invalid) {
  1380. o->mode = Addressing_Invalid;
  1381. error(e, "Array count must be a constant");
  1382. }
  1383. return 0;
  1384. }
  1385. Type *type = base_type(o->type);
  1386. if (is_type_untyped(type) || is_type_integer(type)) {
  1387. if (o->value.kind == ExactValue_Integer) {
  1388. i64 count = o->value.value_integer;
  1389. if (count >= 0) {
  1390. return count;
  1391. }
  1392. error(e, "Invalid negative array count %lld", cast(long long)count);
  1393. return 0;
  1394. }
  1395. }
  1396. error(e, "Array count must be an integer");
  1397. return 0;
  1398. }
  1399. Type *make_optional_ok_type(Type *value) {
  1400. gbAllocator a = heap_allocator();
  1401. bool typed = true;
  1402. Type *t = alloc_type_tuple();
  1403. array_init(&t->Tuple.variables, a, 0, 2);
  1404. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, value, false, 0));
  1405. array_add (&t->Tuple.variables, alloc_entity_field(nullptr, blank_token, typed ? t_bool : t_untyped_bool, false, 1));
  1406. return t;
  1407. }
  1408. void init_map_entry_type(Type *type) {
  1409. GB_ASSERT(type->kind == Type_Map);
  1410. if (type->Map.entry_type != nullptr) return;
  1411. // NOTE(bill): The preload types may have not been set yet
  1412. GB_ASSERT(t_map_key != nullptr);
  1413. gbAllocator a = heap_allocator();
  1414. Type *entry_type = alloc_type_struct();
  1415. /*
  1416. struct {
  1417. hash: __MapKey;
  1418. next: int;
  1419. key: Key;
  1420. value: Value;
  1421. }
  1422. */
  1423. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1424. dummy_node->kind = AstNode_Invalid;
  1425. Scope *s = create_scope(universal_scope, a);
  1426. auto fields = array_make<Entity *>(a, 0, 3);
  1427. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_key, false, 0, EntityState_Resolved));
  1428. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, EntityState_Resolved));
  1429. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, 2, EntityState_Resolved));
  1430. entry_type->Struct.fields = fields;
  1431. // type_set_offsets(a, entry_type);
  1432. type->Map.entry_type = entry_type;
  1433. }
  1434. void init_map_internal_types(Type *type) {
  1435. GB_ASSERT(type->kind == Type_Map);
  1436. init_map_entry_type(type);
  1437. if (type->Map.internal_type != nullptr) return;
  1438. if (type->Map.generated_struct_type != nullptr) return;
  1439. Type *key = type->Map.key;
  1440. Type *value = type->Map.value;
  1441. GB_ASSERT(key != nullptr);
  1442. GB_ASSERT(value != nullptr);
  1443. Type *generated_struct_type = alloc_type_struct();
  1444. /*
  1445. struct {
  1446. hashes: [dynamic]int;
  1447. entries: [dynamic]EntryType;
  1448. }
  1449. */
  1450. gbAllocator a = heap_allocator();
  1451. AstNode *dummy_node = gb_alloc_item(a, AstNode);
  1452. dummy_node->kind = AstNode_Invalid;
  1453. Scope *s = create_scope(universal_scope, a);
  1454. Type *hashes_type = alloc_type_dynamic_array(t_int);
  1455. Type *entries_type = alloc_type_dynamic_array(type->Map.entry_type);
  1456. auto fields = array_make<Entity *>(a, 0, 2);
  1457. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hashes")), hashes_type, false, 0, EntityState_Resolved));
  1458. array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("entries")), entries_type, false, 1, EntityState_Resolved));
  1459. generated_struct_type->Struct.fields = fields;
  1460. type_set_offsets(generated_struct_type);
  1461. type->Map.generated_struct_type = generated_struct_type;
  1462. type->Map.internal_type = generated_struct_type;
  1463. type->Map.lookup_result_type = make_optional_ok_type(value);
  1464. }
  1465. void check_map_type(CheckerContext *ctx, Type *type, AstNode *node) {
  1466. GB_ASSERT(type->kind == Type_Map);
  1467. ast_node(mt, MapType, node);
  1468. Type *key = check_type(ctx, mt->key);
  1469. Type *value = check_type(ctx, mt->value);
  1470. if (!is_type_valid_for_keys(key)) {
  1471. if (is_type_boolean(key)) {
  1472. error(node, "A boolean cannot be used as a key for a map, use an array instead for this case");
  1473. } else {
  1474. gbString str = type_to_string(key);
  1475. error(node, "Invalid type of a key for a map, got '%s'", str);
  1476. gb_string_free(str);
  1477. }
  1478. }
  1479. type->Map.key = key;
  1480. type->Map.value = value;
  1481. if (is_type_string(key)) {
  1482. add_package_dependency(ctx, "runtime", "__default_hash_string");
  1483. }
  1484. init_preload(ctx->checker);
  1485. init_map_internal_types(type);
  1486. // error(node, "'map' types are not yet implemented");
  1487. }
  1488. bool check_type_internal(CheckerContext *ctx, AstNode *e, Type **type, Type *named_type) {
  1489. GB_ASSERT_NOT_NULL(type);
  1490. if (e == nullptr) {
  1491. *type = t_invalid;
  1492. return true;
  1493. }
  1494. switch (e->kind) {
  1495. case_ast_node(i, Ident, e);
  1496. Operand o = {};
  1497. Entity *entity = check_ident(ctx, &o, e, named_type, nullptr, false);
  1498. gbString err_str = nullptr;
  1499. defer (gb_string_free(err_str));
  1500. switch (o.mode) {
  1501. case Addressing_Invalid:
  1502. break;
  1503. case Addressing_Type: {
  1504. *type = o.type;
  1505. if (!ctx->in_polymorphic_specialization) {
  1506. Type *t = base_type(o.type);
  1507. if (t != nullptr && is_type_polymorphic_struct_unspecialized(t)) {
  1508. err_str = expr_to_string(e);
  1509. error(e, "Invalid use of a non-specialized polymorphic type '%s'", err_str);
  1510. return true;
  1511. }
  1512. }
  1513. // if (ctx->type_level == 0 && entity->state == EntityState_InProgress) {
  1514. // error(entity->token, "Illegal declaration cycle of `%.*s`", LIT(entity->token.string));
  1515. // for_array(j, *ctx->type_path) {
  1516. // Entity *k = (*ctx->type_path)[j];
  1517. // error(k->token, "\t%.*s refers to", LIT(k->token.string));
  1518. // }
  1519. // error(entity->token, "\t%.*s", LIT(entity->token.string));
  1520. // *type = t_invalid;
  1521. // }
  1522. return true;
  1523. }
  1524. case Addressing_NoValue:
  1525. err_str = expr_to_string(e);
  1526. error(e, "'%s' used as a type", err_str);
  1527. break;
  1528. default:
  1529. err_str = expr_to_string(e);
  1530. error(e, "'%s' used as a type when not a type", err_str);
  1531. break;
  1532. }
  1533. case_end;
  1534. case_ast_node(ht, HelperType, e);
  1535. return check_type_internal(ctx, ht->type, type, named_type);
  1536. case_end;
  1537. case_ast_node(dt, DistinctType, e);
  1538. error(e, "Invalid use of a distinct type");
  1539. // NOTE(bill): Treat it as a HelperType to remove errors
  1540. return check_type_internal(ctx, dt->type, type, named_type);
  1541. case_end;
  1542. case_ast_node(pt, PolyType, e);
  1543. AstNode *ident = pt->type;
  1544. if (ident->kind != AstNode_Ident) {
  1545. error(ident, "Expected an identifier after the $");
  1546. *type = t_invalid;
  1547. return false;
  1548. }
  1549. Token token = ident->Ident.token;
  1550. Type *specific = nullptr;
  1551. if (pt->specialization != nullptr) {
  1552. CheckerContext c = *ctx;
  1553. c.in_polymorphic_specialization = true;
  1554. AstNode *s = pt->specialization;
  1555. specific = check_type(&c, s);
  1556. }
  1557. Type *t = alloc_type_generic(ctx->scope, 0, token.string, specific);
  1558. if (ctx->allow_polymorphic_types) {
  1559. Scope *ps = ctx->polymorphic_scope;
  1560. Scope *s = ctx->scope;
  1561. Scope *entity_scope = s;
  1562. if (ps != nullptr && ps != s) {
  1563. // TODO(bill): Is this check needed?
  1564. // GB_ASSERT_MSG(is_scope_an_ancestor(ps, s) >= 0);
  1565. entity_scope = ps;
  1566. }
  1567. Entity *e = alloc_entity_type_name(entity_scope, token, t);
  1568. e->TypeName.is_type_alias = true;
  1569. e->state = EntityState_Resolved;
  1570. add_entity(ctx->checker, ps, ident, e);
  1571. add_entity(ctx->checker, s, ident, e);
  1572. } else {
  1573. error(ident, "Invalid use of a polymorphic parameter '$%.*s'", LIT(token.string));
  1574. *type = t_invalid;
  1575. return false;
  1576. }
  1577. *type = t;
  1578. set_base_type(named_type, *type);
  1579. return true;
  1580. case_end;
  1581. case_ast_node(se, SelectorExpr, e);
  1582. Operand o = {};
  1583. check_selector(ctx, &o, e, nullptr);
  1584. gbString err_str;
  1585. switch (o.mode) {
  1586. case Addressing_Invalid:
  1587. break;
  1588. case Addressing_Type:
  1589. GB_ASSERT(o.type != nullptr);
  1590. *type = o.type;
  1591. return true;
  1592. case Addressing_NoValue:
  1593. err_str = expr_to_string(e);
  1594. error(e, "'%s' used as a type", err_str);
  1595. gb_string_free(err_str);
  1596. break;
  1597. default:
  1598. err_str = expr_to_string(e);
  1599. error(e, "'%s' is not a type", err_str);
  1600. gb_string_free(err_str);
  1601. break;
  1602. }
  1603. case_end;
  1604. case_ast_node(pe, ParenExpr, e);
  1605. *type = check_type_expr(ctx, pe->expr, named_type);
  1606. set_base_type(named_type, *type);
  1607. return true;
  1608. case_end;
  1609. case_ast_node(ue, UnaryExpr, e);
  1610. switch (ue->op.kind) {
  1611. case Token_Pointer:
  1612. *type = alloc_type_pointer(check_type(ctx, ue->expr));
  1613. set_base_type(named_type, *type);
  1614. return true;
  1615. }
  1616. case_end;
  1617. case_ast_node(pt, PointerType, e);
  1618. *type = alloc_type_pointer(check_type(ctx, pt->type));
  1619. set_base_type(named_type, *type);
  1620. return true;
  1621. case_end;
  1622. case_ast_node(at, ArrayType, e);
  1623. if (at->count != nullptr) {
  1624. Operand o = {};
  1625. i64 count = check_array_count(ctx, &o, at->count);
  1626. Type *generic_type = nullptr;
  1627. if (o.mode == Addressing_Type && o.type->kind == Type_Generic) {
  1628. generic_type = o.type;
  1629. }
  1630. if (count < 0) {
  1631. error(at->count, "... can only be used in conjuction with compound literals");
  1632. count = 0;
  1633. }
  1634. Type *elem = check_type_expr(ctx, at->elem, nullptr);
  1635. *type = alloc_type_array(elem, count, generic_type);
  1636. } else {
  1637. Type *elem = check_type(ctx, at->elem);
  1638. *type = alloc_type_slice(elem);
  1639. }
  1640. set_base_type(named_type, *type);
  1641. return true;
  1642. case_end;
  1643. case_ast_node(dat, DynamicArrayType, e);
  1644. Type *elem = check_type(ctx, dat->elem);
  1645. *type = alloc_type_dynamic_array(elem);
  1646. set_base_type(named_type, *type);
  1647. return true;
  1648. case_end;
  1649. case_ast_node(st, StructType, e);
  1650. CheckerContext c = *ctx;
  1651. c.in_polymorphic_specialization = false;
  1652. c.type_level += 1;
  1653. *type = alloc_type_struct();
  1654. set_base_type(named_type, *type);
  1655. check_open_scope(&c, e);
  1656. check_struct_type(&c, *type, e, nullptr, named_type);
  1657. check_close_scope(&c);
  1658. (*type)->Struct.node = e;
  1659. return true;
  1660. case_end;
  1661. case_ast_node(ut, UnionType, e);
  1662. CheckerContext c = *ctx;
  1663. c.in_polymorphic_specialization = false;
  1664. c.type_level += 1;
  1665. *type = alloc_type_union();
  1666. set_base_type(named_type, *type);
  1667. check_open_scope(&c, e);
  1668. check_union_type(&c, *type, e);
  1669. check_close_scope(&c);
  1670. (*type)->Union.node = e;
  1671. return true;
  1672. case_end;
  1673. case_ast_node(et, EnumType, e);
  1674. bool ips = ctx->in_polymorphic_specialization;
  1675. defer (ctx->in_polymorphic_specialization = ips);
  1676. ctx->in_polymorphic_specialization = false;
  1677. *type = alloc_type_enum();
  1678. set_base_type(named_type, *type);
  1679. check_open_scope(ctx, e);
  1680. check_enum_type(ctx, *type, named_type, e);
  1681. check_close_scope(ctx);
  1682. (*type)->Enum.node = e;
  1683. return true;
  1684. case_end;
  1685. case_ast_node(et, BitFieldType, e);
  1686. *type = alloc_type_bit_field();
  1687. set_base_type(named_type, *type);
  1688. check_open_scope(ctx, e);
  1689. check_bit_field_type(ctx, *type, e);
  1690. check_close_scope(ctx);
  1691. return true;
  1692. case_end;
  1693. case_ast_node(pt, ProcType, e);
  1694. bool ips = ctx->in_polymorphic_specialization;
  1695. defer (ctx->in_polymorphic_specialization = ips);
  1696. ctx->in_polymorphic_specialization = false;
  1697. *type = alloc_type(Type_Proc);
  1698. set_base_type(named_type, *type);
  1699. check_open_scope(ctx, e);
  1700. check_procedure_type(ctx, *type, e);
  1701. check_close_scope(ctx);
  1702. return true;
  1703. case_end;
  1704. case_ast_node(mt, MapType, e);
  1705. bool ips = ctx->in_polymorphic_specialization;
  1706. defer (ctx->in_polymorphic_specialization = ips);
  1707. ctx->in_polymorphic_specialization = false;
  1708. *type = alloc_type(Type_Map);
  1709. set_base_type(named_type, *type);
  1710. check_map_type(ctx, *type, e);
  1711. return true;
  1712. case_end;
  1713. case_ast_node(ce, CallExpr, e);
  1714. Operand o = {};
  1715. check_expr_or_type(ctx, &o, e);
  1716. if (o.mode == Addressing_Type) {
  1717. *type = o.type;
  1718. set_base_type(named_type, *type);
  1719. return true;
  1720. }
  1721. case_end;
  1722. case_ast_node(te, TernaryExpr, e);
  1723. Operand o = {};
  1724. check_expr_or_type(ctx, &o, e);
  1725. if (o.mode == Addressing_Type) {
  1726. *type = o.type;
  1727. set_base_type(named_type, *type);
  1728. return true;
  1729. }
  1730. case_end;
  1731. }
  1732. *type = t_invalid;
  1733. return false;
  1734. }
  1735. Type *check_type(CheckerContext *ctx, AstNode *e) {
  1736. CheckerContext c = *ctx;
  1737. c.type_path = new_checker_type_path();
  1738. defer (destroy_checker_type_path(c.type_path));
  1739. return check_type_expr(&c, e, nullptr);
  1740. }
  1741. Type *check_type_expr(CheckerContext *ctx, AstNode *e, Type *named_type) {
  1742. Type *type = nullptr;
  1743. bool ok = check_type_internal(ctx, e, &type, named_type);
  1744. if (!ok) {
  1745. gbString err_str = expr_to_string(e);
  1746. error(e, "'%s' is not a type", err_str);
  1747. gb_string_free(err_str);
  1748. type = t_invalid;
  1749. }
  1750. if (type == nullptr) {
  1751. type = t_invalid;
  1752. }
  1753. if (type->kind == Type_Named &&
  1754. type->Named.base == nullptr) {
  1755. // IMPORTANT TODO(bill): Is this a serious error?!
  1756. #if 0
  1757. error(e, "Invalid type definition of '%.*s'", LIT(type->Named.name));
  1758. #endif
  1759. type->Named.base = t_invalid;
  1760. }
  1761. #if 0
  1762. if (!ctx->allow_polymorphic_types && is_type_polymorphic(type)) {
  1763. gbString str = type_to_string(type);
  1764. error(e, "Invalid use of a polymorphic type '%s'", str);
  1765. gb_string_free(str);
  1766. type = t_invalid;
  1767. }
  1768. #endif
  1769. if (is_type_typed(type)) {
  1770. add_type_and_value(&ctx->checker->info, e, Addressing_Type, type, empty_exact_value);
  1771. } else {
  1772. gbString name = type_to_string(type);
  1773. error(e, "Invalid type definition of %s", name);
  1774. gb_string_free(name);
  1775. type = t_invalid;
  1776. }
  1777. set_base_type(named_type, type);
  1778. return type;
  1779. }