check_decl.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. bool check_is_terminating(Ast *node);
  2. void check_stmt (CheckerContext *ctx, Ast *node, u32 flags);
  3. // NOTE(bill): 'content_name' is for debugging and error messages
  4. Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *operand, String context_name) {
  5. if (operand->mode == Addressing_Invalid ||
  6. operand->type == t_invalid ||
  7. e->type == t_invalid) {
  8. if (operand->mode == Addressing_Builtin) {
  9. gbString expr_str = expr_to_string(operand->expr);
  10. // TODO(bill): is this a good enough error message?
  11. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  12. error(operand->expr,
  13. "Cannot assign built-in procedure '%s' in %.*s",
  14. expr_str,
  15. LIT(context_name));
  16. operand->mode = Addressing_Invalid;
  17. gb_string_free(expr_str);
  18. }
  19. if (operand->mode == Addressing_ProcGroup) {
  20. if (e->type == nullptr) {
  21. error(operand->expr, "Cannot determine type from overloaded procedure '%.*s'", LIT(operand->proc_group->token.string));
  22. } else {
  23. check_assignment(ctx, operand, e->type, str_lit("variable assignment"));
  24. if (operand->mode != Addressing_Type) {
  25. return operand->type;
  26. }
  27. }
  28. }
  29. if (e->type == nullptr) {
  30. e->type = t_invalid;
  31. }
  32. return nullptr;
  33. }
  34. if (operand->mode == Addressing_Type) {
  35. gbString t = type_to_string(operand->type);
  36. error(operand->expr, "Cannot assign a type '%s' to variable '%.*s'", t, LIT(e->token.string));
  37. gb_string_free(t);
  38. e->type = operand->type;
  39. return nullptr;
  40. }
  41. if (e->type == nullptr) {
  42. // NOTE(bill): Use the type of the operand
  43. Type *t = operand->type;
  44. if (is_type_untyped(t)) {
  45. if (t == t_invalid || is_type_untyped_nil(t)) {
  46. error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name));
  47. e->type = t_invalid;
  48. return nullptr;
  49. }
  50. if (t == t_invalid || is_type_untyped_undef(t)) {
  51. error(e->token, "Invalid use of --- in %.*s", LIT(context_name));
  52. e->type = t_invalid;
  53. return nullptr;
  54. }
  55. t = default_type(t);
  56. }
  57. if (is_type_polymorphic(t)) {
  58. gbString str = type_to_string(t);
  59. defer (gb_string_free(str));
  60. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  61. e->type = t_invalid;
  62. return nullptr;
  63. } else if (is_type_empty_union(t)) {
  64. gbString str = type_to_string(t);
  65. defer (gb_string_free(str));
  66. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  67. e->type = t_invalid;
  68. return nullptr;
  69. }
  70. if (is_type_bit_field_value(t)) {
  71. t = default_bit_field_value_type(t);
  72. }
  73. GB_ASSERT(is_type_typed(t));
  74. e->type = t;
  75. }
  76. e->parent_proc_decl = ctx->curr_proc_decl;
  77. check_assignment(ctx, operand, e->type, context_name);
  78. if (operand->mode == Addressing_Invalid) {
  79. return nullptr;
  80. }
  81. return e->type;
  82. }
  83. void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Array<Ast *> const &inits, String context_name) {
  84. if ((lhs == nullptr || lhs_count == 0) && inits.count == 0) {
  85. return;
  86. }
  87. // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
  88. // an extra allocation
  89. auto operands = array_make<Operand>(ctx->allocator, 0, 2*lhs_count);
  90. defer (array_free(&operands));
  91. check_unpack_arguments(ctx, lhs, lhs_count, &operands, inits, true, false);
  92. isize rhs_count = operands.count;
  93. for_array(i, operands) {
  94. if (operands[i].mode == Addressing_Invalid) {
  95. rhs_count--;
  96. }
  97. }
  98. isize max = gb_min(lhs_count, rhs_count);
  99. for (isize i = 0; i < max; i++) {
  100. Entity *e = lhs[i];
  101. DeclInfo *d = decl_info_of_entity(e);
  102. Operand *o = &operands[i];
  103. check_init_variable(ctx, e, o, context_name);
  104. if (d != nullptr) {
  105. d->init_expr = o->expr;
  106. }
  107. }
  108. if (rhs_count > 0 && lhs_count != rhs_count) {
  109. error(lhs[0]->token, "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
  110. }
  111. }
  112. void check_init_constant(CheckerContext *ctx, Entity *e, Operand *operand) {
  113. if (operand->mode == Addressing_Invalid ||
  114. operand->type == t_invalid ||
  115. e->type == t_invalid) {
  116. if (e->type == nullptr) {
  117. e->type = t_invalid;
  118. }
  119. return;
  120. }
  121. if (operand->mode != Addressing_Constant) {
  122. // TODO(bill): better error
  123. gbString str = expr_to_string(operand->expr);
  124. error(operand->expr, "'%s' is not a constant", str);
  125. gb_string_free(str);
  126. if (e->type == nullptr) {
  127. e->type = t_invalid;
  128. }
  129. return;
  130. }
  131. if (!is_type_constant_type(operand->type)) {
  132. gbString type_str = type_to_string(operand->type);
  133. error(operand->expr, "Invalid constant type: '%s'", type_str);
  134. gb_string_free(type_str);
  135. if (e->type == nullptr) {
  136. e->type = t_invalid;
  137. }
  138. return;
  139. }
  140. if (e->type == nullptr) { // NOTE(bill): type inference
  141. e->type = operand->type;
  142. }
  143. check_assignment(ctx, operand, e->type, str_lit("constant declaration"));
  144. if (operand->mode == Addressing_Invalid) {
  145. return;
  146. }
  147. e->parent_proc_decl = ctx->curr_proc_decl;
  148. e->Constant.value = operand->value;
  149. }
  150. bool is_type_distinct(Ast *node) {
  151. for (;;) {
  152. if (node == nullptr) {
  153. return false;
  154. }
  155. if (node->kind == Ast_ParenExpr) {
  156. node = node->ParenExpr.expr;
  157. } else if (node->kind == Ast_HelperType) {
  158. node = node->HelperType.type;
  159. } else {
  160. break;
  161. }
  162. }
  163. switch (node->kind) {
  164. case Ast_DistinctType:
  165. return true;
  166. case Ast_StructType:
  167. case Ast_UnionType:
  168. case Ast_EnumType:
  169. case Ast_BitFieldType:
  170. case Ast_ProcType:
  171. return true;
  172. case Ast_PointerType:
  173. case Ast_ArrayType:
  174. case Ast_DynamicArrayType:
  175. case Ast_MapType:
  176. return false;
  177. case Ast_OpaqueType:
  178. return true;
  179. }
  180. return false;
  181. }
  182. Ast *remove_type_alias_clutter(Ast *node) {
  183. for (;;) {
  184. if (node == nullptr) {
  185. return nullptr;
  186. }
  187. if (node->kind == Ast_ParenExpr) {
  188. node = node->ParenExpr.expr;
  189. } else if (node->kind == Ast_DistinctType) {
  190. node = node->DistinctType.type;
  191. } else {
  192. return node;
  193. }
  194. }
  195. }
  196. isize total_attribute_count(DeclInfo *decl) {
  197. isize attribute_count = 0;
  198. for_array(i, decl->attributes) {
  199. Ast *attr = decl->attributes[i];
  200. if (attr->kind != Ast_Attribute) continue;
  201. attribute_count += attr->Attribute.elems.count;
  202. }
  203. return attribute_count;
  204. }
  205. void check_type_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Type *def) {
  206. GB_ASSERT(e->type == nullptr);
  207. DeclInfo *decl = decl_info_of_entity(e);
  208. if (decl != nullptr) {
  209. check_decl_attributes(ctx, decl->attributes, const_decl_attribute, nullptr);
  210. }
  211. bool is_distinct = is_type_distinct(type_expr);
  212. Ast *te = remove_type_alias_clutter(type_expr);
  213. e->type = t_invalid;
  214. String name = e->token.string;
  215. Type *named = alloc_type_named(name, nullptr, e);
  216. if (def != nullptr && def->kind == Type_Named) {
  217. def->Named.base = named;
  218. }
  219. e->type = named;
  220. check_type_path_push(ctx, e);
  221. Type *bt = check_type_expr(ctx, te, named);
  222. check_type_path_pop(ctx);
  223. named->Named.base = base_type(bt);
  224. if (is_distinct && is_type_typeid(e->type)) {
  225. error(type_expr, "'distinct' cannot be applied to 'typeid'");
  226. is_distinct = false;
  227. }
  228. if (!is_distinct) {
  229. e->type = bt;
  230. named->Named.base = bt;
  231. e->TypeName.is_type_alias = true;
  232. }
  233. // using decl
  234. if (decl->is_using) {
  235. // NOTE(bill): Must be an enum declaration
  236. if (te->kind == Ast_EnumType) {
  237. Scope *parent = e->scope;
  238. if (parent->flags&ScopeFlag_File) {
  239. // NOTE(bill): Use package scope
  240. parent = parent->parent;
  241. }
  242. Type *t = base_type(e->type);
  243. if (t->kind == Type_Enum) {
  244. for_array(i, t->Enum.fields) {
  245. Entity *f = t->Enum.fields[i];
  246. if (f->kind != Entity_Constant) {
  247. continue;
  248. }
  249. String name = f->token.string;
  250. if (is_blank_ident(name)) {
  251. continue;
  252. }
  253. add_entity(ctx->checker, parent, nullptr, f);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. void override_entity_in_scope(Entity *original_entity, Entity *new_entity) {
  260. // NOTE(bill): The original_entity's scope may not be same scope that it was inserted into
  261. // e.g. file entity inserted into its package scope
  262. String original_name = original_entity->token.string;
  263. Scope *found_scope = nullptr;
  264. Entity *found_entity = nullptr;
  265. scope_lookup_parent(original_entity->scope, original_name, &found_scope, &found_entity);
  266. // IMPORTANT TODO(bill)
  267. // Date: 2018-09-29
  268. // This assert fails on `using import` if the name of the alias is the same. What should be the expected behaviour?
  269. // Namespace collision or override? Overridding is the current behaviour
  270. //
  271. // using import "foo"
  272. // bar :: foo.bar;
  273. // GB_ASSERT_MSG(found_entity == original_entity, "%.*s == %.*s", LIT(found_entity->token.string), LIT(new_entity->token.string));
  274. map_set(&found_scope->elements, hash_string(original_name), new_entity);
  275. }
  276. void check_const_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init, Type *named_type) {
  277. GB_ASSERT(e->type == nullptr);
  278. GB_ASSERT(e->kind == Entity_Constant);
  279. if (e->flags & EntityFlag_Visited) {
  280. e->type = t_invalid;
  281. return;
  282. }
  283. e->flags |= EntityFlag_Visited;
  284. if (type_expr) {
  285. Type *t = check_type(ctx, type_expr);
  286. if (!is_type_constant_type(t)) {
  287. gbString str = type_to_string(t);
  288. error(type_expr, "Invalid constant type '%s'", str);
  289. gb_string_free(str);
  290. e->type = t_invalid;
  291. return;
  292. }
  293. e->type = t;
  294. }
  295. Operand operand = {};
  296. if (init != nullptr) {
  297. Entity *entity = nullptr;
  298. if (init->kind == Ast_Ident) {
  299. entity = check_ident(ctx, &operand, init, nullptr, e->type, true);
  300. } else if (init->kind == Ast_SelectorExpr) {
  301. entity = check_selector(ctx, &operand, init, e->type);
  302. } else {
  303. check_expr_or_type(ctx, &operand, init, e->type);
  304. }
  305. switch (operand.mode) {
  306. case Addressing_Type: {
  307. e->kind = Entity_TypeName;
  308. e->type = nullptr;
  309. DeclInfo *d = ctx->decl;
  310. if (d->type_expr != nullptr) {
  311. error(e->token, "A type declaration cannot have an type parameter");
  312. }
  313. d->type_expr = d->init_expr;
  314. check_type_decl(ctx, e, d->type_expr, named_type);
  315. return;
  316. }
  317. // NOTE(bill): Check to see if the expression it to be aliases
  318. case Addressing_Builtin:
  319. if (e->type != nullptr) {
  320. error(type_expr, "A constant alias of a built-in procedure may not have a type initializer");
  321. }
  322. e->kind = Entity_Builtin;
  323. e->Builtin.id = operand.builtin_id;
  324. e->type = t_invalid;
  325. return;
  326. case Addressing_ProcGroup:
  327. GB_ASSERT(operand.proc_group != nullptr);
  328. GB_ASSERT(operand.proc_group->kind == Entity_ProcGroup);
  329. override_entity_in_scope(e, operand.proc_group);
  330. return;
  331. }
  332. if (entity != nullptr) {
  333. // NOTE(bill): Override aliased entity
  334. switch (entity->kind) {
  335. case Entity_ProcGroup:
  336. case Entity_Procedure:
  337. case Entity_LibraryName:
  338. case Entity_ImportName:
  339. {
  340. override_entity_in_scope(e, entity);
  341. DeclInfo *decl = decl_info_of_entity(e);
  342. if (decl != nullptr) {
  343. if (decl->attributes.count > 0) {
  344. error(decl->attributes[0], "Constant alias declarations cannot have attributes");
  345. }
  346. }
  347. return;
  348. }
  349. }
  350. }
  351. }
  352. check_init_constant(ctx, e, &operand);
  353. if (operand.mode == Addressing_Invalid ||
  354. base_type(operand.type) == t_invalid) {
  355. gbString str = expr_to_string(init);
  356. error(e->token, "Invalid declaration type '%s'", str);
  357. gb_string_free(str);
  358. }
  359. DeclInfo *decl = decl_info_of_entity(e);
  360. if (decl != nullptr) {
  361. check_decl_attributes(ctx, decl->attributes, const_decl_attribute, nullptr);
  362. }
  363. }
  364. typedef bool TypeCheckSig(Type *t);
  365. bool sig_compare(TypeCheckSig *a, Type *x, Type *y) {
  366. return (a(x) && a(y));
  367. }
  368. bool sig_compare(TypeCheckSig *a, TypeCheckSig *b, Type *x, Type *y) {
  369. if (a == b) {
  370. return sig_compare(a, x, y);
  371. }
  372. return (a(x) && b(y) || b(x) && a(y));
  373. }
  374. bool signature_parameter_similar_enough(Type *x, Type *y) {
  375. if (sig_compare(is_type_pointer, x, y)) {
  376. return true;
  377. }
  378. if (sig_compare(is_type_integer, x, y)) {
  379. GB_ASSERT(x->kind == Type_Basic);
  380. GB_ASSERT(y->kind == Type_Basic);
  381. i64 sx = type_size_of(x);
  382. i64 sy = type_size_of(y);
  383. if (sx == sy) return true;
  384. }
  385. if (sig_compare(is_type_integer, is_type_boolean, x, y)) {
  386. GB_ASSERT(x->kind == Type_Basic);
  387. GB_ASSERT(y->kind == Type_Basic);
  388. i64 sx = type_size_of(x);
  389. i64 sy = type_size_of(y);
  390. if (sx == sy) return true;
  391. }
  392. if (sig_compare(is_type_cstring, is_type_u8_ptr, x, y)) {
  393. return true;
  394. }
  395. if (sig_compare(is_type_uintptr, is_type_rawptr, x, y)) {
  396. return true;
  397. }
  398. return are_types_identical(x, y);
  399. }
  400. bool are_signatures_similar_enough(Type *a_, Type *b_) {
  401. GB_ASSERT(a_->kind == Type_Proc);
  402. GB_ASSERT(b_->kind == Type_Proc);
  403. TypeProc *a = &a_->Proc;
  404. TypeProc *b = &b_->Proc;
  405. if (a->param_count != b->param_count) {
  406. return false;
  407. }
  408. if (a->result_count != b->result_count) {
  409. return false;
  410. }
  411. for (isize i = 0; i < a->param_count; i++) {
  412. Type *x = core_type(a->params->Tuple.variables[i]->type);
  413. Type *y = core_type(b->params->Tuple.variables[i]->type);
  414. if (!signature_parameter_similar_enough(x, y)) {
  415. return false;
  416. }
  417. }
  418. for (isize i = 0; i < a->result_count; i++) {
  419. Type *x = base_type(a->results->Tuple.variables[i]->type);
  420. Type *y = base_type(b->results->Tuple.variables[i]->type);
  421. if (!signature_parameter_similar_enough(x, y)) {
  422. return false;
  423. }
  424. }
  425. return true;
  426. }
  427. void init_entity_foreign_library(CheckerContext *ctx, Entity *e) {
  428. Ast *ident = nullptr;
  429. Entity **foreign_library = nullptr;
  430. switch (e->kind) {
  431. case Entity_Procedure:
  432. ident = e->Procedure.foreign_library_ident;
  433. foreign_library = &e->Procedure.foreign_library;
  434. break;
  435. case Entity_Variable:
  436. ident = e->Variable.foreign_library_ident;
  437. foreign_library = &e->Variable.foreign_library;
  438. break;
  439. default:
  440. return;
  441. }
  442. if (ident == nullptr) {
  443. error(e->token, "foreign entiies must declare which library they are from");
  444. } else if (ident->kind != Ast_Ident) {
  445. error(ident, "foreign library names must be an identifier");
  446. } else {
  447. String name = ident->Ident.token.string;
  448. Entity *found = scope_lookup(ctx->scope, name);
  449. if (found == nullptr) {
  450. if (is_blank_ident(name)) {
  451. // NOTE(bill): link against nothing
  452. } else {
  453. error(ident, "Undeclared name: %.*s", LIT(name));
  454. }
  455. } else if (found->kind != Entity_LibraryName) {
  456. error(ident, "'%.*s' cannot be used as a library name", LIT(name));
  457. } else {
  458. // TODO(bill): Extra stuff to do with library names?
  459. *foreign_library = found;
  460. found->flags |= EntityFlag_Used;
  461. add_entity_use(ctx, ident, found);
  462. }
  463. }
  464. }
  465. String handle_link_name(CheckerContext *ctx, Token token, String link_name, String link_prefix) {
  466. if (link_prefix.len > 0) {
  467. if (link_name.len > 0) {
  468. error(token, "'link_name' and 'link_prefix' cannot be used together");
  469. } else {
  470. isize len = link_prefix.len + token.string.len;
  471. u8 *name = gb_alloc_array(ctx->allocator, u8, len+1);
  472. gb_memmove(name, &link_prefix[0], link_prefix.len);
  473. gb_memmove(name+link_prefix.len, &token.string[0], token.string.len);
  474. name[len] = 0;
  475. link_name = make_string(name, len);
  476. }
  477. }
  478. return link_name;
  479. }
  480. void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) {
  481. GB_ASSERT(e->type == nullptr);
  482. if (d->proc_lit->kind != Ast_ProcLit) {
  483. // TOOD(bill): Better error message
  484. error(d->proc_lit, "Expected a procedure to check");
  485. return;
  486. }
  487. Type *proc_type = e->type;
  488. if (d->gen_proc_type != nullptr) {
  489. proc_type = d->gen_proc_type;
  490. } else {
  491. proc_type = alloc_type_proc(e->scope, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
  492. }
  493. e->type = proc_type;
  494. ast_node(pl, ProcLit, d->proc_lit);
  495. check_open_scope(ctx, pl->type);
  496. defer (check_close_scope(ctx));
  497. auto tmp_ctx = *ctx;
  498. tmp_ctx.allow_polymorphic_types = true;
  499. check_procedure_type(&tmp_ctx, proc_type, pl->type);
  500. TypeProc *pt = &proc_type->Proc;
  501. AttributeContext ac = make_attribute_context(e->Procedure.link_prefix);
  502. if (d != nullptr) {
  503. check_decl_attributes(ctx, d->attributes, proc_decl_attribute, &ac);
  504. }
  505. e->Procedure.is_export = ac.is_export;
  506. e->deprecated_message = ac.deprecated_message;
  507. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  508. bool is_foreign = e->Procedure.is_foreign;
  509. bool is_export = e->Procedure.is_export;
  510. bool is_require_results = (pl->tags & ProcTag_require_results) != 0;
  511. if (e->pkg != nullptr && e->token.string == "main") {
  512. if (pt->param_count != 0 ||
  513. pt->result_count != 0) {
  514. gbString str = type_to_string(proc_type);
  515. error(e->token, "Procedure type of 'main' was expected to be 'proc()', got %s", str);
  516. gb_string_free(str);
  517. }
  518. if (pt->calling_convention != ProcCC_Odin &&
  519. pt->calling_convention != ProcCC_Contextless) {
  520. error(e->token, "Procedure 'main' cannot have a custom calling convention");
  521. }
  522. pt->calling_convention = ProcCC_Contextless;
  523. if (e->pkg->kind == Package_Init) {
  524. if (ctx->info->entry_point != nullptr) {
  525. error(e->token, "Redeclaration of the entry pointer procedure 'main'");
  526. } else {
  527. ctx->info->entry_point = e;
  528. }
  529. }
  530. }
  531. if (is_foreign && is_export) {
  532. error(pl->type, "A foreign procedure cannot have an 'export' tag");
  533. }
  534. if (pt->is_polymorphic) {
  535. if (pl->body == nullptr) {
  536. error(e->token, "Polymorphic procedures must have a body");
  537. }
  538. if (is_foreign) {
  539. error(e->token, "A foreign procedure cannot be a polymorphic");
  540. return;
  541. }
  542. }
  543. if (pl->body != nullptr) {
  544. if (is_foreign) {
  545. error(pl->body, "A foreign procedure cannot have a body");
  546. }
  547. if (proc_type->Proc.c_vararg) {
  548. error(pl->body, "A procedure with a '#c_vararg' field cannot have a body and must be foreign");
  549. }
  550. d->scope = ctx->scope;
  551. GB_ASSERT(pl->body->kind == Ast_BlockStmt);
  552. if (!pt->is_polymorphic) {
  553. check_procedure_later(ctx->checker, ctx->file, e->token, d, proc_type, pl->body, pl->tags);
  554. }
  555. } else if (!is_foreign) {
  556. if (e->Procedure.is_export) {
  557. error(e->token, "Foreign export procedures must have a body");
  558. } else {
  559. error(e->token, "Only a foreign procedure cannot have a body");
  560. }
  561. }
  562. if (pt->result_count == 0 && is_require_results) {
  563. error(pl->type, "'#require_results' is not needed on a procedure with no results");
  564. } else {
  565. pt->require_results = is_require_results;
  566. }
  567. if (ac.link_name.len > 0) {
  568. e->Procedure.link_name = ac.link_name;
  569. }
  570. if (ac.deferred_procedure.entity != nullptr) {
  571. e->Procedure.deferred_procedure = ac.deferred_procedure;
  572. array_add(&ctx->checker->procs_with_deferred_to_check, e);
  573. }
  574. if (is_foreign) {
  575. String name = e->token.string;
  576. if (e->Procedure.link_name.len > 0) {
  577. name = e->Procedure.link_name;
  578. }
  579. e->Procedure.is_foreign = true;
  580. e->Procedure.link_name = name;
  581. init_entity_foreign_library(ctx, e);
  582. auto *fp = &ctx->info->foreigns;
  583. HashKey key = hash_string(name);
  584. Entity **found = map_get(fp, key);
  585. if (found) {
  586. Entity *f = *found;
  587. TokenPos pos = f->token.pos;
  588. Type *this_type = base_type(e->type);
  589. Type *other_type = base_type(f->type);
  590. if (is_type_proc(this_type) && is_type_proc(other_type)) {
  591. if (!are_signatures_similar_enough(this_type, other_type)) {
  592. error(d->proc_lit,
  593. "Redeclaration of foreign procedure '%.*s' with different type signatures\n"
  594. "\tat %.*s(%td:%td)",
  595. LIT(name), LIT(pos.file), pos.line, pos.column);
  596. }
  597. } else if (!are_types_identical(this_type, other_type)) {
  598. error(d->proc_lit,
  599. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  600. "\tat %.*s(%td:%td)",
  601. LIT(name), LIT(pos.file), pos.line, pos.column);
  602. }
  603. } else if (name == "main") {
  604. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  605. } else {
  606. map_set(fp, key, e);
  607. }
  608. } else {
  609. String name = e->token.string;
  610. if (e->Procedure.link_name.len > 0) {
  611. name = e->Procedure.link_name;
  612. }
  613. if (e->Procedure.link_name.len > 0 || is_export) {
  614. auto *fp = &ctx->info->foreigns;
  615. HashKey key = hash_string(name);
  616. Entity **found = map_get(fp, key);
  617. if (found) {
  618. Entity *f = *found;
  619. TokenPos pos = f->token.pos;
  620. // TODO(bill): Better error message?
  621. error(d->proc_lit,
  622. "Non unique linking name for procedure '%.*s'\n"
  623. "\tother at %.*s(%td:%td)",
  624. LIT(name), LIT(pos.file), pos.line, pos.column);
  625. } else if (name == "main") {
  626. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  627. } else {
  628. map_set(fp, key, e);
  629. }
  630. }
  631. }
  632. }
  633. void check_var_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init_expr) {
  634. GB_ASSERT(e->type == nullptr);
  635. GB_ASSERT(e->kind == Entity_Variable);
  636. if (e->flags & EntityFlag_Visited) {
  637. e->type = t_invalid;
  638. return;
  639. }
  640. e->flags |= EntityFlag_Visited;
  641. AttributeContext ac = make_attribute_context(e->Variable.link_prefix);
  642. ac.init_expr_list_count = init_expr != nullptr ? 1 : 0;
  643. DeclInfo *decl = decl_info_of_entity(e);
  644. if (decl != nullptr) {
  645. check_decl_attributes(ctx, decl->attributes, var_decl_attribute, &ac);
  646. }
  647. e->Variable.thread_local_model = ac.thread_local_model;
  648. e->Variable.is_export = ac.is_export;
  649. if (ac.is_static) {
  650. e->flags |= EntityFlag_Static;
  651. } else {
  652. e->flags &= ~EntityFlag_Static;
  653. }
  654. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  655. String context_name = str_lit("variable declaration");
  656. if (type_expr != nullptr) {
  657. e->type = check_type(ctx, type_expr);
  658. }
  659. if (e->type != nullptr) {
  660. if (is_type_polymorphic(base_type(e->type))) {
  661. gbString str = type_to_string(e->type);
  662. defer (gb_string_free(str));
  663. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  664. e->type = t_invalid;
  665. } else if (is_type_empty_union(e->type)) {
  666. gbString str = type_to_string(e->type);
  667. defer (gb_string_free(str));
  668. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  669. e->type = t_invalid;
  670. }
  671. }
  672. if (e->Variable.is_foreign) {
  673. if (init_expr != nullptr) {
  674. error(e->token, "A foreign variable declaration cannot have a default value");
  675. }
  676. init_entity_foreign_library(ctx, e);
  677. }
  678. if (ac.link_name.len > 0) {
  679. e->Variable.link_name = ac.link_name;
  680. }
  681. if (e->Variable.is_foreign || e->Variable.is_export) {
  682. String name = e->token.string;
  683. if (e->Variable.link_name.len > 0) {
  684. name = e->Variable.link_name;
  685. }
  686. auto *fp = &ctx->info->foreigns;
  687. HashKey key = hash_string(name);
  688. Entity **found = map_get(fp, key);
  689. if (found) {
  690. Entity *f = *found;
  691. TokenPos pos = f->token.pos;
  692. Type *this_type = base_type(e->type);
  693. Type *other_type = base_type(f->type);
  694. if (!are_types_identical(this_type, other_type)) {
  695. error(e->token,
  696. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  697. "\tat %.*s(%td:%td)",
  698. LIT(name), LIT(pos.file), pos.line, pos.column);
  699. }
  700. } else {
  701. map_set(fp, key, e);
  702. }
  703. }
  704. if (init_expr == nullptr) {
  705. if (type_expr == nullptr) {
  706. e->type = t_invalid;
  707. }
  708. return;
  709. }
  710. Operand o = {};
  711. check_expr(ctx, &o, init_expr);
  712. check_init_variable(ctx, e, &o, str_lit("variable declaration"));
  713. }
  714. void check_proc_group_decl(CheckerContext *ctx, Entity *pg_entity, DeclInfo *d) {
  715. GB_ASSERT(pg_entity->kind == Entity_ProcGroup);
  716. auto *pge = &pg_entity->ProcGroup;
  717. String proc_group_name = pg_entity->token.string;
  718. ast_node(pg, ProcGroup, d->init_expr);
  719. pge->entities = array_make<Entity*>(ctx->allocator, 0, pg->args.count);
  720. // NOTE(bill): This must be set here to prevent cycles in checking if someone
  721. // places the entity within itself
  722. pg_entity->type = t_invalid;
  723. PtrSet<Entity *> entity_set = {};
  724. ptr_set_init(&entity_set, heap_allocator(), 2*pg->args.count);
  725. for_array(i, pg->args) {
  726. Ast *arg = pg->args[i];
  727. Entity *e = nullptr;
  728. Operand o = {};
  729. if (arg->kind == Ast_Ident) {
  730. e = check_ident(ctx, &o, arg, nullptr, nullptr, true);
  731. } else if (arg->kind == Ast_SelectorExpr) {
  732. e = check_selector(ctx, &o, arg, nullptr);
  733. }
  734. if (e == nullptr) {
  735. error(arg, "Expected a valid entity name in procedure group, got %.*s", LIT(ast_strings[arg->kind]));
  736. continue;
  737. }
  738. if (e->kind == Entity_Variable) {
  739. if (!is_type_proc(e->type)) {
  740. gbString s = type_to_string(e->type);
  741. defer (gb_string_free(s));
  742. error(arg, "Expected a procedure, got %s", s);
  743. continue;
  744. }
  745. } else if (e->kind != Entity_Procedure) {
  746. error(arg, "Expected a procedure entity");
  747. continue;
  748. }
  749. if (ptr_set_exists(&entity_set, e)) {
  750. error(arg, "Previous use of `%.*s` in procedure group", LIT(e->token.string));
  751. continue;
  752. }
  753. ptr_set_add(&entity_set, e);
  754. array_add(&pge->entities, e);
  755. }
  756. ptr_set_destroy(&entity_set);
  757. for_array(j, pge->entities) {
  758. Entity *p = pge->entities[j];
  759. if (p->type == t_invalid) {
  760. // NOTE(bill): This invalid overload has already been handled
  761. continue;
  762. }
  763. String name = p->token.string;
  764. for (isize k = j+1; k < pge->entities.count; k++) {
  765. Entity *q = pge->entities[k];
  766. GB_ASSERT(p != q);
  767. bool is_invalid = false;
  768. TokenPos pos = q->token.pos;
  769. if (q->type == nullptr || q->type == t_invalid) {
  770. continue;
  771. }
  772. ProcTypeOverloadKind kind = are_proc_types_overload_safe(p->type, q->type);
  773. switch (kind) {
  774. case ProcOverload_Identical:
  775. error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in this scope", LIT(name));
  776. is_invalid = true;
  777. break;
  778. // case ProcOverload_CallingConvention:
  779. // error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in this scope", LIT(name));
  780. // is_invalid = true;
  781. // break;
  782. case ProcOverload_ParamVariadic:
  783. error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in this scope", LIT(name));
  784. is_invalid = true;
  785. break;
  786. case ProcOverload_ResultCount:
  787. case ProcOverload_ResultTypes:
  788. error(p->token, "Overloaded procedure '%.*s' as the same parameters but different results in this scope", LIT(name));
  789. is_invalid = true;
  790. break;
  791. case ProcOverload_Polymorphic:
  792. #if 0
  793. error(p->token, "Overloaded procedure '%.*s' has a polymorphic counterpart in this scope which is not allowed", LIT(name));
  794. is_invalid = true;
  795. #endif
  796. break;
  797. case ProcOverload_ParamCount:
  798. case ProcOverload_ParamTypes:
  799. // This is okay :)
  800. break;
  801. }
  802. if (is_invalid) {
  803. gb_printf_err("\tprevious procedure at %.*s(%td:%td)\n", LIT(pos.file), pos.line, pos.column);
  804. q->type = t_invalid;
  805. }
  806. }
  807. }
  808. }
  809. void check_entity_decl(CheckerContext *ctx, Entity *e, DeclInfo *d, Type *named_type) {
  810. if (e->state == EntityState_Resolved) {
  811. return;
  812. }
  813. String name = e->token.string;
  814. if (e->type != nullptr || e->state != EntityState_Unresolved) {
  815. error(e->token, "Illegal declaration cycle of `%.*s`", LIT(name));
  816. return;
  817. }
  818. GB_ASSERT(e->state == EntityState_Unresolved);
  819. #if 0
  820. char buf[256] = {};
  821. isize n = gb_snprintf(buf, 256, "%.*s %d", LIT(name), e->kind);
  822. Timings timings = {};
  823. timings_init(&timings, make_string(cast(u8 *)buf, n-1), 16);
  824. defer ({
  825. timings_print_all(&timings);
  826. timings_destroy(&timings);
  827. });
  828. #define TIME_SECTION(str) timings_start_section(&timings, str_lit(str))
  829. #else
  830. #define TIME_SECTION(str)
  831. #endif
  832. if (d == nullptr) {
  833. d = decl_info_of_entity(e);
  834. if (d == nullptr) {
  835. // TODO(bill): Err here?
  836. e->type = t_invalid;
  837. e->state = EntityState_Resolved;
  838. set_base_type(named_type, t_invalid);
  839. return;
  840. // GB_PANIC("'%.*s' should been declared!", LIT(name));
  841. }
  842. }
  843. CheckerContext c = *ctx;
  844. c.scope = d->scope;
  845. c.decl = d;
  846. c.type_level = 0;
  847. e->parent_proc_decl = c.curr_proc_decl;
  848. e->state = EntityState_InProgress;
  849. switch (e->kind) {
  850. case Entity_Variable:
  851. check_var_decl(&c, e, d->type_expr, d->init_expr);
  852. break;
  853. case Entity_Constant:
  854. check_const_decl(&c, e, d->type_expr, d->init_expr, named_type);
  855. break;
  856. case Entity_TypeName: {
  857. check_type_decl(&c, e, d->type_expr, named_type);
  858. break;
  859. }
  860. case Entity_Procedure:
  861. check_proc_decl(&c, e, d);
  862. break;
  863. case Entity_ProcGroup:
  864. check_proc_group_decl(&c, e, d);
  865. break;
  866. }
  867. e->state = EntityState_Resolved;
  868. #undef TIME_SECTION
  869. }
  870. void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *type, Ast *body) {
  871. if (body == nullptr) {
  872. return;
  873. }
  874. GB_ASSERT(body->kind == Ast_BlockStmt);
  875. String proc_name = {};
  876. if (token.kind == Token_Ident) {
  877. proc_name = token.string;
  878. } else {
  879. // TODO(bill): Better name
  880. proc_name = str_lit("(anonymous-procedure)");
  881. }
  882. CheckerContext new_ctx = *ctx_;
  883. CheckerContext *ctx = &new_ctx;
  884. ctx->scope = decl->scope;
  885. ctx->decl = decl;
  886. ctx->proc_name = proc_name;
  887. ctx->curr_proc_decl = decl;
  888. ctx->curr_proc_sig = type;
  889. GB_ASSERT(type->kind == Type_Proc);
  890. if (type->Proc.param_count > 0) {
  891. TypeTuple *params = &type->Proc.params->Tuple;
  892. for_array(i, params->variables) {
  893. Entity *e = params->variables[i];
  894. if (e->kind != Entity_Variable) {
  895. continue;
  896. }
  897. if (!(e->flags & EntityFlag_Using)) {
  898. continue;
  899. }
  900. bool is_immutable = e->Variable.is_immutable;
  901. bool is_value = (e->flags & EntityFlag_Value) != 0;
  902. String name = e->token.string;
  903. Type *t = base_type(type_deref(e->type));
  904. if (t->kind == Type_Struct) {
  905. Scope *scope = t->Struct.scope;
  906. if (scope == nullptr) {
  907. scope = scope_of_node(t->Struct.node);
  908. }
  909. GB_ASSERT(scope != nullptr);
  910. for_array(i, scope->elements.entries) {
  911. Entity *f = scope->elements.entries[i].value;
  912. if (f->kind == Entity_Variable) {
  913. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type);
  914. uvar->Variable.is_immutable = is_immutable;
  915. if (is_value) uvar->flags |= EntityFlag_Value;
  916. Entity *prev = scope_insert(ctx->scope, uvar);
  917. if (prev != nullptr) {
  918. error(e->token, "Namespace collision while 'using' '%.*s' of: %.*s", LIT(name), LIT(prev->token.string));
  919. break;
  920. }
  921. }
  922. }
  923. } else {
  924. error(e->token, "'using' can only be applied to variables of type struct");
  925. break;
  926. }
  927. }
  928. }
  929. ast_node(bs, BlockStmt, body);
  930. // check_open_scope(ctx, body);
  931. check_stmt_list(ctx, bs->stmts, Stmt_CheckScopeDecls);
  932. if (type->Proc.result_count > 0) {
  933. if (!check_is_terminating(body)) {
  934. if (token.kind == Token_Ident) {
  935. error(bs->close, "Missing return statement at the end of the procedure '%.*s'", LIT(token.string));
  936. } else {
  937. // NOTE(bill): Anonymous procedure (lambda)
  938. error(bs->close, "Missing return statement at the end of the procedure");
  939. }
  940. }
  941. }
  942. // check_close_scope(ctx);
  943. check_scope_usage(ctx->checker, ctx->scope);
  944. if (decl->parent != nullptr) {
  945. // NOTE(bill): Add the dependencies from the procedure literal (lambda)
  946. for_array(i, decl->deps.entries) {
  947. Entity *e = decl->deps.entries[i].ptr;
  948. ptr_set_add(&decl->parent->deps, e);
  949. }
  950. for_array(i, decl->type_info_deps.entries) {
  951. Type *t = decl->type_info_deps.entries[i].ptr;
  952. ptr_set_add(&decl->parent->type_info_deps, t);
  953. }
  954. }
  955. }