• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
17 #define ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
18 
19 #include "evaluate/evaluateContext.h"
20 #include "evaluate/importExportTable.h"
21 #include "util/ustring.h"
22 
23 #include "libpandafile/debug_info_extractor.h"
24 #include "libpandafile/file.h"
25 #include "libpandafile/class_data_accessor.h"
26 
27 #include <memory>
28 #include <optional>
29 #include <string_view>
30 #include <unordered_map>
31 #include <utility>
32 
33 namespace ark::es2panda::util {
34 class Options;
35 }  // namespace ark::es2panda::util
36 
37 namespace ark::es2panda::evaluate {
38 
39 struct FileDebugInfo final {
40     using RecordsMap = ArenaUnorderedMap<util::StringView, panda_file::File::EntityId>;
41 
42     FileDebugInfo() = delete;
FileDebugInfofinal43     explicit FileDebugInfo(std::unique_ptr<const panda_file::File> &&pandaFile, panda_file::File::EntityId classId,
44                            std::string_view module)
45         : pf(std::move(pandaFile)), globalClassAcc(*pf, classId), moduleName(module)
46     {
47         ES2PANDA_ASSERT(pf);
48     }
49 
50     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
51     std::unique_ptr<const panda_file::File> pf {nullptr};
52     panda_file::ClassDataAccessor globalClassAcc;
53     std::string_view moduleName;
54     std::string_view sourceFilePath;
55     std::optional<ImportExportTable> importExportTable;
56     std::optional<RecordsMap> records;
57     // NOLINTEND(misc-non-private-member-variables-in-classes)
58 };
59 
60 // Context-independent debug info lazy-loading storage.
61 // All "find" methods must accept paths to source files.
62 class DebugInfoStorage final {
63 public:
64     explicit DebugInfoStorage(const util::Options &options, ArenaAllocator *allocator);
65 
66     NO_COPY_SEMANTIC(DebugInfoStorage);
67     NO_MOVE_SEMANTIC(DebugInfoStorage);
68 
69     ~DebugInfoStorage() = default;
70 
Allocator()71     ArenaAllocator *Allocator()
72     {
73         return allocator_;
74     }
75 
76     [[nodiscard]] bool FillEvaluateContext(EvaluateContext &context);
77 
78     const panda_file::File *GetPandaFile(std::string_view filePath);
79     const ImportExportTable *GetImportExportTable(std::string_view filePath);
80     panda_file::ClassDataAccessor *GetGlobalClassAccessor(std::string_view filePath);
81     std::string_view GetModuleName(std::string_view filePath);
82 
83     FileDebugInfo *GetDebugInfoByModuleName(std::string_view moduleName) const;
84 
85     /**
86      * @brief Returns class'es panda file Id on success, invalid EntityId otherwise
87      */
88     panda_file::File::EntityId FindClass(std::string_view filePath, std::string_view className);
89 
90     template <typename Callback>
EnumerateContextFiles(const Callback & cb)91     void EnumerateContextFiles(const Callback &cb)
92     {
93         for (const auto &[sourceFilePath, debugInfo] : sourceFileToDebugInfo_) {
94             if (!cb(sourceFilePath, debugInfo->pf.get(), debugInfo->globalClassAcc, debugInfo->moduleName)) {
95                 return;
96             }
97         }
98     }
99 
100 private:
101     using DebugInfoMap = ArenaUnorderedMap<std::string_view, FileDebugInfo *>;
102     static constexpr std::string_view ANNOTATION_MODULE = "Lstd/annotations/Module;";
103 
104 private:
105     void LoadFileDebugInfo(std::string_view pfPath);
106 
107     const ImportExportTable &LazyLoadImportExportTable(FileDebugInfo *info);
108     const FileDebugInfo::RecordsMap &LazyLoadRecords(FileDebugInfo *info);
109 
110 private:
111     ArenaAllocator *allocator_ {nullptr};
112 
113     // Mapping from sources' files names into the corresponding debug information struct.
114     // Used for fast lookups basing on imports/exports tables.
115     DebugInfoMap sourceFileToDebugInfo_;
116     // Mapping from module names into the corresponding debug information struct.
117     // Used for fast lookups during inheritance chain resolution.
118     DebugInfoMap moduleNameToDebugInfo_;
119 };
120 
121 }  // namespace ark::es2panda::evaluate
122 
123 #endif  // ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
124