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 "classProperty.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include "checker/ETSchecker.h" 193af6ab5fSopenharmony_ci#include "checker/TSchecker.h" 203af6ab5fSopenharmony_ci#include "compiler/core/ETSGen.h" 213af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h" 223af6ab5fSopenharmony_ci#include "ir/astDump.h" 233af6ab5fSopenharmony_ci#include "ir/srcDump.h" 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_cinamespace ark::es2panda::ir { 263af6ab5fSopenharmony_civoid ClassProperty::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName) 273af6ab5fSopenharmony_ci{ 283af6ab5fSopenharmony_ci if (auto *transformedNode = cb(key_); key_ != transformedNode) { 293af6ab5fSopenharmony_ci key_->SetTransformedNode(transformationName, transformedNode); 303af6ab5fSopenharmony_ci key_ = transformedNode->AsExpression(); 313af6ab5fSopenharmony_ci } 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_ci if (value_ != nullptr) { 343af6ab5fSopenharmony_ci if (auto *transformedNode = cb(value_); value_ != transformedNode) { 353af6ab5fSopenharmony_ci value_->SetTransformedNode(transformationName, transformedNode); 363af6ab5fSopenharmony_ci value_ = transformedNode->AsExpression(); 373af6ab5fSopenharmony_ci } 383af6ab5fSopenharmony_ci } 393af6ab5fSopenharmony_ci 403af6ab5fSopenharmony_ci if (typeAnnotation_ != nullptr) { 413af6ab5fSopenharmony_ci if (auto *transformedNode = cb(typeAnnotation_); typeAnnotation_ != transformedNode) { 423af6ab5fSopenharmony_ci typeAnnotation_->SetTransformedNode(transformationName, transformedNode); 433af6ab5fSopenharmony_ci typeAnnotation_ = static_cast<TypeNode *>(transformedNode); 443af6ab5fSopenharmony_ci } 453af6ab5fSopenharmony_ci } 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_ci for (auto *&it : decorators_) { 483af6ab5fSopenharmony_ci if (auto *transformedNode = cb(it); it != transformedNode) { 493af6ab5fSopenharmony_ci it->SetTransformedNode(transformationName, transformedNode); 503af6ab5fSopenharmony_ci it = transformedNode->AsDecorator(); 513af6ab5fSopenharmony_ci } 523af6ab5fSopenharmony_ci } 533af6ab5fSopenharmony_ci} 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_civoid ClassProperty::Iterate(const NodeTraverser &cb) const 563af6ab5fSopenharmony_ci{ 573af6ab5fSopenharmony_ci cb(key_); 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_ci if (value_ != nullptr) { 603af6ab5fSopenharmony_ci cb(value_); 613af6ab5fSopenharmony_ci } 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_ci if (typeAnnotation_ != nullptr) { 643af6ab5fSopenharmony_ci cb(typeAnnotation_); 653af6ab5fSopenharmony_ci } 663af6ab5fSopenharmony_ci 673af6ab5fSopenharmony_ci for (auto *it : decorators_) { 683af6ab5fSopenharmony_ci cb(it); 693af6ab5fSopenharmony_ci } 703af6ab5fSopenharmony_ci} 713af6ab5fSopenharmony_ci 723af6ab5fSopenharmony_civoid ClassProperty::Dump(ir::AstDumper *dumper) const 733af6ab5fSopenharmony_ci{ 743af6ab5fSopenharmony_ci dumper->Add({{"type", "ClassProperty"}, 753af6ab5fSopenharmony_ci {"key", key_}, 763af6ab5fSopenharmony_ci {"value", AstDumper::Optional(value_)}, 773af6ab5fSopenharmony_ci {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(flags_))}, 783af6ab5fSopenharmony_ci {"abstract", AstDumper::Optional(IsAbstract())}, 793af6ab5fSopenharmony_ci {"static", IsStatic()}, 803af6ab5fSopenharmony_ci {"readonly", IsReadonly()}, 813af6ab5fSopenharmony_ci {"declare", IsDeclare()}, 823af6ab5fSopenharmony_ci {"optional", IsOptionalDeclaration()}, 833af6ab5fSopenharmony_ci {"computed", isComputed_}, 843af6ab5fSopenharmony_ci {"typeAnnotation", AstDumper::Optional(typeAnnotation_)}, 853af6ab5fSopenharmony_ci {"definite", IsDefinite()}, 863af6ab5fSopenharmony_ci {"decorators", decorators_}}); 873af6ab5fSopenharmony_ci} 883af6ab5fSopenharmony_ci 893af6ab5fSopenharmony_civoid ClassProperty::Dump(ir::SrcDumper *dumper) const 903af6ab5fSopenharmony_ci{ 913af6ab5fSopenharmony_ci if (Parent() != nullptr && Parent()->IsClassDefinition() && !Parent()->AsClassDefinition()->IsLocal()) { 923af6ab5fSopenharmony_ci if (IsPrivate()) { 933af6ab5fSopenharmony_ci dumper->Add("private "); 943af6ab5fSopenharmony_ci } else if (IsProtected()) { 953af6ab5fSopenharmony_ci dumper->Add("protected "); 963af6ab5fSopenharmony_ci } else if (IsInternal()) { 973af6ab5fSopenharmony_ci dumper->Add("internal "); 983af6ab5fSopenharmony_ci } else { 993af6ab5fSopenharmony_ci dumper->Add("public "); 1003af6ab5fSopenharmony_ci } 1013af6ab5fSopenharmony_ci } 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ci if (IsStatic()) { 1043af6ab5fSopenharmony_ci dumper->Add("static "); 1053af6ab5fSopenharmony_ci } 1063af6ab5fSopenharmony_ci 1073af6ab5fSopenharmony_ci if (IsReadonly()) { 1083af6ab5fSopenharmony_ci dumper->Add("readonly "); 1093af6ab5fSopenharmony_ci } 1103af6ab5fSopenharmony_ci 1113af6ab5fSopenharmony_ci if (key_ != nullptr) { 1123af6ab5fSopenharmony_ci key_->Dump(dumper); 1133af6ab5fSopenharmony_ci } 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_ci if (IsOptionalDeclaration()) { 1163af6ab5fSopenharmony_ci dumper->Add("?"); 1173af6ab5fSopenharmony_ci } 1183af6ab5fSopenharmony_ci 1193af6ab5fSopenharmony_ci if (typeAnnotation_ != nullptr) { 1203af6ab5fSopenharmony_ci dumper->Add(": "); 1213af6ab5fSopenharmony_ci typeAnnotation_->Dump(dumper); 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci 1243af6ab5fSopenharmony_ci if (value_ != nullptr) { 1253af6ab5fSopenharmony_ci dumper->Add(" = "); 1263af6ab5fSopenharmony_ci value_->Dump(dumper); 1273af6ab5fSopenharmony_ci } 1283af6ab5fSopenharmony_ci 1293af6ab5fSopenharmony_ci dumper->Add(";"); 1303af6ab5fSopenharmony_ci dumper->Endl(); 1313af6ab5fSopenharmony_ci} 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_civoid ClassProperty::Compile(compiler::PandaGen *pg) const 1343af6ab5fSopenharmony_ci{ 1353af6ab5fSopenharmony_ci pg->GetAstCompiler()->Compile(this); 1363af6ab5fSopenharmony_ci} 1373af6ab5fSopenharmony_ci 1383af6ab5fSopenharmony_civoid ClassProperty::Compile(compiler::ETSGen *etsg) const 1393af6ab5fSopenharmony_ci{ 1403af6ab5fSopenharmony_ci etsg->GetAstCompiler()->Compile(this); 1413af6ab5fSopenharmony_ci} 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_cichecker::Type *ClassProperty::Check(checker::TSChecker *checker) 1443af6ab5fSopenharmony_ci{ 1453af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1463af6ab5fSopenharmony_ci} 1473af6ab5fSopenharmony_ci 1483af6ab5fSopenharmony_cichecker::Type *ClassProperty::Check(checker::ETSChecker *checker) 1493af6ab5fSopenharmony_ci{ 1503af6ab5fSopenharmony_ci return checker->GetAnalyzer()->Check(this); 1513af6ab5fSopenharmony_ci} 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_ciClassProperty *ClassProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent) 1543af6ab5fSopenharmony_ci{ 1553af6ab5fSopenharmony_ci auto *const key = key_->Clone(allocator, nullptr)->AsExpression(); 1563af6ab5fSopenharmony_ci auto *const value = value_ != nullptr ? value_->Clone(allocator, nullptr)->AsExpression() : nullptr; 1573af6ab5fSopenharmony_ci auto *const typeAnnotation = typeAnnotation_ != nullptr ? typeAnnotation_->Clone(allocator, nullptr) : nullptr; 1583af6ab5fSopenharmony_ci 1593af6ab5fSopenharmony_ci if (auto *const clone = allocator->New<ClassProperty>(key, value, typeAnnotation, flags_, allocator, isComputed_); 1603af6ab5fSopenharmony_ci clone != nullptr) { 1613af6ab5fSopenharmony_ci if (parent != nullptr) { 1623af6ab5fSopenharmony_ci clone->SetParent(parent); 1633af6ab5fSopenharmony_ci } 1643af6ab5fSopenharmony_ci 1653af6ab5fSopenharmony_ci key->SetParent(clone); 1663af6ab5fSopenharmony_ci if (value != nullptr) { 1673af6ab5fSopenharmony_ci value->SetParent(clone); 1683af6ab5fSopenharmony_ci } 1693af6ab5fSopenharmony_ci if (typeAnnotation != nullptr) { 1703af6ab5fSopenharmony_ci typeAnnotation->SetParent(clone); 1713af6ab5fSopenharmony_ci } 1723af6ab5fSopenharmony_ci 1733af6ab5fSopenharmony_ci for (auto *const decorator : decorators_) { 1743af6ab5fSopenharmony_ci clone->AddDecorator(decorator->Clone(allocator, clone)); 1753af6ab5fSopenharmony_ci } 1763af6ab5fSopenharmony_ci 1773af6ab5fSopenharmony_ci return clone; 1783af6ab5fSopenharmony_ci } 1793af6ab5fSopenharmony_ci 1803af6ab5fSopenharmony_ci throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); 1813af6ab5fSopenharmony_ci} 1823af6ab5fSopenharmony_ci} // namespace ark::es2panda::ir 183