• 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 "lexenv.h"
17 
18 #include <binder/variable.h>
19 #include <compiler/core/compilerContext.h>
20 #include <compiler/core/envScope.h>
21 #include <compiler/core/pandagen.h>
22 #include <ir/expressions/identifier.h>
23 #include <typescript/extractor/typeRecorder.h>
24 
25 namespace panda::es2panda::compiler {
26 
27 // Helpers
28 
CheckTdz(const ir::AstNode * node)29 static bool CheckTdz(const ir::AstNode *node)
30 {
31     return node->IsIdentifier() && node->AsIdentifier()->IsTdz();
32 }
33 
CheckConstAssignment(PandaGen * pg,const ir::AstNode * node,binder::Variable * variable)34 static void CheckConstAssignment(PandaGen *pg, const ir::AstNode *node, binder::Variable *variable)
35 {
36     if (!variable->Declaration()->IsConstDecl()) {
37         return;
38     }
39 
40     pg->ThrowConstAssignment(node, variable->Name());
41 }
42 
43 // VirtualLoadVar
44 
ExpandLoadLexVar(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result)45 static void ExpandLoadLexVar(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result)
46 {
47     pg->LoadLexicalVar(node, result.lexLevel, result.variable->AsLocalVariable()->LexIdx(), result.variable->Name());
48     const auto *decl = result.variable->Declaration();
49     if (decl->IsLetOrConstOrClassDecl()) {
50         pg->ThrowUndefinedIfHole(node, result.variable->Name());
51     }
52 }
53 
ExpandLoadNormalVar(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result)54 static void ExpandLoadNormalVar(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result)
55 {
56     auto *local = result.variable->AsLocalVariable();
57 
58     if (CheckTdz(node)) {
59         pg->LoadConst(node, Constant::JS_HOLE);
60         pg->ThrowUndefinedIfHole(node, local->Name());
61     } else {
62         pg->LoadAccumulator(node, local->Vreg());
63     }
64 }
65 
Expand(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result)66 void VirtualLoadVar::Expand(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result)
67 {
68     if (result.variable->LexicalBound()) {
69         ExpandLoadLexVar(pg, node, result);
70     } else {
71         ExpandLoadNormalVar(pg, node, result);
72     }
73 }
74 
75 // VirtualStoreVar
76 
ExpandStoreLexVar(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result,bool isDecl)77 static void ExpandStoreLexVar(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result, bool isDecl)
78 {
79     binder::LocalVariable *local = result.variable->AsLocalVariable();
80 
81     const auto *decl = result.variable->Declaration();
82 
83     if (decl->IsLetOrConstOrClassDecl() && !isDecl) {
84         RegScope rs(pg);
85 
86         VReg valueReg = pg->AllocReg();
87         pg->StoreAccumulator(node, valueReg);
88 
89         ExpandLoadLexVar(pg, node, result);
90 
91         if (decl->IsConstDecl()) {
92             pg->ThrowConstAssignment(node, local->Name());
93         }
94 
95         pg->LoadAccumulator(node, valueReg);
96     }
97 
98     pg->StoreLexicalVar(node, result.lexLevel, local->LexIdx(), local->Name());
99 }
100 
ExpandStoreNormalVar(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result,bool isDecl)101 static void ExpandStoreNormalVar(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result,
102                                  bool isDecl)
103 {
104     auto *local = result.variable->AsLocalVariable();
105     VReg localReg = local->Vreg();
106 
107     if (!isDecl) {
108         if (CheckTdz(node)) {
109             pg->LoadConst(node, Constant::JS_HOLE);
110             pg->ThrowUndefinedIfHole(node, local->Name());
111         }
112 
113         CheckConstAssignment(pg, node, local);
114     }
115 
116     auto context = pg->Context();
117     if (context->IsTypeExtractorEnabled()) {
118         auto typeIndex = context->TypeRecorder()->GetVariableTypeIndex(local);
119         if (typeIndex != extractor::TypeRecorder::PRIMITIVETYPE_ANY) {
120             pg->StoreAccumulatorWithType(node, typeIndex, localReg);
121 #ifndef NDEBUG
122             std::cout << "[LOG]Local vreg in variable has type index: " << local->Name() << "@" <<
123                 local << " | " << typeIndex << std::endl;
124 #endif
125             return;
126         }
127         typeIndex = context->TypeRecorder()->GetNodeTypeIndex(node);
128         if (typeIndex != extractor::TypeRecorder::PRIMITIVETYPE_ANY) {
129             pg->StoreAccumulatorWithType(node, typeIndex, localReg);
130 #ifndef NDEBUG
131             std::cout << "[LOG]Local vreg in declnode has type index: " << local->Name() << "@" <<
132                 local << " | " << typeIndex << std::endl;
133 #endif
134             return;
135         }
136 #ifndef NDEBUG
137         std::cout << "[WARNING]Local vreg lose type index: " << local->Name() << "@" << local << std::endl;
138 #endif
139     }
140     pg->StoreAccumulator(node, localReg);
141 }
142 
Expand(PandaGen * pg,const ir::AstNode * node,const binder::ScopeFindResult & result,bool isDecl)143 void VirtualStoreVar::Expand(PandaGen *pg, const ir::AstNode *node, const binder::ScopeFindResult &result, bool isDecl)
144 {
145     if (result.variable->LexicalBound()) {
146         ExpandStoreLexVar(pg, node, result, isDecl);
147     } else {
148         ExpandStoreNormalVar(pg, node, result, isDecl);
149     }
150 }
151 
152 }  // namespace panda::es2panda::compiler
153