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