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