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