1f247f59b6577b8460b6466083b5bf9014e5c423
[oota-llvm.git] / include / llvm / IR / DebugInfoMetadata.h
1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- 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 // Declarations for metadata specific to debug info.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
15 #define LLVM_IR_DEBUGINFOMETADATA_H
16
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Dwarf.h"
19
20 // Helper macros for defining get() overrides.
21 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
22 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
23 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)                                 \
24   static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) {  \
25     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued);          \
26   }                                                                            \
27   static CLASS *getIfExists(LLVMContext &Context,                              \
28                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
29     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued,           \
30                    /* ShouldCreate */ false);                                  \
31   }                                                                            \
32   static CLASS *getDistinct(LLVMContext &Context,                              \
33                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
34     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct);         \
35   }                                                                            \
36   static Temp##CLASS getTemporary(LLVMContext &Context,                        \
37                                   DEFINE_MDNODE_GET_UNPACK(FORMAL)) {          \
38     return Temp##CLASS(                                                        \
39         getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary));          \
40   }
41
42 namespace llvm {
43
44 /// \brief Pointer union between a subclass of DINode and MDString.
45 ///
46 /// \a DICompositeType can be referenced via an \a MDString unique identifier.
47 /// This class allows some type safety in the face of that, requiring either a
48 /// node of a particular type or an \a MDString.
49 template <class T> class TypedDINodeRef {
50   const Metadata *MD = nullptr;
51
52 public:
53   TypedDINodeRef() = default;
54   TypedDINodeRef(std::nullptr_t) {}
55
56   /// \brief Construct from a raw pointer.
57   explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
58     assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
59   }
60
61   template <class U>
62   TypedDINodeRef(
63       const TypedDINodeRef<U> &X,
64       typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
65           nullptr)
66       : MD(X) {}
67
68   operator Metadata *() const { return const_cast<Metadata *>(MD); }
69
70   bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; }
71   bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; }
72
73   /// \brief Create a reference.
74   ///
75   /// Get a reference to \c N, using an \a MDString reference if available.
76   static TypedDINodeRef get(const T *N);
77
78   template <class MapTy> T *resolve(const MapTy &Map) const {
79     if (!MD)
80       return nullptr;
81
82     if (auto *Typed = dyn_cast<T>(MD))
83       return const_cast<T *>(Typed);
84
85     auto *S = cast<MDString>(MD);
86     auto I = Map.find(S);
87     assert(I != Map.end() && "Missing identifier in type map");
88     return cast<T>(I->second);
89   }
90 };
91
92 typedef TypedDINodeRef<DINode> DINodeRef;
93 typedef TypedDINodeRef<DIScope> DIScopeRef;
94 typedef TypedDINodeRef<DIType> DITypeRef;
95
96 class DITypeRefArray {
97   const MDTuple *N = nullptr;
98
99 public:
100   DITypeRefArray(const MDTuple *N) : N(N) {}
101
102   explicit operator bool() const { return get(); }
103   explicit operator MDTuple *() const { return get(); }
104
105   MDTuple *get() const { return const_cast<MDTuple *>(N); }
106   MDTuple *operator->() const { return get(); }
107   MDTuple &operator*() const { return *get(); }
108
109   // FIXME: Fix callers and remove condition on N.
110   unsigned size() const { return N ? N->getNumOperands() : 0u; }
111   DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
112
113   class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
114                                  std::ptrdiff_t, void, DITypeRef> {
115     MDNode::op_iterator I = nullptr;
116
117   public:
118     iterator() = default;
119     explicit iterator(MDNode::op_iterator I) : I(I) {}
120     DITypeRef operator*() const { return DITypeRef(*I); }
121     iterator &operator++() {
122       ++I;
123       return *this;
124     }
125     iterator operator++(int) {
126       iterator Temp(*this);
127       ++I;
128       return Temp;
129     }
130     bool operator==(const iterator &X) const { return I == X.I; }
131     bool operator!=(const iterator &X) const { return I != X.I; }
132   };
133
134   // FIXME: Fix callers and remove condition on N.
135   iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
136   iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
137 };
138
139 /// \brief Tagged DWARF-like metadata node.
140 ///
141 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
142 /// defined in llvm/Support/Dwarf.h).  Called \a DINode because it's
143 /// potentially used for non-DWARF output.
144 class DINode : public MDNode {
145   friend class LLVMContextImpl;
146   friend class MDNode;
147
148 protected:
149   DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
150          ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
151       : MDNode(C, ID, Storage, Ops1, Ops2) {
152     assert(Tag < 1u << 16);
153     SubclassData16 = Tag;
154   }
155   ~DINode() = default;
156
157   template <class Ty> Ty *getOperandAs(unsigned I) const {
158     return cast_or_null<Ty>(getOperand(I));
159   }
160
161   StringRef getStringOperand(unsigned I) const {
162     if (auto *S = getOperandAs<MDString>(I))
163       return S->getString();
164     return StringRef();
165   }
166
167   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
168     if (S.empty())
169       return nullptr;
170     return MDString::get(Context, S);
171   }
172
173 public:
174   unsigned getTag() const { return SubclassData16; }
175
176   /// \brief Debug info flags.
177   ///
178   /// The three accessibility flags are mutually exclusive and rolled together
179   /// in the first two bits.
180   enum DIFlags {
181 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
182 #include "llvm/IR/DebugInfoFlags.def"
183     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
184   };
185
186   static unsigned getFlag(StringRef Flag);
187   static const char *getFlagString(unsigned Flag);
188
189   /// \brief Split up a flags bitfield.
190   ///
191   /// Split \c Flags into \c SplitFlags, a vector of its components.  Returns
192   /// any remaining (unrecognized) bits.
193   static unsigned splitFlags(unsigned Flags,
194                              SmallVectorImpl<unsigned> &SplitFlags);
195
196   DINodeRef getRef() const { return DINodeRef::get(this); }
197
198   static bool classof(const Metadata *MD) {
199     switch (MD->getMetadataID()) {
200     default:
201       return false;
202     case GenericDINodeKind:
203     case DISubrangeKind:
204     case DIEnumeratorKind:
205     case DIBasicTypeKind:
206     case DIDerivedTypeKind:
207     case DICompositeTypeKind:
208     case DISubroutineTypeKind:
209     case DIFileKind:
210     case DICompileUnitKind:
211     case DISubprogramKind:
212     case DILexicalBlockKind:
213     case DILexicalBlockFileKind:
214     case DINamespaceKind:
215     case DITemplateTypeParameterKind:
216     case DITemplateValueParameterKind:
217     case DIGlobalVariableKind:
218     case DILocalVariableKind:
219     case DIObjCPropertyKind:
220     case DIImportedEntityKind:
221     case DIModuleKind:
222       return true;
223     }
224   }
225 };
226
227 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
228   typedef Metadata *SimpleType;
229   static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
230     return MD;
231   }
232 };
233
234 template <class T>
235 struct simplify_type<TypedDINodeRef<T>>
236     : simplify_type<const TypedDINodeRef<T>> {};
237
238 /// \brief Generic tagged DWARF-like metadata node.
239 ///
240 /// An un-specialized DWARF-like metadata node.  The first operand is a
241 /// (possibly empty) null-separated \a MDString header that contains arbitrary
242 /// fields.  The remaining operands are \a dwarf_operands(), and are pointers
243 /// to other metadata.
244 class GenericDINode : public DINode {
245   friend class LLVMContextImpl;
246   friend class MDNode;
247
248   GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
249                 unsigned Tag, ArrayRef<Metadata *> Ops1,
250                 ArrayRef<Metadata *> Ops2)
251       : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
252     setHash(Hash);
253   }
254   ~GenericDINode() { dropAllReferences(); }
255
256   void setHash(unsigned Hash) { SubclassData32 = Hash; }
257   void recalculateHash();
258
259   static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
260                                 StringRef Header, ArrayRef<Metadata *> DwarfOps,
261                                 StorageType Storage, bool ShouldCreate = true) {
262     return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
263                    DwarfOps, Storage, ShouldCreate);
264   }
265
266   static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
267                                 MDString *Header, ArrayRef<Metadata *> DwarfOps,
268                                 StorageType Storage, bool ShouldCreate = true);
269
270   TempGenericDINode cloneImpl() const {
271     return getTemporary(
272         getContext(), getTag(), getHeader(),
273         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
274   }
275
276 public:
277   unsigned getHash() const { return SubclassData32; }
278
279   DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
280                                     ArrayRef<Metadata *> DwarfOps),
281                     (Tag, Header, DwarfOps))
282   DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
283                                     ArrayRef<Metadata *> DwarfOps),
284                     (Tag, Header, DwarfOps))
285
286   /// \brief Return a (temporary) clone of this.
287   TempGenericDINode clone() const { return cloneImpl(); }
288
289   unsigned getTag() const { return SubclassData16; }
290   StringRef getHeader() const { return getStringOperand(0); }
291
292   op_iterator dwarf_op_begin() const { return op_begin() + 1; }
293   op_iterator dwarf_op_end() const { return op_end(); }
294   op_range dwarf_operands() const {
295     return op_range(dwarf_op_begin(), dwarf_op_end());
296   }
297
298   unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
299   const MDOperand &getDwarfOperand(unsigned I) const {
300     return getOperand(I + 1);
301   }
302   void replaceDwarfOperandWith(unsigned I, Metadata *New) {
303     replaceOperandWith(I + 1, New);
304   }
305
306   static bool classof(const Metadata *MD) {
307     return MD->getMetadataID() == GenericDINodeKind;
308   }
309 };
310
311 /// \brief Array subrange.
312 ///
313 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
314 /// type.
315 class DISubrange : public DINode {
316   friend class LLVMContextImpl;
317   friend class MDNode;
318
319   int64_t Count;
320   int64_t LowerBound;
321
322   DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
323              int64_t LowerBound)
324       : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
325         Count(Count), LowerBound(LowerBound) {}
326   ~DISubrange() = default;
327
328   static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
329                              int64_t LowerBound, StorageType Storage,
330                              bool ShouldCreate = true);
331
332   TempDISubrange cloneImpl() const {
333     return getTemporary(getContext(), getCount(), getLowerBound());
334   }
335
336 public:
337   DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
338                     (Count, LowerBound))
339
340   TempDISubrange clone() const { return cloneImpl(); }
341
342   int64_t getLowerBound() const { return LowerBound; }
343   int64_t getCount() const { return Count; }
344
345   static bool classof(const Metadata *MD) {
346     return MD->getMetadataID() == DISubrangeKind;
347   }
348 };
349
350 /// \brief Enumeration value.
351 ///
352 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
353 /// longer creates a type cycle.
354 class DIEnumerator : public DINode {
355   friend class LLVMContextImpl;
356   friend class MDNode;
357
358   int64_t Value;
359
360   DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
361                ArrayRef<Metadata *> Ops)
362       : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
363         Value(Value) {}
364   ~DIEnumerator() = default;
365
366   static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
367                                StringRef Name, StorageType Storage,
368                                bool ShouldCreate = true) {
369     return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
370                    ShouldCreate);
371   }
372   static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
373                                MDString *Name, StorageType Storage,
374                                bool ShouldCreate = true);
375
376   TempDIEnumerator cloneImpl() const {
377     return getTemporary(getContext(), getValue(), getName());
378   }
379
380 public:
381   DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
382                     (Value, Name))
383   DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
384                     (Value, Name))
385
386   TempDIEnumerator clone() const { return cloneImpl(); }
387
388   int64_t getValue() const { return Value; }
389   StringRef getName() const { return getStringOperand(0); }
390
391   MDString *getRawName() const { return getOperandAs<MDString>(0); }
392
393   static bool classof(const Metadata *MD) {
394     return MD->getMetadataID() == DIEnumeratorKind;
395   }
396 };
397
398 /// \brief Base class for scope-like contexts.
399 ///
400 /// Base class for lexical scopes and types (which are also declaration
401 /// contexts).
402 ///
403 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
404 class DIScope : public DINode {
405 protected:
406   DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
407           ArrayRef<Metadata *> Ops)
408       : DINode(C, ID, Storage, Tag, Ops) {}
409   ~DIScope() = default;
410
411 public:
412   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
413
414   inline StringRef getFilename() const;
415   inline StringRef getDirectory() const;
416
417   StringRef getName() const;
418   DIScopeRef getScope() const;
419
420   /// \brief Return the raw underlying file.
421   ///
422   /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
423   /// (it\em is the file).  If \c this is an \a DIFile, we need to return \c
424   /// this.  Otherwise, return the first operand, which is where all other
425   /// subclasses store their file pointer.
426   Metadata *getRawFile() const {
427     return isa<DIFile>(this) ? const_cast<DIScope *>(this)
428                              : static_cast<Metadata *>(getOperand(0));
429   }
430
431   DIScopeRef getRef() const { return DIScopeRef::get(this); }
432
433   static bool classof(const Metadata *MD) {
434     switch (MD->getMetadataID()) {
435     default:
436       return false;
437     case DIBasicTypeKind:
438     case DIDerivedTypeKind:
439     case DICompositeTypeKind:
440     case DISubroutineTypeKind:
441     case DIFileKind:
442     case DICompileUnitKind:
443     case DISubprogramKind:
444     case DILexicalBlockKind:
445     case DILexicalBlockFileKind:
446     case DINamespaceKind:
447     case DIModuleKind:
448       return true;
449     }
450   }
451 };
452
453 /// \brief File.
454 ///
455 /// TODO: Merge with directory/file node (including users).
456 /// TODO: Canonicalize paths on creation.
457 class DIFile : public DIScope {
458   friend class LLVMContextImpl;
459   friend class MDNode;
460
461   DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
462       : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
463   ~DIFile() = default;
464
465   static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
466                          StringRef Directory, StorageType Storage,
467                          bool ShouldCreate = true) {
468     return getImpl(Context, getCanonicalMDString(Context, Filename),
469                    getCanonicalMDString(Context, Directory), Storage,
470                    ShouldCreate);
471   }
472   static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
473                          MDString *Directory, StorageType Storage,
474                          bool ShouldCreate = true);
475
476   TempDIFile cloneImpl() const {
477     return getTemporary(getContext(), getFilename(), getDirectory());
478   }
479
480 public:
481   DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
482                     (Filename, Directory))
483   DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
484                     (Filename, Directory))
485
486   TempDIFile clone() const { return cloneImpl(); }
487
488   StringRef getFilename() const { return getStringOperand(0); }
489   StringRef getDirectory() const { return getStringOperand(1); }
490
491   MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
492   MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
493
494   static bool classof(const Metadata *MD) {
495     return MD->getMetadataID() == DIFileKind;
496   }
497 };
498
499 StringRef DIScope::getFilename() const {
500   if (auto *F = getFile())
501     return F->getFilename();
502   return "";
503 }
504
505 StringRef DIScope::getDirectory() const {
506   if (auto *F = getFile())
507     return F->getDirectory();
508   return "";
509 }
510
511 /// \brief Base class for types.
512 ///
513 /// TODO: Remove the hardcoded name and context, since many types don't use
514 /// them.
515 /// TODO: Split up flags.
516 class DIType : public DIScope {
517   unsigned Line;
518   unsigned Flags;
519   uint64_t SizeInBits;
520   uint64_t AlignInBits;
521   uint64_t OffsetInBits;
522
523 protected:
524   DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
525          unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
526          uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
527       : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
528         SizeInBits(SizeInBits), AlignInBits(AlignInBits),
529         OffsetInBits(OffsetInBits) {}
530   ~DIType() = default;
531
532 public:
533   TempDIType clone() const {
534     return TempDIType(cast<DIType>(MDNode::clone().release()));
535   }
536
537   unsigned getLine() const { return Line; }
538   uint64_t getSizeInBits() const { return SizeInBits; }
539   uint64_t getAlignInBits() const { return AlignInBits; }
540   uint64_t getOffsetInBits() const { return OffsetInBits; }
541   unsigned getFlags() const { return Flags; }
542
543   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
544   StringRef getName() const { return getStringOperand(2); }
545
546
547   Metadata *getRawScope() const { return getOperand(1); }
548   MDString *getRawName() const { return getOperandAs<MDString>(2); }
549
550   void setFlags(unsigned NewFlags) {
551     assert(!isUniqued() && "Cannot set flags on uniqued nodes");
552     Flags = NewFlags;
553   }
554
555   bool isPrivate() const {
556     return (getFlags() & FlagAccessibility) == FlagPrivate;
557   }
558   bool isProtected() const {
559     return (getFlags() & FlagAccessibility) == FlagProtected;
560   }
561   bool isPublic() const {
562     return (getFlags() & FlagAccessibility) == FlagPublic;
563   }
564   bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
565   bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
566   bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
567   bool isVirtual() const { return getFlags() & FlagVirtual; }
568   bool isArtificial() const { return getFlags() & FlagArtificial; }
569   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
570   bool isObjcClassComplete() const {
571     return getFlags() & FlagObjcClassComplete;
572   }
573   bool isVector() const { return getFlags() & FlagVector; }
574   bool isStaticMember() const { return getFlags() & FlagStaticMember; }
575   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
576   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
577   bool isExternalTypeRef() const { return getFlags() & FlagExternalTypeRef; }
578
579   DITypeRef getRef() const { return DITypeRef::get(this); }
580
581   static bool classof(const Metadata *MD) {
582     switch (MD->getMetadataID()) {
583     default:
584       return false;
585     case DIBasicTypeKind:
586     case DIDerivedTypeKind:
587     case DICompositeTypeKind:
588     case DISubroutineTypeKind:
589       return true;
590     }
591   }
592 };
593
594 /// \brief Basic type, like 'int' or 'float'.
595 ///
596 /// TODO: Split out DW_TAG_unspecified_type.
597 /// TODO: Drop unused accessors.
598 class DIBasicType : public DIType {
599   friend class LLVMContextImpl;
600   friend class MDNode;
601
602   unsigned Encoding;
603
604   DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
605               uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
606               ArrayRef<Metadata *> Ops)
607       : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
608                0, Ops),
609         Encoding(Encoding) {}
610   ~DIBasicType() = default;
611
612   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
613                               StringRef Name, uint64_t SizeInBits,
614                               uint64_t AlignInBits, unsigned Encoding,
615                               StorageType Storage, bool ShouldCreate = true) {
616     return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
617                    SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
618   }
619   static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
620                               MDString *Name, uint64_t SizeInBits,
621                               uint64_t AlignInBits, unsigned Encoding,
622                               StorageType Storage, bool ShouldCreate = true);
623
624   TempDIBasicType cloneImpl() const {
625     return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
626                         getAlignInBits(), getEncoding());
627   }
628
629 public:
630   DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
631                     (Tag, Name, 0, 0, 0))
632   DEFINE_MDNODE_GET(DIBasicType,
633                     (unsigned Tag, StringRef Name, uint64_t SizeInBits,
634                      uint64_t AlignInBits, unsigned Encoding),
635                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
636   DEFINE_MDNODE_GET(DIBasicType,
637                     (unsigned Tag, MDString *Name, uint64_t SizeInBits,
638                      uint64_t AlignInBits, unsigned Encoding),
639                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
640
641   TempDIBasicType clone() const { return cloneImpl(); }
642
643   unsigned getEncoding() const { return Encoding; }
644
645   static bool classof(const Metadata *MD) {
646     return MD->getMetadataID() == DIBasicTypeKind;
647   }
648 };
649
650 /// \brief Derived types.
651 ///
652 /// This includes qualified types, pointers, references, friends, typedefs, and
653 /// class members.
654 ///
655 /// TODO: Split out members (inheritance, fields, methods, etc.).
656 class DIDerivedType : public DIType {
657   friend class LLVMContextImpl;
658   friend class MDNode;
659
660   DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
661                 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
662                 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
663       : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
664                AlignInBits, OffsetInBits, Flags, Ops) {}
665   ~DIDerivedType() = default;
666
667   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
668                                 StringRef Name, DIFile *File, unsigned Line,
669                                 DIScopeRef Scope, DITypeRef BaseType,
670                                 uint64_t SizeInBits, uint64_t AlignInBits,
671                                 uint64_t OffsetInBits, unsigned Flags,
672                                 Metadata *ExtraData, StorageType Storage,
673                                 bool ShouldCreate = true) {
674     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
675                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
676                    Flags, ExtraData, Storage, ShouldCreate);
677   }
678   static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
679                                 MDString *Name, Metadata *File, unsigned Line,
680                                 Metadata *Scope, Metadata *BaseType,
681                                 uint64_t SizeInBits, uint64_t AlignInBits,
682                                 uint64_t OffsetInBits, unsigned Flags,
683                                 Metadata *ExtraData, StorageType Storage,
684                                 bool ShouldCreate = true);
685
686   TempDIDerivedType cloneImpl() const {
687     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
688                         getScope(), getBaseType(), getSizeInBits(),
689                         getAlignInBits(), getOffsetInBits(), getFlags(),
690                         getExtraData());
691   }
692
693 public:
694   DEFINE_MDNODE_GET(DIDerivedType,
695                     (unsigned Tag, MDString *Name, Metadata *File,
696                      unsigned Line, Metadata *Scope, Metadata *BaseType,
697                      uint64_t SizeInBits, uint64_t AlignInBits,
698                      uint64_t OffsetInBits, unsigned Flags,
699                      Metadata *ExtraData = nullptr),
700                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
701                      AlignInBits, OffsetInBits, Flags, ExtraData))
702   DEFINE_MDNODE_GET(DIDerivedType,
703                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
704                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
705                      uint64_t AlignInBits, uint64_t OffsetInBits,
706                      unsigned Flags, Metadata *ExtraData = nullptr),
707                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
708                      AlignInBits, OffsetInBits, Flags, ExtraData))
709
710   TempDIDerivedType clone() const { return cloneImpl(); }
711
712   //// Get the base type this is derived from.
713   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
714   Metadata *getRawBaseType() const { return getOperand(3); }
715
716   /// \brief Get extra data associated with this derived type.
717   ///
718   /// Class type for pointer-to-members, objective-c property node for ivars,
719   /// or global constant wrapper for static members.
720   ///
721   /// TODO: Separate out types that need this extra operand: pointer-to-member
722   /// types and member fields (static members and ivars).
723   Metadata *getExtraData() const { return getRawExtraData(); }
724   Metadata *getRawExtraData() const { return getOperand(4); }
725
726   /// \brief Get casted version of extra data.
727   /// @{
728   DITypeRef getClassType() const {
729     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
730     return DITypeRef(getExtraData());
731   }
732   DIObjCProperty *getObjCProperty() const {
733     return dyn_cast_or_null<DIObjCProperty>(getExtraData());
734   }
735   Constant *getConstant() const {
736     assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
737     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
738       return C->getValue();
739     return nullptr;
740   }
741   /// @}
742
743   static bool classof(const Metadata *MD) {
744     return MD->getMetadataID() == DIDerivedTypeKind;
745   }
746 };
747
748 /// \brief Composite types.
749 ///
750 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
751 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
752 class DICompositeType : public DIType {
753   friend class LLVMContextImpl;
754   friend class MDNode;
755
756   unsigned RuntimeLang;
757
758   DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
759                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
760                   uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
761                   ArrayRef<Metadata *> Ops)
762       : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
763                AlignInBits, OffsetInBits, Flags, Ops),
764         RuntimeLang(RuntimeLang) {}
765   ~DICompositeType() = default;
766
767   static DICompositeType *
768   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
769           unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
770           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
771           uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
772           DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
773           StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
774     return getImpl(
775         Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
776         BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
777         RuntimeLang, VTableHolder, TemplateParams.get(),
778         getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
779   }
780   static DICompositeType *
781   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
782           unsigned Line, Metadata *Scope, Metadata *BaseType,
783           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
784           unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
785           Metadata *VTableHolder, Metadata *TemplateParams,
786           MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
787
788   TempDICompositeType cloneImpl() const {
789     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
790                         getScope(), getBaseType(), getSizeInBits(),
791                         getAlignInBits(), getOffsetInBits(), getFlags(),
792                         getElements(), getRuntimeLang(), getVTableHolder(),
793                         getTemplateParams(), getIdentifier());
794   }
795
796 public:
797   DEFINE_MDNODE_GET(DICompositeType,
798                     (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
799                      DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
800                      uint64_t AlignInBits, uint64_t OffsetInBits,
801                      unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
802                      DITypeRef VTableHolder,
803                      DITemplateParameterArray TemplateParams = nullptr,
804                      StringRef Identifier = ""),
805                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
806                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
807                      VTableHolder, TemplateParams, Identifier))
808   DEFINE_MDNODE_GET(DICompositeType,
809                     (unsigned Tag, MDString *Name, Metadata *File,
810                      unsigned Line, Metadata *Scope, Metadata *BaseType,
811                      uint64_t SizeInBits, uint64_t AlignInBits,
812                      uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
813                      unsigned RuntimeLang, Metadata *VTableHolder,
814                      Metadata *TemplateParams = nullptr,
815                      MDString *Identifier = nullptr),
816                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
817                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
818                      VTableHolder, TemplateParams, Identifier))
819
820   TempDICompositeType clone() const { return cloneImpl(); }
821
822   DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
823   DINodeArray getElements() const {
824     return cast_or_null<MDTuple>(getRawElements());
825   }
826   DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
827   DITemplateParameterArray getTemplateParams() const {
828     return cast_or_null<MDTuple>(getRawTemplateParams());
829   }
830   StringRef getIdentifier() const { return getStringOperand(7); }
831   unsigned getRuntimeLang() const { return RuntimeLang; }
832
833   Metadata *getRawBaseType() const { return getOperand(3); }
834   Metadata *getRawElements() const { return getOperand(4); }
835   Metadata *getRawVTableHolder() const { return getOperand(5); }
836   Metadata *getRawTemplateParams() const { return getOperand(6); }
837   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
838
839   /// \brief Replace operands.
840   ///
841   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
842   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
843   /// of its movement if necessary.
844   /// @{
845   void replaceElements(DINodeArray Elements) {
846 #ifndef NDEBUG
847     for (DINode *Op : getElements())
848       assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
849              "Lost a member during member list replacement");
850 #endif
851     replaceOperandWith(4, Elements.get());
852   }
853   void replaceVTableHolder(DITypeRef VTableHolder) {
854     replaceOperandWith(5, VTableHolder);
855   }
856   void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
857     replaceOperandWith(6, TemplateParams.get());
858   }
859   /// @}
860
861   static bool classof(const Metadata *MD) {
862     return MD->getMetadataID() == DICompositeTypeKind;
863   }
864 };
865
866 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
867   if (N)
868     if (auto *Composite = dyn_cast<DICompositeType>(N))
869       if (auto *S = Composite->getRawIdentifier())
870         return TypedDINodeRef<T>(S);
871   return TypedDINodeRef<T>(N);
872 }
873
874 /// \brief Type array for a subprogram.
875 ///
876 /// TODO: Fold the array of types in directly as operands.
877 class DISubroutineType : public DIType {
878   friend class LLVMContextImpl;
879   friend class MDNode;
880
881   DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
882                    ArrayRef<Metadata *> Ops)
883       : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
884                0, 0, 0, 0, Flags, Ops) {}
885   ~DISubroutineType() = default;
886
887   static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
888                                    DITypeRefArray TypeArray,
889                                    StorageType Storage,
890                                    bool ShouldCreate = true) {
891     return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
892   }
893   static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
894                                    Metadata *TypeArray, StorageType Storage,
895                                    bool ShouldCreate = true);
896
897   TempDISubroutineType cloneImpl() const {
898     return getTemporary(getContext(), getFlags(), getTypeArray());
899   }
900
901 public:
902   DEFINE_MDNODE_GET(DISubroutineType,
903                     (unsigned Flags, DITypeRefArray TypeArray),
904                     (Flags, TypeArray))
905   DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
906                     (Flags, TypeArray))
907
908   TempDISubroutineType clone() const { return cloneImpl(); }
909
910   DITypeRefArray getTypeArray() const {
911     return cast_or_null<MDTuple>(getRawTypeArray());
912   }
913   Metadata *getRawTypeArray() const { return getOperand(3); }
914
915   static bool classof(const Metadata *MD) {
916     return MD->getMetadataID() == DISubroutineTypeKind;
917   }
918 };
919
920 /// \brief Compile unit.
921 class DICompileUnit : public DIScope {
922   friend class LLVMContextImpl;
923   friend class MDNode;
924
925   unsigned SourceLanguage;
926   bool IsOptimized;
927   unsigned RuntimeVersion;
928   unsigned EmissionKind;
929   uint64_t DWOId;
930
931   DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
932                 bool IsOptimized, unsigned RuntimeVersion,
933                 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
934       : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
935         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
936         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
937         DWOId(DWOId) {}
938   ~DICompileUnit() = default;
939
940   static DICompileUnit *
941   getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
942           StringRef Producer, bool IsOptimized, StringRef Flags,
943           unsigned RuntimeVersion, StringRef SplitDebugFilename,
944           unsigned EmissionKind, DICompositeTypeArray EnumTypes,
945           DITypeArray RetainedTypes, DISubprogramArray Subprograms,
946           DIGlobalVariableArray GlobalVariables,
947           DIImportedEntityArray ImportedEntities, uint64_t DWOId,
948           StorageType Storage, bool ShouldCreate = true) {
949     return getImpl(Context, SourceLanguage, File,
950                    getCanonicalMDString(Context, Producer), IsOptimized,
951                    getCanonicalMDString(Context, Flags), RuntimeVersion,
952                    getCanonicalMDString(Context, SplitDebugFilename),
953                    EmissionKind, EnumTypes.get(), RetainedTypes.get(),
954                    Subprograms.get(), GlobalVariables.get(),
955                    ImportedEntities.get(), DWOId, Storage, ShouldCreate);
956   }
957   static DICompileUnit *
958   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
959           MDString *Producer, bool IsOptimized, MDString *Flags,
960           unsigned RuntimeVersion, MDString *SplitDebugFilename,
961           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
962           Metadata *Subprograms, Metadata *GlobalVariables,
963           Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
964           bool ShouldCreate = true);
965
966   TempDICompileUnit cloneImpl() const {
967     return getTemporary(
968         getContext(), getSourceLanguage(), getFile(), getProducer(),
969         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
970         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
971         getGlobalVariables(), getImportedEntities(), DWOId);
972   }
973
974 public:
975   DEFINE_MDNODE_GET(DICompileUnit,
976                     (unsigned SourceLanguage, DIFile *File, StringRef Producer,
977                      bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
978                      StringRef SplitDebugFilename, unsigned EmissionKind,
979                      DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
980                      DISubprogramArray Subprograms,
981                      DIGlobalVariableArray GlobalVariables,
982                      DIImportedEntityArray ImportedEntities, uint64_t DWOId),
983                     (SourceLanguage, File, Producer, IsOptimized, Flags,
984                      RuntimeVersion, SplitDebugFilename, EmissionKind,
985                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
986                      ImportedEntities, DWOId))
987   DEFINE_MDNODE_GET(
988       DICompileUnit,
989       (unsigned SourceLanguage, Metadata *File, MDString *Producer,
990        bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
991        MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
992        Metadata *RetainedTypes, Metadata *Subprograms,
993        Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
994       (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
995        SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
996        GlobalVariables, ImportedEntities, DWOId))
997
998   TempDICompileUnit clone() const { return cloneImpl(); }
999
1000   unsigned getSourceLanguage() const { return SourceLanguage; }
1001   bool isOptimized() const { return IsOptimized; }
1002   unsigned getRuntimeVersion() const { return RuntimeVersion; }
1003   unsigned getEmissionKind() const { return EmissionKind; }
1004   StringRef getProducer() const { return getStringOperand(1); }
1005   StringRef getFlags() const { return getStringOperand(2); }
1006   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1007   DICompositeTypeArray getEnumTypes() const {
1008     return cast_or_null<MDTuple>(getRawEnumTypes());
1009   }
1010   DITypeArray getRetainedTypes() const {
1011     return cast_or_null<MDTuple>(getRawRetainedTypes());
1012   }
1013   DISubprogramArray getSubprograms() const {
1014     return cast_or_null<MDTuple>(getRawSubprograms());
1015   }
1016   DIGlobalVariableArray getGlobalVariables() const {
1017     return cast_or_null<MDTuple>(getRawGlobalVariables());
1018   }
1019   DIImportedEntityArray getImportedEntities() const {
1020     return cast_or_null<MDTuple>(getRawImportedEntities());
1021   }
1022   unsigned getDWOId() const { return DWOId; }
1023
1024   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1025   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1026   MDString *getRawSplitDebugFilename() const {
1027     return getOperandAs<MDString>(3);
1028   }
1029   Metadata *getRawEnumTypes() const { return getOperand(4); }
1030   Metadata *getRawRetainedTypes() const { return getOperand(5); }
1031   Metadata *getRawSubprograms() const { return getOperand(6); }
1032   Metadata *getRawGlobalVariables() const { return getOperand(7); }
1033   Metadata *getRawImportedEntities() const { return getOperand(8); }
1034
1035   /// \brief Replace arrays.
1036   ///
1037   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1038   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
1039   /// DICompileUnit should be fairly rare.
1040   /// @{
1041   void replaceEnumTypes(DICompositeTypeArray N) {
1042     replaceOperandWith(4, N.get());
1043   }
1044   void replaceRetainedTypes(DITypeArray N) {
1045     replaceOperandWith(5, N.get());
1046   }
1047   void replaceSubprograms(DISubprogramArray N) {
1048     replaceOperandWith(6, N.get());
1049   }
1050   void replaceGlobalVariables(DIGlobalVariableArray N) {
1051     replaceOperandWith(7, N.get());
1052   }
1053   void replaceImportedEntities(DIImportedEntityArray N) {
1054     replaceOperandWith(8, N.get());
1055   }
1056   /// @}
1057
1058   static bool classof(const Metadata *MD) {
1059     return MD->getMetadataID() == DICompileUnitKind;
1060   }
1061 };
1062
1063 /// \brief A scope for locals.
1064 ///
1065 /// A legal scope for lexical blocks, local variables, and debug info
1066 /// locations.  Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1067 /// DILexicalBlockFile.
1068 class DILocalScope : public DIScope {
1069 protected:
1070   DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1071                ArrayRef<Metadata *> Ops)
1072       : DIScope(C, ID, Storage, Tag, Ops) {}
1073   ~DILocalScope() = default;
1074
1075 public:
1076   /// \brief Get the subprogram for this scope.
1077   ///
1078   /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1079   /// chain.
1080   DISubprogram *getSubprogram() const;
1081
1082   static bool classof(const Metadata *MD) {
1083     return MD->getMetadataID() == DISubprogramKind ||
1084            MD->getMetadataID() == DILexicalBlockKind ||
1085            MD->getMetadataID() == DILexicalBlockFileKind;
1086   }
1087 };
1088
1089 /// \brief Debug location.
1090 ///
1091 /// A debug location in source code, used for debug info and otherwise.
1092 class DILocation : public MDNode {
1093   friend class LLVMContextImpl;
1094   friend class MDNode;
1095
1096   DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1097              unsigned Column, ArrayRef<Metadata *> MDs);
1098   ~DILocation() { dropAllReferences(); }
1099
1100   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1101                              unsigned Column, Metadata *Scope,
1102                              Metadata *InlinedAt, StorageType Storage,
1103                              bool ShouldCreate = true);
1104   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1105                              unsigned Column, DILocalScope *Scope,
1106                              DILocation *InlinedAt, StorageType Storage,
1107                              bool ShouldCreate = true) {
1108     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1109                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1110   }
1111
1112   TempDILocation cloneImpl() const {
1113     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1114                         getInlinedAt());
1115   }
1116
1117   // Disallow replacing operands.
1118   void replaceOperandWith(unsigned I, Metadata *New) = delete;
1119
1120 public:
1121   DEFINE_MDNODE_GET(DILocation,
1122                     (unsigned Line, unsigned Column, Metadata *Scope,
1123                      Metadata *InlinedAt = nullptr),
1124                     (Line, Column, Scope, InlinedAt))
1125   DEFINE_MDNODE_GET(DILocation,
1126                     (unsigned Line, unsigned Column, DILocalScope *Scope,
1127                      DILocation *InlinedAt = nullptr),
1128                     (Line, Column, Scope, InlinedAt))
1129
1130   /// \brief Return a (temporary) clone of this.
1131   TempDILocation clone() const { return cloneImpl(); }
1132
1133   unsigned getLine() const { return SubclassData32; }
1134   unsigned getColumn() const { return SubclassData16; }
1135   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1136   DILocation *getInlinedAt() const {
1137     return cast_or_null<DILocation>(getRawInlinedAt());
1138   }
1139
1140   DIFile *getFile() const { return getScope()->getFile(); }
1141   StringRef getFilename() const { return getScope()->getFilename(); }
1142   StringRef getDirectory() const { return getScope()->getDirectory(); }
1143
1144   /// \brief Get the scope where this is inlined.
1145   ///
1146   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1147   /// location.
1148   DILocalScope *getInlinedAtScope() const {
1149     if (auto *IA = getInlinedAt())
1150       return IA->getInlinedAtScope();
1151     return getScope();
1152   }
1153
1154   /// \brief Check whether this can be discriminated from another location.
1155   ///
1156   /// Check \c this can be discriminated from \c RHS in a linetable entry.
1157   /// Scope and inlined-at chains are not recorded in the linetable, so they
1158   /// cannot be used to distinguish basic blocks.
1159   ///
1160   /// The current implementation is weaker than it should be, since it just
1161   /// checks filename and line.
1162   ///
1163   /// FIXME: Add a check for getDiscriminator().
1164   /// FIXME: Add a check for getColumn().
1165   /// FIXME: Change the getFilename() check to getFile() (or add one for
1166   /// getDirectory()).
1167   bool canDiscriminate(const DILocation &RHS) const {
1168     return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1169   }
1170
1171   /// \brief Get the DWARF discriminator.
1172   ///
1173   /// DWARF discriminators distinguish identical file locations between
1174   /// instructions that are on different basic blocks.
1175   inline unsigned getDiscriminator() const;
1176
1177   /// \brief Compute new discriminator in the given context.
1178   ///
1179   /// This modifies the \a LLVMContext that \c this is in to increment the next
1180   /// discriminator for \c this's line/filename combination.
1181   ///
1182   /// FIXME: Delete this.  See comments in implementation and at the only call
1183   /// site in \a AddDiscriminators::runOnFunction().
1184   unsigned computeNewDiscriminator() const;
1185
1186   Metadata *getRawScope() const { return getOperand(0); }
1187   Metadata *getRawInlinedAt() const {
1188     if (getNumOperands() == 2)
1189       return getOperand(1);
1190     return nullptr;
1191   }
1192
1193   static bool classof(const Metadata *MD) {
1194     return MD->getMetadataID() == DILocationKind;
1195   }
1196 };
1197
1198 /// \brief Subprogram description.
1199 ///
1200 /// TODO: Remove DisplayName.  It's always equal to Name.
1201 /// TODO: Split up flags.
1202 class DISubprogram : public DILocalScope {
1203   friend class LLVMContextImpl;
1204   friend class MDNode;
1205
1206   unsigned Line;
1207   unsigned ScopeLine;
1208   unsigned Virtuality;
1209   unsigned VirtualIndex;
1210   unsigned Flags;
1211   bool IsLocalToUnit;
1212   bool IsDefinition;
1213   bool IsOptimized;
1214
1215   DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1216                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1217                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1218                bool IsOptimized, ArrayRef<Metadata *> Ops)
1219       : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1220                      Ops),
1221         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1222         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1223         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1224   ~DISubprogram() = default;
1225
1226   static DISubprogram *
1227   getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1228           StringRef LinkageName, DIFile *File, unsigned Line,
1229           DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1230           unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1231           unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1232           Constant *Function, DITemplateParameterArray TemplateParams,
1233           DISubprogram *Declaration, DILocalVariableArray Variables,
1234           StorageType Storage, bool ShouldCreate = true) {
1235     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1236                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1237                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1238                    Virtuality, VirtualIndex, Flags, IsOptimized,
1239                    Function ? ConstantAsMetadata::get(Function) : nullptr,
1240                    TemplateParams.get(), Declaration, Variables.get(), Storage,
1241                    ShouldCreate);
1242   }
1243   static DISubprogram *
1244   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1245           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1246           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1247           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1248           unsigned Flags, bool IsOptimized, Metadata *Function,
1249           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1250           StorageType Storage, bool ShouldCreate = true);
1251
1252   TempDISubprogram cloneImpl() const {
1253     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1254                         getFile(), getLine(), getType(), isLocalToUnit(),
1255                         isDefinition(), getScopeLine(), getContainingType(),
1256                         getVirtuality(), getVirtualIndex(), getFlags(),
1257                         isOptimized(), getFunctionConstant(),
1258                         getTemplateParams(), getDeclaration(), getVariables());
1259   }
1260
1261 public:
1262   DEFINE_MDNODE_GET(DISubprogram,
1263                     (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1264                      DIFile *File, unsigned Line, DISubroutineType *Type,
1265                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1266                      DITypeRef ContainingType, unsigned Virtuality,
1267                      unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1268                      Constant *Function = nullptr,
1269                      DITemplateParameterArray TemplateParams = nullptr,
1270                      DISubprogram *Declaration = nullptr,
1271                      DILocalVariableArray Variables = nullptr),
1272                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1273                      IsDefinition, ScopeLine, ContainingType, Virtuality,
1274                      VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1275                      Declaration, Variables))
1276   DEFINE_MDNODE_GET(
1277       DISubprogram,
1278       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1279        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1280        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1281        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1282        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1283        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1284       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1285        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1286        Function, TemplateParams, Declaration, Variables))
1287
1288   TempDISubprogram clone() const { return cloneImpl(); }
1289
1290 public:
1291   unsigned getLine() const { return Line; }
1292   unsigned getVirtuality() const { return Virtuality; }
1293   unsigned getVirtualIndex() const { return VirtualIndex; }
1294   unsigned getScopeLine() const { return ScopeLine; }
1295   unsigned getFlags() const { return Flags; }
1296   bool isLocalToUnit() const { return IsLocalToUnit; }
1297   bool isDefinition() const { return IsDefinition; }
1298   bool isOptimized() const { return IsOptimized; }
1299
1300   unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1301   bool isPrivate() const {
1302     return (getFlags() & FlagAccessibility) == FlagPrivate;
1303   }
1304   bool isProtected() const {
1305     return (getFlags() & FlagAccessibility) == FlagProtected;
1306   }
1307   bool isPublic() const {
1308     return (getFlags() & FlagAccessibility) == FlagPublic;
1309   }
1310   bool isExplicit() const { return getFlags() & FlagExplicit; }
1311   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1312
1313   /// \brief Check if this is reference-qualified.
1314   ///
1315   /// Return true if this subprogram is a C++11 reference-qualified non-static
1316   /// member function (void foo() &).
1317   unsigned isLValueReference() const {
1318     return getFlags() & FlagLValueReference;
1319   }
1320
1321   /// \brief Check if this is rvalue-reference-qualified.
1322   ///
1323   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1324   /// non-static member function (void foo() &&).
1325   unsigned isRValueReference() const {
1326     return getFlags() & FlagRValueReference;
1327   }
1328
1329   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1330
1331   StringRef getName() const { return getStringOperand(2); }
1332   StringRef getDisplayName() const { return getStringOperand(3); }
1333   StringRef getLinkageName() const { return getStringOperand(4); }
1334
1335   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1336   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1337
1338   DISubroutineType *getType() const {
1339     return cast_or_null<DISubroutineType>(getRawType());
1340   }
1341   DITypeRef getContainingType() const {
1342     return DITypeRef(getRawContainingType());
1343   }
1344
1345   Constant *getFunctionConstant() const {
1346     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1347       return C->getValue();
1348     return nullptr;
1349   }
1350   DITemplateParameterArray getTemplateParams() const {
1351     return cast_or_null<MDTuple>(getRawTemplateParams());
1352   }
1353   DISubprogram *getDeclaration() const {
1354     return cast_or_null<DISubprogram>(getRawDeclaration());
1355   }
1356   DILocalVariableArray getVariables() const {
1357     return cast_or_null<MDTuple>(getRawVariables());
1358   }
1359
1360   Metadata *getRawScope() const { return getOperand(1); }
1361   Metadata *getRawType() const { return getOperand(5); }
1362   Metadata *getRawContainingType() const { return getOperand(6); }
1363   Metadata *getRawFunction() const { return getOperand(7); }
1364   Metadata *getRawTemplateParams() const { return getOperand(8); }
1365   Metadata *getRawDeclaration() const { return getOperand(9); }
1366   Metadata *getRawVariables() const { return getOperand(10); }
1367
1368   /// \brief Get a pointer to the function this subprogram describes.
1369   ///
1370   /// This dyn_casts \a getFunctionConstant() to \a Function.
1371   ///
1372   /// FIXME: Should this be looking through bitcasts?
1373   Function *getFunction() const;
1374
1375   /// \brief Replace the function.
1376   ///
1377   /// If \a isUniqued() and not \a isResolved(), this could node will be
1378   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1379   /// that's a problem.
1380   /// @{
1381   void replaceFunction(Function *F);
1382   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1383   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1384   /// @}
1385
1386   /// \brief Check if this subprogram decribes the given function.
1387   ///
1388   /// FIXME: Should this be looking through bitcasts?
1389   bool describes(const Function *F) const;
1390
1391   static bool classof(const Metadata *MD) {
1392     return MD->getMetadataID() == DISubprogramKind;
1393   }
1394 };
1395
1396 class DILexicalBlockBase : public DILocalScope {
1397 protected:
1398   DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1399                      ArrayRef<Metadata *> Ops)
1400       : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1401   ~DILexicalBlockBase() = default;
1402
1403 public:
1404   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1405
1406   Metadata *getRawScope() const { return getOperand(1); }
1407
1408   /// \brief Forwarding accessors to LexicalBlock.
1409   ///
1410   /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1411   /// @{
1412   inline unsigned getLine() const;
1413   inline unsigned getColumn() const;
1414   /// @}
1415   static bool classof(const Metadata *MD) {
1416     return MD->getMetadataID() == DILexicalBlockKind ||
1417            MD->getMetadataID() == DILexicalBlockFileKind;
1418   }
1419 };
1420
1421 class DILexicalBlock : public DILexicalBlockBase {
1422   friend class LLVMContextImpl;
1423   friend class MDNode;
1424
1425   unsigned Line;
1426   unsigned Column;
1427
1428   DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1429                  unsigned Column, ArrayRef<Metadata *> Ops)
1430       : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1431         Column(Column) {}
1432   ~DILexicalBlock() = default;
1433
1434   static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1435                                  DIFile *File, unsigned Line, unsigned Column,
1436                                  StorageType Storage,
1437                                  bool ShouldCreate = true) {
1438     return getImpl(Context, static_cast<Metadata *>(Scope),
1439                    static_cast<Metadata *>(File), Line, Column, Storage,
1440                    ShouldCreate);
1441   }
1442
1443   static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1444                                  Metadata *File, unsigned Line, unsigned Column,
1445                                  StorageType Storage, bool ShouldCreate = true);
1446
1447   TempDILexicalBlock cloneImpl() const {
1448     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1449                         getColumn());
1450   }
1451
1452 public:
1453   DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1454                                      unsigned Line, unsigned Column),
1455                     (Scope, File, Line, Column))
1456   DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1457                                      unsigned Line, unsigned Column),
1458                     (Scope, File, Line, Column))
1459
1460   TempDILexicalBlock clone() const { return cloneImpl(); }
1461
1462   unsigned getLine() const { return Line; }
1463   unsigned getColumn() const { return Column; }
1464
1465   static bool classof(const Metadata *MD) {
1466     return MD->getMetadataID() == DILexicalBlockKind;
1467   }
1468 };
1469
1470 unsigned DILexicalBlockBase::getLine() const {
1471   if (auto *N = dyn_cast<DILexicalBlock>(this))
1472     return N->getLine();
1473   return 0;
1474 }
1475
1476 unsigned DILexicalBlockBase::getColumn() const {
1477   if (auto *N = dyn_cast<DILexicalBlock>(this))
1478     return N->getColumn();
1479   return 0;
1480 }
1481
1482 class DILexicalBlockFile : public DILexicalBlockBase {
1483   friend class LLVMContextImpl;
1484   friend class MDNode;
1485
1486   unsigned Discriminator;
1487
1488   DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1489                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1490       : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1491         Discriminator(Discriminator) {}
1492   ~DILexicalBlockFile() = default;
1493
1494   static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1495                                      DIFile *File, unsigned Discriminator,
1496                                      StorageType Storage,
1497                                      bool ShouldCreate = true) {
1498     return getImpl(Context, static_cast<Metadata *>(Scope),
1499                    static_cast<Metadata *>(File), Discriminator, Storage,
1500                    ShouldCreate);
1501   }
1502
1503   static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1504                                      Metadata *File, unsigned Discriminator,
1505                                      StorageType Storage,
1506                                      bool ShouldCreate = true);
1507
1508   TempDILexicalBlockFile cloneImpl() const {
1509     return getTemporary(getContext(), getScope(), getFile(),
1510                         getDiscriminator());
1511   }
1512
1513 public:
1514   DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1515                                          unsigned Discriminator),
1516                     (Scope, File, Discriminator))
1517   DEFINE_MDNODE_GET(DILexicalBlockFile,
1518                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1519                     (Scope, File, Discriminator))
1520
1521   TempDILexicalBlockFile clone() const { return cloneImpl(); }
1522
1523   // TODO: Remove these once they're gone from DILexicalBlockBase.
1524   unsigned getLine() const = delete;
1525   unsigned getColumn() const = delete;
1526
1527   unsigned getDiscriminator() const { return Discriminator; }
1528
1529   static bool classof(const Metadata *MD) {
1530     return MD->getMetadataID() == DILexicalBlockFileKind;
1531   }
1532 };
1533
1534 unsigned DILocation::getDiscriminator() const {
1535   if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1536     return F->getDiscriminator();
1537   return 0;
1538 }
1539
1540 class DINamespace : public DIScope {
1541   friend class LLVMContextImpl;
1542   friend class MDNode;
1543
1544   unsigned Line;
1545
1546   DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1547               ArrayRef<Metadata *> Ops)
1548       : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1549                 Ops),
1550         Line(Line) {}
1551   ~DINamespace() = default;
1552
1553   static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1554                               DIFile *File, StringRef Name, unsigned Line,
1555                               StorageType Storage, bool ShouldCreate = true) {
1556     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1557                    Line, Storage, ShouldCreate);
1558   }
1559   static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1560                               Metadata *File, MDString *Name, unsigned Line,
1561                               StorageType Storage, bool ShouldCreate = true);
1562
1563   TempDINamespace cloneImpl() const {
1564     return getTemporary(getContext(), getScope(), getFile(), getName(),
1565                         getLine());
1566   }
1567
1568 public:
1569   DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1570                                   unsigned Line),
1571                     (Scope, File, Name, Line))
1572   DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1573                                   MDString *Name, unsigned Line),
1574                     (Scope, File, Name, Line))
1575
1576   TempDINamespace clone() const { return cloneImpl(); }
1577
1578   unsigned getLine() const { return Line; }
1579   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1580   StringRef getName() const { return getStringOperand(2); }
1581
1582   Metadata *getRawScope() const { return getOperand(1); }
1583   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1584
1585   static bool classof(const Metadata *MD) {
1586     return MD->getMetadataID() == DINamespaceKind;
1587   }
1588 };
1589
1590 /// \brief A (clang) module that has been imported by the compile unit.
1591 ///
1592 class DIModule : public DIScope {
1593   friend class LLVMContextImpl;
1594   friend class MDNode;
1595
1596   DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1597       : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1598   ~DIModule() {}
1599
1600   static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1601                            StringRef Name, StringRef ConfigurationMacros,
1602                            StringRef IncludePath, StringRef ISysRoot,
1603                            StorageType Storage, bool ShouldCreate = true) {
1604     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1605                    getCanonicalMDString(Context, ConfigurationMacros),
1606                    getCanonicalMDString(Context, IncludePath),
1607                    getCanonicalMDString(Context, ISysRoot),
1608                    Storage, ShouldCreate);
1609   }
1610   static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1611                            MDString *Name, MDString *ConfigurationMacros,
1612                            MDString *IncludePath, MDString *ISysRoot,
1613                            StorageType Storage, bool ShouldCreate = true);
1614
1615   TempDIModule cloneImpl() const {
1616     return getTemporary(getContext(), getScope(), getName(),
1617                         getConfigurationMacros(), getIncludePath(),
1618                         getISysRoot());
1619   }
1620
1621 public:
1622   DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1623                                StringRef ConfigurationMacros, StringRef IncludePath,
1624                                StringRef ISysRoot),
1625                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1626   DEFINE_MDNODE_GET(DIModule,
1627                     (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1628                      MDString *IncludePath, MDString *ISysRoot),
1629                     (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1630
1631   TempDIModule clone() const { return cloneImpl(); }
1632
1633   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1634   StringRef getName() const { return getStringOperand(1); }
1635   StringRef getConfigurationMacros() const { return getStringOperand(2); }
1636   StringRef getIncludePath() const { return getStringOperand(3); }
1637   StringRef getISysRoot() const { return getStringOperand(4); }
1638
1639   Metadata *getRawScope() const { return getOperand(0); }
1640   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1641   MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1642   MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1643   MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1644
1645   static bool classof(const Metadata *MD) {
1646     return MD->getMetadataID() == DIModuleKind;
1647   }
1648 };
1649
1650 /// \brief Base class for template parameters.
1651 class DITemplateParameter : public DINode {
1652 protected:
1653   DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1654                       unsigned Tag, ArrayRef<Metadata *> Ops)
1655       : DINode(Context, ID, Storage, Tag, Ops) {}
1656   ~DITemplateParameter() = default;
1657
1658 public:
1659   StringRef getName() const { return getStringOperand(0); }
1660   DITypeRef getType() const { return DITypeRef(getRawType()); }
1661
1662   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1663   Metadata *getRawType() const { return getOperand(1); }
1664
1665   static bool classof(const Metadata *MD) {
1666     return MD->getMetadataID() == DITemplateTypeParameterKind ||
1667            MD->getMetadataID() == DITemplateValueParameterKind;
1668   }
1669 };
1670
1671 class DITemplateTypeParameter : public DITemplateParameter {
1672   friend class LLVMContextImpl;
1673   friend class MDNode;
1674
1675   DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1676                           ArrayRef<Metadata *> Ops)
1677       : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1678                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1679   ~DITemplateTypeParameter() = default;
1680
1681   static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1682                                           DITypeRef Type, StorageType Storage,
1683                                           bool ShouldCreate = true) {
1684     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1685                    ShouldCreate);
1686   }
1687   static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1688                                           Metadata *Type, StorageType Storage,
1689                                           bool ShouldCreate = true);
1690
1691   TempDITemplateTypeParameter cloneImpl() const {
1692     return getTemporary(getContext(), getName(), getType());
1693   }
1694
1695 public:
1696   DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1697                     (Name, Type))
1698   DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1699                     (Name, Type))
1700
1701   TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1702
1703   static bool classof(const Metadata *MD) {
1704     return MD->getMetadataID() == DITemplateTypeParameterKind;
1705   }
1706 };
1707
1708 class DITemplateValueParameter : public DITemplateParameter {
1709   friend class LLVMContextImpl;
1710   friend class MDNode;
1711
1712   DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1713                            unsigned Tag, ArrayRef<Metadata *> Ops)
1714       : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1715                             Ops) {}
1716   ~DITemplateValueParameter() = default;
1717
1718   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1719                                            StringRef Name, DITypeRef Type,
1720                                            Metadata *Value, StorageType Storage,
1721                                            bool ShouldCreate = true) {
1722     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1723                    Value, Storage, ShouldCreate);
1724   }
1725   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1726                                            MDString *Name, Metadata *Type,
1727                                            Metadata *Value, StorageType Storage,
1728                                            bool ShouldCreate = true);
1729
1730   TempDITemplateValueParameter cloneImpl() const {
1731     return getTemporary(getContext(), getTag(), getName(), getType(),
1732                         getValue());
1733   }
1734
1735 public:
1736   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1737                                                DITypeRef Type, Metadata *Value),
1738                     (Tag, Name, Type, Value))
1739   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1740                                                Metadata *Type, Metadata *Value),
1741                     (Tag, Name, Type, Value))
1742
1743   TempDITemplateValueParameter clone() const { return cloneImpl(); }
1744
1745   Metadata *getValue() const { return getOperand(2); }
1746
1747   static bool classof(const Metadata *MD) {
1748     return MD->getMetadataID() == DITemplateValueParameterKind;
1749   }
1750 };
1751
1752 /// \brief Base class for variables.
1753 ///
1754 /// TODO: Hardcode to DW_TAG_variable.
1755 class DIVariable : public DINode {
1756   unsigned Line;
1757
1758 protected:
1759   DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1760              unsigned Line, ArrayRef<Metadata *> Ops)
1761       : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1762   ~DIVariable() = default;
1763
1764 public:
1765   unsigned getLine() const { return Line; }
1766   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1767   StringRef getName() const { return getStringOperand(1); }
1768   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1769   DITypeRef getType() const { return DITypeRef(getRawType()); }
1770
1771   StringRef getFilename() const {
1772     if (auto *F = getFile())
1773       return F->getFilename();
1774     return "";
1775   }
1776   StringRef getDirectory() const {
1777     if (auto *F = getFile())
1778       return F->getDirectory();
1779     return "";
1780   }
1781
1782   Metadata *getRawScope() const { return getOperand(0); }
1783   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1784   Metadata *getRawFile() const { return getOperand(2); }
1785   Metadata *getRawType() const { return getOperand(3); }
1786
1787   static bool classof(const Metadata *MD) {
1788     return MD->getMetadataID() == DILocalVariableKind ||
1789            MD->getMetadataID() == DIGlobalVariableKind;
1790   }
1791 };
1792
1793 /// \brief Global variables.
1794 ///
1795 /// TODO: Remove DisplayName.  It's always equal to Name.
1796 class DIGlobalVariable : public DIVariable {
1797   friend class LLVMContextImpl;
1798   friend class MDNode;
1799
1800   bool IsLocalToUnit;
1801   bool IsDefinition;
1802
1803   DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1804                    bool IsLocalToUnit, bool IsDefinition,
1805                    ArrayRef<Metadata *> Ops)
1806       : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1807                    Line, Ops),
1808         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1809   ~DIGlobalVariable() = default;
1810
1811   static DIGlobalVariable *
1812   getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1813           StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1814           bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1815           DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1816           bool ShouldCreate = true) {
1817     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1818                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1819                    IsLocalToUnit, IsDefinition,
1820                    Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1821                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1822   }
1823   static DIGlobalVariable *
1824   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1825           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1826           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1827           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1828           bool ShouldCreate = true);
1829
1830   TempDIGlobalVariable cloneImpl() const {
1831     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1832                         getFile(), getLine(), getType(), isLocalToUnit(),
1833                         isDefinition(), getVariable(),
1834                         getStaticDataMemberDeclaration());
1835   }
1836
1837 public:
1838   DEFINE_MDNODE_GET(DIGlobalVariable,
1839                     (DIScope * Scope, StringRef Name, StringRef LinkageName,
1840                      DIFile *File, unsigned Line, DITypeRef Type,
1841                      bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1842                      DIDerivedType *StaticDataMemberDeclaration),
1843                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1844                      IsDefinition, Variable, StaticDataMemberDeclaration))
1845   DEFINE_MDNODE_GET(DIGlobalVariable,
1846                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1847                      Metadata *File, unsigned Line, Metadata *Type,
1848                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1849                      Metadata *StaticDataMemberDeclaration),
1850                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1851                      IsDefinition, Variable, StaticDataMemberDeclaration))
1852
1853   TempDIGlobalVariable clone() const { return cloneImpl(); }
1854
1855   bool isLocalToUnit() const { return IsLocalToUnit; }
1856   bool isDefinition() const { return IsDefinition; }
1857   StringRef getDisplayName() const { return getStringOperand(4); }
1858   StringRef getLinkageName() const { return getStringOperand(5); }
1859   Constant *getVariable() const {
1860     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1861       return dyn_cast<Constant>(C->getValue());
1862     return nullptr;
1863   }
1864   DIDerivedType *getStaticDataMemberDeclaration() const {
1865     return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1866   }
1867
1868   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1869   Metadata *getRawVariable() const { return getOperand(6); }
1870   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1871
1872   static bool classof(const Metadata *MD) {
1873     return MD->getMetadataID() == DIGlobalVariableKind;
1874   }
1875 };
1876
1877 /// \brief Local variable.
1878 ///
1879 /// TODO: Split between arguments and otherwise.
1880 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1881 /// TODO: Split up flags.
1882 class DILocalVariable : public DIVariable {
1883   friend class LLVMContextImpl;
1884   friend class MDNode;
1885
1886   unsigned Arg;
1887   unsigned Flags;
1888
1889   DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1890                   unsigned Line, unsigned Arg, unsigned Flags,
1891                   ArrayRef<Metadata *> Ops)
1892       : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1893         Flags(Flags) {}
1894   ~DILocalVariable() = default;
1895
1896   static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1897                                   DIScope *Scope, StringRef Name, DIFile *File,
1898                                   unsigned Line, DITypeRef Type, unsigned Arg,
1899                                   unsigned Flags, StorageType Storage,
1900                                   bool ShouldCreate = true) {
1901     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1902                    File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1903   }
1904   static DILocalVariable *
1905   getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1906           Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1907           unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1908
1909   TempDILocalVariable cloneImpl() const {
1910     return getTemporary(getContext(), getTag(), getScope(), getName(),
1911                         getFile(), getLine(), getType(), getArg(), getFlags());
1912   }
1913
1914 public:
1915   DEFINE_MDNODE_GET(DILocalVariable,
1916                     (unsigned Tag, DILocalScope *Scope, StringRef Name,
1917                      DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1918                      unsigned Flags),
1919                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1920   DEFINE_MDNODE_GET(DILocalVariable,
1921                     (unsigned Tag, Metadata *Scope, MDString *Name,
1922                      Metadata *File, unsigned Line, Metadata *Type,
1923                      unsigned Arg, unsigned Flags),
1924                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1925
1926   TempDILocalVariable clone() const { return cloneImpl(); }
1927
1928   /// \brief Get the local scope for this variable.
1929   ///
1930   /// Variables must be defined in a local scope.
1931   DILocalScope *getScope() const {
1932     return cast<DILocalScope>(DIVariable::getScope());
1933   }
1934
1935   unsigned getArg() const { return Arg; }
1936   unsigned getFlags() const { return Flags; }
1937
1938   bool isArtificial() const { return getFlags() & FlagArtificial; }
1939   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1940
1941   /// \brief Check that a location is valid for this variable.
1942   ///
1943   /// Check that \c DL exists, is in the same subprogram, and has the same
1944   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachemnt
1945   /// to a \a DbgInfoIntrinsic.)
1946   bool isValidLocationForIntrinsic(const DILocation *DL) const {
1947     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1948   }
1949
1950   static bool classof(const Metadata *MD) {
1951     return MD->getMetadataID() == DILocalVariableKind;
1952   }
1953 };
1954
1955 /// \brief DWARF expression.
1956 ///
1957 /// This is (almost) a DWARF expression that modifies the location of a
1958 /// variable or (or the location of a single piece of a variable).
1959 ///
1960 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1961 /// and have DW_OP_plus consume the topmost elements on the stack.
1962 ///
1963 /// TODO: Co-allocate the expression elements.
1964 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1965 /// storage types.
1966 class DIExpression : public MDNode {
1967   friend class LLVMContextImpl;
1968   friend class MDNode;
1969
1970   std::vector<uint64_t> Elements;
1971
1972   DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1973       : MDNode(C, DIExpressionKind, Storage, None),
1974         Elements(Elements.begin(), Elements.end()) {}
1975   ~DIExpression() = default;
1976
1977   static DIExpression *getImpl(LLVMContext &Context,
1978                                ArrayRef<uint64_t> Elements, StorageType Storage,
1979                                bool ShouldCreate = true);
1980
1981   TempDIExpression cloneImpl() const {
1982     return getTemporary(getContext(), getElements());
1983   }
1984
1985 public:
1986   DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1987
1988   TempDIExpression clone() const { return cloneImpl(); }
1989
1990   ArrayRef<uint64_t> getElements() const { return Elements; }
1991
1992   unsigned getNumElements() const { return Elements.size(); }
1993   uint64_t getElement(unsigned I) const {
1994     assert(I < Elements.size() && "Index out of range");
1995     return Elements[I];
1996   }
1997
1998   /// \brief Return whether this is a piece of an aggregate variable.
1999   bool isBitPiece() const;
2000
2001   /// \brief Return the offset of this piece in bits.
2002   uint64_t getBitPieceOffset() const;
2003
2004   /// \brief Return the size of this piece in bits.
2005   uint64_t getBitPieceSize() const;
2006
2007   typedef ArrayRef<uint64_t>::iterator element_iterator;
2008   element_iterator elements_begin() const { return getElements().begin(); }
2009   element_iterator elements_end() const { return getElements().end(); }
2010
2011   /// \brief A lightweight wrapper around an expression operand.
2012   ///
2013   /// TODO: Store arguments directly and change \a DIExpression to store a
2014   /// range of these.
2015   class ExprOperand {
2016     const uint64_t *Op;
2017
2018   public:
2019     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2020
2021     const uint64_t *get() const { return Op; }
2022
2023     /// \brief Get the operand code.
2024     uint64_t getOp() const { return *Op; }
2025
2026     /// \brief Get an argument to the operand.
2027     ///
2028     /// Never returns the operand itself.
2029     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2030
2031     unsigned getNumArgs() const { return getSize() - 1; }
2032
2033     /// \brief Return the size of the operand.
2034     ///
2035     /// Return the number of elements in the operand (1 + args).
2036     unsigned getSize() const;
2037   };
2038
2039   /// \brief An iterator for expression operands.
2040   class expr_op_iterator
2041       : public std::iterator<std::input_iterator_tag, ExprOperand> {
2042     ExprOperand Op;
2043
2044   public:
2045     explicit expr_op_iterator(element_iterator I) : Op(I) {}
2046
2047     element_iterator getBase() const { return Op.get(); }
2048     const ExprOperand &operator*() const { return Op; }
2049     const ExprOperand *operator->() const { return &Op; }
2050
2051     expr_op_iterator &operator++() {
2052       increment();
2053       return *this;
2054     }
2055     expr_op_iterator operator++(int) {
2056       expr_op_iterator T(*this);
2057       increment();
2058       return T;
2059     }
2060
2061     /// \brief Get the next iterator.
2062     ///
2063     /// \a std::next() doesn't work because this is technically an
2064     /// input_iterator, but it's a perfectly valid operation.  This is an
2065     /// accessor to provide the same functionality.
2066     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2067
2068     bool operator==(const expr_op_iterator &X) const {
2069       return getBase() == X.getBase();
2070     }
2071     bool operator!=(const expr_op_iterator &X) const {
2072       return getBase() != X.getBase();
2073     }
2074
2075   private:
2076     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2077   };
2078
2079   /// \brief Visit the elements via ExprOperand wrappers.
2080   ///
2081   /// These range iterators visit elements through \a ExprOperand wrappers.
2082   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2083   /// true.
2084   ///
2085   /// \pre \a isValid() gives \c true.
2086   /// @{
2087   expr_op_iterator expr_op_begin() const {
2088     return expr_op_iterator(elements_begin());
2089   }
2090   expr_op_iterator expr_op_end() const {
2091     return expr_op_iterator(elements_end());
2092   }
2093   /// @}
2094
2095   bool isValid() const;
2096
2097   static bool classof(const Metadata *MD) {
2098     return MD->getMetadataID() == DIExpressionKind;
2099   }
2100 };
2101
2102 class DIObjCProperty : public DINode {
2103   friend class LLVMContextImpl;
2104   friend class MDNode;
2105
2106   unsigned Line;
2107   unsigned Attributes;
2108
2109   DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2110                  unsigned Attributes, ArrayRef<Metadata *> Ops)
2111       : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2112                Ops),
2113         Line(Line), Attributes(Attributes) {}
2114   ~DIObjCProperty() = default;
2115
2116   static DIObjCProperty *
2117   getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2118           StringRef GetterName, StringRef SetterName, unsigned Attributes,
2119           DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2120     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2121                    getCanonicalMDString(Context, GetterName),
2122                    getCanonicalMDString(Context, SetterName), Attributes, Type,
2123                    Storage, ShouldCreate);
2124   }
2125   static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2126                                  Metadata *File, unsigned Line,
2127                                  MDString *GetterName, MDString *SetterName,
2128                                  unsigned Attributes, Metadata *Type,
2129                                  StorageType Storage, bool ShouldCreate = true);
2130
2131   TempDIObjCProperty cloneImpl() const {
2132     return getTemporary(getContext(), getName(), getFile(), getLine(),
2133                         getGetterName(), getSetterName(), getAttributes(),
2134                         getType());
2135   }
2136
2137 public:
2138   DEFINE_MDNODE_GET(DIObjCProperty,
2139                     (StringRef Name, DIFile *File, unsigned Line,
2140                      StringRef GetterName, StringRef SetterName,
2141                      unsigned Attributes, DITypeRef Type),
2142                     (Name, File, Line, GetterName, SetterName, Attributes,
2143                      Type))
2144   DEFINE_MDNODE_GET(DIObjCProperty,
2145                     (MDString * Name, Metadata *File, unsigned Line,
2146                      MDString *GetterName, MDString *SetterName,
2147                      unsigned Attributes, Metadata *Type),
2148                     (Name, File, Line, GetterName, SetterName, Attributes,
2149                      Type))
2150
2151   TempDIObjCProperty clone() const { return cloneImpl(); }
2152
2153   unsigned getLine() const { return Line; }
2154   unsigned getAttributes() const { return Attributes; }
2155   StringRef getName() const { return getStringOperand(0); }
2156   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2157   StringRef getGetterName() const { return getStringOperand(2); }
2158   StringRef getSetterName() const { return getStringOperand(3); }
2159   DITypeRef getType() const { return DITypeRef(getRawType()); }
2160
2161   StringRef getFilename() const {
2162     if (auto *F = getFile())
2163       return F->getFilename();
2164     return "";
2165   }
2166   StringRef getDirectory() const {
2167     if (auto *F = getFile())
2168       return F->getDirectory();
2169     return "";
2170   }
2171
2172   MDString *getRawName() const { return getOperandAs<MDString>(0); }
2173   Metadata *getRawFile() const { return getOperand(1); }
2174   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2175   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2176   Metadata *getRawType() const { return getOperand(4); }
2177
2178   static bool classof(const Metadata *MD) {
2179     return MD->getMetadataID() == DIObjCPropertyKind;
2180   }
2181 };
2182
2183 /// \brief An imported module (C++ using directive or similar).
2184 class DIImportedEntity : public DINode {
2185   friend class LLVMContextImpl;
2186   friend class MDNode;
2187
2188   unsigned Line;
2189
2190   DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2191                    unsigned Line, ArrayRef<Metadata *> Ops)
2192       : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2193   ~DIImportedEntity() = default;
2194
2195   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2196                                    DIScope *Scope, DINodeRef Entity,
2197                                    unsigned Line, StringRef Name,
2198                                    StorageType Storage,
2199                                    bool ShouldCreate = true) {
2200     return getImpl(Context, Tag, Scope, Entity, Line,
2201                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2202   }
2203   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2204                                    Metadata *Scope, Metadata *Entity,
2205                                    unsigned Line, MDString *Name,
2206                                    StorageType Storage,
2207                                    bool ShouldCreate = true);
2208
2209   TempDIImportedEntity cloneImpl() const {
2210     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2211                         getLine(), getName());
2212   }
2213
2214 public:
2215   DEFINE_MDNODE_GET(DIImportedEntity,
2216                     (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2217                      unsigned Line, StringRef Name = ""),
2218                     (Tag, Scope, Entity, Line, Name))
2219   DEFINE_MDNODE_GET(DIImportedEntity,
2220                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
2221                      unsigned Line, MDString *Name),
2222                     (Tag, Scope, Entity, Line, Name))
2223
2224   TempDIImportedEntity clone() const { return cloneImpl(); }
2225
2226   unsigned getLine() const { return Line; }
2227   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2228   DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2229   StringRef getName() const { return getStringOperand(2); }
2230
2231   Metadata *getRawScope() const { return getOperand(0); }
2232   Metadata *getRawEntity() const { return getOperand(1); }
2233   MDString *getRawName() const { return getOperandAs<MDString>(2); }
2234
2235   static bool classof(const Metadata *MD) {
2236     return MD->getMetadataID() == DIImportedEntityKind;
2237   }
2238 };
2239
2240 } // end namespace llvm
2241
2242 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2243 #undef DEFINE_MDNODE_GET_UNPACK
2244 #undef DEFINE_MDNODE_GET
2245
2246 #endif