From aa5a83b7cb43d08f715477cbab8ea3a6c767f7cb Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Wed, 13 Aug 2025 15:46:00 +0300 Subject: [PATCH] Add cacheDir Signed-off-by: Ilya Trubachev Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICTQX8 --- ets2panda/cmake/coverage.cmake | 2 +- ets2panda/compiler/core/compilerImpl.cpp | 10 ++ ets2panda/test/unit/plugin/CMakeLists.txt | 26 ++++- .../test/unit/plugin/arktsconfig.in.json | 10 ++ ets2panda/test/unit/plugin/cache/export.d.ets | 27 ++++++ ets2panda/test/unit/plugin/export.d.ets | 27 ++++++ ...in_proceed_to_state_resolve_path_cache.cpp | 94 +++++++++++++++++++ ets2panda/util/arktsconfig.cpp | 14 ++- ets2panda/util/arktsconfig.h | 6 ++ .../util/diagnostic/arktsconfig_error.yaml | 4 + ets2panda/util/helpers.cpp | 10 +- ets2panda/util/helpers.h | 3 +- ets2panda/util/importPathManager.cpp | 48 +++++++++- ets2panda/util/importPathManager.h | 3 +- 14 files changed, 268 insertions(+), 16 deletions(-) create mode 100644 ets2panda/test/unit/plugin/arktsconfig.in.json create mode 100644 ets2panda/test/unit/plugin/cache/export.d.ets create mode 100644 ets2panda/test/unit/plugin/export.d.ets create mode 100644 ets2panda/test/unit/plugin/plugin_proceed_to_state_resolve_path_cache.cpp diff --git a/ets2panda/cmake/coverage.cmake b/ets2panda/cmake/coverage.cmake index d0233f5275..e9a3af2269 100644 --- a/ets2panda/cmake/coverage.cmake +++ b/ets2panda/cmake/coverage.cmake @@ -18,7 +18,7 @@ include(${PANDA_ROOT}/cmake/toolchain/coverage/unit_tests_lcov.cmake) add_custom_target(es2panda_coverage DEPENDS etsstdlib es2panda verifier ark) if (NOT DEFINED ES2PANDA_PATH) - set(ES2PANDA_PATH ${PANDA_ROOT}/tools/es2panda) + file(REAL_PATH ${PANDA_ROOT}/tools/es2panda ES2PANDA_PATH) endif() add_custom_command(TARGET es2panda_coverage POST_BUILD diff --git a/ets2panda/compiler/core/compilerImpl.cpp b/ets2panda/compiler/core/compilerImpl.cpp index 539573f68c..37b272b0ce 100644 --- a/ets2panda/compiler/core/compilerImpl.cpp +++ b/ets2panda/compiler/core/compilerImpl.cpp @@ -176,6 +176,16 @@ static bool RunVerifierAndPhases(public_lib::Context &context, parser::Program & if (afterCheckerPhase && context.diagnosticEngine->IsAnyError()) { return false; } + + if (options.IsGenerateDeclEnabled() && name == compiler::CheckerPhase::NAME) { + std::string path; + if (!options.WasSetGenerateDeclPath()) { + path = ark::os::RemoveExtension(util::BaseName(options.SourceFileName())).append(".d.ets"); + } else { + path = options.GetGenerateDeclPath(); + } + HandleGenerateDecl(program, *context.diagnosticEngine, path); + } } verifier.After(); diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index e0fa2fe3f5..d4ac3253c1 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -29,6 +29,9 @@ set(COMMON_SOURCE_FILES "util.cpp" ) +set(ETS_CONFIG_WITH_CACHE ${CMAKE_CURRENT_BINARY_DIR}/arktsconfig.json) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/arktsconfig.in.json ${ETS_CONFIG_WITH_CACHE}) + set(PLUGIN_TESTS #"test_name test_sts_file how_to_test_mode plugin_file_extension(c|cpp) how_to_compile_mode" "e2p_test_plugin_dump_json compile.ets ${RUNTIME_MODE} c ${LIBRARY_PLUGIN}" @@ -60,6 +63,7 @@ set(PLUGIN_TESTS # "plugin_proceed_to_state_run_verifier compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_ast_node_check compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_resolve_path compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_proceed_to_state_resolve_path_cache compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN} ${ETS_CONFIG_WITH_CACHE}" "plugin_proceed_to_state_arktsconfig compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_find_method_decl compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_find_method_decl_by_name import.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" @@ -156,11 +160,18 @@ foreach(TEST_DATA IN ITEMS ${PLUGIN_TESTS}) list(GET TEST_DATA_ELEM 1 TEST_STS_FILE) list(GET TEST_DATA_ELEM 2 TEST_MODE) list(GET TEST_DATA_ELEM 4 PLUGIN_MODE) + # if provided, use custom arktsconfig + list(LENGTH TEST_DATA_ELEM list_length) + if (list_length EQUAL 6) + list(GET TEST_DATA_ELEM 5 ARKTSCONFIG) + else() + set(ARKTSCONFIG ${CMAKE_BINARY_DIR}/bin/arktsconfig.json) + endif() if(${PLUGIN_MODE} STREQUAL ${LIBRARY_PLUGIN}) add_custom_target(es2panda-plugin-test-compile-${TEST_NAME} COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} ${PANDA_RUN_PREFIX} $ - --plugins=${TEST_NAME} --extension=ets --ets-unnamed --output="${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.abc" + --plugins=${TEST_NAME} --arktsconfig=${ARKTSCONFIG} --extension=ets --ets-unnamed --output="${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.abc" "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_STS_FILE}" > "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.out" ) endif() @@ -169,7 +180,7 @@ foreach(TEST_DATA IN ITEMS ${PLUGIN_TESTS}) COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} ${PANDA_RUN_PREFIX} ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} $ - --extension=ets --ets-unnamed --output="${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.abc" + --extension=ets --arktsconfig=${ARKTSCONFIG} --ets-unnamed --output="${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.abc" "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_STS_FILE}" > "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.out" 2>&1 ) endif() @@ -205,18 +216,25 @@ foreach(TEST_DATA IN ITEMS ${PLUGIN_TESTS}) list(GET TEST_DATA_ELEM 1 TEST_STS_FILE) list(GET TEST_DATA_ELEM 2 TEST_MODE) list(GET TEST_DATA_ELEM 4 PLUGIN_MODE) + # if provided, use custom arktsconfig + list(LENGTH TEST_DATA_ELEM list_length) + if (list_length EQUAL 6) + list(GET TEST_DATA_ELEM 5 ARKTSCONFIG) + else() + set(ARKTSCONFIG ${CMAKE_BINARY_DIR}/bin/arktsconfig.json) + endif() if(${TEST_MODE} STREQUAL ${EXPECTED_MODE}) if(${PLUGIN_MODE} STREQUAL ${LIBRARY_PLUGIN}) add_custom_target(update-es2panda-plugin-test-expected-${TEST_NAME} COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} ${PANDA_RUN_PREFIX} $ - --plugins=${TEST_NAME} --extension=ets --ets-unnamed "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_STS_FILE}" > + --plugins=${TEST_NAME} --extension=ets --arktsconfig=${ARKTSCONFIG} --ets-unnamed "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_STS_FILE}" > "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_NAME}-expected.txt") else() add_custom_target(update-es2panda-plugin-test-expected-${TEST_NAME} COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} ${PANDA_RUN_PREFIX} - ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} $ + ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} $ --arktsconfig=${ARKTSCONFIG} "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_STS_FILE}" > "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_NAME}-expected.txt") endif() diff --git a/ets2panda/test/unit/plugin/arktsconfig.in.json b/ets2panda/test/unit/plugin/arktsconfig.in.json new file mode 100644 index 0000000000..aa77ce4cc0 --- /dev/null +++ b/ets2panda/test/unit/plugin/arktsconfig.in.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "rootDir": "${ES2PANDA_PATH}${DELIM}test${DELIM}unit${DELIM}plugin", + "cacheDir": "${ES2PANDA_PATH}${DELIM}test${DELIM}unit${DELIM}plugin${DELIM}cache", + "paths": { + "std": ["${STATIC_CORE}${DELIM}plugins${DELIM}ets${DELIM}stdlib${DELIM}std"], + "escompat": ["${STATIC_CORE}${DELIM}plugins${DELIM}ets${DELIM}stdlib${DELIM}escompat"] + } + } +} \ No newline at end of file diff --git a/ets2panda/test/unit/plugin/cache/export.d.ets b/ets2panda/test/unit/plugin/cache/export.d.ets new file mode 100644 index 0000000000..05ef6b19ce --- /dev/null +++ b/ets2panda/test/unit/plugin/cache/export.d.ets @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export declare function foo() : int + +export declare class A { + foo() : int + too() : int + b: int; +} + +export declare class B { + foo() : int + b: int +} \ No newline at end of file diff --git a/ets2panda/test/unit/plugin/export.d.ets b/ets2panda/test/unit/plugin/export.d.ets new file mode 100644 index 0000000000..05ef6b19ce --- /dev/null +++ b/ets2panda/test/unit/plugin/export.d.ets @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export declare function foo() : int + +export declare class A { + foo() : int + too() : int + b: int; +} + +export declare class B { + foo() : int + b: int +} \ No newline at end of file diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_resolve_path_cache.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_resolve_path_cache.cpp new file mode 100644 index 0000000000..09713e886b --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_resolve_path_cache.cpp @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "util.h" +#include "public/es2panda_lib.h" +#include "os/file.h" + +// NOLINTBEGIN + +static es2panda_Impl *impl = nullptr; +static auto source = std::string("function main() { 1 + 2 }"); + +static auto ResolveImportPath(int argc, char **argv, es2panda_Context *context, char *sourceLiteral) +{ + auto importPathLiteral = impl->CreateStringLiteral1(context, sourceLiteral); + impl->AstNodeSetRange(context, importPathLiteral, + impl->CreateSourceRange(context, impl->CreateSourcePosition(context, 0, 0), + impl->CreateSourcePosition(context, 0, 0))); + auto resolvedPath = impl->ImportPathManagerResolvePathAPIConst( + context, impl->ETSParserGetImportPathManager(context), argv[argc - 1], importPathLiteral); + return resolvedPath; +} + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return 1; + } + + if (GetImpl() == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + impl = GetImpl(); + const char **args = const_cast(&(argv[1])); + auto config = impl->CreateConfig(argc - 1, args); + auto context = impl->CreateContextFromString(config, source.data(), argv[argc - 1]); + if (context == nullptr) { + std::cerr << "FAILED TO CREATE CONTEXT" << std::endl; + return NULLPTR_CONTEXT_ERROR_CODE; + } + + impl->ProceedToState(context, ES2PANDA_STATE_PARSED); + CheckForErrors("PARSE", context); + + auto pathDelim = ark::os::file::File::GetPathDelim(); + auto resolvedPath = ResolveImportPath(argc, argv, context, const_cast("./export.ets")); + if (strstr(resolvedPath, ("cache" + std::string(pathDelim) + "export.d.ets").c_str()) == nullptr) { + return TEST_ERROR_CODE; + } + + auto resolvedPath1 = ResolveImportPath(argc, argv, context, const_cast("./export")); + if (strstr(resolvedPath1, ("cache" + std::string(pathDelim) + "export.d.ets").c_str()) == nullptr) { + return TEST_ERROR_CODE; + } + + auto resolvedPath2 = ResolveImportPath(argc, argv, context, const_cast("./export.d.ets")); + if (strstr(resolvedPath2, "export.d.ets") == nullptr || + strstr(resolvedPath2, ("cache" + std::string(pathDelim) + "export.d.ets").c_str()) != nullptr) { + return TEST_ERROR_CODE; + } + + impl->ProceedToState(context, ES2PANDA_STATE_CHECKED); + if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { + return PROCEED_ERROR_CODE; + } + + auto ast = impl->ProgramAst(context, impl->ContextProgram(context)); + impl->AstNodeRecheck(context, ast); + + impl->DestroyContext(context); + impl->DestroyConfig(config); + + return 0; +} + +// NOLINTEND diff --git a/ets2panda/util/arktsconfig.cpp b/ets2panda/util/arktsconfig.cpp index 4dc5a76a68..c63bc91f97 100644 --- a/ets2panda/util/arktsconfig.cpp +++ b/ets2panda/util/arktsconfig.cpp @@ -365,11 +365,19 @@ bool ArkTsConfig::ParseCompilerOptions(std::string &arktsConfigDir, const JsonOb // Parse "package" package_ = ValueOrEmptyString(compilerOptions, PACKAGE); - // Parse "baseUrl", "outDir", "rootDir" + // Parse "baseUrl", "outDir", "rootDir", "cacheDir" baseUrl_ = MakeAbsolute(ValueOrEmptyString(compilerOptions, BASE_URL), arktsConfigDir); outDir_ = MakeAbsolute(ValueOrEmptyString(compilerOptions, OUT_DIR), arktsConfigDir); - rootDir_ = MakeAbsolute(ValueOrEmptyString(compilerOptions, ROOT_DIR), arktsConfigDir); - + rootDir_ = ValueOrEmptyString(compilerOptions, ROOT_DIR); + if (!rootDir_.empty() && + !Check(ark::os::file::File::IsDirectory(rootDir_), diagnostic::NOT_A_DIR, {"rootDir " + rootDir_})) { + return false; + } + cacheDir_ = ValueOrEmptyString(compilerOptions, CACHE_DIR); + if (!cacheDir_.empty() && + !Check(ark::os::file::File::IsDirectory(cacheDir_), diagnostic::NOT_A_DIR, {"cacheDir " + cacheDir_})) { + return false; + } // Parse "useUrl" if (compilerOptions->get()->HasKey(USE_EMPTY_PACKAGE)) { auto *useUrl = compilerOptions->get()->GetValue(USE_EMPTY_PACKAGE); diff --git a/ets2panda/util/arktsconfig.h b/ets2panda/util/arktsconfig.h index 5e7e115714..eeb613ccb2 100644 --- a/ets2panda/util/arktsconfig.h +++ b/ets2panda/util/arktsconfig.h @@ -155,6 +155,10 @@ public: { return outDir_; } + const std::string &CacheDir() const + { + return cacheDir_; + } const std::vector &Files() const { return files_; @@ -208,6 +212,7 @@ private: static constexpr const char *OUT_DIR = "outDir"; static constexpr const char *ROOT_DIR = "rootDir"; static constexpr const char *DEPENDENCIES = "dependencies"; // CC-OFF(G.NAM.03,G.NAM.03-CPP) project code style + static constexpr const char *CACHE_DIR = "cacheDir"; // CC-OFF(G.NAM.03,G.NAM.03-CPP) project code style void Inherit(const ArkTsConfig &base); bool ParseDependency(size_t keyIdx, const std::unique_ptr *dependencies, @@ -221,6 +226,7 @@ private: bool useUrl_ = false; std::string outDir_ {}; std::string rootDir_ {}; + std::string cacheDir_ {}; PathsMap paths_ {}; std::map dependencies_ {}; std::vector files_ {}; diff --git a/ets2panda/util/diagnostic/arktsconfig_error.yaml b/ets2panda/util/diagnostic/arktsconfig_error.yaml index d839babae0..21d311b7b1 100644 --- a/ets2panda/util/diagnostic/arktsconfig_error.yaml +++ b/ets2panda/util/diagnostic/arktsconfig_error.yaml @@ -65,6 +65,10 @@ arkts_config_error: id: 4 message: Invalid value for '{}' with key '{}' +- name: NOT_A_DIR + id: 19 + message: '{} is not a directory' + - name: NOT_ROOT_DIR id: 15 message: '{} is not root directory for {}' diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index 51c7b324e4..3e5771e8cf 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -165,7 +165,15 @@ util::StringView Helpers::ToStringView(ArenaAllocator *allocator, int32_t number return str.View(); } -bool Helpers::EndsWith(const std::string &str, const std::string &suffix) +bool Helpers::StartsWith(const std::string_view str, const std::string_view prefix) +{ + if (str.length() < prefix.length()) { + return false; + } + return str.compare(0, prefix.size(), prefix) == 0; +} + +bool Helpers::EndsWith(const std::string_view str, const std::string_view suffix) { if (str.length() < suffix.length()) { return false; diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index 396ff2c401..c6ece8cab1 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -113,7 +113,8 @@ public: static util::StringView ToStringView(ArenaAllocator *allocator, double number); static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number); static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number); - static bool EndsWith(const std::string &str, const std::string &suffix); + static bool StartsWith(const std::string_view str, const std::string_view prefix); + static bool EndsWith(const std::string_view str, const std::string_view suffix); static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node); static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node); diff --git a/ets2panda/util/importPathManager.cpp b/ets2panda/util/importPathManager.cpp index 9b64cc0f4b..712cb83141 100644 --- a/ets2panda/util/importPathManager.cpp +++ b/ets2panda/util/importPathManager.cpp @@ -28,6 +28,7 @@ #include "abc2program_driver.h" #include "checker/types/signature.h" #include "compiler/lowering/ets/declGenPhase.h" +#include "libpandabase/utils/logger.h" #ifdef USE_UNIX_SYSCALL #include @@ -62,7 +63,7 @@ static bool IsAbsolute(const std::string &path) #endif // ARKTSCONFIG_USE_FILESYSTEM } -void ImportPathManager::ProcessExternalModuleImport(ImportMetadata &importData) +void ImportPathManager::ProcessExternalLibraryImport(ImportMetadata &importData) { ES2PANDA_ASSERT(!IsAbsolute(std::string(importData.resolvedSource))); auto it = arktsConfig_->Dependencies().find(std::string(importData.resolvedSource)); @@ -111,6 +112,36 @@ void ImportPathManager::ProcessExternalModuleImport(ImportMetadata &importData) importData.declText = declText; } +// If needed, the result of this function can be cached +std::string_view ImportPathManager::tryImportFromDeclarationCache(std::string_view resolvedImportPath) const +{ + // if package or unresolved file, just skip + if (ark::os::file::File::IsDirectory(std::string(resolvedImportPath)) || + !ark::os::file::File::IsRegularFile(std::string(resolvedImportPath))) { + return resolvedImportPath; + } + const std::string etsSuffix = ".ets"; + const std::string dEtsSuffix = ".d.ets"; + const auto &rootDir = ArkTSConfig().get()->RootDir(); + const auto &cacheDir = ArkTSConfig().get()->CacheDir(); + if (cacheDir.empty() || rootDir.empty()) { + return resolvedImportPath; + } + // declaration cache is used only for .ets files, located in the same library as compiling file + if (!Helpers::EndsWith(resolvedImportPath, etsSuffix) || !Helpers::StartsWith(resolvedImportPath, rootDir)) { + return resolvedImportPath; + } + const auto &relativeFilePath = + resolvedImportPath.substr(rootDir.size(), resolvedImportPath.size() - rootDir.size()); + const auto &declarationCacheFile = + cacheDir + std::string(relativeFilePath.substr(0, relativeFilePath.size() - etsSuffix.size())) + dEtsSuffix; + + if (!ark::os::file::File::IsRegularFile(declarationCacheFile)) { + return resolvedImportPath; + } + return UString(declarationCacheFile, allocator_).View().Utf8(); +} + ImportPathManager::ImportMetadata ImportPathManager::GatherImportMetadata(parser::Program *program, ImportFlags importFlags, ir::StringLiteral *importPath) @@ -132,9 +163,8 @@ ImportPathManager::ImportMetadata ImportPathManager::GatherImportMetadata(parser ImportMetadata importData {importFlags}; importData.resolvedSource = resolvedImportPath; if (resolvedIsExternalModule) { - ProcessExternalModuleImport(importData); + ProcessExternalLibraryImport(importData); } else { - ES2PANDA_ASSERT(IsAbsolute(std::string(importData.resolvedSource))); importData.lang = ToLanguage(program->Extension()).GetId(); importData.declPath = util::ImportPathManager::DUMMY_PATH; importData.ohmUrl = util::ImportPathManager::DUMMY_PATH; @@ -144,6 +174,8 @@ ImportPathManager::ImportMetadata ImportPathManager::GatherImportMetadata(parser AddToParseList(importData); } + LOG(DEBUG, ES2PANDA) << "[" << curModulePath << "] " + << "Import " << importPath->ToString() << " resolved to " << importData.resolvedSource; return importData; } @@ -173,6 +205,7 @@ ImportPathManager::ResolvedPathRes ImportPathManager::ResolvePath(std::string_vi diagnosticEngine_.LogDiagnostic(diagnostic::EMPTY_IMPORT_PATH, util::DiagnosticMessageParams {}); return {*importPath}; } + ResolvedPathRes result {}; if (IsRelativePath(*importPath)) { size_t pos = curModulePath.find_last_of("/\\"); auto currentDir = (pos != std::string::npos) ? curModulePath.substr(0, pos) : "."; @@ -181,10 +214,15 @@ ImportPathManager::ResolvedPathRes ImportPathManager::ResolvePath(std::string_vi resolvedPath.Append(pathDelimiter_); resolvedPath.Append(*importPath); - return AppendExtensionOrIndexFileIfOmitted(resolvedPath.View()); + result = AppendExtensionOrIndexFileIfOmitted(resolvedPath.View()); + } else { + result = ResolveAbsolutePath(*importPath); } - return ResolveAbsolutePath(*importPath); + if (!result.resolvedIsExternalModule) { + result.resolvedPath = tryImportFromDeclarationCache(result.resolvedPath); + } + return result; } ImportPathManager::ResolvedPathRes ImportPathManager::ResolveAbsolutePath(const ir::StringLiteral &importPathNode) const diff --git a/ets2panda/util/importPathManager.h b/ets2panda/util/importPathManager.h index 04c4742836..9d4a701777 100644 --- a/ets2panda/util/importPathManager.h +++ b/ets2panda/util/importPathManager.h @@ -184,7 +184,8 @@ private: ResolvedPathRes AppendExtensionOrIndexFileIfOmitted(StringView basePath) const; std::string TryMatchDependencies(std::string_view fixedPath) const; StringView GetRealPath(StringView path) const; - void ProcessExternalModuleImport(ImportMetadata &importData); + void ProcessExternalLibraryImport(ImportMetadata &importData); + std::string_view tryImportFromDeclarationCache(std::string_view resolvedImportPath) const; public: void AddToParseList(const ImportMetadata &importMetadata); -- Gitee