diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 43a98efbaadad7c669278604df5e3d24fc8176da..f94f9f997f68ec36eef47afa22f05cdcc8ba1a53 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -499,9 +500,13 @@ void Binder::ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode) break; } case ir::AstNodeType::SWITCH_STATEMENT: { - auto scopeCtx = LexicalScope::Enter(this, childNode->AsSwitchStatement()->Scope()); + auto *switchStatement = childNode->AsSwitchStatement(); + ResolveReference(switchStatement, switchStatement->Discriminant()); - ResolveReferences(childNode); + auto scopeCtx = LexicalScope::Enter(this, childNode->AsSwitchStatement()->Scope()); + for (auto *it : switchStatement->Cases()) { + ResolveReference(switchStatement, it); + } break; } case ir::AstNodeType::DO_WHILE_STATEMENT: { diff --git a/es2panda/ir/statements/switchStatement.cpp b/es2panda/ir/statements/switchStatement.cpp index fb9be5f8ed81f7fbc0414b6f542601cea357af26..6555aa6b1fa86b29d5b2205e267b3851facb7503 100644 --- a/es2panda/ir/statements/switchStatement.cpp +++ b/es2panda/ir/statements/switchStatement.cpp @@ -42,11 +42,12 @@ void SwitchStatement::Dump(ir::AstDumper *dumper) const void SwitchStatement::Compile(compiler::PandaGen *pg) const { - compiler::LocalRegScope lrs(pg, scope_); compiler::SwitchBuilder builder(pg, this); compiler::VReg tag = pg->AllocReg(); builder.CompileTagOfSwitch(tag); + + compiler::LocalRegScope lrs(pg, scope_); uint32_t defaultIndex = 0; for (size_t i = 0; i < cases_.size(); i++) { diff --git a/es2panda/ir/statements/switchStatement.h b/es2panda/ir/statements/switchStatement.h index 3389f5bd0e39b27492650806f1f60bc12dcc769f..029b9db0e2e9eb07b60390f289a16c4a502edefd 100644 --- a/es2panda/ir/statements/switchStatement.h +++ b/es2panda/ir/statements/switchStatement.h @@ -49,11 +49,21 @@ public: return discriminant_; } + Expression *Discriminant() + { + return discriminant_; + } + const ArenaVector &Cases() const { return cases_; } + ArenaVector &Cases() + { + return cases_; + } + binder::LocalScope *Scope() const { return scope_; diff --git a/ts2panda/src/statement/switchStatement.ts b/ts2panda/src/statement/switchStatement.ts index 438bf7b7a570b8b77ec70e5c80bfb7411cb3bae5..c47474a70bc28a44bf94c9cd1abbeb4cc6c45c1e 100644 --- a/ts2panda/src/statement/switchStatement.ts +++ b/ts2panda/src/statement/switchStatement.ts @@ -99,7 +99,6 @@ export class SwitchBase { } export function compileSwitchStatement(stmt: ts.SwitchStatement, compiler: Compiler) { - compiler.pushScope(stmt); let pandaGen = compiler.getPandaGen(); let caseNums = stmt.caseBlock.clauses.length; let switchEndLabel = new Label(); @@ -107,6 +106,7 @@ export function compileSwitchStatement(stmt: ts.SwitchStatement, compiler: Compi let tagReg = pandaGen.getTemp(); switchBuilder.compileTagOfSwitch(tagReg); + compiler.pushScope(stmt); let caseTargets = stmt.caseBlock.clauses; let defaultIndex = 0; let defaultCnt = 0;