From 14f9dd8a3c06a6e33e741da9e1062f5c4111aab5 Mon Sep 17 00:00:00 2001 From: dongchao Date: Wed, 30 Jul 2025 17:28:37 +0800 Subject: [PATCH] Fix declgen bugs Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSE6S Signed-off-by: dongchao Change-Id: I7dc6648ba468adbb4d565a4d8f7b5180e9e828de --- ets2panda/declgen_ets2ts/declgenEts2Ts.cpp | 26 ++++++++++++++----- .../build_system/src/build/base_mode.ts | 2 +- .../build_system/src/build/declgen_worker.ts | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index 4b64326756..e685df218f 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -415,7 +415,8 @@ void TSDeclGen::GenImportDeclarations() void TSDeclGen::GenImportRecordDeclarations(const std::string &source) { - if (importSet_.find("Record") != importSet_.end()) { + const std::string recordKey = "Record"; + if (indirectDependencyObjects_.find(recordKey) != indirectDependencyObjects_.end()) { OutDts("import type { Record } from \"", source, "\";\n"); } } @@ -576,6 +577,8 @@ bool TSDeclGen::HandleObjectType(const checker::Type *checkerType) OutDts("number"); } else if (typeStr == "BigInt") { OutDts("bigint"); + } else if (typeStr == "ESValue") { + OutDts("ESObject"); } else { GenObjectType(checkerType->AsETSObjectType()); } @@ -2008,7 +2011,7 @@ void TSDeclGen::GenPartName(std::string &partName) partName = "string"; } else if (numberTypes_.count(partName) != 0U) { partName = "number"; - } else if (partName == "ESObject") { + } else if (partName == "ESValue") { partName = "ESObject"; } else if (partName == "BigInt") { partName = "bigint"; @@ -2016,6 +2019,8 @@ void TSDeclGen::GenPartName(std::string &partName) partName = "Error"; } else if (partName == "Any") { partName = "ESObject"; + } else if (partName == "Floating" || partName == "Integral") { + partName = "number"; } } @@ -2242,6 +2247,9 @@ bool TSDeclGen::ShouldSkipMethodDeclaration(const ir::MethodDefinition *methodDe void TSDeclGen::EmitMethodGlueCode(const std::string &methodName, const ir::Identifier *methodIdentifier) { + if (!state_.inGlobalClass && (!state_.inNamespace || state_.isClassInNamespace || state_.isInterfaceInNamespace)) { + return; + } if (!ShouldEmitDeclarationSymbol(methodIdentifier)) { return; } @@ -2473,10 +2481,12 @@ void TSDeclGen::GenPropAccessor(const ir::ClassProperty *classProp, const std::s const auto propName = GetKeyIdent(classProp->Key())->Name().Mutf8(); OutDts(accessorKind, propName, accessorKind == "set " ? "(value: " : "(): "); - if (classProp->TypeAnnotation() != nullptr) { - ProcessTypeAnnotationType(classProp->TypeAnnotation(), classProp->TsType()); + auto typeAnnotation = classProp->TypeAnnotation(); + auto tsType = classProp->TsType(); + if (typeAnnotation != nullptr) { + ProcessTypeAnnotationType(typeAnnotation, tsType); } else { - GenType(classProp->TsType()); + GenType(tsType); } OutDts(accessorKind == "set " ? ");" : ";"); OutEndlDts(); @@ -2489,7 +2499,11 @@ void TSDeclGen::GenGlobalVarDeclaration(const ir::ClassProperty *globalVar) } const auto symbol = GetKeyIdent(globalVar->Key()); - const auto varName = symbol->Name().Mutf8(); + auto varName = symbol->Name().Mutf8(); + const std::string prefix = "gensym%%_"; + if (varName.rfind(prefix, 0) == 0) { + varName = varName.substr(prefix.size()); + } const bool isConst = globalVar->IsConst(); const bool isDefaultExported = globalVar->IsDefaultExported(); DebugPrint("GenGlobalVarDeclaration: " + varName); diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 2bfbb81790..a79857d9c7 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -184,7 +184,7 @@ export abstract class BaseMode { ) const declEtsOutputDir = path.dirname(declEtsOutputPath); const staticRecordRelativePath = changeFileExtension( - path.relative(declEtsOutputDir, staticRecordPath).replaceAll(/\\/g, '\/'), + path.relative(declEtsOutputDir, staticRecordPath).replace(/\\/g, '\/'), "", DECL_TS_SUFFIX ); diff --git a/ets2panda/driver/build_system/src/build/declgen_worker.ts b/ets2panda/driver/build_system/src/build/declgen_worker.ts index e25cdff91d..d19417743a 100644 --- a/ets2panda/driver/build_system/src/build/declgen_worker.ts +++ b/ets2panda/driver/build_system/src/build/declgen_worker.ts @@ -81,7 +81,7 @@ process.on('message', (message: { ) const declEtsOutputDir = path.dirname(declEtsOutputPath); const staticRecordRelativePath = changeFileExtension( - path.relative(declEtsOutputDir, staticRecordPath).replaceAll(/\\/g, '\/'), + path.relative(declEtsOutputDir, staticRecordPath).replace(/\\/g, '\/'), "", DECL_TS_SUFFIX ); -- Gitee