1 /*
2 * Copyright (c) 2021-2024 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 "varbinder/declaration.h"
19 #include "varbinder/variableFlags.h"
20 #include "compiler/base/destructuring.h"
21 #include "compiler/core/function.h"
22 #include "compiler/core/pandagen.h"
23 #include "compiler/core/ETSGen.h"
24 #include "checker/types/ets/etsTupleType.h"
25 #include "ir/base/spreadElement.h"
26 #include "ir/base/classProperty.h"
27 #include "ir/base/classDefinition.h"
28 #include "ir/base/scriptFunction.h"
29 #include "ir/expressions/assignmentExpression.h"
30 #include "ir/expressions/memberExpression.h"
31 #include "ir/statements/variableDeclaration.h"
32 #include "util/helpers.h"
33
34 namespace ark::es2panda::compiler {
35
CreateBase(CodeGen * cg,const ir::AstNode * node,bool isDeclaration)36 LReference::LReferenceBase LReference::CreateBase(CodeGen *cg, const ir::AstNode *node, bool isDeclaration)
37 {
38 switch (node->Type()) {
39 // NOTE: This case is never reached in case of ETS
40 case ir::AstNodeType::IDENTIFIER: {
41 const util::StringView &name = node->AsIdentifier()->Name();
42 auto res = cg->Scope()->Find(name, varbinder::ResolveBindingOptions::ALL);
43 if (res.variable == nullptr) {
44 res.variable = node->AsIdentifier()->Variable();
45 }
46
47 return {cg, node, ReferenceKind::VAR_OR_GLOBAL, res, isDeclaration};
48 }
49 case ir::AstNodeType::MEMBER_EXPRESSION: {
50 return {cg, node, ReferenceKind::MEMBER, {}, false};
51 }
52 case ir::AstNodeType::VARIABLE_DECLARATION: {
53 ASSERT(node->AsVariableDeclaration()->Declarators().size() == 1);
54 return CreateBase(cg, node->AsVariableDeclaration()->Declarators()[0]->Id(), true);
55 }
56 case ir::AstNodeType::VARIABLE_DECLARATOR: {
57 return CreateBase(cg, node->AsVariableDeclarator()->Id(), true);
58 }
59 case ir::AstNodeType::ARRAY_PATTERN:
60 case ir::AstNodeType::OBJECT_PATTERN: {
61 return {cg, node, ReferenceKind::DESTRUCTURING, {}, isDeclaration};
62 }
63 case ir::AstNodeType::ASSIGNMENT_PATTERN: {
64 return CreateBase(cg, node->AsAssignmentPattern()->Left(), true);
65 }
66 case ir::AstNodeType::REST_ELEMENT: {
67 return CreateBase(cg, node->AsRestElement()->Argument(), true);
68 }
69 case ir::AstNodeType::TS_NON_NULL_EXPRESSION: {
70 return CreateBase(cg, node->AsTSNonNullExpression()->Expr(), isDeclaration);
71 }
72 default: {
73 UNREACHABLE();
74 }
75 }
76 }
77
JSLReference(CodeGen * cg,const ir::AstNode * node,ReferenceKind refKind,varbinder::ConstScopeFindResult res,bool isDeclaration)78 JSLReference::JSLReference(CodeGen *cg, const ir::AstNode *node, ReferenceKind refKind,
79 varbinder::ConstScopeFindResult res, bool isDeclaration)
80 : LReference(node, refKind, res, isDeclaration), pg_(static_cast<PandaGen *>(cg))
81 {
82 if (Kind() != ReferenceKind::MEMBER) {
83 return;
84 }
85
86 const auto *memberExpr = Node()->AsMemberExpression();
87
88 if (memberExpr->Object()->IsSuperExpression()) {
89 SetKind(ReferenceKind::SUPER);
90 } else if (memberExpr->IsPrivateReference()) {
91 SetKind(ReferenceKind::PRIVATE);
92 privateCtor_ = pg_->AllocReg();
93 Function::LoadClassContexts(Node(), pg_, privateCtor_, memberExpr->Property()->AsIdentifier()->Name());
94 }
95
96 obj_ = pg_->AllocReg();
97 memberExpr->Object()->Compile(pg_);
98 pg_->StoreAccumulator(Node(), obj_);
99
100 prop_ = pg_->ToNamedPropertyKey(memberExpr->Property(), memberExpr->IsComputed());
101 if (std::holds_alternative<util::StringView>(prop_)) {
102 return;
103 }
104
105 if (std::holds_alternative<int64_t>(prop_) && Kind() != ReferenceKind::SUPER) {
106 return;
107 }
108
109 memberExpr->Property()->Compile(pg_);
110
111 VReg propReg = pg_->AllocReg();
112 pg_->StoreAccumulator(Node(), propReg);
113 prop_ = propReg;
114 }
115
GetValue() const116 void JSLReference::GetValue() const
117 {
118 switch (Kind()) {
119 case ReferenceKind::VAR_OR_GLOBAL: {
120 pg_->LoadVar(Node()->AsIdentifier(), Result());
121 break;
122 }
123 case ReferenceKind::MEMBER: {
124 if (std::holds_alternative<VReg>(prop_)) {
125 pg_->LoadObjProperty(Node(), obj_);
126 break;
127 }
128 [[fallthrough]];
129 }
130 case ReferenceKind::SUPER: {
131 pg_->LoadObjProperty(Node(), prop_);
132 break;
133 }
134 case ReferenceKind::PRIVATE: {
135 pg_->ClassPrivateFieldGet(Node(), privateCtor_, obj_, std::get<util::StringView>(prop_));
136 break;
137 }
138 default: {
139 UNREACHABLE();
140 }
141 }
142 }
143
SetValue() const144 void JSLReference::SetValue() const
145 {
146 switch (Kind()) {
147 case ReferenceKind::VAR_OR_GLOBAL: {
148 pg_->StoreVar(Node(), Result(), IsDeclaration());
149 break;
150 }
151 case ReferenceKind::SUPER: {
152 pg_->StoreSuperProperty(Node(), obj_, prop_);
153
154 break;
155 }
156 case ReferenceKind::MEMBER: {
157 pg_->StoreObjProperty(Node(), obj_, prop_);
158
159 break;
160 }
161 case ReferenceKind::PRIVATE: {
162 pg_->ClassPrivateFieldSet(Node(), privateCtor_, obj_, std::get<util::StringView>(prop_));
163 break;
164 }
165 case ReferenceKind::DESTRUCTURING: {
166 Destructuring::Compile(pg_, Node()->AsExpression());
167 break;
168 }
169 default: {
170 UNREACHABLE();
171 }
172 }
173 }
174
ETSLReference(CodeGen * cg,const ir::AstNode * node,ReferenceKind refKind,varbinder::ConstScopeFindResult res,bool isDeclaration)175 ETSLReference::ETSLReference(CodeGen *cg, const ir::AstNode *node, ReferenceKind refKind,
176 varbinder::ConstScopeFindResult res, bool isDeclaration)
177 : LReference(node, refKind, res, isDeclaration), etsg_(static_cast<ETSGen *>(cg))
178 {
179 if (Kind() != ReferenceKind::MEMBER) {
180 SetKind(ResolveReferenceKind(res.variable));
181 return;
182 }
183
184 const auto *memberExpr = Node()->AsMemberExpression();
185 staticObjRef_ = memberExpr->Object()->TsType();
186 if (!memberExpr->IsComputed() && etsg_->Checker()->IsVariableStatic(memberExpr->PropVar()) &&
187 !staticObjRef_->IsETSDynamicType()) {
188 return;
189 }
190
191 TargetTypeContext ttctx(etsg_, memberExpr->Object()->TsType());
192 memberExpr->Object()->Compile(etsg_);
193 baseReg_ = etsg_->AllocReg();
194 etsg_->StoreAccumulator(node, baseReg_);
195
196 if (memberExpr->IsComputed()) {
197 TargetTypeContext pttctx(etsg_, memberExpr->Property()->TsType());
198 memberExpr->Property()->Compile(etsg_);
199 etsg_->ApplyConversion(memberExpr->Property());
200 ASSERT(etsg_->GetAccumulatorType()->HasTypeFlag(checker::TypeFlag::ETS_INTEGRAL));
201 propReg_ = etsg_->AllocReg();
202 etsg_->StoreAccumulator(node, propReg_);
203 }
204 }
205
Create(CodeGen * const cg,const ir::AstNode * const node,const bool isDeclaration)206 ETSLReference ETSLReference::Create(CodeGen *const cg, const ir::AstNode *const node, const bool isDeclaration)
207 {
208 if (node->Type() == ir::AstNodeType::IDENTIFIER) {
209 if (node->AsIdentifier()->Variable() != nullptr) {
210 auto *var = node->AsIdentifier()->Variable();
211 varbinder::ConstScopeFindResult res;
212 res.name = var->Name();
213 res.variable = var;
214 res.scope = var->GetScope();
215 auto refKind = ReferenceKind::VAR_OR_GLOBAL;
216 if (var->HasFlag(varbinder::VariableFlags::PROPERTY)) {
217 refKind = ReferenceKind::FIELD;
218 }
219 return {cg, node, refKind, res, isDeclaration};
220 }
221
222 const auto &name = node->AsIdentifier()->Name();
223 auto res = cg->Scope()->FindInFunctionScope(name, varbinder::ResolveBindingOptions::ALL);
224 if (res.variable == nullptr) {
225 res = cg->Scope()->FindInGlobal(name, varbinder::ResolveBindingOptions::ALL_VARIABLES |
226 varbinder::ResolveBindingOptions::ALL_METHOD);
227 }
228
229 return {cg, node, ReferenceKind::VAR_OR_GLOBAL, res, isDeclaration};
230 }
231 return std::make_from_tuple<ETSLReference>(CreateBase(cg, node, isDeclaration));
232 }
233
ResolveReferenceKind(const varbinder::Variable * variable)234 ReferenceKind ETSLReference::ResolveReferenceKind(const varbinder::Variable *variable)
235 {
236 if (variable->HasFlag(varbinder::VariableFlags::SYNTHETIC)) {
237 return ReferenceKind::METHOD;
238 }
239 if (variable->HasFlag(varbinder::VariableFlags::LOCAL)) {
240 return ReferenceKind::LOCAL;
241 }
242
243 auto *declNode = variable->Declaration()->Node();
244
245 switch (declNode->Type()) {
246 case ir::AstNodeType::CLASS_PROPERTY: {
247 auto *classField = declNode->AsClassProperty();
248 return classField->IsStatic() ? ReferenceKind::STATIC_FIELD : ReferenceKind::FIELD;
249 }
250 case ir::AstNodeType::CLASS_DEFINITION: {
251 auto *classDef = declNode->AsClassDefinition();
252 return classDef->IsStatic() ? ReferenceKind::STATIC_CLASS : ReferenceKind::CLASS;
253 }
254 case ir::AstNodeType::METHOD_DEFINITION: {
255 return ReferenceKind::METHOD;
256 }
257 case ir::AstNodeType::TS_INTERFACE_DECLARATION: {
258 return ReferenceKind::CLASS;
259 }
260 default: {
261 break;
262 }
263 }
264
265 return ReferenceKind::LOCAL;
266 }
267
GetValue() const268 void ETSLReference::GetValue() const
269 {
270 switch (Kind()) {
271 case ReferenceKind::MEMBER: {
272 Node()->AsMemberExpression()->Compile(etsg_);
273 break;
274 }
275 default: {
276 ASSERT(Node()->IsIdentifier());
277 auto const *const ident = Node()->AsIdentifier();
278 auto const *const variable = Variable();
279 etsg_->LoadVar(ident, variable);
280 // Process possible smart type of identifier.
281 if (auto const *const smartType = ident->TsType();
282 !etsg_->Checker()->Relation()->IsIdenticalTo(smartType, variable->TsType())) {
283 etsg_->SetAccumulatorType(smartType);
284 }
285 break;
286 }
287 }
288 }
289
SetValueComputed(const ir::MemberExpression * memberExpr) const290 void ETSLReference::SetValueComputed(const ir::MemberExpression *memberExpr) const
291 {
292 const auto *const objectType = memberExpr->Object()->TsType();
293
294 if (objectType->IsETSDynamicType()) {
295 etsg_->StoreElementDynamic(Node(), baseReg_, propReg_);
296 return;
297 }
298
299 // Same bypass for tuples, as at MemberExpression::Compile
300 const auto *const savedVregType = etsg_->GetVRegType(baseReg_);
301
302 if (objectType->IsETSTupleType()) {
303 etsg_->SetVRegType(baseReg_, objectType);
304 }
305
306 etsg_->StoreArrayElement(Node(), baseReg_, propReg_, etsg_->GetVRegType(baseReg_)->AsETSArrayType()->ElementType());
307
308 if (objectType->IsETSTupleType()) {
309 etsg_->SetVRegType(baseReg_, savedVregType);
310 }
311 }
312
SetValueGetterSetter(const ir::MemberExpression * memberExpr) const313 void ETSLReference::SetValueGetterSetter(const ir::MemberExpression *memberExpr) const
314 {
315 const auto *sig = memberExpr->PropVar()->TsType()->AsETSFunctionType()->FindSetter();
316
317 auto argReg = etsg_->AllocReg();
318 etsg_->StoreAccumulator(Node(), argReg);
319
320 if (sig->Function()->IsStatic()) {
321 etsg_->CallExact(Node(), sig->InternalName(), argReg);
322 } else {
323 etsg_->CallVirtual(Node(), sig, baseReg_, argReg);
324 }
325 }
326
SetValue() const327 void ETSLReference::SetValue() const
328 {
329 if (Kind() != ReferenceKind::MEMBER) {
330 etsg_->StoreVar(Node()->AsIdentifier(), Result());
331 return;
332 }
333
334 const auto *const memberExpr = Node()->AsMemberExpression();
335 const auto *const memberExprTsType = memberExpr->Object()->TsType()->IsETSTupleType()
336 ? memberExpr->Object()->TsType()->AsETSTupleType()->ElementType()
337 : memberExpr->TsType();
338
339 if (!memberExpr->IsIgnoreBox()) {
340 etsg_->ApplyConversion(Node(), memberExprTsType);
341 }
342
343 if (memberExpr->IsComputed()) {
344 SetValueComputed(memberExpr);
345 return;
346 }
347
348 if (memberExpr->PropVar()->TsType()->HasTypeFlag(checker::TypeFlag::GETTER_SETTER)) {
349 SetValueGetterSetter(memberExpr);
350 return;
351 }
352
353 const auto &propName = memberExpr->Property()->AsIdentifier()->Name();
354 if (memberExpr->PropVar()->HasFlag(varbinder::VariableFlags::STATIC)) {
355 const util::StringView fullName = etsg_->FormClassPropReference(staticObjRef_->AsETSObjectType(), propName);
356
357 if (staticObjRef_->IsETSDynamicType()) {
358 etsg_->StorePropertyDynamic(Node(), memberExprTsType, baseReg_, propName);
359 } else {
360 etsg_->StoreStaticProperty(Node(), memberExprTsType, fullName);
361 }
362
363 return;
364 }
365
366 auto const *objectType = memberExpr->Object()->TsType();
367
368 if (objectType->IsETSDynamicType()) {
369 etsg_->StorePropertyDynamic(Node(), memberExprTsType, baseReg_, propName);
370 return;
371 }
372
373 if (objectType->IsETSUnionType()) {
374 etsg_->StoreUnionProperty(Node(), memberExprTsType, baseReg_, propName);
375 return;
376 }
377
378 const auto *type = memberExpr->PropVar()->TsType();
379
380 etsg_->StoreProperty(Node(), type, baseReg_, propName);
381 }
382
383 } // namespace ark::es2panda::compiler
384