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