13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021 - 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include "forOfStatement.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include "checker/TSchecker.h" 193af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h" 203af6ab5fSopenharmony_ci#include "compiler/core/ETSGen.h" 213af6ab5fSopenharmony_ci#include "ir/astDump.h" 223af6ab5fSopenharmony_ci#include "ir/srcDump.h" 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_cinamespace ark::es2panda::ir { 253af6ab5fSopenharmony_ci 263af6ab5fSopenharmony_cichecker::Type *ForOfStatement::CreateUnionIteratorTypes(checker::ETSChecker *checker, checker::Type *exprType) 273af6ab5fSopenharmony_ci{ 283af6ab5fSopenharmony_ci ArenaVector<checker::Type *> types(checker->Allocator()->Adapter()); 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci for (auto it : exprType->AsETSUnionType()->ConstituentTypes()) { 313af6ab5fSopenharmony_ci if (it->IsETSStringType()) { 323af6ab5fSopenharmony_ci types.push_back(checker->GetGlobalTypesHolder()->GlobalCharType()); 333af6ab5fSopenharmony_ci } else if (it->IsETSObjectType()) { 343af6ab5fSopenharmony_ci types.push_back(this->CheckIteratorMethodForObject(checker, it->AsETSObjectType())); 353af6ab5fSopenharmony_ci } else if (it->IsETSArrayType()) { 363af6ab5fSopenharmony_ci types.push_back(it->AsETSArrayType()->ElementType()->Instantiate(checker->Allocator(), checker->Relation(), 373af6ab5fSopenharmony_ci checker->GetGlobalTypesHolder())); 383af6ab5fSopenharmony_ci types.back()->RemoveTypeFlag(checker::TypeFlag::CONSTANT); 393af6ab5fSopenharmony_ci } else { 403af6ab5fSopenharmony_ci return nullptr; 413af6ab5fSopenharmony_ci } 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ci return checker->CreateETSUnionType(std::move(types)); 453af6ab5fSopenharmony_ci} 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_civoid ForOfStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) 483af6ab5fSopenharmony_ci{ 493af6ab5fSopenharmony_ci if (auto *transformedNode = cb(left_); left_ != transformedNode) { 503af6ab5fSopenharmony_ci left_->SetTransformedNode(transformationName, transformedNode); 513af6ab5fSopenharmony_ci left_ = transformedNode; 523af6ab5fSopenharmony_ci } 533af6ab5fSopenharmony_ci 543af6ab5fSopenharmony_ci if (auto *transformedNode = cb(right_); right_ != transformedNode) { 553af6ab5fSopenharmony_ci right_->SetTransformedNode(transformationName, transformedNode); 563af6ab5fSopenharmony_ci right_ = transformedNode->AsExpression(); 573af6ab5fSopenharmony_ci } 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_ci if (auto *transformedNode = cb(body_); body_ != transformedNode) { 603af6ab5fSopenharmony_ci body_->SetTransformedNode(transformationName, transformedNode); 613af6ab5fSopenharmony_ci body_ = transformedNode->AsStatement(); 623af6ab5fSopenharmony_ci } 633af6ab5fSopenharmony_ci} 643af6ab5fSopenharmony_ci 653af6ab5fSopenharmony_civoid ForOfStatement::Iterate(const NodeTraverser &cb) const 663af6ab5fSopenharmony_ci{ 673af6ab5fSopenharmony_ci cb(left_); 683af6ab5fSopenharmony_ci cb(right_); 693af6ab5fSopenharmony_ci cb(body_); 703af6ab5fSopenharmony_ci} 713af6ab5fSopenharmony_ci 723af6ab5fSopenharmony_civoid ForOfStatement::Dump(ir::AstDumper *dumper) const 733af6ab5fSopenharmony_ci{ 743af6ab5fSopenharmony_ci dumper->Add({{"type", "ForOfStatement"}, {"await", isAwait_}, {"left", left_}, {"right", right_}, {"body", body_}}); 753af6ab5fSopenharmony_ci} 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_civoid ForOfStatement::Dump(ir::SrcDumper *dumper) const 783af6ab5fSopenharmony_ci{ 793af6ab5fSopenharmony_ci ASSERT(left_ != nullptr); 803af6ab5fSopenharmony_ci ASSERT(right_ != nullptr); 813af6ab5fSopenharmony_ci dumper->Add("for "); 823af6ab5fSopenharmony_ci if (isAwait_) { 833af6ab5fSopenharmony_ci dumper->Add("await "); 843af6ab5fSopenharmony_ci } 853af6ab5fSopenharmony_ci dumper->Add("("); 863af6ab5fSopenharmony_ci left_->Dump(dumper); 873af6ab5fSopenharmony_ci dumper->Add(" of "); 883af6ab5fSopenharmony_ci right_->Dump(dumper); 893af6ab5fSopenharmony_ci dumper->Add(") {"); 903af6ab5fSopenharmony_ci if (body_ != nullptr) { 913af6ab5fSopenharmony_ci dumper->IncrIndent(); 923af6ab5fSopenharmony_ci dumper->Endl(); 933af6ab5fSopenharmony_ci body_->Dump(dumper); 943af6ab5fSopenharmony_ci dumper->DecrIndent(); 953af6ab5fSopenharmony_ci dumper->Endl(); 963af6ab5fSopenharmony_ci } 973af6ab5fSopenharmony_ci dumper->Add("}"); 983af6ab5fSopenharmony_ci} 993af6ab5fSopenharmony_ci 1003af6ab5fSopenharmony_civoid ForOfStatement::Compile(compiler::PandaGen *pg) const 1013af6ab5fSopenharmony_ci{ 1023af6ab5fSopenharmony_ci pg->GetAstCompiler()->Compile(this); 1033af6ab5fSopenharmony_ci} 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_civoid ForOfStatement::Compile(compiler::ETSGen *etsg) const 1063af6ab5fSopenharmony_ci{ 1073af6ab5fSopenharmony_ci etsg->GetAstCompiler()->Compile(this); 1083af6ab5fSopenharmony_ci} 1093af6ab5fSopenharmony_ci 1103af6ab5fSopenharmony_cichecker::Type *ForOfStatement::Check(checker::TSChecker *checker) 1113af6ab5fSopenharmony_ci{ 1123af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1133af6ab5fSopenharmony_ci} 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_cichecker::Type *ForOfStatement::Check(checker::ETSChecker *checker) 1163af6ab5fSopenharmony_ci{ 1173af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1183af6ab5fSopenharmony_ci} 1193af6ab5fSopenharmony_ci 1203af6ab5fSopenharmony_ciForOfStatement *ForOfStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent) 1213af6ab5fSopenharmony_ci{ 1223af6ab5fSopenharmony_ci auto *const left = left_ != nullptr ? left_->Clone(allocator, nullptr) : nullptr; 1233af6ab5fSopenharmony_ci auto *const right = right_ != nullptr ? right_->Clone(allocator, nullptr)->AsExpression() : nullptr; 1243af6ab5fSopenharmony_ci auto *const body = body_ != nullptr ? body_->Clone(allocator, nullptr)->AsStatement() : nullptr; 1253af6ab5fSopenharmony_ci 1263af6ab5fSopenharmony_ci if (auto *const clone = allocator->New<ForOfStatement>(left, right, body, isAwait_); clone != nullptr) { 1273af6ab5fSopenharmony_ci if (left != nullptr) { 1283af6ab5fSopenharmony_ci left->SetParent(clone); 1293af6ab5fSopenharmony_ci } 1303af6ab5fSopenharmony_ci 1313af6ab5fSopenharmony_ci if (right != nullptr) { 1323af6ab5fSopenharmony_ci right->SetParent(clone); 1333af6ab5fSopenharmony_ci } 1343af6ab5fSopenharmony_ci 1353af6ab5fSopenharmony_ci if (body != nullptr) { 1363af6ab5fSopenharmony_ci body->SetParent(clone); 1373af6ab5fSopenharmony_ci } 1383af6ab5fSopenharmony_ci 1393af6ab5fSopenharmony_ci if (parent != nullptr) { 1403af6ab5fSopenharmony_ci clone->SetParent(parent); 1413af6ab5fSopenharmony_ci } 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_ci clone->SetRange(Range()); 1443af6ab5fSopenharmony_ci return clone; 1453af6ab5fSopenharmony_ci } 1463af6ab5fSopenharmony_ci 1473af6ab5fSopenharmony_ci throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); 1483af6ab5fSopenharmony_ci} 1493af6ab5fSopenharmony_ci 1503af6ab5fSopenharmony_cichecker::Type *ForOfStatement::CheckIteratorMethodForObject(checker::ETSChecker *checker, 1513af6ab5fSopenharmony_ci checker::ETSObjectType *sourceType) 1523af6ab5fSopenharmony_ci{ 1533af6ab5fSopenharmony_ci auto const &position = right_->Start(); 1543af6ab5fSopenharmony_ci 1553af6ab5fSopenharmony_ci checker::PropertySearchFlags searchFlag = 1563af6ab5fSopenharmony_ci checker::PropertySearchFlags::SEARCH_METHOD | checker::PropertySearchFlags::IS_FUNCTIONAL; 1573af6ab5fSopenharmony_ci searchFlag |= checker::PropertySearchFlags::SEARCH_IN_BASE | checker::PropertySearchFlags::SEARCH_IN_INTERFACES; 1583af6ab5fSopenharmony_ci // NOTE: maybe we need to exclude static methods: search_flag &= ~(checker::PropertySearchFlags::SEARCH_STATIC) 1593af6ab5fSopenharmony_ci 1603af6ab5fSopenharmony_ci if (sourceType->HasTypeFlag(checker::TypeFlag::GENERIC)) { 1613af6ab5fSopenharmony_ci searchFlag |= checker::PropertySearchFlags::SEARCH_ALL; 1623af6ab5fSopenharmony_ci } 1633af6ab5fSopenharmony_ci 1643af6ab5fSopenharmony_ci auto *const method = sourceType->GetProperty(compiler::Signatures::ITERATOR_METHOD, searchFlag); 1653af6ab5fSopenharmony_ci if (method == nullptr || !method->HasFlag(varbinder::VariableFlags::METHOD)) { 1663af6ab5fSopenharmony_ci checker->LogTypeError("Object type doesn't have proper iterator method.", position); 1673af6ab5fSopenharmony_ci return nullptr; 1683af6ab5fSopenharmony_ci } 1693af6ab5fSopenharmony_ci 1703af6ab5fSopenharmony_ci ArenaVector<Expression *> arguments {checker->Allocator()->Adapter()}; 1713af6ab5fSopenharmony_ci auto &signatures = checker->GetTypeOfVariable(method)->AsETSFunctionType()->CallSignatures(); 1723af6ab5fSopenharmony_ci 1733af6ab5fSopenharmony_ci checker::Signature *signature = checker->ValidateSignatures(signatures, nullptr, arguments, position, "iterator", 1743af6ab5fSopenharmony_ci checker::TypeRelationFlag::NO_THROW); 1753af6ab5fSopenharmony_ci if (signature == nullptr) { 1763af6ab5fSopenharmony_ci checker->LogTypeError("Cannot find iterator method with the required signature.", position); 1773af6ab5fSopenharmony_ci return nullptr; 1783af6ab5fSopenharmony_ci } 1793af6ab5fSopenharmony_ci checker->ValidateSignatureAccessibility(sourceType, nullptr, signature, position, 1803af6ab5fSopenharmony_ci "Iterator method is not visible here."); 1813af6ab5fSopenharmony_ci 1823af6ab5fSopenharmony_ci ASSERT(signature->Function() != nullptr); 1833af6ab5fSopenharmony_ci 1843af6ab5fSopenharmony_ci if (signature->Function()->IsThrowing() || signature->Function()->IsRethrowing()) { 1853af6ab5fSopenharmony_ci checker->CheckThrowingStatements(this); 1863af6ab5fSopenharmony_ci } 1873af6ab5fSopenharmony_ci 1883af6ab5fSopenharmony_ci if (!CheckReturnTypeOfIteratorMethod(checker, sourceType, signature, position)) { 1893af6ab5fSopenharmony_ci return nullptr; 1903af6ab5fSopenharmony_ci } 1913af6ab5fSopenharmony_ci 1923af6ab5fSopenharmony_ci auto *const nextMethod = 1933af6ab5fSopenharmony_ci signature->ReturnType()->AsETSObjectType()->GetProperty(ITERATOR_INTERFACE_METHOD, searchFlag); 1943af6ab5fSopenharmony_ci if (nextMethod == nullptr || !nextMethod->HasFlag(varbinder::VariableFlags::METHOD)) { 1953af6ab5fSopenharmony_ci checker->LogTypeError("Iterator object doesn't have proper next method.", position); 1963af6ab5fSopenharmony_ci return nullptr; 1973af6ab5fSopenharmony_ci } 1983af6ab5fSopenharmony_ci 1993af6ab5fSopenharmony_ci auto &nextSignatures = checker->GetTypeOfVariable(nextMethod)->AsETSFunctionType()->CallSignatures(); 2003af6ab5fSopenharmony_ci 2013af6ab5fSopenharmony_ci auto const *const nextSignature = checker->ValidateSignatures(nextSignatures, nullptr, arguments, position, 2023af6ab5fSopenharmony_ci "iterator", checker::TypeRelationFlag::NO_THROW); 2033af6ab5fSopenharmony_ci if (nextSignature != nullptr && nextSignature->ReturnType()->IsETSObjectType()) { 2043af6ab5fSopenharmony_ci if (auto const *const resultType = nextSignature->ReturnType()->AsETSObjectType(); 2053af6ab5fSopenharmony_ci resultType->Name().Is(ITERATOR_RESULT_NAME)) { 2063af6ab5fSopenharmony_ci return resultType->TypeArguments()[0]; 2073af6ab5fSopenharmony_ci } 2083af6ab5fSopenharmony_ci } 2093af6ab5fSopenharmony_ci 2103af6ab5fSopenharmony_ci return nullptr; 2113af6ab5fSopenharmony_ci} 2123af6ab5fSopenharmony_ci 2133af6ab5fSopenharmony_cibool ForOfStatement::CheckReturnTypeOfIteratorMethod(checker::ETSChecker *checker, checker::ETSObjectType *sourceType, 2143af6ab5fSopenharmony_ci checker::Signature *signature, 2153af6ab5fSopenharmony_ci const lexer::SourcePosition &position) 2163af6ab5fSopenharmony_ci{ 2173af6ab5fSopenharmony_ci if ((signature->ReturnType() == nullptr || signature->ReturnType() == checker->GlobalVoidType()) && 2183af6ab5fSopenharmony_ci signature->Function()->HasBody() && signature->Function()->Body()->IsBlockStatement()) { 2193af6ab5fSopenharmony_ci for (auto *const it : signature->Function()->Body()->AsBlockStatement()->Statements()) { 2203af6ab5fSopenharmony_ci if (it->IsReturnStatement()) { 2213af6ab5fSopenharmony_ci checker::SavedCheckerContext savedContext(checker, checker::CheckerStatus::IN_CLASS, sourceType); 2223af6ab5fSopenharmony_ci it->AsReturnStatement()->Check(checker); 2233af6ab5fSopenharmony_ci break; 2243af6ab5fSopenharmony_ci } 2253af6ab5fSopenharmony_ci } 2263af6ab5fSopenharmony_ci } 2273af6ab5fSopenharmony_ci 2283af6ab5fSopenharmony_ci if (signature->ReturnType() != nullptr && signature->ReturnType()->IsETSObjectType() && 2293af6ab5fSopenharmony_ci ForOfStatement::CheckIteratorInterfaceForObject(checker, signature->ReturnType()->AsETSObjectType())) { 2303af6ab5fSopenharmony_ci return true; 2313af6ab5fSopenharmony_ci } 2323af6ab5fSopenharmony_ci 2333af6ab5fSopenharmony_ci checker->LogTypeError("Iterator method must return an object which implements Iterator<T>", position); 2343af6ab5fSopenharmony_ci return false; 2353af6ab5fSopenharmony_ci} 2363af6ab5fSopenharmony_ci 2373af6ab5fSopenharmony_cibool ForOfStatement::CheckIteratorInterfaceForObject(checker::ETSChecker *checker, checker::ETSObjectType *obj) 2383af6ab5fSopenharmony_ci{ 2393af6ab5fSopenharmony_ci for (auto *const it : obj->Interfaces()) { 2403af6ab5fSopenharmony_ci if (it->Name().Is(ITERATOR_INTERFACE_NAME)) { 2413af6ab5fSopenharmony_ci return true; 2423af6ab5fSopenharmony_ci } 2433af6ab5fSopenharmony_ci } 2443af6ab5fSopenharmony_ci 2453af6ab5fSopenharmony_ci return obj->SuperType() != nullptr && obj->SuperType()->IsETSObjectType() && 2463af6ab5fSopenharmony_ci CheckIteratorInterfaceForObject(checker, obj->SuperType()->AsETSObjectType()); 2473af6ab5fSopenharmony_ci} 2483af6ab5fSopenharmony_ci 2493af6ab5fSopenharmony_cichecker::Type *ForOfStatement::CheckIteratorMethod(checker::ETSChecker *const checker) 2503af6ab5fSopenharmony_ci{ 2513af6ab5fSopenharmony_ci if (auto *exprType = right_->TsType(); exprType != nullptr) { 2523af6ab5fSopenharmony_ci if (exprType->IsETSObjectType()) { 2533af6ab5fSopenharmony_ci return CheckIteratorMethodForObject(checker, exprType->AsETSObjectType()); 2543af6ab5fSopenharmony_ci } 2553af6ab5fSopenharmony_ci 2563af6ab5fSopenharmony_ci if (exprType->IsETSUnionType()) { 2573af6ab5fSopenharmony_ci return this->CreateUnionIteratorTypes(checker, exprType); 2583af6ab5fSopenharmony_ci } 2593af6ab5fSopenharmony_ci } 2603af6ab5fSopenharmony_ci 2613af6ab5fSopenharmony_ci return nullptr; 2623af6ab5fSopenharmony_ci} 2633af6ab5fSopenharmony_ci} // namespace ark::es2panda::ir 264