1 /**
2 * Copyright (c) 2024 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 var->vreg_ = vreg_;
43 return var;
44 }
45
SetLexical(Scope *scope)46 void LocalVariable::SetLexical(Scope *scope)
47 {
48 if (LexicalBound()) {
49 return;
50 }
51
52 VariableScope *varScope = scope->EnclosingVariableScope();
53
54 BindLexEnvSlot(varScope->NextSlot());
55 }
56
SetLexical([[maybe_unused]] Scope *scope)57 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical([[maybe_unused]] Scope *scope)58 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical([[maybe_unused]] Scope *scope)59 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope) {}
60
ResetDecl(Decl *decl)61 void EnumVariable::ResetDecl(Decl *decl)
62 {
63 decl_ = decl;
64 }
65 } // namespace ark::es2panda::varbinder
66