DebugInfo: Use DebugNodeRef in MDImportedEntity::getEntity()
[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   unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1240   bool isPrivate() const {
1241     return (getFlags() & FlagAccessibility) == FlagPrivate;
1242   }
1243   bool isProtected() const {
1244     return (getFlags() & FlagAccessibility) == FlagProtected;
1245   }
1246   bool isPublic() const {
1247     return (getFlags() & FlagAccessibility) == FlagPublic;
1248   }
1249   bool isExplicit() const { return getFlags() & FlagExplicit; }
1250   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1251
1252   /// \brief Check if this is reference-qualified.
1253   ///
1254   /// Return true if this subprogram is a C++11 reference-qualified non-static
1255   /// member function (void foo() &).
1256   unsigned isLValueReference() const {
1257     return getFlags() & FlagLValueReference;
1258   }
1259
1260   /// \brief Check if this is rvalue-reference-qualified.
1261   ///
1262   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1263   /// non-static member function (void foo() &&).
1264   unsigned isRValueReference() const {
1265     return getFlags() & FlagRValueReference;
1266   }
1267
1268   MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
1269
1270   StringRef getName() const { return getStringOperand(2); }
1271   StringRef getDisplayName() const { return getStringOperand(3); }
1272   StringRef getLinkageName() const { return getStringOperand(4); }
1273
1274   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1275   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1276
1277   MDSubroutineType *getType() const {
1278     return cast_or_null<MDSubroutineType>(getRawType());
1279   }
1280   MDTypeRef getContainingType() const {
1281     return MDTypeRef(getRawContainingType());
1282   }
1283
1284   ConstantAsMetadata *getFunction() const {
1285     return cast_or_null<ConstantAsMetadata>(getRawFunction());
1286   }
1287   MDTemplateParameterArray getTemplateParams() const {
1288     return cast_or_null<MDTuple>(getRawTemplateParams());
1289   }
1290   MDSubprogram *getDeclaration() const {
1291     return cast_or_null<MDSubprogram>(getRawDeclaration());
1292   }
1293   MDLocalVariableArray getVariables() const {
1294     return cast_or_null<MDTuple>(getRawVariables());
1295   }
1296
1297   Metadata *getRawScope() const { return getOperand(1); }
1298   Metadata *getRawType() const { return getOperand(5); }
1299   Metadata *getRawContainingType() const { return getOperand(6); }
1300   Metadata *getRawFunction() const { return getOperand(7); }
1301   Metadata *getRawTemplateParams() const { return getOperand(8); }
1302   Metadata *getRawDeclaration() const { return getOperand(9); }
1303   Metadata *getRawVariables() const { return getOperand(10); }
1304
1305   /// \brief Replace the function.
1306   ///
1307   /// If \a isUniqued() and not \a isResolved(), this could node will be
1308   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1309   /// that's a problem.
1310   /// @{
1311   void replaceFunction(Function *F);
1312   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1313   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1314   /// @}
1315
1316   static bool classof(const Metadata *MD) {
1317     return MD->getMetadataID() == MDSubprogramKind;
1318   }
1319 };
1320
1321 class MDLexicalBlockBase : public MDLocalScope {
1322 protected:
1323   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1324                      ArrayRef<Metadata *> Ops)
1325       : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1326   ~MDLexicalBlockBase() {}
1327
1328 public:
1329   MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
1330
1331   Metadata *getRawScope() const { return getOperand(1); }
1332
1333   static bool classof(const Metadata *MD) {
1334     return MD->getMetadataID() == MDLexicalBlockKind ||
1335            MD->getMetadataID() == MDLexicalBlockFileKind;
1336   }
1337 };
1338
1339 class MDLexicalBlock : public MDLexicalBlockBase {
1340   friend class LLVMContextImpl;
1341   friend class MDNode;
1342
1343   unsigned Line;
1344   unsigned Column;
1345
1346   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1347                  unsigned Column, ArrayRef<Metadata *> Ops)
1348       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1349         Column(Column) {}
1350   ~MDLexicalBlock() {}
1351
1352   static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1353                                  MDFile *File, unsigned Line, unsigned Column,
1354                                  StorageType Storage,
1355                                  bool ShouldCreate = true) {
1356     return getImpl(Context, static_cast<Metadata *>(Scope),
1357                    static_cast<Metadata *>(File), Line, Column, Storage,
1358                    ShouldCreate);
1359   }
1360
1361   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1362                                  Metadata *File, unsigned Line, unsigned Column,
1363                                  StorageType Storage, bool ShouldCreate = true);
1364
1365   TempMDLexicalBlock cloneImpl() const {
1366     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1367                         getColumn());
1368   }
1369
1370 public:
1371   DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1372                                      unsigned Line, unsigned Column),
1373                     (Scope, File, Line, Column))
1374   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1375                                      unsigned Line, unsigned Column),
1376                     (Scope, File, Line, Column))
1377
1378   TempMDLexicalBlock clone() const { return cloneImpl(); }
1379
1380   unsigned getLine() const { return Line; }
1381   unsigned getColumn() const { return Column; }
1382
1383   static bool classof(const Metadata *MD) {
1384     return MD->getMetadataID() == MDLexicalBlockKind;
1385   }
1386 };
1387
1388 class MDLexicalBlockFile : public MDLexicalBlockBase {
1389   friend class LLVMContextImpl;
1390   friend class MDNode;
1391
1392   unsigned Discriminator;
1393
1394   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1395                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1396       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1397         Discriminator(Discriminator) {}
1398   ~MDLexicalBlockFile() {}
1399
1400   static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1401                                      MDFile *File, unsigned Discriminator,
1402                                      StorageType Storage,
1403                                      bool ShouldCreate = true) {
1404     return getImpl(Context, static_cast<Metadata *>(Scope),
1405                    static_cast<Metadata *>(File), Discriminator, Storage,
1406                    ShouldCreate);
1407   }
1408
1409   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1410                                      Metadata *File, unsigned Discriminator,
1411                                      StorageType Storage,
1412                                      bool ShouldCreate = true);
1413
1414   TempMDLexicalBlockFile cloneImpl() const {
1415     return getTemporary(getContext(), getScope(), getFile(),
1416                         getDiscriminator());
1417   }
1418
1419 public:
1420   DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1421                                          unsigned Discriminator),
1422                     (Scope, File, Discriminator))
1423   DEFINE_MDNODE_GET(MDLexicalBlockFile,
1424                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1425                     (Scope, File, Discriminator))
1426
1427   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1428
1429   unsigned getDiscriminator() const { return Discriminator; }
1430
1431   static bool classof(const Metadata *MD) {
1432     return MD->getMetadataID() == MDLexicalBlockFileKind;
1433   }
1434 };
1435
1436 class MDNamespace : public MDScope {
1437   friend class LLVMContextImpl;
1438   friend class MDNode;
1439
1440   unsigned Line;
1441
1442   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1443               ArrayRef<Metadata *> Ops)
1444       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1445                 Ops),
1446         Line(Line) {}
1447   ~MDNamespace() {}
1448
1449   static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1450                               MDFile *File, StringRef Name, unsigned Line,
1451                               StorageType Storage, bool ShouldCreate = true) {
1452     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1453                    Line, Storage, ShouldCreate);
1454   }
1455   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1456                               Metadata *File, MDString *Name, unsigned Line,
1457                               StorageType Storage, bool ShouldCreate = true);
1458
1459   TempMDNamespace cloneImpl() const {
1460     return getTemporary(getContext(), getScope(), getFile(), getName(),
1461                         getLine());
1462   }
1463
1464 public:
1465   DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1466                                   unsigned Line),
1467                     (Scope, File, Name, Line))
1468   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1469                                   MDString *Name, unsigned Line),
1470                     (Scope, File, Name, Line))
1471
1472   TempMDNamespace clone() const { return cloneImpl(); }
1473
1474   unsigned getLine() const { return Line; }
1475   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1476   StringRef getName() const { return getStringOperand(2); }
1477
1478   Metadata *getRawScope() const { return getOperand(1); }
1479   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1480
1481   static bool classof(const Metadata *MD) {
1482     return MD->getMetadataID() == MDNamespaceKind;
1483   }
1484 };
1485
1486 /// \brief Base class for template parameters.
1487 class MDTemplateParameter : public DebugNode {
1488 protected:
1489   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1490                       unsigned Tag, ArrayRef<Metadata *> Ops)
1491       : DebugNode(Context, ID, Storage, Tag, Ops) {}
1492   ~MDTemplateParameter() {}
1493
1494 public:
1495   StringRef getName() const { return getStringOperand(0); }
1496   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1497
1498   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1499   Metadata *getRawType() const { return getOperand(1); }
1500
1501   static bool classof(const Metadata *MD) {
1502     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1503            MD->getMetadataID() == MDTemplateValueParameterKind;
1504   }
1505 };
1506
1507 class MDTemplateTypeParameter : public MDTemplateParameter {
1508   friend class LLVMContextImpl;
1509   friend class MDNode;
1510
1511   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1512                           ArrayRef<Metadata *> Ops)
1513       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1514                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1515   ~MDTemplateTypeParameter() {}
1516
1517   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1518                                           MDTypeRef Type, StorageType Storage,
1519                                           bool ShouldCreate = true) {
1520     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1521                    ShouldCreate);
1522   }
1523   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1524                                           Metadata *Type, StorageType Storage,
1525                                           bool ShouldCreate = true);
1526
1527   TempMDTemplateTypeParameter cloneImpl() const {
1528     return getTemporary(getContext(), getName(), getType());
1529   }
1530
1531 public:
1532   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
1533                     (Name, Type))
1534   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1535                     (Name, Type))
1536
1537   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1538
1539   static bool classof(const Metadata *MD) {
1540     return MD->getMetadataID() == MDTemplateTypeParameterKind;
1541   }
1542 };
1543
1544 class MDTemplateValueParameter : public MDTemplateParameter {
1545   friend class LLVMContextImpl;
1546   friend class MDNode;
1547
1548   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1549                            unsigned Tag, ArrayRef<Metadata *> Ops)
1550       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1551                             Ops) {}
1552   ~MDTemplateValueParameter() {}
1553
1554   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1555                                            StringRef Name, MDTypeRef Type,
1556                                            Metadata *Value, StorageType Storage,
1557                                            bool ShouldCreate = true) {
1558     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1559                    Value, Storage, ShouldCreate);
1560   }
1561   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1562                                            MDString *Name, Metadata *Type,
1563                                            Metadata *Value, StorageType Storage,
1564                                            bool ShouldCreate = true);
1565
1566   TempMDTemplateValueParameter cloneImpl() const {
1567     return getTemporary(getContext(), getTag(), getName(), getType(),
1568                         getValue());
1569   }
1570
1571 public:
1572   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1573                                                MDTypeRef Type, Metadata *Value),
1574                     (Tag, Name, Type, Value))
1575   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1576                                                Metadata *Type, Metadata *Value),
1577                     (Tag, Name, Type, Value))
1578
1579   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1580
1581   Metadata *getValue() const { return getOperand(2); }
1582
1583   static bool classof(const Metadata *MD) {
1584     return MD->getMetadataID() == MDTemplateValueParameterKind;
1585   }
1586 };
1587
1588 /// \brief Base class for variables.
1589 ///
1590 /// TODO: Hardcode to DW_TAG_variable.
1591 class MDVariable : public DebugNode {
1592   unsigned Line;
1593
1594 protected:
1595   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1596              unsigned Line, ArrayRef<Metadata *> Ops)
1597       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1598   ~MDVariable() {}
1599
1600 public:
1601   unsigned getLine() const { return Line; }
1602   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1603   StringRef getName() const { return getStringOperand(1); }
1604   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1605   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1606
1607   Metadata *getRawScope() const { return getOperand(0); }
1608   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1609   Metadata *getRawFile() const { return getOperand(2); }
1610   Metadata *getRawType() const { return getOperand(3); }
1611
1612   static bool classof(const Metadata *MD) {
1613     return MD->getMetadataID() == MDLocalVariableKind ||
1614            MD->getMetadataID() == MDGlobalVariableKind;
1615   }
1616 };
1617
1618 /// \brief Global variables.
1619 ///
1620 /// TODO: Remove DisplayName.  It's always equal to Name.
1621 class MDGlobalVariable : public MDVariable {
1622   friend class LLVMContextImpl;
1623   friend class MDNode;
1624
1625   bool IsLocalToUnit;
1626   bool IsDefinition;
1627
1628   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1629                    bool IsLocalToUnit, bool IsDefinition,
1630                    ArrayRef<Metadata *> Ops)
1631       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1632                    Line, Ops),
1633         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1634   ~MDGlobalVariable() {}
1635
1636   static MDGlobalVariable *
1637   getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1638           StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
1639           bool IsLocalToUnit, bool IsDefinition, ConstantAsMetadata *Variable,
1640           MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1641           bool ShouldCreate = true) {
1642     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1643                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1644                    IsLocalToUnit, IsDefinition, Variable,
1645                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1646   }
1647   static MDGlobalVariable *
1648   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1649           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1650           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1651           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1652           bool ShouldCreate = true);
1653
1654   TempMDGlobalVariable cloneImpl() const {
1655     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1656                         getFile(), getLine(), getType(), isLocalToUnit(),
1657                         isDefinition(), getVariable(),
1658                         getStaticDataMemberDeclaration());
1659   }
1660
1661 public:
1662   DEFINE_MDNODE_GET(MDGlobalVariable,
1663                     (MDScope * Scope, StringRef Name, StringRef LinkageName,
1664                      MDFile *File, unsigned Line, MDTypeRef Type,
1665                      bool IsLocalToUnit, bool IsDefinition,
1666                      ConstantAsMetadata *Variable,
1667                      MDDerivedType *StaticDataMemberDeclaration),
1668                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1669                      IsDefinition, Variable, StaticDataMemberDeclaration))
1670   DEFINE_MDNODE_GET(MDGlobalVariable,
1671                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1672                      Metadata *File, unsigned Line, Metadata *Type,
1673                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1674                      Metadata *StaticDataMemberDeclaration),
1675                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1676                      IsDefinition, Variable, StaticDataMemberDeclaration))
1677
1678   TempMDGlobalVariable clone() const { return cloneImpl(); }
1679
1680   bool isLocalToUnit() const { return IsLocalToUnit; }
1681   bool isDefinition() const { return IsDefinition; }
1682   StringRef getDisplayName() const { return getStringOperand(4); }
1683   StringRef getLinkageName() const { return getStringOperand(5); }
1684   ConstantAsMetadata *getVariable() const {
1685     return cast_or_null<ConstantAsMetadata>(getRawVariable());
1686   }
1687   MDDerivedType *getStaticDataMemberDeclaration() const {
1688     return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1689   }
1690
1691   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1692   Metadata *getRawVariable() const { return getOperand(6); }
1693   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1694
1695   static bool classof(const Metadata *MD) {
1696     return MD->getMetadataID() == MDGlobalVariableKind;
1697   }
1698 };
1699
1700 /// \brief Local variable.
1701 ///
1702 /// TODO: Split between arguments and otherwise.
1703 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1704 /// TODO: Split up flags.
1705 class MDLocalVariable : public MDVariable {
1706   friend class LLVMContextImpl;
1707   friend class MDNode;
1708
1709   unsigned Arg;
1710   unsigned Flags;
1711
1712   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1713                   unsigned Line, unsigned Arg, unsigned Flags,
1714                   ArrayRef<Metadata *> Ops)
1715       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1716         Flags(Flags) {}
1717   ~MDLocalVariable() {}
1718
1719   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1720                                   MDScope *Scope, StringRef Name, MDFile *File,
1721                                   unsigned Line, MDTypeRef Type, unsigned Arg,
1722                                   unsigned Flags, MDLocation *InlinedAt,
1723                                   StorageType Storage,
1724                                   bool ShouldCreate = true) {
1725     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1726                    File, Line, Type, Arg, Flags, InlinedAt, Storage,
1727                    ShouldCreate);
1728   }
1729   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1730                                   Metadata *Scope, MDString *Name,
1731                                   Metadata *File, unsigned Line, Metadata *Type,
1732                                   unsigned Arg, unsigned Flags,
1733                                   Metadata *InlinedAt, StorageType Storage,
1734                                   bool ShouldCreate = true);
1735
1736   TempMDLocalVariable cloneImpl() const {
1737     return getTemporary(getContext(), getTag(), getScope(), getName(),
1738                         getFile(), getLine(), getType(), getArg(), getFlags(),
1739                         getInlinedAt());
1740   }
1741
1742 public:
1743   DEFINE_MDNODE_GET(MDLocalVariable,
1744                     (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1745                      MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
1746                      unsigned Flags, MDLocation *InlinedAt = nullptr),
1747                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1748   DEFINE_MDNODE_GET(MDLocalVariable,
1749                     (unsigned Tag, Metadata *Scope, MDString *Name,
1750                      Metadata *File, unsigned Line, Metadata *Type,
1751                      unsigned Arg, unsigned Flags,
1752                      Metadata *InlinedAt = nullptr),
1753                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1754
1755   TempMDLocalVariable clone() const { return cloneImpl(); }
1756
1757   /// \brief Get the local scope for this variable.
1758   ///
1759   /// Variables must be defined in a local scope.
1760   MDLocalScope *getScope() const {
1761     return cast<MDLocalScope>(MDVariable::getScope());
1762   }
1763
1764   unsigned getArg() const { return Arg; }
1765   unsigned getFlags() const { return Flags; }
1766   MDLocation *getInlinedAt() const {
1767     return cast_or_null<MDLocation>(getRawInlinedAt());
1768   }
1769
1770   Metadata *getRawInlinedAt() const { return getOperand(4); }
1771
1772   bool isArtificial() const { return getFlags() & FlagArtificial; }
1773   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1774
1775   /// \brief Check that a location is valid for this variable.
1776   ///
1777   /// Check that \c DL has the same inlined-at location as this variable,
1778   /// making them valid for the same \a DbgInfoIntrinsic.
1779   bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1780     return getInlinedAt() == (DL ? DL->getInlinedAt() : nullptr);
1781   }
1782
1783   /// \brief Get an inlined version of this variable.
1784   ///
1785   /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1786   MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1787     if (InlinedAt == getInlinedAt())
1788       return const_cast<MDLocalVariable *>(this);
1789     auto Temp = clone();
1790     Temp->replaceOperandWith(4, InlinedAt);
1791     return replaceWithUniqued(std::move(Temp));
1792   }
1793   MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1794
1795   static bool classof(const Metadata *MD) {
1796     return MD->getMetadataID() == MDLocalVariableKind;
1797   }
1798 };
1799
1800 /// \brief DWARF expression.
1801 ///
1802 /// TODO: Co-allocate the expression elements.
1803 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1804 /// storage types.
1805 class MDExpression : public MDNode {
1806   friend class LLVMContextImpl;
1807   friend class MDNode;
1808
1809   std::vector<uint64_t> Elements;
1810
1811   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1812       : MDNode(C, MDExpressionKind, Storage, None),
1813         Elements(Elements.begin(), Elements.end()) {}
1814   ~MDExpression() {}
1815
1816   static MDExpression *getImpl(LLVMContext &Context,
1817                                ArrayRef<uint64_t> Elements, StorageType Storage,
1818                                bool ShouldCreate = true);
1819
1820   TempMDExpression cloneImpl() const {
1821     return getTemporary(getContext(), getElements());
1822   }
1823
1824 public:
1825   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1826
1827   TempMDExpression clone() const { return cloneImpl(); }
1828
1829   ArrayRef<uint64_t> getElements() const { return Elements; }
1830
1831   unsigned getNumElements() const { return Elements.size(); }
1832   uint64_t getElement(unsigned I) const {
1833     assert(I < Elements.size() && "Index out of range");
1834     return Elements[I];
1835   }
1836
1837   /// \brief Return whether this is a piece of an aggregate variable.
1838   bool isBitPiece() const;
1839
1840   /// \brief Return the offset of this piece in bits.
1841   uint64_t getBitPieceOffset() const;
1842
1843   /// \brief Return the size of this piece in bits.
1844   uint64_t getBitPieceSize() const;
1845
1846   typedef ArrayRef<uint64_t>::iterator element_iterator;
1847   element_iterator elements_begin() const { return getElements().begin(); }
1848   element_iterator elements_end() const { return getElements().end(); }
1849
1850   /// \brief A lightweight wrapper around an expression operand.
1851   ///
1852   /// TODO: Store arguments directly and change \a MDExpression to store a
1853   /// range of these.
1854   class ExprOperand {
1855     const uint64_t *Op;
1856
1857   public:
1858     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1859
1860     const uint64_t *get() const { return Op; }
1861
1862     /// \brief Get the operand code.
1863     uint64_t getOp() const { return *Op; }
1864
1865     /// \brief Get an argument to the operand.
1866     ///
1867     /// Never returns the operand itself.
1868     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1869
1870     unsigned getNumArgs() const { return getSize() - 1; }
1871
1872     /// \brief Return the size of the operand.
1873     ///
1874     /// Return the number of elements in the operand (1 + args).
1875     unsigned getSize() const;
1876   };
1877
1878   /// \brief An iterator for expression operands.
1879   class expr_op_iterator
1880       : public std::iterator<std::input_iterator_tag, ExprOperand> {
1881     ExprOperand Op;
1882
1883   public:
1884     explicit expr_op_iterator(element_iterator I) : Op(I) {}
1885
1886     element_iterator getBase() const { return Op.get(); }
1887     const ExprOperand &operator*() const { return Op; }
1888     const ExprOperand *operator->() const { return &Op; }
1889
1890     expr_op_iterator &operator++() {
1891       increment();
1892       return *this;
1893     }
1894     expr_op_iterator operator++(int) {
1895       expr_op_iterator T(*this);
1896       increment();
1897       return T;
1898     }
1899
1900     /// \brief Get the next iterator.
1901     ///
1902     /// \a std::next() doesn't work because this is technically an
1903     /// input_iterator, but it's a perfectly valid operation.  This is an
1904     /// accessor to provide the same functionality.
1905     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
1906
1907     bool operator==(const expr_op_iterator &X) const {
1908       return getBase() == X.getBase();
1909     }
1910     bool operator!=(const expr_op_iterator &X) const {
1911       return getBase() != X.getBase();
1912     }
1913
1914   private:
1915     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
1916   };
1917
1918   /// \brief Visit the elements via ExprOperand wrappers.
1919   ///
1920   /// These range iterators visit elements through \a ExprOperand wrappers.
1921   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
1922   /// true.
1923   ///
1924   /// \pre \a isValid() gives \c true.
1925   /// @{
1926   expr_op_iterator expr_op_begin() const {
1927     return expr_op_iterator(elements_begin());
1928   }
1929   expr_op_iterator expr_op_end() const {
1930     return expr_op_iterator(elements_end());
1931   }
1932   /// @}
1933
1934   bool isValid() const;
1935
1936   static bool classof(const Metadata *MD) {
1937     return MD->getMetadataID() == MDExpressionKind;
1938   }
1939 };
1940
1941 class MDObjCProperty : public DebugNode {
1942   friend class LLVMContextImpl;
1943   friend class MDNode;
1944
1945   unsigned Line;
1946   unsigned Attributes;
1947
1948   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
1949                  unsigned Attributes, ArrayRef<Metadata *> Ops)
1950       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
1951                   Ops),
1952         Line(Line), Attributes(Attributes) {}
1953   ~MDObjCProperty() {}
1954
1955   static MDObjCProperty *
1956   getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
1957           StringRef GetterName, StringRef SetterName, unsigned Attributes,
1958           MDType *Type, StorageType Storage, bool ShouldCreate = true) {
1959     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
1960                    getCanonicalMDString(Context, GetterName),
1961                    getCanonicalMDString(Context, SetterName), Attributes, Type,
1962                    Storage, ShouldCreate);
1963   }
1964   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
1965                                  Metadata *File, unsigned Line,
1966                                  MDString *GetterName, MDString *SetterName,
1967                                  unsigned Attributes, Metadata *Type,
1968                                  StorageType Storage, bool ShouldCreate = true);
1969
1970   TempMDObjCProperty cloneImpl() const {
1971     return getTemporary(getContext(), getName(), getFile(), getLine(),
1972                         getGetterName(), getSetterName(), getAttributes(),
1973                         getType());
1974   }
1975
1976 public:
1977   DEFINE_MDNODE_GET(MDObjCProperty,
1978                     (StringRef Name, MDFile *File, unsigned Line,
1979                      StringRef GetterName, StringRef SetterName,
1980                      unsigned Attributes, MDType *Type),
1981                     (Name, File, Line, GetterName, SetterName, Attributes,
1982                      Type))
1983   DEFINE_MDNODE_GET(MDObjCProperty,
1984                     (MDString * Name, Metadata *File, unsigned Line,
1985                      MDString *GetterName, MDString *SetterName,
1986                      unsigned Attributes, Metadata *Type),
1987                     (Name, File, Line, GetterName, SetterName, Attributes,
1988                      Type))
1989
1990   TempMDObjCProperty clone() const { return cloneImpl(); }
1991
1992   unsigned getLine() const { return Line; }
1993   unsigned getAttributes() const { return Attributes; }
1994   StringRef getName() const { return getStringOperand(0); }
1995   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1996   StringRef getGetterName() const { return getStringOperand(2); }
1997   StringRef getSetterName() const { return getStringOperand(3); }
1998   MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
1999
2000   MDString *getRawName() const { return getOperandAs<MDString>(0); }
2001   Metadata *getRawFile() const { return getOperand(1); }
2002   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2003   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2004   Metadata *getRawType() const { return getOperand(4); }
2005
2006   static bool classof(const Metadata *MD) {
2007     return MD->getMetadataID() == MDObjCPropertyKind;
2008   }
2009 };
2010
2011 class MDImportedEntity : public DebugNode {
2012   friend class LLVMContextImpl;
2013   friend class MDNode;
2014
2015   unsigned Line;
2016
2017   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2018                    unsigned Line, ArrayRef<Metadata *> Ops)
2019       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2020   ~MDImportedEntity() {}
2021
2022   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2023                                    MDScope *Scope, DebugNodeRef Entity,
2024                                    unsigned Line, StringRef Name,
2025                                    StorageType Storage,
2026                                    bool ShouldCreate = true) {
2027     return getImpl(Context, Tag, Scope, Entity, Line,
2028                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2029   }
2030   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2031                                    Metadata *Scope, Metadata *Entity,
2032                                    unsigned Line, MDString *Name,
2033                                    StorageType Storage,
2034                                    bool ShouldCreate = true);
2035
2036   TempMDImportedEntity cloneImpl() const {
2037     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2038                         getLine(), getName());
2039   }
2040
2041 public:
2042   DEFINE_MDNODE_GET(MDImportedEntity,
2043                     (unsigned Tag, MDScope *Scope, DebugNodeRef Entity,
2044                      unsigned Line, StringRef Name = ""),
2045                     (Tag, Scope, Entity, Line, Name))
2046   DEFINE_MDNODE_GET(MDImportedEntity,
2047                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
2048                      unsigned Line, MDString *Name),
2049                     (Tag, Scope, Entity, Line, Name))
2050
2051   TempMDImportedEntity clone() const { return cloneImpl(); }
2052
2053   unsigned getLine() const { return Line; }
2054   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
2055   DebugNodeRef getEntity() const { return DebugNodeRef(getRawEntity()); }
2056   StringRef getName() const { return getStringOperand(2); }
2057
2058   Metadata *getRawScope() const { return getOperand(0); }
2059   Metadata *getRawEntity() const { return getOperand(1); }
2060   MDString *getRawName() const { return getOperandAs<MDString>(2); }
2061
2062   static bool classof(const Metadata *MD) {
2063     return MD->getMetadataID() == MDImportedEntityKind;
2064   }
2065 };
2066
2067 } // end namespace llvm
2068
2069 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2070 #undef DEFINE_MDNODE_GET_UNPACK
2071 #undef DEFINE_MDNODE_GET
2072
2073 #endif