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