check_stmt.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. gb_internal bool is_diverging_expr(Ast *expr) {
  2. expr = unparen_expr(expr);
  3. if (expr->kind != Ast_CallExpr) {
  4. return false;
  5. }
  6. if (expr->CallExpr.proc->kind == Ast_BasicDirective) {
  7. String name = expr->CallExpr.proc->BasicDirective.name.string;
  8. return name == "panic";
  9. }
  10. Ast *proc = unparen_expr(expr->CallExpr.proc);
  11. TypeAndValue tv = proc->tav;
  12. if (tv.mode == Addressing_Builtin) {
  13. Entity *e = entity_of_node(proc);
  14. BuiltinProcId id = BuiltinProc_Invalid;
  15. if (e != nullptr) {
  16. id = cast(BuiltinProcId)e->Builtin.id;
  17. } else {
  18. id = BuiltinProc_DIRECTIVE;
  19. }
  20. return builtin_procs[id].diverging;
  21. }
  22. Type *t = tv.type;
  23. t = base_type(t);
  24. return t != nullptr && t->kind == Type_Proc && t->Proc.diverging;
  25. }
  26. gb_internal bool is_diverging_stmt(Ast *stmt) {
  27. if (stmt->kind != Ast_ExprStmt) {
  28. return false;
  29. }
  30. return is_diverging_expr(stmt->ExprStmt.expr);
  31. }
  32. gb_internal bool contains_deferred_call(Ast *node) {
  33. if (node->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) {
  34. return true;
  35. }
  36. switch (node->kind) {
  37. case Ast_ExprStmt:
  38. return contains_deferred_call(node->ExprStmt.expr);
  39. case Ast_AssignStmt:
  40. for_array(i, node->AssignStmt.rhs) {
  41. if (contains_deferred_call(node->AssignStmt.rhs[i])) {
  42. return true;
  43. }
  44. }
  45. for_array(i, node->AssignStmt.lhs) {
  46. if (contains_deferred_call(node->AssignStmt.lhs[i])) {
  47. return true;
  48. }
  49. }
  50. break;
  51. case Ast_ValueDecl:
  52. for_array(i, node->ValueDecl.values) {
  53. if (contains_deferred_call(node->ValueDecl.values[i])) {
  54. return true;
  55. }
  56. }
  57. break;
  58. }
  59. return false;
  60. }
  61. gb_internal void check_stmt_list(CheckerContext *ctx, Slice<Ast *> const &stmts, u32 flags) {
  62. if (stmts.count == 0) {
  63. return;
  64. }
  65. if (flags&Stmt_CheckScopeDecls) {
  66. check_scope_decls(ctx, stmts, cast(isize)(1.2*stmts.count));
  67. }
  68. bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0;
  69. flags &= ~Stmt_FallthroughAllowed;
  70. isize max = stmts.count;
  71. for (isize i = stmts.count-1; i >= 0; i--) {
  72. if (stmts[i]->kind != Ast_EmptyStmt) {
  73. break;
  74. }
  75. max--;
  76. }
  77. isize max_non_constant_declaration = stmts.count;
  78. for (isize i = stmts.count-1; i >= 0; i--) {
  79. if (stmts[i]->kind == Ast_EmptyStmt) {
  80. // Okay
  81. } else if (stmts[i]->kind == Ast_ValueDecl && !stmts[i]->ValueDecl.is_mutable) {
  82. // Okay
  83. } else {
  84. break;
  85. }
  86. max_non_constant_declaration--;
  87. }
  88. for (isize i = 0; i < max; i++) {
  89. Ast *n = stmts[i];
  90. if (n->kind == Ast_EmptyStmt) {
  91. continue;
  92. }
  93. u32 new_flags = flags;
  94. if (ft_ok && i+1 == max) {
  95. new_flags |= Stmt_FallthroughAllowed;
  96. }
  97. check_stmt(ctx, n, new_flags);
  98. if (i+1 < max_non_constant_declaration) {
  99. switch (n->kind) {
  100. case Ast_ReturnStmt:
  101. error(n, "Statements after this 'return' are never executed");
  102. break;
  103. case Ast_BranchStmt:
  104. error(n, "Statements after this '%.*s' are never executed", LIT(n->BranchStmt.token.string));
  105. break;
  106. case Ast_ExprStmt:
  107. if (is_diverging_stmt(n)) {
  108. error(n, "Statements after a diverging procedure call are never executed");
  109. }
  110. break;
  111. }
  112. } else if (i+1 == max_non_constant_declaration) {
  113. if (is_diverging_stmt(n)) {
  114. for (isize j = 0; j < i; j++) {
  115. Ast *stmt = stmts[j];
  116. if (stmt->kind == Ast_ValueDecl && !stmt->ValueDecl.is_mutable) {
  117. } else if (stmt->kind == Ast_DeferStmt) {
  118. error(stmt, "Unreachable defer statement due to diverging procedure call at the end of the current scope");
  119. } else if (contains_deferred_call(stmt)) {
  120. error(stmt, "Unreachable deferred procedure call due to a diverging procedure call at the end of the current scope");
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. gb_internal bool check_is_terminating_list(Slice<Ast *> const &stmts, String const &label) {
  128. // Iterate backwards
  129. for (isize n = stmts.count-1; n >= 0; n--) {
  130. Ast *stmt = stmts[n];
  131. if (stmt->kind == Ast_EmptyStmt) {
  132. // Okay
  133. } else if (stmt->kind == Ast_ValueDecl && !stmt->ValueDecl.is_mutable) {
  134. // Okay
  135. } else if (is_diverging_stmt(stmt)) {
  136. return true;
  137. } else {
  138. return check_is_terminating(stmt, label);
  139. }
  140. }
  141. return false;
  142. }
  143. gb_internal bool check_has_break_list(Slice<Ast *> const &stmts, String const &label, bool implicit) {
  144. for_array(i, stmts) {
  145. Ast *stmt = stmts[i];
  146. if (check_has_break(stmt, label, implicit)) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. gb_internal bool check_has_break(Ast *stmt, String const &label, bool implicit) {
  153. switch (stmt->kind) {
  154. case Ast_BranchStmt:
  155. if (stmt->BranchStmt.token.kind == Token_break) {
  156. if (stmt->BranchStmt.label == nullptr) {
  157. return implicit;
  158. }
  159. if (stmt->BranchStmt.label->kind == Ast_Ident &&
  160. stmt->BranchStmt.label->Ident.token.string == label) {
  161. return true;
  162. }
  163. }
  164. break;
  165. case Ast_BlockStmt:
  166. return check_has_break_list(stmt->BlockStmt.stmts, label, implicit);
  167. case Ast_IfStmt:
  168. if (check_has_break(stmt->IfStmt.body, label, implicit) ||
  169. (stmt->IfStmt.else_stmt != nullptr && check_has_break(stmt->IfStmt.else_stmt, label, implicit))) {
  170. return true;
  171. }
  172. break;
  173. case Ast_CaseClause:
  174. return check_has_break_list(stmt->CaseClause.stmts, label, implicit);
  175. case Ast_SwitchStmt:
  176. if (label != "" && check_has_break(stmt->SwitchStmt.body, label, false)) {
  177. return true;
  178. }
  179. break;
  180. case Ast_TypeSwitchStmt:
  181. if (label != "" && check_has_break(stmt->TypeSwitchStmt.body, label, false)) {
  182. return true;
  183. }
  184. break;
  185. case Ast_ForStmt:
  186. if (label != "" && check_has_break(stmt->ForStmt.body, label, false)) {
  187. return true;
  188. }
  189. break;
  190. case Ast_RangeStmt:
  191. if (label != "" && check_has_break(stmt->RangeStmt.body, label, false)) {
  192. return true;
  193. }
  194. break;
  195. }
  196. return false;
  197. }
  198. // NOTE(bill): The last expression has to be a 'return' statement
  199. // TODO(bill): This is a mild hack and should be probably handled properly
  200. gb_internal bool check_is_terminating(Ast *node, String const &label) {
  201. switch (node->kind) {
  202. case_ast_node(rs, ReturnStmt, node);
  203. return true;
  204. case_end;
  205. case_ast_node(bs, BlockStmt, node);
  206. return check_is_terminating_list(bs->stmts, label);
  207. case_end;
  208. case_ast_node(es, ExprStmt, node);
  209. return check_is_terminating(unparen_expr(es->expr), label);
  210. case_end;
  211. case_ast_node(bs, BranchStmt, node);
  212. return bs->token.kind == Token_fallthrough;
  213. case_end;
  214. case_ast_node(is, IfStmt, node);
  215. if (is->else_stmt != nullptr) {
  216. if (check_is_terminating(is->body, label) &&
  217. check_is_terminating(is->else_stmt, label)) {
  218. return true;
  219. }
  220. }
  221. case_end;
  222. case_ast_node(ws, WhenStmt, node);
  223. // TODO(bill): Is this logic correct for when statements?
  224. auto const &tv = ws->cond->tav;
  225. if (tv.mode != Addressing_Constant) {
  226. // NOTE(bill): Check the things regardless as a bug occurred earlier
  227. if (ws->else_stmt != nullptr) {
  228. if (check_is_terminating(ws->body, label) &&
  229. check_is_terminating(ws->else_stmt, label)) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. if (tv.value.kind == ExactValue_Bool) {
  236. if (tv.value.value_bool) {
  237. return check_is_terminating(ws->body, label);
  238. } else {
  239. if (ws->else_stmt == nullptr) {
  240. return false;
  241. }
  242. return check_is_terminating(ws->else_stmt, label);
  243. }
  244. }
  245. case_end;
  246. case_ast_node(fs, ForStmt, node);
  247. if (fs->cond == nullptr && !check_has_break(fs->body, label, true)) {
  248. return true;
  249. }
  250. case_end;
  251. case_ast_node(rs, UnrollRangeStmt, node);
  252. return false;
  253. case_end;
  254. case_ast_node(rs, RangeStmt, node);
  255. return false;
  256. case_end;
  257. case_ast_node(ss, SwitchStmt, node);
  258. bool has_default = false;
  259. for_array(i, ss->body->BlockStmt.stmts) {
  260. Ast *clause = ss->body->BlockStmt.stmts[i];
  261. ast_node(cc, CaseClause, clause);
  262. if (cc->list.count == 0) {
  263. has_default = true;
  264. }
  265. if (!check_is_terminating_list(cc->stmts, label) ||
  266. check_has_break_list(cc->stmts, label, true)) {
  267. return false;
  268. }
  269. }
  270. return has_default;
  271. case_end;
  272. case_ast_node(ss, TypeSwitchStmt, node);
  273. bool has_default = false;
  274. for_array(i, ss->body->BlockStmt.stmts) {
  275. Ast *clause = ss->body->BlockStmt.stmts[i];
  276. ast_node(cc, CaseClause, clause);
  277. if (cc->list.count == 0) {
  278. has_default = true;
  279. }
  280. if (!check_is_terminating_list(cc->stmts, label) ||
  281. check_has_break_list(cc->stmts, label, true)) {
  282. return false;
  283. }
  284. }
  285. return has_default;
  286. case_end;
  287. }
  288. return false;
  289. }
  290. gb_internal Type *check_assignment_variable(CheckerContext *ctx, Operand *lhs, Operand *rhs) {
  291. if (rhs->mode == Addressing_Invalid) {
  292. return nullptr;
  293. }
  294. if (rhs->type == t_invalid &&
  295. rhs->mode != Addressing_ProcGroup &&
  296. rhs->mode != Addressing_Builtin) {
  297. return nullptr;
  298. }
  299. Ast *node = unparen_expr(lhs->expr);
  300. check_no_copy_assignment(*rhs, str_lit("assignment"));
  301. // NOTE(bill): Ignore assignments to '_'
  302. if (is_blank_ident(node)) {
  303. check_assignment(ctx, rhs, nullptr, str_lit("assignment to '_' identifier"));
  304. if (rhs->mode == Addressing_Invalid) {
  305. return nullptr;
  306. }
  307. return rhs->type;
  308. }
  309. Entity *e = nullptr;
  310. bool used = false;
  311. if (lhs->mode == Addressing_Invalid ||
  312. (lhs->type == t_invalid &&
  313. lhs->mode != Addressing_ProcGroup &&
  314. lhs->mode != Addressing_Builtin)) {
  315. return nullptr;
  316. }
  317. if (rhs->mode == Addressing_ProcGroup) {
  318. Array<Entity *> procs = proc_group_entities(ctx, *rhs);
  319. GB_ASSERT(procs.count > 0);
  320. // NOTE(bill): These should be done
  321. for_array(i, procs) {
  322. Type *t = base_type(procs[i]->type);
  323. if (t == t_invalid) {
  324. continue;
  325. }
  326. Operand x = {};
  327. x.mode = Addressing_Value;
  328. x.type = t;
  329. if (check_is_assignable_to(ctx, &x, lhs->type)) {
  330. e = procs[i];
  331. add_entity_use(ctx, rhs->expr, e);
  332. break;
  333. }
  334. }
  335. if (e != nullptr) {
  336. // HACK TODO(bill): Should the entities be freed as it's technically a leak
  337. rhs->mode = Addressing_Value;
  338. rhs->type = e->type;
  339. rhs->proc_group = nullptr;
  340. }
  341. } else {
  342. if (node->kind == Ast_Ident) {
  343. ast_node(i, Ident, node);
  344. e = scope_lookup(ctx->scope, i->token.string);
  345. if (e != nullptr && e->kind == Entity_Variable) {
  346. used = (e->flags & EntityFlag_Used) != 0; // TODO(bill): Make backup just in case
  347. }
  348. }
  349. }
  350. if (e != nullptr && used) {
  351. e->flags |= EntityFlag_Used;
  352. }
  353. Type *assignment_type = lhs->type;
  354. if (rhs->mode == Addressing_Type && is_type_polymorphic(rhs->type)) {
  355. gbString t = type_to_string(rhs->type);
  356. error(rhs->expr, "Invalid use of a non-specialized polymorphic type '%s'", t);
  357. gb_string_free(t);
  358. }
  359. switch (lhs->mode) {
  360. case Addressing_Invalid:
  361. return nullptr;
  362. case Addressing_Variable:
  363. break;
  364. case Addressing_MapIndex: {
  365. Ast *ln = unparen_expr(lhs->expr);
  366. if (ln->kind == Ast_IndexExpr) {
  367. Ast *x = ln->IndexExpr.expr;
  368. TypeAndValue tav = x->tav;
  369. GB_ASSERT(tav.mode != Addressing_Invalid);
  370. if (tav.mode != Addressing_Variable) {
  371. if (!is_type_pointer(tav.type)) {
  372. gbString str = expr_to_string(lhs->expr);
  373. error(lhs->expr, "Cannot assign to the value of a map '%s'", str);
  374. gb_string_free(str);
  375. return nullptr;
  376. }
  377. }
  378. }
  379. break;
  380. }
  381. case Addressing_Context: {
  382. break;
  383. }
  384. case Addressing_SoaVariable:
  385. break;
  386. case Addressing_SwizzleVariable:
  387. break;
  388. default: {
  389. if (lhs->expr->kind == Ast_SelectorExpr) {
  390. // NOTE(bill): Extra error checks
  391. Operand op_c = {Addressing_Invalid};
  392. ast_node(se, SelectorExpr, lhs->expr);
  393. check_expr(ctx, &op_c, se->expr);
  394. if (op_c.mode == Addressing_MapIndex) {
  395. gbString str = expr_to_string(lhs->expr);
  396. error(lhs->expr, "Cannot assign to struct field '%s' in map", str);
  397. gb_string_free(str);
  398. return nullptr;
  399. }
  400. }
  401. Entity *e = entity_of_node(lhs->expr);
  402. gbString str = expr_to_string(lhs->expr);
  403. if (e != nullptr && e->flags & EntityFlag_Param) {
  404. if (e->flags & EntityFlag_Using) {
  405. error(lhs->expr, "Cannot assign to '%s' which is from a 'using' procedure parameter", str);
  406. } else {
  407. error(lhs->expr, "Cannot assign to '%s' which is a procedure parameter", str);
  408. }
  409. } else {
  410. error(lhs->expr, "Cannot assign to '%s'", str);
  411. }
  412. gb_string_free(str);
  413. break;
  414. }
  415. }
  416. check_assignment(ctx, rhs, assignment_type, str_lit("assignment"));
  417. if (rhs->mode == Addressing_Invalid) {
  418. return nullptr;
  419. }
  420. return rhs->type;
  421. }
  422. gb_internal void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags);
  423. gb_internal void check_stmt(CheckerContext *ctx, Ast *node, u32 flags) {
  424. u32 prev_state_flags = ctx->state_flags;
  425. if (node->state_flags != 0) {
  426. u32 in = node->state_flags;
  427. u32 out = ctx->state_flags;
  428. if (in & StateFlag_no_bounds_check) {
  429. out |= StateFlag_no_bounds_check;
  430. out &= ~StateFlag_bounds_check;
  431. } else if (in & StateFlag_bounds_check) {
  432. out |= StateFlag_bounds_check;
  433. out &= ~StateFlag_no_bounds_check;
  434. }
  435. if (in & StateFlag_no_type_assert) {
  436. out |= StateFlag_no_type_assert;
  437. out &= ~StateFlag_type_assert;
  438. } else if (in & StateFlag_type_assert) {
  439. out |= StateFlag_type_assert;
  440. out &= ~StateFlag_no_type_assert;
  441. }
  442. ctx->state_flags = out;
  443. }
  444. check_stmt_internal(ctx, node, flags);
  445. ctx->state_flags = prev_state_flags;
  446. }
  447. gb_internal void check_when_stmt(CheckerContext *ctx, AstWhenStmt *ws, u32 flags) {
  448. Operand operand = {Addressing_Invalid};
  449. check_expr(ctx, &operand, ws->cond);
  450. if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
  451. error(ws->cond, "Non-constant boolean 'when' condition");
  452. return;
  453. }
  454. if (ws->body == nullptr || ws->body->kind != Ast_BlockStmt) {
  455. error(ws->cond, "Invalid body for 'when' statement");
  456. return;
  457. }
  458. if (operand.value.kind == ExactValue_Bool &&
  459. operand.value.value_bool) {
  460. check_stmt_list(ctx, ws->body->BlockStmt.stmts, flags);
  461. } else if (ws->else_stmt) {
  462. switch (ws->else_stmt->kind) {
  463. case Ast_BlockStmt:
  464. check_stmt_list(ctx, ws->else_stmt->BlockStmt.stmts, flags);
  465. break;
  466. case Ast_WhenStmt:
  467. check_when_stmt(ctx, &ws->else_stmt->WhenStmt, flags);
  468. break;
  469. default:
  470. error(ws->else_stmt, "Invalid 'else' statement in 'when' statement");
  471. break;
  472. }
  473. }
  474. }
  475. gb_internal void check_label(CheckerContext *ctx, Ast *label, Ast *parent) {
  476. if (label == nullptr) {
  477. return;
  478. }
  479. ast_node(l, Label, label);
  480. if (l->name->kind != Ast_Ident) {
  481. error(l->name, "A label's name must be an identifier");
  482. return;
  483. }
  484. String name = l->name->Ident.token.string;
  485. if (is_blank_ident(name)) {
  486. error(l->name, "A label's name cannot be a blank identifier");
  487. return;
  488. }
  489. if (ctx->curr_proc_decl == nullptr) {
  490. error(l->name, "A label is only allowed within a procedure");
  491. return;
  492. }
  493. GB_ASSERT(ctx->decl != nullptr);
  494. bool ok = true;
  495. for_array(i, ctx->decl->labels) {
  496. BlockLabel bl = ctx->decl->labels[i];
  497. if (bl.name == name) {
  498. error(label, "Duplicate label with the name '%.*s'", LIT(name));
  499. ok = false;
  500. break;
  501. }
  502. }
  503. Entity *e = alloc_entity_label(ctx->scope, l->name->Ident.token, t_invalid, label, parent);
  504. add_entity(ctx, ctx->scope, l->name, e);
  505. e->parent_proc_decl = ctx->curr_proc_decl;
  506. if (ok) {
  507. BlockLabel bl = {name, label};
  508. array_add(&ctx->decl->labels, bl);
  509. }
  510. }
  511. // Returns 'true' for 'continue', 'false' for 'return'
  512. gb_internal bool check_using_stmt_entity(CheckerContext *ctx, AstUsingStmt *us, Ast *expr, bool is_selector, Entity *e) {
  513. if (e == nullptr) {
  514. if (is_blank_ident(expr)) {
  515. error(us->token, "'using' in a statement is not allowed with the blank identifier '_'");
  516. } else {
  517. error(us->token, "'using' applied to an unknown entity");
  518. }
  519. return true;
  520. }
  521. add_entity_use(ctx, expr, e);
  522. ERROR_BLOCK();
  523. switch (e->kind) {
  524. case Entity_TypeName: {
  525. Type *t = base_type(e->type);
  526. if (t->kind == Type_Enum) {
  527. for_array(i, t->Enum.fields) {
  528. Entity *f = t->Enum.fields[i];
  529. if (!is_entity_exported(f)) continue;
  530. Entity *found = scope_insert(ctx->scope, f);
  531. if (found != nullptr) {
  532. gbString expr_str = expr_to_string(expr);
  533. error(us->token, "Namespace collision while 'using' enum '%s' of: %.*s", expr_str, LIT(found->token.string));
  534. gb_string_free(expr_str);
  535. return false;
  536. }
  537. f->using_parent = e;
  538. }
  539. } else {
  540. error(us->token, "'using' can be only applied to enum type entities");
  541. }
  542. break;
  543. }
  544. case Entity_ImportName: {
  545. Scope *scope = e->ImportName.scope;
  546. rw_mutex_lock(&scope->mutex);
  547. defer (rw_mutex_unlock(&scope->mutex));
  548. for (auto const &entry : scope->elements) {
  549. String name = entry.key;
  550. Entity *decl = entry.value;
  551. if (!is_entity_exported(decl)) continue;
  552. Entity *found = scope_insert_with_name(ctx->scope, name, decl);
  553. if (found != nullptr) {
  554. gbString expr_str = expr_to_string(expr);
  555. error(us->token,
  556. "Namespace collision while 'using' import name '%s' of: %.*s\n"
  557. "\tat %s\n"
  558. "\tat %s",
  559. expr_str, LIT(found->token.string),
  560. token_pos_to_string(found->token.pos),
  561. token_pos_to_string(decl->token.pos)
  562. );
  563. gb_string_free(expr_str);
  564. return false;
  565. }
  566. }
  567. break;
  568. }
  569. case Entity_Variable: {
  570. bool is_ptr = is_type_pointer(e->type);
  571. Type *t = base_type(type_deref(e->type));
  572. if (t->kind == Type_Struct) {
  573. Scope *found = t->Struct.scope;
  574. GB_ASSERT(found != nullptr);
  575. for (auto const &entry : found->elements) {
  576. Entity *f = entry.value;
  577. if (f->kind == Entity_Variable) {
  578. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, expr);
  579. if (!is_ptr && e->flags & EntityFlag_Value) uvar->flags |= EntityFlag_Value;
  580. if (e->flags & EntityFlag_Param) uvar->flags |= EntityFlag_Param;
  581. Entity *prev = scope_insert(ctx->scope, uvar);
  582. if (prev != nullptr) {
  583. gbString expr_str = expr_to_string(expr);
  584. error(us->token, "Namespace collision while using '%s' of: '%.*s'", expr_str, LIT(prev->token.string));
  585. gb_string_free(expr_str);
  586. return false;
  587. }
  588. }
  589. }
  590. } else {
  591. error(us->token, "'using' can only be applied to variables of type 'struct'");
  592. return false;
  593. }
  594. break;
  595. }
  596. case Entity_Constant:
  597. error(us->token, "'using' cannot be applied to a constant");
  598. break;
  599. case Entity_Procedure:
  600. case Entity_ProcGroup:
  601. case Entity_Builtin:
  602. error(us->token, "'using' cannot be applied to a procedure");
  603. break;
  604. case Entity_Nil:
  605. error(us->token, "'using' cannot be applied to 'nil'");
  606. break;
  607. case Entity_Label:
  608. error(us->token, "'using' cannot be applied to a label");
  609. break;
  610. case Entity_Invalid:
  611. error(us->token, "'using' cannot be applied to an invalid entity");
  612. break;
  613. default:
  614. GB_PANIC("TODO(bill): 'using' other expressions?");
  615. }
  616. return true;
  617. }
  618. gb_internal void check_inline_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  619. ast_node(irs, UnrollRangeStmt, node);
  620. check_open_scope(ctx, node);
  621. Type *val0 = nullptr;
  622. Type *val1 = nullptr;
  623. Entity *entities[2] = {};
  624. isize entity_count = 0;
  625. Ast *expr = unparen_expr(irs->expr);
  626. ExactValue inline_for_depth = exact_value_i64(0);
  627. if (is_ast_range(expr)) {
  628. ast_node(ie, BinaryExpr, expr);
  629. Operand x = {};
  630. Operand y = {};
  631. bool ok = check_range(ctx, expr, true, &x, &y, &inline_for_depth);
  632. if (!ok) {
  633. goto skip_expr;
  634. }
  635. val0 = x.type;
  636. val1 = t_int;
  637. } else {
  638. Operand operand = {Addressing_Invalid};
  639. check_expr_or_type(ctx, &operand, irs->expr);
  640. if (operand.mode == Addressing_Type) {
  641. if (!is_type_enum(operand.type)) {
  642. gbString t = type_to_string(operand.type);
  643. error(operand.expr, "Cannot iterate over the type '%s'", t);
  644. gb_string_free(t);
  645. goto skip_expr;
  646. } else {
  647. val0 = operand.type;
  648. val1 = t_int;
  649. add_type_info_type(ctx, operand.type);
  650. Type *bt = base_type(operand.type);
  651. inline_for_depth = exact_value_i64(bt->Enum.fields.count);
  652. goto skip_expr;
  653. }
  654. } else if (operand.mode != Addressing_Invalid) {
  655. Type *t = base_type(operand.type);
  656. switch (t->kind) {
  657. case Type_Basic:
  658. if (is_type_string(t) && t->Basic.kind != Basic_cstring) {
  659. val0 = t_rune;
  660. val1 = t_int;
  661. inline_for_depth = exact_value_i64(operand.value.value_string.len);
  662. }
  663. break;
  664. case Type_Array:
  665. val0 = t->Array.elem;
  666. val1 = t_int;
  667. inline_for_depth = exact_value_i64(t->Array.count);
  668. break;
  669. case Type_EnumeratedArray:
  670. val0 = t->EnumeratedArray.elem;
  671. val1 = t->EnumeratedArray.index;
  672. inline_for_depth = exact_value_i64(t->EnumeratedArray.count);
  673. break;
  674. }
  675. }
  676. if (val0 == nullptr) {
  677. gbString s = expr_to_string(operand.expr);
  678. gbString t = type_to_string(operand.type);
  679. error(operand.expr, "Cannot iterate over '%s' of type '%s' in an '#unroll for' statement", s, t);
  680. gb_string_free(t);
  681. gb_string_free(s);
  682. } else if (operand.mode != Addressing_Constant) {
  683. error(operand.expr, "An '#unroll for' expression must be known at compile time");
  684. }
  685. }
  686. skip_expr:; // NOTE(zhiayang): again, declaring a variable immediately after a label... weird.
  687. Ast * lhs[2] = {irs->val0, irs->val1};
  688. Type *rhs[2] = {val0, val1};
  689. for (isize i = 0; i < 2; i++) {
  690. if (lhs[i] == nullptr) {
  691. continue;
  692. }
  693. Ast * name = lhs[i];
  694. Type *type = rhs[i];
  695. Entity *entity = nullptr;
  696. if (name->kind == Ast_Ident) {
  697. Token token = name->Ident.token;
  698. String str = token.string;
  699. Entity *found = nullptr;
  700. if (!is_blank_ident(str)) {
  701. found = scope_lookup_current(ctx->scope, str);
  702. }
  703. if (found == nullptr) {
  704. entity = alloc_entity_variable(ctx->scope, token, type, EntityState_Resolved);
  705. entity->flags |= EntityFlag_Value;
  706. add_entity_definition(&ctx->checker->info, name, entity);
  707. } else {
  708. TokenPos pos = found->token.pos;
  709. error(token,
  710. "Redeclaration of '%.*s' in this scope\n"
  711. "\tat %s", LIT(str), token_pos_to_string(pos));
  712. entity = found;
  713. }
  714. } else {
  715. error(name, "A variable declaration must be an identifier");
  716. }
  717. if (entity == nullptr) {
  718. entity = alloc_entity_dummy_variable(builtin_pkg->scope, ast_token(name));
  719. }
  720. entities[entity_count++] = entity;
  721. if (type == nullptr) {
  722. entity->type = t_invalid;
  723. entity->flags |= EntityFlag_Used;
  724. }
  725. }
  726. for (isize i = 0; i < entity_count; i++) {
  727. add_entity(ctx, ctx->scope, entities[i]->identifier, entities[i]);
  728. }
  729. // NOTE(bill): Minimize the amount of nesting of an '#unroll for'
  730. i64 prev_inline_for_depth = ctx->inline_for_depth;
  731. defer (ctx->inline_for_depth = prev_inline_for_depth);
  732. {
  733. i64 v = exact_value_to_i64(inline_for_depth);
  734. if (v <= 0) {
  735. // Do nothing
  736. } else {
  737. ctx->inline_for_depth = gb_max(ctx->inline_for_depth, 1) * v;
  738. }
  739. if (ctx->inline_for_depth >= MAX_INLINE_FOR_DEPTH && prev_inline_for_depth < MAX_INLINE_FOR_DEPTH) {
  740. if (prev_inline_for_depth > 0) {
  741. error(node, "Nested '#unroll for' loop cannot be inlined as it exceeds the maximum '#unroll for' depth (%lld levels >= %lld maximum levels)", v, MAX_INLINE_FOR_DEPTH);
  742. } else {
  743. error(node, "'#unroll for' loop cannot be inlined as it exceeds the maximum '#unroll for' depth (%lld levels >= %lld maximum levels)", v, MAX_INLINE_FOR_DEPTH);
  744. }
  745. error_line("\tUse a normal 'for' loop instead by removing the 'inline' prefix\n");
  746. ctx->inline_for_depth = MAX_INLINE_FOR_DEPTH;
  747. }
  748. }
  749. check_stmt(ctx, irs->body, mod_flags);
  750. check_close_scope(ctx);
  751. }
  752. gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  753. ast_node(ss, SwitchStmt, node);
  754. Operand x = {};
  755. mod_flags |= Stmt_BreakAllowed | Stmt_FallthroughAllowed;
  756. check_open_scope(ctx, node);
  757. defer (check_close_scope(ctx));
  758. check_label(ctx, ss->label, node); // TODO(bill): What should the label's "scope" be?
  759. if (ss->init != nullptr) {
  760. check_stmt(ctx, ss->init, 0);
  761. }
  762. if (ss->tag != nullptr) {
  763. check_expr(ctx, &x, ss->tag);
  764. check_assignment(ctx, &x, nullptr, str_lit("switch expression"));
  765. } else {
  766. x.mode = Addressing_Constant;
  767. x.type = t_bool;
  768. x.value = exact_value_bool(true);
  769. Token token = {};
  770. token.pos = ast_token(ss->body).pos;
  771. token.string = str_lit("true");
  772. x.expr = alloc_ast_node(nullptr, Ast_Ident);
  773. x.expr->Ident.token = token;
  774. }
  775. // NOTE(bill): Check for multiple defaults
  776. Ast *first_default = nullptr;
  777. ast_node(bs, BlockStmt, ss->body);
  778. for_array(i, bs->stmts) {
  779. Ast *stmt = bs->stmts[i];
  780. Ast *default_stmt = nullptr;
  781. if (stmt->kind == Ast_CaseClause) {
  782. ast_node(cc, CaseClause, stmt);
  783. if (cc->list.count == 0) {
  784. default_stmt = stmt;
  785. }
  786. } else {
  787. error(stmt, "Invalid AST - expected case clause");
  788. }
  789. if (default_stmt != nullptr) {
  790. if (first_default != nullptr) {
  791. TokenPos pos = ast_token(first_default).pos;
  792. error(stmt,
  793. "multiple default clauses\n"
  794. "\tfirst at %s", token_pos_to_string(pos));
  795. } else {
  796. first_default = default_stmt;
  797. }
  798. }
  799. }
  800. bool is_partial = ss->partial;
  801. if (is_partial) {
  802. if (!is_type_enum(x.type)) {
  803. error(x.expr, "#partial switch statement can be only used with an enum type");
  804. }
  805. }
  806. SeenMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue
  807. defer (map_destroy(&seen));
  808. for (Ast *stmt : bs->stmts) {
  809. if (stmt->kind != Ast_CaseClause) {
  810. // NOTE(bill): error handled by above multiple default checker
  811. continue;
  812. }
  813. ast_node(cc, CaseClause, stmt);
  814. for (Ast *expr : cc->list) {
  815. expr = unparen_expr(expr);
  816. if (is_ast_range(expr)) {
  817. ast_node(be, BinaryExpr, expr);
  818. Operand lhs = {};
  819. Operand rhs = {};
  820. check_expr_with_type_hint(ctx, &lhs, be->left, x.type);
  821. if (x.mode == Addressing_Invalid) {
  822. continue;
  823. }
  824. if (lhs.mode == Addressing_Invalid) {
  825. continue;
  826. }
  827. check_expr_with_type_hint(ctx, &rhs, be->right, x.type);
  828. if (rhs.mode == Addressing_Invalid) {
  829. continue;
  830. }
  831. if (!is_type_ordered(x.type)) {
  832. gbString str = type_to_string(x.type);
  833. error(expr, "Unordered type '%s', is invalid for an interval expression", str);
  834. gb_string_free(str);
  835. continue;
  836. }
  837. TokenKind upper_op = Token_Invalid;
  838. switch (be->op.kind) {
  839. case Token_Ellipsis: upper_op = Token_LtEq; break;
  840. case Token_RangeFull: upper_op = Token_LtEq; break;
  841. case Token_RangeHalf: upper_op = Token_Lt; break;
  842. default: GB_PANIC("Invalid range operator"); break;
  843. }
  844. Operand a = lhs;
  845. Operand b = rhs;
  846. check_comparison(ctx, expr, &a, &x, Token_LtEq);
  847. if (a.mode == Addressing_Invalid) {
  848. continue;
  849. }
  850. check_comparison(ctx, expr, &b, &x, upper_op);
  851. if (b.mode == Addressing_Invalid) {
  852. continue;
  853. }
  854. Operand a1 = lhs;
  855. Operand b1 = rhs;
  856. check_comparison(ctx, expr, &a1, &b1, Token_LtEq);
  857. add_to_seen_map(ctx, &seen, upper_op, x, lhs, rhs);
  858. if (is_type_string(x.type)) {
  859. // NOTE(bill): Force dependency for strings here
  860. add_package_dependency(ctx, "runtime", "string_le");
  861. add_package_dependency(ctx, "runtime", "string_lt");
  862. }
  863. } else {
  864. Operand y = {};
  865. if (is_type_typeid(x.type)) {
  866. check_expr_or_type(ctx, &y, expr, x.type);
  867. } else {
  868. check_expr_with_type_hint(ctx, &y, expr, x.type);
  869. }
  870. if (x.mode == Addressing_Invalid ||
  871. y.mode == Addressing_Invalid) {
  872. continue;
  873. }
  874. if (y.mode == Addressing_Type) {
  875. Type *t = y.type;
  876. if (t == nullptr || t == t_invalid || is_type_polymorphic(t)) {
  877. error(y.expr, "Invalid type for case clause");
  878. continue;
  879. }
  880. t = default_type(t);
  881. add_type_info_type(ctx, t);
  882. } else {
  883. convert_to_typed(ctx, &y, x.type);
  884. if (y.mode == Addressing_Invalid) {
  885. continue;
  886. }
  887. // NOTE(bill): the ordering here matters
  888. Operand z = y;
  889. check_comparison(ctx, expr, &z, &x, Token_CmpEq);
  890. if (z.mode == Addressing_Invalid) {
  891. continue;
  892. }
  893. if (y.mode != Addressing_Constant) {
  894. continue;
  895. }
  896. update_untyped_expr_type(ctx, z.expr, x.type, !is_type_untyped(x.type));
  897. add_to_seen_map(ctx, &seen, y);
  898. }
  899. }
  900. }
  901. check_open_scope(ctx, stmt);
  902. check_stmt_list(ctx, cc->stmts, mod_flags);
  903. check_close_scope(ctx);
  904. }
  905. if (!is_partial && is_type_enum(x.type)) {
  906. TEMPORARY_ALLOCATOR_GUARD();
  907. Type *et = base_type(x.type);
  908. GB_ASSERT(is_type_enum(et));
  909. auto fields = et->Enum.fields;
  910. auto unhandled = array_make<Entity *>(temporary_allocator(), 0, fields.count);
  911. for (Entity *f : fields) {
  912. if (f->kind != Entity_Constant) {
  913. continue;
  914. }
  915. ExactValue v = f->Constant.value;
  916. auto found = map_get(&seen, hash_exact_value(v));
  917. if (!found) {
  918. array_add(&unhandled, f);
  919. }
  920. }
  921. if (unhandled.count > 0) {
  922. begin_error_block();
  923. defer (end_error_block());
  924. if (unhandled.count == 1) {
  925. error_no_newline(node, "Unhandled switch case: %.*s", LIT(unhandled[0]->token.string));
  926. } else {
  927. error(node, "Unhandled switch cases:");
  928. for (Entity *f : unhandled) {
  929. error_line("\t%.*s\n", LIT(f->token.string));
  930. }
  931. }
  932. error_line("\n");
  933. error_line("\tSuggestion: Was '#partial switch' wanted?\n");
  934. }
  935. }
  936. }
  937. enum TypeSwitchKind {
  938. TypeSwitch_Invalid,
  939. TypeSwitch_Union,
  940. TypeSwitch_Any,
  941. };
  942. gb_internal TypeSwitchKind check_valid_type_switch_type(Type *type) {
  943. type = type_deref(type);
  944. if (is_type_union(type)) {
  945. return TypeSwitch_Union;
  946. }
  947. if (is_type_any(type)) {
  948. return TypeSwitch_Any;
  949. }
  950. return TypeSwitch_Invalid;
  951. }
  952. gb_internal void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  953. ast_node(ss, TypeSwitchStmt, node);
  954. Operand x = {};
  955. mod_flags |= Stmt_BreakAllowed | Stmt_TypeSwitch;
  956. check_open_scope(ctx, node);
  957. defer (check_close_scope(ctx));
  958. check_label(ctx, ss->label, node); // TODO(bill): What should the label's "scope" be?
  959. if (ss->tag->kind != Ast_AssignStmt) {
  960. error(ss->tag, "Expected an 'in' assignment for this type switch statement");
  961. return;
  962. }
  963. ast_node(as, AssignStmt, ss->tag);
  964. Token as_token = ast_token(ss->tag);
  965. if (as->lhs.count != 1) {
  966. syntax_error(as_token, "Expected 1 name before 'in'");
  967. return;
  968. }
  969. if (as->rhs.count != 1) {
  970. syntax_error(as_token, "Expected 1 expression after 'in'");
  971. return;
  972. }
  973. Ast *lhs = as->lhs[0];
  974. Ast *rhs = as->rhs[0];
  975. check_expr(ctx, &x, rhs);
  976. check_assignment(ctx, &x, nullptr, str_lit("type switch expression"));
  977. add_type_info_type(ctx, x.type);
  978. TypeSwitchKind switch_kind = check_valid_type_switch_type(x.type);
  979. if (switch_kind == TypeSwitch_Invalid) {
  980. gbString str = type_to_string(x.type);
  981. error(x.expr, "Invalid type for this type switch expression, got '%s'", str);
  982. gb_string_free(str);
  983. return;
  984. }
  985. bool is_partial = ss->partial;
  986. if (is_partial) {
  987. if (switch_kind != TypeSwitch_Union) {
  988. error(node, "#partial switch statement may only be used with a union");
  989. }
  990. }
  991. bool is_ptr = is_type_pointer(x.type);
  992. // NOTE(bill): Check for multiple defaults
  993. Ast *first_default = nullptr;
  994. ast_node(bs, BlockStmt, ss->body);
  995. for (Ast *stmt : bs->stmts) {
  996. Ast *default_stmt = nullptr;
  997. if (stmt->kind == Ast_CaseClause) {
  998. ast_node(cc, CaseClause, stmt);
  999. if (cc->list.count == 0) {
  1000. default_stmt = stmt;
  1001. }
  1002. } else {
  1003. error(stmt, "Invalid AST - expected case clause");
  1004. }
  1005. if (default_stmt != nullptr) {
  1006. if (first_default != nullptr) {
  1007. TokenPos pos = ast_token(first_default).pos;
  1008. error(stmt,
  1009. "Multiple default clauses\n"
  1010. "\tfirst at %s", token_pos_to_string(pos));
  1011. } else {
  1012. first_default = default_stmt;
  1013. }
  1014. }
  1015. }
  1016. if (lhs->kind != Ast_Ident) {
  1017. error(rhs, "Expected an identifier, got '%.*s'", LIT(ast_strings[rhs->kind]));
  1018. return;
  1019. }
  1020. Ast *nil_seen = nullptr;
  1021. PtrSet<Type *> seen = {};
  1022. defer (ptr_set_destroy(&seen));
  1023. for (Ast *stmt : bs->stmts) {
  1024. if (stmt->kind != Ast_CaseClause) {
  1025. // NOTE(bill): error handled by above multiple default checker
  1026. continue;
  1027. }
  1028. ast_node(cc, CaseClause, stmt);
  1029. bool saw_nil = false;
  1030. // TODO(bill): Make robust
  1031. Type *bt = base_type(type_deref(x.type));
  1032. Type *case_type = nullptr;
  1033. for (Ast *type_expr : cc->list) {
  1034. if (type_expr != nullptr) { // Otherwise it's a default expression
  1035. Operand y = {};
  1036. check_expr_or_type(ctx, &y, type_expr);
  1037. if (is_operand_nil(y)) {
  1038. if (!type_has_nil(type_deref(x.type))) {
  1039. error(type_expr, "'nil' case is not allowed for the type '%s'", type_to_string(type_deref(x.type)));
  1040. continue;
  1041. }
  1042. saw_nil = true;
  1043. if (nil_seen) {
  1044. ERROR_BLOCK();
  1045. error(type_expr, "'nil' case has already been handled previously");
  1046. error_line("\t 'nil' was already previously seen at %s", token_pos_to_string(ast_token(nil_seen).pos));
  1047. } else {
  1048. nil_seen = type_expr;
  1049. }
  1050. case_type = y.type;
  1051. continue;
  1052. }
  1053. if (y.mode != Addressing_Type) {
  1054. gbString str = expr_to_string(type_expr);
  1055. error(type_expr, "Expected a type as a case, got %s", str);
  1056. gb_string_free(str);
  1057. continue;
  1058. }
  1059. if (switch_kind == TypeSwitch_Union) {
  1060. GB_ASSERT(is_type_union(bt));
  1061. bool tag_type_found = false;
  1062. for (Type *vt : bt->Union.variants) {
  1063. if (are_types_identical(vt, y.type)) {
  1064. tag_type_found = true;
  1065. break;
  1066. }
  1067. }
  1068. if (!tag_type_found) {
  1069. gbString type_str = type_to_string(y.type);
  1070. error(y.expr, "Unknown variant type, got '%s'", type_str);
  1071. gb_string_free(type_str);
  1072. continue;
  1073. }
  1074. case_type = y.type;
  1075. add_type_info_type(ctx, y.type);
  1076. } else if (switch_kind == TypeSwitch_Any) {
  1077. case_type = y.type;
  1078. add_type_info_type(ctx, y.type);
  1079. } else {
  1080. GB_PANIC("Unknown type to type switch statement");
  1081. }
  1082. if (type_ptr_set_update(&seen, y.type)) {
  1083. TokenPos pos = cc->token.pos;
  1084. gbString expr_str = expr_to_string(y.expr);
  1085. error(y.expr,
  1086. "Duplicate type case '%s'\n"
  1087. "\tprevious type case at %s",
  1088. expr_str,
  1089. token_pos_to_string(pos));
  1090. gb_string_free(expr_str);
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. bool is_reference = false;
  1096. if (is_ptr &&
  1097. cc->list.count == 1 &&
  1098. case_type != nullptr) {
  1099. is_reference = true;
  1100. }
  1101. if (cc->list.count > 1 || saw_nil) {
  1102. case_type = nullptr;
  1103. }
  1104. if (case_type == nullptr) {
  1105. case_type = x.type;
  1106. }
  1107. if (switch_kind == TypeSwitch_Any) {
  1108. if (!is_type_untyped(case_type)) {
  1109. add_type_info_type(ctx, case_type);
  1110. }
  1111. }
  1112. check_open_scope(ctx, stmt);
  1113. {
  1114. Entity *tag_var = alloc_entity_variable(ctx->scope, lhs->Ident.token, case_type, EntityState_Resolved);
  1115. tag_var->flags |= EntityFlag_Used;
  1116. if (!is_reference) {
  1117. tag_var->flags |= EntityFlag_Value;
  1118. tag_var->flags |= EntityFlag_SwitchValue;
  1119. }
  1120. add_entity(ctx, ctx->scope, lhs, tag_var);
  1121. add_entity_use(ctx, lhs, tag_var);
  1122. add_implicit_entity(ctx, stmt, tag_var);
  1123. }
  1124. check_stmt_list(ctx, cc->stmts, mod_flags);
  1125. check_close_scope(ctx);
  1126. }
  1127. if (!is_partial && is_type_union(type_deref(x.type))) {
  1128. TEMPORARY_ALLOCATOR_GUARD();
  1129. Type *ut = base_type(type_deref(x.type));
  1130. GB_ASSERT(is_type_union(ut));
  1131. auto variants = ut->Union.variants;
  1132. auto unhandled = array_make<Type *>(temporary_allocator(), 0, variants.count);
  1133. for (Type *t : variants) {
  1134. if (!type_ptr_set_exists(&seen, t)) {
  1135. array_add(&unhandled, t);
  1136. }
  1137. }
  1138. if (unhandled.count > 0) {
  1139. if (unhandled.count == 1) {
  1140. gbString s = type_to_string(unhandled[0]);
  1141. error_no_newline(node, "Unhandled switch case: %s", s);
  1142. gb_string_free(s);
  1143. } else {
  1144. error_no_newline(node, "Unhandled switch cases:\n");
  1145. for (Type *t : unhandled) {
  1146. gbString s = type_to_string(t);
  1147. error_line("\t%s\n", s);
  1148. gb_string_free(s);
  1149. }
  1150. }
  1151. error_line("\n");
  1152. error_line("\tSuggestion: Was '#partial switch' wanted?\n");
  1153. }
  1154. }
  1155. }
  1156. gb_internal void check_block_stmt_for_errors(CheckerContext *ctx, Ast *body) {
  1157. if (body->kind != Ast_BlockStmt) {
  1158. return;
  1159. }
  1160. ast_node(bs, BlockStmt, body);
  1161. // NOTE(bill, 2020-09-23): This logic is prevent common erros with block statements
  1162. // e.g. if cond { x := 123; } // this is an error
  1163. if (bs->scope != nullptr && bs->scope->elements.count > 0) {
  1164. if (bs->scope->parent->node != nullptr) {
  1165. switch (bs->scope->parent->node->kind) {
  1166. case Ast_IfStmt:
  1167. case Ast_ForStmt:
  1168. case Ast_RangeStmt:
  1169. case Ast_UnrollRangeStmt:
  1170. case Ast_SwitchStmt:
  1171. case Ast_TypeSwitchStmt:
  1172. // TODO(bill): Is this a correct checking system?
  1173. break;
  1174. default:
  1175. return;
  1176. }
  1177. }
  1178. isize stmt_count = 0;
  1179. Ast *the_stmt = nullptr;
  1180. for (Ast *stmt : bs->stmts) {
  1181. GB_ASSERT(stmt != nullptr);
  1182. switch (stmt->kind) {
  1183. case_ast_node(es, EmptyStmt, stmt);
  1184. case_end;
  1185. case_ast_node(bs, BadStmt, stmt);
  1186. case_end;
  1187. case_ast_node(bd, BadDecl, stmt);
  1188. case_end;
  1189. default:
  1190. the_stmt = stmt;
  1191. stmt_count += 1;
  1192. break;
  1193. }
  1194. }
  1195. if (stmt_count == 1) {
  1196. if (the_stmt->kind == Ast_ValueDecl) {
  1197. for (Ast *name : the_stmt->ValueDecl.names) {
  1198. if (name->kind != Ast_Ident) {
  1199. continue;
  1200. }
  1201. String n = name->Ident.token.string;
  1202. if (n != "_") {
  1203. error(name, "'%.*s' declared but not used", LIT(n));
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. gb_internal bool all_operands_valid(Array<Operand> const &operands) {
  1211. if (any_errors()) {
  1212. for (Operand const &o : operands) {
  1213. if (o.type == t_invalid) {
  1214. return false;
  1215. }
  1216. }
  1217. }
  1218. return true;
  1219. }
  1220. gb_internal bool check_stmt_internal_builtin_proc_id(Ast *expr, BuiltinProcId *id_) {
  1221. BuiltinProcId id = BuiltinProc_Invalid;
  1222. Entity *e = entity_of_node(expr);
  1223. if (e != nullptr && e->kind == Entity_Builtin) {
  1224. if (e->Builtin.id && e->Builtin.id != BuiltinProc_DIRECTIVE) {
  1225. id = cast(BuiltinProcId)e->Builtin.id;
  1226. }
  1227. }
  1228. if (id_) *id_ = id;
  1229. return id != BuiltinProc_Invalid;
  1230. }
  1231. gb_internal bool check_expr_is_stack_variable(Ast *expr) {
  1232. /*
  1233. expr = unparen_expr(expr);
  1234. Entity *e = entity_of_node(expr);
  1235. if (e && e->kind == Entity_Variable) {
  1236. if (e->flags & (EntityFlag_Static|EntityFlag_Using|EntityFlag_ImplicitReference|EntityFlag_ForValue)) {
  1237. // okay
  1238. } else if (e->Variable.thread_local_model.len != 0) {
  1239. // okay
  1240. } else if (e->scope) {
  1241. if ((e->scope->flags & (ScopeFlag_Global|ScopeFlag_File|ScopeFlag_Type)) == 0) {
  1242. return true;
  1243. }
  1244. }
  1245. }
  1246. */
  1247. return false;
  1248. }
  1249. gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  1250. ast_node(rs, RangeStmt, node);
  1251. TEMPORARY_ALLOCATOR_GUARD();
  1252. u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
  1253. check_open_scope(ctx, node);
  1254. check_label(ctx, rs->label, node);
  1255. auto vals = array_make<Type *>(temporary_allocator(), 0, 2);
  1256. auto entities = array_make<Entity *>(temporary_allocator(), 0, 2);
  1257. bool is_map = false;
  1258. bool use_by_reference_for_value = false;
  1259. bool is_soa = false;
  1260. bool is_reverse = rs->reverse;
  1261. Ast *expr = unparen_expr(rs->expr);
  1262. isize max_val_count = 2;
  1263. if (is_ast_range(expr)) {
  1264. ast_node(ie, BinaryExpr, expr);
  1265. Operand x = {};
  1266. Operand y = {};
  1267. bool ok = check_range(ctx, expr, true, &x, &y, nullptr);
  1268. if (!ok) {
  1269. goto skip_expr_range_stmt;
  1270. }
  1271. array_add(&vals, x.type);
  1272. array_add(&vals, t_int);
  1273. if (is_reverse) {
  1274. error(node, "#reverse for is not supported with ranges, prefer an explicit for loop with init, condition, and post arguments");
  1275. }
  1276. } else {
  1277. Operand operand = {Addressing_Invalid};
  1278. check_expr_base(ctx, &operand, expr, nullptr);
  1279. error_operand_no_value(&operand);
  1280. if (operand.mode == Addressing_Type) {
  1281. if (!is_type_enum(operand.type)) {
  1282. gbString t = type_to_string(operand.type);
  1283. error(operand.expr, "Cannot iterate over the type '%s'", t);
  1284. gb_string_free(t);
  1285. goto skip_expr_range_stmt;
  1286. } else {
  1287. if (is_reverse) {
  1288. error(node, "#reverse for is not supported for enum types");
  1289. }
  1290. array_add(&vals, operand.type);
  1291. array_add(&vals, t_int);
  1292. add_type_info_type(ctx, operand.type);
  1293. goto skip_expr_range_stmt;
  1294. }
  1295. } else if (operand.mode != Addressing_Invalid) {
  1296. bool is_ptr = is_type_pointer(operand.type);
  1297. Type *t = base_type(type_deref(operand.type));
  1298. switch (t->kind) {
  1299. case Type_Basic:
  1300. if (is_type_string(t) && t->Basic.kind != Basic_cstring) {
  1301. array_add(&vals, t_rune);
  1302. array_add(&vals, t_int);
  1303. if (is_reverse) {
  1304. add_package_dependency(ctx, "runtime", "string_decode_last_rune");
  1305. } else {
  1306. add_package_dependency(ctx, "runtime", "string_decode_rune");
  1307. }
  1308. }
  1309. break;
  1310. case Type_EnumeratedArray:
  1311. if (is_ptr) use_by_reference_for_value = true;
  1312. array_add(&vals, t->EnumeratedArray.elem);
  1313. array_add(&vals, t->EnumeratedArray.index);
  1314. break;
  1315. case Type_Array:
  1316. if (is_ptr) use_by_reference_for_value = true;
  1317. array_add(&vals, t->Array.elem);
  1318. array_add(&vals, t_int);
  1319. break;
  1320. case Type_DynamicArray:
  1321. if (is_ptr) use_by_reference_for_value = true;
  1322. array_add(&vals, t->DynamicArray.elem);
  1323. array_add(&vals, t_int);
  1324. break;
  1325. case Type_Slice:
  1326. if (is_ptr) use_by_reference_for_value = true;
  1327. array_add(&vals, t->Slice.elem);
  1328. array_add(&vals, t_int);
  1329. break;
  1330. case Type_Map:
  1331. if (is_ptr) use_by_reference_for_value = true;
  1332. is_map = true;
  1333. array_add(&vals, t->Map.key);
  1334. array_add(&vals, t->Map.value);
  1335. if (is_reverse) {
  1336. error(node, "#reverse for is not supported for map types, as maps are unordered");
  1337. }
  1338. break;
  1339. case Type_Tuple:
  1340. {
  1341. isize count = t->Tuple.variables.count;
  1342. if (count < 1 || count > 3) {
  1343. check_not_tuple(ctx, &operand);
  1344. error_line("\tMultiple return valued parameters in a range statement are limited to a maximum of 2 usable values with a trailing boolean for the conditional\n");
  1345. break;
  1346. }
  1347. Type *cond_type = t->Tuple.variables[count-1]->type;
  1348. if (!is_type_boolean(cond_type)) {
  1349. gbString s = type_to_string(cond_type);
  1350. error(operand.expr, "The final type of %td-valued expression must be a boolean, got %s", count, s);
  1351. gb_string_free(s);
  1352. break;
  1353. }
  1354. for (Entity *e : t->Tuple.variables) {
  1355. array_add(&vals, e->type);
  1356. }
  1357. if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) {
  1358. gbString s = type_to_string(t);
  1359. error(operand.expr, "Expected a 3-valued expression on the rhs, got (%s)", s);
  1360. gb_string_free(s);
  1361. break;
  1362. }
  1363. if (rs->vals.count > 0 && rs->vals[0] != nullptr && count < 2) {
  1364. gbString s = type_to_string(t);
  1365. error(operand.expr, "Expected at least a 2-valued expression on the rhs, got (%s)", s);
  1366. gb_string_free(s);
  1367. break;
  1368. }
  1369. if (is_reverse) {
  1370. error(node, "#reverse for is not supported for multiple return valued parameters");
  1371. }
  1372. }
  1373. break;
  1374. case Type_Struct:
  1375. if (t->Struct.soa_kind != StructSoa_None) {
  1376. is_soa = true;
  1377. if (is_ptr) use_by_reference_for_value = true;
  1378. array_add(&vals, t->Struct.soa_elem);
  1379. array_add(&vals, t_int);
  1380. }
  1381. break;
  1382. }
  1383. }
  1384. if (vals.count == 0 || vals[0] == nullptr) {
  1385. gbString s = expr_to_string(operand.expr);
  1386. gbString t = type_to_string(operand.type);
  1387. defer (gb_string_free(s));
  1388. defer (gb_string_free(t));
  1389. error(operand.expr, "Cannot iterate over '%s' of type '%s'", s, t);
  1390. if (rs->vals.count == 1) {
  1391. Type *t = type_deref(operand.type);
  1392. if (is_type_map(t) || is_type_bit_set(t)) {
  1393. gbString v = expr_to_string(rs->vals[0]);
  1394. defer (gb_string_free(v));
  1395. error_line("\tSuggestion: place parentheses around the expression\n");
  1396. error_line("\t for (%s in %s) {\n", v, s);
  1397. }
  1398. }
  1399. }
  1400. }
  1401. skip_expr_range_stmt:; // NOTE(zhiayang): again, declaring a variable immediately after a label... weird.
  1402. if (rs->vals.count > max_val_count) {
  1403. error(rs->vals[max_val_count], "Expected a maximum of %td identifier%s, got %td", max_val_count, max_val_count == 1 ? "" : "s", rs->vals.count);
  1404. }
  1405. auto rhs = slice_from_array(vals);
  1406. auto lhs = slice_make<Ast *>(temporary_allocator(), rhs.count);
  1407. slice_copy(&lhs, rs->vals);
  1408. isize addressable_index = cast(isize)is_map;
  1409. for_array(i, rhs) {
  1410. if (lhs[i] == nullptr) {
  1411. continue;
  1412. }
  1413. Ast * name = lhs[i];
  1414. Type *type = rhs[i];
  1415. Entity *entity = nullptr;
  1416. if (name->kind == Ast_Ident) {
  1417. Token token = name->Ident.token;
  1418. String str = token.string;
  1419. Entity *found = nullptr;
  1420. if (!is_blank_ident(str)) {
  1421. found = scope_lookup_current(ctx->scope, str);
  1422. }
  1423. if (found == nullptr) {
  1424. entity = alloc_entity_variable(ctx->scope, token, type, EntityState_Resolved);
  1425. entity->flags |= EntityFlag_ForValue;
  1426. entity->flags |= EntityFlag_Value;
  1427. entity->identifier = name;
  1428. if (i == addressable_index && use_by_reference_for_value) {
  1429. entity->flags &= ~EntityFlag_Value;
  1430. }
  1431. if (is_soa) {
  1432. if (i == 0) {
  1433. entity->flags |= EntityFlag_SoaPtrField;
  1434. }
  1435. }
  1436. add_entity_definition(&ctx->checker->info, name, entity);
  1437. } else {
  1438. TokenPos pos = found->token.pos;
  1439. error(token,
  1440. "Redeclaration of '%.*s' in this scope\n"
  1441. "\tat %s",
  1442. LIT(str), token_pos_to_string(pos));
  1443. entity = found;
  1444. }
  1445. } else {
  1446. error(name, "A variable declaration must be an identifier");
  1447. }
  1448. if (entity == nullptr) {
  1449. entity = alloc_entity_dummy_variable(builtin_pkg->scope, ast_token(name));
  1450. entity->identifier = name; // might not be an identifier
  1451. }
  1452. array_add(&entities, entity);
  1453. if (type == nullptr) {
  1454. entity->type = t_invalid;
  1455. entity->flags |= EntityFlag_Used;
  1456. }
  1457. }
  1458. for (Entity *e : entities) {
  1459. DeclInfo *d = decl_info_of_entity(e);
  1460. GB_ASSERT(d == nullptr);
  1461. add_entity(ctx, ctx->scope, e->identifier, e);
  1462. d = make_decl_info(ctx->scope, ctx->decl);
  1463. add_entity_and_decl_info(ctx, e->identifier, e, d);
  1464. }
  1465. check_stmt(ctx, rs->body, new_flags);
  1466. check_close_scope(ctx);
  1467. }
  1468. gb_internal void check_value_decl_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  1469. ast_node(vd, ValueDecl, node);
  1470. if (!vd->is_mutable) {
  1471. // constant value declaration
  1472. // NOTE(bill): Check `_` declarations
  1473. for (Ast *name : vd->names) {
  1474. if (is_blank_ident(name)) {
  1475. Entity *e = name->Ident.entity;
  1476. DeclInfo *d = decl_info_of_entity(e);
  1477. if (d != nullptr) {
  1478. check_entity_decl(ctx, e, d, nullptr);
  1479. }
  1480. }
  1481. }
  1482. return;
  1483. }
  1484. Entity **entities = gb_alloc_array(permanent_allocator(), Entity *, vd->names.count);
  1485. isize entity_count = 0;
  1486. isize new_name_count = 0;
  1487. for (Ast *name : vd->names) {
  1488. Entity *entity = nullptr;
  1489. if (name->kind != Ast_Ident) {
  1490. error(name, "A variable declaration must be an identifier");
  1491. } else {
  1492. Token token = name->Ident.token;
  1493. String str = token.string;
  1494. Entity *found = nullptr;
  1495. // NOTE(bill): Ignore assignments to '_'
  1496. if (!is_blank_ident(str)) {
  1497. found = scope_lookup_current(ctx->scope, str);
  1498. new_name_count += 1;
  1499. }
  1500. if (found == nullptr) {
  1501. entity = alloc_entity_variable(ctx->scope, token, nullptr);
  1502. entity->identifier = name;
  1503. Ast *fl = ctx->foreign_context.curr_library;
  1504. if (fl != nullptr) {
  1505. GB_ASSERT(fl->kind == Ast_Ident);
  1506. entity->Variable.is_foreign = true;
  1507. entity->Variable.foreign_library_ident = fl;
  1508. }
  1509. } else {
  1510. TokenPos pos = found->token.pos;
  1511. error(token,
  1512. "Redeclaration of '%.*s' in this scope\n"
  1513. "\tat %s",
  1514. LIT(str), token_pos_to_string(pos));
  1515. entity = found;
  1516. }
  1517. }
  1518. if (entity == nullptr) {
  1519. entity = alloc_entity_dummy_variable(builtin_pkg->scope, ast_token(name));
  1520. }
  1521. entity->parent_proc_decl = ctx->curr_proc_decl;
  1522. entities[entity_count++] = entity;
  1523. if (name->kind == Ast_Ident) {
  1524. name->Ident.entity = entity;
  1525. }
  1526. }
  1527. if (new_name_count == 0) {
  1528. begin_error_block();
  1529. error(node, "No new declarations on the left hand side");
  1530. bool all_underscore = true;
  1531. for (Ast *name : vd->names) {
  1532. if (name->kind == Ast_Ident) {
  1533. if (!is_blank_ident(name)) {
  1534. all_underscore = false;
  1535. break;
  1536. }
  1537. } else {
  1538. all_underscore = false;
  1539. break;
  1540. }
  1541. }
  1542. if (all_underscore) {
  1543. error_line("\tSuggestion: Try changing the declaration (:=) to an assignment (=)\n");
  1544. }
  1545. end_error_block();
  1546. }
  1547. Type *init_type = nullptr;
  1548. if (vd->type != nullptr) {
  1549. init_type = check_type(ctx, vd->type);
  1550. if (init_type == nullptr) {
  1551. init_type = t_invalid;
  1552. } else if (is_type_polymorphic(base_type(init_type))) {
  1553. gbString str = type_to_string(init_type);
  1554. error(vd->type, "Invalid use of a polymorphic type '%s' in variable declaration", str);
  1555. gb_string_free(str);
  1556. init_type = t_invalid;
  1557. }
  1558. }
  1559. // TODO NOTE(bill): This technically checks things multple times
  1560. AttributeContext ac = make_attribute_context(ctx->foreign_context.link_prefix);
  1561. check_decl_attributes(ctx, vd->attributes, var_decl_attribute, &ac);
  1562. for (isize i = 0; i < entity_count; i++) {
  1563. Entity *e = entities[i];
  1564. GB_ASSERT(e != nullptr);
  1565. if (e->flags & EntityFlag_Visited) {
  1566. e->type = t_invalid;
  1567. continue;
  1568. }
  1569. e->flags |= EntityFlag_Visited;
  1570. e->state = EntityState_InProgress;
  1571. if (e->type == nullptr) {
  1572. e->type = init_type;
  1573. e->state = EntityState_Resolved;
  1574. }
  1575. ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
  1576. if (ac.link_name.len > 0) {
  1577. e->Variable.link_name = ac.link_name;
  1578. }
  1579. e->flags &= ~EntityFlag_Static;
  1580. if (ac.is_static) {
  1581. String name = e->token.string;
  1582. if (name == "_") {
  1583. error(e->token, "The 'static' attribute is not allowed to be applied to '_'");
  1584. } else {
  1585. e->flags |= EntityFlag_Static;
  1586. if (ctx->in_defer) {
  1587. error(e->token, "'static' variables cannot be declared within a defer statement");
  1588. }
  1589. }
  1590. }
  1591. if (ac.thread_local_model != "") {
  1592. String name = e->token.string;
  1593. if (name == "_") {
  1594. error(e->token, "The 'thread_local' attribute is not allowed to be applied to '_'");
  1595. } else {
  1596. e->flags |= EntityFlag_Static;
  1597. if (ctx->in_defer) {
  1598. error(e->token, "'thread_local' variables cannot be declared within a defer statement");
  1599. }
  1600. }
  1601. e->Variable.thread_local_model = ac.thread_local_model;
  1602. }
  1603. if (is_arch_wasm() && e->Variable.thread_local_model.len != 0) {
  1604. error(e->token, "@(thread_local) is not supported for this target platform");
  1605. }
  1606. if (ac.is_static && ac.thread_local_model != "") {
  1607. error(e->token, "The 'static' attribute is not needed if 'thread_local' is applied");
  1608. }
  1609. }
  1610. check_init_variables(ctx, entities, entity_count, vd->values, str_lit("variable declaration"));
  1611. check_arity_match(ctx, vd, false);
  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(ctx, e);
  1626. auto *fp = &ctx->checker->info.foreigns;
  1627. StringHashKey key = string_hash_string(name);
  1628. Entity **found = string_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",
  1638. LIT(name), token_pos_to_string(pos));
  1639. }
  1640. } else {
  1641. string_map_set(fp, key, e);
  1642. }
  1643. } else if (e->flags & EntityFlag_Static) {
  1644. if (vd->values.count > 0) {
  1645. if (entity_count != vd->values.count) {
  1646. error(e->token, "A static variable declaration with a default value must be constant");
  1647. } else {
  1648. Ast *value = vd->values[i];
  1649. if (value->tav.mode != Addressing_Constant) {
  1650. error(e->token, "A static variable declaration with a default value must be constant");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. add_entity(ctx, ctx->scope, e->identifier, e);
  1656. }
  1657. if (vd->is_using != 0) {
  1658. Token token = ast_token(node);
  1659. if (vd->type != nullptr && entity_count > 1) {
  1660. error(token, "'using' can only be applied to one variable of the same type");
  1661. // TODO(bill): Should a 'continue' happen here?
  1662. }
  1663. for (isize entity_index = 0; entity_index < 1; entity_index++) {
  1664. Entity *e = entities[entity_index];
  1665. if (e == nullptr) {
  1666. continue;
  1667. }
  1668. if (e->kind != Entity_Variable) {
  1669. continue;
  1670. }
  1671. String name = e->token.string;
  1672. Type *t = base_type(type_deref(e->type));
  1673. if (is_blank_ident(name)) {
  1674. error(token, "'using' cannot be applied variable declared as '_'");
  1675. } else if (is_type_struct(t) || is_type_raw_union(t)) {
  1676. ERROR_BLOCK();
  1677. Scope *scope = t->Struct.scope;
  1678. GB_ASSERT(scope != nullptr);
  1679. for (auto const &entry : scope->elements) {
  1680. Entity *f = entry.value;
  1681. if (f->kind == Entity_Variable) {
  1682. Entity *uvar = alloc_entity_using_variable(e, f->token, f->type, nullptr);
  1683. uvar->flags |= (e->flags & EntityFlag_Value);
  1684. Entity *prev = scope_insert(ctx->scope, uvar);
  1685. if (prev != nullptr) {
  1686. error(token, "Namespace collision while 'using' '%.*s' of: %.*s", LIT(name), LIT(prev->token.string));
  1687. return;
  1688. }
  1689. }
  1690. }
  1691. add_entity_use(ctx, nullptr, e);
  1692. } else {
  1693. // NOTE(bill): skip the rest to remove extra errors
  1694. error(token, "'using' can only be applied to variables of type struct or raw_union");
  1695. return;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. gb_internal void check_expr_stmt(CheckerContext *ctx, Ast *node) {
  1701. ast_node(es, ExprStmt, node);
  1702. Operand operand = {Addressing_Invalid};
  1703. ExprKind kind = check_expr_base(ctx, &operand, es->expr, nullptr);
  1704. switch (operand.mode) {
  1705. case Addressing_Type:
  1706. {
  1707. gbString str = type_to_string(operand.type);
  1708. error(node, "'%s' is not an expression but a type and cannot be used as a statement", str);
  1709. gb_string_free(str);
  1710. break;
  1711. }
  1712. case Addressing_NoValue:
  1713. return;
  1714. }
  1715. if (kind == Expr_Stmt) {
  1716. return;
  1717. }
  1718. Ast *expr = strip_or_return_expr(operand.expr);
  1719. if (expr->kind == Ast_CallExpr) {
  1720. BuiltinProcId builtin_id = BuiltinProc_Invalid;
  1721. bool do_require = false;
  1722. AstCallExpr *ce = &expr->CallExpr;
  1723. Type *t = base_type(type_of_expr(ce->proc));
  1724. if (t->kind == Type_Proc) {
  1725. do_require = t->Proc.require_results;
  1726. } else if (check_stmt_internal_builtin_proc_id(ce->proc, &builtin_id)) {
  1727. auto const &bp = builtin_procs[builtin_id];
  1728. do_require = bp.kind == Expr_Expr && !bp.ignore_results;
  1729. }
  1730. if (do_require) {
  1731. gbString expr_str = expr_to_string(ce->proc);
  1732. error(node, "'%s' requires that its results must be handled", expr_str);
  1733. gb_string_free(expr_str);
  1734. }
  1735. return;
  1736. } else if (expr->kind == Ast_SelectorCallExpr) {
  1737. BuiltinProcId builtin_id = BuiltinProc_Invalid;
  1738. bool do_require = false;
  1739. AstSelectorCallExpr *se = &expr->SelectorCallExpr;
  1740. ast_node(ce, CallExpr, se->call);
  1741. Type *t = base_type(type_of_expr(ce->proc));
  1742. if (t == nullptr) {
  1743. gbString expr_str = expr_to_string(ce->proc);
  1744. error(node, "'%s' is not a value field nor procedure", expr_str);
  1745. gb_string_free(expr_str);
  1746. return;
  1747. }
  1748. if (t->kind == Type_Proc) {
  1749. do_require = t->Proc.require_results;
  1750. } else if (check_stmt_internal_builtin_proc_id(ce->proc, &builtin_id)) {
  1751. auto const &bp = builtin_procs[builtin_id];
  1752. do_require = bp.kind == Expr_Expr && !bp.ignore_results;
  1753. }
  1754. if (do_require) {
  1755. gbString expr_str = expr_to_string(ce->proc);
  1756. error(node, "'%s' requires that its results must be handled", expr_str);
  1757. gb_string_free(expr_str);
  1758. }
  1759. return;
  1760. }
  1761. gbString expr_str = expr_to_string(operand.expr);
  1762. error(node, "Expression is not used: '%s'", expr_str);
  1763. gb_string_free(expr_str);
  1764. if (operand.expr->kind == Ast_BinaryExpr) {
  1765. ast_node(be, BinaryExpr, operand.expr);
  1766. if (be->op.kind != Token_CmpEq) {
  1767. return;
  1768. }
  1769. switch (be->left->tav.mode) {
  1770. case Addressing_Context:
  1771. case Addressing_Variable:
  1772. case Addressing_MapIndex:
  1773. case Addressing_SoaVariable:
  1774. {
  1775. gbString lhs = expr_to_string(be->left);
  1776. gbString rhs = expr_to_string(be->right);
  1777. error_line("\tSuggestion: Did you mean to do an assignment?\n", lhs, rhs);
  1778. error_line("\t '%s = %s;'\n", lhs, rhs);
  1779. gb_string_free(rhs);
  1780. gb_string_free(lhs);
  1781. }
  1782. break;
  1783. }
  1784. }
  1785. }
  1786. gb_internal void check_assign_stmt(CheckerContext *ctx, Ast *node) {
  1787. ast_node(as, AssignStmt, node);
  1788. if (as->op.kind == Token_Eq) {
  1789. // a, b, c = 1, 2, 3; // Multisided
  1790. isize lhs_count = as->lhs.count;
  1791. if (lhs_count == 0) {
  1792. error(as->op, "Missing lhs in assignment statement");
  1793. return;
  1794. }
  1795. TEMPORARY_ALLOCATOR_GUARD();
  1796. // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
  1797. // an extra allocation
  1798. auto lhs_operands = array_make<Operand>(temporary_allocator(), lhs_count);
  1799. auto rhs_operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
  1800. for_array(i, as->lhs) {
  1801. if (is_blank_ident(as->lhs[i])) {
  1802. Operand *o = &lhs_operands[i];
  1803. o->expr = as->lhs[i];
  1804. o->mode = Addressing_Value;
  1805. } else {
  1806. ctx->assignment_lhs_hint = unparen_expr(as->lhs[i]);
  1807. check_expr(ctx, &lhs_operands[i], as->lhs[i]);
  1808. }
  1809. }
  1810. ctx->assignment_lhs_hint = nullptr; // Reset the assignment_lhs_hint
  1811. check_assignment_arguments(ctx, lhs_operands, &rhs_operands, as->rhs);
  1812. auto lhs_to_ignore = array_make<bool>(temporary_allocator(), lhs_count);
  1813. isize rhs_count = rhs_operands.count;
  1814. isize max = gb_min(lhs_count, rhs_count);
  1815. for (isize i = 0; i < max; i++) {
  1816. if (lhs_to_ignore[i]) {
  1817. continue;
  1818. }
  1819. check_assignment_variable(ctx, &lhs_operands[i], &rhs_operands[i]);
  1820. }
  1821. if (lhs_count != rhs_count) {
  1822. error(as->lhs[0], "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
  1823. }
  1824. } else {
  1825. // a += 1; // Single-sided
  1826. Token op = as->op;
  1827. if (as->lhs.count != 1 || as->rhs.count != 1) {
  1828. error(op, "Assignment operation '%.*s' requires single-valued expressions", LIT(op.string));
  1829. return;
  1830. }
  1831. if (!gb_is_between(op.kind, Token__AssignOpBegin+1, Token__AssignOpEnd-1)) {
  1832. error(op, "Unknown Assignment operation '%.*s'", LIT(op.string));
  1833. return;
  1834. }
  1835. Operand lhs = {Addressing_Invalid};
  1836. Operand rhs = {Addressing_Invalid};
  1837. Ast *binary_expr = alloc_ast_node(node->file(), Ast_BinaryExpr);
  1838. ast_node(be, BinaryExpr, binary_expr);
  1839. be->op = op;
  1840. be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));
  1841. // NOTE(bill): Only use the first one will be used
  1842. be->left = as->lhs[0];
  1843. be->right = as->rhs[0];
  1844. check_expr(ctx, &lhs, as->lhs[0]);
  1845. check_binary_expr(ctx, &rhs, binary_expr, nullptr, true);
  1846. if (rhs.mode != Addressing_Invalid) {
  1847. // NOTE(bill): Only use the first one will be used
  1848. check_assignment_variable(ctx, &lhs, &rhs);
  1849. }
  1850. }
  1851. }
  1852. gb_internal void check_if_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  1853. ast_node(is, IfStmt, node);
  1854. check_open_scope(ctx, node);
  1855. check_label(ctx, is->label, node);
  1856. if (is->init != nullptr) {
  1857. check_stmt(ctx, is->init, 0);
  1858. }
  1859. Operand operand = {Addressing_Invalid};
  1860. check_expr(ctx, &operand, is->cond);
  1861. if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
  1862. error(is->cond, "Non-boolean condition in 'if' statement");
  1863. }
  1864. check_stmt(ctx, is->body, mod_flags);
  1865. if (is->else_stmt != nullptr) {
  1866. switch (is->else_stmt->kind) {
  1867. case Ast_IfStmt:
  1868. case Ast_BlockStmt:
  1869. check_stmt(ctx, is->else_stmt, mod_flags);
  1870. break;
  1871. default:
  1872. error(is->else_stmt, "Invalid 'else' statement in 'if' statement");
  1873. break;
  1874. }
  1875. }
  1876. check_close_scope(ctx);
  1877. }
  1878. gb_internal void check_return_stmt(CheckerContext *ctx, Ast *node) {
  1879. ast_node(rs, ReturnStmt, node);
  1880. GB_ASSERT(ctx->curr_proc_sig != nullptr);
  1881. if (ctx->in_defer) {
  1882. error(rs->token, "'return' cannot be used within a defer statement");
  1883. return;
  1884. }
  1885. Type *proc_type = ctx->curr_proc_sig;
  1886. GB_ASSERT(proc_type != nullptr);
  1887. GB_ASSERT(proc_type->kind == Type_Proc);
  1888. TypeProc *pt = &proc_type->Proc;
  1889. if (pt->diverging) {
  1890. error(rs->token, "Diverging procedures may not return");
  1891. return;
  1892. }
  1893. Entity **result_entities = nullptr;
  1894. isize result_count = 0;
  1895. bool has_named_results = pt->has_named_results;
  1896. if (pt->results) {
  1897. result_entities = proc_type->Proc.results->Tuple.variables.data;
  1898. result_count = proc_type->Proc.results->Tuple.variables.count;
  1899. }
  1900. auto operands = array_make<Operand>(heap_allocator(), 0, 2*rs->results.count);
  1901. defer (array_free(&operands));
  1902. check_unpack_arguments(ctx, result_entities, result_count, &operands, rs->results, UnpackFlag_AllowOk);
  1903. if (result_count == 0 && rs->results.count > 0) {
  1904. error(rs->results[0], "No return values expected");
  1905. } else if (has_named_results && operands.count == 0) {
  1906. // Okay
  1907. } else if (operands.count != result_count) {
  1908. // Ignore error message as it has most likely already been reported
  1909. if (all_operands_valid(operands)) {
  1910. if (operands.count == 1) {
  1911. gbString t = type_to_string(operands[0].type);
  1912. error(node, "Expected %td return values, got %td (%s)", result_count, operands.count, t);
  1913. gb_string_free(t);
  1914. } else {
  1915. error(node, "Expected %td return values, got %td", result_count, operands.count);
  1916. }
  1917. }
  1918. } else {
  1919. for (isize i = 0; i < result_count; i++) {
  1920. Entity *e = pt->results->Tuple.variables[i];
  1921. Operand *o = &operands[i];
  1922. check_assignment(ctx, o, e->type, str_lit("return statement"));
  1923. if (is_type_untyped(o->type)) {
  1924. update_untyped_expr_type(ctx, o->expr, e->type, true);
  1925. }
  1926. // NOTE(bill): This is very basic escape analysis
  1927. // This needs to be improved tremendously, and a lot of it done during the
  1928. // middle-end (or LLVM side) to improve checks and error messages
  1929. Ast *expr = unparen_expr(o->expr);
  1930. if (expr->kind == Ast_UnaryExpr && expr->UnaryExpr.op.kind == Token_And) {
  1931. Ast *x = unparen_expr(expr->UnaryExpr.expr);
  1932. if (x->kind == Ast_CompoundLit) {
  1933. error(expr, "Cannot return the address to a stack value from a procedure");
  1934. } else if (x->kind == Ast_IndexExpr) {
  1935. Ast *array = x->IndexExpr.expr;
  1936. if (is_type_array_like(type_of_expr(array)) && check_expr_is_stack_variable(array)) {
  1937. gbString t = type_to_string(type_of_expr(array));
  1938. error(expr, "Cannot return the address to an element of stack variable from a procedure, of type %s", t);
  1939. gb_string_free(t);
  1940. }
  1941. } else {
  1942. if (check_expr_is_stack_variable(x)) {
  1943. error(expr, "Cannot return the address to a stack variable from a procedure");
  1944. }
  1945. }
  1946. }
  1947. }
  1948. }
  1949. }
  1950. gb_internal void check_for_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
  1951. ast_node(fs, ForStmt, node);
  1952. mod_flags |= Stmt_BreakAllowed | Stmt_ContinueAllowed;
  1953. check_open_scope(ctx, node);
  1954. check_label(ctx, fs->label, node); // TODO(bill): What should the label's "scope" be?
  1955. if (fs->init != nullptr) {
  1956. check_stmt(ctx, fs->init, 0);
  1957. }
  1958. if (fs->cond != nullptr) {
  1959. Operand o = {Addressing_Invalid};
  1960. check_expr(ctx, &o, fs->cond);
  1961. if (o.mode != Addressing_Invalid && !is_type_boolean(o.type)) {
  1962. error(fs->cond, "Non-boolean condition in 'for' statement");
  1963. }
  1964. }
  1965. if (fs->post != nullptr) {
  1966. check_stmt(ctx, fs->post, 0);
  1967. if (fs->post->kind != Ast_AssignStmt) {
  1968. error(fs->post, "'for' statement post statement must be a simple statement");
  1969. }
  1970. }
  1971. check_stmt(ctx, fs->body, mod_flags);
  1972. check_close_scope(ctx);
  1973. }
  1974. gb_internal void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
  1975. u32 mod_flags = flags & (~Stmt_FallthroughAllowed);
  1976. switch (node->kind) {
  1977. case_ast_node(_, EmptyStmt, node); case_end;
  1978. case_ast_node(_, BadStmt, node); case_end;
  1979. case_ast_node(_, BadDecl, node); case_end;
  1980. case_ast_node(es, ExprStmt, node)
  1981. check_expr_stmt(ctx, node);
  1982. case_end;
  1983. case_ast_node(as, AssignStmt, node);
  1984. check_assign_stmt(ctx, node);
  1985. case_end;
  1986. case_ast_node(bs, BlockStmt, node);
  1987. check_open_scope(ctx, node);
  1988. check_label(ctx, bs->label, node);
  1989. check_stmt_list(ctx, bs->stmts, flags);
  1990. check_block_stmt_for_errors(ctx, node);
  1991. check_close_scope(ctx);
  1992. case_end;
  1993. case_ast_node(is, IfStmt, node);
  1994. check_if_stmt(ctx, node, mod_flags);
  1995. case_end;
  1996. case_ast_node(ws, WhenStmt, node);
  1997. check_when_stmt(ctx, ws, flags);
  1998. case_end;
  1999. case_ast_node(rs, ReturnStmt, node);
  2000. check_return_stmt(ctx, node);
  2001. case_end;
  2002. case_ast_node(fs, ForStmt, node);
  2003. check_for_stmt(ctx, node, mod_flags);
  2004. case_end;
  2005. case_ast_node(rs, RangeStmt, node);
  2006. check_range_stmt(ctx, node, mod_flags);
  2007. case_end;
  2008. case_ast_node(irs, UnrollRangeStmt, node);
  2009. check_inline_range_stmt(ctx, node, mod_flags);
  2010. case_end;
  2011. case_ast_node(ss, SwitchStmt, node);
  2012. check_switch_stmt(ctx, node, mod_flags);
  2013. case_end;
  2014. case_ast_node(ss, TypeSwitchStmt, node);
  2015. check_type_switch_stmt(ctx, node, mod_flags);
  2016. case_end;
  2017. case_ast_node(ds, DeferStmt, node);
  2018. if (is_ast_decl(ds->stmt)) {
  2019. error(ds->token, "You cannot defer a declaration");
  2020. } else {
  2021. bool out_in_defer = ctx->in_defer;
  2022. ctx->in_defer = true;
  2023. check_stmt(ctx, ds->stmt, 0);
  2024. ctx->in_defer = out_in_defer;
  2025. if (ctx->decl) {
  2026. ctx->decl->defer_used += 1;
  2027. }
  2028. }
  2029. case_end;
  2030. case_ast_node(bs, BranchStmt, node);
  2031. Token token = bs->token;
  2032. switch (token.kind) {
  2033. case Token_break:
  2034. if ((flags & Stmt_BreakAllowed) == 0 && bs->label == nullptr) {
  2035. error(token, "'break' only allowed in non-inline loops or 'switch' statements");
  2036. }
  2037. break;
  2038. case Token_continue:
  2039. if ((flags & Stmt_ContinueAllowed) == 0 && bs->label == nullptr) {
  2040. error(token, "'continue' only allowed in non-inline loops");
  2041. }
  2042. break;
  2043. case Token_fallthrough:
  2044. if ((flags & Stmt_FallthroughAllowed) == 0) {
  2045. if ((flags & Stmt_TypeSwitch) != 0) {
  2046. error(token, "'fallthrough' statement not allowed within a type switch statement");
  2047. } else {
  2048. error(token, "'fallthrough' statement in illegal position, expected at the end of a 'case' block");
  2049. }
  2050. } else if (bs->label != nullptr) {
  2051. error(token, "'fallthrough' cannot have a label");
  2052. }
  2053. break;
  2054. default:
  2055. error(token, "Invalid AST: Branch Statement '%.*s'", LIT(token.string));
  2056. break;
  2057. }
  2058. if (bs->label != nullptr) {
  2059. if (bs->label->kind != Ast_Ident) {
  2060. error(bs->label, "A branch statement's label name must be an identifier");
  2061. return;
  2062. }
  2063. Ast *ident = bs->label;
  2064. String name = ident->Ident.token.string;
  2065. Operand o = {};
  2066. Entity *e = check_ident(ctx, &o, ident, nullptr, nullptr, false);
  2067. if (e == nullptr) {
  2068. error(ident, "Undeclared label name: %.*s", LIT(name));
  2069. return;
  2070. }
  2071. add_entity_use(ctx, ident, e);
  2072. if (e->kind != Entity_Label) {
  2073. error(ident, "'%.*s' is not a label", LIT(name));
  2074. return;
  2075. }
  2076. Ast *parent = e->Label.parent;
  2077. GB_ASSERT(parent != nullptr);
  2078. switch (parent->kind) {
  2079. case Ast_BlockStmt:
  2080. case Ast_IfStmt:
  2081. case Ast_SwitchStmt:
  2082. if (token.kind != Token_break) {
  2083. error(bs->label, "Label '%.*s' can only be used with 'break'", LIT(e->token.string));
  2084. }
  2085. break;
  2086. case Ast_RangeStmt:
  2087. case Ast_ForStmt:
  2088. if ((token.kind != Token_break) && (token.kind != Token_continue)) {
  2089. error(bs->label, "Label '%.*s' can only be used with 'break' and 'continue'", LIT(e->token.string));
  2090. }
  2091. break;
  2092. }
  2093. }
  2094. case_end;
  2095. case_ast_node(us, UsingStmt, node);
  2096. if (us->list.count == 0) {
  2097. error(us->token, "Empty 'using' list");
  2098. return;
  2099. }
  2100. for (Ast *expr : us->list) {
  2101. expr = unparen_expr(expr);
  2102. Entity *e = nullptr;
  2103. bool is_selector = false;
  2104. Operand o = {};
  2105. switch (expr->kind) {
  2106. case Ast_Ident:
  2107. e = check_ident(ctx, &o, expr, nullptr, nullptr, true);
  2108. break;
  2109. case Ast_SelectorExpr:
  2110. e = check_selector(ctx, &o, expr, nullptr);
  2111. is_selector = true;
  2112. break;
  2113. case Ast_Implicit:
  2114. error(us->token, "'using' applied to an implicit value");
  2115. continue;
  2116. default:
  2117. error(us->token, "'using' can only be applied to an entity, got %.*s", LIT(ast_strings[expr->kind]));
  2118. continue;
  2119. }
  2120. if (!check_using_stmt_entity(ctx, us, expr, is_selector, e)) {
  2121. return;
  2122. }
  2123. }
  2124. case_end;
  2125. case_ast_node(fb, ForeignBlockDecl, node);
  2126. Ast *foreign_library = fb->foreign_library;
  2127. CheckerContext c = *ctx;
  2128. if (foreign_library->kind != Ast_Ident) {
  2129. error(foreign_library, "foreign library name must be an identifier");
  2130. } else {
  2131. c.foreign_context.curr_library = foreign_library;
  2132. c.foreign_context.default_cc = ProcCC_CDecl;
  2133. }
  2134. check_decl_attributes(&c, fb->attributes, foreign_block_decl_attribute, nullptr);
  2135. ast_node(block, BlockStmt, fb->body);
  2136. for (Ast *decl : block->stmts) {
  2137. if (decl->kind == Ast_ValueDecl && decl->ValueDecl.is_mutable) {
  2138. check_stmt(&c, decl, flags);
  2139. }
  2140. }
  2141. case_end;
  2142. case_ast_node(vd, ValueDecl, node);
  2143. check_value_decl_stmt(ctx, node, mod_flags);
  2144. case_end;
  2145. }
  2146. }