check_decl.cpp 34 KB

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