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