check_decl.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. void check_stmt (CheckerContext *ctx, Ast *node, u32 flags);
  2. // NOTE(bill): 'content_name' is for debugging and error messages
  3. Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *operand, String context_name) {
  4. if (operand->mode == Addressing_Invalid ||
  5. operand->type == t_invalid ||
  6. e->type == t_invalid) {
  7. if (operand->mode == Addressing_Builtin) {
  8. gbString expr_str = expr_to_string(operand->expr);
  9. // TODO(bill): is this a good enough error message?
  10. // TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
  11. error(operand->expr,
  12. "Cannot assign built-in procedure '%s' in %.*s",
  13. expr_str,
  14. LIT(context_name));
  15. operand->mode = Addressing_Invalid;
  16. gb_string_free(expr_str);
  17. }
  18. if (operand->mode == Addressing_ProcGroup) {
  19. if (e->type == nullptr) {
  20. error(operand->expr, "Cannot determine type from overloaded procedure '%.*s'", LIT(operand->proc_group->token.string));
  21. } else {
  22. check_assignment(ctx, operand, e->type, str_lit("variable assignment"));
  23. if (operand->mode != Addressing_Type) {
  24. return operand->type;
  25. }
  26. }
  27. }
  28. if (e->type == nullptr) {
  29. e->type = t_invalid;
  30. }
  31. return nullptr;
  32. }
  33. if (e->kind == Entity_Variable) {
  34. e->Variable.init_expr = operand->expr;
  35. }
  36. if (operand->mode == Addressing_Type) {
  37. if (e->type != nullptr && is_type_typeid(e->type)) {
  38. add_type_info_type(ctx, operand->type);
  39. add_type_and_value(ctx->info, operand->expr, Addressing_Value, e->type, exact_value_typeid(operand->type));
  40. return e->type;
  41. } else {
  42. gbString t = type_to_string(operand->type);
  43. defer (gb_string_free(t));
  44. error(operand->expr, "Cannot assign a type '%s' to variable '%.*s'", t, LIT(e->token.string));
  45. if (e->type == nullptr) {
  46. error_line("\tThe type of the variable '%.*s' cannot be inferred as a type does not have a default type\n", LIT(e->token.string));
  47. }
  48. e->type = operand->type;
  49. return nullptr;
  50. }
  51. }
  52. if (e->type == nullptr) {
  53. // NOTE(bill): Use the type of the operand
  54. Type *t = operand->type;
  55. if (is_type_untyped(t)) {
  56. if (t == t_invalid || is_type_untyped_nil(t)) {
  57. error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name));
  58. e->type = t_invalid;
  59. return nullptr;
  60. }
  61. if (t == t_invalid || is_type_untyped_undef(t)) {
  62. error(e->token, "Invalid use of --- in %.*s", LIT(context_name));
  63. e->type = t_invalid;
  64. return nullptr;
  65. }
  66. t = default_type(t);
  67. }
  68. if (is_type_asm_proc(t)) {
  69. error(e->token, "Invalid use of inline asm in %.*s", LIT(context_name));
  70. e->type = t_invalid;
  71. return nullptr;
  72. } else if (is_type_polymorphic(t)) {
  73. gbString str = type_to_string(t);
  74. defer (gb_string_free(str));
  75. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  76. e->type = t_invalid;
  77. return nullptr;
  78. } else if (is_type_empty_union(t)) {
  79. gbString str = type_to_string(t);
  80. defer (gb_string_free(str));
  81. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  82. e->type = t_invalid;
  83. return nullptr;
  84. }
  85. GB_ASSERT(is_type_typed(t));
  86. e->type = t;
  87. }
  88. e->parent_proc_decl = ctx->curr_proc_decl;
  89. check_assignment(ctx, operand, e->type, context_name);
  90. if (operand->mode == Addressing_Invalid) {
  91. return nullptr;
  92. }
  93. return e->type;
  94. }
  95. void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Slice<Ast *> const &inits, String context_name) {
  96. if ((lhs == nullptr || lhs_count == 0) && inits.count == 0) {
  97. return;
  98. }
  99. // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
  100. // an extra allocation
  101. auto operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
  102. check_unpack_arguments(ctx, lhs, lhs_count, &operands, inits, true, false);
  103. isize rhs_count = operands.count;
  104. for_array(i, operands) {
  105. if (operands[i].mode == Addressing_Invalid) {
  106. // TODO(bill): Should I ignore invalid parameters?
  107. // rhs_count--;
  108. }
  109. }
  110. isize max = gb_min(lhs_count, rhs_count);
  111. for (isize i = 0; i < max; i++) {
  112. Entity *e = lhs[i];
  113. DeclInfo *d = decl_info_of_entity(e);
  114. Operand *o = &operands[i];
  115. check_init_variable(ctx, e, o, context_name);
  116. if (d != nullptr) {
  117. d->init_expr = o->expr;
  118. }
  119. }
  120. if (rhs_count > 0 && lhs_count != rhs_count) {
  121. error(lhs[0]->token, "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
  122. }
  123. }
  124. void check_init_constant(CheckerContext *ctx, Entity *e, Operand *operand) {
  125. if (operand->mode == Addressing_Invalid ||
  126. operand->type == t_invalid ||
  127. e->type == t_invalid) {
  128. if (e->type == nullptr) {
  129. e->type = t_invalid;
  130. }
  131. return;
  132. }
  133. if (operand->mode != Addressing_Constant) {
  134. // TODO(bill): better error
  135. gbString str = expr_to_string(operand->expr);
  136. error(operand->expr, "'%s' is not a constant", str);
  137. gb_string_free(str);
  138. if (e->type == nullptr) {
  139. e->type = t_invalid;
  140. }
  141. return;
  142. }
  143. if (e->type == nullptr) { // NOTE(bill): type inference
  144. e->type = operand->type;
  145. }
  146. check_assignment(ctx, operand, e->type, str_lit("constant declaration"));
  147. if (operand->mode == Addressing_Invalid) {
  148. return;
  149. }
  150. e->parent_proc_decl = ctx->curr_proc_decl;
  151. e->Constant.value = operand->value;
  152. }
  153. bool is_type_distinct(Ast *node) {
  154. for (;;) {
  155. if (node == nullptr) {
  156. return false;
  157. }
  158. if (node->kind == Ast_ParenExpr) {
  159. node = node->ParenExpr.expr;
  160. } else if (node->kind == Ast_HelperType) {
  161. node = node->HelperType.type;
  162. } else {
  163. break;
  164. }
  165. }
  166. switch (node->kind) {
  167. case Ast_DistinctType:
  168. return true;
  169. case Ast_StructType:
  170. case Ast_UnionType:
  171. case Ast_EnumType:
  172. case Ast_ProcType:
  173. return true;
  174. case Ast_PointerType:
  175. case Ast_ArrayType:
  176. case Ast_DynamicArrayType:
  177. case Ast_MapType:
  178. return false;
  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 *init_expr, Type *def) {
  206. GB_ASSERT(e->type == nullptr);
  207. DeclInfo *decl = decl_info_of_entity(e);
  208. bool is_distinct = is_type_distinct(init_expr);
  209. Ast *te = remove_type_alias_clutter(init_expr);
  210. e->type = t_invalid;
  211. String name = e->token.string;
  212. Type *named = alloc_type_named(name, nullptr, e);
  213. if (def != nullptr && def->kind == Type_Named) {
  214. def->Named.base = named;
  215. }
  216. e->type = named;
  217. check_type_path_push(ctx, e);
  218. Type *bt = check_type_expr(ctx, te, named);
  219. check_type_path_pop(ctx);
  220. named->Named.base = base_type(bt);
  221. if (is_distinct && is_type_typeid(e->type)) {
  222. error(init_expr, "'distinct' cannot be applied to 'typeid'");
  223. is_distinct = false;
  224. }
  225. if (is_distinct && is_type_any(e->type)) {
  226. error(init_expr, "'distinct' cannot be applied to 'any'");
  227. is_distinct = false;
  228. }
  229. if (!is_distinct) {
  230. e->type = bt;
  231. named->Named.base = bt;
  232. e->TypeName.is_type_alias = true;
  233. }
  234. if (decl->type_expr != nullptr) {
  235. Type *t = check_type(ctx, decl->type_expr);
  236. if (t != nullptr && !is_type_typeid(t)) {
  237. Operand operand = {};
  238. operand.mode = Addressing_Type;
  239. operand.type = e->type;
  240. operand.expr = init_expr;
  241. check_assignment(ctx, &operand, t, str_lit("constant declaration"));
  242. }
  243. }
  244. if (decl != nullptr) {
  245. AttributeContext ac = {};
  246. check_decl_attributes(ctx, decl->attributes, type_decl_attribute, &ac);
  247. }
  248. // using decl
  249. if (decl->is_using) {
  250. warning(init_expr, "'using' an enum declaration is not allowed, prefer using implicit selector expressions e.g. '.A'");
  251. #if 0
  252. // NOTE(bill): Must be an enum declaration
  253. if (te->kind == Ast_EnumType) {
  254. Scope *parent = e->scope;
  255. if (parent->flags&ScopeFlag_File) {
  256. // NOTE(bill): Use package scope
  257. parent = parent->parent;
  258. }
  259. Type *t = base_type(e->type);
  260. if (t->kind == Type_Enum) {
  261. for_array(i, t->Enum.fields) {
  262. Entity *f = t->Enum.fields[i];
  263. if (f->kind != Entity_Constant) {
  264. continue;
  265. }
  266. String name = f->token.string;
  267. if (is_blank_ident(name)) {
  268. continue;
  269. }
  270. add_entity(ctx, parent, nullptr, f);
  271. }
  272. }
  273. }
  274. #endif
  275. }
  276. }
  277. void override_entity_in_scope(Entity *original_entity, Entity *new_entity) {
  278. // NOTE(bill): The original_entity's scope may not be same scope that it was inserted into
  279. // e.g. file entity inserted into its package scope
  280. String original_name = original_entity->token.string;
  281. Scope *found_scope = nullptr;
  282. Entity *found_entity = nullptr;
  283. scope_lookup_parent(original_entity->scope, original_name, &found_scope, &found_entity);
  284. if (found_scope == nullptr) {
  285. return;
  286. }
  287. // IMPORTANT NOTE(bill, 2021-04-10): Overriding behaviour was flawed in that the
  288. // original entity was still used check checked, but the checking was only
  289. // relying on "constant" data such as the Entity.type and Entity.Constant.value
  290. //
  291. // Therefore two things can be done: the type can be assigned to state that it
  292. // has been "evaluated" and the variant data can be copied across
  293. string_map_set(&found_scope->elements, original_name, new_entity);
  294. original_entity->flags |= EntityFlag_Overridden;
  295. original_entity->type = new_entity->type;
  296. original_entity->aliased_of = new_entity;
  297. if (original_entity->identifier == nullptr) {
  298. original_entity->identifier = new_entity->identifier;
  299. }
  300. if (original_entity->identifier != nullptr &&
  301. original_entity->identifier->kind == Ast_Ident) {
  302. original_entity->identifier->Ident.entity = new_entity;
  303. }
  304. // IMPORTANT NOTE(bill, 2021-04-10): copy only the variants
  305. // This is most likely NEVER required, but it does not at all hurt to keep
  306. isize offset = cast(u8 *)&original_entity->Dummy.start - cast(u8 *)original_entity;
  307. isize size = gb_size_of(*original_entity) - offset;
  308. gb_memmove(cast(u8 *)original_entity, cast(u8 *)new_entity, size);
  309. }
  310. void check_const_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init, Type *named_type) {
  311. GB_ASSERT(e->type == nullptr);
  312. GB_ASSERT(e->kind == Entity_Constant);
  313. init = unparen_expr(init);
  314. if (e->flags & EntityFlag_Visited) {
  315. e->type = t_invalid;
  316. return;
  317. }
  318. e->flags |= EntityFlag_Visited;
  319. if (type_expr) {
  320. e->type = check_type(ctx, type_expr);
  321. }
  322. Operand operand = {};
  323. Entity *other_entity = nullptr;
  324. if (init != nullptr) {
  325. Entity *entity = nullptr;
  326. if (init->kind == Ast_Ident) {
  327. entity = check_ident(ctx, &operand, init, nullptr, e->type, true);
  328. } else if (init->kind == Ast_SelectorExpr) {
  329. entity = check_selector(ctx, &operand, init, e->type);
  330. } else {
  331. check_expr_or_type(ctx, &operand, init, e->type);
  332. }
  333. switch (operand.mode) {
  334. case Addressing_Type: {
  335. if (e->type != nullptr && !is_type_typeid(e->type)) {
  336. check_assignment(ctx, &operand, e->type, str_lit("constant declaration"));
  337. }
  338. e->kind = Entity_TypeName;
  339. e->type = nullptr;
  340. if (entity != nullptr && entity->type != nullptr &&
  341. is_type_polymorphic_record_unspecialized(entity->type)) {
  342. DeclInfo *decl = decl_info_of_entity(e);
  343. if (decl != nullptr) {
  344. if (decl->attributes.count > 0) {
  345. error(decl->attributes[0], "Constant alias declarations cannot have attributes");
  346. }
  347. }
  348. override_entity_in_scope(e, entity);
  349. return;
  350. }
  351. check_type_decl(ctx, e, ctx->decl->init_expr, named_type);
  352. return;
  353. }
  354. // NOTE(bill): Check to see if the expression it to be aliases
  355. case Addressing_Builtin:
  356. if (e->type != nullptr) {
  357. error(type_expr, "A constant alias of a built-in procedure may not have a type initializer");
  358. }
  359. e->kind = Entity_Builtin;
  360. e->Builtin.id = operand.builtin_id;
  361. e->type = t_invalid;
  362. return;
  363. case Addressing_ProcGroup:
  364. GB_ASSERT(operand.proc_group != nullptr);
  365. GB_ASSERT(operand.proc_group->kind == Entity_ProcGroup);
  366. // NOTE(bill, 2020-06-10): It is better to just clone the contents than overriding the entity in the scope
  367. // Thank goodness I made entities a tagged union to allow for this implace patching
  368. e->kind = Entity_ProcGroup;
  369. e->ProcGroup.entities = array_clone(heap_allocator(), operand.proc_group->ProcGroup.entities);
  370. return;
  371. }
  372. if (entity != nullptr) {
  373. if (e->type != nullptr) {
  374. Operand x = {};
  375. x.type = entity->type;
  376. x.mode = Addressing_Variable;
  377. if (!check_is_assignable_to(ctx, &x, e->type)) {
  378. gbString expr_str = expr_to_string(init);
  379. gbString op_type_str = type_to_string(entity->type);
  380. gbString type_str = type_to_string(e->type);
  381. error(e->token,
  382. "Cannot assign '%s' of type '%s' to '%s'",
  383. expr_str,
  384. op_type_str,
  385. type_str);
  386. gb_string_free(type_str);
  387. gb_string_free(op_type_str);
  388. gb_string_free(expr_str);
  389. }
  390. }
  391. // NOTE(bill): Override aliased entity
  392. switch (entity->kind) {
  393. case Entity_ProcGroup:
  394. case Entity_Procedure:
  395. case Entity_LibraryName:
  396. case Entity_ImportName:
  397. {
  398. DeclInfo *decl = decl_info_of_entity(e);
  399. if (decl != nullptr) {
  400. if (decl->attributes.count > 0) {
  401. error(decl->attributes[0], "Constant alias declarations cannot have attributes");
  402. }
  403. }
  404. override_entity_in_scope(e, entity);
  405. return;
  406. }
  407. }
  408. }
  409. }
  410. check_init_constant(ctx, e, &operand);
  411. if (operand.mode == Addressing_Invalid ||
  412. base_type(operand.type) == t_invalid) {
  413. gbString str = expr_to_string(init);
  414. error(e->token, "Invalid declaration type '%s'", str);
  415. gb_string_free(str);
  416. }
  417. DeclInfo *decl = decl_info_of_entity(e);
  418. if (decl != nullptr) {
  419. check_decl_attributes(ctx, decl->attributes, const_decl_attribute, nullptr);
  420. }
  421. }
  422. typedef bool TypeCheckSig(Type *t);
  423. bool sig_compare(TypeCheckSig *a, Type *x, Type *y) {
  424. x = core_type(x);
  425. y = core_type(y);
  426. return (a(x) && a(y));
  427. }
  428. bool sig_compare(TypeCheckSig *a, TypeCheckSig *b, Type *x, Type *y) {
  429. x = core_type(x);
  430. y = core_type(y);
  431. if (a == b) {
  432. return sig_compare(a, x, y);
  433. }
  434. return ((a(x) && b(y)) || (b(x) && a(y)));
  435. }
  436. bool signature_parameter_similar_enough(Type *x, Type *y) {
  437. if (sig_compare(is_type_pointer, x, y)) {
  438. return true;
  439. }
  440. if (sig_compare(is_type_integer, x, y)) {
  441. GB_ASSERT(core_type(x)->kind == Type_Basic);
  442. GB_ASSERT(core_type(y)->kind == Type_Basic);
  443. i64 sx = type_size_of(x);
  444. i64 sy = type_size_of(y);
  445. if (sx == sy) return true;
  446. }
  447. if (sig_compare(is_type_integer, is_type_boolean, x, y)) {
  448. GB_ASSERT(core_type(x)->kind == Type_Basic);
  449. GB_ASSERT(core_type(y)->kind == Type_Basic);
  450. i64 sx = type_size_of(x);
  451. i64 sy = type_size_of(y);
  452. if (sx == sy) return true;
  453. }
  454. if (sig_compare(is_type_cstring, is_type_u8_ptr, x, y)) {
  455. return true;
  456. }
  457. if (sig_compare(is_type_uintptr, is_type_rawptr, x, y)) {
  458. return true;
  459. }
  460. if (sig_compare(is_type_proc, is_type_proc, x, y)) {
  461. return true;
  462. }
  463. if (sig_compare(is_type_proc, is_type_pointer, x, y)) {
  464. return true;
  465. }
  466. return are_types_identical(x, y);
  467. }
  468. bool are_signatures_similar_enough(Type *a_, Type *b_) {
  469. GB_ASSERT(a_->kind == Type_Proc);
  470. GB_ASSERT(b_->kind == Type_Proc);
  471. TypeProc *a = &a_->Proc;
  472. TypeProc *b = &b_->Proc;
  473. if (a->param_count != b->param_count) {
  474. return false;
  475. }
  476. if (a->result_count != b->result_count) {
  477. return false;
  478. }
  479. for (isize i = 0; i < a->param_count; i++) {
  480. Type *x = core_type(a->params->Tuple.variables[i]->type);
  481. Type *y = core_type(b->params->Tuple.variables[i]->type);
  482. if (!signature_parameter_similar_enough(x, y)) {
  483. return false;
  484. }
  485. }
  486. for (isize i = 0; i < a->result_count; i++) {
  487. Type *x = base_type(a->results->Tuple.variables[i]->type);
  488. Type *y = base_type(b->results->Tuple.variables[i]->type);
  489. if (!signature_parameter_similar_enough(x, y)) {
  490. return false;
  491. }
  492. }
  493. return true;
  494. }
  495. void init_entity_foreign_library(CheckerContext *ctx, Entity *e) {
  496. Ast *ident = nullptr;
  497. Entity **foreign_library = nullptr;
  498. switch (e->kind) {
  499. case Entity_Procedure:
  500. ident = e->Procedure.foreign_library_ident;
  501. foreign_library = &e->Procedure.foreign_library;
  502. break;
  503. case Entity_Variable:
  504. ident = e->Variable.foreign_library_ident;
  505. foreign_library = &e->Variable.foreign_library;
  506. break;
  507. default:
  508. return;
  509. }
  510. if (ident == nullptr) {
  511. error(e->token, "foreign entiies must declare which library they are from");
  512. } else if (ident->kind != Ast_Ident) {
  513. error(ident, "foreign library names must be an identifier");
  514. } else {
  515. String name = ident->Ident.token.string;
  516. Entity *found = scope_lookup(ctx->scope, name);
  517. if (found == nullptr) {
  518. if (is_blank_ident(name)) {
  519. // NOTE(bill): link against nothing
  520. } else {
  521. error(ident, "Undeclared name: %.*s", LIT(name));
  522. }
  523. } else if (found->kind != Entity_LibraryName) {
  524. error(ident, "'%.*s' cannot be used as a library name", LIT(name));
  525. } else {
  526. // TODO(bill): Extra stuff to do with library names?
  527. *foreign_library = found;
  528. found->flags |= EntityFlag_Used;
  529. add_entity_use(ctx, ident, found);
  530. }
  531. }
  532. }
  533. String handle_link_name(CheckerContext *ctx, Token token, String link_name, String link_prefix) {
  534. if (link_prefix.len > 0) {
  535. if (link_name.len > 0) {
  536. error(token, "'link_name' and 'link_prefix' cannot be used together");
  537. } else {
  538. isize len = link_prefix.len + token.string.len;
  539. u8 *name = gb_alloc_array(permanent_allocator(), u8, len+1);
  540. gb_memmove(name, &link_prefix[0], link_prefix.len);
  541. gb_memmove(name+link_prefix.len, &token.string[0], token.string.len);
  542. name[len] = 0;
  543. link_name = make_string(name, len);
  544. }
  545. }
  546. return link_name;
  547. }
  548. void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) {
  549. GB_ASSERT(e->type == nullptr);
  550. if (d->proc_lit->kind != Ast_ProcLit) {
  551. // TOOD(bill): Better error message
  552. error(d->proc_lit, "Expected a procedure to check");
  553. return;
  554. }
  555. Type *proc_type = e->type;
  556. if (d->gen_proc_type != nullptr) {
  557. proc_type = d->gen_proc_type;
  558. } else {
  559. proc_type = alloc_type_proc(e->scope, nullptr, 0, nullptr, 0, false, default_calling_convention());
  560. }
  561. e->type = proc_type;
  562. ast_node(pl, ProcLit, d->proc_lit);
  563. check_open_scope(ctx, pl->type);
  564. defer (check_close_scope(ctx));
  565. ctx->scope->procedure_entity = e;
  566. Type *decl_type = nullptr;
  567. if (d->type_expr != nullptr) {
  568. decl_type = check_type(ctx, d->type_expr);
  569. if (!is_type_proc(decl_type)) {
  570. gbString str = type_to_string(decl_type);
  571. error(d->type_expr, "Expected a procedure type, got '%s'", str);
  572. gb_string_free(str);
  573. }
  574. }
  575. auto tmp_ctx = *ctx;
  576. tmp_ctx.allow_polymorphic_types = true;
  577. if (decl_type != nullptr) {
  578. tmp_ctx.type_hint = decl_type;
  579. }
  580. check_procedure_type(&tmp_ctx, proc_type, pl->type);
  581. if (decl_type != nullptr) {
  582. Operand x = {};
  583. x.type = e->type;
  584. x.mode = Addressing_Variable;
  585. if (!check_is_assignable_to(ctx, &x, decl_type)) {
  586. gbString expr_str = expr_to_string(d->proc_lit);
  587. gbString op_type_str = type_to_string(e->type);
  588. gbString type_str = type_to_string(decl_type);
  589. error(e->token,
  590. "Cannot assign '%s' of type '%s' to '%s'",
  591. expr_str,
  592. op_type_str,
  593. type_str);
  594. gb_string_free(type_str);
  595. gb_string_free(op_type_str);
  596. gb_string_free(expr_str);
  597. }
  598. }
  599. TypeProc *pt = &proc_type->Proc;
  600. AttributeContext ac = make_attribute_context(e->Procedure.link_prefix);
  601. if (d != nullptr) {
  602. check_decl_attributes(ctx, d->attributes, proc_decl_attribute, &ac);
  603. }
  604. if (ac.test) {
  605. e->flags |= EntityFlag_Test;
  606. }
  607. if (ac.set_cold) {
  608. e->flags |= EntityFlag_Cold;
  609. }
  610. e->Procedure.optimization_mode = cast(ProcedureOptimizationMode)ac.optimization_mode;
  611. switch (e->Procedure.optimization_mode) {
  612. case ProcedureOptimizationMode_None:
  613. case ProcedureOptimizationMode_Minimal:
  614. if (pl->inlining == ProcInlining_inline) {
  615. error(e->token, "#force_inline cannot be used in conjunction with the attribute 'optimization_mode' with neither \"none\" nor \"minimal\"");
  616. }
  617. break;
  618. }
  619. e->Procedure.is_export = ac.is_export;
  620. e->deprecated_message = ac.deprecated_message;
  621. e->warning_message = ac.warning_message;
  622. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  623. if (ac.has_disabled_proc) {
  624. if (ac.disabled_proc) {
  625. e->flags |= EntityFlag_Disabled;
  626. }
  627. Type *t = base_type(e->type);
  628. GB_ASSERT(t->kind == Type_Proc);
  629. if (t->Proc.result_count != 0) {
  630. error(e->token, "Procedure with the 'disabled' attribute may not have any return values");
  631. }
  632. }
  633. bool is_foreign = e->Procedure.is_foreign;
  634. bool is_export = e->Procedure.is_export;
  635. if (e->pkg != nullptr && e->token.string == "main") {
  636. if (pt->param_count != 0 ||
  637. pt->result_count != 0) {
  638. gbString str = type_to_string(proc_type);
  639. error(e->token, "Procedure type of 'main' was expected to be 'proc()', got %s", str);
  640. gb_string_free(str);
  641. }
  642. if (pt->calling_convention != default_calling_convention()) {
  643. error(e->token, "Procedure 'main' cannot have a custom calling convention");
  644. }
  645. pt->calling_convention = default_calling_convention();
  646. if (e->pkg->kind == Package_Init) {
  647. if (ctx->info->entry_point != nullptr) {
  648. error(e->token, "Redeclaration of the entry pointer procedure 'main'");
  649. } else {
  650. ctx->info->entry_point = e;
  651. }
  652. }
  653. }
  654. if (is_foreign && is_export) {
  655. error(pl->type, "A foreign procedure cannot have an 'export' tag");
  656. }
  657. if (pt->is_polymorphic) {
  658. if (pl->body == nullptr) {
  659. error(e->token, "Polymorphic procedures must have a body");
  660. }
  661. if (is_foreign) {
  662. error(e->token, "A foreign procedure cannot be a polymorphic");
  663. return;
  664. }
  665. }
  666. if (pl->body != nullptr) {
  667. if (is_foreign) {
  668. error(pl->body, "A foreign procedure cannot have a body");
  669. }
  670. if (proc_type->Proc.c_vararg) {
  671. error(pl->body, "A procedure with a '#c_vararg' field cannot have a body and must be foreign");
  672. }
  673. d->scope = ctx->scope;
  674. GB_ASSERT(pl->body->kind == Ast_BlockStmt);
  675. if (!pt->is_polymorphic) {
  676. check_procedure_later(ctx, ctx->file, e->token, d, proc_type, pl->body, pl->tags);
  677. }
  678. } else if (!is_foreign) {
  679. if (e->Procedure.is_export) {
  680. error(e->token, "Foreign export procedures must have a body");
  681. } else {
  682. error(e->token, "Only a foreign procedure cannot have a body");
  683. }
  684. }
  685. if (pt->result_count == 0 && ac.require_results) {
  686. error(pl->type, "'require_results' is not needed on a procedure with no results");
  687. } else {
  688. pt->require_results = ac.require_results;
  689. }
  690. if (ac.link_name.len > 0) {
  691. e->Procedure.link_name = ac.link_name;
  692. }
  693. if (ac.deferred_procedure.entity != nullptr) {
  694. e->Procedure.deferred_procedure = ac.deferred_procedure;
  695. mpmc_enqueue(&ctx->checker->procs_with_deferred_to_check, e);
  696. }
  697. if (is_foreign) {
  698. String name = e->token.string;
  699. if (e->Procedure.link_name.len > 0) {
  700. name = e->Procedure.link_name;
  701. }
  702. e->Procedure.is_foreign = true;
  703. e->Procedure.link_name = name;
  704. init_entity_foreign_library(ctx, e);
  705. mutex_lock(&ctx->info->foreign_mutex);
  706. auto *fp = &ctx->info->foreigns;
  707. StringHashKey key = string_hash_string(name);
  708. Entity **found = string_map_get(fp, key);
  709. if (found) {
  710. Entity *f = *found;
  711. TokenPos pos = f->token.pos;
  712. Type *this_type = base_type(e->type);
  713. Type *other_type = base_type(f->type);
  714. if (is_type_proc(this_type) && is_type_proc(other_type)) {
  715. if (!are_signatures_similar_enough(this_type, other_type)) {
  716. error(d->proc_lit,
  717. "Redeclaration of foreign procedure '%.*s' with different type signatures\n"
  718. "\tat %s",
  719. LIT(name), token_pos_to_string(pos));
  720. }
  721. } else if (!are_types_identical(this_type, other_type)) {
  722. error(d->proc_lit,
  723. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  724. "\tat %s",
  725. LIT(name), token_pos_to_string(pos));
  726. }
  727. } else if (name == "main") {
  728. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  729. } else {
  730. string_map_set(fp, key, e);
  731. }
  732. mutex_unlock(&ctx->info->foreign_mutex);
  733. } else {
  734. String name = e->token.string;
  735. if (e->Procedure.link_name.len > 0) {
  736. name = e->Procedure.link_name;
  737. }
  738. if (e->Procedure.link_name.len > 0 || is_export) {
  739. mutex_lock(&ctx->info->foreign_mutex);
  740. auto *fp = &ctx->info->foreigns;
  741. StringHashKey key = string_hash_string(name);
  742. Entity **found = string_map_get(fp, key);
  743. if (found) {
  744. Entity *f = *found;
  745. TokenPos pos = f->token.pos;
  746. // TODO(bill): Better error message?
  747. error(d->proc_lit,
  748. "Non unique linking name for procedure '%.*s'\n"
  749. "\tother at %s",
  750. LIT(name), token_pos_to_string(pos));
  751. } else if (name == "main") {
  752. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  753. } else {
  754. string_map_set(fp, key, e);
  755. }
  756. mutex_unlock(&ctx->info->foreign_mutex);
  757. }
  758. }
  759. }
  760. void check_global_variable_decl(CheckerContext *ctx, Entity *&e, Ast *type_expr, Ast *init_expr) {
  761. GB_ASSERT(e->type == nullptr);
  762. GB_ASSERT(e->kind == Entity_Variable);
  763. if (e->flags & EntityFlag_Visited) {
  764. e->type = t_invalid;
  765. return;
  766. }
  767. e->flags |= EntityFlag_Visited;
  768. AttributeContext ac = make_attribute_context(e->Variable.link_prefix);
  769. ac.init_expr_list_count = init_expr != nullptr ? 1 : 0;
  770. DeclInfo *decl = decl_info_of_entity(e);
  771. GB_ASSERT(decl == ctx->decl);
  772. if (decl != nullptr) {
  773. check_decl_attributes(ctx, decl->attributes, var_decl_attribute, &ac);
  774. }
  775. if (ac.require_declaration) {
  776. mpmc_enqueue(&ctx->info->required_global_variable_queue, e);
  777. }
  778. e->Variable.thread_local_model = ac.thread_local_model;
  779. e->Variable.is_export = ac.is_export;
  780. e->flags &= ~EntityFlag_Static;
  781. if (ac.is_static) {
  782. error(e->token, "@(static) is not supported for global variables, nor required");
  783. }
  784. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  785. String context_name = str_lit("variable declaration");
  786. if (type_expr != nullptr) {
  787. e->type = check_type(ctx, type_expr);
  788. }
  789. if (e->type != nullptr) {
  790. if (is_type_polymorphic(base_type(e->type))) {
  791. gbString str = type_to_string(e->type);
  792. defer (gb_string_free(str));
  793. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  794. e->type = t_invalid;
  795. } else if (is_type_empty_union(e->type)) {
  796. gbString str = type_to_string(e->type);
  797. defer (gb_string_free(str));
  798. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  799. e->type = t_invalid;
  800. }
  801. }
  802. if (e->Variable.is_foreign) {
  803. if (init_expr != nullptr) {
  804. error(e->token, "A foreign variable declaration cannot have a default value");
  805. }
  806. init_entity_foreign_library(ctx, e);
  807. }
  808. if (ac.link_name.len > 0) {
  809. e->Variable.link_name = ac.link_name;
  810. }
  811. if (ac.link_section.len > 0) {
  812. e->Variable.link_section = ac.link_section;
  813. }
  814. if (e->Variable.is_foreign || e->Variable.is_export) {
  815. String name = e->token.string;
  816. if (e->Variable.link_name.len > 0) {
  817. name = e->Variable.link_name;
  818. }
  819. auto *fp = &ctx->info->foreigns;
  820. StringHashKey key = string_hash_string(name);
  821. Entity **found = string_map_get(fp, key);
  822. if (found) {
  823. Entity *f = *found;
  824. TokenPos pos = f->token.pos;
  825. Type *this_type = base_type(e->type);
  826. Type *other_type = base_type(f->type);
  827. if (!are_types_identical(this_type, other_type)) {
  828. error(e->token,
  829. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  830. "\tat %s",
  831. LIT(name), token_pos_to_string(pos));
  832. }
  833. } else {
  834. string_map_set(fp, key, e);
  835. }
  836. }
  837. if (init_expr == nullptr) {
  838. if (type_expr == nullptr) {
  839. e->type = t_invalid;
  840. }
  841. return;
  842. }
  843. Operand o = {};
  844. check_expr_with_type_hint(ctx, &o, init_expr, e->type);
  845. check_init_variable(ctx, e, &o, str_lit("variable declaration"));
  846. }
  847. void check_proc_group_decl(CheckerContext *ctx, Entity *&pg_entity, DeclInfo *d) {
  848. GB_ASSERT(pg_entity->kind == Entity_ProcGroup);
  849. auto *pge = &pg_entity->ProcGroup;
  850. String proc_group_name = pg_entity->token.string;
  851. ast_node(pg, ProcGroup, d->init_expr);
  852. pge->entities = array_make<Entity*>(permanent_allocator(), 0, pg->args.count);
  853. // NOTE(bill): This must be set here to prevent cycles in checking if someone
  854. // places the entity within itself
  855. pg_entity->type = t_invalid;
  856. PtrSet<Entity *> entity_set = {};
  857. ptr_set_init(&entity_set, heap_allocator(), 2*pg->args.count);
  858. for_array(i, pg->args) {
  859. Ast *arg = pg->args[i];
  860. Entity *e = nullptr;
  861. Operand o = {};
  862. if (arg->kind == Ast_Ident) {
  863. e = check_ident(ctx, &o, arg, nullptr, nullptr, true);
  864. } else if (arg->kind == Ast_SelectorExpr) {
  865. e = check_selector(ctx, &o, arg, nullptr);
  866. }
  867. if (e == nullptr) {
  868. error(arg, "Expected a valid entity name in procedure group, got %.*s", LIT(ast_strings[arg->kind]));
  869. continue;
  870. }
  871. if (e->kind == Entity_Variable) {
  872. if (!is_type_proc(e->type)) {
  873. gbString s = type_to_string(e->type);
  874. defer (gb_string_free(s));
  875. error(arg, "Expected a procedure, got %s", s);
  876. continue;
  877. }
  878. } else if (e->kind != Entity_Procedure) {
  879. error(arg, "Expected a procedure entity");
  880. continue;
  881. }
  882. if (ptr_set_update(&entity_set, e)) {
  883. error(arg, "Previous use of `%.*s` in procedure group", LIT(e->token.string));
  884. continue;
  885. }
  886. array_add(&pge->entities, e);
  887. }
  888. ptr_set_destroy(&entity_set);
  889. for_array(j, pge->entities) {
  890. Entity *p = pge->entities[j];
  891. if (p->type == t_invalid) {
  892. // NOTE(bill): This invalid overload has already been handled
  893. continue;
  894. }
  895. String name = p->token.string;
  896. for (isize k = j+1; k < pge->entities.count; k++) {
  897. Entity *q = pge->entities[k];
  898. GB_ASSERT(p != q);
  899. bool is_invalid = false;
  900. TokenPos pos = q->token.pos;
  901. if (q->type == nullptr || q->type == t_invalid) {
  902. continue;
  903. }
  904. begin_error_block();
  905. defer (end_error_block());
  906. ProcTypeOverloadKind kind = are_proc_types_overload_safe(p->type, q->type);
  907. bool both_have_where_clauses = false;
  908. if (p->decl_info->proc_lit != nullptr && q->decl_info->proc_lit != nullptr) {
  909. GB_ASSERT(p->decl_info->proc_lit->kind == Ast_ProcLit);
  910. GB_ASSERT(q->decl_info->proc_lit->kind == Ast_ProcLit);
  911. auto pl = &p->decl_info->proc_lit->ProcLit;
  912. auto ql = &q->decl_info->proc_lit->ProcLit;
  913. // Allow collisions if the procedures both have 'where' clauses and are both polymorphic
  914. bool pw = pl->where_token.kind != Token_Invalid && is_type_polymorphic(p->type, true);
  915. bool qw = ql->where_token.kind != Token_Invalid && is_type_polymorphic(q->type, true);
  916. both_have_where_clauses = pw && qw;
  917. }
  918. if (!both_have_where_clauses) switch (kind) {
  919. case ProcOverload_Identical:
  920. error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  921. is_invalid = true;
  922. break;
  923. // case ProcOverload_CallingConvention:
  924. // error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  925. // is_invalid = true;
  926. // break;
  927. case ProcOverload_ParamVariadic:
  928. error(p->token, "Overloaded procedure '%.*s' as the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  929. is_invalid = true;
  930. break;
  931. case ProcOverload_ResultCount:
  932. case ProcOverload_ResultTypes:
  933. error(p->token, "Overloaded procedure '%.*s' as the same parameters but different results in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  934. is_invalid = true;
  935. break;
  936. case ProcOverload_Polymorphic:
  937. #if 0
  938. error(p->token, "Overloaded procedure '%.*s' has a polymorphic counterpart in the procedure group '%.*s' which is not allowed", LIT(name), LIT(proc_group_name));
  939. is_invalid = true;
  940. #endif
  941. break;
  942. case ProcOverload_ParamCount:
  943. case ProcOverload_ParamTypes:
  944. // This is okay :)
  945. break;
  946. }
  947. if (is_invalid) {
  948. error_line("\tprevious procedure at %s\n", token_pos_to_string(pos));
  949. q->type = t_invalid;
  950. }
  951. }
  952. }
  953. }
  954. void check_entity_decl(CheckerContext *ctx, Entity *e, DeclInfo *d, Type *named_type) {
  955. if (e->state == EntityState_Resolved) {
  956. return;
  957. }
  958. if (e->flags & EntityFlag_Lazy) {
  959. mutex_lock(&ctx->info->lazy_mutex);
  960. }
  961. String name = e->token.string;
  962. if (e->type != nullptr || e->state != EntityState_Unresolved) {
  963. error(e->token, "Illegal declaration cycle of `%.*s`", LIT(name));
  964. } else {
  965. GB_ASSERT(e->state == EntityState_Unresolved);
  966. if (d == nullptr) {
  967. d = decl_info_of_entity(e);
  968. if (d == nullptr) {
  969. // TODO(bill): Err here?
  970. e->type = t_invalid;
  971. e->state = EntityState_Resolved;
  972. set_base_type(named_type, t_invalid);
  973. goto end;
  974. }
  975. }
  976. CheckerContext c = *ctx;
  977. c.scope = d->scope;
  978. c.decl = d;
  979. c.type_level = 0;
  980. e->parent_proc_decl = c.curr_proc_decl;
  981. e->state = EntityState_InProgress;
  982. switch (e->kind) {
  983. case Entity_Variable:
  984. check_global_variable_decl(&c, e, d->type_expr, d->init_expr);
  985. break;
  986. case Entity_Constant:
  987. check_const_decl(&c, e, d->type_expr, d->init_expr, named_type);
  988. break;
  989. case Entity_TypeName: {
  990. check_type_decl(&c, e, d->init_expr, named_type);
  991. break;
  992. }
  993. case Entity_Procedure:
  994. check_proc_decl(&c, e, d);
  995. break;
  996. case Entity_ProcGroup:
  997. check_proc_group_decl(&c, e, d);
  998. break;
  999. }
  1000. e->state = EntityState_Resolved;
  1001. }
  1002. end:;
  1003. // NOTE(bill): Add it to the list of checked entities
  1004. if (e->flags & EntityFlag_Lazy) {
  1005. array_add(&ctx->info->entities, e);
  1006. mutex_unlock(&ctx->info->lazy_mutex);
  1007. }
  1008. }
  1009. struct ProcUsingVar {
  1010. Entity *e;
  1011. Entity *uvar;
  1012. };
  1013. void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *type, Ast *body) {
  1014. if (body == nullptr) {
  1015. return;
  1016. }
  1017. GB_ASSERT(body->kind == Ast_BlockStmt);
  1018. String proc_name = {};
  1019. if (token.kind == Token_Ident) {
  1020. proc_name = token.string;
  1021. } else {
  1022. // TODO(bill): Better name
  1023. proc_name = str_lit("(anonymous-procedure)");
  1024. }
  1025. CheckerContext new_ctx = *ctx_;
  1026. CheckerContext *ctx = &new_ctx;
  1027. GB_ASSERT(type->kind == Type_Proc);
  1028. ctx->scope = decl->scope;
  1029. ctx->decl = decl;
  1030. ctx->proc_name = proc_name;
  1031. ctx->curr_proc_decl = decl;
  1032. ctx->curr_proc_sig = type;
  1033. ctx->curr_proc_calling_convention = type->Proc.calling_convention;
  1034. if (ctx->pkg->name != "runtime") {
  1035. switch (type->Proc.calling_convention) {
  1036. case ProcCC_None:
  1037. error(body, "Procedures with the calling convention \"none\" are not allowed a body");
  1038. break;
  1039. }
  1040. }
  1041. ast_node(bs, BlockStmt, body);
  1042. Array<ProcUsingVar> using_entities = {};
  1043. using_entities.allocator = heap_allocator();
  1044. defer (array_free(&using_entities));
  1045. {
  1046. if (type->Proc.param_count > 0) {
  1047. TypeTuple *params = &type->Proc.params->Tuple;
  1048. for_array(i, params->variables) {
  1049. Entity *e = params->variables[i];
  1050. if (e->kind != Entity_Variable) {
  1051. continue;
  1052. }
  1053. if (!(e->flags & EntityFlag_Using)) {
  1054. continue;
  1055. }
  1056. bool is_value = (e->flags & EntityFlag_Value) != 0 && !is_type_pointer(e->type);
  1057. String name = e->token.string;
  1058. Type *t = base_type(type_deref(e->type));
  1059. if (t->kind == Type_Struct) {
  1060. Scope *scope = t->Struct.scope;
  1061. if (scope == nullptr) {
  1062. scope = scope_of_node(t->Struct.node);
  1063. }
  1064. GB_ASSERT(scope != nullptr);
  1065. for_array(i, scope->elements.entries) {
  1066. Entity *f = scope->elements.entries[i].value;
  1067. if (f->kind == Entity_Variable) {
  1068. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, nullptr);
  1069. if (is_value) uvar->flags |= EntityFlag_Value;
  1070. ProcUsingVar puv = {e, uvar};
  1071. array_add(&using_entities, puv);
  1072. }
  1073. }
  1074. } else {
  1075. error(e->token, "'using' can only be applied to variables of type struct");
  1076. break;
  1077. }
  1078. }
  1079. }
  1080. }
  1081. for_array(i, using_entities) {
  1082. Entity *e = using_entities[i].e;
  1083. Entity *uvar = using_entities[i].uvar;
  1084. Entity *prev = scope_insert(ctx->scope, uvar);
  1085. if (prev != nullptr) {
  1086. error(e->token, "Namespace collision while 'using' procedure argument '%.*s' of: %.*s", LIT(e->token.string), LIT(prev->token.string));
  1087. error_line("%.*s != %.*s\n", LIT(uvar->token.string), LIT(prev->token.string));
  1088. break;
  1089. }
  1090. }
  1091. bool where_clause_ok = evaluate_where_clauses(ctx, nullptr, decl->scope, &decl->proc_lit->ProcLit.where_clauses, !decl->where_clauses_evaluated);
  1092. if (!where_clause_ok) {
  1093. // NOTE(bill, 2019-08-31): Don't check the body as the where clauses failed
  1094. return;
  1095. }
  1096. check_open_scope(ctx, body);
  1097. {
  1098. for_array(i, using_entities) {
  1099. Entity *e = using_entities[i].e;
  1100. Entity *uvar = using_entities[i].uvar;
  1101. Entity *prev = scope_insert(ctx->scope, uvar);
  1102. // NOTE(bill): Don't err here
  1103. }
  1104. check_stmt_list(ctx, bs->stmts, Stmt_CheckScopeDecls);
  1105. if (type->Proc.result_count > 0) {
  1106. if (!check_is_terminating(body, str_lit(""))) {
  1107. if (token.kind == Token_Ident) {
  1108. error(bs->close, "Missing return statement at the end of the procedure '%.*s'", LIT(token.string));
  1109. } else {
  1110. // NOTE(bill): Anonymous procedure (lambda)
  1111. error(bs->close, "Missing return statement at the end of the procedure");
  1112. }
  1113. }
  1114. } else if (type->Proc.diverging) {
  1115. if (!check_is_terminating(body, str_lit(""))) {
  1116. if (token.kind == Token_Ident) {
  1117. error(bs->close, "Missing diverging call at the end of the procedure '%.*s'", LIT(token.string));
  1118. } else {
  1119. // NOTE(bill): Anonymous procedure (lambda)
  1120. error(bs->close, "Missing diverging call at the end of the procedure");
  1121. }
  1122. }
  1123. }
  1124. }
  1125. check_close_scope(ctx);
  1126. check_scope_usage(ctx->checker, ctx->scope);
  1127. if (decl->parent != nullptr) {
  1128. Scope *ps = decl->parent->scope;
  1129. if (ps->flags & (ScopeFlag_File & ScopeFlag_Pkg & ScopeFlag_Global)) {
  1130. return;
  1131. } else {
  1132. mutex_lock(&ctx->info->deps_mutex);
  1133. // NOTE(bill): Add the dependencies from the procedure literal (lambda)
  1134. // But only at the procedure level
  1135. for_array(i, decl->deps.entries) {
  1136. Entity *e = decl->deps.entries[i].ptr;
  1137. ptr_set_add(&decl->parent->deps, e);
  1138. }
  1139. for_array(i, decl->type_info_deps.entries) {
  1140. Type *t = decl->type_info_deps.entries[i].ptr;
  1141. ptr_set_add(&decl->parent->type_info_deps, t);
  1142. }
  1143. mutex_unlock(&ctx->info->deps_mutex);
  1144. }
  1145. }
  1146. }