• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "lreference.h"
17 
18 #include <binder/declaration.h>
19 #include <compiler/base/destructuring.h>
20 #include <compiler/core/pandagen.h>
21 #include <ir/base/spreadElement.h>
22 #include <ir/expressions/assignmentExpression.h>
23 #include <ir/expressions/identifier.h>
24 #include <ir/expressions/memberExpression.h>
25 #include <ir/statements/variableDeclaration.h>
26 #include <ir/statements/variableDeclarator.h>
27 #include <ir/ts/tsAsExpression.h>
28 #include <ir/ts/tsTypeAssertion.h>
29 
30 namespace panda::es2panda::compiler {
31 
32 // LReference
33 
LReference(const ir::AstNode * node,PandaGen * pg,bool isDeclaration,ReferenceKind refKind,binder::ScopeFindResult res)34 LReference::LReference(const ir::AstNode *node, PandaGen *pg, bool isDeclaration, ReferenceKind refKind,
35                        binder::ScopeFindResult res)
36     : node_(node), pg_(pg), refKind_(refKind), res_(res), isDeclaration_(isDeclaration)
37 {
38     if (refKind == ReferenceKind::MEMBER) {
39         obj_ = pg_->AllocReg();
40 
41         node_->AsMemberExpression()->CompileObject(pg_, obj_);
42         prop_ = node->AsMemberExpression()->CompileKey(pg_);
43     }
44 }
45 
GetValue()46 void LReference::GetValue()
47 {
48     switch (refKind_) {
49         case ReferenceKind::VAR_OR_GLOBAL: {
50             pg_->LoadVar(node_->AsIdentifier(), res_);
51             break;
52         }
53         case ReferenceKind::MEMBER: {
54             pg_->LoadObjProperty(node_, obj_, prop_);
55             break;
56         }
57         default: {
58             UNREACHABLE();
59         }
60     }
61 }
62 
SetValue()63 void LReference::SetValue()
64 {
65     switch (refKind_) {
66         case ReferenceKind::VAR_OR_GLOBAL: {
67             pg_->StoreVar(node_, res_, isDeclaration_);
68             break;
69         }
70         case ReferenceKind::MEMBER: {
71             if (node_->AsMemberExpression()->Object()->IsSuperExpression()) {
72                 pg_->StoreSuperProperty(node_, obj_, prop_);
73             } else {
74                 pg_->StoreObjProperty(node_, obj_, prop_);
75             }
76 
77             break;
78         }
79         case ReferenceKind::DESTRUCTURING: {
80             Destructuring::Compile(pg_, node_->AsExpression());
81             break;
82         }
83         default: {
84             UNREACHABLE();
85         }
86     }
87 }
88 
Kind() const89 ReferenceKind LReference::Kind() const
90 {
91     return refKind_;
92 }
93 
Variable() const94 binder::Variable *LReference::Variable() const
95 {
96     return res_.variable;
97 }
98 
CreateLRef(PandaGen * pg,const ir::AstNode * node,bool isDeclaration)99 LReference LReference::CreateLRef(PandaGen *pg, const ir::AstNode *node, bool isDeclaration)
100 {
101     switch (node->Type()) {
102         case ir::AstNodeType::IDENTIFIER: {
103             const util::StringView &name = node->AsIdentifier()->Name();
104             binder::ScopeFindResult res = pg->Scope()->Find(name);
105 
106             return {node, pg, isDeclaration, ReferenceKind::VAR_OR_GLOBAL, res};
107         }
108         case ir::AstNodeType::MEMBER_EXPRESSION: {
109             return {node, pg, false, ReferenceKind::MEMBER, {}};
110         }
111         case ir::AstNodeType::VARIABLE_DECLARATION: {
112             ASSERT(node->AsVariableDeclaration()->Declarators().size() == 1);
113             return LReference::CreateLRef(pg, node->AsVariableDeclaration()->Declarators()[0]->Id(), true);
114         }
115         case ir::AstNodeType::VARIABLE_DECLARATOR: {
116             return LReference::CreateLRef(pg, node->AsVariableDeclarator()->Id(), true);
117         }
118         case ir::AstNodeType::ARRAY_PATTERN:
119         case ir::AstNodeType::OBJECT_PATTERN:
120         case ir::AstNodeType::ARRAY_EXPRESSION:
121         case ir::AstNodeType::OBJECT_EXPRESSION: {
122             return {node, pg, isDeclaration, ReferenceKind::DESTRUCTURING, {}};
123         }
124         case ir::AstNodeType::ASSIGNMENT_PATTERN: {
125             return LReference::CreateLRef(pg, node->AsAssignmentPattern()->Left(), true);
126         }
127         case ir::AstNodeType::REST_ELEMENT: {
128             return LReference::CreateLRef(pg, node->AsRestElement()->Argument(), isDeclaration);
129         }
130         case ir::AstNodeType::EXPORT_DEFAULT_DECLARATION: {
131             // export default [anonymous class decl]
132             util::StringView name = parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME;
133             binder::ScopeFindResult res = pg->Scope()->Find(name);
134 
135             return {node, pg, isDeclaration, ReferenceKind::VAR_OR_GLOBAL, res};
136         }
137         case ir::AstNodeType::TS_AS_EXPRESSION: {
138             return LReference::CreateLRef(pg, node->AsTSAsExpression()->Expr(), isDeclaration);
139         }
140         case ir::AstNodeType::TS_TYPE_ASSERTION: {
141             return LReference::CreateLRef(pg, node->AsTSTypeAssertion()->GetExpression(), isDeclaration);
142         }
143         default: {
144             UNREACHABLE();
145         }
146     }
147 }
148 
149 }  // namespace panda::es2panda::compiler
150