13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include "evaluate/pathResolver.h" 173af6ab5fSopenharmony_ci#include "evaluate/debugInfoStorage.h" 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "parser/program/program.h" 203af6ab5fSopenharmony_ci 213af6ab5fSopenharmony_cinamespace ark::es2panda::evaluate { 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_civoid PathResolver::FindImportedFunctions(ArenaVector<EntityInfo> &overloadSet, std::string_view filePath, 243af6ab5fSopenharmony_ci std::string_view entityName) 253af6ab5fSopenharmony_ci{ 263af6ab5fSopenharmony_ci // NOTE: cache all the resolved paths. 273af6ab5fSopenharmony_ci auto *table = debugInfoStorage_.GetImportExportTable(filePath); 283af6ab5fSopenharmony_ci if (table == nullptr) { 293af6ab5fSopenharmony_ci LOG(WARNING, ES2PANDA) << "Failed to find import/export table for " << filePath; 303af6ab5fSopenharmony_ci return; 313af6ab5fSopenharmony_ci } 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_ci // `import * as B from "C"` should not be searched, as it handled differently in compiler. 343af6ab5fSopenharmony_ci const auto &imports = table->GetImports(); 353af6ab5fSopenharmony_ci auto optOverloadSet = imports.find(entityName); 363af6ab5fSopenharmony_ci if (optOverloadSet == imports.end()) { 373af6ab5fSopenharmony_ci return; 383af6ab5fSopenharmony_ci } 393af6ab5fSopenharmony_ci 403af6ab5fSopenharmony_ci ASSERT(!optOverloadSet->second.empty()); 413af6ab5fSopenharmony_ci for (const auto &[path, entity] : optOverloadSet->second) { 423af6ab5fSopenharmony_ci // `import {A as B} from "C"` 433af6ab5fSopenharmony_ci FindExportedFunctions(overloadSet, path, entity); 443af6ab5fSopenharmony_ci } 453af6ab5fSopenharmony_ci} 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_civoid PathResolver::FindExportedFunctions(ArenaVector<EntityInfo> &overloadSet, std::string_view filePath, 483af6ab5fSopenharmony_ci std::string_view entityName) 493af6ab5fSopenharmony_ci{ 503af6ab5fSopenharmony_ci // NOTE: cache all the resolved paths. 513af6ab5fSopenharmony_ci auto *table = debugInfoStorage_.GetImportExportTable(filePath); 523af6ab5fSopenharmony_ci if (table == nullptr) { 533af6ab5fSopenharmony_ci LOG(WARNING, ES2PANDA) << "Failed to find import/export table for " << filePath; 543af6ab5fSopenharmony_ci return; 553af6ab5fSopenharmony_ci } 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci const auto &exports = table->GetExports(); 583af6ab5fSopenharmony_ci const auto optOverloadSet = exports.find(entityName); 593af6ab5fSopenharmony_ci if (optOverloadSet != exports.end()) { 603af6ab5fSopenharmony_ci ASSERT(!optOverloadSet->second.empty()); 613af6ab5fSopenharmony_ci for (const auto &[path, entity] : optOverloadSet->second) { 623af6ab5fSopenharmony_ci // `export {A as B} from "C"` 633af6ab5fSopenharmony_ci if (path.empty()) { 643af6ab5fSopenharmony_ci overloadSet.push_back(EntityInfo(filePath, entity)); 653af6ab5fSopenharmony_ci } else { 663af6ab5fSopenharmony_ci FindExportedFunctions(overloadSet, path, entity); 673af6ab5fSopenharmony_ci } 683af6ab5fSopenharmony_ci } 693af6ab5fSopenharmony_ci } 703af6ab5fSopenharmony_ci 713af6ab5fSopenharmony_ci // Still need to traverse re-export-all statements to fill the complete overload set. 723af6ab5fSopenharmony_ci const auto optReExportAll = exports.find(STAR_IMPORT); 733af6ab5fSopenharmony_ci if (optReExportAll != exports.end()) { 743af6ab5fSopenharmony_ci ASSERT(!optReExportAll->second.empty()); 753af6ab5fSopenharmony_ci for (const auto &[path, entity] : optReExportAll->second) { 763af6ab5fSopenharmony_ci // export * from "C" 773af6ab5fSopenharmony_ci (void)entity; 783af6ab5fSopenharmony_ci ASSERT(entity == STAR_IMPORT); 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_ci FindExportedFunctions(overloadSet, path, entityName); 813af6ab5fSopenharmony_ci } 823af6ab5fSopenharmony_ci } 833af6ab5fSopenharmony_ci} 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_cistd::string_view PathResolver::FindNamedImportAll(std::string_view filePath, std::string_view bindingName) 863af6ab5fSopenharmony_ci{ 873af6ab5fSopenharmony_ci auto *table = debugInfoStorage_.GetImportExportTable(filePath); 883af6ab5fSopenharmony_ci if (table == nullptr) { 893af6ab5fSopenharmony_ci LOG(WARNING, ES2PANDA) << "Failed to find import/export table for " << filePath; 903af6ab5fSopenharmony_ci return {}; 913af6ab5fSopenharmony_ci } 923af6ab5fSopenharmony_ci 933af6ab5fSopenharmony_ci const auto &imports = table->GetImports(); 943af6ab5fSopenharmony_ci auto optEntity = imports.find(bindingName); 953af6ab5fSopenharmony_ci if (optEntity == imports.end()) { 963af6ab5fSopenharmony_ci return {}; 973af6ab5fSopenharmony_ci } 983af6ab5fSopenharmony_ci 993af6ab5fSopenharmony_ci ASSERT(!optEntity->second.empty()); 1003af6ab5fSopenharmony_ci for (const auto &[path, entity] : optEntity->second) { 1013af6ab5fSopenharmony_ci if (entity == STAR_IMPORT) { 1023af6ab5fSopenharmony_ci return path; 1033af6ab5fSopenharmony_ci } 1043af6ab5fSopenharmony_ci } 1053af6ab5fSopenharmony_ci return {}; 1063af6ab5fSopenharmony_ci} 1073af6ab5fSopenharmony_ci 1083af6ab5fSopenharmony_cistd::optional<EntityInfo> PathResolver::FindImportedEntity(std::string_view filePath, std::string_view entityName) 1093af6ab5fSopenharmony_ci{ 1103af6ab5fSopenharmony_ci // NOTE: cache all the resolved paths. 1113af6ab5fSopenharmony_ci auto *table = debugInfoStorage_.GetImportExportTable(filePath); 1123af6ab5fSopenharmony_ci if (table == nullptr) { 1133af6ab5fSopenharmony_ci LOG(WARNING, ES2PANDA) << "Failed to find import/export table for " << filePath; 1143af6ab5fSopenharmony_ci return {}; 1153af6ab5fSopenharmony_ci } 1163af6ab5fSopenharmony_ci 1173af6ab5fSopenharmony_ci // `import * as B from "C"` should not be searched, as it handled differently in compiler. 1183af6ab5fSopenharmony_ci const auto &imports = table->GetImports(); 1193af6ab5fSopenharmony_ci auto optEntity = imports.find(entityName); 1203af6ab5fSopenharmony_ci if (optEntity == imports.end()) { 1213af6ab5fSopenharmony_ci return {}; 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci 1243af6ab5fSopenharmony_ci ASSERT(!optEntity->second.empty()); 1253af6ab5fSopenharmony_ci if (optEntity->second.size() > 1) { 1263af6ab5fSopenharmony_ci // Have more than one imports for the given name - it could not be a variable. 1273af6ab5fSopenharmony_ci return {}; 1283af6ab5fSopenharmony_ci } 1293af6ab5fSopenharmony_ci // `import {A as B} from "C"` 1303af6ab5fSopenharmony_ci auto [path, entity] = optEntity->second[0]; 1313af6ab5fSopenharmony_ci return FindExportedEntity(path, entity); 1323af6ab5fSopenharmony_ci} 1333af6ab5fSopenharmony_ci 1343af6ab5fSopenharmony_ci// Note that the current implementation does not guarantee that the found entity is indeed a variable, 1353af6ab5fSopenharmony_ci// so users must check it manually by traversing the found file's ETSGLOBAL fields. 1363af6ab5fSopenharmony_cistd::optional<EntityInfo> PathResolver::FindExportedEntity(std::string_view filePath, std::string_view entityName) 1373af6ab5fSopenharmony_ci{ 1383af6ab5fSopenharmony_ci // NOTE: cache all the resolved paths. 1393af6ab5fSopenharmony_ci auto *table = debugInfoStorage_.GetImportExportTable(filePath); 1403af6ab5fSopenharmony_ci if (table == nullptr) { 1413af6ab5fSopenharmony_ci LOG(WARNING, ES2PANDA) << "Failed to find import/export table for " << filePath; 1423af6ab5fSopenharmony_ci return {}; 1433af6ab5fSopenharmony_ci } 1443af6ab5fSopenharmony_ci 1453af6ab5fSopenharmony_ci const auto &exports = table->GetExports(); 1463af6ab5fSopenharmony_ci const auto optOverloadSet = exports.find(entityName); 1473af6ab5fSopenharmony_ci if (optOverloadSet != exports.end()) { 1483af6ab5fSopenharmony_ci ASSERT(!optOverloadSet->second.empty()); 1493af6ab5fSopenharmony_ci if (optOverloadSet->second.size() > 1) { 1503af6ab5fSopenharmony_ci // Have more than one imports for the given name, but we search for the single one - variable or class. 1513af6ab5fSopenharmony_ci return {}; 1523af6ab5fSopenharmony_ci } 1533af6ab5fSopenharmony_ci // export {A as B} from "C" 1543af6ab5fSopenharmony_ci const auto &[path, entity] = optOverloadSet->second[0]; 1553af6ab5fSopenharmony_ci if (path.empty()) { 1563af6ab5fSopenharmony_ci return EntityInfo(filePath, entity); 1573af6ab5fSopenharmony_ci } 1583af6ab5fSopenharmony_ci return FindExportedEntity(path, entity); 1593af6ab5fSopenharmony_ci } 1603af6ab5fSopenharmony_ci 1613af6ab5fSopenharmony_ci const auto optReExportAll = exports.find(STAR_IMPORT); 1623af6ab5fSopenharmony_ci if (optReExportAll != exports.end()) { 1633af6ab5fSopenharmony_ci ASSERT(!optReExportAll->second.empty()); 1643af6ab5fSopenharmony_ci for (const auto &[path, entity] : optReExportAll->second) { 1653af6ab5fSopenharmony_ci // export * from "C" 1663af6ab5fSopenharmony_ci (void)entity; 1673af6ab5fSopenharmony_ci ASSERT(entity == STAR_IMPORT); 1683af6ab5fSopenharmony_ci 1693af6ab5fSopenharmony_ci auto optResult = FindExportedEntity(path, entityName); 1703af6ab5fSopenharmony_ci if (optResult) { 1713af6ab5fSopenharmony_ci return optResult; 1723af6ab5fSopenharmony_ci } 1733af6ab5fSopenharmony_ci } 1743af6ab5fSopenharmony_ci } 1753af6ab5fSopenharmony_ci 1763af6ab5fSopenharmony_ci return {}; 1773af6ab5fSopenharmony_ci} 1783af6ab5fSopenharmony_ci 1793af6ab5fSopenharmony_ci} // namespace ark::es2panda::evaluate 180