1780232445c923026a21329fab303a0e1eb35345
[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   // Befriends DIRef so DIRef can befriend the protected member
67   // function: getFieldAs<DIRef>.
68   template <typename T> friend class DIRef;
69
70 public:
71   /// \brief Duplicated debug info flags.
72   ///
73   /// \see DebugNode::DIFlags.
74   enum {
75 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = DebugNode::Flag##NAME,
76 #include "llvm/IR/DebugInfoFlags.def"
77     FlagAccessibility = DebugNode::FlagAccessibility
78   };
79
80 protected:
81   const MDNode *DbgNode;
82
83   DIDescriptor getDescriptorField(unsigned Elt) const;
84   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
85     return DescTy(getDescriptorField(Elt));
86   }
87
88 public:
89   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
90
91   MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
92   operator MDNode *() const { return get(); }
93   MDNode *operator->() const { return get(); }
94   MDNode &operator*() const {
95     assert(get() && "Expected valid pointer");
96     return *get();
97   }
98
99   // An explicit operator bool so that we can do testing of DI values
100   // easily.
101   // FIXME: This operator bool isn't actually protecting anything at the
102   // moment due to the conversion operator above making DIDescriptor nodes
103   // implicitly convertable to bool.
104   explicit operator bool() const { return DbgNode != nullptr; }
105
106   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
107   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
108
109   uint16_t getTag() const {
110     if (auto *N = dyn_cast_or_null<DebugNode>(get()))
111       return N->getTag();
112     return 0;
113   }
114
115   void print(raw_ostream &OS) const;
116   void dump() const;
117
118   /// \brief Replace all uses of debug info referenced by this descriptor.
119   void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
120   void replaceAllUsesWith(MDNode *D);
121 };
122
123 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC)                                      \
124   class DESC;                                                                  \
125   template <> struct simplify_type<const DESC>;                                \
126   template <> struct simplify_type<DESC>;
127 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
135 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
136 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
137 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
138 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
139 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
140 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
141 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
142 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
143 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
144 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
145 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
146 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
147 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
148 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
149 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
150 #undef DECLARE_SIMPLIFY_DESCRIPTOR
151
152 /// \brief This is used to represent ranges, for array bounds.
153 class DISubrange : public DIDescriptor {
154 public:
155   DISubrange() = default;
156   DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
157
158   MDSubrange *get() const {
159     return cast_or_null<MDSubrange>(DIDescriptor::get());
160   }
161   operator MDSubrange *() const { return get(); }
162   MDSubrange *operator->() const { return get(); }
163   MDSubrange &operator*() const {
164     assert(get() && "Expected valid pointer");
165     return *get();
166   }
167
168   int64_t getLo() const { return get()->getLowerBound(); }
169   int64_t getCount() const { return get()->getCount(); }
170 };
171
172 /// \brief This descriptor holds an array of nodes with type T.
173 template <typename T> class DITypedArray : public DIDescriptor {
174 public:
175   explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
176   operator MDTuple *() const {
177     return const_cast<MDTuple *>(cast_or_null<MDTuple>(DbgNode));
178   }
179   unsigned getNumElements() const {
180     return DbgNode ? DbgNode->getNumOperands() : 0;
181   }
182   T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
183 };
184
185 typedef DITypedArray<DIDescriptor> DIArray;
186
187 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
188 ///
189 /// FIXME: it seems strange that this doesn't have either a reference to the
190 /// type/precision or a file/line pair for location info.
191 class DIEnumerator : public DIDescriptor {
192 public:
193   DIEnumerator() = default;
194   DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
195
196   MDEnumerator *get() const {
197     return cast_or_null<MDEnumerator>(DIDescriptor::get());
198   }
199   operator MDEnumerator *() const { return get(); }
200   MDEnumerator *operator->() const { return get(); }
201   MDEnumerator &operator*() const {
202     assert(get() && "Expected valid pointer");
203     return *get();
204   }
205
206   StringRef getName() const { return get()->getName(); }
207   int64_t getEnumValue() const { return get()->getValue(); }
208 };
209
210 template <typename T> class DIRef;
211 typedef DIRef<DIDescriptor> DIDescriptorRef;
212 typedef DIRef<DIScope> DIScopeRef;
213 typedef DIRef<DIType> DITypeRef;
214 typedef DITypedArray<DITypeRef> DITypeArray;
215
216 /// \brief A base class for various scopes.
217 ///
218 /// Although, implementation-wise, DIScope is the parent class of most
219 /// other DIxxx classes, including DIType and its descendants, most of
220 /// DIScope's descendants are not a substitutable subtype of
221 /// DIScope. The DIDescriptor::isScope() method only is true for
222 /// DIScopes that are scopes in the strict lexical scope sense
223 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
224 class DIScope : public DIDescriptor {
225 public:
226   DIScope() = default;
227   DIScope(const MDScope *N) : DIDescriptor(N) {}
228
229   MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
230   operator MDScope *() const { return get(); }
231   MDScope *operator->() const { return get(); }
232   MDScope &operator*() const {
233     assert(get() && "Expected valid pointer");
234     return *get();
235   }
236
237   /// \brief Get the parent scope.
238   ///
239   /// Gets the parent scope for this scope node or returns a default
240   /// constructed scope.
241   DIScopeRef getContext() const;
242   /// \brief Get the scope name.
243   ///
244   /// If the scope node has a name, return that, else return an empty string.
245   StringRef getName() const;
246   StringRef getFilename() const;
247   StringRef getDirectory() const;
248
249   /// \brief Generate a reference to this DIScope.
250   ///
251   /// Uses the type identifier instead of the actual MDNode if possible, to
252   /// help type uniquing.
253   DIScopeRef getRef() const;
254 };
255
256 /// \brief Represents reference to a DIDescriptor.
257 ///
258 /// Abstracts over direct and identifier-based metadata references.
259 template <typename T> class DIRef {
260   template <typename DescTy>
261   friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
262   friend DIScopeRef DIScope::getContext() const;
263   friend DIScopeRef DIScope::getRef() const;
264   friend class DIType;
265
266   /// \brief Val can be either a MDNode or a MDString.
267   ///
268   /// In the latter, MDString specifies the type identifier.
269   const Metadata *Val;
270   explicit DIRef(const Metadata *V);
271
272 public:
273   template <class U>
274   DIRef(const TypedDebugNodeRef<U> &Ref,
275         typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
276             nullptr)
277       : Val(Ref) {}
278
279   T resolve(const DITypeIdentifierMap &Map) const;
280   operator Metadata *() const { return const_cast<Metadata *>(Val); }
281
282   static DIRef get(const Metadata *MD) { return DIRef(MD); }
283 };
284
285 template <>
286 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
287 template <>
288 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
289 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
290
291 /// \brief Handle fields that are references to DIDescriptors.
292 template <>
293 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
294 /// \brief Specialize DIRef constructor for DIDescriptorRef.
295 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
296
297 /// \brief Handle fields that are references to DIScopes.
298 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
299 /// \brief Specialize DIRef constructor for DIScopeRef.
300 template <> DIRef<DIScope>::DIRef(const Metadata *V);
301
302 /// \brief Handle fields that are references to DITypes.
303 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
304 /// \brief Specialize DIRef constructor for DITypeRef.
305 template <> DIRef<DIType>::DIRef(const Metadata *V);
306
307 /// \brief This is a wrapper for a type.
308 ///
309 /// FIXME: Types should be factored much better so that CV qualifiers and
310 /// others do not require a huge and empty descriptor full of zeros.
311 class DIType : public DIScope {
312 public:
313   DIType() = default;
314   DIType(const MDType *N) : DIScope(N) {}
315
316   MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
317   operator MDType *() const { return get(); }
318   MDType *operator->() const { return get(); }
319   MDType &operator*() const {
320     assert(get() && "Expected valid pointer");
321     return *get();
322   }
323
324   DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
325   StringRef getName() const { return get()->getName(); }
326   unsigned getLineNumber() const { return get()->getLine(); }
327   uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
328   uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
329   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
330   // carry this is just plain insane.
331   uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
332   unsigned getFlags() const { return get()->getFlags(); }
333   bool isPrivate() const {
334     return (getFlags() & FlagAccessibility) == FlagPrivate;
335   }
336   bool isProtected() const {
337     return (getFlags() & FlagAccessibility) == FlagProtected;
338   }
339   bool isPublic() const {
340     return (getFlags() & FlagAccessibility) == FlagPublic;
341   }
342   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
343   bool isAppleBlockExtension() const {
344     return (getFlags() & FlagAppleBlock) != 0;
345   }
346   bool isBlockByrefStruct() const {
347     return (getFlags() & FlagBlockByrefStruct) != 0;
348   }
349   bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
350   bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
351   bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
352   bool isObjcClassComplete() const {
353     return (getFlags() & FlagObjcClassComplete) != 0;
354   }
355   bool isVector() const { return (getFlags() & FlagVector) != 0; }
356   bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
357   bool isLValueReference() const {
358     return (getFlags() & FlagLValueReference) != 0;
359   }
360   bool isRValueReference() const {
361     return (getFlags() & FlagRValueReference) != 0;
362   }
363   bool isValid() const { return DbgNode && isa<MDType>(*this); }
364 };
365
366 /// \brief A basic type, like 'int' or 'float'.
367 class DIBasicType : public DIType {
368 public:
369   DIBasicType() = default;
370   DIBasicType(const MDBasicType *N) : DIType(N) {}
371
372   MDBasicType *get() const {
373     return cast_or_null<MDBasicType>(DIDescriptor::get());
374   }
375   operator MDBasicType *() const { return get(); }
376   MDBasicType *operator->() const { return get(); }
377   MDBasicType &operator*() const {
378     assert(get() && "Expected valid pointer");
379     return *get();
380   }
381
382   unsigned getEncoding() const { return get()->getEncoding(); }
383 };
384
385 /// \brief A simple derived type
386 ///
387 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
388 /// Or, a data member of a class/struct/union.
389 class DIDerivedType : public DIType {
390 public:
391   DIDerivedType() = default;
392   DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
393
394   MDDerivedTypeBase *get() const {
395     return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
396   }
397   operator MDDerivedTypeBase *() const { return get(); }
398   MDDerivedTypeBase *operator->() const { return get(); }
399   MDDerivedTypeBase &operator*() const {
400     assert(get() && "Expected valid pointer");
401     return *get();
402   }
403
404   DITypeRef getTypeDerivedFrom() const {
405     return DITypeRef::get(get()->getBaseType());
406   }
407
408   /// \brief Return property node, if this ivar is associated with one.
409   MDNode *getObjCProperty() const {
410     if (auto *N = dyn_cast<MDDerivedType>(get()))
411       return dyn_cast_or_null<MDNode>(N->getExtraData());
412     return nullptr;
413   }
414
415   DITypeRef getClassType() const {
416     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
417     if (auto *N = dyn_cast<MDDerivedType>(get()))
418       return DITypeRef::get(N->getExtraData());
419     return DITypeRef::get(nullptr);
420   }
421
422   Constant *getConstant() const {
423     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
424     if (auto *N = dyn_cast<MDDerivedType>(get()))
425       if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
426         return C->getValue();
427
428     return nullptr;
429   }
430 };
431
432 /// \brief Types that refer to multiple other types.
433 ///
434 /// This descriptor holds a type that can refer to multiple other types, like a
435 /// function or struct.
436 ///
437 /// DICompositeType is derived from DIDerivedType because some
438 /// composite types (such as enums) can be derived from basic types
439 // FIXME: Make this derive from DIType directly & just store the
440 // base type in a single DIType field.
441 class DICompositeType : public DIDerivedType {
442   friend class DIBuilder;
443
444   /// \brief Set the array of member DITypes.
445   void setArraysHelper(MDNode *Elements, MDNode *TParams);
446
447 public:
448   DICompositeType() = default;
449   DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
450
451   MDCompositeTypeBase *get() const {
452     return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
453   }
454   operator MDCompositeTypeBase *() const { return get(); }
455   MDCompositeTypeBase *operator->() const { return get(); }
456   MDCompositeTypeBase &operator*() const {
457     assert(get() && "Expected valid pointer");
458     return *get();
459   }
460
461   DIArray getElements() const {
462     assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
463     return DIArray(get()->getElements());
464   }
465
466 private:
467   template <typename T>
468   void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
469     assert(
470         (!TParams || DbgNode->getNumOperands() == 8) &&
471         "If you're setting the template parameters this should include a slot "
472         "for that!");
473     setArraysHelper(Elements, TParams);
474   }
475
476 public:
477   unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
478   DITypeRef getContainingType() const {
479     return DITypeRef::get(get()->getVTableHolder());
480   }
481
482 private:
483   /// \brief Set the containing type.
484   void setContainingType(DICompositeType ContainingType);
485
486 public:
487   DIArray getTemplateParams() const {
488     return DIArray(get()->getTemplateParams());
489   }
490   MDString *getIdentifier() const { return get()->getRawIdentifier(); }
491 };
492
493 class DISubroutineType : public DICompositeType {
494 public:
495   DISubroutineType() = default;
496   DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
497
498   MDSubroutineType *get() const {
499     return cast_or_null<MDSubroutineType>(DIDescriptor::get());
500   }
501   operator MDSubroutineType *() const { return get(); }
502   MDSubroutineType *operator->() const { return get(); }
503   MDSubroutineType &operator*() const {
504     assert(get() && "Expected valid pointer");
505     return *get();
506   }
507
508   DITypedArray<DITypeRef> getTypeArray() const {
509     return DITypedArray<DITypeRef>(get()->getTypeArray());
510   }
511 };
512
513 /// \brief This is a wrapper for a file.
514 class DIFile : public DIScope {
515 public:
516   DIFile() = default;
517   DIFile(const MDFile *N) : DIScope(N) {}
518
519   MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
520   operator MDFile *() const { return get(); }
521   MDFile *operator->() const { return get(); }
522   MDFile &operator*() const {
523     assert(get() && "Expected valid pointer");
524     return *get();
525   }
526
527   /// \brief Retrieve the MDNode for the directory/file pair.
528   MDNode *getFileNode() const { return get(); }
529 };
530
531 /// \brief A wrapper for a compile unit.
532 class DICompileUnit : public DIScope {
533 public:
534   DICompileUnit() = default;
535   DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
536
537   MDCompileUnit *get() const {
538     return cast_or_null<MDCompileUnit>(DIDescriptor::get());
539   }
540   operator MDCompileUnit *() const { return get(); }
541   MDCompileUnit *operator->() const { return get(); }
542   MDCompileUnit &operator*() const {
543     assert(get() && "Expected valid pointer");
544     return *get();
545   }
546
547   dwarf::SourceLanguage getLanguage() const {
548     return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
549   }
550   StringRef getProducer() const { return get()->getProducer(); }
551   bool isOptimized() const { return get()->isOptimized(); }
552   StringRef getFlags() const { return get()->getFlags(); }
553   unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
554
555   DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
556   DIArray getRetainedTypes() const {
557     return DIArray(get()->getRetainedTypes());
558   }
559   DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
560   DIArray getGlobalVariables() const {
561     return DIArray(get()->getGlobalVariables());
562   }
563   DIArray getImportedEntities() const {
564     return DIArray(get()->getImportedEntities());
565   }
566
567   void replaceSubprograms(DIArray Subprograms);
568   void replaceGlobalVariables(DIArray GlobalVariables);
569
570   StringRef getSplitDebugFilename() const {
571     return get()->getSplitDebugFilename();
572   }
573   unsigned getEmissionKind() const { return get()->getEmissionKind(); }
574 };
575
576 /// \brief This is a wrapper for a subprogram (e.g. a function).
577 class DISubprogram : public DIScope {
578 public:
579   DISubprogram() = default;
580   DISubprogram(const MDSubprogram *N) : DIScope(N) {}
581
582   MDSubprogram *get() const {
583     return cast_or_null<MDSubprogram>(DIDescriptor::get());
584   }
585   operator MDSubprogram *() const { return get(); }
586   MDSubprogram *operator->() const { return get(); }
587   MDSubprogram &operator*() const {
588     assert(get() && "Expected valid pointer");
589     return *get();
590   }
591
592   StringRef getName() const { return get()->getName(); }
593   StringRef getDisplayName() const { return get()->getDisplayName(); }
594   StringRef getLinkageName() const { return get()->getLinkageName(); }
595   unsigned getLineNumber() const { return get()->getLine(); }
596
597   /// \brief Check if this is local (like 'static' in C).
598   unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
599   unsigned isDefinition() const { return get()->isDefinition(); }
600
601   unsigned getVirtuality() const { return get()->getVirtuality(); }
602   unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
603
604   unsigned getFlags() const { return get()->getFlags(); }
605
606   unsigned isOptimized() const { return get()->isOptimized(); }
607
608   /// \brief Get the beginning of the scope of the function (not the name).
609   unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
610
611   DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
612   DISubroutineType getType() const {
613     return DISubroutineType(get()->getType());
614   }
615
616   DITypeRef getContainingType() const {
617     return DITypeRef::get(get()->getContainingType());
618   }
619
620   /// \brief Check if this provides debugging information for the function F.
621   bool describes(const Function *F);
622
623   Function *getFunction() const;
624
625   void replaceFunction(Function *F) {
626     if (auto *N = get())
627       N->replaceFunction(F);
628   }
629   DIArray getTemplateParams() const {
630     return DIArray(get()->getTemplateParams());
631   }
632   DISubprogram getFunctionDeclaration() const {
633     return DISubprogram(get()->getDeclaration());
634   }
635   MDNode *getVariablesNodes() const { return getVariables(); }
636   DIArray getVariables() const { return DIArray(get()->getVariables()); }
637
638   unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
639   /// \brief Check for the "private" access specifier.
640   bool isPrivate() const {
641     return (getFlags() & FlagAccessibility) == FlagPrivate;
642   }
643   /// \brief Check for the "protected" access specifier.
644   bool isProtected() const {
645     return (getFlags() & FlagAccessibility) == FlagProtected;
646   }
647   /// \brief Check for the "public" access specifier.
648   bool isPublic() const {
649     return (getFlags() & FlagAccessibility) == FlagPublic;
650   }
651   /// \brief Check for "explicit".
652   bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
653   /// \brief Check if this is prototyped.
654   bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
655
656   /// \brief Check if this is reference-qualified.
657   ///
658   /// Return true if this subprogram is a C++11 reference-qualified non-static
659   /// member function (void foo() &).
660   unsigned isLValueReference() const {
661     return (getFlags() & FlagLValueReference) != 0;
662   }
663
664   /// \brief Check if this is rvalue-reference-qualified.
665   ///
666   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
667   /// non-static member function (void foo() &&).
668   unsigned isRValueReference() const {
669     return (getFlags() & FlagRValueReference) != 0;
670   }
671 };
672
673 /// \brief This is a wrapper for a lexical block.
674 class DILexicalBlock : public DIScope {
675 public:
676   DILexicalBlock() = default;
677   DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
678
679   MDLexicalBlockBase *get() const {
680     return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
681   }
682   operator MDLexicalBlockBase *() const { return get(); }
683   MDLexicalBlockBase *operator->() const { return get(); }
684   MDLexicalBlockBase &operator*() const {
685     assert(get() && "Expected valid pointer");
686     return *get();
687   }
688
689   DIScope getContext() const { return DIScope(get()->getScope()); }
690   unsigned getLineNumber() const {
691     if (auto *N = dyn_cast<MDLexicalBlock>(get()))
692       return N->getLine();
693     return 0;
694   }
695   unsigned getColumnNumber() const {
696     if (auto *N = dyn_cast<MDLexicalBlock>(get()))
697       return N->getColumn();
698     return 0;
699   }
700 };
701
702 /// \brief This is a wrapper for a lexical block with a filename change.
703 class DILexicalBlockFile : public DIScope {
704 public:
705   DILexicalBlockFile() = default;
706   DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
707
708   MDLexicalBlockFile *get() const {
709     return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
710   }
711   operator MDLexicalBlockFile *() const { return get(); }
712   MDLexicalBlockFile *operator->() const { return get(); }
713   MDLexicalBlockFile &operator*() const {
714     assert(get() && "Expected valid pointer");
715     return *get();
716   }
717
718   DIScope getContext() const { return get()->getScope(); }
719   unsigned getDiscriminator() const { return get()->getDiscriminator(); }
720 };
721
722 /// \brief A wrapper for a C++ style name space.
723 class DINameSpace : public DIScope {
724 public:
725   DINameSpace() = default;
726   DINameSpace(const MDNamespace *N) : DIScope(N) {}
727
728   MDNamespace *get() const {
729     return cast_or_null<MDNamespace>(DIDescriptor::get());
730   }
731   operator MDNamespace *() const { return get(); }
732   MDNamespace *operator->() const { return get(); }
733   MDNamespace &operator*() const {
734     assert(get() && "Expected valid pointer");
735     return *get();
736   }
737
738   StringRef getName() const { return get()->getName(); }
739   unsigned getLineNumber() const { return get()->getLine(); }
740   DIScope getContext() const { return DIScope(get()->getScope()); }
741 };
742
743 /// \brief This is a wrapper for template type parameter.
744 class DITemplateTypeParameter : public DIDescriptor {
745 public:
746   DITemplateTypeParameter() = default;
747   DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
748
749   MDTemplateTypeParameter *get() const {
750     return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
751   }
752   operator MDTemplateTypeParameter *() const { return get(); }
753   MDTemplateTypeParameter *operator->() const { return get(); }
754   MDTemplateTypeParameter &operator*() const {
755     assert(get() && "Expected valid pointer");
756     return *get();
757   }
758
759   StringRef getName() const { return get()->getName(); }
760
761   DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
762 };
763
764 /// \brief This is a wrapper for template value parameter.
765 class DITemplateValueParameter : public DIDescriptor {
766 public:
767   DITemplateValueParameter() = default;
768   DITemplateValueParameter(const MDTemplateValueParameter *N)
769       : DIDescriptor(N) {}
770
771   MDTemplateValueParameter *get() const {
772     return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
773   }
774   operator MDTemplateValueParameter *() const { return get(); }
775   MDTemplateValueParameter *operator->() const { return get(); }
776   MDTemplateValueParameter &operator*() const {
777     assert(get() && "Expected valid pointer");
778     return *get();
779   }
780
781   StringRef getName() const { return get()->getName(); }
782   DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
783   Metadata *getValue() const { return get()->getValue(); }
784 };
785
786 /// \brief This is a wrapper for a global variable.
787 class DIGlobalVariable : public DIDescriptor {
788   DIFile getFile() const { return DIFile(get()->getFile()); }
789
790 public:
791   DIGlobalVariable() = default;
792   DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
793
794   MDGlobalVariable *get() const {
795     return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
796   }
797   operator MDGlobalVariable *() const { return get(); }
798   MDGlobalVariable *operator->() const { return get(); }
799   MDGlobalVariable &operator*() const {
800     assert(get() && "Expected valid pointer");
801     return *get();
802   }
803
804   StringRef getName() const { return get()->getName(); }
805   StringRef getDisplayName() const { return get()->getDisplayName(); }
806   StringRef getLinkageName() const { return get()->getLinkageName(); }
807   unsigned getLineNumber() const { return get()->getLine(); }
808   unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
809   unsigned isDefinition() const { return get()->isDefinition(); }
810
811   DIScope getContext() const { return DIScope(get()->getScope()); }
812   StringRef getFilename() const { return getFile().getFilename(); }
813   StringRef getDirectory() const { return getFile().getDirectory(); }
814   DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
815
816   GlobalVariable *getGlobal() const;
817   Constant *getConstant() const {
818     if (auto *N = get())
819       if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
820         return C->getValue();
821     return nullptr;
822   }
823   DIDerivedType getStaticDataMemberDeclaration() const {
824     return DIDerivedType(get()->getStaticDataMemberDeclaration());
825   }
826 };
827
828 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
829 class DIVariable : public DIDescriptor {
830   unsigned getFlags() const { return get()->getFlags(); }
831
832 public:
833   DIVariable() = default;
834   DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
835
836   MDLocalVariable *get() const {
837     return cast_or_null<MDLocalVariable>(DIDescriptor::get());
838   }
839   operator MDLocalVariable *() const { return get(); }
840   MDLocalVariable *operator->() const { return get(); }
841   MDLocalVariable &operator*() const {
842     assert(get() && "Expected valid pointer");
843     return *get();
844   }
845
846   StringRef getName() const { return get()->getName(); }
847   unsigned getLineNumber() const { return get()->getLine(); }
848   unsigned getArgNumber() const { return get()->getArg(); }
849
850   DIScope getContext() const { return DIScope(get()->getScope()); }
851   DIFile getFile() const { return DIFile(get()->getFile()); }
852   DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
853
854   /// \brief Return true if this variable is marked as "artificial".
855   bool isArtificial() const {
856     return (getFlags() & FlagArtificial) != 0;
857   }
858
859   bool isObjectPointer() const {
860     return (getFlags() & FlagObjectPointer) != 0;
861   }
862
863   /// \brief If this variable is inlined then return inline location.
864   MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
865
866   /// \brief Check if this is a "__block" variable (Apple Blocks).
867   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
868     return (getType().resolve(Map)).isBlockByrefStruct();
869   }
870
871   /// \brief Check if this is an inlined function argument.
872   bool isInlinedFnArgument(const Function *CurFn);
873
874   /// \brief Return the size reported by the variable's type.
875   unsigned getSizeInBits(const DITypeIdentifierMap &Map);
876
877   void printExtendedName(raw_ostream &OS) const;
878 };
879
880 /// \brief A complex location expression in postfix notation.
881 ///
882 /// This is (almost) a DWARF expression that modifies the location of a
883 /// variable or (or the location of a single piece of a variable).
884 ///
885 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
886 /// and have DW_OP_plus consume the topmost elements on the stack.
887 class DIExpression : public DIDescriptor {
888 public:
889   DIExpression() = default;
890   DIExpression(const MDExpression *N) : DIDescriptor(N) {}
891
892   MDExpression *get() const {
893     return cast_or_null<MDExpression>(DIDescriptor::get());
894   }
895   operator MDExpression *() const { return get(); }
896   MDExpression *operator->() const { return get(); }
897   MDExpression &operator*() const {
898     assert(get() && "Expected valid pointer");
899     return *get();
900   }
901
902   /// \brief Return the number of elements in the complex expression.
903   unsigned getNumElements() const { return get()->getNumElements(); }
904
905   /// \brief return the Idx'th complex address element.
906   uint64_t getElement(unsigned I) const { return get()->getElement(I); }
907
908   /// \brief Return whether this is a piece of an aggregate variable.
909   bool isBitPiece() const;
910   /// \brief Return the offset of this piece in bits.
911   uint64_t getBitPieceOffset() const;
912   /// \brief Return the size of this piece in bits.
913   uint64_t getBitPieceSize() const;
914
915   class iterator;
916   /// \brief A lightweight wrapper around an element of a DIExpression.
917   class Operand {
918     friend class iterator;
919     MDExpression::element_iterator I;
920     Operand() {}
921     Operand(MDExpression::element_iterator I) : I(I) {}
922   public:
923     /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
924     /// Argument 0 is the operand itself.
925     uint64_t getArg(unsigned N) const {
926       MDExpression::element_iterator In = I;
927       std::advance(In, N);
928       return *In;
929     }
930     operator uint64_t () const { return *I; }
931     /// \brief Returns underlying MDExpression::element_iterator.
932     const MDExpression::element_iterator &getBase() const { return I; }
933     /// \brief Returns the next operand.
934     iterator getNext() const;
935   };
936
937   /// \brief An iterator for DIExpression elements.
938   class iterator : public std::iterator<std::input_iterator_tag, StringRef,
939                                         unsigned, const Operand*, Operand> {
940     friend class Operand;
941     MDExpression::element_iterator I;
942     Operand Tmp;
943
944   public:
945     iterator(MDExpression::element_iterator I) : I(I) {}
946     const Operand &operator*() { return Tmp = Operand(I); }
947     const Operand *operator->() { return &(Tmp = Operand(I)); }
948     iterator &operator++() {
949       increment();
950       return *this;
951     }
952     iterator operator++(int) {
953       iterator X(*this);
954       increment();
955       return X;
956     }
957     bool operator==(const iterator &X) const { return I == X.I; }
958     bool operator!=(const iterator &X) const { return !(*this == X); }
959
960   private:
961     void increment() {
962       switch (**this) {
963       case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
964       case dwarf::DW_OP_plus:      std::advance(I, 2); break;
965       case dwarf::DW_OP_deref:     std::advance(I, 1); break;
966       default:
967         llvm_unreachable("unsupported operand");
968       }
969     }
970   };
971
972   iterator begin() const { return get()->elements_begin(); }
973   iterator end() const { return get()->elements_end(); }
974 };
975
976 /// \brief This object holds location information.
977 ///
978 /// This object is not associated with any DWARF tag.
979 class DILocation : public DIDescriptor {
980 public:
981   DILocation() = default;
982   DILocation(const MDLocation *N) : DIDescriptor(N) {}
983
984   MDLocation *get() const {
985     return cast_or_null<MDLocation>(DIDescriptor::get());
986   }
987   operator MDLocation *() const { return get(); }
988   MDLocation *operator->() const { return get(); }
989   MDLocation &operator*() const {
990     assert(get() && "Expected valid pointer");
991     return *get();
992   }
993
994   unsigned getLineNumber() const { return get()->getLine(); }
995   unsigned getColumnNumber() const { return get()->getColumn(); }
996   DIScope getScope() const { return DIScope(get()->getScope()); }
997   DILocation getOrigLocation() const {
998     return DILocation(get()->getInlinedAt());
999   }
1000   StringRef getFilename() const { return getScope().getFilename(); }
1001   StringRef getDirectory() const { return getScope().getDirectory(); }
1002   bool atSameLineAs(const DILocation &Other) const {
1003     return (getLineNumber() == Other.getLineNumber() &&
1004             getFilename() == Other.getFilename());
1005   }
1006   /// \brief Get the DWAF discriminator.
1007   ///
1008   /// DWARF discriminators are used to distinguish identical file locations for
1009   /// instructions that are on different basic blocks. If two instructions are
1010   /// inside the same lexical block and are in different basic blocks, we
1011   /// create a new lexical block with identical location as the original but
1012   /// with a different discriminator value
1013   /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1014   unsigned getDiscriminator() const {
1015     // Since discriminators are associated with lexical blocks, make
1016     // sure this location is a lexical block before retrieving its
1017     // value.
1018     if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
1019       return F->getDiscriminator();
1020     return 0;
1021   }
1022
1023   /// \brief Generate a new discriminator value for this location.
1024   unsigned computeNewDiscriminator(LLVMContext &Ctx);
1025
1026   /// \brief Return a copy of this location with a different scope.
1027   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1028 };
1029
1030 class DIObjCProperty : public DIDescriptor {
1031 public:
1032   DIObjCProperty() = default;
1033   DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1034
1035   MDObjCProperty *get() const {
1036     return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1037   }
1038   operator MDObjCProperty *() const { return get(); }
1039   MDObjCProperty *operator->() const { return get(); }
1040   MDObjCProperty &operator*() const {
1041     assert(get() && "Expected valid pointer");
1042     return *get();
1043   }
1044
1045   StringRef getObjCPropertyName() const { return get()->getName(); }
1046   DIFile getFile() const { return DIFile(get()->getFile()); }
1047   unsigned getLineNumber() const { return get()->getLine(); }
1048
1049   StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1050   StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1051   unsigned getAttributes() const { return get()->getAttributes(); }
1052   bool isReadOnlyObjCProperty() const {
1053     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1054   }
1055   bool isReadWriteObjCProperty() const {
1056     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1057   }
1058   bool isAssignObjCProperty() const {
1059     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1060   }
1061   bool isRetainObjCProperty() const {
1062     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1063   }
1064   bool isCopyObjCProperty() const {
1065     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1066   }
1067   bool isNonAtomicObjCProperty() const {
1068     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1069   }
1070
1071   /// \brief Get the type.
1072   ///
1073   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1074   /// the type as a DITypeRef here.
1075   DIType getType() const { return DIType(get()->getType()); }
1076 };
1077
1078 /// \brief An imported module (C++ using directive or similar).
1079 class DIImportedEntity : public DIDescriptor {
1080 public:
1081   DIImportedEntity() = default;
1082   DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1083
1084   MDImportedEntity *get() const {
1085     return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1086   }
1087   operator MDImportedEntity *() const { return get(); }
1088   MDImportedEntity *operator->() const { return get(); }
1089   MDImportedEntity &operator*() const {
1090     assert(get() && "Expected valid pointer");
1091     return *get();
1092   }
1093
1094   DIScope getContext() const { return DIScope(get()->getScope()); }
1095   DIDescriptorRef getEntity() const {
1096     return DIDescriptorRef::get(get()->getEntity());
1097   }
1098   unsigned getLineNumber() const { return get()->getLine(); }
1099   StringRef getName() const { return get()->getName(); }
1100 };
1101
1102 #define SIMPLIFY_DESCRIPTOR(DESC)                                              \
1103   template <> struct simplify_type<const DESC> {                               \
1104     typedef Metadata *SimpleType;                                              \
1105     static SimpleType getSimplifiedValue(const DESC &DI) { return DI; }        \
1106   };                                                                           \
1107   template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
1108 SIMPLIFY_DESCRIPTOR(DIDescriptor)
1109 SIMPLIFY_DESCRIPTOR(DISubrange)
1110 SIMPLIFY_DESCRIPTOR(DIEnumerator)
1111 SIMPLIFY_DESCRIPTOR(DIScope)
1112 SIMPLIFY_DESCRIPTOR(DIType)
1113 SIMPLIFY_DESCRIPTOR(DIBasicType)
1114 SIMPLIFY_DESCRIPTOR(DIDerivedType)
1115 SIMPLIFY_DESCRIPTOR(DICompositeType)
1116 SIMPLIFY_DESCRIPTOR(DISubroutineType)
1117 SIMPLIFY_DESCRIPTOR(DIFile)
1118 SIMPLIFY_DESCRIPTOR(DICompileUnit)
1119 SIMPLIFY_DESCRIPTOR(DISubprogram)
1120 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
1121 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
1122 SIMPLIFY_DESCRIPTOR(DINameSpace)
1123 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
1124 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
1125 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
1126 SIMPLIFY_DESCRIPTOR(DIVariable)
1127 SIMPLIFY_DESCRIPTOR(DIExpression)
1128 SIMPLIFY_DESCRIPTOR(DILocation)
1129 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
1130 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
1131 #undef SIMPLIFY_DESCRIPTOR
1132
1133 /// \brief Find subprogram that is enclosing this scope.
1134 DISubprogram getDISubprogram(const MDNode *Scope);
1135
1136 /// \brief Find debug info for a given function.
1137 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1138 /// DISubprogram.
1139 DISubprogram getDISubprogram(const Function *F);
1140
1141 /// \brief Find underlying composite type.
1142 DICompositeType getDICompositeType(DIType T);
1143
1144 /// \brief Create a new inlined variable based on current variable.
1145 ///
1146 /// @param DV            Current Variable.
1147 /// @param InlinedScope  Location at current variable is inlined.
1148 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1149                                  LLVMContext &VMContext);
1150
1151 /// \brief Remove inlined scope from the variable.
1152 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1153
1154 /// \brief Generate map by visiting all retained types.
1155 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1156
1157 /// \brief Strip debug info in the module if it exists.
1158 ///
1159 /// To do this, we remove all calls to the debugger intrinsics and any named
1160 /// metadata for debugging. We also remove debug locations for instructions.
1161 /// Return true if module is modified.
1162 bool StripDebugInfo(Module &M);
1163 bool stripDebugInfo(Function &F);
1164
1165 /// \brief Return Debug Info Metadata Version by checking module flags.
1166 unsigned getDebugMetadataVersionFromModule(const Module &M);
1167
1168 /// \brief Utility to find all debug info in a module.
1169 ///
1170 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1171 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1172 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1173 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1174 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1175 /// used by the CUs.
1176 class DebugInfoFinder {
1177 public:
1178   DebugInfoFinder() : TypeMapInitialized(false) {}
1179
1180   /// \brief Process entire module and collect debug info anchors.
1181   void processModule(const Module &M);
1182
1183   /// \brief Process DbgDeclareInst.
1184   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1185   /// \brief Process DbgValueInst.
1186   void processValue(const Module &M, const DbgValueInst *DVI);
1187   /// \brief Process DILocation.
1188   void processLocation(const Module &M, DILocation Loc);
1189
1190   /// \brief Clear all lists.
1191   void reset();
1192
1193 private:
1194   void InitializeTypeMap(const Module &M);
1195
1196   void processType(DIType DT);
1197   void processSubprogram(DISubprogram SP);
1198   void processScope(DIScope Scope);
1199   bool addCompileUnit(DICompileUnit CU);
1200   bool addGlobalVariable(DIGlobalVariable DIG);
1201   bool addSubprogram(DISubprogram SP);
1202   bool addType(DIType DT);
1203   bool addScope(DIScope Scope);
1204
1205 public:
1206   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1207   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1208   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1209       global_variable_iterator;
1210   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1211   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1212
1213   iterator_range<compile_unit_iterator> compile_units() const {
1214     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1215   }
1216
1217   iterator_range<subprogram_iterator> subprograms() const {
1218     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1219   }
1220
1221   iterator_range<global_variable_iterator> global_variables() const {
1222     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1223   }
1224
1225   iterator_range<type_iterator> types() const {
1226     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1227   }
1228
1229   iterator_range<scope_iterator> scopes() const {
1230     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1231   }
1232
1233   unsigned compile_unit_count() const { return CUs.size(); }
1234   unsigned global_variable_count() const { return GVs.size(); }
1235   unsigned subprogram_count() const { return SPs.size(); }
1236   unsigned type_count() const { return TYs.size(); }
1237   unsigned scope_count() const { return Scopes.size(); }
1238
1239 private:
1240   SmallVector<DICompileUnit, 8> CUs;
1241   SmallVector<DISubprogram, 8> SPs;
1242   SmallVector<DIGlobalVariable, 8> GVs;
1243   SmallVector<DIType, 8> TYs;
1244   SmallVector<DIScope, 8> Scopes;
1245   SmallPtrSet<MDNode *, 64> NodesSeen;
1246   DITypeIdentifierMap TypeIdentifierMap;
1247
1248   /// \brief Specify if TypeIdentifierMap is initialized.
1249   bool TypeMapInitialized;
1250 };
1251
1252 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1253
1254 } // end namespace llvm
1255
1256 #endif