• 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 "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 (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     binder::ScopeFindResult result(decl->Name(), scope, 0, var);
81 
82     pg->DefineFunction(decl->Node(), scriptFunction, internalName);
83     StoreModuleVarOrLocalVar(pg, result, decl);
84 }
85 
HoistNameSpaceImports(PandaGen * pg)86 static void HoistNameSpaceImports(PandaGen *pg)
87 {
88     if (pg->Scope()->IsModuleScope()) {
89         parser::SourceTextModuleRecord *moduleRecord = pg->Binder()->Program()->ModuleRecord();
90         ASSERT(moduleRecord != nullptr);
91         for (auto nameSpaceEntry : moduleRecord->GetNamespaceImportEntries()) {
92             auto *var = pg->TopScope()->FindLocal(nameSpaceEntry->localName_);
93             ASSERT(var != nullptr);
94             auto *node = var->Declaration()->Node();
95             ASSERT(node != nullptr);
96             pg->GetModuleNamespace(node, nameSpaceEntry->moduleRequestIdx_);
97             pg->StoreVar(node, {nameSpaceEntry->localName_, pg->TopScope(), 0, var}, true);
98         }
99     }
100 }
101 
Hoist(PandaGen * pg)102 void Hoisting::Hoist(PandaGen *pg)
103 {
104     const auto *scope = pg->Scope();
105 
106     for (const auto &[_, var] : scope->Bindings()) {
107         (void)_;
108         if (!var->HasFlag(binder::VariableFlags::HOIST)) {
109             continue;
110         }
111 
112         const auto *decl = var->Declaration();
113 
114         if (decl->IsVarDecl()) {
115             HoistVar(pg, var, decl->AsVarDecl());
116         } else {
117             ASSERT(decl->IsFunctionDecl());
118             HoistFunction(pg, var, decl->AsFunctionDecl());
119         }
120     }
121 
122     HoistNameSpaceImports(pg);
123 }
124 
125 }  // namespace panda::es2panda::compiler
126