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