Add remove method to operate on AttrBuilder instead of AttributeSet.
[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
975   DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
976                 bool IsOptimized, unsigned RuntimeVersion,
977                 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
978       : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
979         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
980         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
981   ~DICompileUnit() = default;
982
983   static DICompileUnit *
984   getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
985           StringRef Producer, bool IsOptimized, StringRef Flags,
986           unsigned RuntimeVersion, StringRef SplitDebugFilename,
987           unsigned EmissionKind, DICompositeTypeArray EnumTypes,
988           DITypeArray RetainedTypes, DISubprogramArray Subprograms,
989           DIGlobalVariableArray GlobalVariables,
990           DIImportedEntityArray ImportedEntities, StorageType Storage,
991           bool ShouldCreate = true) {
992     return getImpl(
993         Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
994         IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
995         getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
996         EnumTypes.get(), RetainedTypes.get(), Subprograms.get(),
997         GlobalVariables.get(), ImportedEntities.get(), Storage, ShouldCreate);
998   }
999   static DICompileUnit *
1000   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1001           MDString *Producer, bool IsOptimized, MDString *Flags,
1002           unsigned RuntimeVersion, MDString *SplitDebugFilename,
1003           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1004           Metadata *Subprograms, Metadata *GlobalVariables,
1005           Metadata *ImportedEntities, StorageType Storage,
1006           bool ShouldCreate = true);
1007
1008   TempDICompileUnit cloneImpl() const {
1009     return getTemporary(
1010         getContext(), getSourceLanguage(), getFile(), getProducer(),
1011         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1012         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1013         getGlobalVariables(), getImportedEntities());
1014   }
1015
1016 public:
1017   DEFINE_MDNODE_GET(DICompileUnit,
1018                     (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1019                      bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1020                      StringRef SplitDebugFilename, unsigned EmissionKind,
1021                      DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1022                      DISubprogramArray Subprograms,
1023                      DIGlobalVariableArray GlobalVariables,
1024                      DIImportedEntityArray ImportedEntities),
1025                     (SourceLanguage, File, Producer, IsOptimized, Flags,
1026                      RuntimeVersion, SplitDebugFilename, EmissionKind,
1027                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1028                      ImportedEntities))
1029   DEFINE_MDNODE_GET(DICompileUnit,
1030                     (unsigned SourceLanguage, Metadata *File,
1031                      MDString *Producer, bool IsOptimized, MDString *Flags,
1032                      unsigned RuntimeVersion, MDString *SplitDebugFilename,
1033                      unsigned EmissionKind, Metadata *EnumTypes,
1034                      Metadata *RetainedTypes, Metadata *Subprograms,
1035                      Metadata *GlobalVariables, Metadata *ImportedEntities),
1036                     (SourceLanguage, File, Producer, IsOptimized, Flags,
1037                      RuntimeVersion, SplitDebugFilename, EmissionKind,
1038                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1039                      ImportedEntities))
1040
1041   TempDICompileUnit clone() const { return cloneImpl(); }
1042
1043   unsigned getSourceLanguage() const { return SourceLanguage; }
1044   bool isOptimized() const { return IsOptimized; }
1045   unsigned getRuntimeVersion() const { return RuntimeVersion; }
1046   unsigned getEmissionKind() const { return EmissionKind; }
1047   StringRef getProducer() const { return getStringOperand(1); }
1048   StringRef getFlags() const { return getStringOperand(2); }
1049   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1050   DICompositeTypeArray getEnumTypes() const {
1051     return cast_or_null<MDTuple>(getRawEnumTypes());
1052   }
1053   DITypeArray getRetainedTypes() const {
1054     return cast_or_null<MDTuple>(getRawRetainedTypes());
1055   }
1056   DISubprogramArray getSubprograms() const {
1057     return cast_or_null<MDTuple>(getRawSubprograms());
1058   }
1059   DIGlobalVariableArray getGlobalVariables() const {
1060     return cast_or_null<MDTuple>(getRawGlobalVariables());
1061   }
1062   DIImportedEntityArray getImportedEntities() const {
1063     return cast_or_null<MDTuple>(getRawImportedEntities());
1064   }
1065
1066   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1067   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1068   MDString *getRawSplitDebugFilename() const {
1069     return getOperandAs<MDString>(3);
1070   }
1071   Metadata *getRawEnumTypes() const { return getOperand(4); }
1072   Metadata *getRawRetainedTypes() const { return getOperand(5); }
1073   Metadata *getRawSubprograms() const { return getOperand(6); }
1074   Metadata *getRawGlobalVariables() const { return getOperand(7); }
1075   Metadata *getRawImportedEntities() const { return getOperand(8); }
1076
1077   /// \brief Replace arrays.
1078   ///
1079   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1080   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
1081   /// DICompileUnit should be fairly rare.
1082   /// @{
1083   void replaceSubprograms(DISubprogramArray N) {
1084     replaceOperandWith(6, N.get());
1085   }
1086   void replaceGlobalVariables(DIGlobalVariableArray N) {
1087     replaceOperandWith(7, N.get());
1088   }
1089   /// @}
1090
1091   static bool classof(const Metadata *MD) {
1092     return MD->getMetadataID() == DICompileUnitKind;
1093   }
1094 };
1095
1096 /// \brief A scope for locals.
1097 ///
1098 /// A legal scope for lexical blocks, local variables, and debug info
1099 /// locations.  Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1100 /// DILexicalBlockFile.
1101 class DILocalScope : public DIScope {
1102 protected:
1103   DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1104                ArrayRef<Metadata *> Ops)
1105       : DIScope(C, ID, Storage, Tag, Ops) {}
1106   ~DILocalScope() = default;
1107
1108 public:
1109   /// \brief Get the subprogram for this scope.
1110   ///
1111   /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1112   /// chain.
1113   DISubprogram *getSubprogram() const;
1114
1115   static bool classof(const Metadata *MD) {
1116     return MD->getMetadataID() == DISubprogramKind ||
1117            MD->getMetadataID() == DILexicalBlockKind ||
1118            MD->getMetadataID() == DILexicalBlockFileKind;
1119   }
1120 };
1121
1122 /// \brief Debug location.
1123 ///
1124 /// A debug location in source code, used for debug info and otherwise.
1125 class DILocation : public MDNode {
1126   friend class LLVMContextImpl;
1127   friend class MDNode;
1128
1129   DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1130              unsigned Column, ArrayRef<Metadata *> MDs);
1131   ~DILocation() { dropAllReferences(); }
1132
1133   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1134                              unsigned Column, Metadata *Scope,
1135                              Metadata *InlinedAt, StorageType Storage,
1136                              bool ShouldCreate = true);
1137   static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1138                              unsigned Column, DILocalScope *Scope,
1139                              DILocation *InlinedAt, StorageType Storage,
1140                              bool ShouldCreate = true) {
1141     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1142                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1143   }
1144
1145   TempDILocation cloneImpl() const {
1146     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1147                         getInlinedAt());
1148   }
1149
1150   // Disallow replacing operands.
1151   void replaceOperandWith(unsigned I, Metadata *New) = delete;
1152
1153 public:
1154   DEFINE_MDNODE_GET(DILocation,
1155                     (unsigned Line, unsigned Column, Metadata *Scope,
1156                      Metadata *InlinedAt = nullptr),
1157                     (Line, Column, Scope, InlinedAt))
1158   DEFINE_MDNODE_GET(DILocation,
1159                     (unsigned Line, unsigned Column, DILocalScope *Scope,
1160                      DILocation *InlinedAt = nullptr),
1161                     (Line, Column, Scope, InlinedAt))
1162
1163   /// \brief Return a (temporary) clone of this.
1164   TempDILocation clone() const { return cloneImpl(); }
1165
1166   unsigned getLine() const { return SubclassData32; }
1167   unsigned getColumn() const { return SubclassData16; }
1168   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1169   DILocation *getInlinedAt() const {
1170     return cast_or_null<DILocation>(getRawInlinedAt());
1171   }
1172
1173   DIFile *getFile() const { return getScope()->getFile(); }
1174   StringRef getFilename() const { return getScope()->getFilename(); }
1175   StringRef getDirectory() const { return getScope()->getDirectory(); }
1176
1177   /// \brief Get the scope where this is inlined.
1178   ///
1179   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1180   /// location.
1181   DILocalScope *getInlinedAtScope() const {
1182     if (auto *IA = getInlinedAt())
1183       return IA->getInlinedAtScope();
1184     return getScope();
1185   }
1186
1187   /// \brief Check whether this can be discriminated from another location.
1188   ///
1189   /// Check \c this can be discriminated from \c RHS in a linetable entry.
1190   /// Scope and inlined-at chains are not recorded in the linetable, so they
1191   /// cannot be used to distinguish basic blocks.
1192   ///
1193   /// The current implementation is weaker than it should be, since it just
1194   /// checks filename and line.
1195   ///
1196   /// FIXME: Add a check for getDiscriminator().
1197   /// FIXME: Add a check for getColumn().
1198   /// FIXME: Change the getFilename() check to getFile() (or add one for
1199   /// getDirectory()).
1200   bool canDiscriminate(const DILocation &RHS) const {
1201     return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1202   }
1203
1204   /// \brief Get the DWARF discriminator.
1205   ///
1206   /// DWARF discriminators distinguish identical file locations between
1207   /// instructions that are on different basic blocks.
1208   inline unsigned getDiscriminator() const;
1209
1210   /// \brief Compute new discriminator in the given context.
1211   ///
1212   /// This modifies the \a LLVMContext that \c this is in to increment the next
1213   /// discriminator for \c this's line/filename combination.
1214   ///
1215   /// FIXME: Delete this.  See comments in implementation and at the only call
1216   /// site in \a AddDiscriminators::runOnFunction().
1217   unsigned computeNewDiscriminator() const;
1218
1219   Metadata *getRawScope() const { return getOperand(0); }
1220   Metadata *getRawInlinedAt() const {
1221     if (getNumOperands() == 2)
1222       return getOperand(1);
1223     return nullptr;
1224   }
1225
1226   static bool classof(const Metadata *MD) {
1227     return MD->getMetadataID() == DILocationKind;
1228   }
1229 };
1230
1231 /// \brief Subprogram description.
1232 ///
1233 /// TODO: Remove DisplayName.  It's always equal to Name.
1234 /// TODO: Split up flags.
1235 class DISubprogram : public DILocalScope {
1236   friend class LLVMContextImpl;
1237   friend class MDNode;
1238
1239   unsigned Line;
1240   unsigned ScopeLine;
1241   unsigned Virtuality;
1242   unsigned VirtualIndex;
1243   unsigned Flags;
1244   bool IsLocalToUnit;
1245   bool IsDefinition;
1246   bool IsOptimized;
1247
1248   DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1249                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1250                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1251                bool IsOptimized, ArrayRef<Metadata *> Ops)
1252       : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1253                      Ops),
1254         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1255         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1256         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1257   ~DISubprogram() = default;
1258
1259   static DISubprogram *
1260   getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1261           StringRef LinkageName, DIFile *File, unsigned Line,
1262           DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1263           unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1264           unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1265           Constant *Function, DITemplateParameterArray TemplateParams,
1266           DISubprogram *Declaration, DILocalVariableArray Variables,
1267           StorageType Storage, bool ShouldCreate = true) {
1268     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1269                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1270                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1271                    Virtuality, VirtualIndex, Flags, IsOptimized,
1272                    Function ? ConstantAsMetadata::get(Function) : nullptr,
1273                    TemplateParams.get(), Declaration, Variables.get(), Storage,
1274                    ShouldCreate);
1275   }
1276   static DISubprogram *
1277   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1278           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1279           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1280           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1281           unsigned Flags, bool IsOptimized, Metadata *Function,
1282           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1283           StorageType Storage, bool ShouldCreate = true);
1284
1285   TempDISubprogram cloneImpl() const {
1286     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1287                         getFile(), getLine(), getType(), isLocalToUnit(),
1288                         isDefinition(), getScopeLine(), getContainingType(),
1289                         getVirtuality(), getVirtualIndex(), getFlags(),
1290                         isOptimized(), getFunctionConstant(),
1291                         getTemplateParams(), getDeclaration(), getVariables());
1292   }
1293
1294 public:
1295   DEFINE_MDNODE_GET(DISubprogram,
1296                     (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1297                      DIFile *File, unsigned Line, DISubroutineType *Type,
1298                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1299                      DITypeRef ContainingType, unsigned Virtuality,
1300                      unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1301                      Constant *Function = nullptr,
1302                      DITemplateParameterArray TemplateParams = nullptr,
1303                      DISubprogram *Declaration = nullptr,
1304                      DILocalVariableArray Variables = nullptr),
1305                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1306                      IsDefinition, ScopeLine, ContainingType, Virtuality,
1307                      VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1308                      Declaration, Variables))
1309   DEFINE_MDNODE_GET(
1310       DISubprogram,
1311       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1312        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1313        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1314        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1315        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1316        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1317       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1318        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1319        Function, TemplateParams, Declaration, Variables))
1320
1321   TempDISubprogram clone() const { return cloneImpl(); }
1322
1323 public:
1324   unsigned getLine() const { return Line; }
1325   unsigned getVirtuality() const { return Virtuality; }
1326   unsigned getVirtualIndex() const { return VirtualIndex; }
1327   unsigned getScopeLine() const { return ScopeLine; }
1328   unsigned getFlags() const { return Flags; }
1329   bool isLocalToUnit() const { return IsLocalToUnit; }
1330   bool isDefinition() const { return IsDefinition; }
1331   bool isOptimized() const { return IsOptimized; }
1332
1333   unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1334   bool isPrivate() const {
1335     return (getFlags() & FlagAccessibility) == FlagPrivate;
1336   }
1337   bool isProtected() const {
1338     return (getFlags() & FlagAccessibility) == FlagProtected;
1339   }
1340   bool isPublic() const {
1341     return (getFlags() & FlagAccessibility) == FlagPublic;
1342   }
1343   bool isExplicit() const { return getFlags() & FlagExplicit; }
1344   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1345
1346   /// \brief Check if this is reference-qualified.
1347   ///
1348   /// Return true if this subprogram is a C++11 reference-qualified non-static
1349   /// member function (void foo() &).
1350   unsigned isLValueReference() const {
1351     return getFlags() & FlagLValueReference;
1352   }
1353
1354   /// \brief Check if this is rvalue-reference-qualified.
1355   ///
1356   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1357   /// non-static member function (void foo() &&).
1358   unsigned isRValueReference() const {
1359     return getFlags() & FlagRValueReference;
1360   }
1361
1362   DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1363
1364   StringRef getName() const { return getStringOperand(2); }
1365   StringRef getDisplayName() const { return getStringOperand(3); }
1366   StringRef getLinkageName() const { return getStringOperand(4); }
1367
1368   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1369   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1370
1371   DISubroutineType *getType() const {
1372     return cast_or_null<DISubroutineType>(getRawType());
1373   }
1374   DITypeRef getContainingType() const {
1375     return DITypeRef(getRawContainingType());
1376   }
1377
1378   Constant *getFunctionConstant() const {
1379     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1380       return C->getValue();
1381     return nullptr;
1382   }
1383   DITemplateParameterArray getTemplateParams() const {
1384     return cast_or_null<MDTuple>(getRawTemplateParams());
1385   }
1386   DISubprogram *getDeclaration() const {
1387     return cast_or_null<DISubprogram>(getRawDeclaration());
1388   }
1389   DILocalVariableArray getVariables() const {
1390     return cast_or_null<MDTuple>(getRawVariables());
1391   }
1392
1393   Metadata *getRawScope() const { return getOperand(1); }
1394   Metadata *getRawType() const { return getOperand(5); }
1395   Metadata *getRawContainingType() const { return getOperand(6); }
1396   Metadata *getRawFunction() const { return getOperand(7); }
1397   Metadata *getRawTemplateParams() const { return getOperand(8); }
1398   Metadata *getRawDeclaration() const { return getOperand(9); }
1399   Metadata *getRawVariables() const { return getOperand(10); }
1400
1401   /// \brief Get a pointer to the function this subprogram describes.
1402   ///
1403   /// This dyn_casts \a getFunctionConstant() to \a Function.
1404   ///
1405   /// FIXME: Should this be looking through bitcasts?
1406   Function *getFunction() const;
1407
1408   /// \brief Replace the function.
1409   ///
1410   /// If \a isUniqued() and not \a isResolved(), this could node will be
1411   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1412   /// that's a problem.
1413   /// @{
1414   void replaceFunction(Function *F);
1415   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1416   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1417   /// @}
1418
1419   /// \brief Check if this subprogram decribes the given function.
1420   ///
1421   /// FIXME: Should this be looking through bitcasts?
1422   bool describes(const Function *F) const;
1423
1424   static bool classof(const Metadata *MD) {
1425     return MD->getMetadataID() == DISubprogramKind;
1426   }
1427 };
1428
1429 class DILexicalBlockBase : public DILocalScope {
1430 protected:
1431   DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1432                      ArrayRef<Metadata *> Ops)
1433       : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1434   ~DILexicalBlockBase() = default;
1435
1436 public:
1437   DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1438
1439   Metadata *getRawScope() const { return getOperand(1); }
1440
1441   /// \brief Forwarding accessors to LexicalBlock.
1442   ///
1443   /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1444   /// @{
1445   inline unsigned getLine() const;
1446   inline unsigned getColumn() const;
1447   /// @}
1448   static bool classof(const Metadata *MD) {
1449     return MD->getMetadataID() == DILexicalBlockKind ||
1450            MD->getMetadataID() == DILexicalBlockFileKind;
1451   }
1452 };
1453
1454 class DILexicalBlock : public DILexicalBlockBase {
1455   friend class LLVMContextImpl;
1456   friend class MDNode;
1457
1458   unsigned Line;
1459   unsigned Column;
1460
1461   DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1462                  unsigned Column, ArrayRef<Metadata *> Ops)
1463       : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1464         Column(Column) {}
1465   ~DILexicalBlock() = default;
1466
1467   static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1468                                  DIFile *File, unsigned Line, unsigned Column,
1469                                  StorageType Storage,
1470                                  bool ShouldCreate = true) {
1471     return getImpl(Context, static_cast<Metadata *>(Scope),
1472                    static_cast<Metadata *>(File), Line, Column, Storage,
1473                    ShouldCreate);
1474   }
1475
1476   static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1477                                  Metadata *File, unsigned Line, unsigned Column,
1478                                  StorageType Storage, bool ShouldCreate = true);
1479
1480   TempDILexicalBlock cloneImpl() const {
1481     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1482                         getColumn());
1483   }
1484
1485 public:
1486   DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1487                                      unsigned Line, unsigned Column),
1488                     (Scope, File, Line, Column))
1489   DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1490                                      unsigned Line, unsigned Column),
1491                     (Scope, File, Line, Column))
1492
1493   TempDILexicalBlock clone() const { return cloneImpl(); }
1494
1495   unsigned getLine() const { return Line; }
1496   unsigned getColumn() const { return Column; }
1497
1498   static bool classof(const Metadata *MD) {
1499     return MD->getMetadataID() == DILexicalBlockKind;
1500   }
1501 };
1502
1503 unsigned DILexicalBlockBase::getLine() const {
1504   if (auto *N = dyn_cast<DILexicalBlock>(this))
1505     return N->getLine();
1506   return 0;
1507 }
1508
1509 unsigned DILexicalBlockBase::getColumn() const {
1510   if (auto *N = dyn_cast<DILexicalBlock>(this))
1511     return N->getColumn();
1512   return 0;
1513 }
1514
1515 class DILexicalBlockFile : public DILexicalBlockBase {
1516   friend class LLVMContextImpl;
1517   friend class MDNode;
1518
1519   unsigned Discriminator;
1520
1521   DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1522                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1523       : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1524         Discriminator(Discriminator) {}
1525   ~DILexicalBlockFile() = default;
1526
1527   static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1528                                      DIFile *File, unsigned Discriminator,
1529                                      StorageType Storage,
1530                                      bool ShouldCreate = true) {
1531     return getImpl(Context, static_cast<Metadata *>(Scope),
1532                    static_cast<Metadata *>(File), Discriminator, Storage,
1533                    ShouldCreate);
1534   }
1535
1536   static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1537                                      Metadata *File, unsigned Discriminator,
1538                                      StorageType Storage,
1539                                      bool ShouldCreate = true);
1540
1541   TempDILexicalBlockFile cloneImpl() const {
1542     return getTemporary(getContext(), getScope(), getFile(),
1543                         getDiscriminator());
1544   }
1545
1546 public:
1547   DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1548                                          unsigned Discriminator),
1549                     (Scope, File, Discriminator))
1550   DEFINE_MDNODE_GET(DILexicalBlockFile,
1551                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1552                     (Scope, File, Discriminator))
1553
1554   TempDILexicalBlockFile clone() const { return cloneImpl(); }
1555
1556   // TODO: Remove these once they're gone from DILexicalBlockBase.
1557   unsigned getLine() const = delete;
1558   unsigned getColumn() const = delete;
1559
1560   unsigned getDiscriminator() const { return Discriminator; }
1561
1562   static bool classof(const Metadata *MD) {
1563     return MD->getMetadataID() == DILexicalBlockFileKind;
1564   }
1565 };
1566
1567 unsigned DILocation::getDiscriminator() const {
1568   if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1569     return F->getDiscriminator();
1570   return 0;
1571 }
1572
1573 class DINamespace : public DIScope {
1574   friend class LLVMContextImpl;
1575   friend class MDNode;
1576
1577   unsigned Line;
1578
1579   DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1580               ArrayRef<Metadata *> Ops)
1581       : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1582                 Ops),
1583         Line(Line) {}
1584   ~DINamespace() = default;
1585
1586   static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1587                               DIFile *File, StringRef Name, unsigned Line,
1588                               StorageType Storage, bool ShouldCreate = true) {
1589     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1590                    Line, Storage, ShouldCreate);
1591   }
1592   static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1593                               Metadata *File, MDString *Name, unsigned Line,
1594                               StorageType Storage, bool ShouldCreate = true);
1595
1596   TempDINamespace cloneImpl() const {
1597     return getTemporary(getContext(), getScope(), getFile(), getName(),
1598                         getLine());
1599   }
1600
1601 public:
1602   DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1603                                   unsigned Line),
1604                     (Scope, File, Name, Line))
1605   DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1606                                   MDString *Name, unsigned Line),
1607                     (Scope, File, Name, Line))
1608
1609   TempDINamespace clone() const { return cloneImpl(); }
1610
1611   unsigned getLine() const { return Line; }
1612   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1613   StringRef getName() const { return getStringOperand(2); }
1614
1615   Metadata *getRawScope() const { return getOperand(1); }
1616   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1617
1618   static bool classof(const Metadata *MD) {
1619     return MD->getMetadataID() == DINamespaceKind;
1620   }
1621 };
1622
1623 /// \brief Base class for template parameters.
1624 class DITemplateParameter : public DINode {
1625 protected:
1626   DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1627                       unsigned Tag, ArrayRef<Metadata *> Ops)
1628       : DINode(Context, ID, Storage, Tag, Ops) {}
1629   ~DITemplateParameter() = default;
1630
1631 public:
1632   StringRef getName() const { return getStringOperand(0); }
1633   DITypeRef getType() const { return DITypeRef(getRawType()); }
1634
1635   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1636   Metadata *getRawType() const { return getOperand(1); }
1637
1638   static bool classof(const Metadata *MD) {
1639     return MD->getMetadataID() == DITemplateTypeParameterKind ||
1640            MD->getMetadataID() == DITemplateValueParameterKind;
1641   }
1642 };
1643
1644 class DITemplateTypeParameter : public DITemplateParameter {
1645   friend class LLVMContextImpl;
1646   friend class MDNode;
1647
1648   DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1649                           ArrayRef<Metadata *> Ops)
1650       : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1651                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1652   ~DITemplateTypeParameter() = default;
1653
1654   static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1655                                           DITypeRef Type, StorageType Storage,
1656                                           bool ShouldCreate = true) {
1657     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1658                    ShouldCreate);
1659   }
1660   static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1661                                           Metadata *Type, StorageType Storage,
1662                                           bool ShouldCreate = true);
1663
1664   TempDITemplateTypeParameter cloneImpl() const {
1665     return getTemporary(getContext(), getName(), getType());
1666   }
1667
1668 public:
1669   DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1670                     (Name, Type))
1671   DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1672                     (Name, Type))
1673
1674   TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1675
1676   static bool classof(const Metadata *MD) {
1677     return MD->getMetadataID() == DITemplateTypeParameterKind;
1678   }
1679 };
1680
1681 class DITemplateValueParameter : public DITemplateParameter {
1682   friend class LLVMContextImpl;
1683   friend class MDNode;
1684
1685   DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1686                            unsigned Tag, ArrayRef<Metadata *> Ops)
1687       : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1688                             Ops) {}
1689   ~DITemplateValueParameter() = default;
1690
1691   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1692                                            StringRef Name, DITypeRef Type,
1693                                            Metadata *Value, StorageType Storage,
1694                                            bool ShouldCreate = true) {
1695     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1696                    Value, Storage, ShouldCreate);
1697   }
1698   static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1699                                            MDString *Name, Metadata *Type,
1700                                            Metadata *Value, StorageType Storage,
1701                                            bool ShouldCreate = true);
1702
1703   TempDITemplateValueParameter cloneImpl() const {
1704     return getTemporary(getContext(), getTag(), getName(), getType(),
1705                         getValue());
1706   }
1707
1708 public:
1709   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1710                                                DITypeRef Type, Metadata *Value),
1711                     (Tag, Name, Type, Value))
1712   DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1713                                                Metadata *Type, Metadata *Value),
1714                     (Tag, Name, Type, Value))
1715
1716   TempDITemplateValueParameter clone() const { return cloneImpl(); }
1717
1718   Metadata *getValue() const { return getOperand(2); }
1719
1720   static bool classof(const Metadata *MD) {
1721     return MD->getMetadataID() == DITemplateValueParameterKind;
1722   }
1723 };
1724
1725 /// \brief Base class for variables.
1726 ///
1727 /// TODO: Hardcode to DW_TAG_variable.
1728 class DIVariable : public DINode {
1729   unsigned Line;
1730
1731 protected:
1732   DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1733              unsigned Line, ArrayRef<Metadata *> Ops)
1734       : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1735   ~DIVariable() = default;
1736
1737 public:
1738   unsigned getLine() const { return Line; }
1739   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1740   StringRef getName() const { return getStringOperand(1); }
1741   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1742   DITypeRef getType() const { return DITypeRef(getRawType()); }
1743
1744   StringRef getFilename() const {
1745     if (auto *F = getFile())
1746       return F->getFilename();
1747     return "";
1748   }
1749   StringRef getDirectory() const {
1750     if (auto *F = getFile())
1751       return F->getDirectory();
1752     return "";
1753   }
1754
1755   Metadata *getRawScope() const { return getOperand(0); }
1756   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1757   Metadata *getRawFile() const { return getOperand(2); }
1758   Metadata *getRawType() const { return getOperand(3); }
1759
1760   static bool classof(const Metadata *MD) {
1761     return MD->getMetadataID() == DILocalVariableKind ||
1762            MD->getMetadataID() == DIGlobalVariableKind;
1763   }
1764 };
1765
1766 /// \brief Global variables.
1767 ///
1768 /// TODO: Remove DisplayName.  It's always equal to Name.
1769 class DIGlobalVariable : public DIVariable {
1770   friend class LLVMContextImpl;
1771   friend class MDNode;
1772
1773   bool IsLocalToUnit;
1774   bool IsDefinition;
1775
1776   DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1777                    bool IsLocalToUnit, bool IsDefinition,
1778                    ArrayRef<Metadata *> Ops)
1779       : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1780                    Line, Ops),
1781         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1782   ~DIGlobalVariable() = default;
1783
1784   static DIGlobalVariable *
1785   getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1786           StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1787           bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1788           DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1789           bool ShouldCreate = true) {
1790     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1791                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1792                    IsLocalToUnit, IsDefinition,
1793                    Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1794                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1795   }
1796   static DIGlobalVariable *
1797   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1798           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1799           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1800           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1801           bool ShouldCreate = true);
1802
1803   TempDIGlobalVariable cloneImpl() const {
1804     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1805                         getFile(), getLine(), getType(), isLocalToUnit(),
1806                         isDefinition(), getVariable(),
1807                         getStaticDataMemberDeclaration());
1808   }
1809
1810 public:
1811   DEFINE_MDNODE_GET(DIGlobalVariable,
1812                     (DIScope * Scope, StringRef Name, StringRef LinkageName,
1813                      DIFile *File, unsigned Line, DITypeRef Type,
1814                      bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1815                      DIDerivedType *StaticDataMemberDeclaration),
1816                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1817                      IsDefinition, Variable, StaticDataMemberDeclaration))
1818   DEFINE_MDNODE_GET(DIGlobalVariable,
1819                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1820                      Metadata *File, unsigned Line, Metadata *Type,
1821                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1822                      Metadata *StaticDataMemberDeclaration),
1823                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1824                      IsDefinition, Variable, StaticDataMemberDeclaration))
1825
1826   TempDIGlobalVariable clone() const { return cloneImpl(); }
1827
1828   bool isLocalToUnit() const { return IsLocalToUnit; }
1829   bool isDefinition() const { return IsDefinition; }
1830   StringRef getDisplayName() const { return getStringOperand(4); }
1831   StringRef getLinkageName() const { return getStringOperand(5); }
1832   Constant *getVariable() const {
1833     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1834       return dyn_cast<Constant>(C->getValue());
1835     return nullptr;
1836   }
1837   DIDerivedType *getStaticDataMemberDeclaration() const {
1838     return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1839   }
1840
1841   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1842   Metadata *getRawVariable() const { return getOperand(6); }
1843   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1844
1845   static bool classof(const Metadata *MD) {
1846     return MD->getMetadataID() == DIGlobalVariableKind;
1847   }
1848 };
1849
1850 /// \brief Local variable.
1851 ///
1852 /// TODO: Split between arguments and otherwise.
1853 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1854 /// TODO: Split up flags.
1855 class DILocalVariable : public DIVariable {
1856   friend class LLVMContextImpl;
1857   friend class MDNode;
1858
1859   unsigned Arg;
1860   unsigned Flags;
1861
1862   DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1863                   unsigned Line, unsigned Arg, unsigned Flags,
1864                   ArrayRef<Metadata *> Ops)
1865       : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1866         Flags(Flags) {}
1867   ~DILocalVariable() = default;
1868
1869   static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1870                                   DIScope *Scope, StringRef Name, DIFile *File,
1871                                   unsigned Line, DITypeRef Type, unsigned Arg,
1872                                   unsigned Flags, StorageType Storage,
1873                                   bool ShouldCreate = true) {
1874     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1875                    File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1876   }
1877   static DILocalVariable *
1878   getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1879           Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1880           unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1881
1882   TempDILocalVariable cloneImpl() const {
1883     return getTemporary(getContext(), getTag(), getScope(), getName(),
1884                         getFile(), getLine(), getType(), getArg(), getFlags());
1885   }
1886
1887 public:
1888   DEFINE_MDNODE_GET(DILocalVariable,
1889                     (unsigned Tag, DILocalScope *Scope, StringRef Name,
1890                      DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1891                      unsigned Flags),
1892                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1893   DEFINE_MDNODE_GET(DILocalVariable,
1894                     (unsigned Tag, Metadata *Scope, MDString *Name,
1895                      Metadata *File, unsigned Line, Metadata *Type,
1896                      unsigned Arg, unsigned Flags),
1897                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1898
1899   TempDILocalVariable clone() const { return cloneImpl(); }
1900
1901   /// \brief Get the local scope for this variable.
1902   ///
1903   /// Variables must be defined in a local scope.
1904   DILocalScope *getScope() const {
1905     return cast<DILocalScope>(DIVariable::getScope());
1906   }
1907
1908   unsigned getArg() const { return Arg; }
1909   unsigned getFlags() const { return Flags; }
1910
1911   bool isArtificial() const { return getFlags() & FlagArtificial; }
1912   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1913
1914   /// \brief Check that a location is valid for this variable.
1915   ///
1916   /// Check that \c DL exists, is in the same subprogram, and has the same
1917   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachemnt
1918   /// to a \a DbgInfoIntrinsic.)
1919   bool isValidLocationForIntrinsic(const DILocation *DL) const {
1920     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1921   }
1922
1923   static bool classof(const Metadata *MD) {
1924     return MD->getMetadataID() == DILocalVariableKind;
1925   }
1926 };
1927
1928 /// \brief DWARF expression.
1929 ///
1930 /// This is (almost) a DWARF expression that modifies the location of a
1931 /// variable or (or the location of a single piece of a variable).
1932 ///
1933 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1934 /// and have DW_OP_plus consume the topmost elements on the stack.
1935 ///
1936 /// TODO: Co-allocate the expression elements.
1937 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1938 /// storage types.
1939 class DIExpression : public MDNode {
1940   friend class LLVMContextImpl;
1941   friend class MDNode;
1942
1943   std::vector<uint64_t> Elements;
1944
1945   DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1946       : MDNode(C, DIExpressionKind, Storage, None),
1947         Elements(Elements.begin(), Elements.end()) {}
1948   ~DIExpression() = default;
1949
1950   static DIExpression *getImpl(LLVMContext &Context,
1951                                ArrayRef<uint64_t> Elements, StorageType Storage,
1952                                bool ShouldCreate = true);
1953
1954   TempDIExpression cloneImpl() const {
1955     return getTemporary(getContext(), getElements());
1956   }
1957
1958 public:
1959   DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1960
1961   TempDIExpression clone() const { return cloneImpl(); }
1962
1963   ArrayRef<uint64_t> getElements() const { return Elements; }
1964
1965   unsigned getNumElements() const { return Elements.size(); }
1966   uint64_t getElement(unsigned I) const {
1967     assert(I < Elements.size() && "Index out of range");
1968     return Elements[I];
1969   }
1970
1971   /// \brief Return whether this is a piece of an aggregate variable.
1972   bool isBitPiece() const;
1973
1974   /// \brief Return the offset of this piece in bits.
1975   uint64_t getBitPieceOffset() const;
1976
1977   /// \brief Return the size of this piece in bits.
1978   uint64_t getBitPieceSize() const;
1979
1980   typedef ArrayRef<uint64_t>::iterator element_iterator;
1981   element_iterator elements_begin() const { return getElements().begin(); }
1982   element_iterator elements_end() const { return getElements().end(); }
1983
1984   /// \brief A lightweight wrapper around an expression operand.
1985   ///
1986   /// TODO: Store arguments directly and change \a DIExpression to store a
1987   /// range of these.
1988   class ExprOperand {
1989     const uint64_t *Op;
1990
1991   public:
1992     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1993
1994     const uint64_t *get() const { return Op; }
1995
1996     /// \brief Get the operand code.
1997     uint64_t getOp() const { return *Op; }
1998
1999     /// \brief Get an argument to the operand.
2000     ///
2001     /// Never returns the operand itself.
2002     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2003
2004     unsigned getNumArgs() const { return getSize() - 1; }
2005
2006     /// \brief Return the size of the operand.
2007     ///
2008     /// Return the number of elements in the operand (1 + args).
2009     unsigned getSize() const;
2010   };
2011
2012   /// \brief An iterator for expression operands.
2013   class expr_op_iterator
2014       : public std::iterator<std::input_iterator_tag, ExprOperand> {
2015     ExprOperand Op;
2016
2017   public:
2018     explicit expr_op_iterator(element_iterator I) : Op(I) {}
2019
2020     element_iterator getBase() const { return Op.get(); }
2021     const ExprOperand &operator*() const { return Op; }
2022     const ExprOperand *operator->() const { return &Op; }
2023
2024     expr_op_iterator &operator++() {
2025       increment();
2026       return *this;
2027     }
2028     expr_op_iterator operator++(int) {
2029       expr_op_iterator T(*this);
2030       increment();
2031       return T;
2032     }
2033
2034     /// \brief Get the next iterator.
2035     ///
2036     /// \a std::next() doesn't work because this is technically an
2037     /// input_iterator, but it's a perfectly valid operation.  This is an
2038     /// accessor to provide the same functionality.
2039     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2040
2041     bool operator==(const expr_op_iterator &X) const {
2042       return getBase() == X.getBase();
2043     }
2044     bool operator!=(const expr_op_iterator &X) const {
2045       return getBase() != X.getBase();
2046     }
2047
2048   private:
2049     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2050   };
2051
2052   /// \brief Visit the elements via ExprOperand wrappers.
2053   ///
2054   /// These range iterators visit elements through \a ExprOperand wrappers.
2055   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2056   /// true.
2057   ///
2058   /// \pre \a isValid() gives \c true.
2059   /// @{
2060   expr_op_iterator expr_op_begin() const {
2061     return expr_op_iterator(elements_begin());
2062   }
2063   expr_op_iterator expr_op_end() const {
2064     return expr_op_iterator(elements_end());
2065   }
2066   /// @}
2067
2068   bool isValid() const;
2069
2070   static bool classof(const Metadata *MD) {
2071     return MD->getMetadataID() == DIExpressionKind;
2072   }
2073 };
2074
2075 class DIObjCProperty : public DINode {
2076   friend class LLVMContextImpl;
2077   friend class MDNode;
2078
2079   unsigned Line;
2080   unsigned Attributes;
2081
2082   DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2083                  unsigned Attributes, ArrayRef<Metadata *> Ops)
2084       : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2085                Ops),
2086         Line(Line), Attributes(Attributes) {}
2087   ~DIObjCProperty() = default;
2088
2089   static DIObjCProperty *
2090   getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2091           StringRef GetterName, StringRef SetterName, unsigned Attributes,
2092           DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2093     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2094                    getCanonicalMDString(Context, GetterName),
2095                    getCanonicalMDString(Context, SetterName), Attributes, Type,
2096                    Storage, ShouldCreate);
2097   }
2098   static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2099                                  Metadata *File, unsigned Line,
2100                                  MDString *GetterName, MDString *SetterName,
2101                                  unsigned Attributes, Metadata *Type,
2102                                  StorageType Storage, bool ShouldCreate = true);
2103
2104   TempDIObjCProperty cloneImpl() const {
2105     return getTemporary(getContext(), getName(), getFile(), getLine(),
2106                         getGetterName(), getSetterName(), getAttributes(),
2107                         getType());
2108   }
2109
2110 public:
2111   DEFINE_MDNODE_GET(DIObjCProperty,
2112                     (StringRef Name, DIFile *File, unsigned Line,
2113                      StringRef GetterName, StringRef SetterName,
2114                      unsigned Attributes, DIType *Type),
2115                     (Name, File, Line, GetterName, SetterName, Attributes,
2116                      Type))
2117   DEFINE_MDNODE_GET(DIObjCProperty,
2118                     (MDString * Name, Metadata *File, unsigned Line,
2119                      MDString *GetterName, MDString *SetterName,
2120                      unsigned Attributes, Metadata *Type),
2121                     (Name, File, Line, GetterName, SetterName, Attributes,
2122                      Type))
2123
2124   TempDIObjCProperty clone() const { return cloneImpl(); }
2125
2126   unsigned getLine() const { return Line; }
2127   unsigned getAttributes() const { return Attributes; }
2128   StringRef getName() const { return getStringOperand(0); }
2129   DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2130   StringRef getGetterName() const { return getStringOperand(2); }
2131   StringRef getSetterName() const { return getStringOperand(3); }
2132
2133   /// \brief Get the type.
2134   ///
2135   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
2136   /// a type ref here.
2137   DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2138
2139   StringRef getFilename() const {
2140     if (auto *F = getFile())
2141       return F->getFilename();
2142     return "";
2143   }
2144   StringRef getDirectory() const {
2145     if (auto *F = getFile())
2146       return F->getDirectory();
2147     return "";
2148   }
2149
2150   MDString *getRawName() const { return getOperandAs<MDString>(0); }
2151   Metadata *getRawFile() const { return getOperand(1); }
2152   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2153   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2154   Metadata *getRawType() const { return getOperand(4); }
2155
2156   static bool classof(const Metadata *MD) {
2157     return MD->getMetadataID() == DIObjCPropertyKind;
2158   }
2159 };
2160
2161 /// \brief An imported module (C++ using directive or similar).
2162 class DIImportedEntity : public DINode {
2163   friend class LLVMContextImpl;
2164   friend class MDNode;
2165
2166   unsigned Line;
2167
2168   DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2169                    unsigned Line, ArrayRef<Metadata *> Ops)
2170       : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2171   ~DIImportedEntity() = default;
2172
2173   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2174                                    DIScope *Scope, DINodeRef Entity,
2175                                    unsigned Line, StringRef Name,
2176                                    StorageType Storage,
2177                                    bool ShouldCreate = true) {
2178     return getImpl(Context, Tag, Scope, Entity, Line,
2179                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2180   }
2181   static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2182                                    Metadata *Scope, Metadata *Entity,
2183                                    unsigned Line, MDString *Name,
2184                                    StorageType Storage,
2185                                    bool ShouldCreate = true);
2186
2187   TempDIImportedEntity cloneImpl() const {
2188     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2189                         getLine(), getName());
2190   }
2191
2192 public:
2193   DEFINE_MDNODE_GET(DIImportedEntity,
2194                     (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2195                      unsigned Line, StringRef Name = ""),
2196                     (Tag, Scope, Entity, Line, Name))
2197   DEFINE_MDNODE_GET(DIImportedEntity,
2198                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
2199                      unsigned Line, MDString *Name),
2200                     (Tag, Scope, Entity, Line, Name))
2201
2202   TempDIImportedEntity clone() const { return cloneImpl(); }
2203
2204   unsigned getLine() const { return Line; }
2205   DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2206   DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2207   StringRef getName() const { return getStringOperand(2); }
2208
2209   Metadata *getRawScope() const { return getOperand(0); }
2210   Metadata *getRawEntity() const { return getOperand(1); }
2211   MDString *getRawName() const { return getOperandAs<MDString>(2); }
2212
2213   static bool classof(const Metadata *MD) {
2214     return MD->getMetadataID() == DIImportedEntityKind;
2215   }
2216 };
2217
2218 } // end namespace llvm
2219
2220 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2221 #undef DEFINE_MDNODE_GET_UNPACK
2222 #undef DEFINE_MDNODE_GET
2223
2224 #endif