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