4149bd424386e9cad427f7f87c31548e1ae915bb
[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   MDScopeRef getScope() const { return MDScopeRef(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   MDTypeRef getBaseType() const { return MDTypeRef(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                                 MDScopeRef Scope, MDTypeRef 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, unsigned Line,
630                      MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
631                      uint64_t AlignInBits, uint64_t OffsetInBits,
632                      unsigned Flags, Metadata *ExtraData = nullptr),
633                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
634                      AlignInBits, OffsetInBits, Flags, ExtraData))
635
636   TempMDDerivedType clone() const { return cloneImpl(); }
637
638   /// \brief Get extra data associated with this derived type.
639   ///
640   /// Class type for pointer-to-members, objective-c property node for ivars,
641   /// or global constant wrapper for static members.
642   ///
643   /// TODO: Separate out types that need this extra operand: pointer-to-member
644   /// types and member fields (static members and ivars).
645   Metadata *getExtraData() const { return getRawExtraData(); }
646   Metadata *getRawExtraData() const { return getOperand(4); }
647
648   static bool classof(const Metadata *MD) {
649     return MD->getMetadataID() == MDDerivedTypeKind;
650   }
651 };
652
653 /// \brief Base class for MDCompositeType and MDSubroutineType.
654 ///
655 /// TODO: Delete; they're not really related.
656 class MDCompositeTypeBase : public MDDerivedTypeBase {
657   unsigned RuntimeLang;
658
659 protected:
660   MDCompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
661                       unsigned Tag, unsigned Line, unsigned RuntimeLang,
662                       uint64_t SizeInBits, uint64_t AlignInBits,
663                       uint64_t OffsetInBits, unsigned Flags,
664                       ArrayRef<Metadata *> Ops)
665       : MDDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
666                           OffsetInBits, Flags, Ops),
667         RuntimeLang(RuntimeLang) {}
668   ~MDCompositeTypeBase() {}
669
670 public:
671   MDTuple *getElements() const {
672     return cast_or_null<MDTuple>(getRawElements());
673   }
674   MDTypeRef getVTableHolder() const { return MDTypeRef(getRawVTableHolder()); }
675   MDTemplateParameterArray getTemplateParams() const {
676     return cast_or_null<MDTuple>(getRawTemplateParams());
677   }
678   StringRef getIdentifier() const { return getStringOperand(7); }
679   unsigned getRuntimeLang() const { return RuntimeLang; }
680
681   Metadata *getRawElements() const { return getOperand(4); }
682   Metadata *getRawVTableHolder() const { return getOperand(5); }
683   Metadata *getRawTemplateParams() const { return getOperand(6); }
684   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
685
686   /// \brief Replace operands.
687   ///
688   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
689   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
690   /// of its movement if necessary.
691   /// @{
692   void replaceElements(MDTuple *Elements) {
693 #ifndef NDEBUG
694     if (auto *Old = cast_or_null<MDTuple>(getElements()))
695       for (const auto &Op : Old->operands())
696         assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
697                "Lost a member during member list replacement");
698 #endif
699     replaceOperandWith(4, Elements);
700   }
701   void replaceVTableHolder(MDTypeRef VTableHolder) {
702     replaceOperandWith(5, VTableHolder);
703   }
704   void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
705     replaceOperandWith(6, TemplateParams);
706   }
707   /// @}
708
709   static bool classof(const Metadata *MD) {
710     return MD->getMetadataID() == MDCompositeTypeKind ||
711            MD->getMetadataID() == MDSubroutineTypeKind;
712   }
713 };
714
715 /// \brief Composite types.
716 ///
717 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
718 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
719 class MDCompositeType : public MDCompositeTypeBase {
720   friend class LLVMContextImpl;
721   friend class MDNode;
722
723   MDCompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
724                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
725                   uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
726                   ArrayRef<Metadata *> Ops)
727       : MDCompositeTypeBase(C, MDCompositeTypeKind, Storage, Tag, Line,
728                             RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
729                             Flags, Ops) {}
730   ~MDCompositeType() {}
731
732   static MDCompositeType *
733   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
734           unsigned Line, MDScopeRef Scope, MDTypeRef BaseType,
735           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
736           uint64_t Flags, MDTuple *Elements, unsigned RuntimeLang,
737           MDTypeRef VTableHolder, MDTemplateParameterArray TemplateParams,
738           StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
739     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
740                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
741                    Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
742                    getCanonicalMDString(Context, Identifier), Storage,
743                    ShouldCreate);
744   }
745   static MDCompositeType *
746   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
747           unsigned Line, Metadata *Scope, Metadata *BaseType,
748           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
749           unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
750           Metadata *VTableHolder, Metadata *TemplateParams,
751           MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
752
753   TempMDCompositeType cloneImpl() const {
754     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
755                         getScope(), getBaseType(), getSizeInBits(),
756                         getAlignInBits(), getOffsetInBits(), getFlags(),
757                         getElements(), getRuntimeLang(), getVTableHolder(),
758                         getTemplateParams(), getIdentifier());
759   }
760
761 public:
762   DEFINE_MDNODE_GET(MDCompositeType,
763                     (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
764                      MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
765                      uint64_t AlignInBits, uint64_t OffsetInBits,
766                      unsigned Flags, MDTuple *Elements, unsigned RuntimeLang,
767                      MDTypeRef VTableHolder,
768                      MDTemplateParameterArray TemplateParams = nullptr,
769                      StringRef Identifier = ""),
770                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
771                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
772                      VTableHolder, TemplateParams, Identifier))
773   DEFINE_MDNODE_GET(MDCompositeType,
774                     (unsigned Tag, MDString *Name, Metadata *File,
775                      unsigned Line, Metadata *Scope, Metadata *BaseType,
776                      uint64_t SizeInBits, uint64_t AlignInBits,
777                      uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
778                      unsigned RuntimeLang, Metadata *VTableHolder,
779                      Metadata *TemplateParams = nullptr,
780                      MDString *Identifier = nullptr),
781                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
782                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
783                      VTableHolder, TemplateParams, Identifier))
784
785   TempMDCompositeType clone() const { return cloneImpl(); }
786
787   static bool classof(const Metadata *MD) {
788     return MD->getMetadataID() == MDCompositeTypeKind;
789   }
790 };
791
792 template <class T> TypedDebugNodeRef<T> TypedDebugNodeRef<T>::get(const T *N) {
793   if (N)
794     if (auto *Composite = dyn_cast<MDCompositeType>(N))
795       if (auto *S = Composite->getRawIdentifier())
796         return TypedDebugNodeRef<T>(S);
797   return TypedDebugNodeRef<T>(N);
798 }
799
800 /// \brief Type array for a subprogram.
801 ///
802 /// TODO: Detach from CompositeType, and fold the array of types in directly
803 /// as operands.
804 class MDSubroutineType : public MDCompositeTypeBase {
805   friend class LLVMContextImpl;
806   friend class MDNode;
807
808   MDSubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
809                    ArrayRef<Metadata *> Ops)
810       : MDCompositeTypeBase(C, MDSubroutineTypeKind, Storage,
811                             dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
812                             Ops) {}
813   ~MDSubroutineType() {}
814
815   static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
816                                    Metadata *TypeArray, StorageType Storage,
817                                    bool ShouldCreate = true);
818
819   TempMDSubroutineType cloneImpl() const {
820     return getTemporary(getContext(), getFlags(), getTypeArray());
821   }
822
823 public:
824   DEFINE_MDNODE_GET(MDSubroutineType, (unsigned Flags, Metadata *TypeArray),
825                     (Flags, TypeArray))
826
827   TempMDSubroutineType clone() const { return cloneImpl(); }
828
829   MDTuple *getTypeArray() const { return getElements(); }
830   Metadata *getRawTypeArray() const { return getRawElements(); }
831
832   static bool classof(const Metadata *MD) {
833     return MD->getMetadataID() == MDSubroutineTypeKind;
834   }
835 };
836
837 /// \brief Compile unit.
838 class MDCompileUnit : public MDScope {
839   friend class LLVMContextImpl;
840   friend class MDNode;
841
842   unsigned SourceLanguage;
843   bool IsOptimized;
844   unsigned RuntimeVersion;
845   unsigned EmissionKind;
846
847   MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
848                 bool IsOptimized, unsigned RuntimeVersion,
849                 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
850       : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
851         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
852         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
853   ~MDCompileUnit() {}
854
855   static MDCompileUnit *
856   getImpl(LLVMContext &Context, unsigned SourceLanguage, MDFile *File,
857           StringRef Producer, bool IsOptimized, StringRef Flags,
858           unsigned RuntimeVersion, StringRef SplitDebugFilename,
859           unsigned EmissionKind, MDTuple *EnumTypes, MDTuple *RetainedTypes,
860           MDTuple *Subprograms, MDTuple *GlobalVariables,
861           MDTuple *ImportedEntities, StorageType Storage,
862           bool ShouldCreate = true) {
863     return getImpl(Context, SourceLanguage, File,
864                    getCanonicalMDString(Context, Producer), IsOptimized,
865                    getCanonicalMDString(Context, Flags), RuntimeVersion,
866                    getCanonicalMDString(Context, SplitDebugFilename),
867                    EmissionKind, EnumTypes, RetainedTypes, Subprograms,
868                    GlobalVariables, ImportedEntities, Storage, ShouldCreate);
869   }
870   static MDCompileUnit *
871   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
872           MDString *Producer, bool IsOptimized, MDString *Flags,
873           unsigned RuntimeVersion, MDString *SplitDebugFilename,
874           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
875           Metadata *Subprograms, Metadata *GlobalVariables,
876           Metadata *ImportedEntities, StorageType Storage,
877           bool ShouldCreate = true);
878
879   TempMDCompileUnit cloneImpl() const {
880     return getTemporary(
881         getContext(), getSourceLanguage(), getFile(), getProducer(),
882         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
883         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
884         getGlobalVariables(), getImportedEntities());
885   }
886
887 public:
888   DEFINE_MDNODE_GET(MDCompileUnit,
889                     (unsigned SourceLanguage, MDFile *File, StringRef Producer,
890                      bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
891                      StringRef SplitDebugFilename, unsigned EmissionKind,
892                      MDTuple *EnumTypes, MDTuple *RetainedTypes,
893                      MDTuple *Subprograms, MDTuple *GlobalVariables,
894                      MDTuple *ImportedEntities),
895                     (SourceLanguage, File, Producer, IsOptimized, Flags,
896                      RuntimeVersion, SplitDebugFilename, EmissionKind,
897                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
898                      ImportedEntities))
899   DEFINE_MDNODE_GET(MDCompileUnit,
900                     (unsigned SourceLanguage, Metadata *File,
901                      MDString *Producer, bool IsOptimized, MDString *Flags,
902                      unsigned RuntimeVersion, MDString *SplitDebugFilename,
903                      unsigned EmissionKind, Metadata *EnumTypes,
904                      Metadata *RetainedTypes, Metadata *Subprograms,
905                      Metadata *GlobalVariables, Metadata *ImportedEntities),
906                     (SourceLanguage, File, Producer, IsOptimized, Flags,
907                      RuntimeVersion, SplitDebugFilename, EmissionKind,
908                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
909                      ImportedEntities))
910
911   TempMDCompileUnit clone() const { return cloneImpl(); }
912
913   unsigned getSourceLanguage() const { return SourceLanguage; }
914   bool isOptimized() const { return IsOptimized; }
915   unsigned getRuntimeVersion() const { return RuntimeVersion; }
916   unsigned getEmissionKind() const { return EmissionKind; }
917   StringRef getProducer() const { return getStringOperand(1); }
918   StringRef getFlags() const { return getStringOperand(2); }
919   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
920   MDCompositeTypeArray getEnumTypes() const {
921     return cast_or_null<MDTuple>(getRawEnumTypes());
922   }
923   MDTypeArray getRetainedTypes() const {
924     return cast_or_null<MDTuple>(getRawRetainedTypes());
925   }
926   MDSubprogramArray getSubprograms() const {
927     return cast_or_null<MDTuple>(getRawSubprograms());
928   }
929   MDGlobalVariableArray getGlobalVariables() const {
930     return cast_or_null<MDTuple>(getRawGlobalVariables());
931   }
932   MDImportedEntityArray getImportedEntities() const {
933     return cast_or_null<MDTuple>(getRawImportedEntities());
934   }
935
936   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
937   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
938   MDString *getRawSplitDebugFilename() const {
939     return getOperandAs<MDString>(3);
940   }
941   Metadata *getRawEnumTypes() const { return getOperand(4); }
942   Metadata *getRawRetainedTypes() const { return getOperand(5); }
943   Metadata *getRawSubprograms() const { return getOperand(6); }
944   Metadata *getRawGlobalVariables() const { return getOperand(7); }
945   Metadata *getRawImportedEntities() const { return getOperand(8); }
946
947   /// \brief Replace arrays.
948   ///
949   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
950   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
951   /// MDCompileUnit should be fairly rare.
952   /// @{
953   void replaceSubprograms(MDTuple *N) { replaceOperandWith(6, N); }
954   void replaceGlobalVariables(MDTuple *N) { replaceOperandWith(7, N); }
955   /// @}
956
957   static bool classof(const Metadata *MD) {
958     return MD->getMetadataID() == MDCompileUnitKind;
959   }
960 };
961
962 /// \brief A scope for locals.
963 ///
964 /// A legal scope for lexical blocks, local variables, and debug info
965 /// locations.  Subclasses are \a MDSubprogram, \a MDLexicalBlock, and \a
966 /// MDLexicalBlockFile.
967 class MDLocalScope : public MDScope {
968 protected:
969   MDLocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
970                ArrayRef<Metadata *> Ops)
971       : MDScope(C, ID, Storage, Tag, Ops) {}
972   ~MDLocalScope() {}
973
974 public:
975   /// \brief Get the subprogram for this scope.
976   ///
977   /// Return this if it's an \a MDSubprogram; otherwise, look up the scope
978   /// chain.
979   MDSubprogram *getSubprogram() const;
980
981   static bool classof(const Metadata *MD) {
982     return MD->getMetadataID() == MDSubprogramKind ||
983            MD->getMetadataID() == MDLexicalBlockKind ||
984            MD->getMetadataID() == MDLexicalBlockFileKind;
985   }
986 };
987
988 /// \brief Debug location.
989 ///
990 /// A debug location in source code, used for debug info and otherwise.
991 class MDLocation : public MDNode {
992   friend class LLVMContextImpl;
993   friend class MDNode;
994
995   MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
996              unsigned Column, ArrayRef<Metadata *> MDs);
997   ~MDLocation() { dropAllReferences(); }
998
999   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1000                              unsigned Column, Metadata *Scope,
1001                              Metadata *InlinedAt, StorageType Storage,
1002                              bool ShouldCreate = true);
1003   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1004                              unsigned Column, MDLocalScope *Scope,
1005                              MDLocation *InlinedAt, StorageType Storage,
1006                              bool ShouldCreate = true) {
1007     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1008                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1009   }
1010
1011   TempMDLocation cloneImpl() const {
1012     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1013                         getInlinedAt());
1014   }
1015
1016   // Disallow replacing operands.
1017   void replaceOperandWith(unsigned I, Metadata *New) = delete;
1018
1019 public:
1020   DEFINE_MDNODE_GET(MDLocation,
1021                     (unsigned Line, unsigned Column, Metadata *Scope,
1022                      Metadata *InlinedAt = nullptr),
1023                     (Line, Column, Scope, InlinedAt))
1024   DEFINE_MDNODE_GET(MDLocation,
1025                     (unsigned Line, unsigned Column, MDLocalScope *Scope,
1026                      MDLocation *InlinedAt = nullptr),
1027                     (Line, Column, Scope, InlinedAt))
1028
1029   /// \brief Return a (temporary) clone of this.
1030   TempMDLocation clone() const { return cloneImpl(); }
1031
1032   unsigned getLine() const { return SubclassData32; }
1033   unsigned getColumn() const { return SubclassData16; }
1034   MDLocalScope *getScope() const {
1035     return cast<MDLocalScope>(getRawScope());
1036   }
1037   MDLocation *getInlinedAt() const {
1038     return cast_or_null<MDLocation>(getRawInlinedAt());
1039   }
1040
1041   /// \brief Get the scope where this is inlined.
1042   ///
1043   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1044   /// location.
1045   MDLocalScope *getInlinedAtScope() const {
1046     if (auto *IA = getInlinedAt())
1047       return IA->getInlinedAtScope();
1048     return getScope();
1049   }
1050
1051   Metadata *getRawScope() const { return getOperand(0); }
1052   Metadata *getRawInlinedAt() const {
1053     if (getNumOperands() == 2)
1054       return getOperand(1);
1055     return nullptr;
1056   }
1057
1058   static bool classof(const Metadata *MD) {
1059     return MD->getMetadataID() == MDLocationKind;
1060   }
1061 };
1062
1063 /// \brief Subprogram description.
1064 ///
1065 /// TODO: Remove DisplayName.  It's always equal to Name.
1066 /// TODO: Split up flags.
1067 class MDSubprogram : public MDLocalScope {
1068   friend class LLVMContextImpl;
1069   friend class MDNode;
1070
1071   unsigned Line;
1072   unsigned ScopeLine;
1073   unsigned Virtuality;
1074   unsigned VirtualIndex;
1075   unsigned Flags;
1076   bool IsLocalToUnit;
1077   bool IsDefinition;
1078   bool IsOptimized;
1079
1080   MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1081                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1082                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1083                bool IsOptimized, ArrayRef<Metadata *> Ops)
1084       : MDLocalScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1085                      Ops),
1086         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1087         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1088         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1089   ~MDSubprogram() {}
1090
1091   static MDSubprogram *
1092   getImpl(LLVMContext &Context, MDScopeRef Scope, StringRef Name,
1093           StringRef LinkageName, MDFile *File, unsigned Line,
1094           MDSubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1095           unsigned ScopeLine, MDTypeRef ContainingType, unsigned Virtuality,
1096           unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1097           ConstantAsMetadata *Function, MDTemplateParameterArray TemplateParams,
1098           MDSubprogram *Declaration, MDLocalVariableArray Variables,
1099           StorageType Storage, bool ShouldCreate = true) {
1100     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1101                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1102                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1103                    Virtuality, VirtualIndex, Flags, IsOptimized, Function,
1104                    TemplateParams, Declaration, Variables, Storage,
1105                    ShouldCreate);
1106   }
1107   static MDSubprogram *
1108   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1109           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1110           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1111           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1112           unsigned Flags, bool IsOptimized, Metadata *Function,
1113           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1114           StorageType Storage, bool ShouldCreate = true);
1115
1116   TempMDSubprogram cloneImpl() const {
1117     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1118                         getFile(), getLine(), getType(), isLocalToUnit(),
1119                         isDefinition(), getScopeLine(), getContainingType(),
1120                         getVirtuality(), getVirtualIndex(), getFlags(),
1121                         isOptimized(), getFunction(), getTemplateParams(),
1122                         getDeclaration(), getVariables());
1123   }
1124
1125 public:
1126   DEFINE_MDNODE_GET(MDSubprogram,
1127                     (MDScopeRef Scope, StringRef Name, StringRef LinkageName,
1128                      MDFile *File, unsigned Line, MDSubroutineType *Type,
1129                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1130                      MDTypeRef ContainingType, unsigned Virtuality,
1131                      unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1132                      ConstantAsMetadata *Function = nullptr,
1133                      MDTemplateParameterArray TemplateParams = nullptr,
1134                      MDSubprogram *Declaration = nullptr,
1135                      MDLocalVariableArray Variables = nullptr),
1136                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1137                      IsDefinition, ScopeLine, ContainingType, Virtuality,
1138                      VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1139                      Declaration, Variables))
1140   DEFINE_MDNODE_GET(
1141       MDSubprogram,
1142       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1143        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1144        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1145        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1146        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1147        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1148       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1149        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1150        Function, TemplateParams, Declaration, Variables))
1151
1152   TempMDSubprogram clone() const { return cloneImpl(); }
1153
1154 public:
1155   unsigned getLine() const { return Line; }
1156   unsigned getVirtuality() const { return Virtuality; }
1157   unsigned getVirtualIndex() const { return VirtualIndex; }
1158   unsigned getScopeLine() const { return ScopeLine; }
1159   unsigned getFlags() const { return Flags; }
1160   bool isLocalToUnit() const { return IsLocalToUnit; }
1161   bool isDefinition() const { return IsDefinition; }
1162   bool isOptimized() const { return IsOptimized; }
1163
1164   MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
1165
1166   StringRef getName() const { return getStringOperand(2); }
1167   StringRef getDisplayName() const { return getStringOperand(3); }
1168   StringRef getLinkageName() const { return getStringOperand(4); }
1169
1170   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1171   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1172
1173   MDSubroutineType *getType() const {
1174     return cast_or_null<MDSubroutineType>(getRawType());
1175   }
1176   MDTypeRef getContainingType() const {
1177     return MDTypeRef(getRawContainingType());
1178   }
1179
1180   ConstantAsMetadata *getFunction() const {
1181     return cast_or_null<ConstantAsMetadata>(getRawFunction());
1182   }
1183   MDTemplateParameterArray getTemplateParams() const {
1184     return cast_or_null<MDTuple>(getRawTemplateParams());
1185   }
1186   MDSubprogram *getDeclaration() const {
1187     return cast_or_null<MDSubprogram>(getRawDeclaration());
1188   }
1189   MDLocalVariableArray getVariables() const {
1190     return cast_or_null<MDTuple>(getRawVariables());
1191   }
1192
1193   Metadata *getRawScope() const { return getOperand(1); }
1194   Metadata *getRawType() const { return getOperand(5); }
1195   Metadata *getRawContainingType() const { return getOperand(6); }
1196   Metadata *getRawFunction() const { return getOperand(7); }
1197   Metadata *getRawTemplateParams() const { return getOperand(8); }
1198   Metadata *getRawDeclaration() const { return getOperand(9); }
1199   Metadata *getRawVariables() const { return getOperand(10); }
1200
1201   /// \brief Replace the function.
1202   ///
1203   /// If \a isUniqued() and not \a isResolved(), this could node will be
1204   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1205   /// that's a problem.
1206   /// @{
1207   void replaceFunction(Function *F);
1208   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1209   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1210   /// @}
1211
1212   static bool classof(const Metadata *MD) {
1213     return MD->getMetadataID() == MDSubprogramKind;
1214   }
1215 };
1216
1217 class MDLexicalBlockBase : public MDLocalScope {
1218 protected:
1219   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1220                      ArrayRef<Metadata *> Ops)
1221       : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1222   ~MDLexicalBlockBase() {}
1223
1224 public:
1225   MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
1226
1227   Metadata *getRawScope() const { return getOperand(1); }
1228
1229   static bool classof(const Metadata *MD) {
1230     return MD->getMetadataID() == MDLexicalBlockKind ||
1231            MD->getMetadataID() == MDLexicalBlockFileKind;
1232   }
1233 };
1234
1235 class MDLexicalBlock : public MDLexicalBlockBase {
1236   friend class LLVMContextImpl;
1237   friend class MDNode;
1238
1239   unsigned Line;
1240   unsigned Column;
1241
1242   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1243                  unsigned Column, ArrayRef<Metadata *> Ops)
1244       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1245         Column(Column) {}
1246   ~MDLexicalBlock() {}
1247
1248   static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1249                                  MDFile *File, unsigned Line, unsigned Column,
1250                                  StorageType Storage,
1251                                  bool ShouldCreate = true) {
1252     return getImpl(Context, static_cast<Metadata *>(Scope),
1253                    static_cast<Metadata *>(File), Line, Column, Storage,
1254                    ShouldCreate);
1255   }
1256
1257   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1258                                  Metadata *File, unsigned Line, unsigned Column,
1259                                  StorageType Storage, bool ShouldCreate = true);
1260
1261   TempMDLexicalBlock cloneImpl() const {
1262     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1263                         getColumn());
1264   }
1265
1266 public:
1267   DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1268                                      unsigned Line, unsigned Column),
1269                     (Scope, File, Line, Column))
1270   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1271                                      unsigned Line, unsigned Column),
1272                     (Scope, File, Line, Column))
1273
1274   TempMDLexicalBlock clone() const { return cloneImpl(); }
1275
1276   unsigned getLine() const { return Line; }
1277   unsigned getColumn() const { return Column; }
1278
1279   static bool classof(const Metadata *MD) {
1280     return MD->getMetadataID() == MDLexicalBlockKind;
1281   }
1282 };
1283
1284 class MDLexicalBlockFile : public MDLexicalBlockBase {
1285   friend class LLVMContextImpl;
1286   friend class MDNode;
1287
1288   unsigned Discriminator;
1289
1290   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1291                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1292       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1293         Discriminator(Discriminator) {}
1294   ~MDLexicalBlockFile() {}
1295
1296   static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1297                                      MDFile *File, unsigned Discriminator,
1298                                      StorageType Storage,
1299                                      bool ShouldCreate = true) {
1300     return getImpl(Context, static_cast<Metadata *>(Scope),
1301                    static_cast<Metadata *>(File), Discriminator, Storage,
1302                    ShouldCreate);
1303   }
1304
1305   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1306                                      Metadata *File, unsigned Discriminator,
1307                                      StorageType Storage,
1308                                      bool ShouldCreate = true);
1309
1310   TempMDLexicalBlockFile cloneImpl() const {
1311     return getTemporary(getContext(), getScope(), getFile(),
1312                         getDiscriminator());
1313   }
1314
1315 public:
1316   DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1317                                          unsigned Discriminator),
1318                     (Scope, File, Discriminator))
1319   DEFINE_MDNODE_GET(MDLexicalBlockFile,
1320                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1321                     (Scope, File, Discriminator))
1322
1323   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1324
1325   unsigned getDiscriminator() const { return Discriminator; }
1326
1327   static bool classof(const Metadata *MD) {
1328     return MD->getMetadataID() == MDLexicalBlockFileKind;
1329   }
1330 };
1331
1332 class MDNamespace : public MDScope {
1333   friend class LLVMContextImpl;
1334   friend class MDNode;
1335
1336   unsigned Line;
1337
1338   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1339               ArrayRef<Metadata *> Ops)
1340       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1341                 Ops),
1342         Line(Line) {}
1343   ~MDNamespace() {}
1344
1345   static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1346                               MDFile *File, StringRef Name, unsigned Line,
1347                               StorageType Storage, bool ShouldCreate = true) {
1348     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1349                    Line, Storage, ShouldCreate);
1350   }
1351   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1352                               Metadata *File, MDString *Name, unsigned Line,
1353                               StorageType Storage, bool ShouldCreate = true);
1354
1355   TempMDNamespace cloneImpl() const {
1356     return getTemporary(getContext(), getScope(), getFile(), getName(),
1357                         getLine());
1358   }
1359
1360 public:
1361   DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1362                                   unsigned Line),
1363                     (Scope, File, Name, Line))
1364   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1365                                   MDString *Name, unsigned Line),
1366                     (Scope, File, Name, Line))
1367
1368   TempMDNamespace clone() const { return cloneImpl(); }
1369
1370   unsigned getLine() const { return Line; }
1371   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1372   StringRef getName() const { return getStringOperand(2); }
1373
1374   Metadata *getRawScope() const { return getOperand(1); }
1375   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1376
1377   static bool classof(const Metadata *MD) {
1378     return MD->getMetadataID() == MDNamespaceKind;
1379   }
1380 };
1381
1382 /// \brief Base class for template parameters.
1383 class MDTemplateParameter : public DebugNode {
1384 protected:
1385   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1386                       unsigned Tag, ArrayRef<Metadata *> Ops)
1387       : DebugNode(Context, ID, Storage, Tag, Ops) {}
1388   ~MDTemplateParameter() {}
1389
1390 public:
1391   StringRef getName() const { return getStringOperand(0); }
1392   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1393
1394   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1395   Metadata *getRawType() const { return getOperand(1); }
1396
1397   static bool classof(const Metadata *MD) {
1398     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1399            MD->getMetadataID() == MDTemplateValueParameterKind;
1400   }
1401 };
1402
1403 class MDTemplateTypeParameter : public MDTemplateParameter {
1404   friend class LLVMContextImpl;
1405   friend class MDNode;
1406
1407   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1408                           ArrayRef<Metadata *> Ops)
1409       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1410                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1411   ~MDTemplateTypeParameter() {}
1412
1413   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1414                                           MDTypeRef Type, StorageType Storage,
1415                                           bool ShouldCreate = true) {
1416     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1417                    ShouldCreate);
1418   }
1419   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1420                                           Metadata *Type, StorageType Storage,
1421                                           bool ShouldCreate = true);
1422
1423   TempMDTemplateTypeParameter cloneImpl() const {
1424     return getTemporary(getContext(), getName(), getType());
1425   }
1426
1427 public:
1428   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
1429                     (Name, Type))
1430   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1431                     (Name, Type))
1432
1433   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1434
1435   static bool classof(const Metadata *MD) {
1436     return MD->getMetadataID() == MDTemplateTypeParameterKind;
1437   }
1438 };
1439
1440 class MDTemplateValueParameter : public MDTemplateParameter {
1441   friend class LLVMContextImpl;
1442   friend class MDNode;
1443
1444   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1445                            unsigned Tag, ArrayRef<Metadata *> Ops)
1446       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1447                             Ops) {}
1448   ~MDTemplateValueParameter() {}
1449
1450   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1451                                            StringRef Name, MDTypeRef Type,
1452                                            Metadata *Value, StorageType Storage,
1453                                            bool ShouldCreate = true) {
1454     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1455                    Value, Storage, ShouldCreate);
1456   }
1457   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1458                                            MDString *Name, Metadata *Type,
1459                                            Metadata *Value, StorageType Storage,
1460                                            bool ShouldCreate = true);
1461
1462   TempMDTemplateValueParameter cloneImpl() const {
1463     return getTemporary(getContext(), getTag(), getName(), getType(),
1464                         getValue());
1465   }
1466
1467 public:
1468   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1469                                                MDTypeRef Type, Metadata *Value),
1470                     (Tag, Name, Type, Value))
1471   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1472                                                Metadata *Type, Metadata *Value),
1473                     (Tag, Name, Type, Value))
1474
1475   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1476
1477   Metadata *getValue() const { return getOperand(2); }
1478
1479   static bool classof(const Metadata *MD) {
1480     return MD->getMetadataID() == MDTemplateValueParameterKind;
1481   }
1482 };
1483
1484 /// \brief Base class for variables.
1485 ///
1486 /// TODO: Hardcode to DW_TAG_variable.
1487 class MDVariable : public DebugNode {
1488   unsigned Line;
1489
1490 protected:
1491   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1492              unsigned Line, ArrayRef<Metadata *> Ops)
1493       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1494   ~MDVariable() {}
1495
1496 public:
1497   unsigned getLine() const { return Line; }
1498   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1499   StringRef getName() const { return getStringOperand(1); }
1500   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1501   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1502
1503   Metadata *getRawScope() const { return getOperand(0); }
1504   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1505   Metadata *getRawFile() const { return getOperand(2); }
1506   Metadata *getRawType() const { return getOperand(3); }
1507
1508   static bool classof(const Metadata *MD) {
1509     return MD->getMetadataID() == MDLocalVariableKind ||
1510            MD->getMetadataID() == MDGlobalVariableKind;
1511   }
1512 };
1513
1514 /// \brief Global variables.
1515 ///
1516 /// TODO: Remove DisplayName.  It's always equal to Name.
1517 class MDGlobalVariable : public MDVariable {
1518   friend class LLVMContextImpl;
1519   friend class MDNode;
1520
1521   bool IsLocalToUnit;
1522   bool IsDefinition;
1523
1524   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1525                    bool IsLocalToUnit, bool IsDefinition,
1526                    ArrayRef<Metadata *> Ops)
1527       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1528                    Line, Ops),
1529         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1530   ~MDGlobalVariable() {}
1531
1532   static MDGlobalVariable *
1533   getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1534           StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
1535           bool IsLocalToUnit, bool IsDefinition, ConstantAsMetadata *Variable,
1536           MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1537           bool ShouldCreate = true) {
1538     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1539                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1540                    IsLocalToUnit, IsDefinition, Variable,
1541                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1542   }
1543   static MDGlobalVariable *
1544   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1545           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1546           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1547           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1548           bool ShouldCreate = true);
1549
1550   TempMDGlobalVariable cloneImpl() const {
1551     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1552                         getFile(), getLine(), getType(), isLocalToUnit(),
1553                         isDefinition(), getVariable(),
1554                         getStaticDataMemberDeclaration());
1555   }
1556
1557 public:
1558   DEFINE_MDNODE_GET(MDGlobalVariable,
1559                     (MDScope * Scope, StringRef Name, StringRef LinkageName,
1560                      MDFile *File, unsigned Line, MDTypeRef Type,
1561                      bool IsLocalToUnit, bool IsDefinition,
1562                      ConstantAsMetadata *Variable,
1563                      MDDerivedType *StaticDataMemberDeclaration),
1564                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1565                      IsDefinition, Variable, StaticDataMemberDeclaration))
1566   DEFINE_MDNODE_GET(MDGlobalVariable,
1567                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1568                      Metadata *File, unsigned Line, Metadata *Type,
1569                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1570                      Metadata *StaticDataMemberDeclaration),
1571                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1572                      IsDefinition, Variable, StaticDataMemberDeclaration))
1573
1574   TempMDGlobalVariable clone() const { return cloneImpl(); }
1575
1576   bool isLocalToUnit() const { return IsLocalToUnit; }
1577   bool isDefinition() const { return IsDefinition; }
1578   StringRef getDisplayName() const { return getStringOperand(4); }
1579   StringRef getLinkageName() const { return getStringOperand(5); }
1580   ConstantAsMetadata *getVariable() const {
1581     return cast_or_null<ConstantAsMetadata>(getRawVariable());
1582   }
1583   MDDerivedType *getStaticDataMemberDeclaration() const {
1584     return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1585   }
1586
1587   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1588   Metadata *getRawVariable() const { return getOperand(6); }
1589   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1590
1591   static bool classof(const Metadata *MD) {
1592     return MD->getMetadataID() == MDGlobalVariableKind;
1593   }
1594 };
1595
1596 /// \brief Local variable.
1597 ///
1598 /// TODO: Split between arguments and otherwise.
1599 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1600 /// TODO: Split up flags.
1601 class MDLocalVariable : public MDVariable {
1602   friend class LLVMContextImpl;
1603   friend class MDNode;
1604
1605   unsigned Arg;
1606   unsigned Flags;
1607
1608   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1609                   unsigned Line, unsigned Arg, unsigned Flags,
1610                   ArrayRef<Metadata *> Ops)
1611       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1612         Flags(Flags) {}
1613   ~MDLocalVariable() {}
1614
1615   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1616                                   MDScope *Scope, StringRef Name, MDFile *File,
1617                                   unsigned Line, MDTypeRef Type, unsigned Arg,
1618                                   unsigned Flags, MDLocation *InlinedAt,
1619                                   StorageType Storage,
1620                                   bool ShouldCreate = true) {
1621     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1622                    File, Line, Type, Arg, Flags, InlinedAt, Storage,
1623                    ShouldCreate);
1624   }
1625   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1626                                   Metadata *Scope, MDString *Name,
1627                                   Metadata *File, unsigned Line, Metadata *Type,
1628                                   unsigned Arg, unsigned Flags,
1629                                   Metadata *InlinedAt, StorageType Storage,
1630                                   bool ShouldCreate = true);
1631
1632   TempMDLocalVariable cloneImpl() const {
1633     return getTemporary(getContext(), getTag(), getScope(), getName(),
1634                         getFile(), getLine(), getType(), getArg(), getFlags(),
1635                         getInlinedAt());
1636   }
1637
1638 public:
1639   DEFINE_MDNODE_GET(MDLocalVariable,
1640                     (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1641                      MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
1642                      unsigned Flags, MDLocation *InlinedAt = nullptr),
1643                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1644   DEFINE_MDNODE_GET(MDLocalVariable,
1645                     (unsigned Tag, Metadata *Scope, MDString *Name,
1646                      Metadata *File, unsigned Line, Metadata *Type,
1647                      unsigned Arg, unsigned Flags,
1648                      Metadata *InlinedAt = nullptr),
1649                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1650
1651   TempMDLocalVariable clone() const { return cloneImpl(); }
1652
1653   /// \brief Get the local scope for this variable.
1654   ///
1655   /// Variables must be defined in a local scope.
1656   MDLocalScope *getScope() const {
1657     return cast<MDLocalScope>(MDVariable::getScope());
1658   }
1659
1660   unsigned getArg() const { return Arg; }
1661   unsigned getFlags() const { return Flags; }
1662   MDLocation *getInlinedAt() const {
1663     return cast_or_null<MDLocation>(getRawInlinedAt());
1664   }
1665
1666   Metadata *getRawInlinedAt() const { return getOperand(4); }
1667
1668   /// \brief Check that a location is valid for this variable.
1669   ///
1670   /// Check that \c DL has the same inlined-at location as this variable,
1671   /// making them valid for the same \a DbgInfoIntrinsic.
1672   bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1673     return getInlinedAt() == (DL ? DL->getInlinedAt() : nullptr);
1674   }
1675
1676   /// \brief Get an inlined version of this variable.
1677   ///
1678   /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1679   MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1680     if (InlinedAt == getInlinedAt())
1681       return const_cast<MDLocalVariable *>(this);
1682     auto Temp = clone();
1683     Temp->replaceOperandWith(4, InlinedAt);
1684     return replaceWithUniqued(std::move(Temp));
1685   }
1686   MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1687
1688   static bool classof(const Metadata *MD) {
1689     return MD->getMetadataID() == MDLocalVariableKind;
1690   }
1691 };
1692
1693 /// \brief DWARF expression.
1694 ///
1695 /// TODO: Co-allocate the expression elements.
1696 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1697 /// storage types.
1698 class MDExpression : public MDNode {
1699   friend class LLVMContextImpl;
1700   friend class MDNode;
1701
1702   std::vector<uint64_t> Elements;
1703
1704   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1705       : MDNode(C, MDExpressionKind, Storage, None),
1706         Elements(Elements.begin(), Elements.end()) {}
1707   ~MDExpression() {}
1708
1709   static MDExpression *getImpl(LLVMContext &Context,
1710                                ArrayRef<uint64_t> Elements, StorageType Storage,
1711                                bool ShouldCreate = true);
1712
1713   TempMDExpression cloneImpl() const {
1714     return getTemporary(getContext(), getElements());
1715   }
1716
1717 public:
1718   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1719
1720   TempMDExpression clone() const { return cloneImpl(); }
1721
1722   ArrayRef<uint64_t> getElements() const { return Elements; }
1723
1724   unsigned getNumElements() const { return Elements.size(); }
1725   uint64_t getElement(unsigned I) const {
1726     assert(I < Elements.size() && "Index out of range");
1727     return Elements[I];
1728   }
1729
1730   typedef ArrayRef<uint64_t>::iterator element_iterator;
1731   element_iterator elements_begin() const { return getElements().begin(); }
1732   element_iterator elements_end() const { return getElements().end(); }
1733
1734   /// \brief A lightweight wrapper around an expression operand.
1735   ///
1736   /// TODO: Store arguments directly and change \a MDExpression to store a
1737   /// range of these.
1738   class ExprOperand {
1739     const uint64_t *Op;
1740
1741   public:
1742     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1743
1744     const uint64_t *get() const { return Op; }
1745
1746     /// \brief Get the operand code.
1747     uint64_t getOp() const { return *Op; }
1748
1749     /// \brief Get an argument to the operand.
1750     ///
1751     /// Never returns the operand itself.
1752     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1753
1754     unsigned getNumArgs() const { return getSize() - 1; }
1755
1756     /// \brief Return the size of the operand.
1757     ///
1758     /// Return the number of elements in the operand (1 + args).
1759     unsigned getSize() const;
1760   };
1761
1762   /// \brief An iterator for expression operands.
1763   class expr_op_iterator
1764       : public std::iterator<std::input_iterator_tag, ExprOperand> {
1765     ExprOperand Op;
1766
1767   public:
1768     explicit expr_op_iterator(element_iterator I) : Op(I) {}
1769
1770     element_iterator getBase() const { return Op.get(); }
1771     const ExprOperand &operator*() const { return Op; }
1772     const ExprOperand *operator->() const { return &Op; }
1773
1774     expr_op_iterator &operator++() {
1775       increment();
1776       return *this;
1777     }
1778     expr_op_iterator operator++(int) {
1779       expr_op_iterator T(*this);
1780       increment();
1781       return T;
1782     }
1783
1784     bool operator==(const expr_op_iterator &X) const {
1785       return getBase() == X.getBase();
1786     }
1787     bool operator!=(const expr_op_iterator &X) const {
1788       return getBase() != X.getBase();
1789     }
1790
1791   private:
1792     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
1793   };
1794
1795   /// \brief Visit the elements via ExprOperand wrappers.
1796   ///
1797   /// These range iterators visit elements through \a ExprOperand wrappers.
1798   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
1799   /// true.
1800   ///
1801   /// \pre \a isValid() gives \c true.
1802   /// @{
1803   expr_op_iterator expr_op_begin() const {
1804     return expr_op_iterator(elements_begin());
1805   }
1806   expr_op_iterator expr_op_end() const {
1807     return expr_op_iterator(elements_end());
1808   }
1809   /// @}
1810
1811   bool isValid() const;
1812
1813   static bool classof(const Metadata *MD) {
1814     return MD->getMetadataID() == MDExpressionKind;
1815   }
1816 };
1817
1818 class MDObjCProperty : public DebugNode {
1819   friend class LLVMContextImpl;
1820   friend class MDNode;
1821
1822   unsigned Line;
1823   unsigned Attributes;
1824
1825   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
1826                  unsigned Attributes, ArrayRef<Metadata *> Ops)
1827       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
1828                   Ops),
1829         Line(Line), Attributes(Attributes) {}
1830   ~MDObjCProperty() {}
1831
1832   static MDObjCProperty *
1833   getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
1834           StringRef GetterName, StringRef SetterName, unsigned Attributes,
1835           MDType *Type, StorageType Storage, bool ShouldCreate = true) {
1836     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
1837                    getCanonicalMDString(Context, GetterName),
1838                    getCanonicalMDString(Context, SetterName), Attributes, Type,
1839                    Storage, ShouldCreate);
1840   }
1841   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
1842                                  Metadata *File, unsigned Line,
1843                                  MDString *GetterName, MDString *SetterName,
1844                                  unsigned Attributes, Metadata *Type,
1845                                  StorageType Storage, bool ShouldCreate = true);
1846
1847   TempMDObjCProperty cloneImpl() const {
1848     return getTemporary(getContext(), getName(), getFile(), getLine(),
1849                         getGetterName(), getSetterName(), getAttributes(),
1850                         getType());
1851   }
1852
1853 public:
1854   DEFINE_MDNODE_GET(MDObjCProperty,
1855                     (StringRef Name, MDFile *File, unsigned Line,
1856                      StringRef GetterName, StringRef SetterName,
1857                      unsigned Attributes, MDType *Type),
1858                     (Name, File, Line, GetterName, SetterName, Attributes,
1859                      Type))
1860   DEFINE_MDNODE_GET(MDObjCProperty,
1861                     (MDString * Name, Metadata *File, unsigned Line,
1862                      MDString *GetterName, MDString *SetterName,
1863                      unsigned Attributes, Metadata *Type),
1864                     (Name, File, Line, GetterName, SetterName, Attributes,
1865                      Type))
1866
1867   TempMDObjCProperty clone() const { return cloneImpl(); }
1868
1869   unsigned getLine() const { return Line; }
1870   unsigned getAttributes() const { return Attributes; }
1871   StringRef getName() const { return getStringOperand(0); }
1872   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1873   StringRef getGetterName() const { return getStringOperand(2); }
1874   StringRef getSetterName() const { return getStringOperand(3); }
1875   MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
1876
1877   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1878   Metadata *getRawFile() const { return getOperand(1); }
1879   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
1880   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
1881   Metadata *getRawType() const { return getOperand(4); }
1882
1883   static bool classof(const Metadata *MD) {
1884     return MD->getMetadataID() == MDObjCPropertyKind;
1885   }
1886 };
1887
1888 class MDImportedEntity : public DebugNode {
1889   friend class LLVMContextImpl;
1890   friend class MDNode;
1891
1892   unsigned Line;
1893
1894   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
1895                    unsigned Line, ArrayRef<Metadata *> Ops)
1896       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
1897   ~MDImportedEntity() {}
1898
1899   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1900                                    MDScope *Scope, Metadata *Entity,
1901                                    unsigned Line, StringRef Name,
1902                                    StorageType Storage,
1903                                    bool ShouldCreate = true) {
1904     return getImpl(Context, Tag, Scope, Entity, Line,
1905                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
1906   }
1907   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1908                                    Metadata *Scope, Metadata *Entity,
1909                                    unsigned Line, MDString *Name,
1910                                    StorageType Storage,
1911                                    bool ShouldCreate = true);
1912
1913   TempMDImportedEntity cloneImpl() const {
1914     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
1915                         getLine(), getName());
1916   }
1917
1918 public:
1919   DEFINE_MDNODE_GET(MDImportedEntity,
1920                     (unsigned Tag, MDScope *Scope, Metadata *Entity,
1921                      unsigned Line, StringRef Name = ""),
1922                     (Tag, Scope, Entity, Line, Name))
1923   DEFINE_MDNODE_GET(MDImportedEntity,
1924                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1925                      unsigned Line, MDString *Name),
1926                     (Tag, Scope, Entity, Line, Name))
1927
1928   TempMDImportedEntity clone() const { return cloneImpl(); }
1929
1930   unsigned getLine() const { return Line; }
1931   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1932   Metadata *getEntity() const { return getRawEntity(); }
1933   StringRef getName() const { return getStringOperand(2); }
1934
1935   Metadata *getRawScope() const { return getOperand(0); }
1936   Metadata *getRawEntity() const { return getOperand(1); }
1937   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1938
1939   static bool classof(const Metadata *MD) {
1940     return MD->getMetadataID() == MDImportedEntityKind;
1941   }
1942 };
1943
1944 } // end namespace llvm
1945
1946 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
1947 #undef DEFINE_MDNODE_GET_UNPACK
1948 #undef DEFINE_MDNODE_GET
1949
1950 #endif