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