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