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