From dee909632a3d593c2e6945ee901738fa6aa7950f Mon Sep 17 00:00:00 2001 From: Csaba Hurton Date: Tue, 29 Jul 2025 14:33:32 +0200 Subject: [PATCH 1/3] Update Helpers::IsStdLib logic Make it indifferent from module names Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICCALN Testing: * all test-u-runner test suits Change-Id: I8c3ed616fe6dc4f5de20268e0eed3dbe4ecbd6d0 Signed-off-by: Csaba Hurton --- .../ets/topLevelStmts/globalClassHandler.cpp | 6 ++-- ets2panda/parser/ETSparser.cpp | 4 +-- ets2panda/parser/ETSparser.h | 2 +- ets2panda/parser/parserImpl.cpp | 30 +++++++++++++++++++ ets2panda/parser/parserImpl.h | 5 ++++ ets2panda/parser/program/program.h | 11 +++++++ ets2panda/util/helpers.cpp | 27 ++++++++++------- ets2panda/util/helpers.h | 2 +- ets2panda/varbinder/ETSBinder.cpp | 7 +++-- 9 files changed, 74 insertions(+), 20 deletions(-) diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp index 8ff6a03d75..238a717544 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp @@ -410,7 +410,7 @@ void GlobalClassHandler::SetupGlobalClass(const ArenaVector & // NOTE(vpukhov): stdlib checks are to be removed - do not extend the existing logic if (globalProgram_->Kind() != parser::ScriptKind::STDLIB) { AddStaticBlockToClass(globalClass); - if (!util::Helpers::IsStdLib(globalProgram_)) { + if (!util::Helpers::IsStdLib(globalProgram_, parser_)) { auto initStatements = FormInitMethodStatements(moduleDependencies, std::move(immediateInitializers)); SetupGlobalMethods(std::move(initStatements)); } @@ -550,7 +550,7 @@ ArenaVector> GlobalClassHandler::FormInitStaticBloc ArenaVector> staticBlocks(allocator_->Adapter()); for (const auto &[p, ps] : initStatements) { ArenaVector statements(allocator_->Adapter()); - if (!util::Helpers::IsStdLib(globalProgram_) && moduleDependencies != nullptr) { + if (!util::Helpers::IsStdLib(globalProgram_, parser_) && moduleDependencies != nullptr) { FormDependentInitTriggers(statements, moduleDependencies); } statements.insert(statements.end(), ps.begin(), ps.end()); @@ -564,7 +564,7 @@ ArenaVector GlobalClassHandler::FormInitMethodStatements(const ArenaVector &&initStatements) { ArenaVector statements(allocator_->Adapter()); - if (!util::Helpers::IsStdLib(globalProgram_) && moduleDependencies != nullptr) { + if (!util::Helpers::IsStdLib(globalProgram_, parser_) && moduleDependencies != nullptr) { FormDependentInitTriggers(statements, moduleDependencies); } for (const auto &[p, ps] : initStatements) { diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 0e9894d3ff..575d8ff5f1 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -101,7 +101,7 @@ bool ETSParser::IsETSParser() const noexcept bool ETSParser::IsValidIdentifierName(const lexer::Token &token) const noexcept { - return !token.IsPredefinedType() || util::Helpers::IsStdLib(GetProgram()); + return !token.IsPredefinedType() || util::Helpers::IsStdLib(GetProgram(), this); } std::unique_ptr ETSParser::InitLexer(const SourceFile &sourceFile) @@ -840,7 +840,7 @@ ir::TSTypeAliasDeclaration *ETSParser::ParseTypeAliasDeclaration() return nullptr; } - if (Lexer()->GetToken().IsReservedTypeName() && !util::Helpers::IsStdLib(GetProgram())) { + if (Lexer()->GetToken().IsReservedTypeName() && !util::Helpers::IsStdLib(GetProgram(), this)) { LogError(diagnostic::TYPE_ALIAS_INVALID_NAME, {TokenToString(Lexer()->GetToken().KeywordType())}); } diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index eeb70f15dc..6ad67fd77d 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -442,7 +442,7 @@ private: bool IsExternal() const override { - return util::Helpers::IsStdLib(GetProgram()); + return util::Helpers::IsStdLib(GetProgram(), this); } // NOLINTNEXTLINE(google-default-arguments) diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 1496bd4dbd..9441c09b56 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -87,6 +87,36 @@ void ParserImpl::ParseProgram(ScriptKind kind) program_->SetAst(blockStmt); } +const std::optional> &ParserImpl::GetStdLibDirs() const +{ + return stdLibDirectories_; +} +void ParserImpl::SetStdLibDirs(ArenaUnorderedSet &dirs) const +{ + std::mutex mtx; + std::lock_guard lock(mtx); + stdLibDirectories_ = dirs; +} + +void ParserImpl::CollectStdLibPaths() const +{ + auto isDynamic = GetProgram()->IsDeclForDynamicStaticInterop(); + ArenaUnorderedSet stdLibDirs = ArenaUnorderedSet(Allocator()->Adapter()); + for (auto const &stdLibPath : util::Helpers::StdLib()) { + std::optional resolvedPath; + if (!GetOptions().GetStdlib().empty()) { + resolvedPath = std::string(ark::os::GetAbsolutePath(GetOptions().GetStdlib()) + + ark::os::file::File::GetPathDelim().at(0) + std::string(stdLibPath)); + } else if (GetOptions().ArkTSConfig() != nullptr) { + resolvedPath = GetOptions().ArkTSConfig()->ResolvePath(std::string_view(stdLibPath), isDynamic); + } else { + break; + } + stdLibDirs.insert(util::StringView(util::UString(ark::os::NormalizePath(*resolvedPath), Allocator()).View())); + } + SetStdLibDirs(stdLibDirs); +} + bool ParserImpl::InAmbientContext() { return (context_.Status() & ParserStatus::IN_AMBIENT_CONTEXT) != 0; diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index d0238cef36..b1404dbd3b 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -222,6 +222,10 @@ public: void LogSyntaxError(std::string_view errorMessage, const lexer::SourcePosition &pos); void LogSyntaxError(const util::DiagnosticMessageParams &list, const lexer::SourcePosition &pos); + void CollectStdLibPaths() const; + const std::optional> &GetStdLibDirs() const; + void SetStdLibDirs(ArenaUnorderedSet &dirs) const; + protected: void LogParameterModifierError(ir::ModifierFlags status); // Error handling @@ -608,6 +612,7 @@ private: util::DiagnosticEngine &diagnosticEngine_; public_lib::Context *ctx_ {nullptr}; RecursiveContext recursiveCtx_; + mutable std::optional> stdLibDirectories_ {}; }; } // namespace ark::es2panda::parser diff --git a/ets2panda/parser/program/program.h b/ets2panda/parser/program/program.h index fbb24ddfd2..7097576a39 100644 --- a/ets2panda/parser/program/program.h +++ b/ets2panda/parser/program/program.h @@ -303,6 +303,16 @@ public: (FileName().Is("etsstdlib")); } + void SetIsInStdLib(bool isInStdLib) + { + isInStdLib_ = isInStdLib; + } + + std::optional GetIsInStdLib() + { + return isInStdLib_; + } + bool IsGenAbcForExternal() const; void SetGenAbcForExternalSources(bool genAbc = true) @@ -393,6 +403,7 @@ private: compiler::CFG *cfg_; ArenaVector functionScopes_; std::unordered_map> fileDependencies_; + std::optional isInStdLib_ {}; private: ArenaMap varbinders_; diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index fe9e9b5e32..f31be42a04 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -768,23 +768,30 @@ bool Helpers::IsNumericGlobalBuiltIn(checker::Type *type, checker::ETSChecker *c CheckTypeRelation(checker, type, checker->GetGlobalTypesHolder()->GlobalFloatBuiltinType()); } -bool Helpers::IsStdLib(const parser::Program *program) +bool Helpers::IsStdLib(parser::Program *program, const parser::ParserImpl *parser) { - // NOTE(rsipka): early check: if program is not in a package then it is not part of the stdlib either - if (!program->IsPackage()) { + if (program->GetIsInStdLib().has_value()) { + return program->GetIsInStdLib().value(); + } + + if (program->Extension() != ScriptExtension::ETS || !program->IsPackage()) { + program->SetIsInStdLib(false); return false; } - auto fileFolder = program->ModuleName().Mutf8(); - std::replace(fileFolder.begin(), fileFolder.end(), *compiler::Signatures::METHOD_SEPARATOR.begin(), - *compiler::Signatures::NAMESPACE_SEPARATOR.begin()); + if (!parser->GetStdLibDirs().has_value()) { + parser->CollectStdLibPaths(); + } - if (fileFolder == "std/math/consts") { - return true; + for (auto stdPath : *parser->GetStdLibDirs()) { + if (program->AbsoluteName().StartsWith(stdPath.Mutf8())) { + program->SetIsInStdLib(true); + return true; + } } - auto const &stdlib = StdLib(); - return std::count(stdlib.begin(), stdlib.end(), fileFolder) != 0; + program->SetIsInStdLib(false); + return false; } checker::Type *Helpers::CheckReturnTypeOfCheck([[maybe_unused]] const ir::AstNode *const node, diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index c6ece8cab1..190798e7c0 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -203,7 +203,7 @@ public: static std::pair SplitSignature(std::string_view signature); static std::vector const &StdLib(); - static bool IsStdLib(const parser::Program *program); + static bool IsStdLib(parser::Program *program, const parser::ParserImpl *parser); [[nodiscard]] static checker::Type *CheckReturnTypeOfCheck([[maybe_unused]] const ir::AstNode *const node, checker::Type *const type); diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 1e7d1b087e..8ff17a8a5e 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -659,7 +659,7 @@ void ETSBinder::ImportAllForeignBindings(const varbinder::Scope::VariableMap &gl const varbinder::GlobalScope *const importGlobalScope, const ir::ETSImportDeclaration *const import) { - bool const isStdLib = util::Helpers::IsStdLib(Program()); + bool const isStdLib = util::Helpers::IsStdLib(Program(), GetContext()->parser->AsETSParser()); for (const auto [bindingName, var] : globalBindings) { if (!var->Declaration()->Node()->IsValidInCurrentPhase()) { @@ -946,7 +946,8 @@ bool ETSBinder::DetectNameConflict(const util::StringView localName, Variable *c } if (var->Declaration()->IsFunctionDecl() && otherVar->Declaration()->IsFunctionDecl()) { - AddOverloadFlag(Allocator(), util::Helpers::IsStdLib(Program()), var, otherVar); + AddOverloadFlag(Allocator(), util::Helpers::IsStdLib(Program(), GetContext()->parser->AsETSParser()), var, + otherVar); return true; } @@ -1552,7 +1553,7 @@ bool ETSBinder::ImportGlobalPropertiesForNotDefaultedExports(varbinder::Variable auto variable = Program()->GlobalClassScope()->FindLocal(name, ResolveBindingOptions::ALL); - bool isStdLib = util::Helpers::IsStdLib(Program()); + bool isStdLib = util::Helpers::IsStdLib(Program(), GetContext()->parser); if (variable != nullptr && var != variable) { if (variable->Declaration()->IsFunctionDecl() && var->Declaration()->IsFunctionDecl()) { AddOverloadFlag(Allocator(), isStdLib, var, variable); -- Gitee From 47c6e3e84c103c314cc298c804a84e8877b7982c Mon Sep 17 00:00:00 2001 From: Csaba Hurton Date: Thu, 28 Aug 2025 15:26:06 +0200 Subject: [PATCH 2/3] 25992_3.11A log some infos in CollectStdLibPaths --- ets2panda/parser/parserImpl.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 9441c09b56..39085e22fa 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -102,6 +102,7 @@ void ParserImpl::CollectStdLibPaths() const { auto isDynamic = GetProgram()->IsDeclForDynamicStaticInterop(); ArenaUnorderedSet stdLibDirs = ArenaUnorderedSet(Allocator()->Adapter()); + std::cout << std::endl << std::endl << std::endl << std::endl << std::endl; for (auto const &stdLibPath : util::Helpers::StdLib()) { std::optional resolvedPath; if (!GetOptions().GetStdlib().empty()) { @@ -110,10 +111,16 @@ void ParserImpl::CollectStdLibPaths() const } else if (GetOptions().ArkTSConfig() != nullptr) { resolvedPath = GetOptions().ArkTSConfig()->ResolvePath(std::string_view(stdLibPath), isDynamic); } else { + std::cout << "I have no basepath for stdlib" << std::endl; break; } stdLibDirs.insert(util::StringView(util::UString(ark::os::NormalizePath(*resolvedPath), Allocator()).View())); } + std::cout << "---- these are the stdlib folders:" << std::endl; + for (auto d : stdLibDirs) { + std::cout << d << std::endl; + } + std::cout << std::endl << std::endl << std::endl << std::endl << std::endl; SetStdLibDirs(stdLibDirs); } -- Gitee From c86dd7924e81986b7864de88b80f7bbf4be588e7 Mon Sep 17 00:00:00 2001 From: Csaba Hurton Date: Mon, 1 Sep 2025 11:41:39 +0200 Subject: [PATCH 3/3] 25992_3.11B log some infos in IsStdLib --- ets2panda/parser/parserImpl.cpp | 23 +++++++++++++++++++++++ ets2panda/util/helpers.cpp | 7 +++++++ ets2panda/util/importPathManager.cpp | 2 ++ 3 files changed, 32 insertions(+) diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 39085e22fa..0882541fc3 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -103,12 +103,35 @@ void ParserImpl::CollectStdLibPaths() const auto isDynamic = GetProgram()->IsDeclForDynamicStaticInterop(); ArenaUnorderedSet stdLibDirs = ArenaUnorderedSet(Allocator()->Adapter()); std::cout << std::endl << std::endl << std::endl << std::endl << std::endl; + std::cout << "GetOptions().GetStdlib(): " << GetOptions().GetStdlib() << std::endl; + std::cout << "GetOptions().GetStdlib(): " << GetOptions().GetStdlib() << std::endl; + std::cout << std::endl << std::endl << std::endl << std::endl << std::endl; + bool alreadyWritten = false; for (auto const &stdLibPath : util::Helpers::StdLib()) { std::optional resolvedPath; if (!GetOptions().GetStdlib().empty()) { + std::cout << "set using --stdlib" << std::endl; resolvedPath = std::string(ark::os::GetAbsolutePath(GetOptions().GetStdlib()) + ark::os::file::File::GetPathDelim().at(0) + std::string(stdLibPath)); } else if (GetOptions().ArkTSConfig() != nullptr) { + if (!alreadyWritten) { + std::cout << "set using arktsconfig" << std::endl; + std::cout << "BaseURL()" << GetOptions().ArkTSConfig()->BaseUrl() << std::endl; + std::cout << "ConfigPath()" << GetOptions().ArkTSConfig()->ConfigPath() << std::endl; + std::cout << "Files():" << std::endl; + for (auto f : GetOptions().ArkTSConfig()->Files()) { + std::cout << " - " << f << std::endl; + } + std::cout << "OutDir()" << GetOptions().ArkTSConfig()->OutDir() << std::endl; + std::cout << "RootDir()" << GetOptions().ArkTSConfig()->RootDir() << std::endl; + for (auto p : GetOptions().ArkTSConfig()->Paths()) { + std::cout << " - " << p.first << ": " << std::endl; + for (auto pp : p.second) { + std::cout << " - "<< pp << std::endl; + } + } + alreadyWritten = true; + } resolvedPath = GetOptions().ArkTSConfig()->ResolvePath(std::string_view(stdLibPath), isDynamic); } else { std::cout << "I have no basepath for stdlib" << std::endl; diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index f31be42a04..516936db43 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -776,6 +776,11 @@ bool Helpers::IsStdLib(parser::Program *program, const parser::ParserImpl *parse if (program->Extension() != ScriptExtension::ETS || !program->IsPackage()) { program->SetIsInStdLib(false); + if (program->Extension() != ScriptExtension::ETS) { + std::cout << program->AbsoluteName() << ": not std lib because extension is not ETS" << std::endl; + } else if (!program->IsPackage()) { + std::cout << program->AbsoluteName() << ": not std lib because it is not a PACKAGE" << std::endl; + } return false; } @@ -791,6 +796,8 @@ bool Helpers::IsStdLib(parser::Program *program, const parser::ParserImpl *parse } program->SetIsInStdLib(false); + + std::cout << program->AbsoluteName() << ": not std lib because i could not find it in the STDDIRS" << std::endl; return false; } diff --git a/ets2panda/util/importPathManager.cpp b/ets2panda/util/importPathManager.cpp index a7f93fb25e..96906a27e1 100644 --- a/ets2panda/util/importPathManager.cpp +++ b/ets2panda/util/importPathManager.cpp @@ -269,6 +269,7 @@ ImportPathManager::ResolvedPathRes ImportPathManager::ResolveAbsolutePath(const auto rootPart = containsDelim ? importPath.substr(0, pos) : importPath; if (!stdLib_.empty() && ((rootPart == "std") || (rootPart == "escompat"))) { // Get std or escompat path from CLI if provided + std::cout << "stdLib_ is NOT empty in ResolveAbsolutePath" << std::endl; auto baseUrl = std::string(GetRealPath(StringView(stdLib_))) + pathDelimiter_.at(0) + std::string(rootPart); if (containsDelim) { @@ -278,6 +279,7 @@ ImportPathManager::ResolvedPathRes ImportPathManager::ResolveAbsolutePath(const return {UString(baseUrl, allocator_).View().Utf8()}; } + std::cout << "stdLib_ is empty in ResolveAbsolutePath" << std::endl; ES2PANDA_ASSERT(arktsConfig_ != nullptr); auto resolvedPath = arktsConfig_->ResolvePath(importPath, isDynamic_); if (!resolvedPath) { -- Gitee