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