1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Declarations for metadata specific to debug info.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
15 #define LLVM_IR_DEBUGINFOMETADATA_H
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Dwarf.h"
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); \
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); \
32 static CLASS *getDistinct(LLVMContext &Context, \
33 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
34 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
36 static Temp##CLASS getTemporary(LLVMContext &Context, \
37 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
39 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
44 /// \brief Pointer union between a subclass of DINode and MDString.
46 /// \a DICompositeType can be referenced via an \a MDString unique identifier.
47 /// This class allows some type safety in the face of that, requiring either a
48 /// node of a particular type or an \a MDString.
49 template <class T> class TypedDINodeRef {
50 const Metadata *MD = nullptr;
53 TypedDINodeRef() = default;
54 TypedDINodeRef(std::nullptr_t) {}
56 /// \brief Construct from a raw pointer.
57 explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
58 assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
63 const TypedDINodeRef<U> &X,
64 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
68 operator Metadata *() const { return const_cast<Metadata *>(MD); }
70 bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; };
71 bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; };
73 /// \brief Create a reference.
75 /// Get a reference to \c N, using an \a MDString reference if available.
76 static TypedDINodeRef get(const T *N);
78 template <class MapTy> T *resolve(const MapTy &Map) const {
82 if (auto *Typed = dyn_cast<T>(MD))
83 return const_cast<T *>(Typed);
85 auto *S = cast<MDString>(MD);
87 assert(I != Map.end() && "Missing identifier in type map");
88 return cast<T>(I->second);
92 typedef TypedDINodeRef<DINode> DINodeRef;
93 typedef TypedDINodeRef<DIScope> DIScopeRef;
94 typedef TypedDINodeRef<DIType> DITypeRef;
96 class DITypeRefArray {
97 const MDTuple *N = nullptr;
100 DITypeRefArray(const MDTuple *N) : N(N) {}
102 explicit operator bool() const { return get(); }
103 explicit operator MDTuple *() const { return get(); }
105 MDTuple *get() const { return const_cast<MDTuple *>(N); }
106 MDTuple *operator->() const { return get(); }
107 MDTuple &operator*() const { return *get(); }
109 // FIXME: Fix callers and remove condition on N.
110 unsigned size() const { return N ? N->getNumOperands() : 0u; }
111 DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
113 class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
114 std::ptrdiff_t, void, DITypeRef> {
115 MDNode::op_iterator I = nullptr;
118 iterator() = default;
119 explicit iterator(MDNode::op_iterator I) : I(I) {}
120 DITypeRef operator*() const { return DITypeRef(*I); }
121 iterator &operator++() {
125 iterator operator++(int) {
126 iterator Temp(*this);
130 bool operator==(const iterator &X) const { return I == X.I; }
131 bool operator!=(const iterator &X) const { return I != X.I; }
134 // FIXME: Fix callers and remove condition on N.
135 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
136 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
139 /// \brief Tagged DWARF-like metadata node.
141 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
142 /// defined in llvm/Support/Dwarf.h). Called \a DINode because it's
143 /// potentially used for non-DWARF output.
144 class DINode : public MDNode {
145 friend class LLVMContextImpl;
149 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
150 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
151 : MDNode(C, ID, Storage, Ops1, Ops2) {
152 assert(Tag < 1u << 16);
153 SubclassData16 = Tag;
157 template <class Ty> Ty *getOperandAs(unsigned I) const {
158 return cast_or_null<Ty>(getOperand(I));
161 StringRef getStringOperand(unsigned I) const {
162 if (auto *S = getOperandAs<MDString>(I))
163 return S->getString();
167 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
170 return MDString::get(Context, S);
174 unsigned getTag() const { return SubclassData16; }
176 /// \brief Debug info flags.
178 /// The three accessibility flags are mutually exclusive and rolled together
179 /// in the first two bits.
181 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
182 #include "llvm/IR/DebugInfoFlags.def"
183 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
186 static unsigned getFlag(StringRef Flag);
187 static const char *getFlagString(unsigned Flag);
189 /// \brief Split up a flags bitfield.
191 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
192 /// any remaining (unrecognized) bits.
193 static unsigned splitFlags(unsigned Flags,
194 SmallVectorImpl<unsigned> &SplitFlags);
196 DINodeRef getRef() const { return DINodeRef::get(this); }
198 static bool classof(const Metadata *MD) {
199 switch (MD->getMetadataID()) {
202 case GenericDINodeKind:
204 case DIEnumeratorKind:
205 case DIBasicTypeKind:
206 case DIDerivedTypeKind:
207 case DICompositeTypeKind:
208 case DISubroutineTypeKind:
210 case DICompileUnitKind:
211 case DISubprogramKind:
212 case DILexicalBlockKind:
213 case DILexicalBlockFileKind:
214 case DINamespaceKind:
215 case DITemplateTypeParameterKind:
216 case DITemplateValueParameterKind:
217 case DIGlobalVariableKind:
218 case DILocalVariableKind:
219 case DIObjCPropertyKind:
220 case DIImportedEntityKind:
227 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
228 typedef Metadata *SimpleType;
229 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
235 struct simplify_type<TypedDINodeRef<T>>
236 : simplify_type<const TypedDINodeRef<T>> {};
238 /// \brief Generic tagged DWARF-like metadata node.
240 /// An un-specialized DWARF-like metadata node. The first operand is a
241 /// (possibly empty) null-separated \a MDString header that contains arbitrary
242 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
243 /// to other metadata.
244 class GenericDINode : public DINode {
245 friend class LLVMContextImpl;
248 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
249 unsigned Tag, ArrayRef<Metadata *> Ops1,
250 ArrayRef<Metadata *> Ops2)
251 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
254 ~GenericDINode() { dropAllReferences(); }
256 void setHash(unsigned Hash) { SubclassData32 = Hash; }
257 void recalculateHash();
259 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
260 StringRef Header, ArrayRef<Metadata *> DwarfOps,
261 StorageType Storage, bool ShouldCreate = true) {
262 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
263 DwarfOps, Storage, ShouldCreate);
266 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
267 MDString *Header, ArrayRef<Metadata *> DwarfOps,
268 StorageType Storage, bool ShouldCreate = true);
270 TempGenericDINode cloneImpl() const {
272 getContext(), getTag(), getHeader(),
273 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
277 unsigned getHash() const { return SubclassData32; }
279 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
280 ArrayRef<Metadata *> DwarfOps),
281 (Tag, Header, DwarfOps))
282 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
283 ArrayRef<Metadata *> DwarfOps),
284 (Tag, Header, DwarfOps))
286 /// \brief Return a (temporary) clone of this.
287 TempGenericDINode clone() const { return cloneImpl(); }
289 unsigned getTag() const { return SubclassData16; }
290 StringRef getHeader() const { return getStringOperand(0); }
292 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
293 op_iterator dwarf_op_end() const { return op_end(); }
294 op_range dwarf_operands() const {
295 return op_range(dwarf_op_begin(), dwarf_op_end());
298 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
299 const MDOperand &getDwarfOperand(unsigned I) const {
300 return getOperand(I + 1);
302 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
303 replaceOperandWith(I + 1, New);
306 static bool classof(const Metadata *MD) {
307 return MD->getMetadataID() == GenericDINodeKind;
311 /// \brief Array subrange.
313 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
315 class DISubrange : public DINode {
316 friend class LLVMContextImpl;
322 DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
324 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
325 Count(Count), LowerBound(LowerBound) {}
326 ~DISubrange() = default;
328 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
329 int64_t LowerBound, StorageType Storage,
330 bool ShouldCreate = true);
332 TempDISubrange cloneImpl() const {
333 return getTemporary(getContext(), getCount(), getLowerBound());
337 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
340 TempDISubrange clone() const { return cloneImpl(); }
342 int64_t getLowerBound() const { return LowerBound; }
343 int64_t getCount() const { return Count; }
345 static bool classof(const Metadata *MD) {
346 return MD->getMetadataID() == DISubrangeKind;
350 /// \brief Enumeration value.
352 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
353 /// longer creates a type cycle.
354 class DIEnumerator : public DINode {
355 friend class LLVMContextImpl;
360 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
361 ArrayRef<Metadata *> Ops)
362 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
364 ~DIEnumerator() = default;
366 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
367 StringRef Name, StorageType Storage,
368 bool ShouldCreate = true) {
369 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
372 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
373 MDString *Name, StorageType Storage,
374 bool ShouldCreate = true);
376 TempDIEnumerator cloneImpl() const {
377 return getTemporary(getContext(), getValue(), getName());
381 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
383 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
386 TempDIEnumerator clone() const { return cloneImpl(); }
388 int64_t getValue() const { return Value; }
389 StringRef getName() const { return getStringOperand(0); }
391 MDString *getRawName() const { return getOperandAs<MDString>(0); }
393 static bool classof(const Metadata *MD) {
394 return MD->getMetadataID() == DIEnumeratorKind;
398 /// \brief Base class for scope-like contexts.
400 /// Base class for lexical scopes and types (which are also declaration
403 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
404 class DIScope : public DINode {
406 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
407 ArrayRef<Metadata *> Ops)
408 : DINode(C, ID, Storage, Tag, Ops) {}
409 ~DIScope() = default;
412 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
414 inline StringRef getFilename() const;
415 inline StringRef getDirectory() const;
417 StringRef getName() const;
418 DIScopeRef getScope() const;
420 /// \brief Return the raw underlying file.
422 /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
423 /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
424 /// this. Otherwise, return the first operand, which is where all other
425 /// subclasses store their file pointer.
426 Metadata *getRawFile() const {
427 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
428 : static_cast<Metadata *>(getOperand(0));
431 DIScopeRef getRef() const { return DIScopeRef::get(this); }
433 static bool classof(const Metadata *MD) {
434 switch (MD->getMetadataID()) {
437 case DIBasicTypeKind:
438 case DIDerivedTypeKind:
439 case DICompositeTypeKind:
440 case DISubroutineTypeKind:
442 case DICompileUnitKind:
443 case DISubprogramKind:
444 case DILexicalBlockKind:
445 case DILexicalBlockFileKind:
446 case DINamespaceKind:
455 /// TODO: Merge with directory/file node (including users).
456 /// TODO: Canonicalize paths on creation.
457 class DIFile : public DIScope {
458 friend class LLVMContextImpl;
461 DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
462 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
465 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
466 StringRef Directory, StorageType Storage,
467 bool ShouldCreate = true) {
468 return getImpl(Context, getCanonicalMDString(Context, Filename),
469 getCanonicalMDString(Context, Directory), Storage,
472 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
473 MDString *Directory, StorageType Storage,
474 bool ShouldCreate = true);
476 TempDIFile cloneImpl() const {
477 return getTemporary(getContext(), getFilename(), getDirectory());
481 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
482 (Filename, Directory))
483 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
484 (Filename, Directory))
486 TempDIFile clone() const { return cloneImpl(); }
488 StringRef getFilename() const { return getStringOperand(0); }
489 StringRef getDirectory() const { return getStringOperand(1); }
491 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
492 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
494 static bool classof(const Metadata *MD) {
495 return MD->getMetadataID() == DIFileKind;
499 StringRef DIScope::getFilename() const {
500 if (auto *F = getFile())
501 return F->getFilename();
505 StringRef DIScope::getDirectory() const {
506 if (auto *F = getFile())
507 return F->getDirectory();
511 /// \brief Base class for types.
513 /// TODO: Remove the hardcoded name and context, since many types don't use
515 /// TODO: Split up flags.
516 class DIType : public DIScope {
520 uint64_t AlignInBits;
521 uint64_t OffsetInBits;
524 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
525 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
526 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
527 : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
528 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
529 OffsetInBits(OffsetInBits) {}
533 TempDIType clone() const {
534 return TempDIType(cast<DIType>(MDNode::clone().release()));
537 unsigned getLine() const { return Line; }
538 uint64_t getSizeInBits() const { return SizeInBits; }
539 uint64_t getAlignInBits() const { return AlignInBits; }
540 uint64_t getOffsetInBits() const { return OffsetInBits; }
541 unsigned getFlags() const { return Flags; }
543 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
544 StringRef getName() const { return getStringOperand(2); }
547 Metadata *getRawScope() const { return getOperand(1); }
548 MDString *getRawName() const { return getOperandAs<MDString>(2); }
550 void setFlags(unsigned NewFlags) {
551 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
555 bool isPrivate() const {
556 return (getFlags() & FlagAccessibility) == FlagPrivate;
558 bool isProtected() const {
559 return (getFlags() & FlagAccessibility) == FlagProtected;
561 bool isPublic() const {
562 return (getFlags() & FlagAccessibility) == FlagPublic;
564 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
565 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
566 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
567 bool isVirtual() const { return getFlags() & FlagVirtual; }
568 bool isArtificial() const { return getFlags() & FlagArtificial; }
569 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
570 bool isObjcClassComplete() const {
571 return getFlags() & FlagObjcClassComplete;
573 bool isVector() const { return getFlags() & FlagVector; }
574 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
575 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
576 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
578 DITypeRef getRef() const { return DITypeRef::get(this); }
580 static bool classof(const Metadata *MD) {
581 switch (MD->getMetadataID()) {
584 case DIBasicTypeKind:
585 case DIDerivedTypeKind:
586 case DICompositeTypeKind:
587 case DISubroutineTypeKind:
593 /// \brief Basic type, like 'int' or 'float'.
595 /// TODO: Split out DW_TAG_unspecified_type.
596 /// TODO: Drop unused accessors.
597 class DIBasicType : public DIType {
598 friend class LLVMContextImpl;
603 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
604 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
605 ArrayRef<Metadata *> Ops)
606 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
608 Encoding(Encoding) {}
609 ~DIBasicType() = default;
611 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
612 StringRef Name, uint64_t SizeInBits,
613 uint64_t AlignInBits, unsigned Encoding,
614 StorageType Storage, bool ShouldCreate = true) {
615 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
616 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
618 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
619 MDString *Name, uint64_t SizeInBits,
620 uint64_t AlignInBits, unsigned Encoding,
621 StorageType Storage, bool ShouldCreate = true);
623 TempDIBasicType cloneImpl() const {
624 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
625 getAlignInBits(), getEncoding());
629 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
630 (Tag, Name, 0, 0, 0))
631 DEFINE_MDNODE_GET(DIBasicType,
632 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
633 uint64_t AlignInBits, unsigned Encoding),
634 (Tag, Name, SizeInBits, AlignInBits, Encoding))
635 DEFINE_MDNODE_GET(DIBasicType,
636 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
637 uint64_t AlignInBits, unsigned Encoding),
638 (Tag, Name, SizeInBits, AlignInBits, Encoding))
640 TempDIBasicType clone() const { return cloneImpl(); }
642 unsigned getEncoding() const { return Encoding; }
644 static bool classof(const Metadata *MD) {
645 return MD->getMetadataID() == DIBasicTypeKind;
649 /// \brief Base class for DIDerivedType and DICompositeType.
651 /// TODO: Delete; they're not really related.
652 class DIDerivedTypeBase : public DIType {
654 DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
655 unsigned Tag, unsigned Line, uint64_t SizeInBits,
656 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
657 ArrayRef<Metadata *> Ops)
658 : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
660 ~DIDerivedTypeBase() = default;
663 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
664 Metadata *getRawBaseType() const { return getOperand(3); }
666 static bool classof(const Metadata *MD) {
667 return MD->getMetadataID() == DIDerivedTypeKind ||
668 MD->getMetadataID() == DICompositeTypeKind ||
669 MD->getMetadataID() == DISubroutineTypeKind;
673 /// \brief Derived types.
675 /// This includes qualified types, pointers, references, friends, typedefs, and
678 /// TODO: Split out members (inheritance, fields, methods, etc.).
679 class DIDerivedType : public DIDerivedTypeBase {
680 friend class LLVMContextImpl;
683 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
684 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
685 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
686 : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
687 AlignInBits, OffsetInBits, Flags, Ops) {}
688 ~DIDerivedType() = default;
690 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
691 StringRef Name, DIFile *File, unsigned Line,
692 DIScopeRef Scope, DITypeRef BaseType,
693 uint64_t SizeInBits, uint64_t AlignInBits,
694 uint64_t OffsetInBits, unsigned Flags,
695 Metadata *ExtraData, StorageType Storage,
696 bool ShouldCreate = true) {
697 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
698 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
699 Flags, ExtraData, Storage, ShouldCreate);
701 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
702 MDString *Name, Metadata *File, unsigned Line,
703 Metadata *Scope, Metadata *BaseType,
704 uint64_t SizeInBits, uint64_t AlignInBits,
705 uint64_t OffsetInBits, unsigned Flags,
706 Metadata *ExtraData, StorageType Storage,
707 bool ShouldCreate = true);
709 TempDIDerivedType cloneImpl() const {
710 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
711 getScope(), getBaseType(), getSizeInBits(),
712 getAlignInBits(), getOffsetInBits(), getFlags(),
717 DEFINE_MDNODE_GET(DIDerivedType,
718 (unsigned Tag, MDString *Name, Metadata *File,
719 unsigned Line, Metadata *Scope, Metadata *BaseType,
720 uint64_t SizeInBits, uint64_t AlignInBits,
721 uint64_t OffsetInBits, unsigned Flags,
722 Metadata *ExtraData = nullptr),
723 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
724 AlignInBits, OffsetInBits, Flags, ExtraData))
725 DEFINE_MDNODE_GET(DIDerivedType,
726 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
727 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
728 uint64_t AlignInBits, uint64_t OffsetInBits,
729 unsigned Flags, Metadata *ExtraData = nullptr),
730 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
731 AlignInBits, OffsetInBits, Flags, ExtraData))
733 TempDIDerivedType clone() const { return cloneImpl(); }
735 /// \brief Get extra data associated with this derived type.
737 /// Class type for pointer-to-members, objective-c property node for ivars,
738 /// or global constant wrapper for static members.
740 /// TODO: Separate out types that need this extra operand: pointer-to-member
741 /// types and member fields (static members and ivars).
742 Metadata *getExtraData() const { return getRawExtraData(); }
743 Metadata *getRawExtraData() const { return getOperand(4); }
745 /// \brief Get casted version of extra data.
747 DITypeRef getClassType() const {
748 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
749 return DITypeRef(getExtraData());
751 DIObjCProperty *getObjCProperty() const {
752 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
754 Constant *getConstant() const {
755 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
756 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
757 return C->getValue();
762 static bool classof(const Metadata *MD) {
763 return MD->getMetadataID() == DIDerivedTypeKind;
767 /// \brief Base class for DICompositeType and DISubroutineType.
769 /// TODO: Delete; they're not really related.
770 class DICompositeTypeBase : public DIDerivedTypeBase {
771 unsigned RuntimeLang;
774 DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
775 unsigned Tag, unsigned Line, unsigned RuntimeLang,
776 uint64_t SizeInBits, uint64_t AlignInBits,
777 uint64_t OffsetInBits, unsigned Flags,
778 ArrayRef<Metadata *> Ops)
779 : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
780 OffsetInBits, Flags, Ops),
781 RuntimeLang(RuntimeLang) {}
782 ~DICompositeTypeBase() = default;
785 /// \brief Get the elements of the composite type.
787 /// \note Calling this is only valid for \a DICompositeType. This assertion
788 /// can be removed once \a DISubroutineType has been separated from
789 /// "composite types".
790 DINodeArray getElements() const {
791 assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
792 return cast_or_null<MDTuple>(getRawElements());
794 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
795 DITemplateParameterArray getTemplateParams() const {
796 return cast_or_null<MDTuple>(getRawTemplateParams());
798 StringRef getIdentifier() const { return getStringOperand(7); }
799 unsigned getRuntimeLang() const { return RuntimeLang; }
801 Metadata *getRawElements() const { return getOperand(4); }
802 Metadata *getRawVTableHolder() const { return getOperand(5); }
803 Metadata *getRawTemplateParams() const { return getOperand(6); }
804 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
806 /// \brief Replace operands.
808 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
809 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
810 /// of its movement if necessary.
812 void replaceElements(DINodeArray Elements) {
814 for (DINode *Op : getElements())
815 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
816 "Lost a member during member list replacement");
818 replaceOperandWith(4, Elements.get());
820 void replaceVTableHolder(DITypeRef VTableHolder) {
821 replaceOperandWith(5, VTableHolder);
823 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
824 replaceOperandWith(6, TemplateParams.get());
828 static bool classof(const Metadata *MD) {
829 return MD->getMetadataID() == DICompositeTypeKind ||
830 MD->getMetadataID() == DISubroutineTypeKind;
834 /// \brief Composite types.
836 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
837 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
838 class DICompositeType : public DICompositeTypeBase {
839 friend class LLVMContextImpl;
842 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
843 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
844 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
845 ArrayRef<Metadata *> Ops)
846 : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
847 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
849 ~DICompositeType() = default;
851 static DICompositeType *
852 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
853 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
854 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
855 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
856 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
857 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
859 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
860 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
861 RuntimeLang, VTableHolder, TemplateParams.get(),
862 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
864 static DICompositeType *
865 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
866 unsigned Line, Metadata *Scope, Metadata *BaseType,
867 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
868 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
869 Metadata *VTableHolder, Metadata *TemplateParams,
870 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
872 TempDICompositeType cloneImpl() const {
873 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
874 getScope(), getBaseType(), getSizeInBits(),
875 getAlignInBits(), getOffsetInBits(), getFlags(),
876 getElements(), getRuntimeLang(), getVTableHolder(),
877 getTemplateParams(), getIdentifier());
881 DEFINE_MDNODE_GET(DICompositeType,
882 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
883 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
884 uint64_t AlignInBits, uint64_t OffsetInBits,
885 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
886 DITypeRef VTableHolder,
887 DITemplateParameterArray TemplateParams = nullptr,
888 StringRef Identifier = ""),
889 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
890 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
891 VTableHolder, TemplateParams, Identifier))
892 DEFINE_MDNODE_GET(DICompositeType,
893 (unsigned Tag, MDString *Name, Metadata *File,
894 unsigned Line, Metadata *Scope, Metadata *BaseType,
895 uint64_t SizeInBits, uint64_t AlignInBits,
896 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
897 unsigned RuntimeLang, Metadata *VTableHolder,
898 Metadata *TemplateParams = nullptr,
899 MDString *Identifier = nullptr),
900 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
901 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
902 VTableHolder, TemplateParams, Identifier))
904 TempDICompositeType clone() const { return cloneImpl(); }
906 static bool classof(const Metadata *MD) {
907 return MD->getMetadataID() == DICompositeTypeKind;
911 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
913 if (auto *Composite = dyn_cast<DICompositeType>(N))
914 if (auto *S = Composite->getRawIdentifier())
915 return TypedDINodeRef<T>(S);
916 return TypedDINodeRef<T>(N);
919 /// \brief Type array for a subprogram.
921 /// TODO: Detach from CompositeType, and fold the array of types in directly
923 class DISubroutineType : public DICompositeTypeBase {
924 friend class LLVMContextImpl;
927 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
928 ArrayRef<Metadata *> Ops)
929 : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
930 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
932 ~DISubroutineType() = default;
934 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
935 DITypeRefArray TypeArray,
937 bool ShouldCreate = true) {
938 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
940 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
941 Metadata *TypeArray, StorageType Storage,
942 bool ShouldCreate = true);
944 TempDISubroutineType cloneImpl() const {
945 return getTemporary(getContext(), getFlags(), getTypeArray());
949 DEFINE_MDNODE_GET(DISubroutineType,
950 (unsigned Flags, DITypeRefArray TypeArray),
952 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
955 TempDISubroutineType clone() const { return cloneImpl(); }
957 DITypeRefArray getTypeArray() const {
958 return cast_or_null<MDTuple>(getRawTypeArray());
960 Metadata *getRawTypeArray() const { return getRawElements(); }
962 static bool classof(const Metadata *MD) {
963 return MD->getMetadataID() == DISubroutineTypeKind;
967 /// \brief Compile unit.
968 class DICompileUnit : public DIScope {
969 friend class LLVMContextImpl;
972 unsigned SourceLanguage;
974 unsigned RuntimeVersion;
975 unsigned EmissionKind;
978 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
979 bool IsOptimized, unsigned RuntimeVersion,
980 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
981 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
982 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
983 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
985 ~DICompileUnit() = default;
987 static DICompileUnit *
988 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
989 StringRef Producer, bool IsOptimized, StringRef Flags,
990 unsigned RuntimeVersion, StringRef SplitDebugFilename,
991 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
992 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
993 DIGlobalVariableArray GlobalVariables,
994 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
995 StorageType Storage, bool ShouldCreate = true) {
996 return getImpl(Context, SourceLanguage, File,
997 getCanonicalMDString(Context, Producer), IsOptimized,
998 getCanonicalMDString(Context, Flags), RuntimeVersion,
999 getCanonicalMDString(Context, SplitDebugFilename),
1000 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
1001 Subprograms.get(), GlobalVariables.get(),
1002 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
1004 static DICompileUnit *
1005 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1006 MDString *Producer, bool IsOptimized, MDString *Flags,
1007 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1008 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1009 Metadata *Subprograms, Metadata *GlobalVariables,
1010 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
1011 bool ShouldCreate = true);
1013 TempDICompileUnit cloneImpl() const {
1014 return getTemporary(
1015 getContext(), getSourceLanguage(), getFile(), getProducer(),
1016 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1017 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1018 getGlobalVariables(), getImportedEntities(), DWOId);
1022 DEFINE_MDNODE_GET(DICompileUnit,
1023 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1024 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1025 StringRef SplitDebugFilename, unsigned EmissionKind,
1026 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1027 DISubprogramArray Subprograms,
1028 DIGlobalVariableArray GlobalVariables,
1029 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1030 (SourceLanguage, File, Producer, IsOptimized, Flags,
1031 RuntimeVersion, SplitDebugFilename, EmissionKind,
1032 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1033 ImportedEntities, DWOId))
1036 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1037 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1038 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1039 Metadata *RetainedTypes, Metadata *Subprograms,
1040 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1041 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1042 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1043 GlobalVariables, ImportedEntities, DWOId))
1045 TempDICompileUnit clone() const { return cloneImpl(); }
1047 unsigned getSourceLanguage() const { return SourceLanguage; }
1048 bool isOptimized() const { return IsOptimized; }
1049 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1050 unsigned getEmissionKind() const { return EmissionKind; }
1051 StringRef getProducer() const { return getStringOperand(1); }
1052 StringRef getFlags() const { return getStringOperand(2); }
1053 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1054 DICompositeTypeArray getEnumTypes() const {
1055 return cast_or_null<MDTuple>(getRawEnumTypes());
1057 DITypeArray getRetainedTypes() const {
1058 return cast_or_null<MDTuple>(getRawRetainedTypes());
1060 DISubprogramArray getSubprograms() const {
1061 return cast_or_null<MDTuple>(getRawSubprograms());
1063 DIGlobalVariableArray getGlobalVariables() const {
1064 return cast_or_null<MDTuple>(getRawGlobalVariables());
1066 DIImportedEntityArray getImportedEntities() const {
1067 return cast_or_null<MDTuple>(getRawImportedEntities());
1069 unsigned getDWOId() const { return DWOId; }
1071 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1072 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1073 MDString *getRawSplitDebugFilename() const {
1074 return getOperandAs<MDString>(3);
1076 Metadata *getRawEnumTypes() const { return getOperand(4); }
1077 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1078 Metadata *getRawSubprograms() const { return getOperand(6); }
1079 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1080 Metadata *getRawImportedEntities() const { return getOperand(8); }
1082 /// \brief Replace arrays.
1084 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1085 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1086 /// DICompileUnit should be fairly rare.
1088 void replaceSubprograms(DISubprogramArray N) {
1089 replaceOperandWith(6, N.get());
1091 void replaceGlobalVariables(DIGlobalVariableArray N) {
1092 replaceOperandWith(7, N.get());
1096 static bool classof(const Metadata *MD) {
1097 return MD->getMetadataID() == DICompileUnitKind;
1101 /// \brief A scope for locals.
1103 /// A legal scope for lexical blocks, local variables, and debug info
1104 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1105 /// DILexicalBlockFile.
1106 class DILocalScope : public DIScope {
1108 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1109 ArrayRef<Metadata *> Ops)
1110 : DIScope(C, ID, Storage, Tag, Ops) {}
1111 ~DILocalScope() = default;
1114 /// \brief Get the subprogram for this scope.
1116 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1118 DISubprogram *getSubprogram() const;
1120 static bool classof(const Metadata *MD) {
1121 return MD->getMetadataID() == DISubprogramKind ||
1122 MD->getMetadataID() == DILexicalBlockKind ||
1123 MD->getMetadataID() == DILexicalBlockFileKind;
1127 /// \brief Debug location.
1129 /// A debug location in source code, used for debug info and otherwise.
1130 class DILocation : public MDNode {
1131 friend class LLVMContextImpl;
1132 friend class MDNode;
1134 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1135 unsigned Column, ArrayRef<Metadata *> MDs);
1136 ~DILocation() { dropAllReferences(); }
1138 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1139 unsigned Column, Metadata *Scope,
1140 Metadata *InlinedAt, StorageType Storage,
1141 bool ShouldCreate = true);
1142 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1143 unsigned Column, DILocalScope *Scope,
1144 DILocation *InlinedAt, StorageType Storage,
1145 bool ShouldCreate = true) {
1146 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1147 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1150 TempDILocation cloneImpl() const {
1151 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1155 // Disallow replacing operands.
1156 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1159 DEFINE_MDNODE_GET(DILocation,
1160 (unsigned Line, unsigned Column, Metadata *Scope,
1161 Metadata *InlinedAt = nullptr),
1162 (Line, Column, Scope, InlinedAt))
1163 DEFINE_MDNODE_GET(DILocation,
1164 (unsigned Line, unsigned Column, DILocalScope *Scope,
1165 DILocation *InlinedAt = nullptr),
1166 (Line, Column, Scope, InlinedAt))
1168 /// \brief Return a (temporary) clone of this.
1169 TempDILocation clone() const { return cloneImpl(); }
1171 unsigned getLine() const { return SubclassData32; }
1172 unsigned getColumn() const { return SubclassData16; }
1173 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1174 DILocation *getInlinedAt() const {
1175 return cast_or_null<DILocation>(getRawInlinedAt());
1178 DIFile *getFile() const { return getScope()->getFile(); }
1179 StringRef getFilename() const { return getScope()->getFilename(); }
1180 StringRef getDirectory() const { return getScope()->getDirectory(); }
1182 /// \brief Get the scope where this is inlined.
1184 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1186 DILocalScope *getInlinedAtScope() const {
1187 if (auto *IA = getInlinedAt())
1188 return IA->getInlinedAtScope();
1192 /// \brief Check whether this can be discriminated from another location.
1194 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1195 /// Scope and inlined-at chains are not recorded in the linetable, so they
1196 /// cannot be used to distinguish basic blocks.
1198 /// The current implementation is weaker than it should be, since it just
1199 /// checks filename and line.
1201 /// FIXME: Add a check for getDiscriminator().
1202 /// FIXME: Add a check for getColumn().
1203 /// FIXME: Change the getFilename() check to getFile() (or add one for
1204 /// getDirectory()).
1205 bool canDiscriminate(const DILocation &RHS) const {
1206 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1209 /// \brief Get the DWARF discriminator.
1211 /// DWARF discriminators distinguish identical file locations between
1212 /// instructions that are on different basic blocks.
1213 inline unsigned getDiscriminator() const;
1215 /// \brief Compute new discriminator in the given context.
1217 /// This modifies the \a LLVMContext that \c this is in to increment the next
1218 /// discriminator for \c this's line/filename combination.
1220 /// FIXME: Delete this. See comments in implementation and at the only call
1221 /// site in \a AddDiscriminators::runOnFunction().
1222 unsigned computeNewDiscriminator() const;
1224 Metadata *getRawScope() const { return getOperand(0); }
1225 Metadata *getRawInlinedAt() const {
1226 if (getNumOperands() == 2)
1227 return getOperand(1);
1231 static bool classof(const Metadata *MD) {
1232 return MD->getMetadataID() == DILocationKind;
1236 /// \brief Subprogram description.
1238 /// TODO: Remove DisplayName. It's always equal to Name.
1239 /// TODO: Split up flags.
1240 class DISubprogram : public DILocalScope {
1241 friend class LLVMContextImpl;
1242 friend class MDNode;
1246 unsigned Virtuality;
1247 unsigned VirtualIndex;
1253 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1254 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1255 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1256 bool IsOptimized, ArrayRef<Metadata *> Ops)
1257 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1259 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1260 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1261 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1262 ~DISubprogram() = default;
1264 static DISubprogram *
1265 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1266 StringRef LinkageName, DIFile *File, unsigned Line,
1267 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1268 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1269 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1270 Constant *Function, DITemplateParameterArray TemplateParams,
1271 DISubprogram *Declaration, DILocalVariableArray Variables,
1272 StorageType Storage, bool ShouldCreate = true) {
1273 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1274 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1275 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1276 Virtuality, VirtualIndex, Flags, IsOptimized,
1277 Function ? ConstantAsMetadata::get(Function) : nullptr,
1278 TemplateParams.get(), Declaration, Variables.get(), Storage,
1281 static DISubprogram *
1282 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1283 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1284 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1285 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1286 unsigned Flags, bool IsOptimized, Metadata *Function,
1287 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1288 StorageType Storage, bool ShouldCreate = true);
1290 TempDISubprogram cloneImpl() const {
1291 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1292 getFile(), getLine(), getType(), isLocalToUnit(),
1293 isDefinition(), getScopeLine(), getContainingType(),
1294 getVirtuality(), getVirtualIndex(), getFlags(),
1295 isOptimized(), getFunctionConstant(),
1296 getTemplateParams(), getDeclaration(), getVariables());
1300 DEFINE_MDNODE_GET(DISubprogram,
1301 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1302 DIFile *File, unsigned Line, DISubroutineType *Type,
1303 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1304 DITypeRef ContainingType, unsigned Virtuality,
1305 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1306 Constant *Function = nullptr,
1307 DITemplateParameterArray TemplateParams = nullptr,
1308 DISubprogram *Declaration = nullptr,
1309 DILocalVariableArray Variables = nullptr),
1310 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1311 IsDefinition, ScopeLine, ContainingType, Virtuality,
1312 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1313 Declaration, Variables))
1316 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1317 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1318 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1319 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1320 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1321 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1322 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1323 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1324 Function, TemplateParams, Declaration, Variables))
1326 TempDISubprogram clone() const { return cloneImpl(); }
1329 unsigned getLine() const { return Line; }
1330 unsigned getVirtuality() const { return Virtuality; }
1331 unsigned getVirtualIndex() const { return VirtualIndex; }
1332 unsigned getScopeLine() const { return ScopeLine; }
1333 unsigned getFlags() const { return Flags; }
1334 bool isLocalToUnit() const { return IsLocalToUnit; }
1335 bool isDefinition() const { return IsDefinition; }
1336 bool isOptimized() const { return IsOptimized; }
1338 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1339 bool isPrivate() const {
1340 return (getFlags() & FlagAccessibility) == FlagPrivate;
1342 bool isProtected() const {
1343 return (getFlags() & FlagAccessibility) == FlagProtected;
1345 bool isPublic() const {
1346 return (getFlags() & FlagAccessibility) == FlagPublic;
1348 bool isExplicit() const { return getFlags() & FlagExplicit; }
1349 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1351 /// \brief Check if this is reference-qualified.
1353 /// Return true if this subprogram is a C++11 reference-qualified non-static
1354 /// member function (void foo() &).
1355 unsigned isLValueReference() const {
1356 return getFlags() & FlagLValueReference;
1359 /// \brief Check if this is rvalue-reference-qualified.
1361 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1362 /// non-static member function (void foo() &&).
1363 unsigned isRValueReference() const {
1364 return getFlags() & FlagRValueReference;
1367 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1369 StringRef getName() const { return getStringOperand(2); }
1370 StringRef getDisplayName() const { return getStringOperand(3); }
1371 StringRef getLinkageName() const { return getStringOperand(4); }
1373 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1374 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1376 DISubroutineType *getType() const {
1377 return cast_or_null<DISubroutineType>(getRawType());
1379 DITypeRef getContainingType() const {
1380 return DITypeRef(getRawContainingType());
1383 Constant *getFunctionConstant() const {
1384 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1385 return C->getValue();
1388 DITemplateParameterArray getTemplateParams() const {
1389 return cast_or_null<MDTuple>(getRawTemplateParams());
1391 DISubprogram *getDeclaration() const {
1392 return cast_or_null<DISubprogram>(getRawDeclaration());
1394 DILocalVariableArray getVariables() const {
1395 return cast_or_null<MDTuple>(getRawVariables());
1398 Metadata *getRawScope() const { return getOperand(1); }
1399 Metadata *getRawType() const { return getOperand(5); }
1400 Metadata *getRawContainingType() const { return getOperand(6); }
1401 Metadata *getRawFunction() const { return getOperand(7); }
1402 Metadata *getRawTemplateParams() const { return getOperand(8); }
1403 Metadata *getRawDeclaration() const { return getOperand(9); }
1404 Metadata *getRawVariables() const { return getOperand(10); }
1406 /// \brief Get a pointer to the function this subprogram describes.
1408 /// This dyn_casts \a getFunctionConstant() to \a Function.
1410 /// FIXME: Should this be looking through bitcasts?
1411 Function *getFunction() const;
1413 /// \brief Replace the function.
1415 /// If \a isUniqued() and not \a isResolved(), this could node will be
1416 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1417 /// that's a problem.
1419 void replaceFunction(Function *F);
1420 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1421 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1424 /// \brief Check if this subprogram decribes the given function.
1426 /// FIXME: Should this be looking through bitcasts?
1427 bool describes(const Function *F) const;
1429 static bool classof(const Metadata *MD) {
1430 return MD->getMetadataID() == DISubprogramKind;
1434 class DILexicalBlockBase : public DILocalScope {
1436 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1437 ArrayRef<Metadata *> Ops)
1438 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1439 ~DILexicalBlockBase() = default;
1442 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1444 Metadata *getRawScope() const { return getOperand(1); }
1446 /// \brief Forwarding accessors to LexicalBlock.
1448 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1450 inline unsigned getLine() const;
1451 inline unsigned getColumn() const;
1453 static bool classof(const Metadata *MD) {
1454 return MD->getMetadataID() == DILexicalBlockKind ||
1455 MD->getMetadataID() == DILexicalBlockFileKind;
1459 class DILexicalBlock : public DILexicalBlockBase {
1460 friend class LLVMContextImpl;
1461 friend class MDNode;
1466 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1467 unsigned Column, ArrayRef<Metadata *> Ops)
1468 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1470 ~DILexicalBlock() = default;
1472 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1473 DIFile *File, unsigned Line, unsigned Column,
1474 StorageType Storage,
1475 bool ShouldCreate = true) {
1476 return getImpl(Context, static_cast<Metadata *>(Scope),
1477 static_cast<Metadata *>(File), Line, Column, Storage,
1481 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1482 Metadata *File, unsigned Line, unsigned Column,
1483 StorageType Storage, bool ShouldCreate = true);
1485 TempDILexicalBlock cloneImpl() const {
1486 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1491 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1492 unsigned Line, unsigned Column),
1493 (Scope, File, Line, Column))
1494 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1495 unsigned Line, unsigned Column),
1496 (Scope, File, Line, Column))
1498 TempDILexicalBlock clone() const { return cloneImpl(); }
1500 unsigned getLine() const { return Line; }
1501 unsigned getColumn() const { return Column; }
1503 static bool classof(const Metadata *MD) {
1504 return MD->getMetadataID() == DILexicalBlockKind;
1508 unsigned DILexicalBlockBase::getLine() const {
1509 if (auto *N = dyn_cast<DILexicalBlock>(this))
1510 return N->getLine();
1514 unsigned DILexicalBlockBase::getColumn() const {
1515 if (auto *N = dyn_cast<DILexicalBlock>(this))
1516 return N->getColumn();
1520 class DILexicalBlockFile : public DILexicalBlockBase {
1521 friend class LLVMContextImpl;
1522 friend class MDNode;
1524 unsigned Discriminator;
1526 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1527 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1528 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1529 Discriminator(Discriminator) {}
1530 ~DILexicalBlockFile() = default;
1532 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1533 DIFile *File, unsigned Discriminator,
1534 StorageType Storage,
1535 bool ShouldCreate = true) {
1536 return getImpl(Context, static_cast<Metadata *>(Scope),
1537 static_cast<Metadata *>(File), Discriminator, Storage,
1541 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1542 Metadata *File, unsigned Discriminator,
1543 StorageType Storage,
1544 bool ShouldCreate = true);
1546 TempDILexicalBlockFile cloneImpl() const {
1547 return getTemporary(getContext(), getScope(), getFile(),
1548 getDiscriminator());
1552 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1553 unsigned Discriminator),
1554 (Scope, File, Discriminator))
1555 DEFINE_MDNODE_GET(DILexicalBlockFile,
1556 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1557 (Scope, File, Discriminator))
1559 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1561 // TODO: Remove these once they're gone from DILexicalBlockBase.
1562 unsigned getLine() const = delete;
1563 unsigned getColumn() const = delete;
1565 unsigned getDiscriminator() const { return Discriminator; }
1567 static bool classof(const Metadata *MD) {
1568 return MD->getMetadataID() == DILexicalBlockFileKind;
1572 unsigned DILocation::getDiscriminator() const {
1573 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1574 return F->getDiscriminator();
1578 class DINamespace : public DIScope {
1579 friend class LLVMContextImpl;
1580 friend class MDNode;
1584 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1585 ArrayRef<Metadata *> Ops)
1586 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1589 ~DINamespace() = default;
1591 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1592 DIFile *File, StringRef Name, unsigned Line,
1593 StorageType Storage, bool ShouldCreate = true) {
1594 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1595 Line, Storage, ShouldCreate);
1597 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1598 Metadata *File, MDString *Name, unsigned Line,
1599 StorageType Storage, bool ShouldCreate = true);
1601 TempDINamespace cloneImpl() const {
1602 return getTemporary(getContext(), getScope(), getFile(), getName(),
1607 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1609 (Scope, File, Name, Line))
1610 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1611 MDString *Name, unsigned Line),
1612 (Scope, File, Name, Line))
1614 TempDINamespace clone() const { return cloneImpl(); }
1616 unsigned getLine() const { return Line; }
1617 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1618 StringRef getName() const { return getStringOperand(2); }
1620 Metadata *getRawScope() const { return getOperand(1); }
1621 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1623 static bool classof(const Metadata *MD) {
1624 return MD->getMetadataID() == DINamespaceKind;
1628 /// \brief A (clang) module that has been imported by the compile unit.
1630 class DIModule : public DIScope {
1631 friend class LLVMContextImpl;
1632 friend class MDNode;
1634 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1635 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1638 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1639 StringRef Name, StringRef ConfigurationMacros,
1640 StringRef IncludePath, StringRef ISysRoot,
1641 StorageType Storage, bool ShouldCreate = true) {
1642 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1643 getCanonicalMDString(Context, ConfigurationMacros),
1644 getCanonicalMDString(Context, IncludePath),
1645 getCanonicalMDString(Context, ISysRoot),
1646 Storage, ShouldCreate);
1648 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1649 MDString *Name, MDString *ConfigurationMacros,
1650 MDString *IncludePath, MDString *ISysRoot,
1651 StorageType Storage, bool ShouldCreate = true);
1653 TempDIModule cloneImpl() const {
1654 return getTemporary(getContext(), getScope(), getName(),
1655 getConfigurationMacros(), getIncludePath(),
1660 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1661 StringRef ConfigurationMacros, StringRef IncludePath,
1662 StringRef ISysRoot),
1663 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1664 DEFINE_MDNODE_GET(DIModule,
1665 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1666 MDString *IncludePath, MDString *ISysRoot),
1667 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1669 TempDIModule clone() const { return cloneImpl(); }
1671 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1672 StringRef getName() const { return getStringOperand(1); }
1673 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1674 StringRef getIncludePath() const { return getStringOperand(3); }
1675 StringRef getISysRoot() const { return getStringOperand(4); }
1677 Metadata *getRawScope() const { return getOperand(0); }
1678 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1679 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1680 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1681 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1683 static bool classof(const Metadata *MD) {
1684 return MD->getMetadataID() == DIModuleKind;
1688 /// \brief Base class for template parameters.
1689 class DITemplateParameter : public DINode {
1691 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1692 unsigned Tag, ArrayRef<Metadata *> Ops)
1693 : DINode(Context, ID, Storage, Tag, Ops) {}
1694 ~DITemplateParameter() = default;
1697 StringRef getName() const { return getStringOperand(0); }
1698 DITypeRef getType() const { return DITypeRef(getRawType()); }
1700 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1701 Metadata *getRawType() const { return getOperand(1); }
1703 static bool classof(const Metadata *MD) {
1704 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1705 MD->getMetadataID() == DITemplateValueParameterKind;
1709 class DITemplateTypeParameter : public DITemplateParameter {
1710 friend class LLVMContextImpl;
1711 friend class MDNode;
1713 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1714 ArrayRef<Metadata *> Ops)
1715 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1716 dwarf::DW_TAG_template_type_parameter, Ops) {}
1717 ~DITemplateTypeParameter() = default;
1719 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1720 DITypeRef Type, StorageType Storage,
1721 bool ShouldCreate = true) {
1722 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1725 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1726 Metadata *Type, StorageType Storage,
1727 bool ShouldCreate = true);
1729 TempDITemplateTypeParameter cloneImpl() const {
1730 return getTemporary(getContext(), getName(), getType());
1734 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1736 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1739 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1741 static bool classof(const Metadata *MD) {
1742 return MD->getMetadataID() == DITemplateTypeParameterKind;
1746 class DITemplateValueParameter : public DITemplateParameter {
1747 friend class LLVMContextImpl;
1748 friend class MDNode;
1750 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1751 unsigned Tag, ArrayRef<Metadata *> Ops)
1752 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1754 ~DITemplateValueParameter() = default;
1756 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1757 StringRef Name, DITypeRef Type,
1758 Metadata *Value, StorageType Storage,
1759 bool ShouldCreate = true) {
1760 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1761 Value, Storage, ShouldCreate);
1763 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1764 MDString *Name, Metadata *Type,
1765 Metadata *Value, StorageType Storage,
1766 bool ShouldCreate = true);
1768 TempDITemplateValueParameter cloneImpl() const {
1769 return getTemporary(getContext(), getTag(), getName(), getType(),
1774 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1775 DITypeRef Type, Metadata *Value),
1776 (Tag, Name, Type, Value))
1777 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1778 Metadata *Type, Metadata *Value),
1779 (Tag, Name, Type, Value))
1781 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1783 Metadata *getValue() const { return getOperand(2); }
1785 static bool classof(const Metadata *MD) {
1786 return MD->getMetadataID() == DITemplateValueParameterKind;
1790 /// \brief Base class for variables.
1792 /// TODO: Hardcode to DW_TAG_variable.
1793 class DIVariable : public DINode {
1797 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1798 unsigned Line, ArrayRef<Metadata *> Ops)
1799 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1800 ~DIVariable() = default;
1803 unsigned getLine() const { return Line; }
1804 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1805 StringRef getName() const { return getStringOperand(1); }
1806 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1807 DITypeRef getType() const { return DITypeRef(getRawType()); }
1809 StringRef getFilename() const {
1810 if (auto *F = getFile())
1811 return F->getFilename();
1814 StringRef getDirectory() const {
1815 if (auto *F = getFile())
1816 return F->getDirectory();
1820 Metadata *getRawScope() const { return getOperand(0); }
1821 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1822 Metadata *getRawFile() const { return getOperand(2); }
1823 Metadata *getRawType() const { return getOperand(3); }
1825 static bool classof(const Metadata *MD) {
1826 return MD->getMetadataID() == DILocalVariableKind ||
1827 MD->getMetadataID() == DIGlobalVariableKind;
1831 /// \brief Global variables.
1833 /// TODO: Remove DisplayName. It's always equal to Name.
1834 class DIGlobalVariable : public DIVariable {
1835 friend class LLVMContextImpl;
1836 friend class MDNode;
1841 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1842 bool IsLocalToUnit, bool IsDefinition,
1843 ArrayRef<Metadata *> Ops)
1844 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1846 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1847 ~DIGlobalVariable() = default;
1849 static DIGlobalVariable *
1850 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1851 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1852 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1853 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1854 bool ShouldCreate = true) {
1855 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1856 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1857 IsLocalToUnit, IsDefinition,
1858 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1859 StaticDataMemberDeclaration, Storage, ShouldCreate);
1861 static DIGlobalVariable *
1862 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1863 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1864 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1865 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1866 bool ShouldCreate = true);
1868 TempDIGlobalVariable cloneImpl() const {
1869 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1870 getFile(), getLine(), getType(), isLocalToUnit(),
1871 isDefinition(), getVariable(),
1872 getStaticDataMemberDeclaration());
1876 DEFINE_MDNODE_GET(DIGlobalVariable,
1877 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1878 DIFile *File, unsigned Line, DITypeRef Type,
1879 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1880 DIDerivedType *StaticDataMemberDeclaration),
1881 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1882 IsDefinition, Variable, StaticDataMemberDeclaration))
1883 DEFINE_MDNODE_GET(DIGlobalVariable,
1884 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1885 Metadata *File, unsigned Line, Metadata *Type,
1886 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1887 Metadata *StaticDataMemberDeclaration),
1888 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1889 IsDefinition, Variable, StaticDataMemberDeclaration))
1891 TempDIGlobalVariable clone() const { return cloneImpl(); }
1893 bool isLocalToUnit() const { return IsLocalToUnit; }
1894 bool isDefinition() const { return IsDefinition; }
1895 StringRef getDisplayName() const { return getStringOperand(4); }
1896 StringRef getLinkageName() const { return getStringOperand(5); }
1897 Constant *getVariable() const {
1898 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1899 return dyn_cast<Constant>(C->getValue());
1902 DIDerivedType *getStaticDataMemberDeclaration() const {
1903 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1906 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1907 Metadata *getRawVariable() const { return getOperand(6); }
1908 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1910 static bool classof(const Metadata *MD) {
1911 return MD->getMetadataID() == DIGlobalVariableKind;
1915 /// \brief Local variable.
1917 /// TODO: Split between arguments and otherwise.
1918 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1919 /// TODO: Split up flags.
1920 class DILocalVariable : public DIVariable {
1921 friend class LLVMContextImpl;
1922 friend class MDNode;
1927 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1928 unsigned Line, unsigned Arg, unsigned Flags,
1929 ArrayRef<Metadata *> Ops)
1930 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1932 ~DILocalVariable() = default;
1934 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1935 DIScope *Scope, StringRef Name, DIFile *File,
1936 unsigned Line, DITypeRef Type, unsigned Arg,
1937 unsigned Flags, StorageType Storage,
1938 bool ShouldCreate = true) {
1939 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1940 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1942 static DILocalVariable *
1943 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1944 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1945 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1947 TempDILocalVariable cloneImpl() const {
1948 return getTemporary(getContext(), getTag(), getScope(), getName(),
1949 getFile(), getLine(), getType(), getArg(), getFlags());
1953 DEFINE_MDNODE_GET(DILocalVariable,
1954 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1955 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1957 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1958 DEFINE_MDNODE_GET(DILocalVariable,
1959 (unsigned Tag, Metadata *Scope, MDString *Name,
1960 Metadata *File, unsigned Line, Metadata *Type,
1961 unsigned Arg, unsigned Flags),
1962 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1964 TempDILocalVariable clone() const { return cloneImpl(); }
1966 /// \brief Get the local scope for this variable.
1968 /// Variables must be defined in a local scope.
1969 DILocalScope *getScope() const {
1970 return cast<DILocalScope>(DIVariable::getScope());
1973 unsigned getArg() const { return Arg; }
1974 unsigned getFlags() const { return Flags; }
1976 bool isArtificial() const { return getFlags() & FlagArtificial; }
1977 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1979 /// \brief Check that a location is valid for this variable.
1981 /// Check that \c DL exists, is in the same subprogram, and has the same
1982 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1983 /// to a \a DbgInfoIntrinsic.)
1984 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1985 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1988 static bool classof(const Metadata *MD) {
1989 return MD->getMetadataID() == DILocalVariableKind;
1993 /// \brief DWARF expression.
1995 /// This is (almost) a DWARF expression that modifies the location of a
1996 /// variable or (or the location of a single piece of a variable).
1998 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1999 /// and have DW_OP_plus consume the topmost elements on the stack.
2001 /// TODO: Co-allocate the expression elements.
2002 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2004 class DIExpression : public MDNode {
2005 friend class LLVMContextImpl;
2006 friend class MDNode;
2008 std::vector<uint64_t> Elements;
2010 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2011 : MDNode(C, DIExpressionKind, Storage, None),
2012 Elements(Elements.begin(), Elements.end()) {}
2013 ~DIExpression() = default;
2015 static DIExpression *getImpl(LLVMContext &Context,
2016 ArrayRef<uint64_t> Elements, StorageType Storage,
2017 bool ShouldCreate = true);
2019 TempDIExpression cloneImpl() const {
2020 return getTemporary(getContext(), getElements());
2024 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2026 TempDIExpression clone() const { return cloneImpl(); }
2028 ArrayRef<uint64_t> getElements() const { return Elements; }
2030 unsigned getNumElements() const { return Elements.size(); }
2031 uint64_t getElement(unsigned I) const {
2032 assert(I < Elements.size() && "Index out of range");
2036 /// \brief Return whether this is a piece of an aggregate variable.
2037 bool isBitPiece() const;
2039 /// \brief Return the offset of this piece in bits.
2040 uint64_t getBitPieceOffset() const;
2042 /// \brief Return the size of this piece in bits.
2043 uint64_t getBitPieceSize() const;
2045 typedef ArrayRef<uint64_t>::iterator element_iterator;
2046 element_iterator elements_begin() const { return getElements().begin(); }
2047 element_iterator elements_end() const { return getElements().end(); }
2049 /// \brief A lightweight wrapper around an expression operand.
2051 /// TODO: Store arguments directly and change \a DIExpression to store a
2057 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2059 const uint64_t *get() const { return Op; }
2061 /// \brief Get the operand code.
2062 uint64_t getOp() const { return *Op; }
2064 /// \brief Get an argument to the operand.
2066 /// Never returns the operand itself.
2067 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2069 unsigned getNumArgs() const { return getSize() - 1; }
2071 /// \brief Return the size of the operand.
2073 /// Return the number of elements in the operand (1 + args).
2074 unsigned getSize() const;
2077 /// \brief An iterator for expression operands.
2078 class expr_op_iterator
2079 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2083 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2085 element_iterator getBase() const { return Op.get(); }
2086 const ExprOperand &operator*() const { return Op; }
2087 const ExprOperand *operator->() const { return &Op; }
2089 expr_op_iterator &operator++() {
2093 expr_op_iterator operator++(int) {
2094 expr_op_iterator T(*this);
2099 /// \brief Get the next iterator.
2101 /// \a std::next() doesn't work because this is technically an
2102 /// input_iterator, but it's a perfectly valid operation. This is an
2103 /// accessor to provide the same functionality.
2104 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2106 bool operator==(const expr_op_iterator &X) const {
2107 return getBase() == X.getBase();
2109 bool operator!=(const expr_op_iterator &X) const {
2110 return getBase() != X.getBase();
2114 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2117 /// \brief Visit the elements via ExprOperand wrappers.
2119 /// These range iterators visit elements through \a ExprOperand wrappers.
2120 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2123 /// \pre \a isValid() gives \c true.
2125 expr_op_iterator expr_op_begin() const {
2126 return expr_op_iterator(elements_begin());
2128 expr_op_iterator expr_op_end() const {
2129 return expr_op_iterator(elements_end());
2133 bool isValid() const;
2135 static bool classof(const Metadata *MD) {
2136 return MD->getMetadataID() == DIExpressionKind;
2140 class DIObjCProperty : public DINode {
2141 friend class LLVMContextImpl;
2142 friend class MDNode;
2145 unsigned Attributes;
2147 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2148 unsigned Attributes, ArrayRef<Metadata *> Ops)
2149 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2151 Line(Line), Attributes(Attributes) {}
2152 ~DIObjCProperty() = default;
2154 static DIObjCProperty *
2155 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2156 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2157 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2158 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2159 getCanonicalMDString(Context, GetterName),
2160 getCanonicalMDString(Context, SetterName), Attributes, Type,
2161 Storage, ShouldCreate);
2163 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2164 Metadata *File, unsigned Line,
2165 MDString *GetterName, MDString *SetterName,
2166 unsigned Attributes, Metadata *Type,
2167 StorageType Storage, bool ShouldCreate = true);
2169 TempDIObjCProperty cloneImpl() const {
2170 return getTemporary(getContext(), getName(), getFile(), getLine(),
2171 getGetterName(), getSetterName(), getAttributes(),
2176 DEFINE_MDNODE_GET(DIObjCProperty,
2177 (StringRef Name, DIFile *File, unsigned Line,
2178 StringRef GetterName, StringRef SetterName,
2179 unsigned Attributes, DITypeRef Type),
2180 (Name, File, Line, GetterName, SetterName, Attributes,
2182 DEFINE_MDNODE_GET(DIObjCProperty,
2183 (MDString * Name, Metadata *File, unsigned Line,
2184 MDString *GetterName, MDString *SetterName,
2185 unsigned Attributes, Metadata *Type),
2186 (Name, File, Line, GetterName, SetterName, Attributes,
2189 TempDIObjCProperty clone() const { return cloneImpl(); }
2191 unsigned getLine() const { return Line; }
2192 unsigned getAttributes() const { return Attributes; }
2193 StringRef getName() const { return getStringOperand(0); }
2194 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2195 StringRef getGetterName() const { return getStringOperand(2); }
2196 StringRef getSetterName() const { return getStringOperand(3); }
2197 DITypeRef getType() const { return DITypeRef(getRawType()); }
2199 StringRef getFilename() const {
2200 if (auto *F = getFile())
2201 return F->getFilename();
2204 StringRef getDirectory() const {
2205 if (auto *F = getFile())
2206 return F->getDirectory();
2210 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2211 Metadata *getRawFile() const { return getOperand(1); }
2212 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2213 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2214 Metadata *getRawType() const { return getOperand(4); }
2216 static bool classof(const Metadata *MD) {
2217 return MD->getMetadataID() == DIObjCPropertyKind;
2221 /// \brief An imported module (C++ using directive or similar).
2222 class DIImportedEntity : public DINode {
2223 friend class LLVMContextImpl;
2224 friend class MDNode;
2228 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2229 unsigned Line, ArrayRef<Metadata *> Ops)
2230 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2231 ~DIImportedEntity() = default;
2233 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2234 DIScope *Scope, DINodeRef Entity,
2235 unsigned Line, StringRef Name,
2236 StorageType Storage,
2237 bool ShouldCreate = true) {
2238 return getImpl(Context, Tag, Scope, Entity, Line,
2239 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2241 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2242 Metadata *Scope, Metadata *Entity,
2243 unsigned Line, MDString *Name,
2244 StorageType Storage,
2245 bool ShouldCreate = true);
2247 TempDIImportedEntity cloneImpl() const {
2248 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2249 getLine(), getName());
2253 DEFINE_MDNODE_GET(DIImportedEntity,
2254 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2255 unsigned Line, StringRef Name = ""),
2256 (Tag, Scope, Entity, Line, Name))
2257 DEFINE_MDNODE_GET(DIImportedEntity,
2258 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2259 unsigned Line, MDString *Name),
2260 (Tag, Scope, Entity, Line, Name))
2262 TempDIImportedEntity clone() const { return cloneImpl(); }
2264 unsigned getLine() const { return Line; }
2265 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2266 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2267 StringRef getName() const { return getStringOperand(2); }
2269 Metadata *getRawScope() const { return getOperand(0); }
2270 Metadata *getRawEntity() const { return getOperand(1); }
2271 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2273 static bool classof(const Metadata *MD) {
2274 return MD->getMetadataID() == DIImportedEntityKind;
2278 } // end namespace llvm
2280 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2281 #undef DEFINE_MDNODE_GET_UNPACK
2282 #undef DEFINE_MDNODE_GET