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