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