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