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