DebugInfo: Remove unnecessary API from DIDerivedType and DIType
[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 DIFile;
50 class DISubprogram;
51 class DILexicalBlock;
52 class DILexicalBlockFile;
53 class DIVariable;
54 class DIType;
55 class DIScope;
56 class DIObjCProperty;
57
58 /// \brief Maps from type identifier to the actual MDNode.
59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
60
61 /// \brief A thin wraper around MDNode to access encoded debug info.
62 ///
63 /// This should not be stored in a container, because the underlying MDNode may
64 /// change in certain situations.
65 class DIDescriptor {
66 public:
67   /// \brief Duplicated debug info flags.
68   ///
69   /// \see DebugNode::DIFlags.
70   enum {
71 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
72 #include "llvm/IR/DebugInfoFlags.def"
73     FlagAccessibility = DebugNode::FlagAccessibility
74   };
75
76 protected:
77   const MDNode *DbgNode;
78
79 public:
80   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
81   DIDescriptor(const DebugNode *N) : DbgNode(N) {}
82
83   MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
84   operator MDNode *() const { return get(); }
85   MDNode *operator->() const { return get(); }
86   MDNode &operator*() const { return *get(); }
87
88   // An explicit operator bool so that we can do testing of DI values
89   // easily.
90   // FIXME: This operator bool isn't actually protecting anything at the
91   // moment due to the conversion operator above making DIDescriptor nodes
92   // implicitly convertable to bool.
93   explicit operator bool() const { return DbgNode != nullptr; }
94
95   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
96   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
97
98   uint16_t getTag() const {
99     if (auto *N = dyn_cast_or_null<DebugNode>(get()))
100       return N->getTag();
101     return 0;
102   }
103
104   void print(raw_ostream &OS) const;
105   void dump() const;
106 };
107
108 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
109   class DESC;                                                                  \
110   template <> struct simplify_type<const DESC>;                                \
111   template <> struct simplify_type<DESC>;
112 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
113 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
114 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
115 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
116 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
117 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
118 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
119 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
120 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
121 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
122 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
123 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
124 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
125 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
126 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
127 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
135 #undef DECLARE_SIMPLIFY_DESCRIPTOR
136
137 typedef DebugNodeArray DIArray;
138 typedef MDTypeRefArray DITypeArray;
139
140 /// \brief This is used to represent ranges, for array bounds.
141 class DISubrange : public DIDescriptor {
142 public:
143   DISubrange() = default;
144   DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
145
146   MDSubrange *get() const {
147     return cast_or_null<MDSubrange>(DIDescriptor::get());
148   }
149   operator MDSubrange *() const { return get(); }
150   MDSubrange *operator->() const { return get(); }
151   MDSubrange &operator*() const { return *get(); }
152
153   int64_t getLo() const { return get()->getLowerBound(); }
154   int64_t getCount() const { return get()->getCount(); }
155 };
156
157 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
158 ///
159 /// FIXME: it seems strange that this doesn't have either a reference to the
160 /// type/precision or a file/line pair for location info.
161 class DIEnumerator : public DIDescriptor {
162 public:
163   DIEnumerator() = default;
164   DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
165
166   MDEnumerator *get() const {
167     return cast_or_null<MDEnumerator>(DIDescriptor::get());
168   }
169   operator MDEnumerator *() const { return get(); }
170   MDEnumerator *operator->() const { return get(); }
171   MDEnumerator &operator*() const { return *get(); }
172
173   StringRef getName() const { return get()->getName(); }
174   int64_t getEnumValue() const { return get()->getValue(); }
175 };
176
177 template <typename T> class DIRef;
178 typedef DIRef<DIDescriptor> DIDescriptorRef;
179 typedef DIRef<DIScope> DIScopeRef;
180 typedef DIRef<DIType> DITypeRef;
181
182 /// \brief A base class for various scopes.
183 ///
184 /// Although, implementation-wise, DIScope is the parent class of most
185 /// other DIxxx classes, including DIType and its descendants, most of
186 /// DIScope's descendants are not a substitutable subtype of
187 /// DIScope. The DIDescriptor::isScope() method only is true for
188 /// DIScopes that are scopes in the strict lexical scope sense
189 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
190 class DIScope : public DIDescriptor {
191 public:
192   DIScope() = default;
193   DIScope(const MDScope *N) : DIDescriptor(N) {}
194
195   MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
196   operator MDScope *() const { return get(); }
197   MDScope *operator->() const { return get(); }
198   MDScope &operator*() const { return *get(); }
199
200   inline DIScopeRef getContext() const;
201   StringRef getName() const { return get()->getName(); }
202   StringRef getFilename() const { return get()->getFilename(); }
203   StringRef getDirectory() const { return get()->getDirectory(); }
204
205   /// \brief Generate a reference to this DIScope.
206   ///
207   /// Uses the type identifier instead of the actual MDNode if possible, to
208   /// help type uniquing.
209   DIScopeRef getRef() const;
210 };
211
212 /// \brief Represents reference to a DIDescriptor.
213 ///
214 /// Abstracts over direct and identifier-based metadata references.
215 template <typename T> class DIRef {
216   /// \brief Val can be either a MDNode or a MDString.
217   ///
218   /// In the latter, MDString specifies the type identifier.
219   const Metadata *Val;
220
221 public:
222   template <class U>
223   DIRef(const TypedDebugNodeRef<U> &Ref,
224         typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
225             nullptr)
226       : Val(Ref) {}
227
228   T resolve(const DITypeIdentifierMap &Map) const;
229   operator Metadata *() const { return const_cast<Metadata *>(Val); }
230 };
231
232 template <>
233 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
234 template <>
235 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
236 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
237
238 DIScopeRef DIScope::getContext() const { return get()->getScope(); }
239
240 /// \brief This is a wrapper for a type.
241 ///
242 /// FIXME: Types should be factored much better so that CV qualifiers and
243 /// others do not require a huge and empty descriptor full of zeros.
244 class DIType : public DIScope {
245 public:
246   DIType() = default;
247   DIType(const MDType *N) : DIScope(N) {}
248
249   MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
250   operator MDType *() const { return get(); }
251   MDType *operator->() const { return get(); }
252   MDType &operator*() const { return *get(); }
253
254   DIScopeRef getContext() const { return get()->getScope(); }
255   StringRef getName() const { return get()->getName(); }
256   unsigned getLineNumber() const { return get()->getLine(); }
257   uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
258   uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
259   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
260   // carry this is just plain insane.
261   uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
262   unsigned getFlags() const { return get()->getFlags(); }
263
264   bool isPrivate() const { return get()->isPrivate(); }
265   bool isProtected() const { return get()->isProtected(); }
266   bool isPublic() const { return get()->isPublic(); }
267   bool isForwardDecl() const { return get()->isForwardDecl(); }
268   bool isAppleBlockExtension() const { return get()->isAppleBlockExtension(); }
269   bool isBlockByrefStruct() const { return get()->isBlockByrefStruct(); }
270   bool isVirtual() const { return get()->isVirtual(); }
271   bool isArtificial() const { return get()->isArtificial(); }
272   bool isObjectPointer() const { return get()->isObjectPointer(); }
273   bool isObjcClassComplete() const { return get()->isObjcClassComplete(); }
274   bool isVector() const { return get()->isVector(); }
275   bool isStaticMember() const { return get()->isStaticMember(); }
276   bool isLValueReference() const { return get()->isLValueReference(); }
277   bool isRValueReference() const { return get()->isRValueReference(); }
278 };
279
280 /// \brief A basic type, like 'int' or 'float'.
281 class DIBasicType : public DIType {
282 public:
283   DIBasicType() = default;
284   DIBasicType(const MDBasicType *N) : DIType(N) {}
285
286   MDBasicType *get() const {
287     return cast_or_null<MDBasicType>(DIDescriptor::get());
288   }
289   operator MDBasicType *() const { return get(); }
290   MDBasicType *operator->() const { return get(); }
291   MDBasicType &operator*() const { return *get(); }
292
293   unsigned getEncoding() const { return get()->getEncoding(); }
294 };
295
296 /// \brief A simple derived type
297 ///
298 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
299 /// Or, a data member of a class/struct/union.
300 class DIDerivedType : public DIType {
301 public:
302   DIDerivedType() = default;
303   DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
304
305   MDDerivedTypeBase *get() const {
306     return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
307   }
308   operator MDDerivedTypeBase *() const { return get(); }
309   MDDerivedTypeBase *operator->() const { return get(); }
310   MDDerivedTypeBase &operator*() const { return *get(); }
311
312   DITypeRef getTypeDerivedFrom() const { return get()->getBaseType(); }
313 };
314
315 /// \brief Types that refer to multiple other types.
316 ///
317 /// This descriptor holds a type that can refer to multiple other types, like a
318 /// function or struct.
319 ///
320 /// DICompositeType is derived from DIDerivedType because some
321 /// composite types (such as enums) can be derived from basic types
322 // FIXME: Make this derive from DIType directly & just store the
323 // base type in a single DIType field.
324 class DICompositeType : public DIDerivedType {
325   friend class DIBuilder;
326
327 public:
328   DICompositeType() = default;
329   DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
330
331   MDCompositeTypeBase *get() const {
332     return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
333   }
334   operator MDCompositeTypeBase *() const { return get(); }
335   MDCompositeTypeBase *operator->() const { return get(); }
336   MDCompositeTypeBase &operator*() const { return *get(); }
337
338   DIArray getElements() const { return get()->getElements(); }
339
340   unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
341   DITypeRef getContainingType() const { return get()->getVTableHolder(); }
342
343   DIArray getTemplateParams() const { return get()->getTemplateParams(); }
344   MDString *getIdentifier() const { return get()->getRawIdentifier(); }
345 };
346
347 class DISubroutineType : public DICompositeType {
348 public:
349   DISubroutineType() = default;
350   DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
351
352   MDSubroutineType *get() const {
353     return cast_or_null<MDSubroutineType>(DIDescriptor::get());
354   }
355   operator MDSubroutineType *() const { return get(); }
356   MDSubroutineType *operator->() const { return get(); }
357   MDSubroutineType &operator*() const { return *get(); }
358
359   MDTypeRefArray getTypeArray() const { return get()->getTypeArray(); }
360 };
361
362 class DIFile {
363   MDFile *N;
364
365 public:
366   DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {}
367
368   operator DIDescriptor() const { return N; }
369   operator DIScope() const { return N; }
370   operator MDFile *() const { return N; }
371   MDFile *operator->() const { return N; }
372   MDFile &operator*() const { return *N; }
373 };
374
375 class DICompileUnit {
376   MDCompileUnit *N;
377
378 public:
379   DICompileUnit(const MDCompileUnit *N = nullptr)
380       : N(const_cast<MDCompileUnit *>(N)) {}
381
382   operator DIDescriptor() const { return N; }
383   operator DIScope() const { return N; }
384   operator MDCompileUnit *() const { return N; }
385   MDCompileUnit *operator->() const { return N; }
386   MDCompileUnit &operator*() const { return *N; }
387 };
388
389 class DISubprogram {
390   MDSubprogram *N;
391
392 public:
393   DISubprogram(const MDSubprogram *N = nullptr)
394       : N(const_cast<MDSubprogram *>(N)) {}
395
396   operator DIDescriptor() const { return N; }
397   operator DIScope() const { return N; }
398   operator MDSubprogram *() const { return N; }
399   MDSubprogram *operator->() const { return N; }
400   MDSubprogram &operator*() const { return *N; }
401 };
402
403 class DILexicalBlock {
404   MDLexicalBlockBase *N;
405
406 public:
407   DILexicalBlock(const MDLexicalBlockBase *N = nullptr)
408       : N(const_cast<MDLexicalBlockBase *>(N)) {}
409
410   operator DIDescriptor() const { return N; }
411   operator MDLexicalBlockBase *() const { return N; }
412   MDLexicalBlockBase *operator->() const { return N; }
413   MDLexicalBlockBase &operator*() const { return *N; }
414 };
415
416 class DILexicalBlockFile {
417   MDLexicalBlockFile *N;
418
419 public:
420   DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr)
421       : N(const_cast<MDLexicalBlockFile *>(N)) {}
422
423   operator DIDescriptor() const { return N; }
424   operator MDLexicalBlockFile *() const { return N; }
425   MDLexicalBlockFile *operator->() const { return N; }
426   MDLexicalBlockFile &operator*() const { return *N; }
427 };
428
429 class DINameSpace {
430   MDNamespace *N;
431
432 public:
433   DINameSpace(const MDNamespace *N = nullptr)
434       : N(const_cast<MDNamespace *>(N)) {}
435
436   operator DIDescriptor() const { return N; }
437   operator DIScope() const { return N; }
438   operator MDNamespace *() const { return N; }
439   MDNamespace *operator->() const { return N; }
440   MDNamespace &operator*() const { return *N; }
441 };
442
443 class DITemplateTypeParameter {
444   MDTemplateTypeParameter *N;
445
446 public:
447   DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr)
448       : N(const_cast<MDTemplateTypeParameter *>(N)) {}
449
450   operator MDTemplateTypeParameter *() const { return N; }
451   MDTemplateTypeParameter *operator->() const { return N; }
452   MDTemplateTypeParameter &operator*() const { return *N; }
453 };
454
455 class DITemplateValueParameter {
456   MDTemplateValueParameter *N;
457
458 public:
459   DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr)
460       : N(const_cast<MDTemplateValueParameter *>(N)) {}
461
462   operator MDTemplateValueParameter *() const { return N; }
463   MDTemplateValueParameter *operator->() const { return N; }
464   MDTemplateValueParameter &operator*() const { return *N; }
465 };
466
467 class DIGlobalVariable {
468   MDGlobalVariable *N;
469
470 public:
471   DIGlobalVariable(const MDGlobalVariable *N = nullptr)
472       : N(const_cast<MDGlobalVariable *>(N)) {}
473
474   operator DIDescriptor() const { return N; }
475   operator MDGlobalVariable *() const { return N; }
476   MDGlobalVariable *operator->() const { return N; }
477   MDGlobalVariable &operator*() const { return *N; }
478 };
479
480 class DIVariable {
481   MDLocalVariable *N;
482
483 public:
484   DIVariable(const MDLocalVariable *N = nullptr)
485       : N(const_cast<MDLocalVariable *>(N)) {}
486
487   operator MDLocalVariable *() const { return N; }
488   MDLocalVariable *operator->() const { return N; }
489   MDLocalVariable &operator*() const { return *N; }
490 };
491
492 class DIExpression {
493   MDExpression *N;
494
495 public:
496   DIExpression(const MDExpression *N = nullptr)
497       : N(const_cast<MDExpression *>(N)) {}
498
499   operator MDExpression *() const { return N; }
500   MDExpression *operator->() const { return N; }
501   MDExpression &operator*() const { return *N; }
502 };
503
504 class DILocation {
505   MDLocation *N;
506
507 public:
508   DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {}
509
510   operator MDLocation *() const { return N; }
511   MDLocation *operator->() const { return N; }
512   MDLocation &operator*() const { return *N; }
513 };
514
515 class DIObjCProperty {
516   MDObjCProperty *N;
517
518 public:
519   DIObjCProperty(const MDObjCProperty *N = nullptr)
520       : N(const_cast<MDObjCProperty *>(N)) {}
521
522   operator MDObjCProperty *() const { return N; }
523   MDObjCProperty *operator->() const { return N; }
524   MDObjCProperty &operator*() const { return *N; }
525 };
526
527 class DIImportedEntity {
528   MDImportedEntity *N;
529
530 public:
531   DIImportedEntity(const MDImportedEntity *N = nullptr)
532       : N(const_cast<MDImportedEntity *>(N)) {}
533
534   operator DIDescriptor() const { return N; }
535   operator MDImportedEntity *() const { return N; }
536   MDImportedEntity *operator->() const { return N; }
537   MDImportedEntity &operator*() const { return *N; }
538 };
539
540 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
541   template <> struct simplify_type<const DESC> {                               \
542     typedef Metadata *SimpleType;                                              \
543     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
544   };                                                                           \
545   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
546 SIMPLIFY_DESCRIPTOR(DIDescriptor)
547 SIMPLIFY_DESCRIPTOR(DISubrange)
548 SIMPLIFY_DESCRIPTOR(DIEnumerator)
549 SIMPLIFY_DESCRIPTOR(DIScope)
550 SIMPLIFY_DESCRIPTOR(DIType)
551 SIMPLIFY_DESCRIPTOR(DIBasicType)
552 SIMPLIFY_DESCRIPTOR(DIDerivedType)
553 SIMPLIFY_DESCRIPTOR(DICompositeType)
554 SIMPLIFY_DESCRIPTOR(DISubroutineType)
555 SIMPLIFY_DESCRIPTOR(DIFile)
556 SIMPLIFY_DESCRIPTOR(DICompileUnit)
557 SIMPLIFY_DESCRIPTOR(DISubprogram)
558 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
559 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
560 SIMPLIFY_DESCRIPTOR(DINameSpace)
561 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
562 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
563 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
564 SIMPLIFY_DESCRIPTOR(DIVariable)
565 SIMPLIFY_DESCRIPTOR(DIExpression)
566 SIMPLIFY_DESCRIPTOR(DILocation)
567 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
568 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
569 #undef SIMPLIFY_DESCRIPTOR
570
571 /// \brief Find subprogram that is enclosing this scope.
572 DISubprogram getDISubprogram(const MDNode *Scope);
573
574 /// \brief Find debug info for a given function.
575 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
576 /// DISubprogram.
577 DISubprogram getDISubprogram(const Function *F);
578
579 /// \brief Find underlying composite type.
580 DICompositeType getDICompositeType(DIType T);
581
582 /// \brief Generate map by visiting all retained types.
583 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
584
585 /// \brief Strip debug info in the module if it exists.
586 ///
587 /// To do this, we remove all calls to the debugger intrinsics and any named
588 /// metadata for debugging. We also remove debug locations for instructions.
589 /// Return true if module is modified.
590 bool StripDebugInfo(Module &M);
591 bool stripDebugInfo(Function &F);
592
593 /// \brief Return Debug Info Metadata Version by checking module flags.
594 unsigned getDebugMetadataVersionFromModule(const Module &M);
595
596 /// \brief Utility to find all debug info in a module.
597 ///
598 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
599 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
600 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
601 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
602 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
603 /// used by the CUs.
604 class DebugInfoFinder {
605 public:
606   DebugInfoFinder() : TypeMapInitialized(false) {}
607
608   /// \brief Process entire module and collect debug info anchors.
609   void processModule(const Module &M);
610
611   /// \brief Process DbgDeclareInst.
612   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
613   /// \brief Process DbgValueInst.
614   void processValue(const Module &M, const DbgValueInst *DVI);
615   /// \brief Process DILocation.
616   void processLocation(const Module &M, DILocation Loc);
617
618   /// \brief Clear all lists.
619   void reset();
620
621 private:
622   void InitializeTypeMap(const Module &M);
623
624   void processType(DIType DT);
625   void processSubprogram(DISubprogram SP);
626   void processScope(DIScope Scope);
627   bool addCompileUnit(DICompileUnit CU);
628   bool addGlobalVariable(DIGlobalVariable DIG);
629   bool addSubprogram(DISubprogram SP);
630   bool addType(DIType DT);
631   bool addScope(DIScope Scope);
632
633 public:
634   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
635   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
636   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
637       global_variable_iterator;
638   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
639   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
640
641   iterator_range<compile_unit_iterator> compile_units() const {
642     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
643   }
644
645   iterator_range<subprogram_iterator> subprograms() const {
646     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
647   }
648
649   iterator_range<global_variable_iterator> global_variables() const {
650     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
651   }
652
653   iterator_range<type_iterator> types() const {
654     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
655   }
656
657   iterator_range<scope_iterator> scopes() const {
658     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
659   }
660
661   unsigned compile_unit_count() const { return CUs.size(); }
662   unsigned global_variable_count() const { return GVs.size(); }
663   unsigned subprogram_count() const { return SPs.size(); }
664   unsigned type_count() const { return TYs.size(); }
665   unsigned scope_count() const { return Scopes.size(); }
666
667 private:
668   SmallVector<DICompileUnit, 8> CUs;
669   SmallVector<DISubprogram, 8> SPs;
670   SmallVector<DIGlobalVariable, 8> GVs;
671   SmallVector<DIType, 8> TYs;
672   SmallVector<DIScope, 8> Scopes;
673   SmallPtrSet<MDNode *, 64> NodesSeen;
674   DITypeIdentifierMap TypeIdentifierMap;
675
676   /// \brief Specify if TypeIdentifierMap is initialized.
677   bool TypeMapInitialized;
678 };
679
680 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
681
682 } // end namespace llvm
683
684 #endif