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