DebugInfo: Create MDTypeRef, etc., to replace DITypeRef
[oota-llvm.git] / include / llvm / IR / DebugInfoMetadata.h
1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Declarations for metadata specific to debug info.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
15 #define LLVM_IR_DEBUGINFOMETADATA_H
16
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Dwarf.h"
19
20 // Helper macros for defining get() overrides.
21 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
22 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
23 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)                                 \
24   static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) {  \
25     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued);          \
26   }                                                                            \
27   static CLASS *getIfExists(LLVMContext &Context,                              \
28                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
29     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued,           \
30                    /* ShouldCreate */ false);                                  \
31   }                                                                            \
32   static CLASS *getDistinct(LLVMContext &Context,                              \
33                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
34     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct);         \
35   }                                                                            \
36   static Temp##CLASS getTemporary(LLVMContext &Context,                        \
37                                   DEFINE_MDNODE_GET_UNPACK(FORMAL)) {          \
38     return Temp##CLASS(                                                        \
39         getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary));          \
40   }
41
42 namespace llvm {
43
44 /// \brief Pointer union between a subclass of DebugNode and MDString.
45 ///
46 /// \a MDCompositeType 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 TypedDebugNodeRef {
50   const Metadata *MD = nullptr;
51
52 public:
53   TypedDebugNodeRef(std::nullptr_t) {}
54
55   /// \brief Construct from a raw pointer.
56   explicit TypedDebugNodeRef(const Metadata *MD) : MD(MD) {
57     assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
58   }
59
60   template <class U>
61   TypedDebugNodeRef(
62       const TypedDebugNodeRef<U> &X,
63       typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
64           nullptr)
65       : MD(X) {}
66
67   operator Metadata *() const { return const_cast<Metadata *>(MD); }
68
69   bool operator==(const TypedDebugNodeRef<T> &X) const { return MD == X.MD; };
70   bool operator!=(const TypedDebugNodeRef<T> &X) const { return MD != X.MD; };
71
72   /// \brief Create a reference.
73   ///
74   /// Get a reference to \c N, using an \a MDString reference if available.
75   static TypedDebugNodeRef get(const T *N);
76
77   template <class MapTy> T *resolve(const MapTy &Map) const {
78     if (auto *Typed = dyn_cast<T>(MD))
79       return const_cast<T *>(Typed);
80
81     auto *S = cast<MDString>(MD);
82     auto I = Map.find(S);
83     assert(I != Map.end() && "Missing identifier in type map");
84     return cast<T>(I->second);
85   }
86 };
87
88 typedef TypedDebugNodeRef<DebugNode> DebugNodeRef;
89 typedef TypedDebugNodeRef<MDScope> MDScopeRef;
90 typedef TypedDebugNodeRef<MDType> MDTypeRef;
91
92 /// \brief Tagged DWARF-like metadata node.
93 ///
94 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
95 /// defined in llvm/Support/Dwarf.h).  Called \a DebugNode because it's
96 /// potentially used for non-DWARF output.
97 class DebugNode : public MDNode {
98   friend class LLVMContextImpl;
99   friend class MDNode;
100
101 protected:
102   DebugNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
103             ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
104       : MDNode(C, ID, Storage, Ops1, Ops2) {
105     assert(Tag < 1u << 16);
106     SubclassData16 = Tag;
107   }
108   ~DebugNode() {}
109
110   template <class Ty> Ty *getOperandAs(unsigned I) const {
111     return cast_or_null<Ty>(getOperand(I));
112   }
113
114   StringRef getStringOperand(unsigned I) const {
115     if (auto *S = getOperandAs<MDString>(I))
116       return S->getString();
117     return StringRef();
118   }
119
120   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
121     if (S.empty())
122       return nullptr;
123     return MDString::get(Context, S);
124   }
125
126 public:
127   unsigned getTag() const { return SubclassData16; }
128
129   /// \brief Debug info flags.
130   ///
131   /// The three accessibility flags are mutually exclusive and rolled together
132   /// in the first two bits.
133   enum DIFlags {
134 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
135 #include "llvm/IR/DebugInfoFlags.def"
136     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
137   };
138
139   DebugNodeRef getRef() const { return DebugNodeRef::get(this); }
140
141   static bool classof(const Metadata *MD) {
142     switch (MD->getMetadataID()) {
143     default:
144       return false;
145     case GenericDebugNodeKind:
146     case MDSubrangeKind:
147     case MDEnumeratorKind:
148     case MDBasicTypeKind:
149     case MDDerivedTypeKind:
150     case MDCompositeTypeKind:
151     case MDSubroutineTypeKind:
152     case MDFileKind:
153     case MDCompileUnitKind:
154     case MDSubprogramKind:
155     case MDLexicalBlockKind:
156     case MDLexicalBlockFileKind:
157     case MDNamespaceKind:
158     case MDTemplateTypeParameterKind:
159     case MDTemplateValueParameterKind:
160     case MDGlobalVariableKind:
161     case MDLocalVariableKind:
162     case MDObjCPropertyKind:
163     case MDImportedEntityKind:
164       return true;
165     }
166   }
167 };
168
169 template <class T>
170 struct simplify_type<const TypedDebugNodeRef<T>> {
171   typedef Metadata *SimpleType;
172   static SimpleType getSimplifiedValue(const TypedDebugNodeRef<T> &MD) {
173     return MD;
174   }
175 };
176
177 template <class T>
178 struct simplify_type<TypedDebugNodeRef<T>>
179     : simplify_type<const TypedDebugNodeRef<T>> {};
180
181 /// \brief Generic tagged DWARF-like metadata node.
182 ///
183 /// An un-specialized DWARF-like metadata node.  The first operand is a
184 /// (possibly empty) null-separated \a MDString header that contains arbitrary
185 /// fields.  The remaining operands are \a dwarf_operands(), and are pointers
186 /// to other metadata.
187 class GenericDebugNode : public DebugNode {
188   friend class LLVMContextImpl;
189   friend class MDNode;
190
191   GenericDebugNode(LLVMContext &C, StorageType Storage, unsigned Hash,
192                    unsigned Tag, ArrayRef<Metadata *> Ops1,
193                    ArrayRef<Metadata *> Ops2)
194       : DebugNode(C, GenericDebugNodeKind, Storage, Tag, Ops1, Ops2) {
195     setHash(Hash);
196   }
197   ~GenericDebugNode() { dropAllReferences(); }
198
199   void setHash(unsigned Hash) { SubclassData32 = Hash; }
200   void recalculateHash();
201
202   static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
203                                    StringRef Header,
204                                    ArrayRef<Metadata *> DwarfOps,
205                                    StorageType Storage,
206                                    bool ShouldCreate = true) {
207     return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
208                    DwarfOps, Storage, ShouldCreate);
209   }
210
211   static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
212                                    MDString *Header,
213                                    ArrayRef<Metadata *> DwarfOps,
214                                    StorageType Storage,
215                                    bool ShouldCreate = true);
216
217   TempGenericDebugNode cloneImpl() const {
218     return getTemporary(
219         getContext(), getTag(), getHeader(),
220         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
221   }
222
223 public:
224   unsigned getHash() const { return SubclassData32; }
225
226   DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, StringRef Header,
227                                        ArrayRef<Metadata *> DwarfOps),
228                     (Tag, Header, DwarfOps))
229   DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, MDString *Header,
230                                        ArrayRef<Metadata *> DwarfOps),
231                     (Tag, Header, DwarfOps))
232
233   /// \brief Return a (temporary) clone of this.
234   TempGenericDebugNode clone() const { return cloneImpl(); }
235
236   unsigned getTag() const { return SubclassData16; }
237   StringRef getHeader() const { return getStringOperand(0); }
238
239   op_iterator dwarf_op_begin() const { return op_begin() + 1; }
240   op_iterator dwarf_op_end() const { return op_end(); }
241   op_range dwarf_operands() const {
242     return op_range(dwarf_op_begin(), dwarf_op_end());
243   }
244
245   unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
246   const MDOperand &getDwarfOperand(unsigned I) const {
247     return getOperand(I + 1);
248   }
249   void replaceDwarfOperandWith(unsigned I, Metadata *New) {
250     replaceOperandWith(I + 1, New);
251   }
252
253   static bool classof(const Metadata *MD) {
254     return MD->getMetadataID() == GenericDebugNodeKind;
255   }
256 };
257
258 /// \brief Array subrange.
259 ///
260 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
261 /// type.
262 class MDSubrange : public DebugNode {
263   friend class LLVMContextImpl;
264   friend class MDNode;
265
266   int64_t Count;
267   int64_t Lo;
268
269   MDSubrange(LLVMContext &C, StorageType Storage, int64_t Count, int64_t Lo)
270       : DebugNode(C, MDSubrangeKind, Storage, dwarf::DW_TAG_subrange_type,
271                   None),
272         Count(Count), Lo(Lo) {}
273   ~MDSubrange() {}
274
275   static MDSubrange *getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
276                              StorageType Storage, bool ShouldCreate = true);
277
278   TempMDSubrange cloneImpl() const {
279     return getTemporary(getContext(), getCount(), getLo());
280   }
281
282 public:
283   DEFINE_MDNODE_GET(MDSubrange, (int64_t Count, int64_t Lo = 0), (Count, Lo))
284
285   TempMDSubrange clone() const { return cloneImpl(); }
286
287   int64_t getLo() const { return Lo; }
288   int64_t getCount() const { return Count; }
289
290   static bool classof(const Metadata *MD) {
291     return MD->getMetadataID() == MDSubrangeKind;
292   }
293 };
294
295 /// \brief Enumeration value.
296 ///
297 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
298 /// longer creates a type cycle.
299 class MDEnumerator : public DebugNode {
300   friend class LLVMContextImpl;
301   friend class MDNode;
302
303   int64_t Value;
304
305   MDEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
306                ArrayRef<Metadata *> Ops)
307       : DebugNode(C, MDEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
308         Value(Value) {}
309   ~MDEnumerator() {}
310
311   static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
312                                StringRef Name, StorageType Storage,
313                                bool ShouldCreate = true) {
314     return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
315                    ShouldCreate);
316   }
317   static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
318                                MDString *Name, StorageType Storage,
319                                bool ShouldCreate = true);
320
321   TempMDEnumerator cloneImpl() const {
322     return getTemporary(getContext(), getValue(), getName());
323   }
324
325 public:
326   DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, StringRef Name),
327                     (Value, Name))
328   DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, MDString *Name),
329                     (Value, Name))
330
331   TempMDEnumerator clone() const { return cloneImpl(); }
332
333   int64_t getValue() const { return Value; }
334   StringRef getName() const { return getStringOperand(0); }
335
336   MDString *getRawName() const { return getOperandAs<MDString>(0); }
337
338   static bool classof(const Metadata *MD) {
339     return MD->getMetadataID() == MDEnumeratorKind;
340   }
341 };
342
343 /// \brief Base class for scope-like contexts.
344 ///
345 /// Base class for lexical scopes and types (which are also declaration
346 /// contexts).
347 ///
348 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
349 class MDScope : public DebugNode {
350 protected:
351   MDScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
352           ArrayRef<Metadata *> Ops)
353       : DebugNode(C, ID, Storage, Tag, Ops) {}
354   ~MDScope() {}
355
356 public:
357   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
358
359   /// \brief Return the raw underlying file.
360   ///
361   /// An \a MDFile is an \a MDScope, but it doesn't point at a separate file
362   /// (it\em is the file).  If \c this is an \a MDFile, we need to return \c
363   /// this.  Otherwise, return the first operand, which is where all other
364   /// subclasses store their file pointer.
365   Metadata *getRawFile() const {
366     return isa<MDFile>(this) ? const_cast<MDScope *>(this)
367                              : static_cast<Metadata *>(getOperand(0));
368   }
369
370   MDScopeRef getRef() const { return MDScopeRef::get(this); }
371
372   static bool classof(const Metadata *MD) {
373     switch (MD->getMetadataID()) {
374     default:
375       return false;
376     case MDBasicTypeKind:
377     case MDDerivedTypeKind:
378     case MDCompositeTypeKind:
379     case MDSubroutineTypeKind:
380     case MDFileKind:
381     case MDCompileUnitKind:
382     case MDSubprogramKind:
383     case MDLexicalBlockKind:
384     case MDLexicalBlockFileKind:
385     case MDNamespaceKind:
386       return true;
387     }
388   }
389 };
390
391 /// \brief File.
392 ///
393 /// TODO: Merge with directory/file node (including users).
394 /// TODO: Canonicalize paths on creation.
395 class MDFile : public MDScope {
396   friend class LLVMContextImpl;
397   friend class MDNode;
398
399   MDFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
400       : MDScope(C, MDFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
401   ~MDFile() {}
402
403   static MDFile *getImpl(LLVMContext &Context, StringRef Filename,
404                          StringRef Directory, StorageType Storage,
405                          bool ShouldCreate = true) {
406     return getImpl(Context, getCanonicalMDString(Context, Filename),
407                    getCanonicalMDString(Context, Directory), Storage,
408                    ShouldCreate);
409   }
410   static MDFile *getImpl(LLVMContext &Context, MDString *Filename,
411                          MDString *Directory, StorageType Storage,
412                          bool ShouldCreate = true);
413
414   TempMDFile cloneImpl() const {
415     return getTemporary(getContext(), getFilename(), getDirectory());
416   }
417
418 public:
419   DEFINE_MDNODE_GET(MDFile, (StringRef Filename, StringRef Directory),
420                     (Filename, Directory))
421   DEFINE_MDNODE_GET(MDFile, (MDString * Filename, MDString *Directory),
422                     (Filename, Directory))
423
424   TempMDFile clone() const { return cloneImpl(); }
425
426   StringRef getFilename() const { return getStringOperand(0); }
427   StringRef getDirectory() const { return getStringOperand(1); }
428
429   MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
430   MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
431
432   static bool classof(const Metadata *MD) {
433     return MD->getMetadataID() == MDFileKind;
434   }
435 };
436
437 /// \brief Base class for types.
438 ///
439 /// TODO: Remove the hardcoded name and context, since many types don't use
440 /// them.
441 /// TODO: Split up flags.
442 class MDType : public MDScope {
443   unsigned Line;
444   unsigned Flags;
445   uint64_t SizeInBits;
446   uint64_t AlignInBits;
447   uint64_t OffsetInBits;
448
449 protected:
450   MDType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
451          unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
452          uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
453       : MDScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
454         SizeInBits(SizeInBits), AlignInBits(AlignInBits),
455         OffsetInBits(OffsetInBits) {}
456   ~MDType() {}
457
458 public:
459   TempMDType clone() const {
460     return TempMDType(cast<MDType>(MDNode::clone().release()));
461   }
462
463   unsigned getLine() const { return Line; }
464   uint64_t getSizeInBits() const { return SizeInBits; }
465   uint64_t getAlignInBits() const { return AlignInBits; }
466   uint64_t getOffsetInBits() const { return OffsetInBits; }
467   unsigned getFlags() const { return Flags; }
468
469   Metadata *getScope() const { return getRawScope(); }
470   StringRef getName() const { return getStringOperand(2); }
471
472
473   Metadata *getRawScope() const { return getOperand(1); }
474   MDString *getRawName() const { return getOperandAs<MDString>(2); }
475
476   void setFlags(unsigned NewFlags) {
477     assert(!isUniqued() && "Cannot set flags on uniqued nodes");
478     Flags = NewFlags;
479   }
480
481   MDTypeRef getRef() const { return MDTypeRef::get(this); }
482
483   static bool classof(const Metadata *MD) {
484     switch (MD->getMetadataID()) {
485     default:
486       return false;
487     case MDBasicTypeKind:
488     case MDDerivedTypeKind:
489     case MDCompositeTypeKind:
490     case MDSubroutineTypeKind:
491       return true;
492     }
493   }
494 };
495
496 /// \brief Basic type.
497 ///
498 /// TODO: Split out DW_TAG_unspecified_type.
499 /// TODO: Drop unused accessors.
500 class MDBasicType : public MDType {
501   friend class LLVMContextImpl;
502   friend class MDNode;
503
504   unsigned Encoding;
505
506   MDBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
507               uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
508               ArrayRef<Metadata *> Ops)
509       : MDType(C, MDBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
510                0, Ops),
511         Encoding(Encoding) {}
512   ~MDBasicType() {}
513
514   static MDBasicType *getImpl(LLVMContext &Context, unsigned Tag,
515                               StringRef Name, uint64_t SizeInBits,
516                               uint64_t AlignInBits, unsigned Encoding,
517                               StorageType Storage, bool ShouldCreate = true) {
518     return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
519                    SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
520   }
521   static MDBasicType *getImpl(LLVMContext &Context, unsigned Tag,
522                               MDString *Name, uint64_t SizeInBits,
523                               uint64_t AlignInBits, unsigned Encoding,
524                               StorageType Storage, bool ShouldCreate = true);
525
526   TempMDBasicType cloneImpl() const {
527     return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
528                         getAlignInBits(), getEncoding());
529   }
530
531 public:
532   DEFINE_MDNODE_GET(MDBasicType, (unsigned Tag, StringRef Name),
533                     (Tag, Name, 0, 0, 0))
534   DEFINE_MDNODE_GET(MDBasicType,
535                     (unsigned Tag, StringRef Name, uint64_t SizeInBits,
536                      uint64_t AlignInBits, unsigned Encoding),
537                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
538   DEFINE_MDNODE_GET(MDBasicType,
539                     (unsigned Tag, MDString *Name, uint64_t SizeInBits,
540                      uint64_t AlignInBits, unsigned Encoding),
541                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
542
543   TempMDBasicType clone() const { return cloneImpl(); }
544
545   unsigned getEncoding() const { return Encoding; }
546
547   static bool classof(const Metadata *MD) {
548     return MD->getMetadataID() == MDBasicTypeKind;
549   }
550 };
551
552 /// \brief Base class for MDDerivedType and MDCompositeType.
553 ///
554 /// TODO: Delete; they're not really related.
555 class MDDerivedTypeBase : public MDType {
556 protected:
557   MDDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
558                     unsigned Tag, unsigned Line, uint64_t SizeInBits,
559                     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
560                     ArrayRef<Metadata *> Ops)
561       : MDType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
562                Flags, Ops) {}
563   ~MDDerivedTypeBase() {}
564
565 public:
566   Metadata *getBaseType() const { return getRawBaseType(); }
567   Metadata *getRawBaseType() const { return getOperand(3); }
568
569   static bool classof(const Metadata *MD) {
570     return MD->getMetadataID() == MDDerivedTypeKind ||
571            MD->getMetadataID() == MDCompositeTypeKind ||
572            MD->getMetadataID() == MDSubroutineTypeKind;
573   }
574 };
575
576 /// \brief Derived types.
577 ///
578 /// This includes qualified types, pointers, references, friends, typedefs, and
579 /// class members.
580 ///
581 /// TODO: Split out members (inheritance, fields, methods, etc.).
582 class MDDerivedType : public MDDerivedTypeBase {
583   friend class LLVMContextImpl;
584   friend class MDNode;
585
586   MDDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
587                 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
588                 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
589       : MDDerivedTypeBase(C, MDDerivedTypeKind, Storage, Tag, Line, SizeInBits,
590                           AlignInBits, OffsetInBits, Flags, Ops) {}
591   ~MDDerivedType() {}
592
593   static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
594                                 StringRef Name, MDFile *File, unsigned Line,
595                                 Metadata *Scope, Metadata *BaseType,
596                                 uint64_t SizeInBits, uint64_t AlignInBits,
597                                 uint64_t OffsetInBits, unsigned Flags,
598                                 Metadata *ExtraData, StorageType Storage,
599                                 bool ShouldCreate = true) {
600     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
601                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
602                    Flags, ExtraData, Storage, ShouldCreate);
603   }
604   static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
605                                 MDString *Name, Metadata *File, unsigned Line,
606                                 Metadata *Scope, Metadata *BaseType,
607                                 uint64_t SizeInBits, uint64_t AlignInBits,
608                                 uint64_t OffsetInBits, unsigned Flags,
609                                 Metadata *ExtraData, StorageType Storage,
610                                 bool ShouldCreate = true);
611
612   TempMDDerivedType cloneImpl() const {
613     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
614                         getScope(), getBaseType(), getSizeInBits(),
615                         getAlignInBits(), getOffsetInBits(), getFlags(),
616                         getExtraData());
617   }
618
619 public:
620   DEFINE_MDNODE_GET(MDDerivedType,
621                     (unsigned Tag, MDString *Name, Metadata *File,
622                      unsigned Line, Metadata *Scope, Metadata *BaseType,
623                      uint64_t SizeInBits, uint64_t AlignInBits,
624                      uint64_t OffsetInBits, unsigned Flags,
625                      Metadata *ExtraData = nullptr),
626                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
627                      AlignInBits, OffsetInBits, Flags, ExtraData))
628   DEFINE_MDNODE_GET(MDDerivedType,
629                     (unsigned Tag, StringRef Name, MDFile *File,
630                      unsigned Line, Metadata *Scope, Metadata *BaseType,
631                      uint64_t SizeInBits, uint64_t AlignInBits,
632                      uint64_t OffsetInBits, unsigned Flags,
633                      Metadata *ExtraData = nullptr),
634                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
635                      AlignInBits, OffsetInBits, Flags, ExtraData))
636
637   TempMDDerivedType clone() const { return cloneImpl(); }
638
639   /// \brief Get extra data associated with this derived type.
640   ///
641   /// Class type for pointer-to-members, objective-c property node for ivars,
642   /// or global constant wrapper for static members.
643   ///
644   /// TODO: Separate out types that need this extra operand: pointer-to-member
645   /// types and member fields (static members and ivars).
646   Metadata *getExtraData() const { return getRawExtraData(); }
647   Metadata *getRawExtraData() const { return getOperand(4); }
648
649   static bool classof(const Metadata *MD) {
650     return MD->getMetadataID() == MDDerivedTypeKind;
651   }
652 };
653
654 /// \brief Base class for MDCompositeType and MDSubroutineType.
655 ///
656 /// TODO: Delete; they're not really related.
657 class MDCompositeTypeBase : public MDDerivedTypeBase {
658   unsigned RuntimeLang;
659
660 protected:
661   MDCompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
662                       unsigned Tag, unsigned Line, unsigned RuntimeLang,
663                       uint64_t SizeInBits, uint64_t AlignInBits,
664                       uint64_t OffsetInBits, unsigned Flags,
665                       ArrayRef<Metadata *> Ops)
666       : MDDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
667                           OffsetInBits, Flags, Ops),
668         RuntimeLang(RuntimeLang) {}
669   ~MDCompositeTypeBase() {}
670
671 public:
672   MDTuple *getElements() const {
673     return cast_or_null<MDTuple>(getRawElements());
674   }
675   Metadata *getVTableHolder() const { return getRawVTableHolder(); }
676   MDTemplateParameterArray getTemplateParams() const {
677     return cast_or_null<MDTuple>(getRawTemplateParams());
678   }
679   StringRef getIdentifier() const { return getStringOperand(7); }
680   unsigned getRuntimeLang() const { return RuntimeLang; }
681
682   Metadata *getRawElements() const { return getOperand(4); }
683   Metadata *getRawVTableHolder() const { return getOperand(5); }
684   Metadata *getRawTemplateParams() const { return getOperand(6); }
685   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
686
687   /// \brief Replace operands.
688   ///
689   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
690   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
691   /// of its movement if necessary.
692   /// @{
693   void replaceElements(MDTuple *Elements) {
694 #ifndef NDEBUG
695     if (auto *Old = cast_or_null<MDTuple>(getElements()))
696       for (const auto &Op : Old->operands())
697         assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
698                "Lost a member during member list replacement");
699 #endif
700     replaceOperandWith(4, Elements);
701   }
702   void replaceVTableHolder(Metadata *VTableHolder) {
703     replaceOperandWith(5, VTableHolder);
704   }
705   void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
706     replaceOperandWith(6, TemplateParams);
707   }
708   /// @}
709
710   static bool classof(const Metadata *MD) {
711     return MD->getMetadataID() == MDCompositeTypeKind ||
712            MD->getMetadataID() == MDSubroutineTypeKind;
713   }
714 };
715
716 /// \brief Composite types.
717 ///
718 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
719 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
720 class MDCompositeType : public MDCompositeTypeBase {
721   friend class LLVMContextImpl;
722   friend class MDNode;
723
724   MDCompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
725                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
726                   uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
727                   ArrayRef<Metadata *> Ops)
728       : MDCompositeTypeBase(C, MDCompositeTypeKind, Storage, Tag, Line,
729                             RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
730                             Flags, Ops) {}
731   ~MDCompositeType() {}
732
733   static MDCompositeType *
734   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
735           unsigned Line, Metadata *Scope, Metadata *BaseType,
736           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
737           uint64_t Flags, Metadata *Elements, unsigned RuntimeLang,
738           Metadata *VTableHolder, Metadata *TemplateParams,
739           StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
740     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
741                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
742                    Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
743                    getCanonicalMDString(Context, Identifier), Storage,
744                    ShouldCreate);
745   }
746   static MDCompositeType *
747   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
748           unsigned Line, Metadata *Scope, Metadata *BaseType,
749           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
750           unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
751           Metadata *VTableHolder, Metadata *TemplateParams,
752           MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
753
754   TempMDCompositeType cloneImpl() const {
755     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
756                         getScope(), getBaseType(), getSizeInBits(),
757                         getAlignInBits(), getOffsetInBits(), getFlags(),
758                         getElements(), getRuntimeLang(), getVTableHolder(),
759                         getTemplateParams(), getIdentifier());
760   }
761
762 public:
763   DEFINE_MDNODE_GET(MDCompositeType,
764                     (unsigned Tag, StringRef Name, Metadata *File,
765                      unsigned Line, Metadata *Scope, Metadata *BaseType,
766                      uint64_t SizeInBits, uint64_t AlignInBits,
767                      uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
768                      unsigned RuntimeLang, Metadata *VTableHolder,
769                      Metadata *TemplateParams = nullptr,
770                      StringRef Identifier = ""),
771                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
772                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
773                      VTableHolder, TemplateParams, Identifier))
774   DEFINE_MDNODE_GET(MDCompositeType,
775                     (unsigned Tag, MDString *Name, Metadata *File,
776                      unsigned Line, Metadata *Scope, Metadata *BaseType,
777                      uint64_t SizeInBits, uint64_t AlignInBits,
778                      uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
779                      unsigned RuntimeLang, Metadata *VTableHolder,
780                      Metadata *TemplateParams = nullptr,
781                      MDString *Identifier = nullptr),
782                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
783                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
784                      VTableHolder, TemplateParams, Identifier))
785
786   TempMDCompositeType clone() const { return cloneImpl(); }
787
788   static bool classof(const Metadata *MD) {
789     return MD->getMetadataID() == MDCompositeTypeKind;
790   }
791 };
792
793 template <class T> TypedDebugNodeRef<T> TypedDebugNodeRef<T>::get(const T *N) {
794   if (N)
795     if (auto *Composite = dyn_cast<MDCompositeType>(N))
796       if (auto *S = Composite->getRawIdentifier())
797         return TypedDebugNodeRef<T>(S);
798   return TypedDebugNodeRef<T>(N);
799 }
800
801 /// \brief Type array for a subprogram.
802 ///
803 /// TODO: Detach from CompositeType, and fold the array of types in directly
804 /// as operands.
805 class MDSubroutineType : public MDCompositeTypeBase {
806   friend class LLVMContextImpl;
807   friend class MDNode;
808
809   MDSubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
810                    ArrayRef<Metadata *> Ops)
811       : MDCompositeTypeBase(C, MDSubroutineTypeKind, Storage,
812                             dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
813                             Ops) {}
814   ~MDSubroutineType() {}
815
816   static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
817                                    Metadata *TypeArray, StorageType Storage,
818                                    bool ShouldCreate = true);
819
820   TempMDSubroutineType cloneImpl() const {
821     return getTemporary(getContext(), getFlags(), getTypeArray());
822   }
823
824 public:
825   DEFINE_MDNODE_GET(MDSubroutineType, (unsigned Flags, Metadata *TypeArray),
826                     (Flags, TypeArray))
827
828   TempMDSubroutineType clone() const { return cloneImpl(); }
829
830   MDTuple *getTypeArray() const { return getElements(); }
831   Metadata *getRawTypeArray() const { return getRawElements(); }
832
833   static bool classof(const Metadata *MD) {
834     return MD->getMetadataID() == MDSubroutineTypeKind;
835   }
836 };
837
838 /// \brief Compile unit.
839 class MDCompileUnit : public MDScope {
840   friend class LLVMContextImpl;
841   friend class MDNode;
842
843   unsigned SourceLanguage;
844   bool IsOptimized;
845   unsigned RuntimeVersion;
846   unsigned EmissionKind;
847
848   MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
849                 bool IsOptimized, unsigned RuntimeVersion,
850                 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
851       : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
852         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
853         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
854   ~MDCompileUnit() {}
855
856   static MDCompileUnit *
857   getImpl(LLVMContext &Context, unsigned SourceLanguage, MDFile *File,
858           StringRef Producer, bool IsOptimized, StringRef Flags,
859           unsigned RuntimeVersion, StringRef SplitDebugFilename,
860           unsigned EmissionKind, MDTuple *EnumTypes, MDTuple *RetainedTypes,
861           MDTuple *Subprograms, MDTuple *GlobalVariables,
862           MDTuple *ImportedEntities, StorageType Storage,
863           bool ShouldCreate = true) {
864     return getImpl(Context, SourceLanguage, File,
865                    getCanonicalMDString(Context, Producer), IsOptimized,
866                    getCanonicalMDString(Context, Flags), RuntimeVersion,
867                    getCanonicalMDString(Context, SplitDebugFilename),
868                    EmissionKind, EnumTypes, RetainedTypes, Subprograms,
869                    GlobalVariables, ImportedEntities, Storage, ShouldCreate);
870   }
871   static MDCompileUnit *
872   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
873           MDString *Producer, bool IsOptimized, MDString *Flags,
874           unsigned RuntimeVersion, MDString *SplitDebugFilename,
875           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
876           Metadata *Subprograms, Metadata *GlobalVariables,
877           Metadata *ImportedEntities, StorageType Storage,
878           bool ShouldCreate = true);
879
880   TempMDCompileUnit cloneImpl() const {
881     return getTemporary(
882         getContext(), getSourceLanguage(), getFile(), getProducer(),
883         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
884         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
885         getGlobalVariables(), getImportedEntities());
886   }
887
888 public:
889   DEFINE_MDNODE_GET(MDCompileUnit,
890                     (unsigned SourceLanguage, MDFile *File, StringRef Producer,
891                      bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
892                      StringRef SplitDebugFilename, unsigned EmissionKind,
893                      MDTuple *EnumTypes, MDTuple *RetainedTypes,
894                      MDTuple *Subprograms, MDTuple *GlobalVariables,
895                      MDTuple *ImportedEntities),
896                     (SourceLanguage, File, Producer, IsOptimized, Flags,
897                      RuntimeVersion, SplitDebugFilename, EmissionKind,
898                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
899                      ImportedEntities))
900   DEFINE_MDNODE_GET(MDCompileUnit,
901                     (unsigned SourceLanguage, Metadata *File,
902                      MDString *Producer, bool IsOptimized, MDString *Flags,
903                      unsigned RuntimeVersion, MDString *SplitDebugFilename,
904                      unsigned EmissionKind, Metadata *EnumTypes,
905                      Metadata *RetainedTypes, Metadata *Subprograms,
906                      Metadata *GlobalVariables, Metadata *ImportedEntities),
907                     (SourceLanguage, File, Producer, IsOptimized, Flags,
908                      RuntimeVersion, SplitDebugFilename, EmissionKind,
909                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
910                      ImportedEntities))
911
912   TempMDCompileUnit clone() const { return cloneImpl(); }
913
914   unsigned getSourceLanguage() const { return SourceLanguage; }
915   bool isOptimized() const { return IsOptimized; }
916   unsigned getRuntimeVersion() const { return RuntimeVersion; }
917   unsigned getEmissionKind() const { return EmissionKind; }
918   StringRef getProducer() const { return getStringOperand(1); }
919   StringRef getFlags() const { return getStringOperand(2); }
920   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
921   MDCompositeTypeArray getEnumTypes() const {
922     return cast_or_null<MDTuple>(getRawEnumTypes());
923   }
924   MDTypeArray getRetainedTypes() const {
925     return cast_or_null<MDTuple>(getRawRetainedTypes());
926   }
927   MDSubprogramArray getSubprograms() const {
928     return cast_or_null<MDTuple>(getRawSubprograms());
929   }
930   MDGlobalVariableArray getGlobalVariables() const {
931     return cast_or_null<MDTuple>(getRawGlobalVariables());
932   }
933   MDImportedEntityArray getImportedEntities() const {
934     return cast_or_null<MDTuple>(getRawImportedEntities());
935   }
936
937   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
938   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
939   MDString *getRawSplitDebugFilename() const {
940     return getOperandAs<MDString>(3);
941   }
942   Metadata *getRawEnumTypes() const { return getOperand(4); }
943   Metadata *getRawRetainedTypes() const { return getOperand(5); }
944   Metadata *getRawSubprograms() const { return getOperand(6); }
945   Metadata *getRawGlobalVariables() const { return getOperand(7); }
946   Metadata *getRawImportedEntities() const { return getOperand(8); }
947
948   /// \brief Replace arrays.
949   ///
950   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
951   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
952   /// MDCompileUnit should be fairly rare.
953   /// @{
954   void replaceSubprograms(MDTuple *N) { replaceOperandWith(6, N); }
955   void replaceGlobalVariables(MDTuple *N) { replaceOperandWith(7, N); }
956   /// @}
957
958   static bool classof(const Metadata *MD) {
959     return MD->getMetadataID() == MDCompileUnitKind;
960   }
961 };
962
963 /// \brief A scope for locals.
964 ///
965 /// A legal scope for lexical blocks, local variables, and debug info
966 /// locations.  Subclasses are \a MDSubprogram, \a MDLexicalBlock, and \a
967 /// MDLexicalBlockFile.
968 class MDLocalScope : public MDScope {
969 protected:
970   MDLocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
971                ArrayRef<Metadata *> Ops)
972       : MDScope(C, ID, Storage, Tag, Ops) {}
973   ~MDLocalScope() {}
974
975 public:
976   /// \brief Get the subprogram for this scope.
977   ///
978   /// Return this if it's an \a MDSubprogram; otherwise, look up the scope
979   /// chain.
980   MDSubprogram *getSubprogram() const;
981
982   static bool classof(const Metadata *MD) {
983     return MD->getMetadataID() == MDSubprogramKind ||
984            MD->getMetadataID() == MDLexicalBlockKind ||
985            MD->getMetadataID() == MDLexicalBlockFileKind;
986   }
987 };
988
989 /// \brief Debug location.
990 ///
991 /// A debug location in source code, used for debug info and otherwise.
992 class MDLocation : public MDNode {
993   friend class LLVMContextImpl;
994   friend class MDNode;
995
996   MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
997              unsigned Column, ArrayRef<Metadata *> MDs);
998   ~MDLocation() { dropAllReferences(); }
999
1000   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1001                              unsigned Column, Metadata *Scope,
1002                              Metadata *InlinedAt, StorageType Storage,
1003                              bool ShouldCreate = true);
1004   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1005                              unsigned Column, MDLocalScope *Scope,
1006                              MDLocation *InlinedAt, StorageType Storage,
1007                              bool ShouldCreate = true) {
1008     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1009                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1010   }
1011
1012   TempMDLocation cloneImpl() const {
1013     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1014                         getInlinedAt());
1015   }
1016
1017   // Disallow replacing operands.
1018   void replaceOperandWith(unsigned I, Metadata *New) = delete;
1019
1020 public:
1021   DEFINE_MDNODE_GET(MDLocation,
1022                     (unsigned Line, unsigned Column, Metadata *Scope,
1023                      Metadata *InlinedAt = nullptr),
1024                     (Line, Column, Scope, InlinedAt))
1025   DEFINE_MDNODE_GET(MDLocation,
1026                     (unsigned Line, unsigned Column, MDLocalScope *Scope,
1027                      MDLocation *InlinedAt = nullptr),
1028                     (Line, Column, Scope, InlinedAt))
1029
1030   /// \brief Return a (temporary) clone of this.
1031   TempMDLocation clone() const { return cloneImpl(); }
1032
1033   unsigned getLine() const { return SubclassData32; }
1034   unsigned getColumn() const { return SubclassData16; }
1035   MDLocalScope *getScope() const {
1036     return cast_or_null<MDLocalScope>(getRawScope());
1037   }
1038   MDLocation *getInlinedAt() const {
1039     return cast_or_null<MDLocation>(getRawInlinedAt());
1040   }
1041
1042   /// \brief Get the scope where this is inlined.
1043   ///
1044   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1045   /// location.
1046   MDLocalScope *getInlinedAtScope() const {
1047     if (auto *IA = getInlinedAt())
1048       return IA->getInlinedAtScope();
1049     return getScope();
1050   }
1051
1052   Metadata *getRawScope() const { return getOperand(0); }
1053   Metadata *getRawInlinedAt() const {
1054     if (getNumOperands() == 2)
1055       return getOperand(1);
1056     return nullptr;
1057   }
1058
1059   static bool classof(const Metadata *MD) {
1060     return MD->getMetadataID() == MDLocationKind;
1061   }
1062 };
1063
1064 /// \brief Subprogram description.
1065 ///
1066 /// TODO: Remove DisplayName.  It's always equal to Name.
1067 /// TODO: Split up flags.
1068 class MDSubprogram : public MDLocalScope {
1069   friend class LLVMContextImpl;
1070   friend class MDNode;
1071
1072   unsigned Line;
1073   unsigned ScopeLine;
1074   unsigned Virtuality;
1075   unsigned VirtualIndex;
1076   unsigned Flags;
1077   bool IsLocalToUnit;
1078   bool IsDefinition;
1079   bool IsOptimized;
1080
1081   MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1082                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1083                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1084                bool IsOptimized, ArrayRef<Metadata *> Ops)
1085       : MDLocalScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1086                      Ops),
1087         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1088         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1089         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1090   ~MDSubprogram() {}
1091
1092   static MDSubprogram *
1093   getImpl(LLVMContext &Context, Metadata *Scope, StringRef Name,
1094           StringRef LinkageName, MDFile *File, unsigned Line,
1095           MDSubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1096           unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1097           unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1098           ConstantAsMetadata *Function, MDTuple *TemplateParams,
1099           MDSubprogram *Declaration, MDTuple *Variables, StorageType Storage,
1100           bool ShouldCreate = true) {
1101     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1102                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1103                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1104                    Virtuality, VirtualIndex, Flags, IsOptimized, Function,
1105                    TemplateParams, Declaration, Variables, Storage,
1106                    ShouldCreate);
1107   }
1108   static MDSubprogram *
1109   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1110           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1111           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1112           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1113           unsigned Flags, bool IsOptimized, Metadata *Function,
1114           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1115           StorageType Storage, bool ShouldCreate = true);
1116
1117   TempMDSubprogram cloneImpl() const {
1118     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1119                         getFile(), getLine(), getType(), isLocalToUnit(),
1120                         isDefinition(), getScopeLine(), getContainingType(),
1121                         getVirtuality(), getVirtualIndex(), getFlags(),
1122                         isOptimized(), getFunction(), getTemplateParams(),
1123                         getDeclaration(), getVariables());
1124   }
1125
1126 public:
1127   DEFINE_MDNODE_GET(
1128       MDSubprogram,
1129       (Metadata * Scope, StringRef Name, StringRef LinkageName, MDFile *File,
1130        unsigned Line, MDSubroutineType *Type, bool IsLocalToUnit,
1131        bool IsDefinition, unsigned ScopeLine, Metadata *ContainingType,
1132        unsigned Virtuality, unsigned VirtualIndex, unsigned Flags,
1133        bool IsOptimized, ConstantAsMetadata *Function = nullptr,
1134        MDTuple *TemplateParams = nullptr, MDSubprogram *Declaration = nullptr,
1135        MDTuple *Variables = nullptr),
1136       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1137        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1138        Function, TemplateParams, Declaration, Variables))
1139   DEFINE_MDNODE_GET(
1140       MDSubprogram,
1141       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1142        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1143        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1144        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1145        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1146        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1147       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1148        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1149        Function, TemplateParams, Declaration, Variables))
1150
1151   TempMDSubprogram clone() const { return cloneImpl(); }
1152
1153 public:
1154   unsigned getLine() const { return Line; }
1155   unsigned getVirtuality() const { return Virtuality; }
1156   unsigned getVirtualIndex() const { return VirtualIndex; }
1157   unsigned getScopeLine() const { return ScopeLine; }
1158   unsigned getFlags() const { return Flags; }
1159   bool isLocalToUnit() const { return IsLocalToUnit; }
1160   bool isDefinition() const { return IsDefinition; }
1161   bool isOptimized() const { return IsOptimized; }
1162
1163   Metadata *getScope() const { return getRawScope(); }
1164
1165   StringRef getName() const { return getStringOperand(2); }
1166   StringRef getDisplayName() const { return getStringOperand(3); }
1167   StringRef getLinkageName() const { return getStringOperand(4); }
1168
1169   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1170   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1171
1172   MDSubroutineType *getType() const {
1173     return cast_or_null<MDSubroutineType>(getRawType());
1174   }
1175   Metadata *getContainingType() const { return getRawContainingType(); }
1176
1177   ConstantAsMetadata *getFunction() const {
1178     return cast_or_null<ConstantAsMetadata>(getRawFunction());
1179   }
1180   MDTemplateParameterArray getTemplateParams() const {
1181     return cast_or_null<MDTuple>(getRawTemplateParams());
1182   }
1183   MDSubprogram *getDeclaration() const {
1184     return cast_or_null<MDSubprogram>(getRawDeclaration());
1185   }
1186   MDLocalVariableArray getVariables() const {
1187     return cast_or_null<MDTuple>(getRawVariables());
1188   }
1189
1190   Metadata *getRawScope() const { return getOperand(1); }
1191   Metadata *getRawType() const { return getOperand(5); }
1192   Metadata *getRawContainingType() const { return getOperand(6); }
1193   Metadata *getRawFunction() const { return getOperand(7); }
1194   Metadata *getRawTemplateParams() const { return getOperand(8); }
1195   Metadata *getRawDeclaration() const { return getOperand(9); }
1196   Metadata *getRawVariables() const { return getOperand(10); }
1197
1198   /// \brief Replace the function.
1199   ///
1200   /// If \a isUniqued() and not \a isResolved(), this could node will be
1201   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1202   /// that's a problem.
1203   /// @{
1204   void replaceFunction(Function *F);
1205   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1206   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1207   /// @}
1208
1209   static bool classof(const Metadata *MD) {
1210     return MD->getMetadataID() == MDSubprogramKind;
1211   }
1212 };
1213
1214 class MDLexicalBlockBase : public MDLocalScope {
1215 protected:
1216   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1217                      ArrayRef<Metadata *> Ops)
1218       : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1219   ~MDLexicalBlockBase() {}
1220
1221 public:
1222   MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
1223
1224   Metadata *getRawScope() const { return getOperand(1); }
1225
1226   static bool classof(const Metadata *MD) {
1227     return MD->getMetadataID() == MDLexicalBlockKind ||
1228            MD->getMetadataID() == MDLexicalBlockFileKind;
1229   }
1230 };
1231
1232 class MDLexicalBlock : public MDLexicalBlockBase {
1233   friend class LLVMContextImpl;
1234   friend class MDNode;
1235
1236   unsigned Line;
1237   unsigned Column;
1238
1239   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1240                  unsigned Column, ArrayRef<Metadata *> Ops)
1241       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1242         Column(Column) {}
1243   ~MDLexicalBlock() {}
1244
1245   static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1246                                  MDFile *File, unsigned Line, unsigned Column,
1247                                  StorageType Storage,
1248                                  bool ShouldCreate = true) {
1249     return getImpl(Context, static_cast<Metadata *>(Scope),
1250                    static_cast<Metadata *>(File), Line, Column, Storage,
1251                    ShouldCreate);
1252   }
1253
1254   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1255                                  Metadata *File, unsigned Line, unsigned Column,
1256                                  StorageType Storage, bool ShouldCreate = true);
1257
1258   TempMDLexicalBlock cloneImpl() const {
1259     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1260                         getColumn());
1261   }
1262
1263 public:
1264   DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1265                                      unsigned Line, unsigned Column),
1266                     (Scope, File, Line, Column))
1267   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1268                                      unsigned Line, unsigned Column),
1269                     (Scope, File, Line, Column))
1270
1271   TempMDLexicalBlock clone() const { return cloneImpl(); }
1272
1273   unsigned getLine() const { return Line; }
1274   unsigned getColumn() const { return Column; }
1275
1276   static bool classof(const Metadata *MD) {
1277     return MD->getMetadataID() == MDLexicalBlockKind;
1278   }
1279 };
1280
1281 class MDLexicalBlockFile : public MDLexicalBlockBase {
1282   friend class LLVMContextImpl;
1283   friend class MDNode;
1284
1285   unsigned Discriminator;
1286
1287   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1288                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1289       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1290         Discriminator(Discriminator) {}
1291   ~MDLexicalBlockFile() {}
1292
1293   static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1294                                      MDFile *File, unsigned Discriminator,
1295                                      StorageType Storage,
1296                                      bool ShouldCreate = true) {
1297     return getImpl(Context, static_cast<Metadata *>(Scope),
1298                    static_cast<Metadata *>(File), Discriminator, Storage,
1299                    ShouldCreate);
1300   }
1301
1302   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1303                                      Metadata *File, unsigned Discriminator,
1304                                      StorageType Storage,
1305                                      bool ShouldCreate = true);
1306
1307   TempMDLexicalBlockFile cloneImpl() const {
1308     return getTemporary(getContext(), getScope(), getFile(),
1309                         getDiscriminator());
1310   }
1311
1312 public:
1313   DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1314                                          unsigned Discriminator),
1315                     (Scope, File, Discriminator))
1316   DEFINE_MDNODE_GET(MDLexicalBlockFile,
1317                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1318                     (Scope, File, Discriminator))
1319
1320   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1321
1322   unsigned getDiscriminator() const { return Discriminator; }
1323
1324   static bool classof(const Metadata *MD) {
1325     return MD->getMetadataID() == MDLexicalBlockFileKind;
1326   }
1327 };
1328
1329 class MDNamespace : public MDScope {
1330   friend class LLVMContextImpl;
1331   friend class MDNode;
1332
1333   unsigned Line;
1334
1335   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1336               ArrayRef<Metadata *> Ops)
1337       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1338                 Ops),
1339         Line(Line) {}
1340   ~MDNamespace() {}
1341
1342   static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1343                               MDFile *File, StringRef Name, unsigned Line,
1344                               StorageType Storage, bool ShouldCreate = true) {
1345     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1346                    Line, Storage, ShouldCreate);
1347   }
1348   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1349                               Metadata *File, MDString *Name, unsigned Line,
1350                               StorageType Storage, bool ShouldCreate = true);
1351
1352   TempMDNamespace cloneImpl() const {
1353     return getTemporary(getContext(), getScope(), getFile(), getName(),
1354                         getLine());
1355   }
1356
1357 public:
1358   DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1359                                   unsigned Line),
1360                     (Scope, File, Name, Line))
1361   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1362                                   MDString *Name, unsigned Line),
1363                     (Scope, File, Name, Line))
1364
1365   TempMDNamespace clone() const { return cloneImpl(); }
1366
1367   unsigned getLine() const { return Line; }
1368   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1369   StringRef getName() const { return getStringOperand(2); }
1370
1371   Metadata *getRawScope() const { return getOperand(1); }
1372   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1373
1374   static bool classof(const Metadata *MD) {
1375     return MD->getMetadataID() == MDNamespaceKind;
1376   }
1377 };
1378
1379 /// \brief Base class for template parameters.
1380 class MDTemplateParameter : public DebugNode {
1381 protected:
1382   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1383                       unsigned Tag, ArrayRef<Metadata *> Ops)
1384       : DebugNode(Context, ID, Storage, Tag, Ops) {}
1385   ~MDTemplateParameter() {}
1386
1387 public:
1388   StringRef getName() const { return getStringOperand(0); }
1389   Metadata *getType() const { return getOperand(1); }
1390
1391   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1392
1393   static bool classof(const Metadata *MD) {
1394     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1395            MD->getMetadataID() == MDTemplateValueParameterKind;
1396   }
1397 };
1398
1399 class MDTemplateTypeParameter : public MDTemplateParameter {
1400   friend class LLVMContextImpl;
1401   friend class MDNode;
1402
1403   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1404                           ArrayRef<Metadata *> Ops)
1405       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1406                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1407   ~MDTemplateTypeParameter() {}
1408
1409   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1410                                           Metadata *Type, StorageType Storage,
1411                                           bool ShouldCreate = true) {
1412     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1413                    ShouldCreate);
1414   }
1415   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1416                                           Metadata *Type, StorageType Storage,
1417                                           bool ShouldCreate = true);
1418
1419   TempMDTemplateTypeParameter cloneImpl() const {
1420     return getTemporary(getContext(), getName(), getType());
1421   }
1422
1423 public:
1424   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, Metadata *Type),
1425                     (Name, Type))
1426   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1427                     (Name, Type))
1428
1429   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1430
1431   static bool classof(const Metadata *MD) {
1432     return MD->getMetadataID() == MDTemplateTypeParameterKind;
1433   }
1434 };
1435
1436 class MDTemplateValueParameter : public MDTemplateParameter {
1437   friend class LLVMContextImpl;
1438   friend class MDNode;
1439
1440   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1441                            unsigned Tag, ArrayRef<Metadata *> Ops)
1442       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1443                             Ops) {}
1444   ~MDTemplateValueParameter() {}
1445
1446   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1447                                            StringRef Name, Metadata *Type,
1448                                            Metadata *Value, StorageType Storage,
1449                                            bool ShouldCreate = true) {
1450     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1451                    Value, Storage, ShouldCreate);
1452   }
1453   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1454                                            MDString *Name, Metadata *Type,
1455                                            Metadata *Value, StorageType Storage,
1456                                            bool ShouldCreate = true);
1457
1458   TempMDTemplateValueParameter cloneImpl() const {
1459     return getTemporary(getContext(), getTag(), getName(), getType(),
1460                         getValue());
1461   }
1462
1463 public:
1464   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1465                                                Metadata *Type, Metadata *Value),
1466                     (Tag, Name, Type, Value))
1467   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1468                                                Metadata *Type, Metadata *Value),
1469                     (Tag, Name, Type, Value))
1470
1471   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1472
1473   Metadata *getValue() const { return getOperand(2); }
1474
1475   static bool classof(const Metadata *MD) {
1476     return MD->getMetadataID() == MDTemplateValueParameterKind;
1477   }
1478 };
1479
1480 /// \brief Base class for variables.
1481 ///
1482 /// TODO: Hardcode to DW_TAG_variable.
1483 class MDVariable : public DebugNode {
1484   unsigned Line;
1485
1486 protected:
1487   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1488              unsigned Line, ArrayRef<Metadata *> Ops)
1489       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1490   ~MDVariable() {}
1491
1492 public:
1493   unsigned getLine() const { return Line; }
1494   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1495   StringRef getName() const { return getStringOperand(1); }
1496   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1497   Metadata *getType() const { return getRawType(); }
1498
1499   Metadata *getRawScope() const { return getOperand(0); }
1500   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1501   Metadata *getRawFile() const { return getOperand(2); }
1502   Metadata *getRawType() const { return getOperand(3); }
1503
1504   static bool classof(const Metadata *MD) {
1505     return MD->getMetadataID() == MDLocalVariableKind ||
1506            MD->getMetadataID() == MDGlobalVariableKind;
1507   }
1508 };
1509
1510 /// \brief Global variables.
1511 ///
1512 /// TODO: Remove DisplayName.  It's always equal to Name.
1513 class MDGlobalVariable : public MDVariable {
1514   friend class LLVMContextImpl;
1515   friend class MDNode;
1516
1517   bool IsLocalToUnit;
1518   bool IsDefinition;
1519
1520   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1521                    bool IsLocalToUnit, bool IsDefinition,
1522                    ArrayRef<Metadata *> Ops)
1523       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1524                    Line, Ops),
1525         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1526   ~MDGlobalVariable() {}
1527
1528   static MDGlobalVariable *
1529   getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1530           StringRef LinkageName, MDFile *File, unsigned Line, Metadata *Type,
1531           bool IsLocalToUnit, bool IsDefinition, ConstantAsMetadata *Variable,
1532           MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1533           bool ShouldCreate = true) {
1534     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1535                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1536                    IsLocalToUnit, IsDefinition, Variable,
1537                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1538   }
1539   static MDGlobalVariable *
1540   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1541           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1542           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1543           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1544           bool ShouldCreate = true);
1545
1546   TempMDGlobalVariable cloneImpl() const {
1547     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1548                         getFile(), getLine(), getType(), isLocalToUnit(),
1549                         isDefinition(), getVariable(),
1550                         getStaticDataMemberDeclaration());
1551   }
1552
1553 public:
1554   DEFINE_MDNODE_GET(MDGlobalVariable,
1555                     (MDScope * Scope, StringRef Name, StringRef LinkageName,
1556                      MDFile *File, unsigned Line, Metadata *Type,
1557                      bool IsLocalToUnit, bool IsDefinition,
1558                      ConstantAsMetadata *Variable,
1559                      MDDerivedType *StaticDataMemberDeclaration),
1560                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1561                      IsDefinition, Variable, StaticDataMemberDeclaration))
1562   DEFINE_MDNODE_GET(MDGlobalVariable,
1563                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1564                      Metadata *File, unsigned Line, Metadata *Type,
1565                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1566                      Metadata *StaticDataMemberDeclaration),
1567                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1568                      IsDefinition, Variable, StaticDataMemberDeclaration))
1569
1570   TempMDGlobalVariable clone() const { return cloneImpl(); }
1571
1572   bool isLocalToUnit() const { return IsLocalToUnit; }
1573   bool isDefinition() const { return IsDefinition; }
1574   StringRef getDisplayName() const { return getStringOperand(4); }
1575   StringRef getLinkageName() const { return getStringOperand(5); }
1576   ConstantAsMetadata *getVariable() const {
1577     return cast_or_null<ConstantAsMetadata>(getRawVariable());
1578   }
1579   MDDerivedType *getStaticDataMemberDeclaration() const {
1580     return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1581   }
1582
1583   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1584   Metadata *getRawVariable() const { return getOperand(6); }
1585   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1586
1587   static bool classof(const Metadata *MD) {
1588     return MD->getMetadataID() == MDGlobalVariableKind;
1589   }
1590 };
1591
1592 /// \brief Local variable.
1593 ///
1594 /// TODO: Split between arguments and otherwise.
1595 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1596 /// TODO: Split up flags.
1597 class MDLocalVariable : public MDVariable {
1598   friend class LLVMContextImpl;
1599   friend class MDNode;
1600
1601   unsigned Arg;
1602   unsigned Flags;
1603
1604   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1605                   unsigned Line, unsigned Arg, unsigned Flags,
1606                   ArrayRef<Metadata *> Ops)
1607       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1608         Flags(Flags) {}
1609   ~MDLocalVariable() {}
1610
1611   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1612                                   MDScope *Scope, StringRef Name, MDFile *File,
1613                                   unsigned Line, Metadata *Type, unsigned Arg,
1614                                   unsigned Flags, MDLocation *InlinedAt,
1615                                   StorageType Storage,
1616                                   bool ShouldCreate = true) {
1617     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1618                    File, Line, Type, Arg, Flags, InlinedAt, Storage,
1619                    ShouldCreate);
1620   }
1621   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1622                                   Metadata *Scope, MDString *Name,
1623                                   Metadata *File, unsigned Line, Metadata *Type,
1624                                   unsigned Arg, unsigned Flags,
1625                                   Metadata *InlinedAt, StorageType Storage,
1626                                   bool ShouldCreate = true);
1627
1628   TempMDLocalVariable cloneImpl() const {
1629     return getTemporary(getContext(), getTag(), getScope(), getName(),
1630                         getFile(), getLine(), getType(), getArg(), getFlags(),
1631                         getInlinedAt());
1632   }
1633
1634 public:
1635   DEFINE_MDNODE_GET(MDLocalVariable,
1636                     (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1637                      MDFile *File, unsigned Line, Metadata *Type, unsigned Arg,
1638                      unsigned Flags, MDLocation *InlinedAt = nullptr),
1639                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1640   DEFINE_MDNODE_GET(MDLocalVariable,
1641                     (unsigned Tag, Metadata *Scope, MDString *Name,
1642                      Metadata *File, unsigned Line, Metadata *Type,
1643                      unsigned Arg, unsigned Flags,
1644                      Metadata *InlinedAt = nullptr),
1645                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1646
1647   TempMDLocalVariable clone() const { return cloneImpl(); }
1648
1649   /// \brief Get the local scope for this variable.
1650   ///
1651   /// Variables must be defined in a local scope.
1652   MDLocalScope *getScope() const {
1653     return cast<MDLocalScope>(MDVariable::getScope());
1654   }
1655
1656   unsigned getArg() const { return Arg; }
1657   unsigned getFlags() const { return Flags; }
1658   MDLocation *getInlinedAt() const {
1659     return cast_or_null<MDLocation>(getRawInlinedAt());
1660   }
1661
1662   Metadata *getRawInlinedAt() const { return getOperand(4); }
1663
1664   /// \brief Check that a location is valid for this variable.
1665   ///
1666   /// Check that \c DL has the same inlined-at location as this variable,
1667   /// making them valid for the same \a DbgInfoIntrinsic.
1668   bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1669     return getInlinedAt() == (DL ? DL->getInlinedAt() : nullptr);
1670   }
1671
1672   /// \brief Get an inlined version of this variable.
1673   ///
1674   /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1675   MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1676     if (InlinedAt == getInlinedAt())
1677       return const_cast<MDLocalVariable *>(this);
1678     auto Temp = clone();
1679     Temp->replaceOperandWith(4, InlinedAt);
1680     return replaceWithUniqued(std::move(Temp));
1681   }
1682   MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1683
1684   static bool classof(const Metadata *MD) {
1685     return MD->getMetadataID() == MDLocalVariableKind;
1686   }
1687 };
1688
1689 /// \brief DWARF expression.
1690 ///
1691 /// TODO: Co-allocate the expression elements.
1692 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1693 /// storage types.
1694 class MDExpression : public MDNode {
1695   friend class LLVMContextImpl;
1696   friend class MDNode;
1697
1698   std::vector<uint64_t> Elements;
1699
1700   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1701       : MDNode(C, MDExpressionKind, Storage, None),
1702         Elements(Elements.begin(), Elements.end()) {}
1703   ~MDExpression() {}
1704
1705   static MDExpression *getImpl(LLVMContext &Context,
1706                                ArrayRef<uint64_t> Elements, StorageType Storage,
1707                                bool ShouldCreate = true);
1708
1709   TempMDExpression cloneImpl() const {
1710     return getTemporary(getContext(), getElements());
1711   }
1712
1713 public:
1714   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1715
1716   TempMDExpression clone() const { return cloneImpl(); }
1717
1718   ArrayRef<uint64_t> getElements() const { return Elements; }
1719
1720   unsigned getNumElements() const { return Elements.size(); }
1721   uint64_t getElement(unsigned I) const {
1722     assert(I < Elements.size() && "Index out of range");
1723     return Elements[I];
1724   }
1725
1726   typedef ArrayRef<uint64_t>::iterator element_iterator;
1727   element_iterator elements_begin() const { return getElements().begin(); }
1728   element_iterator elements_end() const { return getElements().end(); }
1729
1730   /// \brief A lightweight wrapper around an expression operand.
1731   ///
1732   /// TODO: Store arguments directly and change \a MDExpression to store a
1733   /// range of these.
1734   class ExprOperand {
1735     const uint64_t *Op;
1736
1737   public:
1738     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1739
1740     const uint64_t *get() const { return Op; }
1741
1742     /// \brief Get the operand code.
1743     uint64_t getOp() const { return *Op; }
1744
1745     /// \brief Get an argument to the operand.
1746     ///
1747     /// Never returns the operand itself.
1748     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1749
1750     unsigned getNumArgs() const { return getSize() - 1; }
1751
1752     /// \brief Return the size of the operand.
1753     ///
1754     /// Return the number of elements in the operand (1 + args).
1755     unsigned getSize() const;
1756   };
1757
1758   /// \brief An iterator for expression operands.
1759   class expr_op_iterator
1760       : public std::iterator<std::input_iterator_tag, ExprOperand> {
1761     ExprOperand Op;
1762
1763   public:
1764     explicit expr_op_iterator(element_iterator I) : Op(I) {}
1765
1766     element_iterator getBase() const { return Op.get(); }
1767     const ExprOperand &operator*() const { return Op; }
1768     const ExprOperand *operator->() const { return &Op; }
1769
1770     expr_op_iterator &operator++() {
1771       increment();
1772       return *this;
1773     }
1774     expr_op_iterator operator++(int) {
1775       expr_op_iterator T(*this);
1776       increment();
1777       return T;
1778     }
1779
1780     bool operator==(const expr_op_iterator &X) const {
1781       return getBase() == X.getBase();
1782     }
1783     bool operator!=(const expr_op_iterator &X) const {
1784       return getBase() != X.getBase();
1785     }
1786
1787   private:
1788     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
1789   };
1790
1791   /// \brief Visit the elements via ExprOperand wrappers.
1792   ///
1793   /// These range iterators visit elements through \a ExprOperand wrappers.
1794   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
1795   /// true.
1796   ///
1797   /// \pre \a isValid() gives \c true.
1798   /// @{
1799   expr_op_iterator expr_op_begin() const {
1800     return expr_op_iterator(elements_begin());
1801   }
1802   expr_op_iterator expr_op_end() const {
1803     return expr_op_iterator(elements_end());
1804   }
1805   /// @}
1806
1807   bool isValid() const;
1808
1809   static bool classof(const Metadata *MD) {
1810     return MD->getMetadataID() == MDExpressionKind;
1811   }
1812 };
1813
1814 class MDObjCProperty : public DebugNode {
1815   friend class LLVMContextImpl;
1816   friend class MDNode;
1817
1818   unsigned Line;
1819   unsigned Attributes;
1820
1821   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
1822                  unsigned Attributes, ArrayRef<Metadata *> Ops)
1823       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
1824                   Ops),
1825         Line(Line), Attributes(Attributes) {}
1826   ~MDObjCProperty() {}
1827
1828   static MDObjCProperty *
1829   getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
1830           StringRef GetterName, StringRef SetterName, unsigned Attributes,
1831           MDType *Type, StorageType Storage, bool ShouldCreate = true) {
1832     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
1833                    getCanonicalMDString(Context, GetterName),
1834                    getCanonicalMDString(Context, SetterName), Attributes, Type,
1835                    Storage, ShouldCreate);
1836   }
1837   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
1838                                  Metadata *File, unsigned Line,
1839                                  MDString *GetterName, MDString *SetterName,
1840                                  unsigned Attributes, Metadata *Type,
1841                                  StorageType Storage, bool ShouldCreate = true);
1842
1843   TempMDObjCProperty cloneImpl() const {
1844     return getTemporary(getContext(), getName(), getFile(), getLine(),
1845                         getGetterName(), getSetterName(), getAttributes(),
1846                         getType());
1847   }
1848
1849 public:
1850   DEFINE_MDNODE_GET(MDObjCProperty,
1851                     (StringRef Name, MDFile *File, unsigned Line,
1852                      StringRef GetterName, StringRef SetterName,
1853                      unsigned Attributes, MDType *Type),
1854                     (Name, File, Line, GetterName, SetterName, Attributes,
1855                      Type))
1856   DEFINE_MDNODE_GET(MDObjCProperty,
1857                     (MDString * Name, Metadata *File, unsigned Line,
1858                      MDString *GetterName, MDString *SetterName,
1859                      unsigned Attributes, Metadata *Type),
1860                     (Name, File, Line, GetterName, SetterName, Attributes,
1861                      Type))
1862
1863   TempMDObjCProperty clone() const { return cloneImpl(); }
1864
1865   unsigned getLine() const { return Line; }
1866   unsigned getAttributes() const { return Attributes; }
1867   StringRef getName() const { return getStringOperand(0); }
1868   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1869   StringRef getGetterName() const { return getStringOperand(2); }
1870   StringRef getSetterName() const { return getStringOperand(3); }
1871   MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
1872
1873   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1874   Metadata *getRawFile() const { return getOperand(1); }
1875   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
1876   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
1877   Metadata *getRawType() const { return getOperand(4); }
1878
1879   static bool classof(const Metadata *MD) {
1880     return MD->getMetadataID() == MDObjCPropertyKind;
1881   }
1882 };
1883
1884 class MDImportedEntity : public DebugNode {
1885   friend class LLVMContextImpl;
1886   friend class MDNode;
1887
1888   unsigned Line;
1889
1890   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
1891                    unsigned Line, ArrayRef<Metadata *> Ops)
1892       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
1893   ~MDImportedEntity() {}
1894
1895   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1896                                    MDScope *Scope, Metadata *Entity,
1897                                    unsigned Line, StringRef Name,
1898                                    StorageType Storage,
1899                                    bool ShouldCreate = true) {
1900     return getImpl(Context, Tag, Scope, Entity, Line,
1901                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
1902   }
1903   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1904                                    Metadata *Scope, Metadata *Entity,
1905                                    unsigned Line, MDString *Name,
1906                                    StorageType Storage,
1907                                    bool ShouldCreate = true);
1908
1909   TempMDImportedEntity cloneImpl() const {
1910     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
1911                         getLine(), getName());
1912   }
1913
1914 public:
1915   DEFINE_MDNODE_GET(MDImportedEntity,
1916                     (unsigned Tag, MDScope *Scope, Metadata *Entity,
1917                      unsigned Line, StringRef Name = ""),
1918                     (Tag, Scope, Entity, Line, Name))
1919   DEFINE_MDNODE_GET(MDImportedEntity,
1920                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1921                      unsigned Line, MDString *Name),
1922                     (Tag, Scope, Entity, Line, Name))
1923
1924   TempMDImportedEntity clone() const { return cloneImpl(); }
1925
1926   unsigned getLine() const { return Line; }
1927   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1928   Metadata *getEntity() const { return getRawEntity(); }
1929   StringRef getName() const { return getStringOperand(2); }
1930
1931   Metadata *getRawScope() const { return getOperand(0); }
1932   Metadata *getRawEntity() const { return getOperand(1); }
1933   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1934
1935   static bool classof(const Metadata *MD) {
1936     return MD->getMetadataID() == MDImportedEntityKind;
1937   }
1938 };
1939
1940 } // end namespace llvm
1941
1942 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
1943 #undef DEFINE_MDNODE_GET_UNPACK
1944 #undef DEFINE_MDNODE_GET
1945
1946 #endif