DebugInfo: Delete subclasses of DIScope
[oota-llvm.git] / include / llvm / IR / DebugInfo.h
1 //===- DebugInfo.h - Debug Information Helpers ------------------*- 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 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
19
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <iterator>
30
31 namespace llvm {
32 class BasicBlock;
33 class Constant;
34 class Function;
35 class GlobalVariable;
36 class Module;
37 class Type;
38 class Value;
39 class DbgDeclareInst;
40 class DbgValueInst;
41 class Instruction;
42 class Metadata;
43 class MDNode;
44 class MDString;
45 class NamedMDNode;
46 class LLVMContext;
47 class raw_ostream;
48
49 class DIVariable;
50 class DIObjCProperty;
51
52 /// \brief Maps from type identifier to the actual MDNode.
53 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
54
55 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
56   class DESC;                                                                  \
57   template <> struct simplify_type<const DESC>;                                \
58   template <> struct simplify_type<DESC>;
59 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
60 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
61 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
62 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
63 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
64 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
65 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
66 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
67 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
68 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
69 #undef DECLARE_SIMPLIFY_DESCRIPTOR
70
71 typedef DebugNodeArray DIArray;
72 typedef MDTypeRefArray DITypeArray;
73
74 class DISubrange {
75   MDSubrange *N;
76
77 public:
78   DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {}
79
80   operator MDSubrange *() const { return N; }
81   MDSubrange *operator->() const { return N; }
82   MDSubrange &operator*() const { return *N; }
83 };
84
85 class DIEnumerator {
86   MDEnumerator *N;
87
88 public:
89   DIEnumerator(const MDEnumerator *N = nullptr)
90       : N(const_cast<MDEnumerator *>(N)) {}
91
92   operator MDEnumerator *() const { return N; }
93   MDEnumerator *operator->() const { return N; }
94   MDEnumerator &operator*() const { return *N; }
95 };
96
97 class DITemplateTypeParameter {
98   MDTemplateTypeParameter *N;
99
100 public:
101   DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
102       : N(const_cast<MDTemplateTypeParameter *>(N)) {}
103
104   operator MDTemplateTypeParameter *() const { return N; }
105   MDTemplateTypeParameter *operator->() const { return N; }
106   MDTemplateTypeParameter &operator*() const { return *N; }
107 };
108
109 class DITemplateValueParameter {
110   MDTemplateValueParameter *N;
111
112 public:
113   DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
114       : N(const_cast<MDTemplateValueParameter *>(N)) {}
115
116   operator MDTemplateValueParameter *() const { return N; }
117   MDTemplateValueParameter *operator->() const { return N; }
118   MDTemplateValueParameter &operator*() const { return *N; }
119 };
120
121 class DIGlobalVariable {
122   MDGlobalVariable *N;
123
124 public:
125   DIGlobalVariable(const MDGlobalVariable *N = nullptr)
126       : N(const_cast<MDGlobalVariable *>(N)) {}
127
128   operator MDGlobalVariable *() const { return N; }
129   MDGlobalVariable *operator->() const { return N; }
130   MDGlobalVariable &operator*() const { return *N; }
131 };
132
133 class DIVariable {
134   MDLocalVariable *N;
135
136 public:
137   DIVariable(const MDLocalVariable *N = nullptr)
138       : N(const_cast<MDLocalVariable *>(N)) {}
139
140   operator MDLocalVariable *() const { return N; }
141   MDLocalVariable *operator->() const { return N; }
142   MDLocalVariable &operator*() const { return *N; }
143 };
144
145 class DIExpression {
146   MDExpression *N;
147
148 public:
149   DIExpression(const MDExpression *N = nullptr)
150       : N(const_cast<MDExpression *>(N)) {}
151
152   operator MDExpression *() const { return N; }
153   MDExpression *operator->() const { return N; }
154   MDExpression &operator*() const { return *N; }
155 };
156
157 class DILocation {
158   MDLocation *N;
159
160 public:
161   DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
162
163   operator MDLocation *() const { return N; }
164   MDLocation *operator->() const { return N; }
165   MDLocation &operator*() const { return *N; }
166 };
167
168 class DIObjCProperty {
169   MDObjCProperty *N;
170
171 public:
172   DIObjCProperty(const MDObjCProperty *N = nullptr)
173       : N(const_cast<MDObjCProperty *>(N)) {}
174
175   operator MDObjCProperty *() const { return N; }
176   MDObjCProperty *operator->() const { return N; }
177   MDObjCProperty &operator*() const { return *N; }
178 };
179
180 class DIImportedEntity {
181   MDImportedEntity *N;
182
183 public:
184   DIImportedEntity(const MDImportedEntity *N = nullptr)
185       : N(const_cast<MDImportedEntity *>(N)) {}
186
187   operator MDImportedEntity *() const { return N; }
188   MDImportedEntity *operator->() const { return N; }
189   MDImportedEntity &operator*() const { return *N; }
190 };
191
192 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
193   template <> struct simplify_type<const DESC> {                               \
194     typedef Metadata *SimpleType;                                              \
195     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
196   };                                                                           \
197   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
198 SIMPLIFY_DESCRIPTOR(DISubrange)
199 SIMPLIFY_DESCRIPTOR(DIEnumerator)
200 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
201 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
202 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
203 SIMPLIFY_DESCRIPTOR(DIVariable)
204 SIMPLIFY_DESCRIPTOR(DIExpression)
205 SIMPLIFY_DESCRIPTOR(DILocation)
206 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
207 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
208 #undef SIMPLIFY_DESCRIPTOR
209
210 /// \brief Find subprogram that is enclosing this scope.
211 MDSubprogram *getDISubprogram(const MDNode *Scope);
212
213 /// \brief Find debug info for a given function.
214 ///
215 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
216 MDSubprogram *getDISubprogram(const Function *F);
217
218 /// \brief Find underlying composite type.
219 MDCompositeTypeBase *getDICompositeType(MDType *T);
220
221 /// \brief Generate map by visiting all retained types.
222 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
223
224 /// \brief Strip debug info in the module if it exists.
225 ///
226 /// To do this, we remove all calls to the debugger intrinsics and any named
227 /// metadata for debugging. We also remove debug locations for instructions.
228 /// Return true if module is modified.
229 bool StripDebugInfo(Module &M);
230 bool stripDebugInfo(Function &F);
231
232 /// \brief Return Debug Info Metadata Version by checking module flags.
233 unsigned getDebugMetadataVersionFromModule(const Module &M);
234
235 /// \brief Utility to find all debug info in a module.
236 ///
237 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
238 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
239 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
240 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
241 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
242 /// used by the CUs.
243 class DebugInfoFinder {
244 public:
245   DebugInfoFinder() : TypeMapInitialized(false) {}
246
247   /// \brief Process entire module and collect debug info anchors.
248   void processModule(const Module &M);
249
250   /// \brief Process DbgDeclareInst.
251   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
252   /// \brief Process DbgValueInst.
253   void processValue(const Module &M, const DbgValueInst *DVI);
254   /// \brief Process DILocation.
255   void processLocation(const Module &M, const MDLocation *Loc);
256
257   /// \brief Clear all lists.
258   void reset();
259
260 private:
261   void InitializeTypeMap(const Module &M);
262
263   void processType(MDType *DT);
264   void processSubprogram(MDSubprogram *SP);
265   void processScope(MDScope *Scope);
266   bool addCompileUnit(MDCompileUnit *CU);
267   bool addGlobalVariable(MDGlobalVariable *DIG);
268   bool addSubprogram(MDSubprogram *SP);
269   bool addType(MDType *DT);
270   bool addScope(MDScope *Scope);
271
272 public:
273   typedef SmallVectorImpl<MDCompileUnit *>::const_iterator
274       compile_unit_iterator;
275   typedef SmallVectorImpl<MDSubprogram *>::const_iterator subprogram_iterator;
276   typedef SmallVectorImpl<MDGlobalVariable *>::const_iterator
277       global_variable_iterator;
278   typedef SmallVectorImpl<MDType *>::const_iterator type_iterator;
279   typedef SmallVectorImpl<MDScope *>::const_iterator scope_iterator;
280
281   iterator_range<compile_unit_iterator> compile_units() const {
282     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
283   }
284
285   iterator_range<subprogram_iterator> subprograms() const {
286     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
287   }
288
289   iterator_range<global_variable_iterator> global_variables() const {
290     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
291   }
292
293   iterator_range<type_iterator> types() const {
294     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
295   }
296
297   iterator_range<scope_iterator> scopes() const {
298     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
299   }
300
301   unsigned compile_unit_count() const { return CUs.size(); }
302   unsigned global_variable_count() const { return GVs.size(); }
303   unsigned subprogram_count() const { return SPs.size(); }
304   unsigned type_count() const { return TYs.size(); }
305   unsigned scope_count() const { return Scopes.size(); }
306
307 private:
308   SmallVector<MDCompileUnit *, 8> CUs;
309   SmallVector<MDSubprogram *, 8> SPs;
310   SmallVector<MDGlobalVariable *, 8> GVs;
311   SmallVector<MDType *, 8> TYs;
312   SmallVector<MDScope *, 8> Scopes;
313   SmallPtrSet<const MDNode *, 64> NodesSeen;
314   DITypeIdentifierMap TypeIdentifierMap;
315
316   /// \brief Specify if TypeIdentifierMap is initialized.
317   bool TypeMapInitialized;
318 };
319
320 DenseMap<const Function *, MDSubprogram *> makeSubprogramMap(const Module &M);
321
322 } // end namespace llvm
323
324 #endif