From b36ace8338c86defae56be663b93e57b553c78bd Mon Sep 17 00:00:00 2001 From: Zelentsov Dmitry Date: Tue, 9 Sep 2025 10:56:40 +0300 Subject: [PATCH] Add 'default' keyword to method declarions Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICX4XH Tests: use Test-U-Runner and CI Signed-off-by: Zelentsov Dmitry --- ets2panda/compiler/core/compilerImpl.cpp | 6 +++ ets2panda/ir/astDump.cpp | 2 +- ets2panda/ir/base/classProperty.cpp | 6 +++ ets2panda/ir/base/methodDefinition.cpp | 47 +++++++++++++------ ets2panda/ir/base/methodDefinition.h | 1 + ets2panda/ir/ets/etsParameterExpression.cpp | 8 ++-- .../ir/expressions/literals/stringLiteral.cpp | 2 +- ets2panda/ir/module/importDeclaration.cpp | 23 +++++++-- ets2panda/util/helpers.cpp | 2 +- ets2panda/util/helpers.h | 2 +- 10 files changed, 74 insertions(+), 25 deletions(-) diff --git a/ets2panda/compiler/core/compilerImpl.cpp b/ets2panda/compiler/core/compilerImpl.cpp index 9241405012..155df3eb28 100644 --- a/ets2panda/compiler/core/compilerImpl.cpp +++ b/ets2panda/compiler/core/compilerImpl.cpp @@ -110,12 +110,18 @@ static bool CheckOptionsBeforePhase(const util::Options &options, const parser:: void HandleGenerateDecl(const parser::Program &program, util::DiagnosticEngine &diagnosticEngine, const std::string &outputPath) { + // Don't generate declarations for source code with errors! + if (diagnosticEngine.IsAnyError()) { + return; + } + std::ofstream outFile(outputPath); if (!outFile.is_open()) { diagnosticEngine.LogFatalError(diagnostic::OPEN_FAILED, util::DiagnosticMessageParams {outputPath}, lexer::SourcePosition()); return; } + std::string result = program.Ast()->DumpDecl(); result = "'use static'\n" + result; diff --git a/ets2panda/ir/astDump.cpp b/ets2panda/ir/astDump.cpp index 4706797833..cc850f7341 100644 --- a/ets2panda/ir/astDump.cpp +++ b/ets2panda/ir/astDump.cpp @@ -125,7 +125,7 @@ void AstDumper::SerializeString(const char *str) void AstDumper::SerializeString(const util::StringView &str) { - ss_ << "\"" << util::Helpers::CreateEscapedString(std::string(str)) << "\""; + ss_ << "\"" << util::Helpers::CreateEscapedString(str.Utf8()) << "\""; } void AstDumper::SerializeNumber(size_t number) diff --git a/ets2panda/ir/base/classProperty.cpp b/ets2panda/ir/base/classProperty.cpp index a292d8c689..4432618142 100644 --- a/ets2panda/ir/base/classProperty.cpp +++ b/ets2panda/ir/base/classProperty.cpp @@ -100,6 +100,12 @@ void ClassProperty::DumpModifiers(ir::SrcDumper *dumper) const ES2PANDA_ASSERT(key_); if (compiler::HasGlobalClassParent(this)) { + if (IsExported()) { + dumper->Add("export "); + } + if (dumper->IsDeclgen()) { + dumper->Add("declare "); + } if (key_->Parent()->IsConst()) { dumper->Add("const "); } else { diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index 276232f512..b55a30e0e1 100644 --- a/ets2panda/ir/base/methodDefinition.cpp +++ b/ets2panda/ir/base/methodDefinition.cpp @@ -167,6 +167,38 @@ void MethodDefinition::Dump(ir::AstDumper *dumper) const {"overloads", Overloads()}}); } +void MethodDefinition::DumpAccessorPrefix(ir::SrcDumper *dumper) const +{ + // special processing for overloads + auto const *parent = Parent(); + if (parent != nullptr && parent->IsMethodDefinition()) { + parent = parent->Parent(); + } + + if (parent == nullptr) { + return; + } + + if (parent->IsClassDefinition() && !parent->AsClassDefinition()->IsLocal()) { + if (IsPrivate()) { + dumper->Add("private "); + } else if (IsProtected()) { + dumper->Add("protected "); + } else { + dumper->Add("public "); + } + return; + } + + if (dumper->IsDeclgen() && parent->IsTSInterfaceBody()) { + if (Value() != nullptr && Value()->IsFunctionExpression() && + Value()->AsFunctionExpression()->Function() != nullptr && + Value()->AsFunctionExpression()->Function()->HasBody()) { + dumper->Add("default "); + } + } +} + void MethodDefinition::DumpModifierPrefix(ir::SrcDumper *dumper) const { if (compiler::HasGlobalClassParent(this)) { @@ -234,21 +266,8 @@ void MethodDefinition::DumpPrefix(ir::SrcDumper *dumper) const return; } - // special processing for overloads - auto const *parent = Parent(); - if (parent != nullptr && parent->IsMethodDefinition()) { - parent = parent->Parent(); - } + DumpAccessorPrefix(dumper); - if (parent != nullptr && parent->IsClassDefinition() && !parent->AsClassDefinition()->IsLocal()) { - if (IsPrivate()) { - dumper->Add("private "); - } else if (IsProtected()) { - dumper->Add("protected "); - } else { - dumper->Add("public "); - } - } DumpModifierPrefix(dumper); } diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index e8ea9b58ba..bdee5606c3 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -248,6 +248,7 @@ private: void DumpPrefix(ir::SrcDumper *dumper) const; void ResetOverloads(); void DumpModifierPrefix(ir::SrcDumper *dumper) const; + void DumpAccessorPrefix(ir::SrcDumper *dumper) const; bool DumpNamespaceForDeclGen(ir::SrcDumper *dumper) const; void DumpPrefixForDeclGen(ir::SrcDumper *dumper) const; bool FilterForDeclGen() const; diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index 398d648b26..c0871a9e77 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -224,9 +224,11 @@ void ETSParameterExpression::Dump(ir::SrcDumper *const dumper) const if (IsRestParameter()) { Spread()->Dump(dumper); } else { + ETSParameterExpression const *node = this; // NOTE (DZ): temporary solution until node history starts working properly - ETSParameterExpression const *node = - OriginalNode() == nullptr ? this : OriginalNode()->AsETSParameterExpression(); + if (dumper->IsDeclgen() && OriginalNode() != nullptr) { + node = OriginalNode()->AsETSParameterExpression(); + } auto const ident = node->Ident(); ES2PANDA_ASSERT(ident != nullptr); @@ -239,7 +241,7 @@ void ETSParameterExpression::Dump(ir::SrcDumper *const dumper) const auto typeAnnotation = ident->AsAnnotatedExpression()->TypeAnnotation(); if (typeAnnotation != nullptr) { - if (typeAnnotation->OriginalNode() != nullptr) { + if (dumper->IsDeclgen() && typeAnnotation->OriginalNode() != nullptr) { typeAnnotation = typeAnnotation->OriginalNode()->AsExpression()->AsTypeNode(); } dumper->Add(": "); diff --git a/ets2panda/ir/expressions/literals/stringLiteral.cpp b/ets2panda/ir/expressions/literals/stringLiteral.cpp index c0345f6b61..6c6e598e0d 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.cpp +++ b/ets2panda/ir/expressions/literals/stringLiteral.cpp @@ -34,7 +34,7 @@ void StringLiteral::Dump(ir::AstDumper *dumper) const void StringLiteral::Dump(ir::SrcDumper *dumper) const { - dumper->Add("\"" + util::Helpers::CreateEscapedString(std::string(str_)) + "\""); + dumper->Add("\"" + util::Helpers::CreateEscapedString(str_.Utf8()) + "\""); } void StringLiteral::Compile(compiler::PandaGen *pg) const diff --git a/ets2panda/ir/module/importDeclaration.cpp b/ets2panda/ir/module/importDeclaration.cpp index f35446002c..2f82079f43 100644 --- a/ets2panda/ir/module/importDeclaration.cpp +++ b/ets2panda/ir/module/importDeclaration.cpp @@ -63,9 +63,6 @@ void ImportDeclaration::Dump(ir::AstDumper *dumper) const void ImportDeclaration::Dump(ir::SrcDumper *dumper) const { - if (dumper->IsDeclgen()) { - return; - } dumper->Add("import "); auto const &specifiers = Specifiers(); if (specifiers.size() == 1 && @@ -83,7 +80,25 @@ void ImportDeclaration::Dump(ir::SrcDumper *dumper) const } dumper->Add(" from "); - Source()->Dump(dumper); + + if (dumper->IsDeclgen()) { + auto fileName = Source()->Str(); + auto len = fileName.Length(); + if (fileName.EndsWith(".ets")) { + len -= 4U; + fileName = fileName.Substr(0, len); + } + + std::string importFile = '\"' + util::Helpers::CreateEscapedString(fileName.Utf8()); + if (fileName.Utf8().find('.', 1U) == std::string_view::npos) { + importFile += ".d"; + } + importFile += '\"'; + + dumper->Add(importFile); + } else { + Source()->Dump(dumper); + } dumper->Add(";"); dumper->Endl(); } diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index fe9e9b5e32..d493f7b99b 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -676,7 +676,7 @@ static std::string GetEscapedCharacter(const unsigned char c) return escapedStr.str(); } -std::string Helpers::CreateEscapedString(const std::string &str) +std::string Helpers::CreateEscapedString(std::string_view const str) { std::string escapedStr; for (const unsigned char c : str) { diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index c6ece8cab1..16de466560 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -194,7 +194,7 @@ public: static const uint32_t INVALID_INDEX = 4294967295L; - static std::string CreateEscapedString(const std::string &str); + static std::string CreateEscapedString(std::string_view str); static std::string UTF16toUTF8(const char16_t c); template -- Gitee