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 "etsParameterExpression.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include "checker/ETSchecker.h" 193af6ab5fSopenharmony_ci#include "checker/ets/typeRelationContext.h" 203af6ab5fSopenharmony_ci#include "checker/TSchecker.h" 213af6ab5fSopenharmony_ci#include "compiler/core/ETSGen.h" 223af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h" 233af6ab5fSopenharmony_ci#include "ir/astDump.h" 243af6ab5fSopenharmony_ci#include "ir/srcDump.h" 253af6ab5fSopenharmony_ci#include "ir/typeNode.h" 263af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h" 273af6ab5fSopenharmony_ci#include "ir/base/spreadElement.h" 283af6ab5fSopenharmony_ci 293af6ab5fSopenharmony_cinamespace ark::es2panda::ir { 303af6ab5fSopenharmony_ci 313af6ab5fSopenharmony_ciETSParameterExpression::ETSParameterExpression(AnnotatedExpression *const identOrSpread, Expression *const initializer) 323af6ab5fSopenharmony_ci : Expression(AstNodeType::ETS_PARAMETER_EXPRESSION), initializer_(initializer) 333af6ab5fSopenharmony_ci{ 343af6ab5fSopenharmony_ci ASSERT(identOrSpread != nullptr); 353af6ab5fSopenharmony_ci identOrSpread->SetParent(this); 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci if (identOrSpread->IsIdentifier()) { 383af6ab5fSopenharmony_ci ident_ = identOrSpread->AsIdentifier(); 393af6ab5fSopenharmony_ci } else if (identOrSpread->IsRestElement()) { 403af6ab5fSopenharmony_ci spread_ = identOrSpread->AsRestElement(); 413af6ab5fSopenharmony_ci ASSERT(spread_->Argument()->IsIdentifier()); 423af6ab5fSopenharmony_ci ident_ = spread_->Argument()->AsIdentifier(); 433af6ab5fSopenharmony_ci ident_->SetParent(spread_); 443af6ab5fSopenharmony_ci initializer_ = nullptr; // Just in case! 453af6ab5fSopenharmony_ci } else { 463af6ab5fSopenharmony_ci UNREACHABLE(); 473af6ab5fSopenharmony_ci } 483af6ab5fSopenharmony_ci} 493af6ab5fSopenharmony_ci 503af6ab5fSopenharmony_ciconst Identifier *ETSParameterExpression::Ident() const noexcept 513af6ab5fSopenharmony_ci{ 523af6ab5fSopenharmony_ci return ident_; 533af6ab5fSopenharmony_ci} 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_ciIdentifier *ETSParameterExpression::Ident() noexcept 563af6ab5fSopenharmony_ci{ 573af6ab5fSopenharmony_ci return ident_; 583af6ab5fSopenharmony_ci} 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ciconst SpreadElement *ETSParameterExpression::RestParameter() const noexcept 613af6ab5fSopenharmony_ci{ 623af6ab5fSopenharmony_ci return spread_; 633af6ab5fSopenharmony_ci} 643af6ab5fSopenharmony_ci 653af6ab5fSopenharmony_ciSpreadElement *ETSParameterExpression::RestParameter() noexcept 663af6ab5fSopenharmony_ci{ 673af6ab5fSopenharmony_ci return spread_; 683af6ab5fSopenharmony_ci} 693af6ab5fSopenharmony_ci 703af6ab5fSopenharmony_ciconst Expression *ETSParameterExpression::Initializer() const noexcept 713af6ab5fSopenharmony_ci{ 723af6ab5fSopenharmony_ci return initializer_; 733af6ab5fSopenharmony_ci} 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ciExpression *ETSParameterExpression::Initializer() noexcept 763af6ab5fSopenharmony_ci{ 773af6ab5fSopenharmony_ci return initializer_; 783af6ab5fSopenharmony_ci} 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_civarbinder::Variable *ETSParameterExpression::Variable() const noexcept 813af6ab5fSopenharmony_ci{ 823af6ab5fSopenharmony_ci return ident_->Variable(); 833af6ab5fSopenharmony_ci} 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_ciTypeNode const *ETSParameterExpression::TypeAnnotation() const noexcept 863af6ab5fSopenharmony_ci{ 873af6ab5fSopenharmony_ci return !IsRestParameter() ? ident_->TypeAnnotation() : spread_->TypeAnnotation(); 883af6ab5fSopenharmony_ci} 893af6ab5fSopenharmony_ci 903af6ab5fSopenharmony_ciTypeNode *ETSParameterExpression::TypeAnnotation() noexcept 913af6ab5fSopenharmony_ci{ 923af6ab5fSopenharmony_ci return !IsRestParameter() ? ident_->TypeAnnotation() : spread_->TypeAnnotation(); 933af6ab5fSopenharmony_ci} 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_civoid ETSParameterExpression::SetVariable(varbinder::Variable *const variable) noexcept 963af6ab5fSopenharmony_ci{ 973af6ab5fSopenharmony_ci ident_->SetVariable(variable); 983af6ab5fSopenharmony_ci} 993af6ab5fSopenharmony_ci 1003af6ab5fSopenharmony_civoid ETSParameterExpression::SetLexerSaved(util::StringView s) noexcept 1013af6ab5fSopenharmony_ci{ 1023af6ab5fSopenharmony_ci savedLexer_ = s; 1033af6ab5fSopenharmony_ci} 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ciutil::StringView ETSParameterExpression::LexerSaved() const noexcept 1063af6ab5fSopenharmony_ci{ 1073af6ab5fSopenharmony_ci return savedLexer_; 1083af6ab5fSopenharmony_ci} 1093af6ab5fSopenharmony_ci 1103af6ab5fSopenharmony_civoid ETSParameterExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName) 1113af6ab5fSopenharmony_ci{ 1123af6ab5fSopenharmony_ci if (IsRestParameter()) { 1133af6ab5fSopenharmony_ci if (auto *transformedNode = cb(spread_); spread_ != transformedNode) { 1143af6ab5fSopenharmony_ci spread_->SetTransformedNode(transformationName, transformedNode); 1153af6ab5fSopenharmony_ci spread_ = transformedNode->AsRestElement(); 1163af6ab5fSopenharmony_ci } 1173af6ab5fSopenharmony_ci ident_ = spread_->Argument()->AsIdentifier(); 1183af6ab5fSopenharmony_ci } else { 1193af6ab5fSopenharmony_ci if (auto *transformedNode = cb(ident_); ident_ != transformedNode) { 1203af6ab5fSopenharmony_ci ident_->SetTransformedNode(transformationName, transformedNode); 1213af6ab5fSopenharmony_ci ident_ = transformedNode->AsIdentifier(); 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci } 1243af6ab5fSopenharmony_ci 1253af6ab5fSopenharmony_ci if (IsDefault()) { 1263af6ab5fSopenharmony_ci if (auto *transformedNode = cb(initializer_); initializer_ != transformedNode) { 1273af6ab5fSopenharmony_ci initializer_->SetTransformedNode(transformationName, transformedNode); 1283af6ab5fSopenharmony_ci initializer_ = transformedNode->AsExpression(); 1293af6ab5fSopenharmony_ci } 1303af6ab5fSopenharmony_ci } 1313af6ab5fSopenharmony_ci} 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_civoid ETSParameterExpression::Iterate(const NodeTraverser &cb) const 1343af6ab5fSopenharmony_ci{ 1353af6ab5fSopenharmony_ci if (IsRestParameter()) { 1363af6ab5fSopenharmony_ci cb(spread_); 1373af6ab5fSopenharmony_ci } else { 1383af6ab5fSopenharmony_ci cb(ident_); 1393af6ab5fSopenharmony_ci } 1403af6ab5fSopenharmony_ci 1413af6ab5fSopenharmony_ci if (IsDefault()) { 1423af6ab5fSopenharmony_ci cb(initializer_); 1433af6ab5fSopenharmony_ci } 1443af6ab5fSopenharmony_ci} 1453af6ab5fSopenharmony_ci 1463af6ab5fSopenharmony_civoid ETSParameterExpression::Dump(ir::AstDumper *const dumper) const 1473af6ab5fSopenharmony_ci{ 1483af6ab5fSopenharmony_ci if (!IsRestParameter()) { 1493af6ab5fSopenharmony_ci dumper->Add( 1503af6ab5fSopenharmony_ci {{"type", "ETSParameterExpression"}, {"name", ident_}, {"initializer", AstDumper::Optional(initializer_)}}); 1513af6ab5fSopenharmony_ci } else { 1523af6ab5fSopenharmony_ci dumper->Add({{"type", "ETSParameterExpression"}, {"rest parameter", spread_}}); 1533af6ab5fSopenharmony_ci } 1543af6ab5fSopenharmony_ci} 1553af6ab5fSopenharmony_ci 1563af6ab5fSopenharmony_civoid ETSParameterExpression::Dump(ir::SrcDumper *const dumper) const 1573af6ab5fSopenharmony_ci{ 1583af6ab5fSopenharmony_ci if (IsRestParameter()) { 1593af6ab5fSopenharmony_ci spread_->Dump(dumper); 1603af6ab5fSopenharmony_ci } else { 1613af6ab5fSopenharmony_ci if (ident_ != nullptr) { 1623af6ab5fSopenharmony_ci ASSERT(ident_->IsAnnotatedExpression()); 1633af6ab5fSopenharmony_ci ident_->Dump(dumper); 1643af6ab5fSopenharmony_ci if (initializer_ != nullptr && initializer_->IsUndefinedLiteral()) { 1653af6ab5fSopenharmony_ci dumper->Add("?"); 1663af6ab5fSopenharmony_ci } 1673af6ab5fSopenharmony_ci auto typeAnnotation = ident_->AsAnnotatedExpression()->TypeAnnotation(); 1683af6ab5fSopenharmony_ci if (typeAnnotation != nullptr) { 1693af6ab5fSopenharmony_ci dumper->Add(": "); 1703af6ab5fSopenharmony_ci typeAnnotation->Dump(dumper); 1713af6ab5fSopenharmony_ci } 1723af6ab5fSopenharmony_ci } 1733af6ab5fSopenharmony_ci if (initializer_ != nullptr && !initializer_->IsUndefinedLiteral()) { 1743af6ab5fSopenharmony_ci dumper->Add(" = "); 1753af6ab5fSopenharmony_ci initializer_->Dump(dumper); 1763af6ab5fSopenharmony_ci } 1773af6ab5fSopenharmony_ci } 1783af6ab5fSopenharmony_ci} 1793af6ab5fSopenharmony_ci 1803af6ab5fSopenharmony_civoid ETSParameterExpression::Compile(compiler::PandaGen *const pg) const 1813af6ab5fSopenharmony_ci{ 1823af6ab5fSopenharmony_ci pg->GetAstCompiler()->Compile(this); 1833af6ab5fSopenharmony_ci} 1843af6ab5fSopenharmony_ci 1853af6ab5fSopenharmony_civoid ETSParameterExpression::Compile(compiler::ETSGen *const etsg) const 1863af6ab5fSopenharmony_ci{ 1873af6ab5fSopenharmony_ci etsg->GetAstCompiler()->Compile(this); 1883af6ab5fSopenharmony_ci} 1893af6ab5fSopenharmony_ci 1903af6ab5fSopenharmony_cichecker::Type *ETSParameterExpression::Check(checker::TSChecker *const checker) 1913af6ab5fSopenharmony_ci{ 1923af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1933af6ab5fSopenharmony_ci} 1943af6ab5fSopenharmony_ci 1953af6ab5fSopenharmony_cichecker::Type *ETSParameterExpression::Check(checker::ETSChecker *const checker) 1963af6ab5fSopenharmony_ci{ 1973af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1983af6ab5fSopenharmony_ci} 1993af6ab5fSopenharmony_ci 2003af6ab5fSopenharmony_ciETSParameterExpression *ETSParameterExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) 2013af6ab5fSopenharmony_ci{ 2023af6ab5fSopenharmony_ci auto *const identOrSpread = spread_ != nullptr ? spread_->Clone(allocator, nullptr)->AsAnnotatedExpression() 2033af6ab5fSopenharmony_ci : ident_->Clone(allocator, nullptr)->AsAnnotatedExpression(); 2043af6ab5fSopenharmony_ci auto *const initializer = 2053af6ab5fSopenharmony_ci initializer_ != nullptr ? initializer_->Clone(allocator, nullptr)->AsExpression() : nullptr; 2063af6ab5fSopenharmony_ci 2073af6ab5fSopenharmony_ci if (auto *const clone = allocator->New<ETSParameterExpression>(identOrSpread, initializer); clone != nullptr) { 2083af6ab5fSopenharmony_ci identOrSpread->SetParent(clone); 2093af6ab5fSopenharmony_ci 2103af6ab5fSopenharmony_ci if (initializer != nullptr) { 2113af6ab5fSopenharmony_ci initializer->SetParent(clone); 2123af6ab5fSopenharmony_ci } 2133af6ab5fSopenharmony_ci 2143af6ab5fSopenharmony_ci if (parent != nullptr) { 2153af6ab5fSopenharmony_ci clone->SetParent(parent); 2163af6ab5fSopenharmony_ci } 2173af6ab5fSopenharmony_ci 2183af6ab5fSopenharmony_ci clone->SetRequiredParams(extraValue_); 2193af6ab5fSopenharmony_ci return clone; 2203af6ab5fSopenharmony_ci } 2213af6ab5fSopenharmony_ci 2223af6ab5fSopenharmony_ci throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); 2233af6ab5fSopenharmony_ci} 2243af6ab5fSopenharmony_ci} // namespace ark::es2panda::ir 225