diff --git a/es2panda/binder/scope.cpp b/es2panda/binder/scope.cpp index 90a0c64cebba9176b51b860c6c8087244d6c0068..d4a452d7649c57eb934147a3d3cf04def8841108 100644 --- a/es2panda/binder/scope.cpp +++ b/es2panda/binder/scope.cpp @@ -141,6 +141,17 @@ Decl *Scope::FindDecl(const util::StringView &name) const return nullptr; } +bool Scope::HasVarDecl(const util::StringView &name) const +{ + for (auto *it : decls_) { + if (it->Name() == name && it->IsVarDecl()) { + return true; + } + } + + return false; +} + std::tuple Scope::IterateShadowedVariables(const util::StringView &name, const VariableVisitior &visitor) { auto *iter = this; @@ -175,7 +186,7 @@ bool Scope::AddLocal(ArenaAllocator *allocator, Variable *currentVariable, Decl return false; } - VariableFlags varFlags = VariableFlags::HOIST_VAR | VariableFlags::LEXICAL_VAR; + VariableFlags varFlags = VariableFlags::HOIST_VAR; if (scope->IsGlobalScope()) { scope->Bindings().insert({newDecl->Name(), allocator->New(newDecl, varFlags)}); } else { @@ -205,11 +216,7 @@ bool Scope::AddLocal(ArenaAllocator *allocator, Variable *currentVariable, Decl return false; } - auto [_, shadowed] = IterateShadowedVariables( - newDecl->Name(), [](const Variable *v) { return v->HasFlag(VariableFlags::LEXICAL_VAR); }); - (void)_; - - if (shadowed) { + if (HasVarDecl(newDecl->Name())) { return false; } diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index 7b95f5c0627ee331632642f1c535b6bbc1142a20..702b8bf4bd26d160b583e7f4c24d285acfbf33c4 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -214,6 +214,8 @@ public: Decl *FindDecl(const util::StringView &name) const; + bool HasVarDecl(const util::StringView &name) const; + protected: explicit Scope(ArenaAllocator *allocator, Scope *parent) : parent_(parent), decls_(allocator->Adapter()), bindings_(allocator->Adapter()) diff --git a/es2panda/binder/variableFlags.h b/es2panda/binder/variableFlags.h index 3a7f3a713898e34bc994833a0da56a1e1cd1c2e9..9406cf2cfdaef7f9394c1955b24ee3c638111409 100644 --- a/es2panda/binder/variableFlags.h +++ b/es2panda/binder/variableFlags.h @@ -113,11 +113,10 @@ enum class VariableFlags { LOOP_DECL = 1 << 25, PER_ITERATION = 1 << 26, - LEXICAL_VAR = 1 << 27, - HOIST = 1 << 28, - VAR = 1 << 29, - INITIALIZED = 1 << 30, - LEXICAL_BOUND = 1 << 31, + HOIST = 1 << 27, + VAR = 1 << 28, + INITIALIZED = 1 << 29, + LEXICAL_BOUND = 1 << 30, HOIST_VAR = HOIST | VAR, };