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#ifndef ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
173af6ab5fSopenharmony_ci#define ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include "evaluate/evaluateContext.h"
203af6ab5fSopenharmony_ci#include "evaluate/importExportTable.h"
213af6ab5fSopenharmony_ci#include "util/ustring.h"
223af6ab5fSopenharmony_ci
233af6ab5fSopenharmony_ci#include "libpandafile/debug_info_extractor.h"
243af6ab5fSopenharmony_ci#include "libpandafile/file.h"
253af6ab5fSopenharmony_ci#include "libpandafile/class_data_accessor.h"
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ci#include <memory>
283af6ab5fSopenharmony_ci#include <optional>
293af6ab5fSopenharmony_ci#include <string_view>
303af6ab5fSopenharmony_ci#include <unordered_map>
313af6ab5fSopenharmony_ci#include <utility>
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_cinamespace ark::es2panda::evaluate {
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_cistruct FileDebugInfo final {
363af6ab5fSopenharmony_ci    using RecordsMap = ArenaUnorderedMap<util::StringView, panda_file::File::EntityId>;
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ci    FileDebugInfo() = delete;
393af6ab5fSopenharmony_ci    explicit FileDebugInfo(std::unique_ptr<const panda_file::File> &&pandaFile, panda_file::File::EntityId classId,
403af6ab5fSopenharmony_ci                           std::string_view module)
413af6ab5fSopenharmony_ci        : pf(std::move(pandaFile)), globalClassAcc(*pf, classId), moduleName(module)
423af6ab5fSopenharmony_ci    {
433af6ab5fSopenharmony_ci        ASSERT(pf);
443af6ab5fSopenharmony_ci    }
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci    // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
473af6ab5fSopenharmony_ci    std::unique_ptr<const panda_file::File> pf {nullptr};
483af6ab5fSopenharmony_ci    panda_file::ClassDataAccessor globalClassAcc;
493af6ab5fSopenharmony_ci    std::string_view moduleName;
503af6ab5fSopenharmony_ci    std::string_view sourceFilePath;
513af6ab5fSopenharmony_ci    std::optional<ImportExportTable> importExportTable;
523af6ab5fSopenharmony_ci    std::optional<RecordsMap> records;
533af6ab5fSopenharmony_ci    // NOLINTEND(misc-non-private-member-variables-in-classes)
543af6ab5fSopenharmony_ci};
553af6ab5fSopenharmony_ci
563af6ab5fSopenharmony_ci// Context-independent debug info lazy-loading storage.
573af6ab5fSopenharmony_ci// All "find" methods must accept paths to source files.
583af6ab5fSopenharmony_ciclass DebugInfoStorage final {
593af6ab5fSopenharmony_cipublic:
603af6ab5fSopenharmony_ci    explicit DebugInfoStorage(const CompilerOptions &options, ArenaAllocator *allocator);
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(DebugInfoStorage);
633af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(DebugInfoStorage);
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci    ~DebugInfoStorage() = default;
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ci    ArenaAllocator *Allocator()
683af6ab5fSopenharmony_ci    {
693af6ab5fSopenharmony_ci        return allocator_;
703af6ab5fSopenharmony_ci    }
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci    [[nodiscard]] bool FillEvaluateContext(EvaluateContext &context);
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci    const panda_file::File *GetPandaFile(std::string_view filePath);
753af6ab5fSopenharmony_ci    const ImportExportTable *GetImportExportTable(std::string_view filePath);
763af6ab5fSopenharmony_ci    panda_file::ClassDataAccessor *GetGlobalClassAccessor(std::string_view filePath);
773af6ab5fSopenharmony_ci    std::string_view GetModuleName(std::string_view filePath);
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    FileDebugInfo *GetDebugInfoByModuleName(std::string_view moduleName) const;
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_ci    /**
823af6ab5fSopenharmony_ci     * @brief Returns class'es panda file Id on success, invalid EntityId otherwise
833af6ab5fSopenharmony_ci     */
843af6ab5fSopenharmony_ci    panda_file::File::EntityId FindClass(std::string_view filePath, std::string_view className);
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci    template <typename Callback>
873af6ab5fSopenharmony_ci    void EnumerateContextFiles(const Callback &cb)
883af6ab5fSopenharmony_ci    {
893af6ab5fSopenharmony_ci        for (const auto &[sourceFilePath, debugInfo] : sourceFileToDebugInfo_) {
903af6ab5fSopenharmony_ci            if (!cb(sourceFilePath, debugInfo->pf.get(), debugInfo->globalClassAcc, debugInfo->moduleName)) {
913af6ab5fSopenharmony_ci                return;
923af6ab5fSopenharmony_ci            }
933af6ab5fSopenharmony_ci        }
943af6ab5fSopenharmony_ci    }
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ciprivate:
973af6ab5fSopenharmony_ci    using DebugInfoMap = ArenaUnorderedMap<std::string_view, FileDebugInfo *>;
983af6ab5fSopenharmony_ci
993af6ab5fSopenharmony_ciprivate:
1003af6ab5fSopenharmony_ci    void LoadFileDebugInfo(std::string_view pfPath);
1013af6ab5fSopenharmony_ci
1023af6ab5fSopenharmony_ci    const ImportExportTable &LazyLoadImportExportTable(FileDebugInfo *info);
1033af6ab5fSopenharmony_ci    const FileDebugInfo::RecordsMap &LazyLoadRecords(FileDebugInfo *info);
1043af6ab5fSopenharmony_ci
1053af6ab5fSopenharmony_ciprivate:
1063af6ab5fSopenharmony_ci    ArenaAllocator *allocator_ {nullptr};
1073af6ab5fSopenharmony_ci
1083af6ab5fSopenharmony_ci    // Mapping from sources' files names into the corresponding debug information struct.
1093af6ab5fSopenharmony_ci    // Used for fast lookups basing on imports/exports tables.
1103af6ab5fSopenharmony_ci    DebugInfoMap sourceFileToDebugInfo_;
1113af6ab5fSopenharmony_ci    // Mapping from module names into the corresponding debug information struct.
1123af6ab5fSopenharmony_ci    // Used for fast lookups during inheritance chain resolution.
1133af6ab5fSopenharmony_ci    DebugInfoMap moduleNameToDebugInfo_;
1143af6ab5fSopenharmony_ci};
1153af6ab5fSopenharmony_ci
1163af6ab5fSopenharmony_ci}  // namespace ark::es2panda::evaluate
1173af6ab5fSopenharmony_ci
1183af6ab5fSopenharmony_ci#endif  // ES2PANDA_EVALUATE_DEBUG_INFO_STORAGE_H
119