IR: Fix MDType fields from unsigned to uint64_t
[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   MDTuple *getFileNode() const { return cast<MDTuple>(getOperand(0)); }
758
759   StringRef getFilename() const {
760     if (auto *S = getRawFilename())
761       return S->getString();
762     return StringRef();
763   }
764   StringRef getDirectory() const {
765     if (auto *S = getRawDirectory())
766       return S->getString();
767     return StringRef();
768   }
769
770   MDString *getRawFilename() const {
771     return cast_or_null<MDString>(getFileNode()->getOperand(0));
772   }
773   MDString *getRawDirectory() const {
774     return cast_or_null<MDString>(getFileNode()->getOperand(1));
775   }
776
777   static bool classof(const Metadata *MD) {
778     return MD->getMetadataID() == MDFileKind;
779   }
780 };
781
782 /// \brief Compile unit.
783 class MDCompileUnit : public MDScope {
784   friend class LLVMContextImpl;
785   friend class MDNode;
786
787   unsigned SourceLanguage;
788   bool IsOptimized;
789   unsigned RuntimeVersion;
790   unsigned EmissionKind;
791
792   MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
793                 bool IsOptimized, unsigned RuntimeVersion,
794                 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
795       : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
796         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
797         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
798   ~MDCompileUnit() {}
799
800   static MDCompileUnit *
801   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
802           StringRef Producer, bool IsOptimized, StringRef Flags,
803           unsigned RuntimeVersion, StringRef SplitDebugFilename,
804           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
805           Metadata *Subprograms, Metadata *GlobalVariables,
806           Metadata *ImportedEntities, StorageType Storage,
807           bool ShouldCreate = true) {
808     return getImpl(Context, SourceLanguage, File,
809                    getCanonicalMDString(Context, Producer), IsOptimized,
810                    getCanonicalMDString(Context, Flags), RuntimeVersion,
811                    getCanonicalMDString(Context, SplitDebugFilename),
812                    EmissionKind, EnumTypes, RetainedTypes, Subprograms,
813                    GlobalVariables, ImportedEntities, Storage, ShouldCreate);
814   }
815   static MDCompileUnit *
816   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
817           MDString *Producer, bool IsOptimized, MDString *Flags,
818           unsigned RuntimeVersion, MDString *SplitDebugFilename,
819           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
820           Metadata *Subprograms, Metadata *GlobalVariables,
821           Metadata *ImportedEntities, StorageType Storage,
822           bool ShouldCreate = true);
823
824   TempMDCompileUnit cloneImpl() const {
825     return getTemporary(
826         getContext(), getSourceLanguage(), getFile(), getProducer(),
827         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
828         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
829         getGlobalVariables(), getImportedEntities());
830   }
831
832 public:
833   DEFINE_MDNODE_GET(MDCompileUnit,
834                     (unsigned SourceLanguage, Metadata *File,
835                      StringRef Producer, bool IsOptimized, StringRef Flags,
836                      unsigned RuntimeVersion, StringRef SplitDebugFilename,
837                      unsigned EmissionKind, Metadata *EnumTypes,
838                      Metadata *RetainedTypes, Metadata *Subprograms,
839                      Metadata *GlobalVariables, Metadata *ImportedEntities),
840                     (SourceLanguage, File, Producer, IsOptimized, Flags,
841                      RuntimeVersion, SplitDebugFilename, EmissionKind,
842                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
843                      ImportedEntities))
844   DEFINE_MDNODE_GET(MDCompileUnit,
845                     (unsigned SourceLanguage, Metadata *File,
846                      MDString *Producer, bool IsOptimized, MDString *Flags,
847                      unsigned RuntimeVersion, MDString *SplitDebugFilename,
848                      unsigned EmissionKind, Metadata *EnumTypes,
849                      Metadata *RetainedTypes, Metadata *Subprograms,
850                      Metadata *GlobalVariables, Metadata *ImportedEntities),
851                     (SourceLanguage, File, Producer, IsOptimized, Flags,
852                      RuntimeVersion, SplitDebugFilename, EmissionKind,
853                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
854                      ImportedEntities))
855
856   TempMDCompileUnit clone() const { return cloneImpl(); }
857
858   unsigned getSourceLanguage() const { return SourceLanguage; }
859   bool isOptimized() const { return IsOptimized; }
860   unsigned getRuntimeVersion() const { return RuntimeVersion; }
861   unsigned getEmissionKind() const { return EmissionKind; }
862   StringRef getProducer() const { return getStringOperand(1); }
863   StringRef getFlags() const { return getStringOperand(2); }
864   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
865   Metadata *getEnumTypes() const { return getOperand(4); }
866   Metadata *getRetainedTypes() const { return getOperand(5); }
867   Metadata *getSubprograms() const { return getOperand(6); }
868   Metadata *getGlobalVariables() const { return getOperand(7); }
869   Metadata *getImportedEntities() const { return getOperand(8); }
870
871   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
872   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
873   MDString *getRawSplitDebugFilename() const {
874     return getOperandAs<MDString>(3);
875   }
876
877   /// \brief Replace arrays.
878   ///
879   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
880   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
881   /// MDCompileUnit should be fairly rare.
882   /// @{
883   void replaceSubprograms(MDTuple *N) { replaceOperandWith(6, N); }
884   void replaceGlobalVariables(MDTuple *N) { replaceOperandWith(7, N); }
885   /// @}
886
887   static bool classof(const Metadata *MD) {
888     return MD->getMetadataID() == MDCompileUnitKind;
889   }
890 };
891
892 /// \brief Subprogram description.
893 ///
894 /// TODO: Remove DisplayName.  It's always equal to Name.
895 /// TODO: Split up flags.
896 class MDSubprogram : public MDScope {
897   friend class LLVMContextImpl;
898   friend class MDNode;
899
900   unsigned Line;
901   unsigned ScopeLine;
902   unsigned Virtuality;
903   unsigned VirtualIndex;
904   unsigned Flags;
905   bool IsLocalToUnit;
906   bool IsDefinition;
907   bool IsOptimized;
908
909   MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
910                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
911                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
912                bool IsOptimized, ArrayRef<Metadata *> Ops)
913       : MDScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram, Ops),
914         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
915         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
916         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
917   ~MDSubprogram() {}
918
919   static MDSubprogram *
920   getImpl(LLVMContext &Context, Metadata *Scope, StringRef Name,
921           StringRef LinkageName, Metadata *File, unsigned Line, Metadata *Type,
922           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
923           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
924           unsigned Flags, bool IsOptimized, Metadata *Function,
925           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
926           StorageType Storage, bool ShouldCreate = true) {
927     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
928                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
929                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
930                    Virtuality, VirtualIndex, Flags, IsOptimized, Function,
931                    TemplateParams, Declaration, Variables, Storage,
932                    ShouldCreate);
933   }
934   static MDSubprogram *
935   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
936           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
937           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
938           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
939           unsigned Flags, bool IsOptimized, Metadata *Function,
940           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
941           StorageType Storage, bool ShouldCreate = true);
942
943   TempMDSubprogram cloneImpl() const {
944     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
945                         getFile(), getLine(), getType(), isLocalToUnit(),
946                         isDefinition(), getScopeLine(), getContainingType(),
947                         getVirtuality(), getVirtualIndex(), getFlags(),
948                         isOptimized(), getFunction(), getTemplateParams(),
949                         getDeclaration(), getVariables());
950   }
951
952 public:
953   DEFINE_MDNODE_GET(
954       MDSubprogram,
955       (Metadata * Scope, StringRef Name, StringRef LinkageName, Metadata *File,
956        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
957        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
958        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
959        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
960        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
961       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
962        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
963        Function, TemplateParams, Declaration, Variables))
964   DEFINE_MDNODE_GET(
965       MDSubprogram,
966       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
967        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
968        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
969        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
970        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
971        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
972       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
973        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
974        Function, TemplateParams, Declaration, Variables))
975
976   TempMDSubprogram clone() const { return cloneImpl(); }
977
978 public:
979   unsigned getLine() const { return Line; }
980   unsigned getVirtuality() const { return Virtuality; }
981   unsigned getVirtualIndex() const { return VirtualIndex; }
982   unsigned getScopeLine() const { return ScopeLine; }
983   unsigned getFlags() const { return Flags; }
984   bool isLocalToUnit() const { return IsLocalToUnit; }
985   bool isDefinition() const { return IsDefinition; }
986   bool isOptimized() const { return IsOptimized; }
987
988   Metadata *getScope() const { return getOperand(1); }
989
990   StringRef getName() const { return getStringOperand(2); }
991   StringRef getDisplayName() const { return getStringOperand(3); }
992   StringRef getLinkageName() const { return getStringOperand(4); }
993
994   MDString *getRawName() const { return getOperandAs<MDString>(2); }
995   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
996
997   Metadata *getType() const { return getOperand(5); }
998   Metadata *getContainingType() const { return getOperand(6); }
999
1000   Metadata *getFunction() const { return getOperand(7); }
1001   Metadata *getTemplateParams() const { return getOperand(8); }
1002   Metadata *getDeclaration() const { return getOperand(9); }
1003   Metadata *getVariables() const { return getOperand(10); }
1004
1005   /// \brief Replace the function.
1006   ///
1007   /// If \a isUniqued() and not \a isResolved(), this could node will be
1008   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1009   /// that's a problem.
1010   /// @{
1011   void replaceFunction(Function *F);
1012   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1013   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1014   /// @}
1015
1016   static bool classof(const Metadata *MD) {
1017     return MD->getMetadataID() == MDSubprogramKind;
1018   }
1019 };
1020
1021 class MDLexicalBlockBase : public MDScope {
1022 protected:
1023   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1024                      ArrayRef<Metadata *> Ops)
1025       : MDScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1026   ~MDLexicalBlockBase() {}
1027
1028 public:
1029   Metadata *getScope() const { return getOperand(1); }
1030
1031   static bool classof(const Metadata *MD) {
1032     return MD->getMetadataID() == MDLexicalBlockKind ||
1033            MD->getMetadataID() == MDLexicalBlockFileKind;
1034   }
1035 };
1036
1037 class MDLexicalBlock : public MDLexicalBlockBase {
1038   friend class LLVMContextImpl;
1039   friend class MDNode;
1040
1041   unsigned Line;
1042   unsigned Column;
1043
1044   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1045                  unsigned Column, ArrayRef<Metadata *> Ops)
1046       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1047         Column(Column) {}
1048   ~MDLexicalBlock() {}
1049
1050   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1051                                  Metadata *File, unsigned Line, unsigned Column,
1052                                  StorageType Storage, bool ShouldCreate = true);
1053
1054   TempMDLexicalBlock cloneImpl() const {
1055     return getTemporary(getContext(), getScope(), getFile(), getLine(),
1056                         getColumn());
1057   }
1058
1059 public:
1060   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1061                                      unsigned Line, unsigned Column),
1062                     (Scope, File, Line, Column))
1063
1064   TempMDLexicalBlock clone() const { return cloneImpl(); }
1065
1066   unsigned getLine() const { return Line; }
1067   unsigned getColumn() const { return Column; }
1068
1069   static bool classof(const Metadata *MD) {
1070     return MD->getMetadataID() == MDLexicalBlockKind;
1071   }
1072 };
1073
1074 class MDLexicalBlockFile : public MDLexicalBlockBase {
1075   friend class LLVMContextImpl;
1076   friend class MDNode;
1077
1078   unsigned Discriminator;
1079
1080   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1081                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
1082       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1083         Discriminator(Discriminator) {}
1084   ~MDLexicalBlockFile() {}
1085
1086   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1087                                      Metadata *File, unsigned Discriminator,
1088                                      StorageType Storage,
1089                                      bool ShouldCreate = true);
1090
1091   TempMDLexicalBlockFile cloneImpl() const {
1092     return getTemporary(getContext(), getScope(), getFile(),
1093                         getDiscriminator());
1094   }
1095
1096 public:
1097   DEFINE_MDNODE_GET(MDLexicalBlockFile,
1098                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
1099                     (Scope, File, Discriminator))
1100
1101   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1102
1103   unsigned getDiscriminator() const { return Discriminator; }
1104
1105   static bool classof(const Metadata *MD) {
1106     return MD->getMetadataID() == MDLexicalBlockFileKind;
1107   }
1108 };
1109
1110 class MDNamespace : public MDScope {
1111   friend class LLVMContextImpl;
1112   friend class MDNode;
1113
1114   unsigned Line;
1115
1116   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1117               ArrayRef<Metadata *> Ops)
1118       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1119                 Ops),
1120         Line(Line) {}
1121   ~MDNamespace() {}
1122
1123   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1124                               Metadata *File, StringRef Name, unsigned Line,
1125                               StorageType Storage, bool ShouldCreate = true) {
1126     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1127                    Line, Storage, ShouldCreate);
1128   }
1129   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1130                               Metadata *File, MDString *Name, unsigned Line,
1131                               StorageType Storage, bool ShouldCreate = true);
1132
1133   TempMDNamespace cloneImpl() const {
1134     return getTemporary(getContext(), getScope(), getFile(), getName(),
1135                         getLine());
1136   }
1137
1138 public:
1139   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1140                                   StringRef Name, unsigned Line),
1141                     (Scope, File, Name, Line))
1142   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1143                                   MDString *Name, unsigned Line),
1144                     (Scope, File, Name, Line))
1145
1146   TempMDNamespace clone() const { return cloneImpl(); }
1147
1148   unsigned getLine() const { return Line; }
1149   Metadata *getScope() const { return getOperand(1); }
1150   StringRef getName() const { return getStringOperand(2); }
1151
1152   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1153
1154   static bool classof(const Metadata *MD) {
1155     return MD->getMetadataID() == MDNamespaceKind;
1156   }
1157 };
1158
1159 /// \brief Base class for template parameters.
1160 class MDTemplateParameter : public DebugNode {
1161 protected:
1162   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1163                       unsigned Tag, ArrayRef<Metadata *> Ops)
1164       : DebugNode(Context, ID, Storage, Tag, Ops) {}
1165   ~MDTemplateParameter() {}
1166
1167 public:
1168   StringRef getName() const { return getStringOperand(0); }
1169   Metadata *getType() const { return getOperand(1); }
1170
1171   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1172
1173   static bool classof(const Metadata *MD) {
1174     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1175            MD->getMetadataID() == MDTemplateValueParameterKind;
1176   }
1177 };
1178
1179 class MDTemplateTypeParameter : public MDTemplateParameter {
1180   friend class LLVMContextImpl;
1181   friend class MDNode;
1182
1183   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1184                           ArrayRef<Metadata *> Ops)
1185       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1186                             dwarf::DW_TAG_template_type_parameter, Ops) {}
1187   ~MDTemplateTypeParameter() {}
1188
1189   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1190                                           Metadata *Type, StorageType Storage,
1191                                           bool ShouldCreate = true) {
1192     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1193                    ShouldCreate);
1194   }
1195   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1196                                           Metadata *Type, StorageType Storage,
1197                                           bool ShouldCreate = true);
1198
1199   TempMDTemplateTypeParameter cloneImpl() const {
1200     return getTemporary(getContext(), getName(), getType());
1201   }
1202
1203 public:
1204   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, Metadata *Type),
1205                     (Name, Type))
1206   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1207                     (Name, Type))
1208
1209   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1210
1211   static bool classof(const Metadata *MD) {
1212     return MD->getMetadataID() == MDTemplateTypeParameterKind;
1213   }
1214 };
1215
1216 class MDTemplateValueParameter : public MDTemplateParameter {
1217   friend class LLVMContextImpl;
1218   friend class MDNode;
1219
1220   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1221                            unsigned Tag, ArrayRef<Metadata *> Ops)
1222       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1223                             Ops) {}
1224   ~MDTemplateValueParameter() {}
1225
1226   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1227                                            StringRef Name, Metadata *Type,
1228                                            Metadata *Value, StorageType Storage,
1229                                            bool ShouldCreate = true) {
1230     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1231                    Value, Storage, ShouldCreate);
1232   }
1233   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1234                                            MDString *Name, Metadata *Type,
1235                                            Metadata *Value, StorageType Storage,
1236                                            bool ShouldCreate = true);
1237
1238   TempMDTemplateValueParameter cloneImpl() const {
1239     return getTemporary(getContext(), getTag(), getName(), getType(),
1240                         getValue());
1241   }
1242
1243 public:
1244   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1245                                                Metadata *Type, Metadata *Value),
1246                     (Tag, Name, Type, Value))
1247   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1248                                                Metadata *Type, Metadata *Value),
1249                     (Tag, Name, Type, Value))
1250
1251   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1252
1253   Metadata *getValue() const { return getOperand(2); }
1254
1255   static bool classof(const Metadata *MD) {
1256     return MD->getMetadataID() == MDTemplateValueParameterKind;
1257   }
1258 };
1259
1260 /// \brief Base class for variables.
1261 ///
1262 /// TODO: Hardcode to DW_TAG_variable.
1263 class MDVariable : public DebugNode {
1264   unsigned Line;
1265
1266 protected:
1267   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1268              unsigned Line, ArrayRef<Metadata *> Ops)
1269       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1270   ~MDVariable() {}
1271
1272 public:
1273   unsigned getLine() const { return Line; }
1274   Metadata *getScope() const { return getOperand(0); }
1275   StringRef getName() const { return getStringOperand(1); }
1276   Metadata *getFile() const { return getOperand(2); }
1277   Metadata *getType() const { return getOperand(3); }
1278
1279   MDString *getRawName() const { return getOperandAs<MDString>(1); }
1280
1281   static bool classof(const Metadata *MD) {
1282     return MD->getMetadataID() == MDLocalVariableKind ||
1283            MD->getMetadataID() == MDGlobalVariableKind;
1284   }
1285 };
1286
1287 /// \brief Global variables.
1288 ///
1289 /// TODO: Remove DisplayName.  It's always equal to Name.
1290 class MDGlobalVariable : public MDVariable {
1291   friend class LLVMContextImpl;
1292   friend class MDNode;
1293
1294   bool IsLocalToUnit;
1295   bool IsDefinition;
1296
1297   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1298                    bool IsLocalToUnit, bool IsDefinition,
1299                    ArrayRef<Metadata *> Ops)
1300       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1301                    Line, Ops),
1302         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1303   ~MDGlobalVariable() {}
1304
1305   static MDGlobalVariable *
1306   getImpl(LLVMContext &Context, Metadata *Scope, StringRef Name,
1307           StringRef LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1308           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1309           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1310           bool ShouldCreate = true) {
1311     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1312                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
1313                    IsLocalToUnit, IsDefinition, Variable,
1314                    StaticDataMemberDeclaration, Storage, ShouldCreate);
1315   }
1316   static MDGlobalVariable *
1317   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1318           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1319           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1320           Metadata *StaticDataMemberDeclaration, StorageType Storage,
1321           bool ShouldCreate = true);
1322
1323   TempMDGlobalVariable cloneImpl() const {
1324     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1325                         getFile(), getLine(), getType(), isLocalToUnit(),
1326                         isDefinition(), getVariable(),
1327                         getStaticDataMemberDeclaration());
1328   }
1329
1330 public:
1331   DEFINE_MDNODE_GET(MDGlobalVariable,
1332                     (Metadata * Scope, StringRef Name, StringRef LinkageName,
1333                      Metadata *File, unsigned Line, Metadata *Type,
1334                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1335                      Metadata *StaticDataMemberDeclaration),
1336                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1337                      IsDefinition, Variable, StaticDataMemberDeclaration))
1338   DEFINE_MDNODE_GET(MDGlobalVariable,
1339                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
1340                      Metadata *File, unsigned Line, Metadata *Type,
1341                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1342                      Metadata *StaticDataMemberDeclaration),
1343                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1344                      IsDefinition, Variable, StaticDataMemberDeclaration))
1345
1346   TempMDGlobalVariable clone() const { return cloneImpl(); }
1347
1348   bool isLocalToUnit() const { return IsLocalToUnit; }
1349   bool isDefinition() const { return IsDefinition; }
1350   StringRef getDisplayName() const { return getStringOperand(4); }
1351   StringRef getLinkageName() const { return getStringOperand(5); }
1352   Metadata *getVariable() const { return getOperand(6); }
1353   Metadata *getStaticDataMemberDeclaration() const { return getOperand(7); }
1354
1355   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1356
1357   static bool classof(const Metadata *MD) {
1358     return MD->getMetadataID() == MDGlobalVariableKind;
1359   }
1360 };
1361
1362 /// \brief Local variable.
1363 ///
1364 /// TODO: Split between arguments and otherwise.
1365 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1366 /// TODO: Split up flags.
1367 class MDLocalVariable : public MDVariable {
1368   friend class LLVMContextImpl;
1369   friend class MDNode;
1370
1371   unsigned Arg;
1372   unsigned Flags;
1373
1374   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1375                   unsigned Line, unsigned Arg, unsigned Flags,
1376                   ArrayRef<Metadata *> Ops)
1377       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1378         Flags(Flags) {}
1379   ~MDLocalVariable() {}
1380
1381   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1382                                   Metadata *Scope, StringRef Name,
1383                                   Metadata *File, unsigned Line, Metadata *Type,
1384                                   unsigned Arg, unsigned Flags,
1385                                   Metadata *InlinedAt, StorageType Storage,
1386                                   bool ShouldCreate = true) {
1387     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1388                    File, Line, Type, Arg, Flags, InlinedAt, Storage,
1389                    ShouldCreate);
1390   }
1391   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1392                                   Metadata *Scope, MDString *Name,
1393                                   Metadata *File, unsigned Line, Metadata *Type,
1394                                   unsigned Arg, unsigned Flags,
1395                                   Metadata *InlinedAt, StorageType Storage,
1396                                   bool ShouldCreate = true);
1397
1398   TempMDLocalVariable cloneImpl() const {
1399     return getTemporary(getContext(), getTag(), getScope(), getName(),
1400                         getFile(), getLine(), getType(), getArg(), getFlags(),
1401                         getInlinedAt());
1402   }
1403
1404 public:
1405   DEFINE_MDNODE_GET(MDLocalVariable,
1406                     (unsigned Tag, Metadata *Scope, StringRef Name,
1407                      Metadata *File, unsigned Line, Metadata *Type,
1408                      unsigned Arg, unsigned Flags,
1409                      Metadata *InlinedAt = nullptr),
1410                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1411   DEFINE_MDNODE_GET(MDLocalVariable,
1412                     (unsigned Tag, Metadata *Scope, MDString *Name,
1413                      Metadata *File, unsigned Line, Metadata *Type,
1414                      unsigned Arg, unsigned Flags,
1415                      Metadata *InlinedAt = nullptr),
1416                     (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1417
1418   TempMDLocalVariable clone() const { return cloneImpl(); }
1419
1420   unsigned getArg() const { return Arg; }
1421   unsigned getFlags() const { return Flags; }
1422   Metadata *getInlinedAt() const { return getOperand(4); }
1423
1424   static bool classof(const Metadata *MD) {
1425     return MD->getMetadataID() == MDLocalVariableKind;
1426   }
1427 };
1428
1429 /// \brief DWARF expression.
1430 ///
1431 /// TODO: Co-allocate the expression elements.
1432 /// TODO: Drop fake DW_TAG_expression and separate from DebugNode.
1433 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1434 /// storage types.
1435 class MDExpression : public DebugNode {
1436   friend class LLVMContextImpl;
1437   friend class MDNode;
1438
1439   std::vector<uint64_t> Elements;
1440
1441   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1442       : DebugNode(C, MDExpressionKind, Storage, dwarf::DW_TAG_expression, None),
1443         Elements(Elements.begin(), Elements.end()) {}
1444   ~MDExpression() {}
1445
1446   static MDExpression *getImpl(LLVMContext &Context,
1447                                ArrayRef<uint64_t> Elements, StorageType Storage,
1448                                bool ShouldCreate = true);
1449
1450   TempMDExpression cloneImpl() const {
1451     return getTemporary(getContext(), getElements());
1452   }
1453
1454 public:
1455   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1456
1457   TempMDExpression clone() const { return cloneImpl(); }
1458
1459   ArrayRef<uint64_t> getElements() const { return Elements; }
1460
1461   unsigned getNumElements() const { return Elements.size(); }
1462   uint64_t getElement(unsigned I) const {
1463     assert(I < Elements.size() && "Index out of range");
1464     return Elements[I];
1465   }
1466
1467   typedef ArrayRef<uint64_t>::iterator element_iterator;
1468   element_iterator elements_begin() const { return getElements().begin(); }
1469   element_iterator elements_end() const { return getElements().end(); }
1470
1471   /// \brief A lightweight wrapper around an expression operand.
1472   ///
1473   /// TODO: Store arguments directly and change \a MDExpression to store a
1474   /// range of these.
1475   class ExprOperand {
1476     const uint64_t *Op;
1477
1478   public:
1479     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1480
1481     const uint64_t *get() const { return Op; }
1482
1483     /// \brief Get the operand code.
1484     uint64_t getOp() const { return *Op; }
1485
1486     /// \brief Get an argument to the operand.
1487     ///
1488     /// Never returns the operand itself.
1489     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1490
1491     unsigned getNumArgs() const { return getSize() - 1; }
1492
1493     /// \brief Return the size of the operand.
1494     ///
1495     /// Return the number of elements in the operand (1 + args).
1496     unsigned getSize() const;
1497   };
1498
1499   /// \brief An iterator for expression operands.
1500   class expr_op_iterator
1501       : public std::iterator<std::input_iterator_tag, ExprOperand> {
1502     ExprOperand Op;
1503
1504   public:
1505     explicit expr_op_iterator(element_iterator I) : Op(I) {}
1506
1507     element_iterator getBase() const { return Op.get(); }
1508     const ExprOperand &operator*() const { return Op; }
1509     const ExprOperand *operator->() const { return &Op; }
1510
1511     expr_op_iterator &operator++() {
1512       increment();
1513       return *this;
1514     }
1515     expr_op_iterator operator++(int) {
1516       expr_op_iterator T(*this);
1517       increment();
1518       return T;
1519     }
1520
1521     bool operator==(const expr_op_iterator &X) const {
1522       return getBase() == X.getBase();
1523     }
1524     bool operator!=(const expr_op_iterator &X) const {
1525       return getBase() != X.getBase();
1526     }
1527
1528   private:
1529     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
1530   };
1531
1532   /// \brief Visit the elements via ExprOperand wrappers.
1533   ///
1534   /// These range iterators visit elements through \a ExprOperand wrappers.
1535   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
1536   /// true.
1537   ///
1538   /// \pre \a isValid() gives \c true.
1539   /// @{
1540   expr_op_iterator expr_op_begin() const {
1541     return expr_op_iterator(elements_begin());
1542   }
1543   expr_op_iterator expr_op_end() const {
1544     return expr_op_iterator(elements_end());
1545   }
1546   /// @}
1547
1548   bool isValid() const;
1549
1550   static bool classof(const Metadata *MD) {
1551     return MD->getMetadataID() == MDExpressionKind;
1552   }
1553 };
1554
1555 class MDObjCProperty : public DebugNode {
1556   friend class LLVMContextImpl;
1557   friend class MDNode;
1558
1559   unsigned Line;
1560   unsigned Attributes;
1561
1562   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
1563                  unsigned Attributes, ArrayRef<Metadata *> Ops)
1564       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
1565                   Ops),
1566         Line(Line), Attributes(Attributes) {}
1567   ~MDObjCProperty() {}
1568
1569   static MDObjCProperty *
1570   getImpl(LLVMContext &Context, StringRef Name, Metadata *File, unsigned Line,
1571           StringRef GetterName, StringRef SetterName, unsigned Attributes,
1572           Metadata *Type, StorageType Storage, bool ShouldCreate = true) {
1573     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
1574                    getCanonicalMDString(Context, GetterName),
1575                    getCanonicalMDString(Context, SetterName), Attributes, Type,
1576                    Storage, ShouldCreate);
1577   }
1578   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
1579                                  Metadata *File, unsigned Line,
1580                                  MDString *GetterName, MDString *SetterName,
1581                                  unsigned Attributes, Metadata *Type,
1582                                  StorageType Storage, bool ShouldCreate = true);
1583
1584   TempMDObjCProperty cloneImpl() const {
1585     return getTemporary(getContext(), getName(), getFile(), getLine(),
1586                         getGetterName(), getSetterName(), getAttributes(),
1587                         getType());
1588   }
1589
1590 public:
1591   DEFINE_MDNODE_GET(MDObjCProperty,
1592                     (StringRef Name, Metadata *File, unsigned Line,
1593                      StringRef GetterName, StringRef SetterName,
1594                      unsigned Attributes, Metadata *Type),
1595                     (Name, File, Line, GetterName, SetterName, Attributes,
1596                      Type))
1597   DEFINE_MDNODE_GET(MDObjCProperty,
1598                     (MDString * Name, Metadata *File, unsigned Line,
1599                      MDString *GetterName, MDString *SetterName,
1600                      unsigned Attributes, Metadata *Type),
1601                     (Name, File, Line, GetterName, SetterName, Attributes,
1602                      Type))
1603
1604   TempMDObjCProperty clone() const { return cloneImpl(); }
1605
1606   unsigned getLine() const { return Line; }
1607   unsigned getAttributes() const { return Attributes; }
1608   StringRef getName() const { return getStringOperand(0); }
1609   Metadata *getFile() const { return getOperand(1); }
1610   StringRef getGetterName() const { return getStringOperand(2); }
1611   StringRef getSetterName() const { return getStringOperand(3); }
1612   Metadata *getType() const { return getOperand(4); }
1613
1614   MDString *getRawName() const { return getOperandAs<MDString>(0); }
1615   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
1616   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
1617
1618   static bool classof(const Metadata *MD) {
1619     return MD->getMetadataID() == MDObjCPropertyKind;
1620   }
1621 };
1622
1623 class MDImportedEntity : public DebugNode {
1624   friend class LLVMContextImpl;
1625   friend class MDNode;
1626
1627   unsigned Line;
1628
1629   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
1630                    unsigned Line, ArrayRef<Metadata *> Ops)
1631       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
1632   ~MDImportedEntity() {}
1633
1634   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1635                                    Metadata *Scope, Metadata *Entity,
1636                                    unsigned Line, StringRef Name,
1637                                    StorageType Storage,
1638                                    bool ShouldCreate = true) {
1639     return getImpl(Context, Tag, Scope, Entity, Line,
1640                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
1641   }
1642   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
1643                                    Metadata *Scope, Metadata *Entity,
1644                                    unsigned Line, MDString *Name,
1645                                    StorageType Storage,
1646                                    bool ShouldCreate = true);
1647
1648   TempMDImportedEntity cloneImpl() const {
1649     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
1650                         getLine(), getName());
1651   }
1652
1653 public:
1654   DEFINE_MDNODE_GET(MDImportedEntity,
1655                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1656                      unsigned Line, StringRef Name = ""),
1657                     (Tag, Scope, Entity, Line, Name))
1658   DEFINE_MDNODE_GET(MDImportedEntity,
1659                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
1660                      unsigned Line, MDString *Name),
1661                     (Tag, Scope, Entity, Line, Name))
1662
1663   TempMDImportedEntity clone() const { return cloneImpl(); }
1664
1665   unsigned getLine() const { return Line; }
1666   Metadata *getScope() const { return getOperand(0); }
1667   Metadata *getEntity() const { return getOperand(1); }
1668   StringRef getName() const { return getStringOperand(2); }
1669
1670   MDString *getRawName() const { return getOperandAs<MDString>(2); }
1671
1672   static bool classof(const Metadata *MD) {
1673     return MD->getMetadataID() == MDImportedEntityKind;
1674   }
1675 };
1676
1677 } // end namespace llvm
1678
1679 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
1680 #undef DEFINE_MDNODE_GET_UNPACK
1681 #undef DEFINE_MDNODE_GET
1682
1683 #endif