1307f5ae8SZachary Turner //===-- CompileUnitIndex.h --------------------------------------*- C++ -*-===//
2307f5ae8SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6307f5ae8SZachary Turner //
7307f5ae8SZachary Turner //===----------------------------------------------------------------------===//
8307f5ae8SZachary Turner 
9cdc514e4SJonas Devlieghere #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_NATIVEPDB_COMPILEUNITINDEX_H
10cdc514e4SJonas Devlieghere #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_NATIVEPDB_COMPILEUNITINDEX_H
11307f5ae8SZachary Turner 
12*f00cd23cSZequan Wu #include "lldb/Utility/RangeMap.h"
13307f5ae8SZachary Turner #include "llvm/ADT/DenseMap.h"
14307f5ae8SZachary Turner #include "llvm/ADT/DenseSet.h"
15307f5ae8SZachary Turner #include "llvm/ADT/IntervalMap.h"
16307f5ae8SZachary Turner #include "llvm/ADT/Optional.h"
17*f00cd23cSZequan Wu #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
18307f5ae8SZachary Turner #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
19307f5ae8SZachary Turner #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
20307f5ae8SZachary Turner #include "llvm/DebugInfo/CodeView/TypeIndex.h"
21307f5ae8SZachary Turner #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
22307f5ae8SZachary Turner #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
23307f5ae8SZachary Turner #include "llvm/DebugInfo/PDB/PDBTypes.h"
24307f5ae8SZachary Turner 
25307f5ae8SZachary Turner #include "PdbSymUid.h"
26307f5ae8SZachary Turner 
27307f5ae8SZachary Turner #include <map>
28307f5ae8SZachary Turner #include <memory>
29307f5ae8SZachary Turner 
30307f5ae8SZachary Turner namespace lldb_private {
31307f5ae8SZachary Turner 
32307f5ae8SZachary Turner namespace npdb {
33307f5ae8SZachary Turner class PdbIndex;
34307f5ae8SZachary Turner 
35307f5ae8SZachary Turner /// Represents a single compile unit.  This class is useful for collecting the
36307f5ae8SZachary Turner /// important accessors and information about a compile unit from disparate
37307f5ae8SZachary Turner /// parts of the PDB into a single place, simplifying acess to compile unit
38307f5ae8SZachary Turner /// information for the callers.
39307f5ae8SZachary Turner struct CompilandIndexItem {
406284aee9SZachary Turner   CompilandIndexItem(PdbCompilandId m_id,
41307f5ae8SZachary Turner                      llvm::pdb::ModuleDebugStreamRef debug_stream,
42307f5ae8SZachary Turner                      llvm::pdb::DbiModuleDescriptor descriptor);
43307f5ae8SZachary Turner 
446284aee9SZachary Turner   // index of this compile unit.
456284aee9SZachary Turner   PdbCompilandId m_id;
46307f5ae8SZachary Turner 
47307f5ae8SZachary Turner   // debug stream.
48307f5ae8SZachary Turner   llvm::pdb::ModuleDebugStreamRef m_debug_stream;
49307f5ae8SZachary Turner 
50307f5ae8SZachary Turner   // dbi module descriptor.
51307f5ae8SZachary Turner   llvm::pdb::DbiModuleDescriptor m_module_descriptor;
52307f5ae8SZachary Turner 
53307f5ae8SZachary Turner   llvm::codeview::StringsAndChecksumsRef m_strings;
54307f5ae8SZachary Turner 
55307f5ae8SZachary Turner   // List of files which contribute to this compiland.
56307f5ae8SZachary Turner   std::vector<llvm::StringRef> m_file_list;
57307f5ae8SZachary Turner 
58307f5ae8SZachary Turner   // Maps virtual address to global symbol id, which can then be used to
59307f5ae8SZachary Turner   // locate the exact compile unit and offset of the symbol.  Note that this
60307f5ae8SZachary Turner   // is intentionally an ordered map so that we can find all symbols up to a
61307f5ae8SZachary Turner   // given starting address.
62307f5ae8SZachary Turner   std::map<lldb::addr_t, PdbSymUid> m_symbols_by_va;
63307f5ae8SZachary Turner 
64307f5ae8SZachary Turner   // S_COMPILE3 sym describing compilation settings for the module.
65307f5ae8SZachary Turner   llvm::Optional<llvm::codeview::Compile3Sym> m_compile_opts;
66307f5ae8SZachary Turner 
67307f5ae8SZachary Turner   // S_OBJNAME sym describing object name.
68307f5ae8SZachary Turner   llvm::Optional<llvm::codeview::ObjNameSym> m_obj_name;
69307f5ae8SZachary Turner 
70307f5ae8SZachary Turner   // LF_BUILDINFO sym describing source file name, working directory,
71307f5ae8SZachary Turner   // command line, etc.  This usually contains exactly 5 items which
72307f5ae8SZachary Turner   // are references to other strings.
73307f5ae8SZachary Turner   llvm::SmallVector<llvm::codeview::TypeIndex, 5> m_build_info;
74*f00cd23cSZequan Wu 
75*f00cd23cSZequan Wu   // Inlinee lines table in this compile unit.
76*f00cd23cSZequan Wu   std::map<llvm::codeview::TypeIndex, llvm::codeview::InlineeSourceLine>
77*f00cd23cSZequan Wu       m_inline_map;
78*f00cd23cSZequan Wu 
79*f00cd23cSZequan Wu   // It's the line table parsed from DEBUG_S_LINES sections, mapping the file
80*f00cd23cSZequan Wu   // address range to file index and source line number.
81*f00cd23cSZequan Wu   using GlobalLineTable =
82*f00cd23cSZequan Wu       lldb_private::RangeDataVector<lldb::addr_t, uint32_t,
83*f00cd23cSZequan Wu                                     std::pair<uint32_t, uint32_t>>;
84*f00cd23cSZequan Wu   GlobalLineTable m_global_line_table;
85307f5ae8SZachary Turner };
86307f5ae8SZachary Turner 
87307f5ae8SZachary Turner /// Indexes information about all compile units.  This is really just a map of
88307f5ae8SZachary Turner /// global compile unit index to |CompilandIndexItem| structures.
89307f5ae8SZachary Turner class CompileUnitIndex {
90307f5ae8SZachary Turner   PdbIndex &m_index;
916284aee9SZachary Turner   llvm::DenseMap<uint16_t, std::unique_ptr<CompilandIndexItem>> m_comp_units;
92307f5ae8SZachary Turner 
93307f5ae8SZachary Turner public:
CompileUnitIndex(PdbIndex & index)94307f5ae8SZachary Turner   explicit CompileUnitIndex(PdbIndex &index) : m_index(index) {}
95307f5ae8SZachary Turner 
96307f5ae8SZachary Turner   CompilandIndexItem &GetOrCreateCompiland(uint16_t modi);
97307f5ae8SZachary Turner 
98307f5ae8SZachary Turner   const CompilandIndexItem *GetCompiland(uint16_t modi) const;
99307f5ae8SZachary Turner 
100307f5ae8SZachary Turner   CompilandIndexItem *GetCompiland(uint16_t modi);
101307f5ae8SZachary Turner 
102307f5ae8SZachary Turner   llvm::SmallString<64> GetMainSourceFile(const CompilandIndexItem &item) const;
103307f5ae8SZachary Turner };
104307f5ae8SZachary Turner } // namespace npdb
105307f5ae8SZachary Turner } // namespace lldb_private
106307f5ae8SZachary Turner 
107307f5ae8SZachary Turner #endif
108