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