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)                             \
52   static const PDB_SymType Tag = TagValue;                                     \
53   static bool classof(const PDBSymbol *S) { return S->getSymTag() == Tag; }
54 
55 /// PDBSymbol defines the base of the inheritance hierarchy for concrete symbol
56 /// types (e.g. functions, executables, vtables, etc).  All concrete symbol
57 /// types inherit from PDBSymbol and expose the exact set of methods that are
58 /// valid for that particular symbol type, as described in the Microsoft
59 /// reference "Lexical and Class Hierarchy of Symbol Types":
60 /// https://msdn.microsoft.com/en-us/library/370hs6k4.aspx
61 class PDBSymbol {
62 protected:
63   PDBSymbol(const IPDBSession &PDBSession,
64             std::unique_ptr<IPDBRawSymbol> Symbol);
65   PDBSymbol(PDBSymbol &Symbol);
66 
67 public:
68   static std::unique_ptr<PDBSymbol>
69   create(const IPDBSession &PDBSession, std::unique_ptr<IPDBRawSymbol> Symbol);
70 
71   virtual ~PDBSymbol();
72 
73   /// Dumps the contents of a symbol a raw_ostream.  By default this will just
74   /// call dump() on the underlying RawSymbol, which allows us to discover
75   /// unknown properties, but individual implementations of PDBSymbol may
76   /// override the behavior to only dump known fields.
77   virtual void dump(PDBSymDumper &Dumper) const = 0;
78 
79   /// For certain PDBSymbolTypes, dumps additional information for the type that
80   /// normally goes on the right side of the symbol.
dumpRight(PDBSymDumper & Dumper)81   virtual void dumpRight(PDBSymDumper &Dumper) const {}
82 
83   void defaultDump(raw_ostream &OS, int Indent) const;
84   void dumpProperties() const;
85   void dumpChildStats() const;
86 
87   PDB_SymType getSymTag() const;
88   uint32_t getSymIndexId() const;
89 
findOneChild()90   template <typename T> std::unique_ptr<T> findOneChild() const {
91     auto Enumerator(findAllChildren<T>());
92     if (!Enumerator)
93       return nullptr;
94     return Enumerator->getNext();
95   }
96 
97   std::unique_ptr<PDBSymbol> clone() const;
98 
99   template <typename T>
findAllChildren()100   std::unique_ptr<ConcreteSymbolEnumerator<T>> findAllChildren() const {
101     auto BaseIter = RawSymbol->findChildren(T::Tag);
102     if (!BaseIter)
103       return nullptr;
104     return llvm::make_unique<ConcreteSymbolEnumerator<T>>(std::move(BaseIter));
105   }
106   std::unique_ptr<IPDBEnumSymbols> findAllChildren(PDB_SymType Type) const;
107   std::unique_ptr<IPDBEnumSymbols> findAllChildren() const;
108 
109   std::unique_ptr<IPDBEnumSymbols>
110   findChildren(PDB_SymType Type, StringRef Name,
111                PDB_NameSearchFlags Flags) const;
112   std::unique_ptr<IPDBEnumSymbols> findChildrenByRVA(PDB_SymType Type,
113                                                      StringRef Name,
114                                                      PDB_NameSearchFlags Flags,
115                                                      uint32_t RVA) const;
116   std::unique_ptr<IPDBEnumSymbols> findInlineFramesByRVA(uint32_t RVA) const;
117 
getRawSymbol()118   const IPDBRawSymbol &getRawSymbol() const { return *RawSymbol; }
getRawSymbol()119   IPDBRawSymbol &getRawSymbol() { return *RawSymbol; }
120 
getSession()121   const IPDBSession &getSession() const { return Session; }
122 
123   std::unique_ptr<IPDBEnumSymbols> getChildStats(TagStats &Stats) const;
124 
125 protected:
126   std::unique_ptr<PDBSymbol> getSymbolByIdHelper(uint32_t Id) const;
127 
128   template <typename ConcreteType>
getConcreteSymbolByIdHelper(uint32_t Id)129   std::unique_ptr<ConcreteType> getConcreteSymbolByIdHelper(uint32_t Id) const {
130     return unique_dyn_cast_or_null<ConcreteType>(getSymbolByIdHelper(Id));
131   }
132 
133   const IPDBSession &Session;
134   std::unique_ptr<IPDBRawSymbol> RawSymbol;
135 };
136 
137 } // namespace llvm
138 }
139 
140 #endif
141