From d8ab2fc48de86fb93aac688480e5f1234249111e Mon Sep 17 00:00:00 2001 From: jiangmengyang Date: Wed, 2 Apr 2025 16:51:34 +0800 Subject: [PATCH] remove etsglobal Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBVKV5 Signed-off-by: jiangmengyang Change-Id: I75cdbac9f491a46221949283c16ac4179df95149 --- ets2panda/checker/ETSAnalyzerHelpers.cpp | 7 ++++--- ets2panda/checker/ets/object.cpp | 4 ++-- ets2panda/evaluate/debugInfoStorage.cpp | 13 +++++-------- ets2panda/evaluate/debugInfoStorage.h | 1 + ets2panda/evaluate/evaluateContext.cpp | 3 +-- ets2panda/ir/expressions/identifier.cpp | 9 ++++----- ets2panda/lsp/src/completions.cpp | 2 +- ets2panda/util/helpers.cpp | 6 ++++++ ets2panda/util/helpers.h | 2 ++ ets2panda/varbinder/ETSBinder.cpp | 14 +++----------- 10 files changed, 29 insertions(+), 32 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index 473909a8d1e..217e634fe8e 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -448,7 +448,8 @@ checker::Signature *GetMostSpecificSigFromExtensionFuncAndClassMethod(checker::E expr->Arguments().erase(expr->Arguments().begin()); if (signature != nullptr) { - if (signature->Owner()->Name() == compiler::Signatures::ETS_GLOBAL) { + if (signature->Owner()->GetDeclNode()->IsClassDefinition() && + signature->Owner()->GetDeclNode()->AsClassDefinition()->IsGlobal()) { SwitchMethodCallToFunctionCall(checker, expr, signature); } else { auto *var = type->ClassMethodType()->Variable(); @@ -463,8 +464,8 @@ checker::Signature *ResolveCallForETSExtensionFuncHelperType(checker::ETSExtensi { ES2PANDA_ASSERT(expr->Callee()->IsMemberExpression()); auto *calleeObj = expr->Callee()->AsMemberExpression()->Object(); - bool isCalleeObjETSGlobal = - calleeObj->IsIdentifier() && calleeObj->AsIdentifier()->Name() == compiler::Signatures::ETS_GLOBAL; + bool isCalleeObjETSGlobal = calleeObj->TsType()->AsETSObjectType()->GetDeclNode()->IsClassDefinition() && + calleeObj->TsType()->AsETSObjectType()->GetDeclNode()->AsClassDefinition()->IsGlobal(); // for callExpr `a.foo`, there are 3 situations: // 1.`a.foo` is private method call of class A; // 2.`a.foo` is extension function of `A`(function with receiver `A`) diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 9b36448b0bc..3351d8b641b 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -46,6 +46,7 @@ #include "varbinder/variableFlags.h" #include "generated/signatures.h" #include "generated/diagnostic.h" +#include "util/helpers.h" namespace ark::es2panda::checker { @@ -2403,10 +2404,9 @@ void ETSChecker::CheckGetterSetterProperties(ETSObjectType *classType) void ETSChecker::AddElementsToModuleObject(ETSObjectType *moduleObj, const util::StringView &str) { for (const auto &[name, var] : VarBinder()->GetScope()->Bindings()) { - if (name.Is(str.Mutf8()) || name.Is(compiler::Signatures::ETS_GLOBAL)) { + if (name.Is(str.Mutf8()) || util::Helpers::IsGlobalVar(var)) { continue; } - ES2PANDA_ASSERT(name.Utf8().find(compiler::Signatures::ETS_GLOBAL) == std::string::npos); if (var->HasFlag(varbinder::VariableFlags::METHOD)) { moduleObj->AddProperty(var->AsLocalVariable()); diff --git a/ets2panda/evaluate/debugInfoStorage.cpp b/ets2panda/evaluate/debugInfoStorage.cpp index 6abac655788..1aa286225d6 100644 --- a/ets2panda/evaluate/debugInfoStorage.cpp +++ b/ets2panda/evaluate/debugInfoStorage.cpp @@ -35,12 +35,6 @@ std::string GetFullRecordName(const panda_file::File &pf, const panda_file::File return type.GetPandasmName(); } -bool EndsWith(std::string_view str, std::string_view suffix) -{ - auto pos = str.rfind(suffix); - return pos != std::string::npos && (str.size() - pos) == suffix.size(); -} - } // namespace ImportExportTable::ImportExportTable(ArenaAllocator *allocator) @@ -69,10 +63,13 @@ void DebugInfoStorage::LoadFileDebugInfo(std::string_view pfPath) continue; } - auto recordName = GetFullRecordName(*pf, classId); - if (!EndsWith(recordName, compiler::Signatures::ETS_GLOBAL)) { + panda_file::ClassDataAccessor cda(*pf, classId); + bool isModule = false; + cda.EnumerateAnnotation(ANNOTATION_MODULE.data(), [&isModule](auto &) { return (isModule = true); }); + if (!isModule) { continue; } + auto recordName = GetFullRecordName(*pf, classId); std::string_view moduleName = helpers::SplitRecordName(recordName).first; auto *debugInfo = allocator_->New(std::move(pf), classId, moduleName); diff --git a/ets2panda/evaluate/debugInfoStorage.h b/ets2panda/evaluate/debugInfoStorage.h index e93e64cbc9c..43966dca6b0 100644 --- a/ets2panda/evaluate/debugInfoStorage.h +++ b/ets2panda/evaluate/debugInfoStorage.h @@ -99,6 +99,7 @@ public: private: using DebugInfoMap = ArenaUnorderedMap; + static constexpr std::string_view ANNOTATION_MODULE = "Lstd/annotations/Module;"; private: void LoadFileDebugInfo(std::string_view pfPath); diff --git a/ets2panda/evaluate/evaluateContext.cpp b/ets2panda/evaluate/evaluateContext.cpp index a7b96d36e51..602501673bd 100644 --- a/ets2panda/evaluate/evaluateContext.cpp +++ b/ets2panda/evaluate/evaluateContext.cpp @@ -33,8 +33,7 @@ void EvaluateContext::FindEvaluationMethod(parser::Program *evalMethodProgram) // Find evaluation class. auto evalClassDefIter = std::find_if(topLevelStatements.begin(), topLevelStatements.end(), [](auto *stmt) { - return stmt->IsClassDeclaration() && - !stmt->AsClassDeclaration()->Definition()->Ident()->Name().Is(compiler::Signatures::ETS_GLOBAL); + return stmt->IsClassDeclaration() && !stmt->AsClassDeclaration()->Definition()->IsGlobal(); }); ES2PANDA_ASSERT(evalClassDefIter != topLevelStatements.end()); auto *methodClass = (*evalClassDefIter)->AsClassDeclaration()->Definition(); diff --git a/ets2panda/ir/expressions/identifier.cpp b/ets2panda/ir/expressions/identifier.cpp index c384d5d5f03..1690151cb29 100644 --- a/ets2panda/ir/expressions/identifier.cpp +++ b/ets2panda/ir/expressions/identifier.cpp @@ -176,11 +176,6 @@ checker::VerifiedType Identifier::Check(checker::ETSChecker *checker) bool Identifier::IsDeclaration(ScriptExtension ext) const { - // GLOBAL class is not a reference - if (Name() == compiler::Signatures::ETS_GLOBAL) { - return true; - } - // We can determine reference status from parent node if (Parent() == nullptr) { return false; @@ -387,6 +382,10 @@ bool Identifier::CheckDeclarationsPart1(const ir::AstNode *parent, [[maybe_unuse return true; } + if (Parent()->IsClassDefinition()) { + return Parent()->AsClassDefinition()->IsGlobal(); + } + return false; } diff --git a/ets2panda/lsp/src/completions.cpp b/ets2panda/lsp/src/completions.cpp index 099804f8270..5d82469fe98 100644 --- a/ets2panda/lsp/src/completions.cpp +++ b/ets2panda/lsp/src/completions.cpp @@ -488,7 +488,7 @@ std::string GetDeclName(const ir::AstNode *decl) bool IsGlobalVar(const ir::AstNode *node) { return node->IsClassProperty() && node->Parent()->IsClassDefinition() && - node->Parent()->AsClassDefinition()->Ident()->AsIdentifier()->Name() == compiler::Signatures::ETS_GLOBAL; + node->Parent()->AsClassDefinition()->IsGlobal(); } bool IsVariableOfKind(const ir::Identifier *node, ir::VariableDeclaration::VariableDeclarationKind kind) diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index 2b9494e9664..9fe1f04f065 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -769,4 +769,10 @@ bool Helpers::IsAsyncMethod(ir::AstNode const *node) return method->Function()->IsAsyncFunc() && !method->Function()->IsProxy(); } +bool Helpers::IsGlobalVar(const ark::es2panda::varbinder::Variable *var) +{ + return var->Declaration()->Node()->IsClassDeclaration() && + var->Declaration()->Node()->AsClassDeclaration()->Definition()->IsGlobal(); +} + } // namespace ark::es2panda::util diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index 75b9eed9038..a861a59a42c 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -160,6 +160,8 @@ public: std::uint32_t index); static bool IsAsyncMethod(ir::AstNode const *node); + static bool IsGlobalVar(const ark::es2panda::varbinder::Variable *var); + template static ArenaVector ConvertVector(const ArenaVector &src) { diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index e8a663f1705..2bf06e171c3 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -18,6 +18,7 @@ #include "evaluate/scopedDebugInfoPlugin.h" #include "public/public.h" #include "compiler/lowering/util.h" +#include "util/helpers.h" namespace ark::es2panda::varbinder { @@ -651,13 +652,11 @@ void ETSBinder::ImportAllForeignBindings(ir::AstNode *const specifier, bool const isStdLib = util::Helpers::IsStdLib(Program()); for (const auto [bindingName, var] : globalBindings) { - if (bindingName.Is(compiler::Signatures::ETS_GLOBAL)) { + if (util::Helpers::IsGlobalVar(var)) { const auto *const classDef = var->Declaration()->Node()->AsClassDeclaration()->Definition(); ImportGlobalProperties(classDef); continue; } - ES2PANDA_ASSERT(bindingName.Utf8().find(compiler::Signatures::ETS_GLOBAL) == std::string::npos); - if (!importGlobalScope->IsForeignBinding(bindingName) && !var->Declaration()->Node()->IsDefaultExported() && (var->AsLocalVariable()->Declaration()->Node()->IsExported() || var->AsLocalVariable()->Declaration()->Node()->IsExportedType())) { @@ -1200,14 +1199,7 @@ void ETSBinder::BuildProgram() auto &stmts = Program()->Ast()->Statements(); const auto etsGlobal = std::find_if(stmts.begin(), stmts.end(), [](const ir::Statement *stmt) { - if (stmt->IsClassDeclaration() && - !stmt->AsClassDeclaration()->Definition()->Ident()->Name().Is(compiler::Signatures::ETS_GLOBAL)) { - ES2PANDA_ASSERT(stmt->AsClassDeclaration()->Definition()->Ident()->Name().Utf8().find( - // CC-OFFNXT(G.FMT.06-CPP,G.FMT.05-CPP) project code style - compiler::Signatures::ETS_GLOBAL) == std::string::npos); - } - return stmt->IsClassDeclaration() && - stmt->AsClassDeclaration()->Definition()->Ident()->Name().Is(compiler::Signatures::ETS_GLOBAL); + return stmt->IsClassDeclaration() && stmt->AsClassDeclaration()->Definition()->IsGlobal(); }); if (etsGlobal != stmts.end()) { const auto begin = std::find_if(stmts.rbegin(), stmts.rend(), [](const ir::Statement *stmt) { -- Gitee