• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 "varbinder/scope.h"
20 #include "compiler/core/pandagen.h"
21 
22 namespace ark::es2panda::compiler {
HoistVar(PandaGen * pg,varbinder::Variable * var,const varbinder::VarDecl * decl)23 static void HoistVar(PandaGen *pg, varbinder::Variable *var, const varbinder::VarDecl *decl)
24 {
25     auto *scope = pg->Scope();
26 
27     if (scope->IsGlobalScope()) {
28         pg->LoadConst(decl->Node(), Constant::JS_UNDEFINED);
29         pg->StoreGlobalVar(decl->Node(), decl->Name());
30         return;
31     }
32 
33     varbinder::ConstScopeFindResult result(decl->Name(), scope, 0, var);
34 
35     pg->LoadConst(decl->Node(), Constant::JS_UNDEFINED);
36     pg->StoreAccToLexEnv(decl->Node(), result, true);
37 }
38 
HoistFunction(PandaGen * pg,varbinder::Variable * var,const varbinder::FunctionDecl * decl)39 static void HoistFunction(PandaGen *pg, varbinder::Variable *var, const varbinder::FunctionDecl *decl)
40 {
41     const ir::ScriptFunction *scriptFunction = decl->Node()->AsScriptFunction();
42     auto *scope = pg->Scope();
43 
44     const auto &internalName = scriptFunction->Scope()->InternalName();
45 
46     if (scope->IsGlobalScope()) {
47         pg->DefineFunction(decl->Node(), scriptFunction, internalName);
48         pg->StoreGlobalVar(decl->Node(), var->Declaration()->Name());
49         return;
50     }
51 
52     ES2PANDA_ASSERT(scope->IsFunctionScope() || scope->IsCatchScope() || scope->IsLocalScope() ||
53                     scope->IsModuleScope());
54     varbinder::ConstScopeFindResult result(decl->Name(), scope, 0, var);
55 
56     pg->DefineFunction(decl->Node(), scriptFunction, internalName);
57     pg->StoreAccToLexEnv(decl->Node(), result, true);
58 }
59 
Hoist(PandaGen * pg)60 void Hoisting::Hoist(PandaGen *pg)
61 {
62     const auto *scope = pg->Scope();
63 
64     for (const auto &[_, var] : scope->Bindings()) {
65         (void)_;
66         if (!var->HasFlag(varbinder::VariableFlags::HOIST)) {
67             continue;
68         }
69 
70         const auto *decl = var->Declaration();
71 
72         if (decl->IsVarDecl()) {
73             HoistVar(pg, var, decl->AsVarDecl());
74         } else {
75             ES2PANDA_ASSERT(decl->IsFunctionDecl());
76             HoistFunction(pg, var, decl->AsFunctionDecl());
77         }
78     }
79 }
80 }  // namespace ark::es2panda::compiler
81