• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "variable.h"
17 
18 #include "checker/types/type.h"
19 #include "varbinder/scope.h"
20 
21 #include <utility>
22 
23 namespace ark::es2panda::varbinder {
24 
LocalVariable(Decl * decl,VariableFlags flags)25 LocalVariable::LocalVariable(Decl *decl, VariableFlags flags) : Variable(decl, flags)
26 {
27     if (decl->IsConstDecl() || decl->IsReadonlyDecl()) {
28         flags_ |= VariableFlags::READONLY;
29     }
30 }
31 
LocalVariable(VariableFlags flags)32 LocalVariable::LocalVariable(VariableFlags flags) : Variable(flags) {}
33 
Name() const34 const util::StringView &Variable::Name() const
35 {
36     return decl_->Name();
37 }
38 
Copy(ArenaAllocator * allocator,Decl * decl) const39 LocalVariable *LocalVariable::Copy(ArenaAllocator *allocator, Decl *decl) const
40 {
41     auto *var = allocator->New<LocalVariable>(decl, flags_);
42     ES2PANDA_ASSERT(var != nullptr);
43     var->vreg_ = vreg_;
44     return var;
45 }
46 
SetLexical(Scope * scope)47 void LocalVariable::SetLexical(Scope *scope)
48 {
49     if (LexicalBound()) {
50         return;
51     }
52 
53     VariableScope *varScope = scope->EnclosingVariableScope();
54     ES2PANDA_ASSERT(varScope != nullptr);
55     BindLexEnvSlot(varScope->NextSlot());
56 }
57 
SetLexical(Scope * scope)58 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical(Scope * scope)59 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical(Scope * scope)60 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope) {}
61 
ResetDecl(Decl * decl)62 void EnumVariable::ResetDecl(Decl *decl)
63 {
64     decl_ = decl;
65 }
66 }  // namespace ark::es2panda::varbinder
67