diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index 473909a8d1e7c0bcb079060334621fa403d226ab..217e634fe8e1ba8c190403bef759a95fca2f984d 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 9b36448b0bcfbaeae96b279b35ce235a4e10d262..3351d8b641ba2af040ab56cb8bc1b7187d8b99ca 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 6abac6557882a0ddf4395004b77324d470fdf203..1aa286225d616c9afb852a66bd59efa962c082b0 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 e93e64cbc9cfbee778037395eed79e6169586615..43966dca6b0ff74b13dc79db3d14776c48b1797d 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 a7b96d36e51b71ba9d71e2fb93de7b8044af4155..602501673bde182422c5c065942058992a604e49 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 c384d5d5f03eed13cc8a51f7512281234f2690d3..1690151cb29039cc2e6b1e062bff7757b7a01612 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 099804f8270e9181b856f2d8cf844f2400ae865f..5d82469fe98700f3936bbb7bffbd383af4471dd3 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 2b9494e9664797237dbee76f33a1680bd2c33da1..9fe1f04f065c678eb2b681baf35ba3a2b868dc5d 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 75b9eed90386c1365b5fe083e4a08b028ac5501a..a861a59a42c4503ea57e85770b0bd372d297e3ac 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 e8a663f1705e923a2830273fce99a4eb484203e4..2bf06e171c37bd7c79797f3d437ffe117b4de457 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) {