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 "hoisting.h"
17
18 #include <ir/base/scriptFunction.h>
19 #include <binder/binder.h>
20 #include <binder/scope.h>
21 #include <compiler/core/pandagen.h>
22 #include <parser/module/sourceTextModuleRecord.h>
23
24 namespace panda::es2panda::compiler {
25
StoreModuleVarOrLocalVar(PandaGen * pg,binder::ScopeFindResult & result,const binder::Decl * decl)26 static void StoreModuleVarOrLocalVar(PandaGen *pg, binder::ScopeFindResult &result, const binder::Decl *decl)
27 {
28 if (decl->IsImportOrExportDecl()) {
29 ASSERT(pg->Scope()->IsModuleScope());
30 auto *var = pg->Scope()->FindLocal(decl->Name());
31 ASSERT(var->IsModuleVariable());
32 pg->StoreModuleVariable(decl->Node(), var->AsModuleVariable());
33 } else {
34 pg->StoreAccToLexEnv(decl->Node(), result, true);
35 }
36 }
37
HoistVar(PandaGen * pg,binder::Variable * var,const binder::VarDecl * decl)38 static void HoistVar(PandaGen *pg, binder::Variable *var, const binder::VarDecl *decl)
39 {
40 auto *scope = pg->Scope();
41
42 if (scope->IsGlobalScope()) {
43 pg->LoadConst(decl->Node(), Constant::JS_UNDEFINED);
44 pg->StoreGlobalVar(decl->Node(), decl->Name());
45 return;
46 }
47
48 auto *funcScope = scope->EnclosingFunctionVariableScope();
49 if (funcScope->ParamScope()->HasParam(decl->Name())) {
50 return;
51 }
52
53 binder::ScopeFindResult result(decl->Name(), scope, 0, var);
54
55 pg->LoadConst(decl->Node(), Constant::JS_UNDEFINED);
56 StoreModuleVarOrLocalVar(pg, result, decl);
57 }
58
HoistFunction(PandaGen * pg,binder::Variable * var,const binder::FunctionDecl * decl)59 static void HoistFunction(PandaGen *pg, binder::Variable *var, const binder::FunctionDecl *decl)
60 {
61 const ir::ScriptFunction *scriptFunction = decl->Node()->AsScriptFunction();
62 auto *scope = pg->Scope();
63
64 const auto &internalName = scriptFunction->Scope()->InternalName();
65
66 if (scope->IsGlobalScope()) {
67 pg->DefineFunction(decl->Node(), scriptFunction, internalName);
68 pg->StoreGlobalVar(decl->Node(), var->Declaration()->Name());
69 return;
70 }
71
72 ASSERT(scope->IsFunctionScope() || scope->IsCatchScope() || scope->IsLocalScope() ||
73 scope->IsModuleScope() || scope->IsTSModuleScope());
74 binder::ScopeFindResult result(decl->Name(), scope, 0, var);
75
76 pg->DefineFunction(decl->Node(), scriptFunction, internalName);
77 StoreModuleVarOrLocalVar(pg, result, decl);
78 }
79
HoistNameSpaceImports(PandaGen * pg)80 static void HoistNameSpaceImports(PandaGen *pg)
81 {
82 if (pg->Scope()->IsModuleScope()) {
83 parser::SourceTextModuleRecord *moduleRecord = pg->Binder()->Program()->ModuleRecord();
84 ASSERT(moduleRecord != nullptr);
85 for (auto nameSpaceEntry : moduleRecord->GetNamespaceImportEntries()) {
86 auto *var = pg->TopScope()->FindLocal(nameSpaceEntry->localName_);
87 ASSERT(var != nullptr);
88 auto *node = var->Declaration()->Node();
89 ASSERT(node != nullptr);
90 pg->GetModuleNamespace(node, nameSpaceEntry->moduleRequestIdx_);
91 pg->StoreVar(node, {nameSpaceEntry->localName_, pg->TopScope(), 0, var}, true);
92 }
93 }
94 }
95
Hoist(PandaGen * pg)96 void Hoisting::Hoist(PandaGen *pg)
97 {
98 const auto *scope = pg->Scope();
99
100 for (const auto &[_, var] : scope->Bindings()) {
101 (void)_;
102 if (!var->HasFlag(binder::VariableFlags::HOIST)) {
103 continue;
104 }
105
106 const auto *decl = var->Declaration();
107
108 if (decl->IsVarDecl()) {
109 HoistVar(pg, var, decl->AsVarDecl());
110 } else {
111 ASSERT(decl->IsFunctionDecl());
112 HoistFunction(pg, var, decl->AsFunctionDecl());
113 }
114 }
115
116 HoistNameSpaceImports(pg);
117 }
118
119 } // namespace panda::es2panda::compiler
120