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