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