From d4e4c60c30963db528bd4fa7ebfb5fc4a1c25bf2 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Thu, 11 Sep 2025 15:15:01 +0800 Subject: [PATCH] Functional enhancement for dumpSrc Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICXJ13 Signed-off-by: anjiaqi --- ets2panda/ir/base/classDefinition.cpp | 1 - ets2panda/ir/base/classProperty.cpp | 2 -- ets2panda/ir/base/methodDefinition.cpp | 11 ++++++++++- ets2panda/ir/base/overloadDeclaration.cpp | 4 ++++ ets2panda/ir/base/scriptFunction.cpp | 5 ----- ets2panda/ir/ets/etsFunctionType.cpp | 3 +-- ets2panda/ir/ets/etsTuple.cpp | 5 +++++ ets2panda/ir/ets/etsUnionType.cpp | 10 +++++----- ets2panda/ir/expressions/binaryExpression.cpp | 6 ++---- ets2panda/ir/expressions/literals/numberLiteral.cpp | 2 +- ets2panda/ir/module/importDeclaration.cpp | 3 +++ ets2panda/ir/statements/annotationUsage.cpp | 5 +++-- ets2panda/ir/statements/variableDeclaration.cpp | 3 +++ ets2panda/ir/ts/tsEnumDeclaration.cpp | 4 +++- ets2panda/ir/ts/tsEnumMember.cpp | 2 +- ets2panda/ir/ts/tsInterfaceBody.cpp | 3 +++ ets2panda/ir/ts/tsTypeParameter.cpp | 9 +++++---- ets2panda/parser/ETSparserClasses.cpp | 1 + 18 files changed, 50 insertions(+), 29 deletions(-) diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 2a61c15828..ed62cd4a75 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -465,7 +465,6 @@ void ClassDefinition::Dump(ir::SrcDumper *dumper) const if (IsLocal()) { dumper->Add(";"); } - dumper->Endl(); } void ClassDefinition::Compile(compiler::PandaGen *pg) const diff --git a/ets2panda/ir/base/classProperty.cpp b/ets2panda/ir/base/classProperty.cpp index 944a3462d5..f762c252d0 100644 --- a/ets2panda/ir/base/classProperty.cpp +++ b/ets2panda/ir/base/classProperty.cpp @@ -120,8 +120,6 @@ void ClassProperty::DumpModifiers(ir::SrcDumper *dumper) const dumper->Add("private "); } else if (IsProtected()) { dumper->Add("protected "); - } else { - dumper->Add("public "); } } diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index f3eb2b5978..09ea2268af 100644 --- a/ets2panda/ir/base/methodDefinition.cpp +++ b/ets2panda/ir/base/methodDefinition.cpp @@ -300,13 +300,22 @@ bool MethodDefinition::FilterForDeclGen() const static void DumpSingleOverload(const ir::MethodDefinition *m, ir::SrcDumper *dumper) { + auto value = m->Value(); + if (m->IsConstructor() && ((value->AsFunctionExpression()->Function()->Flags() & + ir::ScriptFunctionFlags::IMPLICIT_SUPER_CALL_NEEDED) != 0U)) { + return; + } + + if (m->IsDefaultAccessModifier() && m->Parent()->IsTSInterfaceBody()) { + dumper->Add("default "); + } + if (compiler::HasGlobalClassParent(m) && m->Id() != nullptr && m->Id()->Name().Is(compiler::Signatures::INIT_METHOD)) { m->Function()->Body()->Dump(dumper); return; } - auto value = m->Value(); if (value->AsFunctionExpression()->Function()->HasAnnotations()) { for (auto *anno : value->AsFunctionExpression()->Function()->Annotations()) { // NOTE(zhelyapov): workaround, see #26031 diff --git a/ets2panda/ir/base/overloadDeclaration.cpp b/ets2panda/ir/base/overloadDeclaration.cpp index 38122f80e3..39439a5680 100644 --- a/ets2panda/ir/base/overloadDeclaration.cpp +++ b/ets2panda/ir/base/overloadDeclaration.cpp @@ -94,12 +94,16 @@ void OverloadDeclaration::Dump(ir::SrcDumper *dumper) const dumper->Add("overload "); dumper->Add(IsConstructor() ? "constructor " : key_->AsIdentifier()->Name().Mutf8()); dumper->Add("{"); + dumper->IncrIndent(); for (size_t i = 0; i < overloadedList_.size(); i++) { if (i != 0) { dumper->Add(", "); } + dumper->Endl(); overloadedList_[i]->Dump(dumper); } + dumper->DecrIndent(); + dumper->Endl(); dumper->Add("};"); } diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index b21230ed99..fbe49e5775 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -345,7 +345,6 @@ void ScriptFunction::Dump(ir::SrcDumper *dumper) const void ScriptFunction::DumpBody(ir::SrcDumper *dumper) const { if (!HasBody()) { - dumper->Endl(); return; } @@ -368,10 +367,6 @@ void ScriptFunction::DumpBody(ir::SrcDumper *dumper) const dumper->Add(" "); body_->Dump(dumper); } - - if (!IsArrow()) { - dumper->Endl(); - } } void ScriptFunction::Compile(compiler::PandaGen *pg) const diff --git a/ets2panda/ir/ets/etsFunctionType.cpp b/ets2panda/ir/ets/etsFunctionType.cpp index 73b0c3bc03..93507d9546 100644 --- a/ets2panda/ir/ets/etsFunctionType.cpp +++ b/ets2panda/ir/ets/etsFunctionType.cpp @@ -55,7 +55,7 @@ void ETSFunctionType::Dump(ir::AstDumper *dumper) const void ETSFunctionType::Dump(ir::SrcDumper *dumper) const { DumpAnnotations(dumper); - dumper->Add("(("); + dumper->Add("("); for (auto *param : Params()) { param->Dump(dumper); if (param != Params().back()) { @@ -73,7 +73,6 @@ void ETSFunctionType::Dump(ir::SrcDumper *dumper) const ReturnType()->Dump(dumper); } - dumper->Add(")"); } void ETSFunctionType::Compile(compiler::PandaGen *pg) const diff --git a/ets2panda/ir/ets/etsTuple.cpp b/ets2panda/ir/ets/etsTuple.cpp index 3012e89b11..4679b86586 100644 --- a/ets2panda/ir/ets/etsTuple.cpp +++ b/ets2panda/ir/ets/etsTuple.cpp @@ -52,12 +52,17 @@ void ETSTuple::Dump(ir::SrcDumper *const dumper) const { DumpAnnotations(dumper); dumper->Add("["); + dumper->IncrIndent(); + dumper->Endl(); for (const auto *const typeAnnot : typeAnnotationList_) { typeAnnot->Dump(dumper); if (typeAnnot != typeAnnotationList_.back()) { dumper->Add(", "); + dumper->Endl(); } } + dumper->DecrIndent(); + dumper->Endl(); dumper->Add("]"); } diff --git a/ets2panda/ir/ets/etsUnionType.cpp b/ets2panda/ir/ets/etsUnionType.cpp index 95f556c907..7f17a4d672 100644 --- a/ets2panda/ir/ets/etsUnionType.cpp +++ b/ets2panda/ir/ets/etsUnionType.cpp @@ -48,14 +48,14 @@ void ETSUnionType::Dump(ir::AstDumper *dumper) const void ETSUnionType::Dump(ir::SrcDumper *dumper) const { DumpAnnotations(dumper); - dumper->Add("("); - for (auto type : Types()) { - type->Dump(dumper); - if (type != Types().back()) { + auto const &types = Types(); + auto size = Parent()->IsOptionalDeclaration() ? types.size() - 1 : types.size(); + for (size_t ix = 0; ix < size; ix++) { + types[ix]->Dump(dumper); + if (ix != size - 1) { dumper->Add(" | "); } } - dumper->Add(")"); } void ETSUnionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} diff --git a/ets2panda/ir/expressions/binaryExpression.cpp b/ets2panda/ir/expressions/binaryExpression.cpp index b7de84484e..8c5869b1de 100644 --- a/ets2panda/ir/expressions/binaryExpression.cpp +++ b/ets2panda/ir/expressions/binaryExpression.cpp @@ -51,13 +51,11 @@ void BinaryExpression::Dump(ir::SrcDumper *dumper) const { ES2PANDA_ASSERT(left_ != nullptr); ES2PANDA_ASSERT(right_ != nullptr); - dumper->Add("(("); left_->Dump(dumper); - dumper->Add(") "); + dumper->Add(" "); dumper->Add(TokenToString(operator_)); - dumper->Add(" ("); + dumper->Add(" "); right_->Dump(dumper); - dumper->Add("))"); } void BinaryExpression::Compile(compiler::PandaGen *pg) const diff --git a/ets2panda/ir/expressions/literals/numberLiteral.cpp b/ets2panda/ir/expressions/literals/numberLiteral.cpp index d3cf60f026..b8c075d249 100644 --- a/ets2panda/ir/expressions/literals/numberLiteral.cpp +++ b/ets2panda/ir/expressions/literals/numberLiteral.cpp @@ -42,7 +42,7 @@ void NumberLiteral::Dump(ir::AstDumper *dumper) const void NumberLiteral::Dump(ir::SrcDumper *dumper) const { - if (std::string(number_.Str()).empty() || (parent_ != nullptr && parent_->IsTSEnumMember())) { + if (std::string(number_.Str()).empty()) { if (number_.IsInt()) { dumper->Add(number_.GetInt()); return; diff --git a/ets2panda/ir/module/importDeclaration.cpp b/ets2panda/ir/module/importDeclaration.cpp index 2dbd9f52c7..99e1b8c147 100644 --- a/ets2panda/ir/module/importDeclaration.cpp +++ b/ets2panda/ir/module/importDeclaration.cpp @@ -68,6 +68,9 @@ void ImportDeclaration::Dump(ir::SrcDumper *dumper) const return; } dumper->Add("import "); + if (IsTypeKind()) { + dumper->Add("type "); + } auto const &specifiers = Specifiers(); if (specifiers.size() == 1 && (specifiers[0]->IsImportNamespaceSpecifier() || specifiers[0]->IsImportDefaultSpecifier())) { diff --git a/ets2panda/ir/statements/annotationUsage.cpp b/ets2panda/ir/statements/annotationUsage.cpp index 2d22aed190..e91e1e525d 100644 --- a/ets2panda/ir/statements/annotationUsage.cpp +++ b/ets2panda/ir/statements/annotationUsage.cpp @@ -53,9 +53,9 @@ void AnnotationUsage::Dump(ir::SrcDumper *dumper) const ES2PANDA_ASSERT(expr_ != nullptr); dumper->Add("@"); expr_->Dump(dumper); - dumper->Add("("); if (!properties_.empty()) { + dumper->Add("("); dumper->Add("{"); for (auto elem : properties_) { ES2PANDA_ASSERT(elem->AsClassProperty()->Id() != nullptr); @@ -67,8 +67,9 @@ void AnnotationUsage::Dump(ir::SrcDumper *dumper) const } } dumper->Add("}"); + dumper->Add(") "); } - dumper->Add(") "); + dumper->Endl(); } AnnotationUsage *AnnotationUsage::Clone(ArenaAllocator *const allocator, AstNode *const parent) diff --git a/ets2panda/ir/statements/variableDeclaration.cpp b/ets2panda/ir/statements/variableDeclaration.cpp index ad2bc7f2e9..8cd9047718 100644 --- a/ets2panda/ir/statements/variableDeclaration.cpp +++ b/ets2panda/ir/statements/variableDeclaration.cpp @@ -113,6 +113,9 @@ void VariableDeclaration::Dump(ir::SrcDumper *dumper) const if (IsDeclare()) { dumper->Add("declare "); } + if (IsExported()) { + dumper->Add("export "); + } switch (Kind()) { case VariableDeclarationKind::CONST: diff --git a/ets2panda/ir/ts/tsEnumDeclaration.cpp b/ets2panda/ir/ts/tsEnumDeclaration.cpp index e51a7d2896..eccc5db721 100644 --- a/ets2panda/ir/ts/tsEnumDeclaration.cpp +++ b/ets2panda/ir/ts/tsEnumDeclaration.cpp @@ -112,6 +112,9 @@ void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const } else if (IsDeclare()) { dumper->Add("declare "); } + if (IsExported()) { + dumper->Add("export "); + } dumper->Add("enum "); Key()->Dump(dumper); dumper->Add(" {"); @@ -130,7 +133,6 @@ void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const dumper->Endl(); } dumper->Add("}"); - dumper->Endl(); } // NOTE (csabahurton): this method has not been moved to TSAnalyizer.cpp, because it is not used. diff --git a/ets2panda/ir/ts/tsEnumMember.cpp b/ets2panda/ir/ts/tsEnumMember.cpp index f4f126095a..770487dc9c 100644 --- a/ets2panda/ir/ts/tsEnumMember.cpp +++ b/ets2panda/ir/ts/tsEnumMember.cpp @@ -55,7 +55,7 @@ void TSEnumMember::Dump(ir::SrcDumper *dumper) const { ES2PANDA_ASSERT(key_ != nullptr); key_->Dump(dumper); - if (init_ != nullptr) { + if (init_ != nullptr && !(init_->IsBinaryExpression() && isGenerated_)) { dumper->Add(" = "); init_->Dump(dumper); } diff --git a/ets2panda/ir/ts/tsInterfaceBody.cpp b/ets2panda/ir/ts/tsInterfaceBody.cpp index c98011586e..7bd5286ced 100644 --- a/ets2panda/ir/ts/tsInterfaceBody.cpp +++ b/ets2panda/ir/ts/tsInterfaceBody.cpp @@ -49,6 +49,9 @@ void TSInterfaceBody::Dump(ir::SrcDumper *dumper) const { for (auto b : body_) { b->Dump(dumper); + if (b != body_.back()) { + dumper->Endl(); + } } } diff --git a/ets2panda/ir/ts/tsTypeParameter.cpp b/ets2panda/ir/ts/tsTypeParameter.cpp index 5e997562f4..b4ecadac09 100644 --- a/ets2panda/ir/ts/tsTypeParameter.cpp +++ b/ets2panda/ir/ts/tsTypeParameter.cpp @@ -109,14 +109,15 @@ void TSTypeParameter::Dump(ir::SrcDumper *dumper) const Name()->Dump(dumper); - if (DefaultType() != nullptr) { - dumper->Add(" = "); - DefaultType()->Dump(dumper); - } if (Constraint() != nullptr) { dumper->Add(" extends "); Constraint()->Dump(dumper); } + + if (DefaultType() != nullptr) { + dumper->Add(" = "); + DefaultType()->Dump(dumper); + } } void TSTypeParameter::Compile([[maybe_unused]] compiler::PandaGen *pg) const diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index 95d4e1b759..ae606844fc 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -1207,6 +1207,7 @@ ir::MethodDefinition *ETSParser::ParseInterfaceMethod(ir::ModifierFlags flags, i auto *method = AllocNode(methodKind, name->Clone(Allocator(), nullptr)->AsExpression(), funcExpr, flags, Allocator(), false); ES2PANDA_ASSERT(method != nullptr); + method->SetDefaultAccessModifier((flags & (ir::ModifierFlags::DEFAULT)) != 0); method->SetRange(funcExpr->Range()); ConsumeSemicolon(method); -- Gitee