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