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