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