Verifier: Check operands of MDType subclasses and MDCompileUnit
[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, Metadata *File, unsigned Line, Metadata *Type,
1002           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1003           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1004           unsigned Flags, bool IsOptimized, Metadata *Function,
1005           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1006           StorageType Storage, bool ShouldCreate = true) {
1007     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1008                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1009                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1010                    Virtuality, VirtualIndex, Flags, IsOptimized, Function,
1011                    TemplateParams, Declaration, Variables, Storage,
1012                    ShouldCreate);
1013   }
1014   static MDSubprogram *
1015   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1016           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1017           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1018           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1019           unsigned Flags, bool IsOptimized, Metadata *Function,
1020           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1021           StorageType Storage, bool ShouldCreate = true);
1022
1023   TempMDSubprogram cloneImpl() const {
1024     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1025                         getFile(), getLine(), getType(), isLocalToUnit(),
1026                         isDefinition(), getScopeLine(), getContainingType(),
1027                         getVirtuality(), getVirtualIndex(), getFlags(),
1028                         isOptimized(), getFunction(), getTemplateParams(),
1029                         getDeclaration(), getVariables());
1030   }
1031
1032 public:
1033   DEFINE_MDNODE_GET(
1034       MDSubprogram,
1035       (Metadata * Scope, StringRef Name, StringRef LinkageName, Metadata *File,
1036        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1037        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1038        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1039        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1040        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1041       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1042        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1043        Function, TemplateParams, Declaration, Variables))
1044   DEFINE_MDNODE_GET(
1045       MDSubprogram,
1046       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1047        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1048        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1049        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1050        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1051        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1052       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1053        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1054        Function, TemplateParams, Declaration, Variables))
1055
1056   TempMDSubprogram clone() const { return cloneImpl(); }
1057
1058 public:
1059   unsigned getLine() const { return Line; }
1060   unsigned getVirtuality() const { return Virtuality; }
1061   unsigned getVirtualIndex() const { return VirtualIndex; }
1062   unsigned getScopeLine() const { return ScopeLine; }
1063   unsigned getFlags() const { return Flags; }
1064   bool isLocalToUnit() const { return IsLocalToUnit; }
1065   bool isDefinition() const { return IsDefinition; }
1066   bool isOptimized() const { return IsOptimized; }
1067
1068   Metadata *getScope() const { return getOperand(1); }
1069
1070   StringRef getName() const { return getStringOperand(2); }
1071   StringRef getDisplayName() const { return getStringOperand(3); }
1072   StringRef getLinkageName() const { return getStringOperand(4); }
1073
1074   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1075   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1076
1077   Metadata *getType() const { return getOperand(5); }
1078   Metadata *getContainingType() const { return getOperand(6); }
1079
1080   Metadata *getFunction() const { return getOperand(7); }
1081   Metadata *getTemplateParams() const { return getOperand(8); }
1082   Metadata *getDeclaration() const { return getOperand(9); }
1083   Metadata *getVariables() const { return getOperand(10); }
1084
1085   /// \brief Replace the function.
1086   ///
1087   /// If \a isUniqued() and not \a isResolved(), this could node will be
1088   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1089   /// that's a problem.
1090   /// @{
1091   void replaceFunction(Function *F);
1092   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1093   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1094   /// @}
1095
1096   static bool classof(const Metadata *MD) {
1097     return MD->getMetadataID() == MDSubprogramKind;
1098   }
1099 };
1100
1101 class MDLexicalBlockBase : public MDLocalScope {
1102 protected:
1103   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1104                      ArrayRef<Metadata *> Ops)
1105       : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1106   ~MDLexicalBlockBase() {}
1107
1108 public:
1109   Metadata *getScope() const { return getOperand(1); }
1110
1111   static bool classof(const Metadata *MD) {
1112     return MD->getMetadataID() == MDLexicalBlockKind ||
1113            MD->getMetadataID() == MDLexicalBlockFileKind;
1114   }
1115 };
1116
1117 class MDLexicalBlock : public MDLexicalBlockBase {
1118   friend class LLVMContextImpl;
1119   friend class MDNode;
1120
1121   unsigned Line;
1122   unsigned Column;
1123
1124   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1125                  unsigned Column, ArrayRef<Metadata *> Ops)
1126       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1127         Column(Column) {}
1128   ~MDLexicalBlock() {}
1129
1130   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1131                                  Metadata *File, unsigned Line, unsigned Column,
1132                                  StorageType Storage, bool ShouldCreate = true);
1133
1134   TempMDLexicalBlock cloneImpl() const {
1135     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1136                         getColumn());
1137   }
1138
1139 public:
1140   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1141                                      unsigned Line, unsigned Column),
1142                     (Scope, File, Line, Column))
1143
1144   TempMDLexicalBlock clone() const { return cloneImpl(); }
1145
1146   unsigned getLine() const { return Line; }
1147   unsigned getColumn() const { return Column; }
1148
1149   static bool classof(const Metadata *MD) {
1150     return MD->getMetadataID() == MDLexicalBlockKind;
1151   }
1152 };
1153
1154 class MDLexicalBlockFile : public MDLexicalBlockBase {
1155   friend class LLVMContextImpl;
1156   friend class MDNode;
1157
1158   unsigned Discriminator;
1159
1160   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1161                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1162       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1163         Discriminator(Discriminator) {}
1164   ~MDLexicalBlockFile() {}
1165
1166   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1167                                      Metadata *File, unsigned Discriminator,
1168                                      StorageType Storage,
1169                                      bool ShouldCreate = true);
1170
1171   TempMDLexicalBlockFile cloneImpl() const {
1172     return getTemporary(getContext(), getScope(), getFile(),
1173                         getDiscriminator());
1174   }
1175
1176 public:
1177   DEFINE_MDNODE_GET(MDLexicalBlockFile,
1178                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1179                     (Scope, File, Discriminator))
1180
1181   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1182
1183   unsigned getDiscriminator() const { return Discriminator; }
1184
1185   static bool classof(const Metadata *MD) {
1186     return MD->getMetadataID() == MDLexicalBlockFileKind;
1187   }
1188 };
1189
1190 class MDNamespace : public MDScope {
1191   friend class LLVMContextImpl;
1192   friend class MDNode;
1193
1194   unsigned Line;
1195
1196   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1197               ArrayRef<Metadata *> Ops)
1198       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1199                 Ops),
1200         Line(Line) {}
1201   ~MDNamespace() {}
1202
1203   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1204                               Metadata *File, StringRef Name, unsigned Line,
1205                               StorageType Storage, bool ShouldCreate = true) {
1206     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1207                    Line, Storage, ShouldCreate);
1208   }
1209   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1210                               Metadata *File, MDString *Name, unsigned Line,
1211                               StorageType Storage, bool ShouldCreate = true);
1212
1213   TempMDNamespace cloneImpl() const {
1214     return getTemporary(getContext(), getScope(), getFile(), getName(),
1215                         getLine());
1216   }
1217
1218 public:
1219   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1220                                   StringRef Name, unsigned Line),
1221                     (Scope, File, Name, Line))
1222   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1223                                   MDString *Name, unsigned Line),
1224                     (Scope, File, Name, Line))
1225
1226   TempMDNamespace clone() const { return cloneImpl(); }
1227
1228   unsigned getLine() const { return Line; }
1229   Metadata *getScope() const { return getOperand(1); }
1230   StringRef getName() const { return getStringOperand(2); }
1231
1232   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1233
1234   static bool classof(const Metadata *MD) {
1235     return MD->getMetadataID() == MDNamespaceKind;
1236   }
1237 };
1238
1239 /// \brief Base class for template parameters.
1240 class MDTemplateParameter : public DebugNode {
1241 protected:
1242   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1243                       unsigned Tag, ArrayRef<Metadata *> Ops)
1244       : DebugNode(Context, ID, Storage, Tag, Ops) {}
1245   ~MDTemplateParameter() {}
1246
1247 public:
1248   StringRef getName() const { return getStringOperand(0); }
1249   Metadata *getType() const { return getOperand(1); }
1250
1251   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1252
1253   static bool classof(const Metadata *MD) {
1254     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1255            MD->getMetadataID() == MDTemplateValueParameterKind;
1256   }
1257 };
1258
1259 class MDTemplateTypeParameter : public MDTemplateParameter {
1260   friend class LLVMContextImpl;
1261   friend class MDNode;
1262
1263   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1264                           ArrayRef<Metadata *> Ops)
1265       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1266                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1267   ~MDTemplateTypeParameter() {}
1268
1269   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1270                                           Metadata *Type, StorageType Storage,
1271                                           bool ShouldCreate = true) {
1272     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1273                    ShouldCreate);
1274   }
1275   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1276                                           Metadata *Type, StorageType Storage,
1277                                           bool ShouldCreate = true);
1278
1279   TempMDTemplateTypeParameter cloneImpl() const {
1280     return getTemporary(getContext(), getName(), getType());
1281   }
1282
1283 public:
1284   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, Metadata *Type),
1285                     (Name, Type))
1286   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1287                     (Name, Type))
1288
1289   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1290
1291   static bool classof(const Metadata *MD) {
1292     return MD->getMetadataID() == MDTemplateTypeParameterKind;
1293   }
1294 };
1295
1296 class MDTemplateValueParameter : public MDTemplateParameter {
1297   friend class LLVMContextImpl;
1298   friend class MDNode;
1299
1300   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1301                            unsigned Tag, ArrayRef<Metadata *> Ops)
1302       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1303                             Ops) {}
1304   ~MDTemplateValueParameter() {}
1305
1306   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1307                                            StringRef Name, Metadata *Type,
1308                                            Metadata *Value, StorageType Storage,
1309                                            bool ShouldCreate = true) {
1310     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1311                    Value, Storage, ShouldCreate);
1312   }
1313   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1314                                            MDString *Name, Metadata *Type,
1315                                            Metadata *Value, StorageType Storage,
1316                                            bool ShouldCreate = true);
1317
1318   TempMDTemplateValueParameter cloneImpl() const {
1319     return getTemporary(getContext(), getTag(), getName(), getType(),
1320                         getValue());
1321   }
1322
1323 public:
1324   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1325                                                Metadata *Type, Metadata *Value),
1326                     (Tag, Name, Type, Value))
1327   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1328                                                Metadata *Type, Metadata *Value),
1329                     (Tag, Name, Type, Value))
1330
1331   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1332
1333   Metadata *getValue() const { return getOperand(2); }
1334
1335   static bool classof(const Metadata *MD) {
1336     return MD->getMetadataID() == MDTemplateValueParameterKind;
1337   }
1338 };
1339
1340 /// \brief Base class for variables.
1341 ///
1342 /// TODO: Hardcode to DW_TAG_variable.
1343 class MDVariable : public DebugNode {
1344   unsigned Line;
1345
1346 protected:
1347   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1348              unsigned Line, ArrayRef<Metadata *> Ops)
1349       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1350   ~MDVariable() {}
1351
1352 public:
1353   unsigned getLine() const { return Line; }
1354   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1355   StringRef getName() const { return getStringOperand(1); }
1356   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1357   Metadata *getType() const { return getRawType(); }
1358
1359   Metadata *getRawScope() const { return getOperand(0); }
1360   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1361   Metadata *getRawFile() const { return getOperand(2); }
1362   Metadata *getRawType() const { return getOperand(3); }
1363
1364   static bool classof(const Metadata *MD) {
1365     return MD->getMetadataID() == MDLocalVariableKind ||
1366            MD->getMetadataID() == MDGlobalVariableKind;
1367   }
1368 };
1369
1370 /// \brief Global variables.
1371 ///
1372 /// TODO: Remove DisplayName.  It's always equal to Name.
1373 class MDGlobalVariable : public MDVariable {
1374   friend class LLVMContextImpl;
1375   friend class MDNode;
1376
1377   bool IsLocalToUnit;
1378   bool IsDefinition;
1379
1380   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1381                    bool IsLocalToUnit, bool IsDefinition,
1382                    ArrayRef<Metadata *> Ops)
1383       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1384                    Line, Ops),
1385         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1386   ~MDGlobalVariable() {}
1387
1388   static MDGlobalVariable *
1389   getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1390           StringRef LinkageName, MDFile *File, unsigned Line, Metadata *Type,
1391           bool IsLocalToUnit, bool IsDefinition, ConstantAsMetadata *Variable,
1392           MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1393           bool ShouldCreate = true) {
1394     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1395                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1396                    IsLocalToUnit, IsDefinition, Variable,
1397                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1398   }
1399   static MDGlobalVariable *
1400   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1401           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1402           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1403           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1404           bool ShouldCreate = true);
1405
1406   TempMDGlobalVariable cloneImpl() const {
1407     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1408                         getFile(), getLine(), getType(), isLocalToUnit(),
1409                         isDefinition(), getVariable(),
1410                         getStaticDataMemberDeclaration());
1411   }
1412
1413 public:
1414   DEFINE_MDNODE_GET(MDGlobalVariable,
1415                     (MDScope * Scope, StringRef Name, StringRef LinkageName,
1416                      MDFile *File, unsigned Line, Metadata *Type,
1417                      bool IsLocalToUnit, bool IsDefinition,
1418                      ConstantAsMetadata *Variable,
1419                      MDDerivedType *StaticDataMemberDeclaration),
1420                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1421                      IsDefinition, Variable, StaticDataMemberDeclaration))
1422   DEFINE_MDNODE_GET(MDGlobalVariable,
1423                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1424                      Metadata *File, unsigned Line, Metadata *Type,
1425                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1426                      Metadata *StaticDataMemberDeclaration),
1427                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1428                      IsDefinition, Variable, StaticDataMemberDeclaration))
1429
1430   TempMDGlobalVariable clone() const { return cloneImpl(); }
1431
1432   bool isLocalToUnit() const { return IsLocalToUnit; }
1433   bool isDefinition() const { return IsDefinition; }
1434   StringRef getDisplayName() const { return getStringOperand(4); }
1435   StringRef getLinkageName() const { return getStringOperand(5); }
1436   ConstantAsMetadata *getVariable() const {
1437     return cast_or_null<ConstantAsMetadata>(getRawVariable());
1438   }
1439   MDDerivedType *getStaticDataMemberDeclaration() const {
1440     return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1441   }
1442
1443   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1444   Metadata *getRawVariable() const { return getOperand(6); }
1445   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1446
1447   static bool classof(const Metadata *MD) {
1448     return MD->getMetadataID() == MDGlobalVariableKind;
1449   }
1450 };
1451
1452 /// \brief Local variable.
1453 ///
1454 /// TODO: Split between arguments and otherwise.
1455 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1456 /// TODO: Split up flags.
1457 class MDLocalVariable : public MDVariable {
1458   friend class LLVMContextImpl;
1459   friend class MDNode;
1460
1461   unsigned Arg;
1462   unsigned Flags;
1463
1464   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1465                   unsigned Line, unsigned Arg, unsigned Flags,
1466                   ArrayRef<Metadata *> Ops)
1467       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1468         Flags(Flags) {}
1469   ~MDLocalVariable() {}
1470
1471   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1472                                   MDScope *Scope, StringRef Name, MDFile *File,
1473                                   unsigned Line, Metadata *Type, unsigned Arg,
1474                                   unsigned Flags, MDLocation *InlinedAt,
1475                                   StorageType Storage,
1476                                   bool ShouldCreate = true) {
1477     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1478                    File, Line, Type, Arg, Flags, InlinedAt, Storage,
1479                    ShouldCreate);
1480   }
1481   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1482                                   Metadata *Scope, MDString *Name,
1483                                   Metadata *File, unsigned Line, Metadata *Type,
1484                                   unsigned Arg, unsigned Flags,
1485                                   Metadata *InlinedAt, StorageType Storage,
1486                                   bool ShouldCreate = true);
1487
1488   TempMDLocalVariable cloneImpl() const {
1489     return getTemporary(getContext(), getTag(), getScope(), getName(),
1490                         getFile(), getLine(), getType(), getArg(), getFlags(),
1491                         getInlinedAt());
1492   }
1493
1494 public:
1495   DEFINE_MDNODE_GET(MDLocalVariable,
1496                     (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1497                      MDFile *File, unsigned Line, Metadata *Type, unsigned Arg,
1498                      unsigned Flags, MDLocation *InlinedAt = nullptr),
1499                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1500   DEFINE_MDNODE_GET(MDLocalVariable,
1501                     (unsigned Tag, Metadata *Scope, MDString *Name,
1502                      Metadata *File, unsigned Line, Metadata *Type,
1503                      unsigned Arg, unsigned Flags,
1504                      Metadata *InlinedAt = nullptr),
1505                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1506
1507   TempMDLocalVariable clone() const { return cloneImpl(); }
1508
1509   /// \brief Get the local scope for this variable.
1510   ///
1511   /// Variables must be defined in a local scope.
1512   MDLocalScope *getScope() const {
1513     return cast<MDLocalScope>(MDVariable::getScope());
1514   }
1515
1516   unsigned getArg() const { return Arg; }
1517   unsigned getFlags() const { return Flags; }
1518   MDLocation *getInlinedAt() const {
1519     return cast_or_null<MDLocation>(getRawInlinedAt());
1520   }
1521
1522   Metadata *getRawInlinedAt() const { return getOperand(4); }
1523
1524   /// \brief Get an inlined version of this variable.
1525   ///
1526   /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1527   MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1528     if (InlinedAt == getInlinedAt())
1529       return const_cast<MDLocalVariable *>(this);
1530     auto Temp = clone();
1531     Temp->replaceOperandWith(4, InlinedAt);
1532     return replaceWithUniqued(std::move(Temp));
1533   }
1534   MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1535
1536   static bool classof(const Metadata *MD) {
1537     return MD->getMetadataID() == MDLocalVariableKind;
1538   }
1539 };
1540
1541 /// \brief DWARF expression.
1542 ///
1543 /// TODO: Co-allocate the expression elements.
1544 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1545 /// storage types.
1546 class MDExpression : public MDNode {
1547   friend class LLVMContextImpl;
1548   friend class MDNode;
1549
1550   std::vector<uint64_t> Elements;
1551
1552   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1553       : MDNode(C, MDExpressionKind, Storage, None),
1554         Elements(Elements.begin(), Elements.end()) {}
1555   ~MDExpression() {}
1556
1557   static MDExpression *getImpl(LLVMContext &Context,
1558                                ArrayRef<uint64_t> Elements, StorageType Storage,
1559                                bool ShouldCreate = true);
1560
1561   TempMDExpression cloneImpl() const {
1562     return getTemporary(getContext(), getElements());
1563   }
1564
1565 public:
1566   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1567
1568   TempMDExpression clone() const { return cloneImpl(); }
1569
1570   ArrayRef<uint64_t> getElements() const { return Elements; }
1571
1572   unsigned getNumElements() const { return Elements.size(); }
1573   uint64_t getElement(unsigned I) const {
1574     assert(I < Elements.size() && "Index out of range");
1575     return Elements[I];
1576   }
1577
1578   typedef ArrayRef<uint64_t>::iterator element_iterator;
1579   element_iterator elements_begin() const { return getElements().begin(); }
1580   element_iterator elements_end() const { return getElements().end(); }
1581
1582   /// \brief A lightweight wrapper around an expression operand.
1583   ///
1584   /// TODO: Store arguments directly and change \a MDExpression to store a
1585   /// range of these.
1586   class ExprOperand {
1587     const uint64_t *Op;
1588
1589   public:
1590     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1591
1592     const uint64_t *get() const { return Op; }
1593
1594     /// \brief Get the operand code.
1595     uint64_t getOp() const { return *Op; }
1596
1597     /// \brief Get an argument to the operand.
1598     ///
1599     /// Never returns the operand itself.
1600     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1601
1602     unsigned getNumArgs() const { return getSize() - 1; }
1603
1604     /// \brief Return the size of the operand.
1605     ///
1606     /// Return the number of elements in the operand (1 + args).
1607     unsigned getSize() const;
1608   };
1609
1610   /// \brief An iterator for expression operands.
1611   class expr_op_iterator
1612       : public std::iterator<std::input_iterator_tag, ExprOperand> {
1613     ExprOperand Op;
1614
1615   public:
1616     explicit expr_op_iterator(element_iterator I) : Op(I) {}
1617
1618     element_iterator getBase() const { return Op.get(); }
1619     const ExprOperand &operator*() const { return Op; }
1620     const ExprOperand *operator->() const { return &Op; }
1621
1622     expr_op_iterator &operator++() {
1623       increment();
1624       return *this;
1625     }
1626     expr_op_iterator operator++(int) {
1627       expr_op_iterator T(*this);
1628       increment();
1629       return T;
1630     }
1631
1632     bool operator==(const expr_op_iterator &X) const {
1633       return getBase() == X.getBase();
1634     }
1635     bool operator!=(const expr_op_iterator &X) const {
1636       return getBase() != X.getBase();
1637     }
1638
1639   private:
1640     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
1641   };
1642
1643   /// \brief Visit the elements via ExprOperand wrappers.
1644   ///
1645   /// These range iterators visit elements through \a ExprOperand wrappers.
1646   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
1647   /// true.
1648   ///
1649   /// \pre \a isValid() gives \c true.
1650   /// @{
1651   expr_op_iterator expr_op_begin() const {
1652     return expr_op_iterator(elements_begin());
1653   }
1654   expr_op_iterator expr_op_end() const {
1655     return expr_op_iterator(elements_end());
1656   }
1657   /// @}
1658
1659   bool isValid() const;
1660
1661   static bool classof(const Metadata *MD) {
1662     return MD->getMetadataID() == MDExpressionKind;
1663   }
1664 };
1665
1666 class MDObjCProperty : public DebugNode {
1667   friend class LLVMContextImpl;
1668   friend class MDNode;
1669
1670   unsigned Line;
1671   unsigned Attributes;
1672
1673   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
1674                  unsigned Attributes, ArrayRef<Metadata *> Ops)
1675       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
1676                   Ops),
1677         Line(Line), Attributes(Attributes) {}
1678   ~MDObjCProperty() {}
1679
1680   static MDObjCProperty *
1681   getImpl(LLVMContext &Context, StringRef Name, Metadata *File, unsigned Line,
1682           StringRef GetterName, StringRef SetterName, unsigned Attributes,
1683           Metadata *Type, StorageType Storage, bool ShouldCreate = true) {
1684     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
1685                    getCanonicalMDString(Context, GetterName),
1686                    getCanonicalMDString(Context, SetterName), Attributes, Type,
1687                    Storage, ShouldCreate);
1688   }
1689   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
1690                                  Metadata *File, unsigned Line,
1691                                  MDString *GetterName, MDString *SetterName,
1692                                  unsigned Attributes, Metadata *Type,
1693                                  StorageType Storage, bool ShouldCreate = true);
1694
1695   TempMDObjCProperty cloneImpl() const {
1696     return getTemporary(getContext(), getName(), getFile(), getLine(),
1697                         getGetterName(), getSetterName(), getAttributes(),
1698                         getType());
1699   }
1700
1701 public:
1702   DEFINE_MDNODE_GET(MDObjCProperty,
1703                     (StringRef Name, Metadata *File, unsigned Line,
1704                      StringRef GetterName, StringRef SetterName,
1705                      unsigned Attributes, Metadata *Type),
1706                     (Name, File, Line, GetterName, SetterName, Attributes,
1707                      Type))
1708   DEFINE_MDNODE_GET(MDObjCProperty,
1709                     (MDString * Name, Metadata *File, unsigned Line,
1710                      MDString *GetterName, MDString *SetterName,
1711                      unsigned Attributes, Metadata *Type),
1712                     (Name, File, Line, GetterName, SetterName, Attributes,
1713                      Type))
1714
1715   TempMDObjCProperty clone() const { return cloneImpl(); }
1716
1717   unsigned getLine() const { return Line; }
1718   unsigned getAttributes() const { return Attributes; }
1719   StringRef getName() const { return getStringOperand(0); }
1720   Metadata *getFile() const { return getOperand(1); }
1721   StringRef getGetterName() const { return getStringOperand(2); }
1722   StringRef getSetterName() const { return getStringOperand(3); }
1723   Metadata *getType() const { return getOperand(4); }
1724
1725   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1726   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
1727   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
1728
1729   static bool classof(const Metadata *MD) {
1730     return MD->getMetadataID() == MDObjCPropertyKind;
1731   }
1732 };
1733
1734 class MDImportedEntity : public DebugNode {
1735   friend class LLVMContextImpl;
1736   friend class MDNode;
1737
1738   unsigned Line;
1739
1740   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
1741                    unsigned Line, ArrayRef<Metadata *> Ops)
1742       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
1743   ~MDImportedEntity() {}
1744
1745   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1746                                    Metadata *Scope, Metadata *Entity,
1747                                    unsigned Line, StringRef Name,
1748                                    StorageType Storage,
1749                                    bool ShouldCreate = true) {
1750     return getImpl(Context, Tag, Scope, Entity, Line,
1751                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
1752   }
1753   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1754                                    Metadata *Scope, Metadata *Entity,
1755                                    unsigned Line, MDString *Name,
1756                                    StorageType Storage,
1757                                    bool ShouldCreate = true);
1758
1759   TempMDImportedEntity cloneImpl() const {
1760     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
1761                         getLine(), getName());
1762   }
1763
1764 public:
1765   DEFINE_MDNODE_GET(MDImportedEntity,
1766                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1767                      unsigned Line, StringRef Name = ""),
1768                     (Tag, Scope, Entity, Line, Name))
1769   DEFINE_MDNODE_GET(MDImportedEntity,
1770                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1771                      unsigned Line, MDString *Name),
1772                     (Tag, Scope, Entity, Line, Name))
1773
1774   TempMDImportedEntity clone() const { return cloneImpl(); }
1775
1776   unsigned getLine() const { return Line; }
1777   Metadata *getScope() const { return getOperand(0); }
1778   Metadata *getEntity() const { return getOperand(1); }
1779   StringRef getName() const { return getStringOperand(2); }
1780
1781   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1782
1783   static bool classof(const Metadata *MD) {
1784     return MD->getMetadataID() == MDImportedEntityKind;
1785   }
1786 };
1787
1788 } // end namespace llvm
1789
1790 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
1791 #undef DEFINE_MDNODE_GET_UNPACK
1792 #undef DEFINE_MDNODE_GET
1793
1794 #endif