Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- PDBSymbol.h - base class for user-facing symbol types -----*- 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 LLVM_DEBUGINFO_PDB_PDBSYMBOL_H |
| 11 | #define LLVM_DEBUGINFO_PDB_PDBSYMBOL_H |
| 12 | |
| 13 | #include "ConcreteSymbolEnumerator.h" |
| 14 | #include "IPDBRawSymbol.h" |
| 15 | #include "PDBExtras.h" |
| 16 | #include "PDBTypes.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/Support/Casting.h" |
| 19 | |
| 20 | #define FORWARD_SYMBOL_METHOD(MethodName) \ |
| 21 | auto MethodName() const->decltype(RawSymbol->MethodName()) { \ |
| 22 | return RawSymbol->MethodName(); \ |
| 23 | } |
| 24 | |
| 25 | #define FORWARD_CONCRETE_SYMBOL_ID_METHOD_WITH_NAME(ConcreteType, PrivateName, \ |
| 26 | PublicName) \ |
| 27 | auto PublicName##Id() const->decltype(RawSymbol->PrivateName##Id()) { \ |
| 28 | return RawSymbol->PrivateName##Id(); \ |
| 29 | } \ |
| 30 | std::unique_ptr<ConcreteType> PublicName() const { \ |
| 31 | uint32_t Id = PublicName##Id(); \ |
| 32 | return getConcreteSymbolByIdHelper<ConcreteType>(Id); \ |
| 33 | } |
| 34 | |
| 35 | #define FORWARD_SYMBOL_ID_METHOD_WITH_NAME(PrivateName, PublicName) \ |
| 36 | FORWARD_CONCRETE_SYMBOL_ID_METHOD_WITH_NAME(PDBSymbol, PrivateName, \ |
| 37 | PublicName) |
| 38 | |
| 39 | #define FORWARD_SYMBOL_ID_METHOD(MethodName) \ |
| 40 | FORWARD_SYMBOL_ID_METHOD_WITH_NAME(MethodName, MethodName) |
| 41 | |
| 42 | namespace llvm { |
| 43 | |
| 44 | class StringRef; |
| 45 | class raw_ostream; |
| 46 | |
| 47 | namespace pdb { |
| 48 | class IPDBRawSymbol; |
| 49 | class IPDBSession; |
| 50 | |
| 51 | #define DECLARE_PDB_SYMBOL_CONCRETE_TYPE(TagValue) \ |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 52 | private: \ |
| 53 | using PDBSymbol::PDBSymbol; \ |
| 54 | friend class PDBSymbol; \ |
| 55 | \ |
| 56 | public: \ |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | static const PDB_SymType Tag = TagValue; \ |
| 58 | static bool classof(const PDBSymbol *S) { return S->getSymTag() == Tag; } |
| 59 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 60 | #define DECLARE_PDB_SYMBOL_CUSTOM_TYPE(Condition) \ |
| 61 | private: \ |
| 62 | using PDBSymbol::PDBSymbol; \ |
| 63 | friend class PDBSymbol; \ |
| 64 | \ |
| 65 | public: \ |
| 66 | static bool classof(const PDBSymbol *S) { return Condition; } |
| 67 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | /// PDBSymbol defines the base of the inheritance hierarchy for concrete symbol |
| 69 | /// types (e.g. functions, executables, vtables, etc). All concrete symbol |
| 70 | /// types inherit from PDBSymbol and expose the exact set of methods that are |
| 71 | /// valid for that particular symbol type, as described in the Microsoft |
| 72 | /// reference "Lexical and Class Hierarchy of Symbol Types": |
| 73 | /// https://msdn.microsoft.com/en-us/library/370hs6k4.aspx |
| 74 | class PDBSymbol { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 75 | static std::unique_ptr<PDBSymbol> createSymbol(const IPDBSession &PDBSession, |
| 76 | PDB_SymType Tag); |
| 77 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | protected: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 79 | explicit PDBSymbol(const IPDBSession &PDBSession); |
| 80 | PDBSymbol(PDBSymbol &&Other); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | |
| 82 | public: |
| 83 | static std::unique_ptr<PDBSymbol> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 84 | create(const IPDBSession &PDBSession, |
| 85 | std::unique_ptr<IPDBRawSymbol> RawSymbol); |
| 86 | static std::unique_ptr<PDBSymbol> create(const IPDBSession &PDBSession, |
| 87 | IPDBRawSymbol &RawSymbol); |
| 88 | |
| 89 | template <typename ConcreteT> |
| 90 | static std::unique_ptr<ConcreteT> |
| 91 | createAs(const IPDBSession &PDBSession, |
| 92 | std::unique_ptr<IPDBRawSymbol> RawSymbol) { |
| 93 | std::unique_ptr<PDBSymbol> S = create(PDBSession, std::move(RawSymbol)); |
| 94 | return unique_dyn_cast_or_null<ConcreteT>(std::move(S)); |
| 95 | } |
| 96 | template <typename ConcreteT> |
| 97 | static std::unique_ptr<ConcreteT> createAs(const IPDBSession &PDBSession, |
| 98 | IPDBRawSymbol &RawSymbol) { |
| 99 | std::unique_ptr<PDBSymbol> S = create(PDBSession, RawSymbol); |
| 100 | return unique_dyn_cast_or_null<ConcreteT>(std::move(S)); |
| 101 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | |
| 103 | virtual ~PDBSymbol(); |
| 104 | |
| 105 | /// Dumps the contents of a symbol a raw_ostream. By default this will just |
| 106 | /// call dump() on the underlying RawSymbol, which allows us to discover |
| 107 | /// unknown properties, but individual implementations of PDBSymbol may |
| 108 | /// override the behavior to only dump known fields. |
| 109 | virtual void dump(PDBSymDumper &Dumper) const = 0; |
| 110 | |
| 111 | /// For certain PDBSymbolTypes, dumps additional information for the type that |
| 112 | /// normally goes on the right side of the symbol. |
| 113 | virtual void dumpRight(PDBSymDumper &Dumper) const {} |
| 114 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 115 | void defaultDump(raw_ostream &OS, int Indent, PdbSymbolIdField ShowFlags, |
| 116 | PdbSymbolIdField RecurseFlags) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | void dumpProperties() const; |
| 118 | void dumpChildStats() const; |
| 119 | |
| 120 | PDB_SymType getSymTag() const; |
| 121 | uint32_t getSymIndexId() const; |
| 122 | |
| 123 | template <typename T> std::unique_ptr<T> findOneChild() const { |
| 124 | auto Enumerator(findAllChildren<T>()); |
| 125 | if (!Enumerator) |
| 126 | return nullptr; |
| 127 | return Enumerator->getNext(); |
| 128 | } |
| 129 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 130 | template <typename T> |
| 131 | std::unique_ptr<ConcreteSymbolEnumerator<T>> findAllChildren() const { |
| 132 | auto BaseIter = RawSymbol->findChildren(T::Tag); |
| 133 | if (!BaseIter) |
| 134 | return nullptr; |
| 135 | return llvm::make_unique<ConcreteSymbolEnumerator<T>>(std::move(BaseIter)); |
| 136 | } |
| 137 | std::unique_ptr<IPDBEnumSymbols> findAllChildren(PDB_SymType Type) const; |
| 138 | std::unique_ptr<IPDBEnumSymbols> findAllChildren() const; |
| 139 | |
| 140 | std::unique_ptr<IPDBEnumSymbols> |
| 141 | findChildren(PDB_SymType Type, StringRef Name, |
| 142 | PDB_NameSearchFlags Flags) const; |
| 143 | std::unique_ptr<IPDBEnumSymbols> findChildrenByRVA(PDB_SymType Type, |
| 144 | StringRef Name, |
| 145 | PDB_NameSearchFlags Flags, |
| 146 | uint32_t RVA) const; |
| 147 | std::unique_ptr<IPDBEnumSymbols> findInlineFramesByRVA(uint32_t RVA) const; |
| 148 | |
| 149 | const IPDBRawSymbol &getRawSymbol() const { return *RawSymbol; } |
| 150 | IPDBRawSymbol &getRawSymbol() { return *RawSymbol; } |
| 151 | |
| 152 | const IPDBSession &getSession() const { return Session; } |
| 153 | |
| 154 | std::unique_ptr<IPDBEnumSymbols> getChildStats(TagStats &Stats) const; |
| 155 | |
| 156 | protected: |
| 157 | std::unique_ptr<PDBSymbol> getSymbolByIdHelper(uint32_t Id) const; |
| 158 | |
| 159 | template <typename ConcreteType> |
| 160 | std::unique_ptr<ConcreteType> getConcreteSymbolByIdHelper(uint32_t Id) const { |
| 161 | return unique_dyn_cast_or_null<ConcreteType>(getSymbolByIdHelper(Id)); |
| 162 | } |
| 163 | |
| 164 | const IPDBSession &Session; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 165 | std::unique_ptr<IPDBRawSymbol> OwnedRawSymbol; |
| 166 | IPDBRawSymbol *RawSymbol = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace llvm |
| 170 | } |
| 171 | |
| 172 | #endif |