• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 "varbinder/scope.h"
19 
20 #include <utility>
21 
22 namespace panda::es2panda::varbinder {
LocalVariable(Decl * decl,VariableFlags flags)23 LocalVariable::LocalVariable(Decl *decl, VariableFlags flags) : Variable(decl, flags)
24 {
25     if (decl->IsConstDecl()) {
26         flags_ |= VariableFlags::READONLY;
27     }
28 }
29 
LocalVariable(VariableFlags flags)30 LocalVariable::LocalVariable(VariableFlags flags) : Variable(flags) {}
31 
Name() const32 const util::StringView &Variable::Name() const
33 {
34     return decl_->Name();
35 }
36 
Copy(ArenaAllocator * allocator,Decl * decl) const37 LocalVariable *LocalVariable::Copy(ArenaAllocator *allocator, Decl *decl) const
38 {
39     auto *var = allocator->New<LocalVariable>(decl, flags_);
40     var->vreg_ = vreg_;
41     return var;
42 }
43 
SetLexical(Scope * scope)44 void LocalVariable::SetLexical(Scope *scope)
45 {
46     if (LexicalBound()) {
47         return;
48     }
49 
50     VariableScope *varScope = scope->EnclosingVariableScope();
51 
52     BindLexEnvSlot(varScope->NextSlot());
53 }
54 
SetLexical(Scope * scope)55 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical(Scope * scope)56 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope) {}
SetLexical(Scope * scope)57 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope) {}
58 
ResetDecl(Decl * decl)59 void EnumVariable::ResetDecl(Decl *decl)
60 {
61     decl_ = decl;
62 }
63 }  // namespace panda::es2panda::varbinder
64