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