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