• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "forOfStatement.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace ark::es2panda::ir {
25 
CreateUnionIteratorTypes(checker::ETSChecker * checker,checker::Type * exprType)26 checker::Type *ForOfStatement::CreateUnionIteratorTypes(checker::ETSChecker *checker, checker::Type *exprType)
27 {
28     ArenaVector<checker::Type *> types(checker->Allocator()->Adapter());
29 
30     for (auto it : exprType->AsETSUnionType()->ConstituentTypes()) {
31         if (it->IsETSStringType()) {
32             types.push_back(checker->GetGlobalTypesHolder()->GlobalCharType());
33         } else if (it->IsETSObjectType()) {
34             types.push_back(this->CheckIteratorMethodForObject(checker, it->AsETSObjectType()));
35         } else if (it->IsETSArrayType()) {
36             types.push_back(it->AsETSArrayType()->ElementType()->Instantiate(checker->Allocator(), checker->Relation(),
37                                                                              checker->GetGlobalTypesHolder()));
38             types.back()->RemoveTypeFlag(checker::TypeFlag::CONSTANT);
39         } else {
40             return nullptr;
41         }
42     }
43 
44     return checker->CreateETSUnionType(std::move(types));
45 }
46 
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)47 void ForOfStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
48 {
49     if (auto *transformedNode = cb(left_); left_ != transformedNode) {
50         left_->SetTransformedNode(transformationName, transformedNode);
51         left_ = transformedNode;
52     }
53 
54     if (auto *transformedNode = cb(right_); right_ != transformedNode) {
55         right_->SetTransformedNode(transformationName, transformedNode);
56         right_ = transformedNode->AsExpression();
57     }
58 
59     if (auto *transformedNode = cb(body_); body_ != transformedNode) {
60         body_->SetTransformedNode(transformationName, transformedNode);
61         body_ = transformedNode->AsStatement();
62     }
63 }
64 
Iterate(const NodeTraverser & cb) const65 void ForOfStatement::Iterate(const NodeTraverser &cb) const
66 {
67     cb(left_);
68     cb(right_);
69     cb(body_);
70 }
71 
Dump(ir::AstDumper * dumper) const72 void ForOfStatement::Dump(ir::AstDumper *dumper) const
73 {
74     dumper->Add({{"type", "ForOfStatement"}, {"await", isAwait_}, {"left", left_}, {"right", right_}, {"body", body_}});
75 }
76 
Dump(ir::SrcDumper * dumper) const77 void ForOfStatement::Dump(ir::SrcDumper *dumper) const
78 {
79     ASSERT(left_ != nullptr);
80     ASSERT(right_ != nullptr);
81     dumper->Add("for ");
82     if (isAwait_) {
83         dumper->Add("await ");
84     }
85     dumper->Add("(");
86     left_->Dump(dumper);
87     dumper->Add(" of ");
88     right_->Dump(dumper);
89     dumper->Add(") {");
90     if (body_ != nullptr) {
91         dumper->IncrIndent();
92         dumper->Endl();
93         body_->Dump(dumper);
94         dumper->DecrIndent();
95         dumper->Endl();
96     }
97     dumper->Add("}");
98 }
99 
Compile(compiler::PandaGen * pg) const100 void ForOfStatement::Compile(compiler::PandaGen *pg) const
101 {
102     pg->GetAstCompiler()->Compile(this);
103 }
104 
Compile(compiler::ETSGen * etsg) const105 void ForOfStatement::Compile(compiler::ETSGen *etsg) const
106 {
107     etsg->GetAstCompiler()->Compile(this);
108 }
109 
Check(checker::TSChecker * checker)110 checker::Type *ForOfStatement::Check(checker::TSChecker *checker)
111 {
112     return checker->GetAnalyzer()->Check(this);
113 }
114 
Check(checker::ETSChecker * checker)115 checker::Type *ForOfStatement::Check(checker::ETSChecker *checker)
116 {
117     return checker->GetAnalyzer()->Check(this);
118 }
119 
Clone(ArenaAllocator * const allocator,AstNode * const parent)120 ForOfStatement *ForOfStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
121 {
122     auto *const left = left_ != nullptr ? left_->Clone(allocator, nullptr) : nullptr;
123     auto *const right = right_ != nullptr ? right_->Clone(allocator, nullptr)->AsExpression() : nullptr;
124     auto *const body = body_ != nullptr ? body_->Clone(allocator, nullptr)->AsStatement() : nullptr;
125 
126     if (auto *const clone = allocator->New<ForOfStatement>(left, right, body, isAwait_); clone != nullptr) {
127         if (left != nullptr) {
128             left->SetParent(clone);
129         }
130 
131         if (right != nullptr) {
132             right->SetParent(clone);
133         }
134 
135         if (body != nullptr) {
136             body->SetParent(clone);
137         }
138 
139         if (parent != nullptr) {
140             clone->SetParent(parent);
141         }
142 
143         clone->SetRange(Range());
144         return clone;
145     }
146 
147     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
148 }
149 
CheckIteratorMethodForObject(checker::ETSChecker * checker,checker::ETSObjectType * sourceType)150 checker::Type *ForOfStatement::CheckIteratorMethodForObject(checker::ETSChecker *checker,
151                                                             checker::ETSObjectType *sourceType)
152 {
153     auto const &position = right_->Start();
154 
155     checker::PropertySearchFlags searchFlag =
156         checker::PropertySearchFlags::SEARCH_METHOD | checker::PropertySearchFlags::IS_FUNCTIONAL;
157     searchFlag |= checker::PropertySearchFlags::SEARCH_IN_BASE | checker::PropertySearchFlags::SEARCH_IN_INTERFACES;
158     // NOTE: maybe we need to exclude static methods: search_flag &= ~(checker::PropertySearchFlags::SEARCH_STATIC)
159 
160     if (sourceType->HasTypeFlag(checker::TypeFlag::GENERIC)) {
161         searchFlag |= checker::PropertySearchFlags::SEARCH_ALL;
162     }
163 
164     auto *const method = sourceType->GetProperty(compiler::Signatures::ITERATOR_METHOD, searchFlag);
165     if (method == nullptr || !method->HasFlag(varbinder::VariableFlags::METHOD)) {
166         checker->ThrowTypeError("Object type doesn't have proper iterator method.", position);
167     }
168 
169     ArenaVector<Expression *> arguments {checker->Allocator()->Adapter()};
170     auto &signatures = checker->GetTypeOfVariable(method)->AsETSFunctionType()->CallSignatures();
171 
172     checker::Signature *signature = checker->ValidateSignatures(signatures, nullptr, arguments, position, "iterator",
173                                                                 checker::TypeRelationFlag::NO_THROW);
174     if (signature == nullptr) {
175         checker->ThrowTypeError("Cannot find iterator method with the required signature.", position);
176     }
177     checker->ValidateSignatureAccessibility(sourceType, nullptr, signature, position,
178                                             "Iterator method is not visible here.");
179 
180     ASSERT(signature->Function() != nullptr);
181 
182     if (signature->Function()->IsThrowing() || signature->Function()->IsRethrowing()) {
183         checker->CheckThrowingStatements(this);
184     }
185 
186     CheckReturnTypeOfIteratorMethod(checker, sourceType, signature, position);
187 
188     auto *const nextMethod =
189         signature->ReturnType()->AsETSObjectType()->GetProperty(ITERATOR_INTERFACE_METHOD, searchFlag);
190     if (nextMethod == nullptr || !nextMethod->HasFlag(varbinder::VariableFlags::METHOD)) {
191         checker->ThrowTypeError("Iterator object doesn't have proper next method.", position);
192     }
193 
194     auto &nextSignatures = checker->GetTypeOfVariable(nextMethod)->AsETSFunctionType()->CallSignatures();
195 
196     auto const *const nextSignature = checker->ValidateSignatures(nextSignatures, nullptr, arguments, position,
197                                                                   "iterator", checker::TypeRelationFlag::NO_THROW);
198     if (nextSignature != nullptr && nextSignature->ReturnType()->IsETSObjectType()) {
199         if (auto const *const resultType = nextSignature->ReturnType()->AsETSObjectType();
200             resultType->Name().Is(ITERATOR_RESULT_NAME)) {
201             return resultType->TypeArguments()[0];
202         }
203     }
204 
205     return nullptr;
206 }
207 
CheckReturnTypeOfIteratorMethod(checker::ETSChecker * checker,checker::ETSObjectType * sourceType,checker::Signature * signature,const lexer::SourcePosition & position)208 void ForOfStatement::CheckReturnTypeOfIteratorMethod(checker::ETSChecker *checker, checker::ETSObjectType *sourceType,
209                                                      checker::Signature *signature,
210                                                      const lexer::SourcePosition &position)
211 {
212     if ((signature->ReturnType() == nullptr || signature->ReturnType() == checker->GlobalVoidType()) &&
213         signature->Function()->HasBody() && signature->Function()->Body()->IsBlockStatement()) {
214         for (auto *const it : signature->Function()->Body()->AsBlockStatement()->Statements()) {
215             if (it->IsReturnStatement()) {
216                 checker::SavedCheckerContext savedContext(checker, checker::CheckerStatus::IN_CLASS, sourceType);
217                 it->AsReturnStatement()->Check(checker);
218                 break;
219             }
220         }
221     }
222 
223     if (signature->ReturnType() != nullptr && signature->ReturnType()->IsETSObjectType() &&
224         ForOfStatement::CheckIteratorInterfaceForObject(checker, signature->ReturnType()->AsETSObjectType())) {
225         return;
226     }
227 
228     checker->ThrowTypeError("Iterator method must return an object which implements Iterator<T>", position);
229 }
230 
CheckIteratorInterfaceForObject(checker::ETSChecker * checker,checker::ETSObjectType * obj)231 bool ForOfStatement::CheckIteratorInterfaceForObject(checker::ETSChecker *checker, checker::ETSObjectType *obj)
232 {
233     for (auto *const it : obj->Interfaces()) {
234         if (it->Name().Is(ITERATOR_INTERFACE_NAME)) {
235             return true;
236         }
237     }
238 
239     return obj->SuperType() != nullptr && obj->SuperType()->IsETSObjectType() &&
240            CheckIteratorInterfaceForObject(checker, obj->SuperType()->AsETSObjectType());
241 }
242 
CheckIteratorMethod(checker::ETSChecker * const checker)243 checker::Type *ForOfStatement::CheckIteratorMethod(checker::ETSChecker *const checker)
244 {
245     if (auto *exprType = right_->TsType(); exprType != nullptr) {
246         if (exprType->IsETSObjectType()) {
247             return CheckIteratorMethodForObject(checker, exprType->AsETSObjectType());
248         }
249 
250         if (exprType->IsETSUnionType()) {
251             return this->CreateUnionIteratorTypes(checker, exprType);
252         }
253     }
254 
255     return nullptr;
256 }
257 }  // namespace ark::es2panda::ir
258