check_stmt.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. void check_stmt_list(Checker *c, Array<AstNode *> stmts, u32 flags) {
  2. if (stmts.count == 0) {
  3. return;
  4. }
  5. if (flags&Stmt_CheckScopeDecls) {
  6. check_scope_decls(c, stmts, cast(isize)(1.2*stmts.count));
  7. }
  8. bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0;
  9. flags &= ~Stmt_FallthroughAllowed;
  10. isize max = stmts.count;
  11. for (isize i = stmts.count-1; i >= 0; i--) {
  12. if (stmts[i]->kind != AstNode_EmptyStmt) {
  13. break;
  14. }
  15. max--;
  16. }
  17. for (isize i = 0; i < max; i++) {
  18. AstNode *n = stmts[i];
  19. if (n->kind == AstNode_EmptyStmt) {
  20. continue;
  21. }
  22. u32 new_flags = flags;
  23. if (ft_ok && i+1 == max) {
  24. new_flags |= Stmt_FallthroughAllowed;
  25. }
  26. if (i+1 < max) {
  27. switch (n->kind) {
  28. case AstNode_ReturnStmt:
  29. error(n, "Statements after this 'return' are never execu");
  30. break;
  31. case AstNode_BranchStmt:
  32. error(n, "Statements after this '%.*s' are never executed", LIT(n->BranchStmt.token.string));
  33. break;
  34. }
  35. }
  36. check_stmt(c, n, new_flags);
  37. }
  38. }
  39. bool check_is_terminating_list(Array<AstNode *> stmts) {
  40. // Iterate backwards
  41. for (isize n = stmts.count-1; n >= 0; n--) {
  42. AstNode *stmt = stmts[n];
  43. if (stmt->kind != AstNode_EmptyStmt) {
  44. return check_is_terminating(stmt);
  45. }
  46. }
  47. return false;
  48. }
  49. bool check_has_break_list(Array<AstNode *> stmts, bool implicit) {
  50. for_array(i, stmts) {
  51. AstNode *stmt = stmts[i];
  52. if (check_has_break(stmt, implicit)) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. bool check_has_break(AstNode *stmt, bool implicit) {
  59. switch (stmt->kind) {
  60. case AstNode_BranchStmt:
  61. if (stmt->BranchStmt.token.kind == Token_break) {
  62. return implicit;
  63. }
  64. break;
  65. case AstNode_BlockStmt:
  66. return check_has_break_list(stmt->BlockStmt.stmts, implicit);
  67. case AstNode_IfStmt:
  68. if (check_has_break(stmt->IfStmt.body, implicit) ||
  69. (stmt->IfStmt.else_stmt != nullptr && check_has_break(stmt->IfStmt.else_stmt, implicit))) {
  70. return true;
  71. }
  72. break;
  73. case AstNode_CaseClause:
  74. return check_has_break_list(stmt->CaseClause.stmts, implicit);
  75. }
  76. return false;
  77. }
  78. // NOTE(bill): The last expression has to be a 'return' statement
  79. // TODO(bill): This is a mild hack and should be probably handled properly
  80. bool check_is_terminating(AstNode *node) {
  81. switch (node->kind) {
  82. case_ast_node(rs, ReturnStmt, node);
  83. return true;
  84. case_end;
  85. case_ast_node(bs, BlockStmt, node);
  86. return check_is_terminating_list(bs->stmts);
  87. case_end;
  88. case_ast_node(es, ExprStmt, node);
  89. return check_is_terminating(es->expr);
  90. case_end;
  91. case_ast_node(is, IfStmt, node);
  92. if (is->else_stmt != nullptr) {
  93. if (check_is_terminating(is->body) &&
  94. check_is_terminating(is->else_stmt)) {
  95. return true;
  96. }
  97. }
  98. case_end;
  99. case_ast_node(ws, WhenStmt, node);
  100. if (ws->else_stmt != nullptr) {
  101. if (check_is_terminating(ws->body) &&
  102. check_is_terminating(ws->else_stmt)) {
  103. return true;
  104. }
  105. }
  106. case_end;
  107. case_ast_node(fs, ForStmt, node);
  108. if (fs->cond == nullptr && !check_has_break(fs->body, true)) {
  109. return check_is_terminating(fs->body);
  110. }
  111. case_end;
  112. case_ast_node(rs, RangeStmt, node);
  113. return false;
  114. case_end;
  115. case_ast_node(ss, SwitchStmt, node);
  116. bool has_default = false;
  117. for_array(i, ss->body->BlockStmt.stmts) {
  118. AstNode *clause = ss->body->BlockStmt.stmts[i];
  119. ast_node(cc, CaseClause, clause);
  120. if (cc->list.count == 0) {
  121. has_default = true;
  122. }
  123. if (!check_is_terminating_list(cc->stmts) ||
  124. check_has_break_list(cc->stmts, true)) {
  125. return false;
  126. }
  127. }
  128. return has_default;
  129. case_end;
  130. case_ast_node(ss, TypeSwitchStmt, node);
  131. bool has_default = false;
  132. for_array(i, ss->body->BlockStmt.stmts) {
  133. AstNode *clause = ss->body->BlockStmt.stmts[i];
  134. ast_node(cc, CaseClause, clause);
  135. if (cc->list.count == 0) {
  136. has_default = true;
  137. }
  138. if (!check_is_terminating_list(cc->stmts) ||
  139. check_has_break_list(cc->stmts, true)) {
  140. return false;
  141. }
  142. }
  143. return has_default;
  144. case_end;
  145. case_ast_node(pc, PushContext, node);
  146. return check_is_terminating(pc->body);
  147. case_end;
  148. }
  149. return false;
  150. }
  151. Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) {
  152. if (rhs->mode == Addressing_Invalid) {
  153. return nullptr;
  154. }
  155. if (rhs->type == t_invalid &&
  156. rhs->mode != Addressing_ProcGroup &&
  157. rhs->mode != Addressing_Builtin) {
  158. return nullptr;
  159. }
  160. AstNode *node = unparen_expr(lhs->expr);
  161. // NOTE(bill): Ignore assignments to '_'
  162. if (is_blank_ident(node)) {
  163. add_entity_definition(&c->info, node, nullptr);
  164. check_assignment(c, rhs, nullptr, str_lit("assignment to '_' identifier"));
  165. if (rhs->mode == Addressing_Invalid) {
  166. return nullptr;
  167. }
  168. return rhs->type;
  169. }
  170. Entity *e = nullptr;
  171. bool used = false;
  172. if (lhs->mode == Addressing_Invalid ||
  173. (lhs->type == t_invalid &&
  174. lhs->mode != Addressing_ProcGroup &&
  175. lhs->mode != Addressing_Builtin)) {
  176. return nullptr;
  177. }
  178. if (rhs->mode == Addressing_ProcGroup) {
  179. Array<Entity *> procs = proc_group_entities(c, *rhs);
  180. GB_ASSERT(procs.count > 0);
  181. // NOTE(bill): These should be done
  182. for_array(i, procs) {
  183. Type *t = base_type(procs[i]->type);
  184. if (t == t_invalid) {
  185. continue;
  186. }
  187. Operand x = {};
  188. x.mode = Addressing_Value;
  189. x.type = t;
  190. if (check_is_assignable_to(c, &x, lhs->type)) {
  191. e = procs[i];
  192. add_entity_use(c, rhs->expr, e);
  193. break;
  194. }
  195. }
  196. if (e != nullptr) {
  197. // HACK TODO(bill): Should the entities be freed as it's technically a leak
  198. rhs->mode = Addressing_Value;
  199. rhs->type = e->type;
  200. rhs->proc_group = nullptr;
  201. }
  202. } else {
  203. if (node->kind == AstNode_Ident) {
  204. ast_node(i, Ident, node);
  205. e = scope_lookup_entity(c->context.scope, i->token.string);
  206. if (e != nullptr && e->kind == Entity_Variable) {
  207. used = (e->flags & EntityFlag_Used) != 0; // TODO(bill): Make backup just in case
  208. }
  209. }
  210. }
  211. if (e != nullptr && used) {
  212. e->flags |= EntityFlag_Used;
  213. }
  214. Type *assignment_type = lhs->type;
  215. switch (lhs->mode) {
  216. case Addressing_Invalid:
  217. return nullptr;
  218. case Addressing_Variable: {
  219. if (is_type_bit_field_value(lhs->type)) {
  220. Type *lt = base_type(lhs->type);
  221. i64 lhs_bits = lt->BitFieldValue.bits;
  222. if (rhs->mode == Addressing_Constant) {
  223. ExactValue v = exact_value_to_integer(rhs->value);
  224. if (v.kind == ExactValue_Integer) {
  225. i64 i = v.value_integer;
  226. u64 u = bit_cast<u64>(i);
  227. u64 umax = ~cast(u64)0ull;
  228. if (lhs_bits < 64) {
  229. umax = (1ull << cast(u64)lhs_bits) - 1ull;
  230. }
  231. i64 imax = 1ll << (cast(i64)lhs_bits-1ll);
  232. bool ok = false;
  233. ok = !(u < 0 || u > umax);
  234. if (ok) {
  235. return rhs->type;
  236. }
  237. }
  238. } else if (is_type_integer(rhs->type)) {
  239. // TODO(bill): Any other checks?
  240. return rhs->type;
  241. }
  242. gbString lhs_expr = expr_to_string(lhs->expr);
  243. gbString rhs_expr = expr_to_string(rhs->expr);
  244. error(rhs->expr, "Cannot assign '%s' to bit field '%s'", rhs_expr, lhs_expr);
  245. gb_string_free(rhs_expr);
  246. gb_string_free(lhs_expr);
  247. return nullptr;
  248. }
  249. break;
  250. }
  251. case Addressing_MapIndex: {
  252. AstNode *ln = unparen_expr(lhs->expr);
  253. if (ln->kind == AstNode_IndexExpr) {
  254. AstNode *x = ln->IndexExpr.expr;
  255. TypeAndValue tav = type_and_value_of_expr(&c->info, x);
  256. GB_ASSERT(tav.mode != Addressing_Invalid);
  257. if (tav.mode != Addressing_Variable) {
  258. if (!is_type_pointer(tav.type)) {
  259. gbString str = expr_to_string(lhs->expr);
  260. error(lhs->expr, "Cannot assign to the value of a map '%s'", str);
  261. gb_string_free(str);
  262. return nullptr;
  263. }
  264. }
  265. }
  266. break;
  267. }
  268. default: {
  269. if (lhs->expr->kind == AstNode_SelectorExpr) {
  270. // NOTE(bill): Extra error checks
  271. Operand op_c = {Addressing_Invalid};
  272. ast_node(se, SelectorExpr, lhs->expr);
  273. check_expr(c, &op_c, se->expr);
  274. if (op_c.mode == Addressing_MapIndex) {
  275. gbString str = expr_to_string(lhs->expr);
  276. error(lhs->expr, "Cannot assign to struct field '%s' in map", str);
  277. gb_string_free(str);
  278. return nullptr;
  279. }
  280. }
  281. gbString str = expr_to_string(lhs->expr);
  282. if (lhs->mode == Addressing_Immutable) {
  283. error(lhs->expr, "Cannot assign to an immutable: '%s'", str);
  284. } else {
  285. error(lhs->expr, "Cannot assign to '%s'", str);
  286. }
  287. gb_string_free(str);
  288. break;
  289. }
  290. }
  291. check_assignment(c, rhs, assignment_type, str_lit("assignment"));
  292. if (rhs->mode == Addressing_Invalid) {
  293. return nullptr;
  294. }
  295. return rhs->type;
  296. }
  297. void check_stmt_internal(Checker *c, AstNode *node, u32 flags);
  298. void check_stmt(Checker *c, AstNode *node, u32 flags) {
  299. u32 prev_stmt_state_flags = c->context.stmt_state_flags;
  300. if (node->stmt_state_flags != 0) {
  301. u32 in = node->stmt_state_flags;
  302. u32 out = c->context.stmt_state_flags;
  303. if (in & StmtStateFlag_no_bounds_check) {
  304. out |= StmtStateFlag_no_bounds_check;
  305. out &= ~StmtStateFlag_bounds_check;
  306. } else {
  307. // if (in & StmtStateFlag_bounds_check) {
  308. out |= StmtStateFlag_bounds_check;
  309. out &= ~StmtStateFlag_no_bounds_check;
  310. }
  311. c->context.stmt_state_flags = out;
  312. }
  313. check_stmt_internal(c, node, flags);
  314. c->context.stmt_state_flags = prev_stmt_state_flags;
  315. }
  316. void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) {
  317. Operand operand = {Addressing_Invalid};
  318. check_expr(c, &operand, ws->cond);
  319. if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
  320. error(ws->cond, "Non-constant boolean 'when' condition");
  321. return;
  322. }
  323. if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
  324. error(ws->cond, "Invalid body for 'when' statement");
  325. return;
  326. }
  327. if (operand.value.kind == ExactValue_Bool &&
  328. operand.value.value_bool) {
  329. check_stmt_list(c, ws->body->BlockStmt.stmts, flags);
  330. } else if (ws->else_stmt) {
  331. switch (ws->else_stmt->kind) {
  332. case AstNode_BlockStmt:
  333. check_stmt_list(c, ws->else_stmt->BlockStmt.stmts, flags);
  334. break;
  335. case AstNode_WhenStmt:
  336. check_when_stmt(c, &ws->else_stmt->WhenStmt, flags);
  337. break;
  338. default:
  339. error(ws->else_stmt, "Invalid 'else' statement in 'when' statement");
  340. break;
  341. }
  342. }
  343. }
  344. void check_label(Checker *c, AstNode *label) {
  345. if (label == nullptr) {
  346. return;
  347. }
  348. ast_node(l, Label, label);
  349. if (l->name->kind != AstNode_Ident) {
  350. error(l->name, "A label's name must be an identifier");
  351. return;
  352. }
  353. String name = l->name->Ident.token.string;
  354. if (is_blank_ident(name)) {
  355. error(l->name, "A label's name cannot be a blank identifier");
  356. return;
  357. }
  358. if (c->proc_stack.count == 0) {
  359. error(l->name, "A label is only allowed within a procedure");
  360. return;
  361. }
  362. GB_ASSERT(c->context.decl != nullptr);
  363. bool ok = true;
  364. for_array(i, c->context.decl->labels) {
  365. BlockLabel bl = c->context.decl->labels[i];
  366. if (bl.name == name) {
  367. error(label, "Duplicate label with the name '%.*s'", LIT(name));
  368. ok = false;
  369. break;
  370. }
  371. }
  372. Entity *e = alloc_entity_label(c->context.scope, l->name->Ident.token, t_invalid, label);
  373. add_entity(c, c->context.scope, l->name, e);
  374. e->parent_proc_decl = c->context.curr_proc_decl;
  375. if (ok) {
  376. BlockLabel bl = {name, label};
  377. array_add(&c->context.decl->labels, bl);
  378. }
  379. }
  380. // Returns 'true' for 'continue', 'false' for 'return'
  381. bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bool is_selector, Entity *e) {
  382. if (e == nullptr) {
  383. error(us->token, "'using' applied to an unknown entity");
  384. return true;
  385. }
  386. add_entity_use(c, expr, e);
  387. switch (e->kind) {
  388. case Entity_TypeName: {
  389. Type *t = base_type(e->type);
  390. if (t->kind == Type_Enum) {
  391. for_array(i, t->Enum.fields) {
  392. Entity *f = t->Enum.fields[i];
  393. if (!is_entity_exported(f)) continue;
  394. Entity *found = scope_insert_entity(c->context.scope, f);
  395. if (found != nullptr) {
  396. gbString expr_str = expr_to_string(expr);
  397. error(us->token, "Namespace collision while 'using' '%s' of: %.*s", expr_str, LIT(found->token.string));
  398. gb_string_free(expr_str);
  399. return false;
  400. }
  401. f->using_parent = e;
  402. }
  403. } else {
  404. error(us->token, "'using' can be only applied to enum type entities");
  405. }
  406. break;
  407. }
  408. case Entity_ImportName: {
  409. Scope *scope = e->ImportName.scope;
  410. for_array(i, scope->elements.entries) {
  411. Entity *decl = scope->elements.entries[i].value;
  412. if (!is_entity_exported(decl)) continue;
  413. Entity *found = scope_insert_entity(c->context.scope, decl);
  414. if (found != nullptr) {
  415. gbString expr_str = expr_to_string(expr);
  416. error(us->token,
  417. "Namespace collision while 'using' '%s' of: %.*s\n"
  418. "\tat %.*s(%td:%td)\n"
  419. "\tat %.*s(%td:%td)",
  420. expr_str, LIT(found->token.string),
  421. LIT(found->token.pos.file), found->token.pos.line, found->token.pos.column,
  422. LIT(decl->token.pos.file), decl->token.pos.line, decl->token.pos.column
  423. );
  424. gb_string_free(expr_str);
  425. return false;
  426. }
  427. }
  428. break;
  429. }
  430. case Entity_Variable: {
  431. Type *t = base_type(type_deref(e->type));
  432. if (t->kind == Type_Struct) {
  433. // TODO(bill): Make it work for unions too
  434. Scope *found = scope_of_node(&c->info, t->Struct.node);
  435. for_array(i, found->elements.entries) {
  436. Entity *f = found->elements.entries[i].value;
  437. if (f->kind == Entity_Variable) {
  438. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type);
  439. uvar->using_expr = expr;
  440. Entity *prev = scope_insert_entity(c->context.scope, uvar);
  441. if (prev != nullptr) {
  442. gbString expr_str = expr_to_string(expr);
  443. error(us->token, "Namespace collision while using '%s' of: '%.*s'", expr_str, LIT(prev->token.string));
  444. gb_string_free(expr_str);
  445. return false;
  446. }
  447. }
  448. }
  449. } else {
  450. error(us->token, "'using' can only be applied to variables of type 'struct'");
  451. return false;
  452. }
  453. break;
  454. }
  455. case Entity_Constant:
  456. error(us->token, "'using' cannot be applied to a constant");
  457. break;
  458. case Entity_Procedure:
  459. case Entity_ProcGroup:
  460. case Entity_Builtin:
  461. error(us->token, "'using' cannot be applied to a procedure");
  462. break;
  463. case Entity_Nil:
  464. error(us->token, "'using' cannot be applied to 'nil'");
  465. break;
  466. case Entity_Label:
  467. error(us->token, "'using' cannot be applied to a label");
  468. break;
  469. case Entity_Invalid:
  470. error(us->token, "'using' cannot be applied to an invalid entity");
  471. break;
  472. default:
  473. GB_PANIC("TODO(bill): 'using' other expressions?");
  474. }
  475. return true;
  476. }
  477. struct TypeAndToken {
  478. Type *type;
  479. Token token;
  480. };
  481. void add_constant_switch_case(Checker *c, Map<TypeAndToken> *seen, Operand operand, bool use_expr = true) {
  482. if (operand.mode != Addressing_Constant) {
  483. return;
  484. }
  485. if (operand.value.kind == ExactValue_Invalid) {
  486. return;
  487. }
  488. HashKey key = hash_exact_value(operand.value);
  489. TypeAndToken *found = map_get(seen, key);
  490. if (found != nullptr) {
  491. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  492. defer (gb_temp_arena_memory_end(tmp));
  493. isize count = multi_map_count(seen, key);
  494. TypeAndToken *taps = gb_alloc_array(c->tmp_allocator, TypeAndToken, count);
  495. multi_map_get_all(seen, key, taps);
  496. for (isize i = 0; i < count; i++) {
  497. TypeAndToken tap = taps[i];
  498. if (are_types_identical(operand.type, tap.type)) {
  499. TokenPos pos = tap.token.pos;
  500. if (use_expr) {
  501. gbString expr_str = expr_to_string(operand.expr);
  502. error(operand.expr,
  503. "Duplicate case '%s'\n"
  504. "\tprevious case at %.*s(%td:%td)",
  505. expr_str,
  506. LIT(pos.file), pos.line, pos.column);
  507. gb_string_free(expr_str);
  508. } else {
  509. error(operand.expr,
  510. "Duplicate case found with previous case at %.*s(%td:%td)",
  511. LIT(pos.file), pos.line, pos.column);
  512. }
  513. return;
  514. }
  515. }
  516. }
  517. TypeAndToken tap = {operand.type, ast_node_token(operand.expr)};
  518. multi_map_insert(seen, key, tap);
  519. }
  520. void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
  521. ast_node(ss, SwitchStmt, node);
  522. Operand x = {};
  523. mod_flags |= Stmt_BreakAllowed | Stmt_FallthroughAllowed;
  524. check_open_scope(c, node);
  525. defer (check_close_scope(c));
  526. check_label(c, ss->label); // TODO(bill): What should the label's "scope" be?
  527. if (ss->init != nullptr) {
  528. check_stmt(c, ss->init, 0);
  529. }
  530. if (ss->tag != nullptr) {
  531. check_expr(c, &x, ss->tag);
  532. check_assignment(c, &x, nullptr, str_lit("switch expression"));
  533. } else {
  534. x.mode = Addressing_Constant;
  535. x.type = t_bool;
  536. x.value = exact_value_bool(true);
  537. Token token = {};
  538. token.pos = ast_node_token(ss->body).pos;
  539. token.string = str_lit("true");
  540. x.expr = ast_ident(c->curr_ast_file, token);
  541. }
  542. // NOTE(bill): Check for multiple defaults
  543. AstNode *first_default = nullptr;
  544. ast_node(bs, BlockStmt, ss->body);
  545. for_array(i, bs->stmts) {
  546. AstNode *stmt = bs->stmts[i];
  547. AstNode *default_stmt = nullptr;
  548. if (stmt->kind == AstNode_CaseClause) {
  549. ast_node(cc, CaseClause, stmt);
  550. if (cc->list.count == 0) {
  551. default_stmt = stmt;
  552. }
  553. } else {
  554. error(stmt, "Invalid AST - expected case clause");
  555. }
  556. if (default_stmt != nullptr) {
  557. if (first_default != nullptr) {
  558. TokenPos pos = ast_node_token(first_default).pos;
  559. error(stmt,
  560. "multiple default clauses\n"
  561. "\tfirst at %.*s(%td:%td)",
  562. LIT(pos.file), pos.line, pos.column);
  563. } else {
  564. first_default = default_stmt;
  565. }
  566. }
  567. }
  568. bool complete = ss->complete;
  569. if (complete) {
  570. if (!is_type_enum(x.type)) {
  571. error(x.expr, "#complete switch statement can be only used with an enum type");
  572. complete = false;
  573. }
  574. }
  575. Map<TypeAndToken> seen = {}; // NOTE(bill): Multimap, Key: ExactValue
  576. map_init(&seen, heap_allocator());
  577. defer (map_destroy(&seen));
  578. for_array(stmt_index, bs->stmts) {
  579. AstNode *stmt = bs->stmts[stmt_index];
  580. if (stmt->kind != AstNode_CaseClause) {
  581. // NOTE(bill): error handled by above multiple default checker
  582. continue;
  583. }
  584. ast_node(cc, CaseClause, stmt);
  585. for_array(j, cc->list) {
  586. AstNode *expr = unparen_expr(cc->list[j]);
  587. if (is_ast_node_a_range(expr)) {
  588. ast_node(ie, BinaryExpr, expr);
  589. Operand lhs = {};
  590. Operand rhs = {};
  591. check_expr(c, &lhs, ie->left);
  592. if (x.mode == Addressing_Invalid) {
  593. continue;
  594. }
  595. if (lhs.mode == Addressing_Invalid) {
  596. continue;
  597. }
  598. check_expr(c, &rhs, ie->right);
  599. if (rhs.mode == Addressing_Invalid) {
  600. continue;
  601. }
  602. if (!is_type_ordered(x.type)) {
  603. gbString str = type_to_string(x.type);
  604. error(expr, "Unordered type '%s', is invalid for an interval expression", str);
  605. gb_string_free(str);
  606. continue;
  607. }
  608. TokenKind op = Token_Invalid;
  609. Operand a = lhs;
  610. Operand b = rhs;
  611. check_comparison(c, &a, &x, Token_LtEq);
  612. if (a.mode == Addressing_Invalid) {
  613. continue;
  614. }
  615. switch (ie->op.kind) {
  616. case Token_Ellipsis: op = Token_GtEq; break;
  617. case Token_HalfClosed: op = Token_Gt; break;
  618. default: error(ie->op, "Invalid interval operator"); continue;
  619. }
  620. check_comparison(c, &b, &x, op);
  621. if (b.mode == Addressing_Invalid) {
  622. continue;
  623. }
  624. switch (ie->op.kind) {
  625. case Token_Ellipsis: op = Token_LtEq; break;
  626. case Token_HalfClosed: op = Token_Lt; break;
  627. default: error(ie->op, "Invalid interval operator"); continue;
  628. }
  629. Operand a1 = lhs;
  630. Operand b1 = rhs;
  631. check_comparison(c, &a1, &b1, op);
  632. if (complete) {
  633. error(lhs.expr, "#complete switch statement does not allow ranges");
  634. }
  635. add_constant_switch_case(c, &seen, lhs);
  636. if (op == Token_LtEq) {
  637. add_constant_switch_case(c, &seen, rhs);
  638. }
  639. } else {
  640. Operand y = {};
  641. check_expr(c, &y, expr);
  642. if (x.mode == Addressing_Invalid ||
  643. y.mode == Addressing_Invalid) {
  644. continue;
  645. }
  646. convert_to_typed(c, &y, x.type);
  647. if (y.mode == Addressing_Invalid) {
  648. continue;
  649. }
  650. // NOTE(bill): the ordering here matters
  651. Operand z = y;
  652. check_comparison(c, &z, &x, Token_CmpEq);
  653. if (z.mode == Addressing_Invalid) {
  654. continue;
  655. }
  656. if (y.mode != Addressing_Constant) {
  657. if (complete) {
  658. error(y.expr, "#complete switch statement only allows constant case clauses");
  659. }
  660. continue;
  661. }
  662. add_constant_switch_case(c, &seen, y);
  663. }
  664. }
  665. check_open_scope(c, stmt);
  666. check_stmt_list(c, cc->stmts, mod_flags);
  667. check_close_scope(c);
  668. }
  669. if (complete) {
  670. Type *et = base_type(x.type);
  671. GB_ASSERT(is_type_enum(et));
  672. auto fields = et->Enum.fields;
  673. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  674. defer (gb_temp_arena_memory_end(tmp));
  675. auto unhandled = array_make<Entity *>(c->tmp_allocator, 0, fields.count);
  676. for_array(i, fields) {
  677. Entity *f = fields[i];
  678. if (f->kind != Entity_Constant) {
  679. continue;
  680. }
  681. ExactValue v = f->Constant.value;
  682. HashKey key = hash_exact_value(v);
  683. auto found = map_get(&seen, key);
  684. if (!found) {
  685. array_add(&unhandled, f);
  686. }
  687. }
  688. if (unhandled.count > 0) {
  689. if (unhandled.count == 1) {
  690. error_no_newline(node, "Unhandled switch case: ");
  691. } else {
  692. error_no_newline(node, "Unhandled switch cases: ");
  693. }
  694. for_array(i, unhandled) {
  695. Entity *f = unhandled[i];
  696. if (i > 0) {
  697. gb_printf_err(", ");
  698. }
  699. gb_printf_err("%.*s", LIT(f->token.string));
  700. }
  701. gb_printf_err("\n");
  702. }
  703. }
  704. }
  705. enum TypeSwitchKind {
  706. TypeSwitch_Invalid,
  707. TypeSwitch_Union,
  708. TypeSwitch_Any,
  709. };
  710. TypeSwitchKind check_valid_type_switch_type(Type *type) {
  711. type = type_deref(type);
  712. if (is_type_union(type)) {
  713. return TypeSwitch_Union;
  714. }
  715. if (is_type_any(type)) {
  716. return TypeSwitch_Any;
  717. }
  718. return TypeSwitch_Invalid;
  719. }
  720. void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) {
  721. ast_node(ss, TypeSwitchStmt, node);
  722. Operand x = {};
  723. mod_flags |= Stmt_BreakAllowed;
  724. check_open_scope(c, node);
  725. defer (check_close_scope(c));
  726. check_label(c, ss->label); // TODO(bill): What should the label's "scope" be?
  727. if (ss->tag->kind != AstNode_AssignStmt) {
  728. error(ss->tag, "Expected an 'in' assignment for this type switch statement");
  729. return;
  730. }
  731. ast_node(as, AssignStmt, ss->tag);
  732. Token as_token = ast_node_token(ss->tag);
  733. if (as->lhs.count != 1) {
  734. syntax_error(as_token, "Expected 1 name before 'in'");
  735. return;
  736. }
  737. if (as->rhs.count != 1) {
  738. syntax_error(as_token, "Expected 1 expression after 'in'");
  739. return;
  740. }
  741. AstNode *lhs = as->lhs[0];
  742. AstNode *rhs = as->rhs[0];
  743. check_expr(c, &x, rhs);
  744. check_assignment(c, &x, nullptr, str_lit("type switch expression"));
  745. TypeSwitchKind switch_kind = check_valid_type_switch_type(x.type);
  746. if (switch_kind == TypeSwitch_Invalid) {
  747. gbString str = type_to_string(x.type);
  748. error(x.expr, "Invalid type for this type switch expression, got '%s'", str);
  749. gb_string_free(str);
  750. return;
  751. }
  752. bool complete = ss->complete;
  753. if (complete) {
  754. if (switch_kind != TypeSwitch_Union) {
  755. error(node, "#complete switch statement may only be used with a union");
  756. complete = false;
  757. }
  758. }
  759. bool is_ptr = is_type_pointer(x.type);
  760. // NOTE(bill): Check for multiple defaults
  761. AstNode *first_default = nullptr;
  762. ast_node(bs, BlockStmt, ss->body);
  763. for_array(i, bs->stmts) {
  764. AstNode *stmt = bs->stmts[i];
  765. AstNode *default_stmt = nullptr;
  766. if (stmt->kind == AstNode_CaseClause) {
  767. ast_node(cc, CaseClause, stmt);
  768. if (cc->list.count == 0) {
  769. default_stmt = stmt;
  770. }
  771. } else {
  772. error(stmt, "Invalid AST - expected case clause");
  773. }
  774. if (default_stmt != nullptr) {
  775. if (first_default != nullptr) {
  776. TokenPos pos = ast_node_token(first_default).pos;
  777. error(stmt,
  778. "Multiple default clauses\n"
  779. "\tfirst at %.*s(%td:%td)",
  780. LIT(pos.file), pos.line, pos.column);
  781. } else {
  782. first_default = default_stmt;
  783. }
  784. }
  785. }
  786. if (lhs->kind != AstNode_Ident) {
  787. error(rhs, "Expected an identifier, got '%.*s'", LIT(ast_node_strings[rhs->kind]));
  788. return;
  789. }
  790. PtrSet<Type *> seen = {};
  791. ptr_set_init(&seen, heap_allocator());
  792. defer (ptr_set_destroy(&seen));
  793. for_array(i, bs->stmts) {
  794. AstNode *stmt = bs->stmts[i];
  795. if (stmt->kind != AstNode_CaseClause) {
  796. // NOTE(bill): error handled by above multiple default checker
  797. continue;
  798. }
  799. ast_node(cc, CaseClause, stmt);
  800. // TODO(bill): Make robust
  801. Type *bt = base_type(type_deref(x.type));
  802. Type *case_type = nullptr;
  803. for_array(type_index, cc->list) {
  804. AstNode *type_expr = cc->list[type_index];
  805. if (type_expr != nullptr) { // Otherwise it's a default expression
  806. Operand y = {};
  807. check_expr_or_type(c, &y, type_expr);
  808. if (switch_kind == TypeSwitch_Union) {
  809. GB_ASSERT(is_type_union(bt));
  810. bool tag_type_found = false;
  811. for_array(i, bt->Union.variants) {
  812. Type *vt = bt->Union.variants[i];
  813. if (are_types_identical(vt, y.type)) {
  814. tag_type_found = true;
  815. break;
  816. }
  817. }
  818. if (!tag_type_found) {
  819. gbString type_str = type_to_string(y.type);
  820. error(y.expr, "Unknown tag type, got '%s'", type_str);
  821. gb_string_free(type_str);
  822. continue;
  823. }
  824. case_type = y.type;
  825. } else if (switch_kind == TypeSwitch_Any) {
  826. case_type = y.type;
  827. } else {
  828. GB_PANIC("Unknown type to type switch statement");
  829. }
  830. if (ptr_set_exists(&seen, y.type)) {
  831. TokenPos pos = cc->token.pos;
  832. gbString expr_str = expr_to_string(y.expr);
  833. error(y.expr,
  834. "Duplicate type case '%s'\n"
  835. "\tprevious type case at %.*s(%td:%td)",
  836. expr_str,
  837. LIT(pos.file), pos.line, pos.column);
  838. gb_string_free(expr_str);
  839. break;
  840. }
  841. ptr_set_add(&seen, y.type);
  842. }
  843. }
  844. if (is_ptr &&
  845. !is_type_any(type_deref(x.type)) &&
  846. cc->list.count == 1 &&
  847. case_type != nullptr) {
  848. case_type = alloc_type_pointer(case_type);
  849. }
  850. if (cc->list.count > 1) {
  851. case_type = nullptr;
  852. }
  853. if (case_type == nullptr) {
  854. case_type = x.type;
  855. }
  856. add_type_info_type(c, case_type);
  857. check_open_scope(c, stmt);
  858. {
  859. Entity *tag_var = alloc_entity_variable(c->context.scope, lhs->Ident.token, case_type, false, EntityState_Resolved);
  860. tag_var->flags |= EntityFlag_Used;
  861. tag_var->flags |= EntityFlag_Value;
  862. add_entity(c, c->context.scope, lhs, tag_var);
  863. add_entity_use(c, lhs, tag_var);
  864. add_implicit_entity(c, stmt, tag_var);
  865. }
  866. check_stmt_list(c, cc->stmts, mod_flags);
  867. check_close_scope(c);
  868. }
  869. if (complete) {
  870. Type *ut = base_type(x.type);
  871. GB_ASSERT(is_type_union(ut));
  872. auto variants = ut->Union.variants;
  873. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  874. defer (gb_temp_arena_memory_end(tmp));
  875. auto unhandled = array_make<Type *>(c->tmp_allocator, 0, variants.count);
  876. for_array(i, variants) {
  877. Type *t = variants[i];
  878. if (!ptr_set_exists(&seen, t)) {
  879. array_add(&unhandled, t);
  880. }
  881. }
  882. if (unhandled.count > 0) {
  883. if (unhandled.count == 1) {
  884. error_no_newline(node, "Unhandled switch case: ");
  885. } else {
  886. error_no_newline(node, "Unhandled switch cases: ");
  887. }
  888. for_array(i, unhandled) {
  889. Type *t = unhandled[i];
  890. if (i > 0) {
  891. gb_printf_err(", ");
  892. }
  893. gbString s = type_to_string(t);
  894. gb_printf_err("%s", s);
  895. gb_string_free(s);
  896. }
  897. gb_printf_err("\n");
  898. }
  899. }
  900. }
  901. void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
  902. u32 mod_flags = flags & (~Stmt_FallthroughAllowed);
  903. switch (node->kind) {
  904. case_ast_node(_, EmptyStmt, node); case_end;
  905. case_ast_node(_, BadStmt, node); case_end;
  906. case_ast_node(_, BadDecl, node); case_end;
  907. case_ast_node(es, ExprStmt, node)
  908. Operand operand = {Addressing_Invalid};
  909. ExprKind kind = check_expr_base(c, &operand, es->expr, nullptr);
  910. switch (operand.mode) {
  911. case Addressing_Type: {
  912. gbString str = type_to_string(operand.type);
  913. error(node, "'%s' is not an expression", str);
  914. gb_string_free(str);
  915. break;
  916. }
  917. case Addressing_NoValue:
  918. return;
  919. default: {
  920. if (kind == Expr_Stmt) {
  921. return;
  922. }
  923. if (operand.expr->kind == AstNode_CallExpr) {
  924. AstNodeCallExpr *ce = &operand.expr->CallExpr;
  925. Type *t = type_of_expr(&c->info, ce->proc);
  926. if (is_type_proc(t)) {
  927. if (t->Proc.require_results) {
  928. gbString expr_str = expr_to_string(ce->proc);
  929. error(node, "'%s' requires that its results must be handled", expr_str);
  930. gb_string_free(expr_str);
  931. }
  932. }
  933. return;
  934. }
  935. gbString expr_str = expr_to_string(operand.expr);
  936. error(node, "Expression is not used: '%s'", expr_str);
  937. gb_string_free(expr_str);
  938. break;
  939. }
  940. }
  941. case_end;
  942. case_ast_node(ts, TagStmt, node);
  943. // TODO(bill): Tag Statements
  944. error(node, "Tag statements are not supported yet");
  945. check_stmt(c, ts->stmt, flags);
  946. case_end;
  947. case_ast_node(as, AssignStmt, node);
  948. switch (as->op.kind) {
  949. case Token_Eq: {
  950. // a, b, c = 1, 2, 3; // Multisided
  951. isize lhs_count = as->lhs.count;
  952. if (lhs_count == 0) {
  953. error(as->op, "Missing lhs in assignment statement");
  954. return;
  955. }
  956. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
  957. defer (gb_temp_arena_memory_end(tmp));
  958. // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
  959. // an extra allocation
  960. auto lhs_operands = array_make<Operand>(c->tmp_allocator, lhs_count);
  961. auto rhs_operands = array_make<Operand>(c->tmp_allocator, 0, 2*lhs_count);
  962. for_array(i, as->lhs) {
  963. if (is_blank_ident(as->lhs[i])) {
  964. Operand *o = &lhs_operands[i];
  965. o->expr = as->lhs[i];
  966. o->mode = Addressing_Value;
  967. } else {
  968. check_expr(c, &lhs_operands[i], as->lhs[i]);
  969. }
  970. }
  971. check_unpack_arguments(c, nullptr, lhs_operands.count, &rhs_operands, as->rhs, true);
  972. isize rhs_count = rhs_operands.count;
  973. for_array(i, rhs_operands) {
  974. if (rhs_operands[i].mode == Addressing_Invalid) {
  975. rhs_count--;
  976. }
  977. }
  978. isize max = gb_min(lhs_count, rhs_count);
  979. for (isize i = 0; i < max; i++) {
  980. check_assignment_variable(c, &lhs_operands[i], &rhs_operands[i]);
  981. }
  982. if (lhs_count != rhs_count) {
  983. error(as->lhs[0], "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
  984. }
  985. break;
  986. }
  987. default: {
  988. // a += 1; // Single-sided
  989. Token op = as->op;
  990. if (as->lhs.count != 1 || as->rhs.count != 1) {
  991. error(op, "Assignment operation '%.*s' requires single-valued expressions", LIT(op.string));
  992. return;
  993. }
  994. if (!gb_is_between(op.kind, Token__AssignOpBegin+1, Token__AssignOpEnd-1)) {
  995. error(op, "Unknown Assignment operation '%.*s'", LIT(op.string));
  996. return;
  997. }
  998. Operand lhs = {Addressing_Invalid};
  999. Operand rhs = {Addressing_Invalid};
  1000. AstNode binary_expr = {AstNode_BinaryExpr};
  1001. ast_node(be, BinaryExpr, &binary_expr);
  1002. be->op = op;
  1003. be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));
  1004. // NOTE(bill): Only use the first one will be used
  1005. be->left = as->lhs[0];
  1006. be->right = as->rhs[0];
  1007. check_expr(c, &lhs, as->lhs[0]);
  1008. check_binary_expr(c, &rhs, &binary_expr);
  1009. if (rhs.mode == Addressing_Invalid) {
  1010. return;
  1011. }
  1012. // NOTE(bill): Only use the first one will be used
  1013. check_assignment_variable(c, &lhs, &rhs);
  1014. break;
  1015. }
  1016. }
  1017. case_end;
  1018. case_ast_node(bs, BlockStmt, node);
  1019. check_open_scope(c, node);
  1020. check_stmt_list(c, bs->stmts, flags);
  1021. check_close_scope(c);
  1022. case_end;
  1023. case_ast_node(is, IfStmt, node);
  1024. check_open_scope(c, node);
  1025. if (is->init != nullptr) {
  1026. check_stmt(c, is->init, 0);
  1027. }
  1028. Operand operand = {Addressing_Invalid};
  1029. check_expr(c, &operand, is->cond);
  1030. if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
  1031. error(is->cond, "Non-boolean condition in 'if' statement");
  1032. }
  1033. check_stmt(c, is->body, mod_flags);
  1034. if (is->else_stmt != nullptr) {
  1035. switch (is->else_stmt->kind) {
  1036. case AstNode_IfStmt:
  1037. case AstNode_BlockStmt:
  1038. check_stmt(c, is->else_stmt, mod_flags);
  1039. break;
  1040. default:
  1041. error(is->else_stmt, "Invalid 'else' statement in 'if' statement");
  1042. break;
  1043. }
  1044. }
  1045. check_close_scope(c);
  1046. case_end;
  1047. case_ast_node(ws, WhenStmt, node);
  1048. check_when_stmt(c, ws, flags);
  1049. case_end;
  1050. case_ast_node(rs, ReturnStmt, node);
  1051. GB_ASSERT(c->proc_stack.count > 0);
  1052. if (c->context.in_defer) {
  1053. error(rs->token, "You cannot 'return' within a defer statement");
  1054. break;
  1055. }
  1056. Type *proc_type = c->proc_stack[c->proc_stack.count-1];
  1057. TypeProc *pt = &proc_type->Proc;
  1058. isize result_count = 0;
  1059. bool has_named_results = pt->has_named_results;
  1060. if (pt->results) {
  1061. result_count = proc_type->Proc.results->Tuple.variables.count;
  1062. }
  1063. auto operands = array_make<Operand>(heap_allocator(), 0, 2*rs->results.count);
  1064. defer (array_free(&operands));
  1065. check_unpack_arguments(c, nullptr, -1, &operands, rs->results, false);
  1066. if (result_count == 0 && rs->results.count > 0) {
  1067. error(rs->results[0], "No return values expected");
  1068. } else if (has_named_results && operands.count == 0) {
  1069. // Okay
  1070. } else if (operands.count != result_count) {
  1071. error(node, "Expected %td return values, got %td", result_count, operands.count);
  1072. } else {
  1073. isize max_count = rs->results.count;
  1074. for (isize i = 0; i < max_count; i++) {
  1075. Entity *e = pt->results->Tuple.variables[i];
  1076. check_assignment(c, &operands[i], e->type, str_lit("return statement"));
  1077. }
  1078. }
  1079. case_end;
  1080. case_ast_node(fs, ForStmt, node);
  1081. u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
  1082. check_open_scope(c, node);
  1083. check_label(c, fs->label); // TODO(bill): What should the label's "scope" be?
  1084. if (fs->init != nullptr) {
  1085. check_stmt(c, fs->init, 0);
  1086. }
  1087. if (fs->cond != nullptr) {
  1088. Operand o = {Addressing_Invalid};
  1089. check_expr(c, &o, fs->cond);
  1090. if (o.mode != Addressing_Invalid && !is_type_boolean(o.type)) {
  1091. error(fs->cond, "Non-boolean condition in 'for' statement");
  1092. }
  1093. }
  1094. if (fs->post != nullptr) {
  1095. check_stmt(c, fs->post, 0);
  1096. if (fs->post->kind != AstNode_AssignStmt &&
  1097. fs->post->kind != AstNode_IncDecStmt) {
  1098. error(fs->post, "'for' statement post statement must be a simple statement");
  1099. }
  1100. }
  1101. check_stmt(c, fs->body, new_flags);
  1102. check_close_scope(c);
  1103. case_end;
  1104. case_ast_node(rs, RangeStmt, node);
  1105. u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
  1106. check_open_scope(c, node);
  1107. check_label(c, rs->label);
  1108. Type *val0 = nullptr;
  1109. Type *val1 = nullptr;
  1110. Entity *entities[2] = {};
  1111. isize entity_count = 0;
  1112. bool is_map = false;
  1113. AstNode *expr = unparen_expr(rs->expr);
  1114. if (is_ast_node_a_range(expr)) {
  1115. ast_node(ie, BinaryExpr, expr);
  1116. Operand x = {Addressing_Invalid};
  1117. Operand y = {Addressing_Invalid};
  1118. check_expr(c, &x, ie->left);
  1119. if (x.mode == Addressing_Invalid) {
  1120. goto skip_expr;
  1121. }
  1122. check_expr(c, &y, ie->right);
  1123. if (y.mode == Addressing_Invalid) {
  1124. goto skip_expr;
  1125. }
  1126. convert_to_typed(c, &x, y.type);
  1127. if (x.mode == Addressing_Invalid) {
  1128. goto skip_expr;
  1129. }
  1130. convert_to_typed(c, &y, x.type);
  1131. if (y.mode == Addressing_Invalid) {
  1132. goto skip_expr;
  1133. }
  1134. convert_to_typed(c, &x, default_type(y.type));
  1135. if (x.mode == Addressing_Invalid) {
  1136. goto skip_expr;
  1137. }
  1138. convert_to_typed(c, &y, default_type(x.type));
  1139. if (y.mode == Addressing_Invalid) {
  1140. goto skip_expr;
  1141. }
  1142. if (!are_types_identical(x.type, y.type)) {
  1143. if (x.type != t_invalid &&
  1144. y.type != t_invalid) {
  1145. gbString xt = type_to_string(x.type);
  1146. gbString yt = type_to_string(y.type);
  1147. gbString expr_str = expr_to_string(x.expr);
  1148. error(ie->op, "Mismatched types in interval expression '%s' : '%s' vs '%s'", expr_str, xt, yt);
  1149. gb_string_free(expr_str);
  1150. gb_string_free(yt);
  1151. gb_string_free(xt);
  1152. }
  1153. goto skip_expr;
  1154. }
  1155. Type *type = x.type;
  1156. if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type)) {
  1157. error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
  1158. goto skip_expr;
  1159. }
  1160. if (x.mode == Addressing_Constant &&
  1161. y.mode == Addressing_Constant) {
  1162. ExactValue a = x.value;
  1163. ExactValue b = y.value;
  1164. GB_ASSERT(are_types_identical(x.type, y.type));
  1165. TokenKind op = Token_Lt;
  1166. switch (ie->op.kind) {
  1167. case Token_Ellipsis: op = Token_LtEq; break;
  1168. case Token_HalfClosed: op = Token_Lt; break;
  1169. default: error(ie->op, "Invalid range operator"); break;
  1170. }
  1171. bool ok = compare_exact_values(op, a, b);
  1172. if (!ok) {
  1173. // TODO(bill): Better error message
  1174. error(ie->op, "Invalid interval range");
  1175. goto skip_expr;
  1176. }
  1177. }
  1178. if (x.mode != Addressing_Constant) {
  1179. x.value = empty_exact_value;
  1180. }
  1181. if (y.mode != Addressing_Constant) {
  1182. y.value = empty_exact_value;
  1183. }
  1184. add_type_and_value(&c->info, ie->left, x.mode, x.type, x.value);
  1185. add_type_and_value(&c->info, ie->right, y.mode, y.type, y.value);
  1186. val0 = type;
  1187. val1 = t_int;
  1188. } else {
  1189. Operand operand = {Addressing_Invalid};
  1190. check_expr_or_type(c, &operand, rs->expr);
  1191. if (operand.mode == Addressing_Type) {
  1192. if (!is_type_enum(operand.type)) {
  1193. gbString t = type_to_string(operand.type);
  1194. error(operand.expr, "Cannot iterate over the type '%s'", t);
  1195. gb_string_free(t);
  1196. goto skip_expr;
  1197. } else {
  1198. val0 = operand.type;
  1199. val1 = t_int;
  1200. add_type_info_type(c, operand.type);
  1201. goto skip_expr;
  1202. }
  1203. } else if (operand.mode != Addressing_Invalid) {
  1204. bool is_ptr = is_type_pointer(operand.type);
  1205. Type *t = base_type(type_deref(operand.type));
  1206. switch (t->kind) {
  1207. case Type_Basic:
  1208. if (is_type_string(t)) {
  1209. val0 = t_rune;
  1210. val1 = t_int;
  1211. add_preload_dependency(c, "__string_decode_rune");
  1212. }
  1213. break;
  1214. case Type_Array:
  1215. val0 = t->Array.elem;
  1216. val1 = t_int;
  1217. break;
  1218. case Type_DynamicArray:
  1219. val0 = t->DynamicArray.elem;
  1220. val1 = t_int;
  1221. break;
  1222. case Type_Slice:
  1223. val0 = t->Slice.elem;
  1224. val1 = t_int;
  1225. break;
  1226. case Type_Map:
  1227. is_map = true;
  1228. val0 = t->Map.key;
  1229. val1 = t->Map.value;
  1230. break;
  1231. }
  1232. }
  1233. if (val0 == nullptr) {
  1234. gbString s = expr_to_string(operand.expr);
  1235. gbString t = type_to_string(operand.type);
  1236. error(operand.expr, "Cannot iterate over '%s' of type '%s'", s, t);
  1237. gb_string_free(t);
  1238. gb_string_free(s);
  1239. }
  1240. }
  1241. skip_expr:; // NOTE(zhiayang): again, declaring a variable immediately after a label... weird.
  1242. AstNode *lhs[2] = {rs->val0, rs->val1};
  1243. Type * rhs[2] = {val0, val1};
  1244. for (isize i = 0; i < 2; i++) {
  1245. if (lhs[i] == nullptr) {
  1246. continue;
  1247. }
  1248. AstNode *name = lhs[i];
  1249. Type * type = rhs[i];
  1250. Entity *entity = nullptr;
  1251. if (name->kind == AstNode_Ident) {
  1252. Token token = name->Ident.token;
  1253. String str = token.string;
  1254. Entity *found = nullptr;
  1255. if (!is_blank_ident(str)) {
  1256. found = current_scope_lookup_entity(c->context.scope, str);
  1257. }
  1258. if (found == nullptr) {
  1259. bool is_immutable = true;
  1260. entity = alloc_entity_variable(c->context.scope, token, type, is_immutable, EntityState_Resolved);
  1261. add_entity_definition(&c->info, name, entity);
  1262. } else {
  1263. TokenPos pos = found->token.pos;
  1264. error(token,
  1265. "Redeclaration of '%.*s' in this scope\n"
  1266. "\tat %.*s(%td:%td)",
  1267. LIT(str), LIT(pos.file), pos.line, pos.column);
  1268. entity = found;
  1269. }
  1270. } else {
  1271. error(name, "A variable declaration must be an identifier");
  1272. }
  1273. if (entity == nullptr) {
  1274. entity = alloc_entity_dummy_variable(c->global_scope, ast_node_token(name));
  1275. }
  1276. entities[entity_count++] = entity;
  1277. if (type == nullptr) {
  1278. entity->type = t_invalid;
  1279. entity->flags |= EntityFlag_Used;
  1280. }
  1281. }
  1282. for (isize i = 0; i < entity_count; i++) {
  1283. add_entity(c, c->context.scope, entities[i]->identifier, entities[i]);
  1284. }
  1285. check_stmt(c, rs->body, new_flags);
  1286. check_close_scope(c);
  1287. case_end;
  1288. case_ast_node(ss, SwitchStmt, node);
  1289. check_switch_stmt(c, node, mod_flags);
  1290. case_end;
  1291. case_ast_node(ss, TypeSwitchStmt, node);
  1292. check_type_switch_stmt(c, node, mod_flags);
  1293. case_end;
  1294. case_ast_node(ds, DeferStmt, node);
  1295. if (is_ast_node_decl(ds->stmt)) {
  1296. error(ds->token, "You cannot defer a declaration");
  1297. } else {
  1298. bool out_in_defer = c->context.in_defer;
  1299. c->context.in_defer = true;
  1300. check_stmt(c, ds->stmt, 0);
  1301. c->context.in_defer = out_in_defer;
  1302. }
  1303. case_end;
  1304. case_ast_node(bs, BranchStmt, node);
  1305. Token token = bs->token;
  1306. switch (token.kind) {
  1307. case Token_break:
  1308. if ((flags & Stmt_BreakAllowed) == 0) {
  1309. error(token, "'break' only allowed in loops or 'switch' statements");
  1310. }
  1311. break;
  1312. case Token_continue:
  1313. if ((flags & Stmt_ContinueAllowed) == 0) {
  1314. error(token, "'continue' only allowed in loops");
  1315. }
  1316. break;
  1317. case Token_fallthrough:
  1318. if ((flags & Stmt_FallthroughAllowed) == 0) {
  1319. error(token, "'fallthrough' statement in illegal position, expected at the end of a 'case' block");
  1320. }
  1321. break;
  1322. default:
  1323. error(token, "Invalid AST: Branch Statement '%.*s'", LIT(token.string));
  1324. break;
  1325. }
  1326. if (bs->label != nullptr) {
  1327. if (bs->label->kind != AstNode_Ident) {
  1328. error(bs->label, "A branch statement's label name must be an identifier");
  1329. return;
  1330. }
  1331. AstNode *ident = bs->label;
  1332. String name = ident->Ident.token.string;
  1333. Operand o = {};
  1334. Entity *e = check_ident(c, &o, ident, nullptr, nullptr, false);
  1335. if (e == nullptr) {
  1336. error(ident, "Undeclared label name: %.*s", LIT(name));
  1337. return;
  1338. }
  1339. add_entity_use(c, ident, e);
  1340. if (e->kind != Entity_Label) {
  1341. error(ident, "'%.*s' is not a label", LIT(name));
  1342. return;
  1343. }
  1344. }
  1345. case_end;
  1346. case_ast_node(us, UsingStmt, node);
  1347. if (us->list.count == 0) {
  1348. error(us->token, "Empty 'using' list");
  1349. return;
  1350. }
  1351. for_array(i, us->list) {
  1352. AstNode *expr = unparen_expr(us->list[0]);
  1353. Entity *e = nullptr;
  1354. bool is_selector = false;
  1355. Operand o = {};
  1356. switch (expr->kind) {
  1357. case AstNode_Ident:
  1358. e = check_ident(c, &o, expr, nullptr, nullptr, true);
  1359. break;
  1360. case AstNode_SelectorExpr:
  1361. e = check_selector(c, &o, expr, nullptr);
  1362. is_selector = true;
  1363. break;
  1364. case AstNode_Implicit:
  1365. error(us->token, "'using' applied to an implicit value");
  1366. continue;
  1367. default:
  1368. error(us->token, "'using' can only be applied to an entity, got %.*s", LIT(ast_node_strings[expr->kind]));
  1369. continue;
  1370. }
  1371. if (!check_using_stmt_entity(c, us, expr, is_selector, e)) {
  1372. return;
  1373. }
  1374. }
  1375. case_end;
  1376. case_ast_node(uis, UsingInStmt, node);
  1377. if (uis->list.count == 0) {
  1378. error(node, "Empty 'using' list");
  1379. return;
  1380. }
  1381. AstNode *expr = uis->expr;
  1382. Entity *e = nullptr;
  1383. Operand o = {};
  1384. if (expr->kind == AstNode_Ident) {
  1385. e = check_ident(c, &o, expr, nullptr, nullptr, true);
  1386. } else if (expr->kind == AstNode_SelectorExpr) {
  1387. e = check_selector(c, &o, expr, nullptr);
  1388. }
  1389. if (e == nullptr) {
  1390. error(expr, "'using' applied to an unknown entity");
  1391. return;
  1392. }
  1393. add_entity_use(c, expr, e);
  1394. switch (e->kind) {
  1395. case Entity_TypeName: {
  1396. Type *t = base_type(e->type);
  1397. if (t->kind == Type_Enum) {
  1398. GB_ASSERT(t->Enum.scope != nullptr);
  1399. for_array(list_index, uis->list) {
  1400. AstNode *node = uis->list[list_index];
  1401. ast_node(ident, Ident, node);
  1402. String name = ident->token.string;
  1403. Entity *f = scope_lookup_entity(t->Enum.scope, name);
  1404. if (f == nullptr || !is_entity_exported(f)) {
  1405. if (is_blank_ident(name)) {
  1406. error(node, "'_' cannot be used as a value");
  1407. } else {
  1408. error(node, "Undeclared name in this enumeration: '%.*s'", LIT(name));
  1409. }
  1410. continue;
  1411. }
  1412. add_entity_use(c, node, f);
  1413. add_entity(c, c->context.scope, node, f);
  1414. }
  1415. } else {
  1416. error(node, "'using' can be only applied to enum type entities");
  1417. }
  1418. break;
  1419. }
  1420. case Entity_ImportName: {
  1421. Scope *scope = e->ImportName.scope;
  1422. for_array(list_index, uis->list) {
  1423. AstNode *node = uis->list[list_index];
  1424. ast_node(ident, Ident, node);
  1425. String name = ident->token.string;
  1426. Entity *f = scope_lookup_entity(scope, name);
  1427. if (f == nullptr) {
  1428. if (is_blank_ident(name)) {
  1429. error(node, "'_' cannot be used as a value");
  1430. } else {
  1431. error(node, "Undeclared name in this import name: '%.*s'", LIT(name));
  1432. }
  1433. continue;
  1434. }
  1435. bool implicit_is_found = ptr_set_exists(&scope->implicit, f);
  1436. if (is_entity_exported(f) && !implicit_is_found) {
  1437. add_entity_use(c, node, f);
  1438. add_entity(c, c->context.scope, node, f);
  1439. } else {
  1440. error(node, "'%.*s' is exported from '%.*s'", LIT(f->token.string), LIT(e->token.string));
  1441. continue;
  1442. }
  1443. }
  1444. break;
  1445. }
  1446. case Entity_Variable: {
  1447. Type *t = base_type(type_deref(e->type));
  1448. if (t->kind == Type_Struct) {
  1449. // TODO(bill): Make it work for unions too
  1450. Scope *found = scope_of_node(&c->info, t->Struct.node);
  1451. for_array(list_index, uis->list) {
  1452. AstNode *node = uis->list[list_index];
  1453. ast_node(ident, Ident, node);
  1454. String name = ident->token.string;
  1455. Entity *f = scope_lookup_entity(found, name);
  1456. if (f == nullptr || f->kind != Entity_Variable) {
  1457. if (is_blank_ident(name)) {
  1458. error(node, "'_' cannot be used as a value");
  1459. } else {
  1460. error(node, "Undeclared name in this variable: '%.*s'", LIT(name));
  1461. }
  1462. continue;
  1463. }
  1464. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type);
  1465. uvar->using_expr = expr;
  1466. Entity *prev = scope_insert_entity(c->context.scope, uvar);
  1467. if (prev != nullptr) {
  1468. gbString expr_str = expr_to_string(expr);
  1469. error(node, "Namespace collision while using '%s' of: '%.*s'", expr_str, LIT(prev->token.string));
  1470. gb_string_free(expr_str);
  1471. continue;
  1472. }
  1473. }
  1474. } else {
  1475. error(node, "'using' can only be applied to variables of type `struct`");
  1476. return;
  1477. }
  1478. break;
  1479. }
  1480. case Entity_Constant:
  1481. error(node, "'using' cannot be applied to a constant");
  1482. break;
  1483. case Entity_Procedure:
  1484. case Entity_ProcGroup:
  1485. case Entity_Builtin:
  1486. error(node, "'using' cannot be applied to a procedure");
  1487. break;
  1488. case Entity_Nil:
  1489. error(node, "'using' cannot be applied to 'nil'");
  1490. break;
  1491. case Entity_Label:
  1492. error(node, "'using' cannot be applied to a label");
  1493. break;
  1494. case Entity_Invalid:
  1495. error(node, "'using' cannot be applied to an invalid entity");
  1496. break;
  1497. default:
  1498. GB_PANIC("TODO(bill): 'using' other expressions?");
  1499. }
  1500. case_end;
  1501. case_ast_node(pa, PushContext, node);
  1502. Operand op = {};
  1503. check_expr(c, &op, pa->expr);
  1504. check_assignment(c, &op, t_context, str_lit("argument to context <-"));
  1505. check_stmt(c, pa->body, mod_flags);
  1506. case_end;
  1507. case_ast_node(fb, ForeignBlockDecl, node);
  1508. AstNode *foreign_library = fb->foreign_library;
  1509. CheckerContext prev_context = c->context;
  1510. defer (c->context = prev_context);
  1511. if (foreign_library->kind != AstNode_Ident) {
  1512. error(foreign_library, "foreign library name must be an identifier");
  1513. } else {
  1514. c->context.foreign_context.curr_library = foreign_library;
  1515. c->context.foreign_context.default_cc = ProcCC_CDecl;
  1516. }
  1517. check_decl_attributes(c, fb->attributes, foreign_block_decl_attribute, nullptr);
  1518. for_array(i, fb->decls) {
  1519. AstNode *decl = fb->decls[i];
  1520. if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) {
  1521. check_stmt(c, decl, flags);
  1522. }
  1523. }
  1524. case_end;
  1525. case_ast_node(vd, ValueDecl, node);
  1526. if (vd->is_mutable) {
  1527. Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count);
  1528. isize entity_count = 0;
  1529. isize new_name_count = 0;
  1530. for_array(i, vd->names) {
  1531. AstNode *name = vd->names[i];
  1532. Entity *entity = nullptr;
  1533. if (name->kind != AstNode_Ident) {
  1534. error(name, "A variable declaration must be an identifier");
  1535. } else {
  1536. Token token = name->Ident.token;
  1537. String str = token.string;
  1538. Entity *found = nullptr;
  1539. // NOTE(bill): Ignore assignments to '_'
  1540. if (!is_blank_ident(str)) {
  1541. found = current_scope_lookup_entity(c->context.scope, str);
  1542. new_name_count += 1;
  1543. }
  1544. if (found == nullptr) {
  1545. entity = alloc_entity_variable(c->context.scope, token, nullptr, false);
  1546. entity->identifier = name;
  1547. AstNode *fl = c->context.foreign_context.curr_library;
  1548. if (fl != nullptr) {
  1549. GB_ASSERT(fl->kind == AstNode_Ident);
  1550. entity->Variable.is_foreign = true;
  1551. entity->Variable.foreign_library_ident = fl;
  1552. }
  1553. } else {
  1554. TokenPos pos = found->token.pos;
  1555. error(token,
  1556. "Redeclaration of '%.*s' in this scope\n"
  1557. "\tat %.*s(%td:%td)",
  1558. LIT(str), LIT(pos.file), pos.line, pos.column);
  1559. entity = found;
  1560. }
  1561. }
  1562. if (entity == nullptr) {
  1563. entity = alloc_entity_dummy_variable(c->global_scope, ast_node_token(name));
  1564. }
  1565. entity->parent_proc_decl = c->context.curr_proc_decl;
  1566. entities[entity_count++] = entity;
  1567. }
  1568. if (new_name_count == 0) {
  1569. error(node, "No new declarations on the lhs");
  1570. }
  1571. Type *init_type = nullptr;
  1572. if (vd->type != nullptr) {
  1573. init_type = check_type(c, vd->type);
  1574. if (init_type == nullptr) {
  1575. init_type = t_invalid;
  1576. } else if (is_type_polymorphic(base_type(init_type))) {
  1577. gbString str = type_to_string(init_type);
  1578. error(vd->type, "Invalid use of a polymorphic type '%s' in variable declaration", str);
  1579. gb_string_free(str);
  1580. init_type = t_invalid;
  1581. } else if (is_type_empty_union(init_type)) {
  1582. gbString str = type_to_string(init_type);
  1583. error(vd->type, "An empty union '%s' cannot be instantiated in variable declaration", str);
  1584. gb_string_free(str);
  1585. init_type = t_invalid;
  1586. }
  1587. }
  1588. // TODO NOTE(bill): This technically checks things multple times
  1589. AttributeContext ac = make_attribute_context(c->context.foreign_context.link_prefix);
  1590. check_decl_attributes(c, vd->attributes, var_decl_attribute, &ac);
  1591. for (isize i = 0; i < entity_count; i++) {
  1592. Entity *e = entities[i];
  1593. GB_ASSERT(e != nullptr);
  1594. if (e->flags & EntityFlag_Visited) {
  1595. e->type = t_invalid;
  1596. continue;
  1597. }
  1598. e->flags |= EntityFlag_Visited;
  1599. e->state = EntityState_InProgress;
  1600. if (e->type == nullptr) {
  1601. e->type = init_type;
  1602. e->state = EntityState_Resolved;
  1603. }
  1604. ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix);
  1605. e->Variable.thread_local_model = ac.thread_local_model;
  1606. if (ac.link_name.len > 0) {
  1607. e->Variable.link_name = ac.link_name;
  1608. }
  1609. }
  1610. check_arity_match(c, vd);
  1611. check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
  1612. for (isize i = 0; i < entity_count; i++) {
  1613. Entity *e = entities[i];
  1614. if (e->Variable.is_foreign) {
  1615. if (vd->values.count > 0) {
  1616. error(e->token, "A foreign variable declaration cannot have a default value");
  1617. }
  1618. String name = e->token.string;
  1619. if (e->Variable.link_name.len > 0) {
  1620. name = e->Variable.link_name;
  1621. }
  1622. if (vd->values.count > 0) {
  1623. error(e->token, "A foreign variable declaration cannot have a default value");
  1624. }
  1625. init_entity_foreign_library(c, e);
  1626. auto *fp = &c->info.foreigns;
  1627. HashKey key = hash_string(name);
  1628. Entity **found = map_get(fp, key);
  1629. if (found) {
  1630. Entity *f = *found;
  1631. TokenPos pos = f->token.pos;
  1632. Type *this_type = base_type(e->type);
  1633. Type *other_type = base_type(f->type);
  1634. if (!are_types_identical(this_type, other_type)) {
  1635. error(e->token,
  1636. "Foreign entity '%.*s' previously declared elsewhere with a different type\n"
  1637. "\tat %.*s(%td:%td)",
  1638. LIT(name), LIT(pos.file), pos.line, pos.column);
  1639. }
  1640. } else {
  1641. map_set(fp, key, e);
  1642. }
  1643. }
  1644. add_entity(c, c->context.scope, e->identifier, e);
  1645. }
  1646. if (vd->is_using != 0) {
  1647. Token token = ast_node_token(node);
  1648. if (vd->type != nullptr && entity_count > 1) {
  1649. error(token, "'using' can only be applied to one variable of the same type");
  1650. // TODO(bill): Should a 'continue' happen here?
  1651. }
  1652. for (isize entity_index = 0; entity_index < entity_count; entity_index++) {
  1653. Entity *e = entities[entity_index];
  1654. if (e == nullptr) {
  1655. continue;
  1656. }
  1657. if (e->kind != Entity_Variable) {
  1658. continue;
  1659. }
  1660. bool is_immutable = e->Variable.is_immutable;
  1661. String name = e->token.string;
  1662. Type *t = base_type(type_deref(e->type));
  1663. if (is_blank_ident(name)) {
  1664. error(token, "'using' cannot be applied variable declared as '_'");
  1665. } else if (is_type_struct(t) || is_type_raw_union(t)) {
  1666. Scope *scope = scope_of_node(&c->info, t->Struct.node);
  1667. for_array(i, scope->elements.entries) {
  1668. Entity *f = scope->elements.entries[i].value;
  1669. if (f->kind == Entity_Variable) {
  1670. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type);
  1671. uvar->Variable.is_immutable = is_immutable;
  1672. Entity *prev = scope_insert_entity(c->context.scope, uvar);
  1673. if (prev != nullptr) {
  1674. error(token, "Namespace collision while 'using' '%.*s' of: %.*s", LIT(name), LIT(prev->token.string));
  1675. return;
  1676. }
  1677. }
  1678. }
  1679. } else {
  1680. // NOTE(bill): skip the rest to remove extra errors
  1681. error(token, "'using' can only be applied to variables of type struct or raw_union");
  1682. return;
  1683. }
  1684. }
  1685. }
  1686. } else {
  1687. // constant value declarations
  1688. }
  1689. case_end;
  1690. }
  1691. }