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