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