• 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 namespace panda::es2panda::binder {
21 
LocalVariable(Decl * decl,VariableFlags flags)22 LocalVariable::LocalVariable(Decl *decl, VariableFlags flags) : Variable(decl, flags)
23 {
24     if (decl->IsConstDecl()) {
25         flags_ |= VariableFlags::READONLY;
26     }
27 }
28 
Name() const29 const util::StringView &Variable::Name() const
30 {
31     return decl_->Name();
32 }
33 
Copy(ArenaAllocator * allocator,Decl * decl) const34 LocalVariable *LocalVariable::Copy(ArenaAllocator *allocator, Decl *decl) const
35 {
36     auto *var = allocator->New<LocalVariable>(decl, flags_);
37     CHECK_NOT_NULL(var);
38     var->vreg_ = vreg_;
39     return var;
40 }
41 
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)42 void LocalVariable::SetLexical(Scope *scope, util::PatchFix *patchFixHelper)
43 {
44     if (LexicalBound()) {
45         return;
46     }
47 
48     VariableScope *varScope = scope->IsFunctionParamScope() ?
49         scope->AsFunctionParamScope()->GetFunctionScope() : scope->EnclosingVariableScope();
50     CHECK_NOT_NULL(varScope);
51     uint32_t slot = 0;
52     auto name = Declaration()->Name();
53 
54     if (patchFixHelper && patchFixHelper->IsScopeValidToPatchLexical(varScope)) {
55         // get slot from symbol table for lexical variable, if not found, slot is set to UINT32_MAX
56         slot = patchFixHelper->GetSlotIdFromSymbolTable(std::string(name));
57         // Store the additional lexical variable into PatchEnv
58         if (patchFixHelper->IsAdditionalVarInPatch(slot)) {
59             patchFixHelper->AllocSlotfromPatchEnv(std::string(name));
60         } else {
61             // Just for restore 'newlexenv' instruction for func_main_0 in patch
62             varScope->RestoreFuncMain0LexEnv(patchFixHelper->GetEnvSizeOfFuncMain0());
63         }
64     } else {
65         if (decl_ && decl_->NeedSetInSendableEnv(varScope)) {
66             AddFlag(VariableFlags::IN_SENDABLE_ENV);
67             slot = varScope->NextSendableSlot();
68             BindLexEnvSlot(slot);
69             return;
70         } else {
71             slot = varScope->NextSlot();
72         }
73     }
74 
75     BindLexEnvSlot(slot);
76     // gather lexical variables for debuginfo
77     varScope->AddLexicalVarNameAndType(slot, name,
78         static_cast<typename std::underlying_type<binder::DeclType>::type>(Declaration()->Type()));
79 }
80 
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)81 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)82 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)83 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)84 void NamespaceVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)85 void ImportEqualsVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)86 void EnumLiteralVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
87 
ResetDecl(Decl * decl)88 void EnumVariable::ResetDecl(Decl *decl)
89 {
90     decl_ = decl;
91 }
92 
93 }  // namespace panda::es2panda::binder
94