From 31b72c840aafbb0d2799ca2ebb3e105271e30e88 Mon Sep 17 00:00:00 2001 From: hufeng Date: Thu, 29 Sep 2022 19:12:10 +0800 Subject: [PATCH] fixed 4726847 from https://gitee.com/hufeng20/arkcompiler_ets_frontend/pulls/563 Fix switchStatement's compiling bug caused by it's expression has same name declaration in it's body Signed-off-by: hufeng Change-Id: Ieccf79c24a4ffdeec20b98d4b704db71f564fbb9 --- es2panda/binder/binder.cpp | 9 +++++++-- es2panda/ir/statements/switchStatement.cpp | 3 ++- es2panda/ir/statements/switchStatement.h | 10 ++++++++++ ts2panda/src/statement/switchStatement.ts | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 43a98efbaa..f94f9f997f 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 fb9be5f8ed..6555aa6b1f 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 3389f5bd0e..029b9db0e2 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 438bf7b7a5..c47474a70b 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; -- Gitee