• 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 <binder/scope.h>
19 
20 #include <utility>
21 
22 namespace panda::es2panda::binder {
23 
LocalVariable(Decl * decl,VariableFlags flags)24 LocalVariable::LocalVariable(Decl *decl, VariableFlags flags) : Variable(decl, flags)
25 {
26     if (decl->IsConstDecl()) {
27         flags_ |= VariableFlags::READONLY;
28     }
29 }
30 
Name() const31 const util::StringView &Variable::Name() const
32 {
33     return decl_->Name();
34 }
35 
Copy(ArenaAllocator * allocator,Decl * decl) const36 LocalVariable *LocalVariable::Copy(ArenaAllocator *allocator, Decl *decl) const
37 {
38     auto *var = allocator->New<LocalVariable>(decl, flags_);
39     var->vreg_ = vreg_;
40     return var;
41 }
42 
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)43 void LocalVariable::SetLexical(Scope *scope, util::Hotfix *hotfixHelper)
44 {
45     if (LexicalBound()) {
46         return;
47     }
48 
49     VariableScope *varScope = scope->EnclosingVariableScope();
50     uint32_t slot = 0;
51     auto name = Declaration()->Name();
52 
53     if (hotfixHelper && hotfixHelper->IsScopeValidToPatchLexical(varScope)) {
54         slot = hotfixHelper->GetSlotIdFromSymbolTable(std::string(name));
55         if (hotfixHelper->IsPatchVar(slot)) {
56             hotfixHelper->AllocSlotfromPatchEnv(std::string(name));
57         }
58     } else {
59         slot = varScope->NextSlot();
60     }
61 
62     BindLexEnvSlot(slot);
63     // gather lexical variables for debuginfo
64     varScope->AddLexicalVarNameAndType(slot, name,
65         static_cast<typename std::underlying_type<binder::DeclType>::type>(Declaration()->Type()));
66 }
67 
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)68 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::Hotfix *hotfixHelper) {}
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)69 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::Hotfix *hotfixHelper) {}
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)70 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::Hotfix *hotfixHelper) {}
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)71 void NamespaceVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::Hotfix *hotfixHelper) {}
SetLexical(Scope * scope,util::Hotfix * hotfixHelper)72 void ImportEqualsVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::Hotfix *hotfixHelper) {}
73 
ResetDecl(Decl * decl)74 void EnumVariable::ResetDecl(Decl *decl)
75 {
76     decl_ = decl;
77 }
78 
79 }  // namespace panda::es2panda::binder
80