1 //===-- PdbAstBuilder.h -----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_PLUGINS_SYMBOLFILE_NATIVEPDB_PDBASTBUILDER_H 10 #define LLDB_PLUGINS_SYMBOLFILE_NATIVEPDB_PDBASTBUILDER_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/StringRef.h" 14 15 #include "lldb/Symbol/ClangASTImporter.h" 16 17 #include "PdbIndex.h" 18 #include "PdbSymUid.h" 19 20 namespace clang { 21 class TagDecl; 22 class DeclContext; 23 class Decl; 24 class QualType; 25 class FunctionDecl; 26 class NamespaceDecl; 27 } // namespace clang 28 29 namespace llvm { 30 namespace codeview { 31 class ProcSym; 32 } 33 } // namespace llvm 34 35 namespace lldb_private { 36 class ClangASTImporter; 37 class ObjectFile; 38 39 namespace npdb { 40 class PdbIndex; 41 struct VariableInfo; 42 43 struct DeclStatus { 44 DeclStatus() = default; 45 DeclStatus(lldb::user_id_t uid, bool resolved) 46 : uid(uid), resolved(resolved) {} 47 lldb::user_id_t uid = 0; 48 bool resolved = false; 49 }; 50 51 class PdbAstBuilder { 52 public: 53 // Constructors and Destructors 54 PdbAstBuilder(ObjectFile &obj, PdbIndex &index); 55 56 lldb_private::CompilerDeclContext GetTranslationUnitDecl(); 57 58 clang::Decl *GetOrCreateDeclForUid(PdbSymUid uid); 59 clang::DeclContext *GetOrCreateDeclContextForUid(PdbSymUid uid); 60 clang::DeclContext *GetParentDeclContext(PdbSymUid uid); 61 62 clang::FunctionDecl *GetOrCreateFunctionDecl(PdbCompilandSymId func_id); 63 clang::BlockDecl *GetOrCreateBlockDecl(PdbCompilandSymId block_id); 64 clang::VarDecl *GetOrCreateVariableDecl(PdbCompilandSymId scope_id, 65 PdbCompilandSymId var_id); 66 clang::VarDecl *GetOrCreateVariableDecl(PdbGlobalSymId var_id); 67 clang::TypedefNameDecl *GetOrCreateTypedefDecl(PdbGlobalSymId id); 68 void ParseDeclsForContext(clang::DeclContext &context); 69 70 clang::QualType GetBasicType(lldb::BasicType type); 71 clang::QualType GetOrCreateType(PdbTypeSymId type); 72 73 bool CompleteTagDecl(clang::TagDecl &tag); 74 bool CompleteType(clang::QualType qt); 75 76 CompilerDecl ToCompilerDecl(clang::Decl &decl); 77 CompilerType ToCompilerType(clang::QualType qt); 78 CompilerDeclContext ToCompilerDeclContext(clang::DeclContext &context); 79 clang::Decl * FromCompilerDecl(CompilerDecl decl); 80 clang::DeclContext *FromCompilerDeclContext(CompilerDeclContext context); 81 82 ClangASTContext &clang() { return m_clang; } 83 ClangASTImporter &importer() { return m_importer; } 84 85 void Dump(Stream &stream); 86 87 private: 88 clang::Decl *TryGetDecl(PdbSymUid uid) const; 89 90 using TypeIndex = llvm::codeview::TypeIndex; 91 92 clang::QualType 93 CreatePointerType(const llvm::codeview::PointerRecord &pointer); 94 clang::QualType 95 CreateModifierType(const llvm::codeview::ModifierRecord &modifier); 96 clang::QualType CreateArrayType(const llvm::codeview::ArrayRecord &array); 97 clang::QualType CreateRecordType(PdbTypeSymId id, 98 const llvm::codeview::TagRecord &record); 99 clang::QualType CreateEnumType(PdbTypeSymId id, 100 const llvm::codeview::EnumRecord &record); 101 clang::QualType 102 CreateFunctionType(TypeIndex args_type_idx, TypeIndex return_type_idx, 103 llvm::codeview::CallingConvention calling_convention); 104 clang::QualType CreateType(PdbTypeSymId type); 105 106 void CreateFunctionParameters(PdbCompilandSymId func_id, 107 clang::FunctionDecl &function_decl, 108 uint32_t param_count); 109 clang::Decl *GetOrCreateSymbolForId(PdbCompilandSymId id); 110 clang::VarDecl *CreateVariableDecl(PdbSymUid uid, 111 llvm::codeview::CVSymbol sym, 112 clang::DeclContext &scope); 113 clang::DeclContext * 114 GetParentDeclContextForSymbol(const llvm::codeview::CVSymbol &sym); 115 116 clang::NamespaceDecl *GetOrCreateNamespaceDecl(const char *name, 117 clang::DeclContext &context); 118 119 void ParseAllNamespacesPlusChildrenOf(llvm::Optional<llvm::StringRef> parent); 120 void ParseDeclsForSimpleContext(clang::DeclContext &context); 121 void ParseBlockChildren(PdbCompilandSymId block_id); 122 123 void BuildParentMap(); 124 std::pair<clang::DeclContext *, std::string> 125 CreateDeclInfoForType(const llvm::codeview::TagRecord &record, TypeIndex ti); 126 std::pair<clang::DeclContext *, std::string> 127 CreateDeclInfoForUndecoratedName(llvm::StringRef uname); 128 clang::QualType CreateSimpleType(TypeIndex ti); 129 130 PdbIndex &m_index; 131 ClangASTContext &m_clang; 132 133 ClangASTImporter m_importer; 134 135 llvm::DenseMap<TypeIndex, TypeIndex> m_parent_types; 136 llvm::DenseMap<clang::Decl *, DeclStatus> m_decl_to_status; 137 llvm::DenseMap<lldb::user_id_t, clang::Decl *> m_uid_to_decl; 138 llvm::DenseMap<lldb::user_id_t, clang::QualType> m_uid_to_type; 139 }; 140 141 } // namespace npdb 142 } // namespace lldb_private 143 144 #endif // lldb_Plugins_SymbolFile_PDB_SymbolFilePDB_h_ 145