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::PatchFix * patchFixHelper)43 void LocalVariable::SetLexical(Scope *scope, util::PatchFix *patchFixHelper)
44 {
45 if (LexicalBound()) {
46 return;
47 }
48
49 VariableScope *varScope = scope->IsFunctionParamScope() ?
50 scope->AsFunctionParamScope()->GetFunctionScope() : scope->EnclosingVariableScope();
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 slot = varScope->NextSlot();
66 }
67
68 BindLexEnvSlot(slot);
69 // gather lexical variables for debuginfo
70 varScope->AddLexicalVarNameAndType(slot, name,
71 static_cast<typename std::underlying_type<binder::DeclType>::type>(Declaration()->Type()));
72 }
73
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)74 void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)75 void ModuleVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)76 void EnumVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)77 void NamespaceVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)78 void ImportEqualsVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
SetLexical(Scope * scope,util::PatchFix * patchFixHelper)79 void EnumLiteralVariable::SetLexical([[maybe_unused]] Scope *scope, [[maybe_unused]] util::PatchFix *patchFixHelper) {}
80
ResetDecl(Decl * decl)81 void EnumVariable::ResetDecl(Decl *decl)
82 {
83 decl_ = decl;
84 }
85
86 } // namespace panda::es2panda::binder
87