diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index d2a8b2ad397c8be9b3a5a17ca306bd6354d2d199..8c2e02f4416e0da86c302b6db2547454437067e3 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -386,9 +387,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 86ea76041aac9fdd2e73815af6df6709ba0dbdf9..2462856fc6f91966de1f19561f520d60e64bc6bd 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([[maybe_unused]] 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 6e85ce9b04ac56903ad8dc4b3776fadcc20fb44b..34eec170577af6d3336555697d99a9a2c69d5d18 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 7c4213bafa1562519ca56bd2604c0712d20f20f1..85d6fb077e8d8ace5e35543ce8e9a45c962467a2 100644 --- a/ts2panda/src/statement/switchStatement.ts +++ b/ts2panda/src/statement/switchStatement.ts @@ -96,7 +96,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(); @@ -104,6 +103,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;