check_decl.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. gb_internal void check_stmt(CheckerContext *ctx, Ast *node, u32 flags);
  2. // NOTE(bill): 'content_name' is for debugging and error messages
  3. gb_internal 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. error(operand->expr,
  11. "Cannot assign built-in procedure '%s' in %.*s",
  12. expr_str,
  13. LIT(context_name));
  14. operand->mode = Addressing_Invalid;
  15. gb_string_free(expr_str);
  16. }
  17. if (operand->mode == Addressing_ProcGroup) {
  18. if (e->type == nullptr) {
  19. error(operand->expr, "Cannot determine type from overloaded procedure '%.*s'", LIT(operand->proc_group->token.string));
  20. } else {
  21. check_assignment(ctx, operand, e->type, str_lit("variable assignment"));
  22. if (operand->mode != Addressing_Type) {
  23. return operand->type;
  24. }
  25. }
  26. }
  27. if (e->type == nullptr) {
  28. e->type = t_invalid;
  29. }
  30. return nullptr;
  31. }
  32. if (e->kind == Entity_Variable) {
  33. e->Variable.init_expr = operand->expr;
  34. }
  35. if (operand->mode == Addressing_Type) {
  36. if (e->type != nullptr && is_type_typeid(e->type) && !is_type_polymorphic(operand->type)) {
  37. add_type_info_type(ctx, operand->type);
  38. add_type_and_value(ctx, operand->expr, Addressing_Value, e->type, exact_value_typeid(operand->type));
  39. return e->type;
  40. } else {
  41. ERROR_BLOCK();
  42. gbString t = type_to_string(operand->type);
  43. defer (gb_string_free(t));
  44. if (is_type_polymorphic(operand->type)) {
  45. error(operand->expr, "Cannot assign a non-specialized polymorphic type '%s' to variable '%.*s'", t, LIT(e->token.string));
  46. } else {
  47. error(operand->expr, "Cannot assign a type '%s' to variable '%.*s'", t, LIT(e->token.string));
  48. }
  49. if (e->type == nullptr) {
  50. 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));
  51. }
  52. e->type = operand->type;
  53. return nullptr;
  54. }
  55. }
  56. if (e->type == nullptr) {
  57. // NOTE(bill): Use the type of the operand
  58. Type *t = operand->type;
  59. if (is_type_untyped(t)) {
  60. if (is_type_untyped_uninit(t)) {
  61. error(e->token, "Invalid use of --- in %.*s", LIT(context_name));
  62. e->type = t_invalid;
  63. return nullptr;
  64. } else if (t == t_invalid || is_type_untyped_nil(t)) {
  65. error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name));
  66. e->type = t_invalid;
  67. return nullptr;
  68. }
  69. t = default_type(t);
  70. }
  71. if (is_type_asm_proc(t)) {
  72. error(e->token, "Invalid use of inline asm in %.*s", LIT(context_name));
  73. e->type = t_invalid;
  74. return nullptr;
  75. } else if (is_type_polymorphic(t)) {
  76. gbString str = type_to_string(t);
  77. defer (gb_string_free(str));
  78. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  79. e->type = t_invalid;
  80. return nullptr;
  81. } else if (is_type_empty_union(t)) {
  82. gbString str = type_to_string(t);
  83. defer (gb_string_free(str));
  84. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  85. e->type = t_invalid;
  86. return nullptr;
  87. }
  88. GB_ASSERT(is_type_typed(t));
  89. e->type = t;
  90. }
  91. e->parent_proc_decl = ctx->curr_proc_decl;
  92. check_assignment(ctx, operand, e->type, context_name);
  93. if (operand->mode == Addressing_Invalid) {
  94. return nullptr;
  95. }
  96. return e->type;
  97. }
  98. gb_internal void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Slice<Ast *> const &inits, String context_name) {
  99. if ((lhs == nullptr || lhs_count == 0) && inits.count == 0) {
  100. return;
  101. }
  102. // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
  103. // an extra allocation
  104. TEMPORARY_ALLOCATOR_GUARD();
  105. auto operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
  106. check_unpack_arguments(ctx, lhs, lhs_count, &operands, inits, UnpackFlag_AllowOk|UnpackFlag_AllowUndef);
  107. isize rhs_count = operands.count;
  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. if (o->type && is_type_no_copy(o->type)) {
  118. begin_error_block();
  119. if (check_no_copy_assignment(*o, str_lit("initialization"))) {
  120. error_line("\tInitialization of a #no_copy type must be either implicitly zero, a constant literal, or a return value from a call expression");
  121. }
  122. end_error_block();
  123. }
  124. }
  125. if (rhs_count > 0 && lhs_count != rhs_count) {
  126. error(lhs[0]->token, "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
  127. }
  128. }
  129. gb_internal void check_init_constant(CheckerContext *ctx, Entity *e, Operand *operand) {
  130. if (operand->mode == Addressing_Invalid ||
  131. operand->type == t_invalid ||
  132. e->type == t_invalid) {
  133. if (e->type == nullptr) {
  134. e->type = t_invalid;
  135. }
  136. return;
  137. }
  138. if (operand->mode != Addressing_Constant) {
  139. // TODO(bill): better error
  140. gbString str = expr_to_string(operand->expr);
  141. error(operand->expr, "'%s' is not a constant", str);
  142. gb_string_free(str);
  143. if (e->type == nullptr) {
  144. e->type = t_invalid;
  145. }
  146. return;
  147. }
  148. if (e->type == nullptr) { // NOTE(bill): type inference
  149. e->type = operand->type;
  150. }
  151. check_assignment(ctx, operand, e->type, str_lit("constant declaration"));
  152. if (operand->mode == Addressing_Invalid) {
  153. return;
  154. }
  155. if (is_type_proc(e->type)) {
  156. error(e->token, "Illegal declaration of a constant procedure value");
  157. }
  158. e->parent_proc_decl = ctx->curr_proc_decl;
  159. e->Constant.value = operand->value;
  160. }
  161. gb_internal bool is_type_distinct(Ast *node) {
  162. for (;;) {
  163. if (node == nullptr) {
  164. return false;
  165. }
  166. if (node->kind == Ast_ParenExpr) {
  167. node = node->ParenExpr.expr;
  168. } else if (node->kind == Ast_HelperType) {
  169. node = node->HelperType.type;
  170. } else {
  171. break;
  172. }
  173. }
  174. switch (node->kind) {
  175. case Ast_DistinctType:
  176. return true;
  177. case Ast_StructType:
  178. case Ast_UnionType:
  179. case Ast_EnumType:
  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. }
  188. return false;
  189. }
  190. gb_internal Ast *remove_type_alias_clutter(Ast *node) {
  191. for (;;) {
  192. if (node == nullptr) {
  193. return nullptr;
  194. }
  195. if (node->kind == Ast_ParenExpr) {
  196. node = node->ParenExpr.expr;
  197. } else if (node->kind == Ast_DistinctType) {
  198. node = node->DistinctType.type;
  199. } else {
  200. return node;
  201. }
  202. }
  203. }
  204. gb_internal Type *clone_enum_type(CheckerContext *ctx, Type *original_enum_type, Type *named_type) {
  205. // NOTE(bill, 2022-02-05): Stupid edge case for `distinct` declarations
  206. //
  207. // X :: enum {A, B, C}
  208. // Y :: distinct X
  209. //
  210. // To make Y be just like X, it will need to copy the elements of X and change their type
  211. // so that they match Y rather than X.
  212. GB_ASSERT(original_enum_type != nullptr);
  213. GB_ASSERT(named_type != nullptr);
  214. GB_ASSERT(original_enum_type->kind == Type_Enum);
  215. GB_ASSERT(named_type->kind == Type_Named);
  216. Scope *parent = original_enum_type->Enum.scope->parent;
  217. Scope *scope = create_scope(nullptr, parent);
  218. Type *et = alloc_type_enum();
  219. et->Enum.base_type = original_enum_type->Enum.base_type;
  220. et->Enum.min_value = original_enum_type->Enum.min_value;
  221. et->Enum.max_value = original_enum_type->Enum.max_value;
  222. et->Enum.min_value_index = original_enum_type->Enum.min_value_index;
  223. et->Enum.max_value_index = original_enum_type->Enum.max_value_index;
  224. et->Enum.scope = scope;
  225. auto fields = array_make<Entity *>(permanent_allocator(), original_enum_type->Enum.fields.count);
  226. for_array(i, fields) {
  227. Entity *old = original_enum_type->Enum.fields[i];
  228. Entity *e = alloc_entity_constant(scope, old->token, named_type, old->Constant.value);
  229. e->file = old->file;
  230. e->identifier = clone_ast(old->identifier);
  231. e->flags |= EntityFlag_Visited;
  232. e->state = EntityState_Resolved;
  233. e->Constant.flags = old->Constant.flags;
  234. e->Constant.docs = old->Constant.docs;
  235. e->Constant.comment = old->Constant.comment;
  236. fields[i] = e;
  237. add_entity(ctx, scope, nullptr, e);
  238. add_entity_use(ctx, e->identifier, e);
  239. }
  240. et->Enum.fields = fields;
  241. return et;
  242. }
  243. gb_internal void check_type_decl(CheckerContext *ctx, Entity *e, Ast *init_expr, Type *def) {
  244. GB_ASSERT(e->type == nullptr);
  245. DeclInfo *decl = decl_info_of_entity(e);
  246. bool is_distinct = is_type_distinct(init_expr);
  247. Ast *te = remove_type_alias_clutter(init_expr);
  248. e->type = t_invalid;
  249. String name = e->token.string;
  250. Type *named = alloc_type_named(name, nullptr, e);
  251. if (def != nullptr && def->kind == Type_Named) {
  252. def->Named.base = named;
  253. }
  254. e->type = named;
  255. check_type_path_push(ctx, e);
  256. Type *bt = check_type_expr(ctx, te, named);
  257. check_type_path_pop(ctx);
  258. Type *base = base_type(bt);
  259. if (is_distinct && bt->kind == Type_Named && base->kind == Type_Enum) {
  260. base = clone_enum_type(ctx, base, named);
  261. }
  262. named->Named.base = base;
  263. if (is_distinct) {
  264. if (is_type_typeid(e->type)) {
  265. error(init_expr, "'distinct' cannot be applied to 'typeid'");
  266. is_distinct = false;
  267. } else if (is_type_any(e->type)) {
  268. error(init_expr, "'distinct' cannot be applied to 'any'");
  269. is_distinct = false;
  270. } else if (is_type_simd_vector(e->type) || is_type_soa_pointer(e->type)) {
  271. gbString str = type_to_string(e->type);
  272. error(init_expr, "'distinct' cannot be applied to '%s'", str);
  273. gb_string_free(str);
  274. is_distinct = false;
  275. }
  276. }
  277. if (!is_distinct) {
  278. e->type = bt;
  279. named->Named.base = bt;
  280. e->TypeName.is_type_alias = true;
  281. }
  282. if (decl->type_expr != nullptr) {
  283. Type *t = check_type(ctx, decl->type_expr);
  284. if (t != nullptr && !is_type_typeid(t)) {
  285. Operand operand = {};
  286. operand.mode = Addressing_Type;
  287. operand.type = e->type;
  288. operand.expr = init_expr;
  289. check_assignment(ctx, &operand, t, str_lit("constant declaration"));
  290. }
  291. }
  292. if (decl != nullptr) {
  293. AttributeContext ac = {};
  294. check_decl_attributes(ctx, decl->attributes, type_decl_attribute, &ac);
  295. if (e->kind == Entity_TypeName && ac.objc_class != "") {
  296. e->TypeName.objc_class_name = ac.objc_class;
  297. if (type_size_of(e->type) > 0) {
  298. error(e->token, "@(objc_class) marked type must be of zero size");
  299. }
  300. }
  301. }
  302. // using decl
  303. if (decl->is_using) {
  304. warning(init_expr, "'using' an enum declaration is not allowed, prefer using implicit selector expressions e.g. '.A'");
  305. #if 1
  306. // NOTE(bill): Must be an enum declaration
  307. if (te->kind == Ast_EnumType) {
  308. Scope *parent = e->scope;
  309. if (parent->flags&ScopeFlag_File) {
  310. // NOTE(bill): Use package scope
  311. parent = parent->parent;
  312. }
  313. Type *t = base_type(e->type);
  314. if (t->kind == Type_Enum) {
  315. for (Entity *f : t->Enum.fields) {
  316. if (f->kind != Entity_Constant) {
  317. continue;
  318. }
  319. String name = f->token.string;
  320. if (is_blank_ident(name)) {
  321. continue;
  322. }
  323. add_entity(ctx, parent, nullptr, f);
  324. }
  325. }
  326. }
  327. #endif
  328. }
  329. }
  330. gb_internal void override_entity_in_scope(Entity *original_entity, Entity *new_entity) {
  331. // NOTE(bill): The original_entity's scope may not be same scope that it was inserted into
  332. // e.g. file entity inserted into its package scope
  333. String original_name = original_entity->token.string;
  334. Scope *found_scope = nullptr;
  335. Entity *found_entity = nullptr;
  336. scope_lookup_parent(original_entity->scope, original_name, &found_scope, &found_entity);
  337. if (found_scope == nullptr) {
  338. return;
  339. }
  340. rw_mutex_lock(&found_scope->mutex);
  341. defer (rw_mutex_unlock(&found_scope->mutex));
  342. // IMPORTANT NOTE(bill, 2021-04-10): Overriding behaviour was flawed in that the
  343. // original entity was still used check checked, but the checking was only
  344. // relying on "constant" data such as the Entity.type and Entity.Constant.value
  345. //
  346. // Therefore two things can be done: the type can be assigned to state that it
  347. // has been "evaluated" and the variant data can be copied across
  348. string_map_set(&found_scope->elements, original_name, new_entity);
  349. original_entity->flags |= EntityFlag_Overridden;
  350. original_entity->type = new_entity->type;
  351. original_entity->aliased_of = new_entity;
  352. Ast *empty_ident = nullptr;
  353. original_entity->identifier.compare_exchange_strong(empty_ident, new_entity->identifier);
  354. if (original_entity->identifier.load() != nullptr &&
  355. original_entity->identifier.load()->kind == Ast_Ident) {
  356. original_entity->identifier.load()->Ident.entity = new_entity;
  357. }
  358. // IMPORTANT NOTE(bill, 2021-04-10): copy only the variants
  359. // This is most likely NEVER required, but it does not at all hurt to keep
  360. isize offset = cast(u8 *)&original_entity->Dummy.start - cast(u8 *)original_entity;
  361. isize size = gb_size_of(*original_entity) - offset;
  362. gb_memmove(cast(u8 *)original_entity, cast(u8 *)new_entity, size);
  363. }
  364. gb_internal void check_const_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init, Type *named_type) {
  365. GB_ASSERT(e->type == nullptr);
  366. GB_ASSERT(e->kind == Entity_Constant);
  367. init = unparen_expr(init);
  368. if (e->flags & EntityFlag_Visited) {
  369. e->type = t_invalid;
  370. return;
  371. }
  372. e->flags |= EntityFlag_Visited;
  373. if (type_expr) {
  374. e->type = check_type(ctx, type_expr);
  375. if (are_types_identical(e->type, t_typeid)) {
  376. e->type = nullptr;
  377. e->kind = Entity_TypeName;
  378. check_type_decl(ctx, e, init, named_type);
  379. return;
  380. }
  381. }
  382. Operand operand = {};
  383. if (init != nullptr) {
  384. Entity *entity = check_entity_from_ident_or_selector(ctx, init, false);
  385. if (entity != nullptr && entity->kind == Entity_TypeName) {
  386. // @TypeAliasingProblem
  387. // NOTE(bill, 2022-02-03): This is used to solve the problem caused by type aliases
  388. // being "confused" as constants
  389. //
  390. // A :: B
  391. // C :: proc "c" (^A)
  392. // B :: struct {x: C}
  393. //
  394. // A gets evaluated first, and then checks B.
  395. // B then checks C.
  396. // C then tries to check A which is unresolved but thought to be a constant.
  397. // Therefore within C's check, A errs as "not a type".
  398. //
  399. // This is because a const declaration may or may not be a type and this cannot
  400. // be determined from a syntactical standpoint.
  401. // This check allows the compiler to override the entity to be checked as a type.
  402. //
  403. // There is no problem if B is prefixed with the `#type` helper enforcing at
  404. // both a syntax and semantic level that B must be a type.
  405. //
  406. // A :: #type B
  407. //
  408. // This approach is not fool proof and can fail in case such as:
  409. //
  410. // X :: type_of(x)
  411. // X :: Foo(int).Type
  412. //
  413. // Since even these kind of declarations may cause weird checking cycles.
  414. // For the time being, these are going to be treated as an unfortunate error
  415. // until there is a proper delaying system to try declaration again if they
  416. // have failed.
  417. e->kind = Entity_TypeName;
  418. check_type_decl(ctx, e, init, named_type);
  419. return;
  420. }
  421. entity = nullptr;
  422. if (init->kind == Ast_Ident) {
  423. entity = check_ident(ctx, &operand, init, nullptr, e->type, true);
  424. } else if (init->kind == Ast_SelectorExpr) {
  425. entity = check_selector(ctx, &operand, init, e->type);
  426. } else {
  427. check_expr_or_type(ctx, &operand, init, e->type);
  428. }
  429. switch (operand.mode) {
  430. case Addressing_Type: {
  431. if (e->type != nullptr && !is_type_typeid(e->type)) {
  432. check_assignment(ctx, &operand, e->type, str_lit("constant declaration"));
  433. }
  434. e->kind = Entity_TypeName;
  435. e->type = nullptr;
  436. if (entity != nullptr && entity->type != nullptr &&
  437. is_type_polymorphic_record_unspecialized(entity->type)) {
  438. DeclInfo *decl = decl_info_of_entity(e);
  439. if (decl != nullptr) {
  440. if (decl->attributes.count > 0) {
  441. error(decl->attributes[0], "Constant alias declarations cannot have attributes");
  442. }
  443. }
  444. override_entity_in_scope(e, entity);
  445. return;
  446. }
  447. check_type_decl(ctx, e, ctx->decl->init_expr, named_type);
  448. return;
  449. }
  450. // NOTE(bill): Check to see if the expression it to be aliases
  451. case Addressing_Builtin:
  452. if (e->type != nullptr) {
  453. error(type_expr, "A constant alias of a built-in procedure may not have a type initializer");
  454. }
  455. e->kind = Entity_Builtin;
  456. e->Builtin.id = operand.builtin_id;
  457. e->type = t_invalid;
  458. return;
  459. case Addressing_ProcGroup:
  460. GB_ASSERT(operand.proc_group != nullptr);
  461. GB_ASSERT(operand.proc_group->kind == Entity_ProcGroup);
  462. // NOTE(bill, 2020-06-10): It is better to just clone the contents than overriding the entity in the scope
  463. // Thank goodness I made entities a tagged union to allow for this implace patching
  464. e->kind = Entity_ProcGroup;
  465. e->ProcGroup.entities = array_clone(heap_allocator(), operand.proc_group->ProcGroup.entities);
  466. return;
  467. }
  468. if (entity != nullptr) {
  469. if (e->type != nullptr) {
  470. Operand x = {};
  471. x.type = entity->type;
  472. x.mode = Addressing_Variable;
  473. if (!check_is_assignable_to(ctx, &x, e->type)) {
  474. gbString expr_str = expr_to_string(init);
  475. gbString op_type_str = type_to_string(entity->type);
  476. gbString type_str = type_to_string(e->type);
  477. error(e->token,
  478. "Cannot assign '%s' of type '%s' to '%s'",
  479. expr_str,
  480. op_type_str,
  481. type_str);
  482. gb_string_free(type_str);
  483. gb_string_free(op_type_str);
  484. gb_string_free(expr_str);
  485. }
  486. }
  487. // NOTE(bill): Override aliased entity
  488. switch (entity->kind) {
  489. case Entity_ProcGroup:
  490. case Entity_Procedure:
  491. case Entity_LibraryName:
  492. case Entity_ImportName:
  493. {
  494. DeclInfo *decl = decl_info_of_entity(e);
  495. if (decl != nullptr) {
  496. if (decl->attributes.count > 0) {
  497. error(decl->attributes[0], "Constant alias declarations cannot have attributes");
  498. }
  499. }
  500. override_entity_in_scope(e, entity);
  501. return;
  502. }
  503. }
  504. }
  505. }
  506. check_init_constant(ctx, e, &operand);
  507. if (operand.mode == Addressing_Invalid ||
  508. base_type(operand.type) == t_invalid) {
  509. gbString str = expr_to_string(init);
  510. error(init, "Invalid declaration value '%s'", str);
  511. gb_string_free(str);
  512. }
  513. DeclInfo *decl = decl_info_of_entity(e);
  514. if (decl != nullptr) {
  515. check_decl_attributes(ctx, decl->attributes, const_decl_attribute, nullptr);
  516. }
  517. }
  518. typedef bool TypeCheckSig(Type *t);
  519. gb_internal bool sig_compare(TypeCheckSig *a, Type *x, Type *y) {
  520. x = core_type(x);
  521. y = core_type(y);
  522. return (a(x) && a(y));
  523. }
  524. gb_internal bool sig_compare(TypeCheckSig *a, TypeCheckSig *b, Type *x, Type *y) {
  525. x = core_type(x);
  526. y = core_type(y);
  527. if (a == b) {
  528. return sig_compare(a, x, y);
  529. }
  530. return ((a(x) && b(y)) || (b(x) && a(y)));
  531. }
  532. gb_internal bool signature_parameter_similar_enough(Type *x, Type *y) {
  533. if (sig_compare(is_type_pointer, x, y)) {
  534. return true;
  535. }
  536. if (sig_compare(is_type_multi_pointer, x, y)) {
  537. return true;
  538. }
  539. if (sig_compare(is_type_proc, x, y)) {
  540. return true;
  541. }
  542. if (sig_compare(is_type_integer, x, y)) {
  543. GB_ASSERT(core_type(x)->kind == Type_Basic);
  544. GB_ASSERT(core_type(y)->kind == Type_Basic);
  545. i64 sx = type_size_of(x);
  546. i64 sy = type_size_of(y);
  547. if (sx == sy) return true;
  548. }
  549. if (sig_compare(is_type_integer, is_type_boolean, x, y)) {
  550. GB_ASSERT(core_type(x)->kind == Type_Basic);
  551. GB_ASSERT(core_type(y)->kind == Type_Basic);
  552. i64 sx = type_size_of(x);
  553. i64 sy = type_size_of(y);
  554. if (sx == sy) return true;
  555. }
  556. if (sig_compare(is_type_cstring, is_type_u8_ptr, x, y)) {
  557. return true;
  558. }
  559. if (sig_compare(is_type_cstring, is_type_u8_multi_ptr, x, y)) {
  560. return true;
  561. }
  562. if (sig_compare(is_type_uintptr, is_type_rawptr, x, y)) {
  563. return true;
  564. }
  565. if (sig_compare(is_type_proc, is_type_pointer, x, y)) {
  566. return true;
  567. }
  568. if (sig_compare(is_type_pointer, is_type_multi_pointer, x, y)) {
  569. return true;
  570. }
  571. if (sig_compare(is_type_proc, is_type_multi_pointer, x, y)) {
  572. return true;
  573. }
  574. return are_types_identical(x, y);
  575. }
  576. gb_internal bool are_signatures_similar_enough(Type *a_, Type *b_) {
  577. GB_ASSERT(a_->kind == Type_Proc);
  578. GB_ASSERT(b_->kind == Type_Proc);
  579. TypeProc *a = &a_->Proc;
  580. TypeProc *b = &b_->Proc;
  581. if (a->param_count != b->param_count) {
  582. return false;
  583. }
  584. if (a->result_count != b->result_count) {
  585. return false;
  586. }
  587. for (isize i = 0; i < a->param_count; i++) {
  588. Type *x = core_type(a->params->Tuple.variables[i]->type);
  589. Type *y = core_type(b->params->Tuple.variables[i]->type);
  590. if (!signature_parameter_similar_enough(x, y)) {
  591. return false;
  592. }
  593. }
  594. for (isize i = 0; i < a->result_count; i++) {
  595. Type *x = base_type(a->results->Tuple.variables[i]->type);
  596. Type *y = base_type(b->results->Tuple.variables[i]->type);
  597. if (!signature_parameter_similar_enough(x, y)) {
  598. return false;
  599. }
  600. }
  601. return true;
  602. }
  603. gb_internal Entity *init_entity_foreign_library(CheckerContext *ctx, Entity *e) {
  604. Ast *ident = nullptr;
  605. Entity **foreign_library = nullptr;
  606. switch (e->kind) {
  607. case Entity_Procedure:
  608. ident = e->Procedure.foreign_library_ident;
  609. foreign_library = &e->Procedure.foreign_library;
  610. break;
  611. case Entity_Variable:
  612. ident = e->Variable.foreign_library_ident;
  613. foreign_library = &e->Variable.foreign_library;
  614. break;
  615. default:
  616. return nullptr;
  617. }
  618. if (ident == nullptr) {
  619. error(e->token, "foreign entiies must declare which library they are from");
  620. } else if (ident->kind != Ast_Ident) {
  621. error(ident, "foreign library names must be an identifier");
  622. } else {
  623. String name = ident->Ident.token.string;
  624. Entity *found = scope_lookup(ctx->scope, name);
  625. if (found == nullptr) {
  626. if (is_blank_ident(name)) {
  627. // NOTE(bill): link against nothing
  628. } else {
  629. error(ident, "Undeclared name: %.*s", LIT(name));
  630. }
  631. } else if (found->kind != Entity_LibraryName) {
  632. error(ident, "'%.*s' cannot be used as a library name", LIT(name));
  633. } else {
  634. // TODO(bill): Extra stuff to do with library names?
  635. *foreign_library = found;
  636. found->flags |= EntityFlag_Used;
  637. add_entity_use(ctx, ident, found);
  638. return found;
  639. }
  640. }
  641. return nullptr;
  642. }
  643. gb_internal String handle_link_name(CheckerContext *ctx, Token token, String link_name, String link_prefix) {
  644. if (link_prefix.len > 0) {
  645. if (link_name.len > 0) {
  646. error(token, "'link_name' and 'link_prefix' cannot be used together");
  647. } else {
  648. isize len = link_prefix.len + token.string.len;
  649. u8 *name = gb_alloc_array(permanent_allocator(), u8, len+1);
  650. gb_memmove(name, &link_prefix[0], link_prefix.len);
  651. gb_memmove(name+link_prefix.len, &token.string[0], token.string.len);
  652. name[len] = 0;
  653. link_name = make_string(name, len);
  654. }
  655. }
  656. return link_name;
  657. }
  658. gb_internal void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) {
  659. GB_ASSERT(e->type == nullptr);
  660. if (d->proc_lit->kind != Ast_ProcLit) {
  661. // TOOD(bill): Better error message
  662. error(d->proc_lit, "Expected a procedure to check");
  663. return;
  664. }
  665. Type *proc_type = e->type;
  666. if (d->gen_proc_type != nullptr) {
  667. proc_type = d->gen_proc_type;
  668. } else {
  669. proc_type = alloc_type_proc(e->scope, nullptr, 0, nullptr, 0, false, default_calling_convention());
  670. }
  671. e->type = proc_type;
  672. ast_node(pl, ProcLit, d->proc_lit);
  673. check_open_scope(ctx, pl->type);
  674. defer (check_close_scope(ctx));
  675. ctx->scope->procedure_entity = e;
  676. Type *decl_type = nullptr;
  677. if (d->type_expr != nullptr) {
  678. decl_type = check_type(ctx, d->type_expr);
  679. if (!is_type_proc(decl_type)) {
  680. gbString str = type_to_string(decl_type);
  681. error(d->type_expr, "Expected a procedure type, got '%s'", str);
  682. gb_string_free(str);
  683. }
  684. }
  685. auto tmp_ctx = *ctx;
  686. tmp_ctx.allow_polymorphic_types = true;
  687. if (decl_type != nullptr) {
  688. tmp_ctx.type_hint = decl_type;
  689. }
  690. check_procedure_type(&tmp_ctx, proc_type, pl->type);
  691. if (decl_type != nullptr) {
  692. Operand x = {};
  693. x.type = e->type;
  694. x.mode = Addressing_Variable;
  695. if (!check_is_assignable_to(ctx, &x, decl_type)) {
  696. gbString expr_str = expr_to_string(d->proc_lit);
  697. gbString op_type_str = type_to_string(e->type);
  698. gbString type_str = type_to_string(decl_type);
  699. error(e->token,
  700. "Cannot assign '%s' of type '%s' to '%s'",
  701. expr_str,
  702. op_type_str,
  703. type_str);
  704. gb_string_free(type_str);
  705. gb_string_free(op_type_str);
  706. gb_string_free(expr_str);
  707. }
  708. }
  709. TypeProc *pt = &proc_type->Proc;
  710. AttributeContext ac = make_attribute_context(e->Procedure.link_prefix);
  711. if (d != nullptr) {
  712. check_decl_attributes(ctx, d->attributes, proc_decl_attribute, &ac);
  713. }
  714. if (ac.test) {
  715. e->flags |= EntityFlag_Test;
  716. }
  717. if (ac.init && ac.fini) {
  718. error(e->token, "A procedure cannot be both declared as @(init) and @(fini)");
  719. } else if (ac.init) {
  720. e->flags |= EntityFlag_Init;
  721. } else if (ac.fini) {
  722. e->flags |= EntityFlag_Fini;
  723. }
  724. if (ac.set_cold) {
  725. e->flags |= EntityFlag_Cold;
  726. }
  727. e->Procedure.optimization_mode = cast(ProcedureOptimizationMode)ac.optimization_mode;
  728. if (ac.objc_name.len || ac.objc_is_class_method || ac.objc_type) {
  729. if (ac.objc_name.len == 0 && ac.objc_is_class_method) {
  730. error(e->token, "@(objc_name) is required with @(objc_is_class_method)");
  731. } else if (ac.objc_type == nullptr) {
  732. error(e->token, "@(objc_name) requires that @(objc_type) to be set");
  733. } else if (ac.objc_name.len == 0 && ac.objc_type) {
  734. error(e->token, "@(objc_name) is required with @(objc_type)");
  735. } else {
  736. Type *t = ac.objc_type;
  737. if (t->kind == Type_Named) {
  738. Entity *tn = t->Named.type_name;
  739. GB_ASSERT(tn->kind == Entity_TypeName);
  740. if (tn->scope != e->scope) {
  741. error(e->token, "@(objc_name) attribute may only be applied to procedures and types within the same scope");
  742. } else {
  743. mutex_lock(&global_type_name_objc_metadata_mutex);
  744. defer (mutex_unlock(&global_type_name_objc_metadata_mutex));
  745. if (!tn->TypeName.objc_metadata) {
  746. tn->TypeName.objc_metadata = create_type_name_obj_c_metadata();
  747. }
  748. auto *md = tn->TypeName.objc_metadata;
  749. mutex_lock(md->mutex);
  750. defer (mutex_unlock(md->mutex));
  751. if (!ac.objc_is_class_method) {
  752. bool ok = true;
  753. for (TypeNameObjCMetadataEntry const &entry : md->value_entries) {
  754. if (entry.name == ac.objc_name) {
  755. error(e->token, "Previous declaration of @(objc_name=\"%.*s\")", LIT(ac.objc_name));
  756. ok = false;
  757. break;
  758. }
  759. }
  760. if (ok) {
  761. array_add(&md->value_entries, TypeNameObjCMetadataEntry{ac.objc_name, e});
  762. }
  763. } else {
  764. bool ok = true;
  765. for (TypeNameObjCMetadataEntry const &entry : md->type_entries) {
  766. if (entry.name == ac.objc_name) {
  767. error(e->token, "Previous declaration of @(objc_name=\"%.*s\")", LIT(ac.objc_name));
  768. ok = false;
  769. break;
  770. }
  771. }
  772. if (ok) {
  773. array_add(&md->type_entries, TypeNameObjCMetadataEntry{ac.objc_name, e});
  774. }
  775. }
  776. }
  777. }
  778. }
  779. }
  780. if (ac.require_target_feature.len != 0 && ac.enable_target_feature.len != 0) {
  781. error(e->token, "Attributes @(require_target_feature=...) and @(enable_target_feature=...) cannot be used together");
  782. } else if (ac.require_target_feature.len != 0) {
  783. if (check_target_feature_is_enabled(e->token.pos, ac.require_target_feature)) {
  784. e->Procedure.target_feature = ac.require_target_feature;
  785. } else {
  786. e->Procedure.target_feature_disabled = true;
  787. }
  788. } else if (ac.enable_target_feature.len != 0) {
  789. enable_target_feature(e->token.pos, ac.enable_target_feature);
  790. e->Procedure.target_feature = ac.enable_target_feature;
  791. }
  792. switch (e->Procedure.optimization_mode) {
  793. case ProcedureOptimizationMode_None:
  794. case ProcedureOptimizationMode_Minimal:
  795. if (pl->inlining == ProcInlining_inline) {
  796. error(e->token, "#force_inline cannot be used in conjunction with the attribute 'optimization_mode' with neither \"none\" nor \"minimal\"");
  797. }
  798. break;
  799. }
  800. e->Procedure.is_export = ac.is_export;
  801. e->deprecated_message = ac.deprecated_message;
  802. e->warning_message = ac.warning_message;
  803. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  804. if (ac.has_disabled_proc) {
  805. if (ac.disabled_proc) {
  806. e->flags |= EntityFlag_Disabled;
  807. }
  808. Type *t = base_type(e->type);
  809. GB_ASSERT(t->kind == Type_Proc);
  810. if (t->Proc.result_count != 0) {
  811. error(e->token, "Procedure with the 'disabled' attribute may not have any return values");
  812. }
  813. }
  814. bool is_foreign = e->Procedure.is_foreign;
  815. bool is_export = e->Procedure.is_export;
  816. if (ac.linkage.len != 0) {
  817. if (ac.linkage == "internal") { e->flags |= EntityFlag_CustomLinkage_Internal; }
  818. else if (ac.linkage == "strong") { e->flags |= EntityFlag_CustomLinkage_Strong; }
  819. else if (ac.linkage == "weak") { e->flags |= EntityFlag_CustomLinkage_Weak; }
  820. else if (ac.linkage == "link_once") { e->flags |= EntityFlag_CustomLinkage_LinkOnce; }
  821. if (is_foreign && (e->flags & EntityFlag_CustomLinkage_Internal)) {
  822. error(e->token, "A foreign procedure may not have an \"internal\" linkage");
  823. }
  824. }
  825. if (ac.require_declaration) {
  826. e->flags |= EntityFlag_Require;
  827. }
  828. if (e->pkg != nullptr && e->token.string == "main") {
  829. if (e->pkg->kind != Package_Runtime) {
  830. if (pt->param_count != 0 ||
  831. pt->result_count != 0) {
  832. gbString str = type_to_string(proc_type);
  833. error(e->token, "Procedure type of 'main' was expected to be 'proc()', got %s", str);
  834. gb_string_free(str);
  835. }
  836. if (pt->calling_convention != default_calling_convention()) {
  837. error(e->token, "Procedure 'main' cannot have a custom calling convention");
  838. }
  839. pt->calling_convention = default_calling_convention();
  840. if (e->pkg->kind == Package_Init) {
  841. if (ctx->info->entry_point != nullptr) {
  842. error(e->token, "Redeclaration of the entry pointer procedure 'main'");
  843. } else {
  844. ctx->info->entry_point = e;
  845. }
  846. }
  847. }
  848. }
  849. if (is_foreign && is_export) {
  850. error(pl->type, "A foreign procedure cannot have an 'export' tag");
  851. }
  852. if (pt->is_polymorphic) {
  853. if (pl->body == nullptr) {
  854. error(e->token, "Polymorphic procedures must have a body");
  855. }
  856. if (is_foreign) {
  857. error(e->token, "A foreign procedure cannot be a polymorphic");
  858. return;
  859. }
  860. }
  861. if (pl->body != nullptr) {
  862. if (is_foreign) {
  863. error(pl->body, "A foreign procedure cannot have a body");
  864. }
  865. if (proc_type->Proc.c_vararg) {
  866. error(pl->body, "A procedure with a '#c_vararg' field cannot have a body and must be foreign");
  867. }
  868. d->scope = ctx->scope;
  869. GB_ASSERT(pl->body->kind == Ast_BlockStmt);
  870. if (!pt->is_polymorphic) {
  871. check_procedure_later(ctx->checker, ctx->file, e->token, d, proc_type, pl->body, pl->tags);
  872. }
  873. } else if (!is_foreign) {
  874. if (e->Procedure.is_export) {
  875. error(e->token, "Foreign export procedures must have a body");
  876. } else {
  877. error(e->token, "Only a foreign procedure cannot have a body");
  878. }
  879. }
  880. if (ac.require_results) {
  881. if (pt->result_count == 0) {
  882. error(pl->type, "'require_results' is not needed on a procedure with no results");
  883. } else {
  884. pt->require_results = true;
  885. }
  886. }
  887. if (ac.link_name.len > 0) {
  888. e->Procedure.link_name = ac.link_name;
  889. }
  890. if (ac.deferred_procedure.entity != nullptr) {
  891. e->Procedure.deferred_procedure = ac.deferred_procedure;
  892. mpsc_enqueue(&ctx->checker->procs_with_deferred_to_check, e);
  893. }
  894. if (is_foreign) {
  895. String name = e->token.string;
  896. if (e->Procedure.link_name.len > 0) {
  897. name = e->Procedure.link_name;
  898. }
  899. Entity *foreign_library = init_entity_foreign_library(ctx, e);
  900. if (is_arch_wasm() && foreign_library != nullptr) {
  901. String module_name = str_lit("env");
  902. GB_ASSERT (foreign_library->kind == Entity_LibraryName);
  903. if (foreign_library->LibraryName.paths.count != 1) {
  904. error(foreign_library->token, "'foreign import' for '%.*s' architecture may only have one path, got %td",
  905. LIT(target_arch_names[build_context.metrics.arch]), foreign_library->LibraryName.paths.count);
  906. }
  907. if (foreign_library->LibraryName.paths.count >= 1) {
  908. module_name = foreign_library->LibraryName.paths[0];
  909. }
  910. name = concatenate3_strings(permanent_allocator(), module_name, WASM_MODULE_NAME_SEPARATOR, name);
  911. }
  912. e->Procedure.is_foreign = true;
  913. e->Procedure.link_name = name;
  914. mutex_lock(&ctx->info->foreign_mutex);
  915. auto *fp = &ctx->info->foreigns;
  916. StringHashKey key = string_hash_string(name);
  917. Entity **found = string_map_get(fp, key);
  918. if (found) {
  919. Entity *f = *found;
  920. TokenPos pos = f->token.pos;
  921. Type *this_type = base_type(e->type);
  922. Type *other_type = base_type(f->type);
  923. if (is_type_proc(this_type) && is_type_proc(other_type)) {
  924. if (!are_signatures_similar_enough(this_type, other_type)) {
  925. error(d->proc_lit,
  926. "Redeclaration of foreign procedure '%.*s' with different type signatures\n"
  927. "\tat %s",
  928. LIT(name), token_pos_to_string(pos));
  929. }
  930. } else if (!are_types_identical(this_type, other_type)) {
  931. error(d->proc_lit,
  932. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  933. "\tat %s",
  934. LIT(name), token_pos_to_string(pos));
  935. }
  936. } else if (name == "main") {
  937. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  938. } else {
  939. string_map_set(fp, key, e);
  940. }
  941. mutex_unlock(&ctx->info->foreign_mutex);
  942. } else {
  943. String name = e->token.string;
  944. if (e->Procedure.link_name.len > 0) {
  945. name = e->Procedure.link_name;
  946. }
  947. if (e->Procedure.link_name.len > 0 || is_export) {
  948. mutex_lock(&ctx->info->foreign_mutex);
  949. auto *fp = &ctx->info->foreigns;
  950. StringHashKey key = string_hash_string(name);
  951. Entity **found = string_map_get(fp, key);
  952. if (found) {
  953. Entity *f = *found;
  954. TokenPos pos = f->token.pos;
  955. // TODO(bill): Better error message?
  956. error(d->proc_lit,
  957. "Non unique linking name for procedure '%.*s'\n"
  958. "\tother at %s",
  959. LIT(name), token_pos_to_string(pos));
  960. } else if (name == "main") {
  961. if (d->entity->pkg->kind != Package_Runtime) {
  962. error(d->proc_lit, "The link name 'main' is reserved for internal use");
  963. }
  964. } else {
  965. string_map_set(fp, key, e);
  966. }
  967. mutex_unlock(&ctx->info->foreign_mutex);
  968. }
  969. }
  970. if (e->Procedure.link_name.len > 0 ) {
  971. e->flags |= EntityFlag_CustomLinkName;
  972. }
  973. }
  974. gb_internal void check_global_variable_decl(CheckerContext *ctx, Entity *&e, Ast *type_expr, Ast *init_expr) {
  975. GB_ASSERT(e->type == nullptr);
  976. GB_ASSERT(e->kind == Entity_Variable);
  977. if (e->flags & EntityFlag_Visited) {
  978. e->type = t_invalid;
  979. return;
  980. }
  981. e->flags |= EntityFlag_Visited;
  982. AttributeContext ac = make_attribute_context(e->Variable.link_prefix);
  983. ac.init_expr_list_count = init_expr != nullptr ? 1 : 0;
  984. DeclInfo *decl = decl_info_of_entity(e);
  985. GB_ASSERT(decl == ctx->decl);
  986. if (decl != nullptr) {
  987. check_decl_attributes(ctx, decl->attributes, var_decl_attribute, &ac);
  988. }
  989. if (ac.require_declaration) {
  990. e->flags |= EntityFlag_Require;
  991. mpsc_enqueue(&ctx->info->required_global_variable_queue, e);
  992. }
  993. e->Variable.thread_local_model = ac.thread_local_model;
  994. e->Variable.is_export = ac.is_export;
  995. e->flags &= ~EntityFlag_Static;
  996. if (ac.is_static) {
  997. error(e->token, "@(static) is not supported for global variables, nor required");
  998. }
  999. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  1000. if (is_arch_wasm() && e->Variable.thread_local_model.len != 0) {
  1001. e->Variable.thread_local_model.len = 0;
  1002. // NOTE(bill): ignore this message for the time being
  1003. // error(e->token, "@(thread_local) is not supported for this target platform");
  1004. }
  1005. if(build_context.no_thread_local) {
  1006. e->Variable.thread_local_model.len = 0;
  1007. }
  1008. String context_name = str_lit("variable declaration");
  1009. if (type_expr != nullptr) {
  1010. e->type = check_type(ctx, type_expr);
  1011. }
  1012. if (e->type != nullptr) {
  1013. if (is_type_polymorphic(base_type(e->type))) {
  1014. gbString str = type_to_string(e->type);
  1015. defer (gb_string_free(str));
  1016. error(e->token, "Invalid use of a polymorphic type '%s' in %.*s", str, LIT(context_name));
  1017. e->type = t_invalid;
  1018. } else if (is_type_empty_union(e->type)) {
  1019. gbString str = type_to_string(e->type);
  1020. defer (gb_string_free(str));
  1021. error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
  1022. e->type = t_invalid;
  1023. }
  1024. }
  1025. if (e->Variable.is_foreign) {
  1026. if (init_expr != nullptr) {
  1027. error(e->token, "A foreign variable declaration cannot have a default value");
  1028. }
  1029. init_entity_foreign_library(ctx, e);
  1030. if (is_arch_wasm()) {
  1031. error(e->token, "A foreign variable declaration are not allowed for the '%.*s' architecture", LIT(target_arch_names[build_context.metrics.arch]));
  1032. }
  1033. }
  1034. if (ac.link_name.len > 0) {
  1035. e->Variable.link_name = ac.link_name;
  1036. }
  1037. if (ac.link_section.len > 0) {
  1038. e->Variable.link_section = ac.link_section;
  1039. }
  1040. if (e->Variable.is_foreign || e->Variable.is_export) {
  1041. String name = e->token.string;
  1042. if (e->Variable.link_name.len > 0) {
  1043. name = e->Variable.link_name;
  1044. }
  1045. auto *fp = &ctx->info->foreigns;
  1046. StringHashKey key = string_hash_string(name);
  1047. Entity **found = string_map_get(fp, key);
  1048. if (found) {
  1049. Entity *f = *found;
  1050. TokenPos pos = f->token.pos;
  1051. Type *this_type = base_type(e->type);
  1052. Type *other_type = base_type(f->type);
  1053. if (!are_types_identical(this_type, other_type)) {
  1054. error(e->token,
  1055. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  1056. "\tat %s",
  1057. LIT(name), token_pos_to_string(pos));
  1058. }
  1059. } else {
  1060. string_map_set(fp, key, e);
  1061. }
  1062. }
  1063. if (e->Variable.link_name.len > 0) {
  1064. e->flags |= EntityFlag_CustomLinkName;
  1065. }
  1066. if (init_expr == nullptr) {
  1067. if (type_expr == nullptr) {
  1068. e->type = t_invalid;
  1069. }
  1070. return;
  1071. }
  1072. Operand o = {};
  1073. check_expr_with_type_hint(ctx, &o, init_expr, e->type);
  1074. check_init_variable(ctx, e, &o, str_lit("variable declaration"));
  1075. check_rtti_type_disallowed(e->token, e->type, "A variable declaration is using a type, %s, which has been disallowed");
  1076. }
  1077. gb_internal void check_proc_group_decl(CheckerContext *ctx, Entity *&pg_entity, DeclInfo *d) {
  1078. GB_ASSERT(pg_entity->kind == Entity_ProcGroup);
  1079. auto *pge = &pg_entity->ProcGroup;
  1080. String proc_group_name = pg_entity->token.string;
  1081. ast_node(pg, ProcGroup, d->init_expr);
  1082. pge->entities = array_make<Entity*>(permanent_allocator(), 0, pg->args.count);
  1083. // NOTE(bill): This must be set here to prevent cycles in checking if someone
  1084. // places the entity within itself
  1085. pg_entity->type = t_invalid;
  1086. PtrSet<Entity *> entity_set = {};
  1087. ptr_set_init(&entity_set, 2*pg->args.count);
  1088. for (Ast *arg : pg->args) {
  1089. Entity *e = nullptr;
  1090. Operand o = {};
  1091. if (arg->kind == Ast_Ident) {
  1092. e = check_ident(ctx, &o, arg, nullptr, nullptr, true);
  1093. } else if (arg->kind == Ast_SelectorExpr) {
  1094. e = check_selector(ctx, &o, arg, nullptr);
  1095. }
  1096. if (e == nullptr) {
  1097. error(arg, "Expected a valid entity name in procedure group, got %.*s", LIT(ast_strings[arg->kind]));
  1098. continue;
  1099. }
  1100. if (e->kind == Entity_Variable) {
  1101. if (!is_type_proc(e->type)) {
  1102. gbString s = type_to_string(e->type);
  1103. defer (gb_string_free(s));
  1104. error(arg, "Expected a procedure, got %s", s);
  1105. continue;
  1106. }
  1107. } else if (e->kind != Entity_Procedure) {
  1108. error(arg, "Expected a procedure entity");
  1109. continue;
  1110. }
  1111. if (ptr_set_update(&entity_set, e)) {
  1112. error(arg, "Previous use of `%.*s` in procedure group", LIT(e->token.string));
  1113. continue;
  1114. }
  1115. array_add(&pge->entities, e);
  1116. }
  1117. ptr_set_destroy(&entity_set);
  1118. for (isize j = 0; j < pge->entities.count; j++) {
  1119. Entity *p = pge->entities[j];
  1120. if (p->type == t_invalid) {
  1121. // NOTE(bill): This invalid overload has already been handled
  1122. continue;
  1123. }
  1124. String name = p->token.string;
  1125. for (isize k = j+1; k < pge->entities.count; k++) {
  1126. Entity *q = pge->entities[k];
  1127. GB_ASSERT(p != q);
  1128. bool is_invalid = false;
  1129. TokenPos pos = q->token.pos;
  1130. if (q->type == nullptr || q->type == t_invalid) {
  1131. continue;
  1132. }
  1133. begin_error_block();
  1134. defer (end_error_block());
  1135. ProcTypeOverloadKind kind = are_proc_types_overload_safe(p->type, q->type);
  1136. bool both_have_where_clauses = false;
  1137. if (p->decl_info->proc_lit != nullptr && q->decl_info->proc_lit != nullptr) {
  1138. GB_ASSERT(p->decl_info->proc_lit->kind == Ast_ProcLit);
  1139. GB_ASSERT(q->decl_info->proc_lit->kind == Ast_ProcLit);
  1140. auto pl = &p->decl_info->proc_lit->ProcLit;
  1141. auto ql = &q->decl_info->proc_lit->ProcLit;
  1142. // Allow collisions if the procedures both have 'where' clauses and are both polymorphic
  1143. bool pw = pl->where_token.kind != Token_Invalid && is_type_polymorphic(p->type, true);
  1144. bool qw = ql->where_token.kind != Token_Invalid && is_type_polymorphic(q->type, true);
  1145. both_have_where_clauses = pw && qw;
  1146. }
  1147. if (!both_have_where_clauses) switch (kind) {
  1148. case ProcOverload_Identical:
  1149. error(p->token, "Overloaded procedure '%.*s' has the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  1150. is_invalid = true;
  1151. break;
  1152. // case ProcOverload_CallingConvention:
  1153. // error(p->token, "Overloaded procedure '%.*s' has the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  1154. // is_invalid = true;
  1155. // break;
  1156. case ProcOverload_ParamVariadic:
  1157. error(p->token, "Overloaded procedure '%.*s' has the same type as another procedure in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  1158. is_invalid = true;
  1159. break;
  1160. case ProcOverload_ResultCount:
  1161. case ProcOverload_ResultTypes:
  1162. error(p->token, "Overloaded procedure '%.*s' has the same parameters but different results in the procedure group '%.*s'", LIT(name), LIT(proc_group_name));
  1163. is_invalid = true;
  1164. break;
  1165. case ProcOverload_Polymorphic:
  1166. #if 0
  1167. 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));
  1168. is_invalid = true;
  1169. #endif
  1170. break;
  1171. case ProcOverload_ParamCount:
  1172. case ProcOverload_ParamTypes:
  1173. // This is okay :)
  1174. break;
  1175. }
  1176. if (is_invalid) {
  1177. error_line("\tprevious procedure at %s\n", token_pos_to_string(pos));
  1178. q->type = t_invalid;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. gb_internal void check_entity_decl(CheckerContext *ctx, Entity *e, DeclInfo *d, Type *named_type) {
  1184. if (e->state == EntityState_Resolved) {
  1185. return;
  1186. }
  1187. if (e->flags & EntityFlag_Lazy) {
  1188. mutex_lock(&ctx->info->lazy_mutex);
  1189. }
  1190. String name = e->token.string;
  1191. if (e->type != nullptr || e->state != EntityState_Unresolved) {
  1192. error(e->token, "Illegal declaration cycle of `%.*s`", LIT(name));
  1193. } else {
  1194. GB_ASSERT(e->state == EntityState_Unresolved);
  1195. if (d == nullptr) {
  1196. d = decl_info_of_entity(e);
  1197. if (d == nullptr) {
  1198. // TODO(bill): Err here?
  1199. e->type = t_invalid;
  1200. e->state = EntityState_Resolved;
  1201. set_base_type(named_type, t_invalid);
  1202. goto end;
  1203. }
  1204. }
  1205. CheckerContext c = *ctx;
  1206. c.scope = d->scope;
  1207. c.decl = d;
  1208. c.type_level = 0;
  1209. e->parent_proc_decl = c.curr_proc_decl;
  1210. e->state = EntityState_InProgress;
  1211. switch (e->kind) {
  1212. case Entity_Variable:
  1213. check_global_variable_decl(&c, e, d->type_expr, d->init_expr);
  1214. break;
  1215. case Entity_Constant:
  1216. check_const_decl(&c, e, d->type_expr, d->init_expr, named_type);
  1217. break;
  1218. case Entity_TypeName: {
  1219. check_type_decl(&c, e, d->init_expr, named_type);
  1220. break;
  1221. }
  1222. case Entity_Procedure:
  1223. check_proc_decl(&c, e, d);
  1224. break;
  1225. case Entity_ProcGroup:
  1226. check_proc_group_decl(&c, e, d);
  1227. break;
  1228. }
  1229. e->state = EntityState_Resolved;
  1230. }
  1231. end:;
  1232. // NOTE(bill): Add it to the list of checked entities
  1233. if (e->flags & EntityFlag_Lazy) {
  1234. array_add(&ctx->info->entities, e);
  1235. mutex_unlock(&ctx->info->lazy_mutex);
  1236. }
  1237. }
  1238. gb_internal void add_deps_from_child_to_parent(DeclInfo *decl) {
  1239. if (decl && decl->parent) {
  1240. Scope *ps = decl->parent->scope;
  1241. if (ps->flags & (ScopeFlag_File & ScopeFlag_Pkg & ScopeFlag_Global)) {
  1242. return;
  1243. } else {
  1244. // NOTE(bill): Add the dependencies from the procedure literal (lambda)
  1245. // But only at the procedure level
  1246. rw_mutex_shared_lock(&decl->deps_mutex);
  1247. rw_mutex_lock(&decl->parent->deps_mutex);
  1248. for (Entity *e : decl->deps) {
  1249. ptr_set_add(&decl->parent->deps, e);
  1250. }
  1251. rw_mutex_unlock(&decl->parent->deps_mutex);
  1252. rw_mutex_shared_unlock(&decl->deps_mutex);
  1253. rw_mutex_shared_lock(&decl->type_info_deps_mutex);
  1254. rw_mutex_lock(&decl->parent->type_info_deps_mutex);
  1255. for (Type *t : decl->type_info_deps) {
  1256. ptr_set_add(&decl->parent->type_info_deps, t);
  1257. }
  1258. rw_mutex_unlock(&decl->parent->type_info_deps_mutex);
  1259. rw_mutex_shared_unlock(&decl->type_info_deps_mutex);
  1260. }
  1261. }
  1262. }
  1263. struct ProcUsingVar {
  1264. Entity *e;
  1265. Entity *uvar;
  1266. };
  1267. gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *type, Ast *body) {
  1268. if (body == nullptr) {
  1269. return false;
  1270. }
  1271. GB_ASSERT(body->kind == Ast_BlockStmt);
  1272. String proc_name = {};
  1273. if (token.kind == Token_Ident) {
  1274. proc_name = token.string;
  1275. } else {
  1276. // TODO(bill): Better name
  1277. proc_name = str_lit("(anonymous-procedure)");
  1278. }
  1279. CheckerContext new_ctx = *ctx_;
  1280. CheckerContext *ctx = &new_ctx;
  1281. GB_ASSERT(type->kind == Type_Proc);
  1282. ctx->scope = decl->scope;
  1283. ctx->decl = decl;
  1284. ctx->proc_name = proc_name;
  1285. ctx->curr_proc_decl = decl;
  1286. ctx->curr_proc_sig = type;
  1287. ctx->curr_proc_calling_convention = type->Proc.calling_convention;
  1288. if (ctx->pkg->name != "runtime") {
  1289. switch (type->Proc.calling_convention) {
  1290. case ProcCC_None:
  1291. error(body, "Procedures with the calling convention \"none\" are not allowed a body");
  1292. break;
  1293. }
  1294. }
  1295. ast_node(bs, BlockStmt, body);
  1296. Array<ProcUsingVar> using_entities = {};
  1297. using_entities.allocator = heap_allocator();
  1298. defer (array_free(&using_entities));
  1299. {
  1300. if (type->Proc.param_count > 0) {
  1301. TypeTuple *params = &type->Proc.params->Tuple;
  1302. for (Entity *e : params->variables) {
  1303. if (e->kind != Entity_Variable) {
  1304. continue;
  1305. }
  1306. if (!(e->flags & EntityFlag_Using)) {
  1307. continue;
  1308. }
  1309. if (is_blank_ident(e->token)) {
  1310. error(e->token, "'using' a procedure parameter requires a non blank identifier");
  1311. break;
  1312. }
  1313. bool is_value = (e->flags & EntityFlag_Value) != 0 && !is_type_pointer(e->type);
  1314. String name = e->token.string;
  1315. Type *t = base_type(type_deref(e->type));
  1316. if (t->kind == Type_Struct) {
  1317. Scope *scope = t->Struct.scope;
  1318. GB_ASSERT(scope != nullptr);
  1319. rw_mutex_lock(&scope->mutex);
  1320. for (auto const &entry : scope->elements) {
  1321. Entity *f = entry.value;
  1322. if (f->kind == Entity_Variable) {
  1323. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, nullptr);
  1324. if (is_value) uvar->flags |= EntityFlag_Value;
  1325. ProcUsingVar puv = {e, uvar};
  1326. array_add(&using_entities, puv);
  1327. }
  1328. }
  1329. rw_mutex_unlock(&scope->mutex);
  1330. } else {
  1331. error(e->token, "'using' can only be applied to variables of type struct");
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. }
  1337. rw_mutex_lock(&ctx->scope->mutex);
  1338. for (auto const &entry : using_entities) {
  1339. Entity *e = entry.e;
  1340. Entity *uvar = entry.uvar;
  1341. Entity *prev = scope_insert_no_mutex(ctx->scope, uvar);
  1342. if (prev != nullptr) {
  1343. error(e->token, "Namespace collision while 'using' procedure argument '%.*s' of: %.*s", LIT(e->token.string), LIT(prev->token.string));
  1344. error_line("%.*s != %.*s\n", LIT(uvar->token.string), LIT(prev->token.string));
  1345. break;
  1346. }
  1347. }
  1348. rw_mutex_unlock(&ctx->scope->mutex);
  1349. bool where_clause_ok = evaluate_where_clauses(ctx, nullptr, decl->scope, &decl->proc_lit->ProcLit.where_clauses, !decl->where_clauses_evaluated);
  1350. if (!where_clause_ok) {
  1351. // NOTE(bill, 2019-08-31): Don't check the body as the where clauses failed
  1352. return false;
  1353. }
  1354. check_open_scope(ctx, body);
  1355. {
  1356. for (auto const &entry : using_entities) {
  1357. Entity *uvar = entry.uvar;
  1358. Entity *prev = scope_insert(ctx->scope, uvar);
  1359. gb_unused(prev);
  1360. // NOTE(bill): Don't err here
  1361. }
  1362. GB_ASSERT(decl->proc_checked_state != ProcCheckedState_Checked);
  1363. if (decl->defer_use_checked) {
  1364. GB_ASSERT(is_type_polymorphic(type, true));
  1365. error(token, "Defer Use Checked: %.*s", LIT(decl->entity->token.string));
  1366. GB_ASSERT(decl->defer_use_checked == false);
  1367. }
  1368. check_stmt_list(ctx, bs->stmts, Stmt_CheckScopeDecls);
  1369. decl->defer_use_checked = true;
  1370. for (Ast *stmt : bs->stmts) {
  1371. if (stmt->kind == Ast_ValueDecl) {
  1372. ast_node(vd, ValueDecl, stmt);
  1373. for (Ast *name : vd->names) {
  1374. if (!is_blank_ident(name)) {
  1375. if (name->kind == Ast_Ident) {
  1376. GB_ASSERT(name->Ident.entity != nullptr);
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. if (type->Proc.result_count > 0) {
  1383. if (!check_is_terminating(body, str_lit(""))) {
  1384. if (token.kind == Token_Ident) {
  1385. error(bs->close, "Missing return statement at the end of the procedure '%.*s'", LIT(token.string));
  1386. } else {
  1387. // NOTE(bill): Anonymous procedure (lambda)
  1388. error(bs->close, "Missing return statement at the end of the procedure");
  1389. }
  1390. }
  1391. } else if (type->Proc.diverging) {
  1392. if (!check_is_terminating(body, str_lit(""))) {
  1393. if (token.kind == Token_Ident) {
  1394. error(bs->close, "Missing diverging call at the end of the procedure '%.*s'", LIT(token.string));
  1395. } else {
  1396. // NOTE(bill): Anonymous procedure (lambda)
  1397. error(bs->close, "Missing diverging call at the end of the procedure");
  1398. }
  1399. }
  1400. }
  1401. }
  1402. check_close_scope(ctx);
  1403. check_scope_usage(ctx->checker, ctx->scope);
  1404. add_deps_from_child_to_parent(decl);
  1405. return true;
  1406. }