1 //===-- SymbolFileNativePDB.h -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLDB_PLUGINS_SYMBOLFILE_NATIVEPDB_SYMBOLFILENATIVEPDB_H
11 #define LLDB_PLUGINS_SYMBOLFILE_NATIVEPDB_SYMBOLFILENATIVEPDB_H
12 
13 #include "lldb/Symbol/ClangASTImporter.h"
14 #include "lldb/Symbol/SymbolFile.h"
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/DebugInfo/CodeView/CVRecord.h"
18 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
19 #include "llvm/DebugInfo/PDB/PDBTypes.h"
20 
21 #include "CompileUnitIndex.h"
22 #include "PdbIndex.h"
23 
24 namespace clang {
25 class TagDecl;
26 }
27 
28 namespace llvm {
29 namespace codeview {
30 class ClassRecord;
31 class EnumRecord;
32 class ModifierRecord;
33 class PointerRecord;
34 struct UnionRecord;
35 } // namespace codeview
36 } // namespace llvm
37 
38 namespace lldb_private {
39 class ClangASTImporter;
40 
41 namespace npdb {
42 
43 struct DeclStatus {
44   DeclStatus() = default;
45   DeclStatus(lldb::user_id_t uid, Type::ResolveStateTag status)
46       : uid(uid), status(status) {}
47   lldb::user_id_t uid = 0;
48   Type::ResolveStateTag status = Type::eResolveStateForward;
49 };
50 
51 class SymbolFileNativePDB : public SymbolFile {
52   friend class UdtRecordCompleter;
53 
54 public:
55   //------------------------------------------------------------------
56   // Static Functions
57   //------------------------------------------------------------------
58   static void Initialize();
59 
60   static void Terminate();
61 
62   static void DebuggerInitialize(Debugger &debugger);
63 
64   static ConstString GetPluginNameStatic();
65 
66   static const char *GetPluginDescriptionStatic();
67 
68   static SymbolFile *CreateInstance(ObjectFile *obj_file);
69 
70   //------------------------------------------------------------------
71   // Constructors and Destructors
72   //------------------------------------------------------------------
73   SymbolFileNativePDB(ObjectFile *ofile);
74 
75   ~SymbolFileNativePDB() override;
76 
77   uint32_t CalculateAbilities() override;
78 
79   void InitializeObject() override;
80 
81   //------------------------------------------------------------------
82   // Compile Unit function calls
83   //------------------------------------------------------------------
84 
85   uint32_t GetNumCompileUnits() override;
86 
87   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
88 
89   lldb::LanguageType ParseCompileUnitLanguage(const SymbolContext &sc) override;
90 
91   size_t ParseCompileUnitFunctions(const SymbolContext &sc) override;
92 
93   bool ParseCompileUnitLineTable(const SymbolContext &sc) override;
94 
95   bool ParseCompileUnitDebugMacros(const SymbolContext &sc) override;
96 
97   bool ParseCompileUnitSupportFiles(const SymbolContext &sc,
98                                     FileSpecList &support_files) override;
99 
100   bool
101   ParseImportedModules(const SymbolContext &sc,
102                        std::vector<ConstString> &imported_modules) override;
103 
104   size_t ParseFunctionBlocks(const SymbolContext &sc) override;
105 
106   uint32_t FindGlobalVariables(const ConstString &name,
107                                const CompilerDeclContext *parent_decl_ctx,
108                                uint32_t max_matches,
109                                VariableList &variables) override;
110 
111   size_t ParseTypes(const SymbolContext &sc) override;
112   size_t ParseVariablesForContext(const SymbolContext &sc) override {
113     return 0;
114   }
115   Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
116   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
117       lldb::user_id_t type_uid,
118       const lldb_private::ExecutionContext *exe_ctx) override;
119 
120   bool CompleteType(CompilerType &compiler_type) override;
121   uint32_t ResolveSymbolContext(const Address &so_addr,
122                                 lldb::SymbolContextItem resolve_scope,
123                                 SymbolContext &sc) override;
124 
125   size_t GetTypes(SymbolContextScope *sc_scope, lldb::TypeClass type_mask,
126                   TypeList &type_list) override;
127 
128   uint32_t FindFunctions(const ConstString &name,
129                          const CompilerDeclContext *parent_decl_ctx,
130                          lldb::FunctionNameType name_type_mask,
131                          bool include_inlines, bool append,
132                          SymbolContextList &sc_list) override;
133 
134   uint32_t FindFunctions(const RegularExpression &regex, bool include_inlines,
135                          bool append, SymbolContextList &sc_list) override;
136 
137   uint32_t FindTypes(const SymbolContext &sc, const ConstString &name,
138                      const CompilerDeclContext *parent_decl_ctx, bool append,
139                      uint32_t max_matches,
140                      llvm::DenseSet<SymbolFile *> &searched_symbol_files,
141                      TypeMap &types) override;
142 
143   size_t FindTypes(const std::vector<CompilerContext> &context, bool append,
144                    TypeMap &types) override;
145 
146   TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language) override;
147 
148   CompilerDeclContext
149   FindNamespace(const SymbolContext &sc, const ConstString &name,
150                 const CompilerDeclContext *parent_decl_ctx) override;
151 
152   ConstString GetPluginName() override;
153 
154   uint32_t GetPluginVersion() override;
155 
156   llvm::pdb::PDBFile &GetPDBFile() { return m_index->pdb(); }
157   const llvm::pdb::PDBFile &GetPDBFile() const { return m_index->pdb(); }
158 
159   ClangASTContext &GetASTContext() { return *m_clang; }
160   ClangASTImporter &GetASTImporter() { return *m_importer; }
161 
162   void DumpClangAST(Stream &s) override;
163 
164 private:
165   std::pair<clang::DeclContext *, std::string>
166   CreateDeclInfoForType(const llvm::codeview::TagRecord &record,
167                         llvm::codeview::TypeIndex ti);
168 
169   void PreprocessTpiStream();
170   size_t FindTypesByName(llvm::StringRef name, uint32_t max_matches,
171                          TypeMap &types);
172 
173   lldb::TypeSP CreateModifierType(PdbSymUid type_uid,
174                                   const llvm::codeview::ModifierRecord &mr);
175   lldb::TypeSP CreatePointerType(PdbSymUid type_uid,
176                                  const llvm::codeview::PointerRecord &pr);
177   lldb::TypeSP CreateSimpleType(llvm::codeview::TypeIndex ti);
178   lldb::TypeSP CreateTagType(PdbSymUid type_uid,
179                              const llvm::codeview::ClassRecord &cr);
180   lldb::TypeSP CreateTagType(PdbSymUid type_uid,
181                              const llvm::codeview::EnumRecord &er);
182   lldb::TypeSP CreateTagType(PdbSymUid type_uid,
183                              const llvm::codeview::UnionRecord &ur);
184   lldb::TypeSP CreateArrayType(PdbSymUid type_uid,
185                                const llvm::codeview::ArrayRecord &ar);
186   lldb::TypeSP CreateProcedureType(PdbSymUid type_uid,
187                                    const llvm::codeview::ProcedureRecord &pr);
188   lldb::TypeSP CreateClassStructUnion(
189       PdbSymUid type_uid, const llvm::codeview::TagRecord &record, size_t size,
190       clang::TagTypeKind ttk, clang::MSInheritanceAttr::Spelling inheritance);
191 
192   lldb::FunctionSP GetOrCreateFunction(PdbSymUid func_uid,
193                                        const SymbolContext &sc);
194   lldb::CompUnitSP GetOrCreateCompileUnit(const CompilandIndexItem &cci);
195   lldb::TypeSP GetOrCreateType(PdbSymUid type_uid);
196   lldb::TypeSP GetOrCreateType(llvm::codeview::TypeIndex ti);
197   lldb::VariableSP GetOrCreateGlobalVariable(PdbSymUid var_uid);
198 
199   lldb::FunctionSP CreateFunction(PdbSymUid func_uid, const SymbolContext &sc);
200   lldb::CompUnitSP CreateCompileUnit(const CompilandIndexItem &cci);
201   lldb::TypeSP CreateType(PdbSymUid type_uid);
202   lldb::TypeSP CreateAndCacheType(PdbSymUid type_uid);
203   lldb::VariableSP CreateGlobalVariable(PdbSymUid var_uid);
204 
205   llvm::BumpPtrAllocator m_allocator;
206 
207   lldb::addr_t m_obj_load_address = 0;
208 
209   std::unique_ptr<PdbIndex> m_index;
210   std::unique_ptr<ClangASTImporter> m_importer;
211   ClangASTContext *m_clang = nullptr;
212 
213   llvm::DenseMap<clang::TagDecl *, DeclStatus> m_decl_to_status;
214 
215   llvm::DenseMap<lldb::user_id_t, clang::TagDecl *> m_uid_to_decl;
216   llvm::DenseMap<llvm::codeview::TypeIndex, llvm::codeview::TypeIndex>
217       m_parent_types;
218 
219   llvm::DenseMap<lldb::user_id_t, lldb::VariableSP> m_global_vars;
220   llvm::DenseMap<lldb::user_id_t, lldb::FunctionSP> m_functions;
221   llvm::DenseMap<lldb::user_id_t, lldb::CompUnitSP> m_compilands;
222   llvm::DenseMap<lldb::user_id_t, lldb::TypeSP> m_types;
223 };
224 
225 } // namespace npdb
226 } // namespace lldb_private
227 
228 #endif // lldb_Plugins_SymbolFile_PDB_SymbolFilePDB_h_
229