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