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_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
24 static CLASS *getDistinct(LLVMContext &Context, \
25 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
26 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
28 static Temp##CLASS getTemporary(LLVMContext &Context, \
29 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
31 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
33 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
34 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
35 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
37 static CLASS *getIfExists(LLVMContext &Context, \
38 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
39 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
40 /* ShouldCreate */ false); \
42 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
46 /// \brief Pointer union between a subclass of DINode and MDString.
48 /// \a DICompositeType can be referenced via an \a MDString unique identifier.
49 /// This class allows some type safety in the face of that, requiring either a
50 /// node of a particular type or an \a MDString.
51 template <class T> class TypedDINodeRef {
52 const Metadata *MD = nullptr;
55 TypedDINodeRef() = default;
56 TypedDINodeRef(std::nullptr_t) {}
58 /// \brief Construct from a raw pointer.
59 explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
60 assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
65 const TypedDINodeRef<U> &X,
66 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
70 operator Metadata *() const { return const_cast<Metadata *>(MD); }
72 bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; }
73 bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; }
75 /// \brief Create a reference.
77 /// Get a reference to \c N, using an \a MDString reference if available.
78 static TypedDINodeRef get(const T *N);
80 template <class MapTy> T *resolve(const MapTy &Map) const {
84 if (auto *Typed = dyn_cast<T>(MD))
85 return const_cast<T *>(Typed);
87 auto *S = cast<MDString>(MD);
89 assert(I != Map.end() && "Missing identifier in type map");
90 return cast<T>(I->second);
94 typedef TypedDINodeRef<DINode> DINodeRef;
95 typedef TypedDINodeRef<DIScope> DIScopeRef;
96 typedef TypedDINodeRef<DIType> DITypeRef;
98 class DITypeRefArray {
99 const MDTuple *N = nullptr;
102 DITypeRefArray(const MDTuple *N) : N(N) {}
104 explicit operator bool() const { return get(); }
105 explicit operator MDTuple *() const { return get(); }
107 MDTuple *get() const { return const_cast<MDTuple *>(N); }
108 MDTuple *operator->() const { return get(); }
109 MDTuple &operator*() const { return *get(); }
111 // FIXME: Fix callers and remove condition on N.
112 unsigned size() const { return N ? N->getNumOperands() : 0u; }
113 DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
115 class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
116 std::ptrdiff_t, void, DITypeRef> {
117 MDNode::op_iterator I = nullptr;
120 iterator() = default;
121 explicit iterator(MDNode::op_iterator I) : I(I) {}
122 DITypeRef operator*() const { return DITypeRef(*I); }
123 iterator &operator++() {
127 iterator operator++(int) {
128 iterator Temp(*this);
132 bool operator==(const iterator &X) const { return I == X.I; }
133 bool operator!=(const iterator &X) const { return I != X.I; }
136 // FIXME: Fix callers and remove condition on N.
137 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
138 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
141 /// \brief Tagged DWARF-like metadata node.
143 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
144 /// defined in llvm/Support/Dwarf.h). Called \a DINode because it's
145 /// potentially used for non-DWARF output.
146 class DINode : public MDNode {
147 friend class LLVMContextImpl;
151 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
152 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
153 : MDNode(C, ID, Storage, Ops1, Ops2) {
154 assert(Tag < 1u << 16);
155 SubclassData16 = Tag;
159 template <class Ty> Ty *getOperandAs(unsigned I) const {
160 return cast_or_null<Ty>(getOperand(I));
163 StringRef getStringOperand(unsigned I) const {
164 if (auto *S = getOperandAs<MDString>(I))
165 return S->getString();
169 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
172 return MDString::get(Context, S);
176 unsigned getTag() const { return SubclassData16; }
178 /// \brief Debug info flags.
180 /// The three accessibility flags are mutually exclusive and rolled together
181 /// in the first two bits.
183 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
184 #include "llvm/IR/DebugInfoFlags.def"
185 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
188 static unsigned getFlag(StringRef Flag);
189 static const char *getFlagString(unsigned Flag);
191 /// \brief Split up a flags bitfield.
193 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
194 /// any remaining (unrecognized) bits.
195 static unsigned splitFlags(unsigned Flags,
196 SmallVectorImpl<unsigned> &SplitFlags);
198 DINodeRef getRef() const { return DINodeRef::get(this); }
200 static bool classof(const Metadata *MD) {
201 switch (MD->getMetadataID()) {
204 case GenericDINodeKind:
206 case DIEnumeratorKind:
207 case DIBasicTypeKind:
208 case DIDerivedTypeKind:
209 case DICompositeTypeKind:
210 case DISubroutineTypeKind:
212 case DICompileUnitKind:
213 case DISubprogramKind:
214 case DILexicalBlockKind:
215 case DILexicalBlockFileKind:
216 case DINamespaceKind:
217 case DITemplateTypeParameterKind:
218 case DITemplateValueParameterKind:
219 case DIGlobalVariableKind:
220 case DILocalVariableKind:
221 case DIObjCPropertyKind:
222 case DIImportedEntityKind:
229 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
230 typedef Metadata *SimpleType;
231 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
237 struct simplify_type<TypedDINodeRef<T>>
238 : simplify_type<const TypedDINodeRef<T>> {};
240 /// \brief Generic tagged DWARF-like metadata node.
242 /// An un-specialized DWARF-like metadata node. The first operand is a
243 /// (possibly empty) null-separated \a MDString header that contains arbitrary
244 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
245 /// to other metadata.
246 class GenericDINode : public DINode {
247 friend class LLVMContextImpl;
250 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
251 unsigned Tag, ArrayRef<Metadata *> Ops1,
252 ArrayRef<Metadata *> Ops2)
253 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
256 ~GenericDINode() { dropAllReferences(); }
258 void setHash(unsigned Hash) { SubclassData32 = Hash; }
259 void recalculateHash();
261 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
262 StringRef Header, ArrayRef<Metadata *> DwarfOps,
263 StorageType Storage, bool ShouldCreate = true) {
264 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
265 DwarfOps, Storage, ShouldCreate);
268 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
269 MDString *Header, ArrayRef<Metadata *> DwarfOps,
270 StorageType Storage, bool ShouldCreate = true);
272 TempGenericDINode cloneImpl() const {
274 getContext(), getTag(), getHeader(),
275 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
279 unsigned getHash() const { return SubclassData32; }
281 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
282 ArrayRef<Metadata *> DwarfOps),
283 (Tag, Header, DwarfOps))
284 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
285 ArrayRef<Metadata *> DwarfOps),
286 (Tag, Header, DwarfOps))
288 /// \brief Return a (temporary) clone of this.
289 TempGenericDINode clone() const { return cloneImpl(); }
291 unsigned getTag() const { return SubclassData16; }
292 StringRef getHeader() const { return getStringOperand(0); }
294 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
295 op_iterator dwarf_op_end() const { return op_end(); }
296 op_range dwarf_operands() const {
297 return op_range(dwarf_op_begin(), dwarf_op_end());
300 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
301 const MDOperand &getDwarfOperand(unsigned I) const {
302 return getOperand(I + 1);
304 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
305 replaceOperandWith(I + 1, New);
308 static bool classof(const Metadata *MD) {
309 return MD->getMetadataID() == GenericDINodeKind;
313 /// \brief Array subrange.
315 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
317 class DISubrange : public DINode {
318 friend class LLVMContextImpl;
324 DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
326 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
327 Count(Count), LowerBound(LowerBound) {}
328 ~DISubrange() = default;
330 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
331 int64_t LowerBound, StorageType Storage,
332 bool ShouldCreate = true);
334 TempDISubrange cloneImpl() const {
335 return getTemporary(getContext(), getCount(), getLowerBound());
339 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
342 TempDISubrange clone() const { return cloneImpl(); }
344 int64_t getLowerBound() const { return LowerBound; }
345 int64_t getCount() const { return Count; }
347 static bool classof(const Metadata *MD) {
348 return MD->getMetadataID() == DISubrangeKind;
352 /// \brief Enumeration value.
354 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
355 /// longer creates a type cycle.
356 class DIEnumerator : public DINode {
357 friend class LLVMContextImpl;
362 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
363 ArrayRef<Metadata *> Ops)
364 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
366 ~DIEnumerator() = default;
368 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
369 StringRef Name, StorageType Storage,
370 bool ShouldCreate = true) {
371 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
374 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
375 MDString *Name, StorageType Storage,
376 bool ShouldCreate = true);
378 TempDIEnumerator cloneImpl() const {
379 return getTemporary(getContext(), getValue(), getName());
383 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
385 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
388 TempDIEnumerator clone() const { return cloneImpl(); }
390 int64_t getValue() const { return Value; }
391 StringRef getName() const { return getStringOperand(0); }
393 MDString *getRawName() const { return getOperandAs<MDString>(0); }
395 static bool classof(const Metadata *MD) {
396 return MD->getMetadataID() == DIEnumeratorKind;
400 /// \brief Base class for scope-like contexts.
402 /// Base class for lexical scopes and types (which are also declaration
405 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
406 class DIScope : public DINode {
408 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
409 ArrayRef<Metadata *> Ops)
410 : DINode(C, ID, Storage, Tag, Ops) {}
411 ~DIScope() = default;
414 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
416 inline StringRef getFilename() const;
417 inline StringRef getDirectory() const;
419 StringRef getName() const;
420 DIScopeRef getScope() const;
422 /// \brief Return the raw underlying file.
424 /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
425 /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
426 /// this. Otherwise, return the first operand, which is where all other
427 /// subclasses store their file pointer.
428 Metadata *getRawFile() const {
429 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
430 : static_cast<Metadata *>(getOperand(0));
433 DIScopeRef getRef() const { return DIScopeRef::get(this); }
435 static bool classof(const Metadata *MD) {
436 switch (MD->getMetadataID()) {
439 case DIBasicTypeKind:
440 case DIDerivedTypeKind:
441 case DICompositeTypeKind:
442 case DISubroutineTypeKind:
444 case DICompileUnitKind:
445 case DISubprogramKind:
446 case DILexicalBlockKind:
447 case DILexicalBlockFileKind:
448 case DINamespaceKind:
457 /// TODO: Merge with directory/file node (including users).
458 /// TODO: Canonicalize paths on creation.
459 class DIFile : public DIScope {
460 friend class LLVMContextImpl;
463 DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
464 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
467 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
468 StringRef Directory, StorageType Storage,
469 bool ShouldCreate = true) {
470 return getImpl(Context, getCanonicalMDString(Context, Filename),
471 getCanonicalMDString(Context, Directory), Storage,
474 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
475 MDString *Directory, StorageType Storage,
476 bool ShouldCreate = true);
478 TempDIFile cloneImpl() const {
479 return getTemporary(getContext(), getFilename(), getDirectory());
483 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
484 (Filename, Directory))
485 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
486 (Filename, Directory))
488 TempDIFile clone() const { return cloneImpl(); }
490 StringRef getFilename() const { return getStringOperand(0); }
491 StringRef getDirectory() const { return getStringOperand(1); }
493 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
494 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
496 static bool classof(const Metadata *MD) {
497 return MD->getMetadataID() == DIFileKind;
501 StringRef DIScope::getFilename() const {
502 if (auto *F = getFile())
503 return F->getFilename();
507 StringRef DIScope::getDirectory() const {
508 if (auto *F = getFile())
509 return F->getDirectory();
513 /// \brief Base class for types.
515 /// TODO: Remove the hardcoded name and context, since many types don't use
517 /// TODO: Split up flags.
518 class DIType : public DIScope {
522 uint64_t AlignInBits;
523 uint64_t OffsetInBits;
526 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
527 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
528 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
529 : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
530 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
531 OffsetInBits(OffsetInBits) {}
535 TempDIType clone() const {
536 return TempDIType(cast<DIType>(MDNode::clone().release()));
539 unsigned getLine() const { return Line; }
540 uint64_t getSizeInBits() const { return SizeInBits; }
541 uint64_t getAlignInBits() const { return AlignInBits; }
542 uint64_t getOffsetInBits() const { return OffsetInBits; }
543 unsigned getFlags() const { return Flags; }
545 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
546 StringRef getName() const { return getStringOperand(2); }
549 Metadata *getRawScope() const { return getOperand(1); }
550 MDString *getRawName() const { return getOperandAs<MDString>(2); }
552 void setFlags(unsigned NewFlags) {
553 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
557 bool isPrivate() const {
558 return (getFlags() & FlagAccessibility) == FlagPrivate;
560 bool isProtected() const {
561 return (getFlags() & FlagAccessibility) == FlagProtected;
563 bool isPublic() const {
564 return (getFlags() & FlagAccessibility) == FlagPublic;
566 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
567 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
568 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
569 bool isVirtual() const { return getFlags() & FlagVirtual; }
570 bool isArtificial() const { return getFlags() & FlagArtificial; }
571 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
572 bool isObjcClassComplete() const {
573 return getFlags() & FlagObjcClassComplete;
575 bool isVector() const { return getFlags() & FlagVector; }
576 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
577 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
578 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
579 bool isExternalTypeRef() const { return getFlags() & FlagExternalTypeRef; }
581 DITypeRef getRef() const { return DITypeRef::get(this); }
583 static bool classof(const Metadata *MD) {
584 switch (MD->getMetadataID()) {
587 case DIBasicTypeKind:
588 case DIDerivedTypeKind:
589 case DICompositeTypeKind:
590 case DISubroutineTypeKind:
596 /// \brief Basic type, like 'int' or 'float'.
598 /// TODO: Split out DW_TAG_unspecified_type.
599 /// TODO: Drop unused accessors.
600 class DIBasicType : public DIType {
601 friend class LLVMContextImpl;
606 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
607 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
608 ArrayRef<Metadata *> Ops)
609 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
611 Encoding(Encoding) {}
612 ~DIBasicType() = default;
614 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
615 StringRef Name, uint64_t SizeInBits,
616 uint64_t AlignInBits, unsigned Encoding,
617 StorageType Storage, bool ShouldCreate = true) {
618 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
619 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
621 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
622 MDString *Name, uint64_t SizeInBits,
623 uint64_t AlignInBits, unsigned Encoding,
624 StorageType Storage, bool ShouldCreate = true);
626 TempDIBasicType cloneImpl() const {
627 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
628 getAlignInBits(), getEncoding());
632 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
633 (Tag, Name, 0, 0, 0))
634 DEFINE_MDNODE_GET(DIBasicType,
635 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
636 uint64_t AlignInBits, unsigned Encoding),
637 (Tag, Name, SizeInBits, AlignInBits, Encoding))
638 DEFINE_MDNODE_GET(DIBasicType,
639 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
640 uint64_t AlignInBits, unsigned Encoding),
641 (Tag, Name, SizeInBits, AlignInBits, Encoding))
643 TempDIBasicType clone() const { return cloneImpl(); }
645 unsigned getEncoding() const { return Encoding; }
647 static bool classof(const Metadata *MD) {
648 return MD->getMetadataID() == DIBasicTypeKind;
652 /// \brief Derived types.
654 /// This includes qualified types, pointers, references, friends, typedefs, and
657 /// TODO: Split out members (inheritance, fields, methods, etc.).
658 class DIDerivedType : public DIType {
659 friend class LLVMContextImpl;
662 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
663 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
664 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
665 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
666 AlignInBits, OffsetInBits, Flags, Ops) {}
667 ~DIDerivedType() = default;
669 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
670 StringRef Name, DIFile *File, unsigned Line,
671 DIScopeRef Scope, DITypeRef BaseType,
672 uint64_t SizeInBits, uint64_t AlignInBits,
673 uint64_t OffsetInBits, unsigned Flags,
674 Metadata *ExtraData, StorageType Storage,
675 bool ShouldCreate = true) {
676 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
677 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
678 Flags, ExtraData, Storage, ShouldCreate);
680 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
681 MDString *Name, Metadata *File, unsigned Line,
682 Metadata *Scope, Metadata *BaseType,
683 uint64_t SizeInBits, uint64_t AlignInBits,
684 uint64_t OffsetInBits, unsigned Flags,
685 Metadata *ExtraData, StorageType Storage,
686 bool ShouldCreate = true);
688 TempDIDerivedType cloneImpl() const {
689 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
690 getScope(), getBaseType(), getSizeInBits(),
691 getAlignInBits(), getOffsetInBits(), getFlags(),
696 DEFINE_MDNODE_GET(DIDerivedType,
697 (unsigned Tag, MDString *Name, Metadata *File,
698 unsigned Line, Metadata *Scope, Metadata *BaseType,
699 uint64_t SizeInBits, uint64_t AlignInBits,
700 uint64_t OffsetInBits, unsigned Flags,
701 Metadata *ExtraData = nullptr),
702 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
703 AlignInBits, OffsetInBits, Flags, ExtraData))
704 DEFINE_MDNODE_GET(DIDerivedType,
705 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
706 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
707 uint64_t AlignInBits, uint64_t OffsetInBits,
708 unsigned Flags, Metadata *ExtraData = nullptr),
709 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
710 AlignInBits, OffsetInBits, Flags, ExtraData))
712 TempDIDerivedType clone() const { return cloneImpl(); }
714 //// Get the base type this is derived from.
715 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
716 Metadata *getRawBaseType() const { return getOperand(3); }
718 /// \brief Get extra data associated with this derived type.
720 /// Class type for pointer-to-members, objective-c property node for ivars,
721 /// or global constant wrapper for static members.
723 /// TODO: Separate out types that need this extra operand: pointer-to-member
724 /// types and member fields (static members and ivars).
725 Metadata *getExtraData() const { return getRawExtraData(); }
726 Metadata *getRawExtraData() const { return getOperand(4); }
728 /// \brief Get casted version of extra data.
730 DITypeRef getClassType() const {
731 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
732 return DITypeRef(getExtraData());
734 DIObjCProperty *getObjCProperty() const {
735 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
737 Constant *getConstant() const {
738 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
739 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
740 return C->getValue();
745 static bool classof(const Metadata *MD) {
746 return MD->getMetadataID() == DIDerivedTypeKind;
750 /// \brief Composite types.
752 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
753 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
754 class DICompositeType : public DIType {
755 friend class LLVMContextImpl;
758 unsigned RuntimeLang;
760 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
761 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
762 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
763 ArrayRef<Metadata *> Ops)
764 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
765 AlignInBits, OffsetInBits, Flags, Ops),
766 RuntimeLang(RuntimeLang) {}
767 ~DICompositeType() = default;
769 static DICompositeType *
770 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
771 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
772 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
773 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
774 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
775 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
777 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
778 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
779 RuntimeLang, VTableHolder, TemplateParams.get(),
780 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
782 static DICompositeType *
783 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
784 unsigned Line, Metadata *Scope, Metadata *BaseType,
785 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
786 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
787 Metadata *VTableHolder, Metadata *TemplateParams,
788 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
790 TempDICompositeType cloneImpl() const {
791 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
792 getScope(), getBaseType(), getSizeInBits(),
793 getAlignInBits(), getOffsetInBits(), getFlags(),
794 getElements(), getRuntimeLang(), getVTableHolder(),
795 getTemplateParams(), getIdentifier());
799 DEFINE_MDNODE_GET(DICompositeType,
800 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
801 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
802 uint64_t AlignInBits, uint64_t OffsetInBits,
803 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
804 DITypeRef VTableHolder,
805 DITemplateParameterArray TemplateParams = nullptr,
806 StringRef Identifier = ""),
807 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
808 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
809 VTableHolder, TemplateParams, Identifier))
810 DEFINE_MDNODE_GET(DICompositeType,
811 (unsigned Tag, MDString *Name, Metadata *File,
812 unsigned Line, Metadata *Scope, Metadata *BaseType,
813 uint64_t SizeInBits, uint64_t AlignInBits,
814 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
815 unsigned RuntimeLang, Metadata *VTableHolder,
816 Metadata *TemplateParams = nullptr,
817 MDString *Identifier = nullptr),
818 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
819 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
820 VTableHolder, TemplateParams, Identifier))
822 TempDICompositeType clone() const { return cloneImpl(); }
824 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
825 DINodeArray getElements() const {
826 return cast_or_null<MDTuple>(getRawElements());
828 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
829 DITemplateParameterArray getTemplateParams() const {
830 return cast_or_null<MDTuple>(getRawTemplateParams());
832 StringRef getIdentifier() const { return getStringOperand(7); }
833 unsigned getRuntimeLang() const { return RuntimeLang; }
835 Metadata *getRawBaseType() const { return getOperand(3); }
836 Metadata *getRawElements() const { return getOperand(4); }
837 Metadata *getRawVTableHolder() const { return getOperand(5); }
838 Metadata *getRawTemplateParams() const { return getOperand(6); }
839 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
841 /// \brief Replace operands.
843 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
844 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
845 /// of its movement if necessary.
847 void replaceElements(DINodeArray Elements) {
849 for (DINode *Op : getElements())
850 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
851 "Lost a member during member list replacement");
853 replaceOperandWith(4, Elements.get());
855 void replaceVTableHolder(DITypeRef VTableHolder) {
856 replaceOperandWith(5, VTableHolder);
858 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
859 replaceOperandWith(6, TemplateParams.get());
863 static bool classof(const Metadata *MD) {
864 return MD->getMetadataID() == DICompositeTypeKind;
868 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
870 if (auto *Composite = dyn_cast<DICompositeType>(N))
871 if (auto *S = Composite->getRawIdentifier())
872 return TypedDINodeRef<T>(S);
873 return TypedDINodeRef<T>(N);
876 /// \brief Type array for a subprogram.
878 /// TODO: Fold the array of types in directly as operands.
879 class DISubroutineType : public DIType {
880 friend class LLVMContextImpl;
883 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
884 ArrayRef<Metadata *> Ops)
885 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
886 0, 0, 0, 0, Flags, Ops) {}
887 ~DISubroutineType() = default;
889 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
890 DITypeRefArray TypeArray,
892 bool ShouldCreate = true) {
893 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
895 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
896 Metadata *TypeArray, StorageType Storage,
897 bool ShouldCreate = true);
899 TempDISubroutineType cloneImpl() const {
900 return getTemporary(getContext(), getFlags(), getTypeArray());
904 DEFINE_MDNODE_GET(DISubroutineType,
905 (unsigned Flags, DITypeRefArray TypeArray),
907 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
910 TempDISubroutineType clone() const { return cloneImpl(); }
912 DITypeRefArray getTypeArray() const {
913 return cast_or_null<MDTuple>(getRawTypeArray());
915 Metadata *getRawTypeArray() const { return getOperand(3); }
917 static bool classof(const Metadata *MD) {
918 return MD->getMetadataID() == DISubroutineTypeKind;
922 /// \brief Compile unit.
923 class DICompileUnit : public DIScope {
924 friend class LLVMContextImpl;
927 unsigned SourceLanguage;
929 unsigned RuntimeVersion;
930 unsigned EmissionKind;
933 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
934 bool IsOptimized, unsigned RuntimeVersion,
935 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
936 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
937 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
938 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
940 assert(Storage != Uniqued);
942 ~DICompileUnit() = default;
944 static DICompileUnit *
945 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
946 StringRef Producer, bool IsOptimized, StringRef Flags,
947 unsigned RuntimeVersion, StringRef SplitDebugFilename,
948 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
949 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
950 DIGlobalVariableArray GlobalVariables,
951 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
952 StorageType Storage, bool ShouldCreate = true) {
953 return getImpl(Context, SourceLanguage, File,
954 getCanonicalMDString(Context, Producer), IsOptimized,
955 getCanonicalMDString(Context, Flags), RuntimeVersion,
956 getCanonicalMDString(Context, SplitDebugFilename),
957 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
958 Subprograms.get(), GlobalVariables.get(),
959 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
961 static DICompileUnit *
962 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
963 MDString *Producer, bool IsOptimized, MDString *Flags,
964 unsigned RuntimeVersion, MDString *SplitDebugFilename,
965 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
966 Metadata *Subprograms, Metadata *GlobalVariables,
967 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
968 bool ShouldCreate = true);
970 TempDICompileUnit cloneImpl() const {
972 getContext(), getSourceLanguage(), getFile(), getProducer(),
973 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
974 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
975 getGlobalVariables(), getImportedEntities(), DWOId);
978 static void get() = delete;
979 static void getIfExists() = delete;
982 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
984 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
985 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
986 StringRef SplitDebugFilename, unsigned EmissionKind,
987 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
988 DISubprogramArray Subprograms, DIGlobalVariableArray GlobalVariables,
989 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
990 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
991 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
992 GlobalVariables, ImportedEntities, DWOId))
993 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
995 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
996 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
997 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
998 Metadata *RetainedTypes, Metadata *Subprograms,
999 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1000 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1001 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1002 GlobalVariables, ImportedEntities, DWOId))
1004 TempDICompileUnit clone() const { return cloneImpl(); }
1006 unsigned getSourceLanguage() const { return SourceLanguage; }
1007 bool isOptimized() const { return IsOptimized; }
1008 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1009 unsigned getEmissionKind() const { return EmissionKind; }
1010 StringRef getProducer() const { return getStringOperand(1); }
1011 StringRef getFlags() const { return getStringOperand(2); }
1012 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1013 DICompositeTypeArray getEnumTypes() const {
1014 return cast_or_null<MDTuple>(getRawEnumTypes());
1016 DITypeArray getRetainedTypes() const {
1017 return cast_or_null<MDTuple>(getRawRetainedTypes());
1019 DISubprogramArray getSubprograms() const {
1020 return cast_or_null<MDTuple>(getRawSubprograms());
1022 DIGlobalVariableArray getGlobalVariables() const {
1023 return cast_or_null<MDTuple>(getRawGlobalVariables());
1025 DIImportedEntityArray getImportedEntities() const {
1026 return cast_or_null<MDTuple>(getRawImportedEntities());
1028 unsigned getDWOId() const { return DWOId; }
1030 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1031 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1032 MDString *getRawSplitDebugFilename() const {
1033 return getOperandAs<MDString>(3);
1035 Metadata *getRawEnumTypes() const { return getOperand(4); }
1036 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1037 Metadata *getRawSubprograms() const { return getOperand(6); }
1038 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1039 Metadata *getRawImportedEntities() const { return getOperand(8); }
1041 /// \brief Replace arrays.
1043 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1044 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1045 /// DICompileUnit should be fairly rare.
1047 void replaceEnumTypes(DICompositeTypeArray N) {
1048 replaceOperandWith(4, N.get());
1050 void replaceRetainedTypes(DITypeArray N) {
1051 replaceOperandWith(5, N.get());
1053 void replaceSubprograms(DISubprogramArray N) {
1054 replaceOperandWith(6, N.get());
1056 void replaceGlobalVariables(DIGlobalVariableArray N) {
1057 replaceOperandWith(7, N.get());
1059 void replaceImportedEntities(DIImportedEntityArray N) {
1060 replaceOperandWith(8, N.get());
1064 static bool classof(const Metadata *MD) {
1065 return MD->getMetadataID() == DICompileUnitKind;
1069 /// \brief A scope for locals.
1071 /// A legal scope for lexical blocks, local variables, and debug info
1072 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1073 /// DILexicalBlockFile.
1074 class DILocalScope : public DIScope {
1076 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1077 ArrayRef<Metadata *> Ops)
1078 : DIScope(C, ID, Storage, Tag, Ops) {}
1079 ~DILocalScope() = default;
1082 /// \brief Get the subprogram for this scope.
1084 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1086 DISubprogram *getSubprogram() const;
1088 static bool classof(const Metadata *MD) {
1089 return MD->getMetadataID() == DISubprogramKind ||
1090 MD->getMetadataID() == DILexicalBlockKind ||
1091 MD->getMetadataID() == DILexicalBlockFileKind;
1095 /// \brief Debug location.
1097 /// A debug location in source code, used for debug info and otherwise.
1098 class DILocation : public MDNode {
1099 friend class LLVMContextImpl;
1100 friend class MDNode;
1102 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1103 unsigned Column, ArrayRef<Metadata *> MDs);
1104 ~DILocation() { dropAllReferences(); }
1106 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1107 unsigned Column, Metadata *Scope,
1108 Metadata *InlinedAt, StorageType Storage,
1109 bool ShouldCreate = true);
1110 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1111 unsigned Column, DILocalScope *Scope,
1112 DILocation *InlinedAt, StorageType Storage,
1113 bool ShouldCreate = true) {
1114 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1115 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1118 TempDILocation cloneImpl() const {
1119 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1123 // Disallow replacing operands.
1124 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1127 DEFINE_MDNODE_GET(DILocation,
1128 (unsigned Line, unsigned Column, Metadata *Scope,
1129 Metadata *InlinedAt = nullptr),
1130 (Line, Column, Scope, InlinedAt))
1131 DEFINE_MDNODE_GET(DILocation,
1132 (unsigned Line, unsigned Column, DILocalScope *Scope,
1133 DILocation *InlinedAt = nullptr),
1134 (Line, Column, Scope, InlinedAt))
1136 /// \brief Return a (temporary) clone of this.
1137 TempDILocation clone() const { return cloneImpl(); }
1139 unsigned getLine() const { return SubclassData32; }
1140 unsigned getColumn() const { return SubclassData16; }
1141 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1142 DILocation *getInlinedAt() const {
1143 return cast_or_null<DILocation>(getRawInlinedAt());
1146 DIFile *getFile() const { return getScope()->getFile(); }
1147 StringRef getFilename() const { return getScope()->getFilename(); }
1148 StringRef getDirectory() const { return getScope()->getDirectory(); }
1150 /// \brief Get the scope where this is inlined.
1152 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1154 DILocalScope *getInlinedAtScope() const {
1155 if (auto *IA = getInlinedAt())
1156 return IA->getInlinedAtScope();
1160 /// \brief Check whether this can be discriminated from another location.
1162 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1163 /// Scope and inlined-at chains are not recorded in the linetable, so they
1164 /// cannot be used to distinguish basic blocks.
1166 /// The current implementation is weaker than it should be, since it just
1167 /// checks filename and line.
1169 /// FIXME: Add a check for getDiscriminator().
1170 /// FIXME: Add a check for getColumn().
1171 /// FIXME: Change the getFilename() check to getFile() (or add one for
1172 /// getDirectory()).
1173 bool canDiscriminate(const DILocation &RHS) const {
1174 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1177 /// \brief Get the DWARF discriminator.
1179 /// DWARF discriminators distinguish identical file locations between
1180 /// instructions that are on different basic blocks.
1181 inline unsigned getDiscriminator() const;
1183 /// \brief Compute new discriminator in the given context.
1185 /// This modifies the \a LLVMContext that \c this is in to increment the next
1186 /// discriminator for \c this's line/filename combination.
1188 /// FIXME: Delete this. See comments in implementation and at the only call
1189 /// site in \a AddDiscriminators::runOnFunction().
1190 unsigned computeNewDiscriminator() const;
1192 Metadata *getRawScope() const { return getOperand(0); }
1193 Metadata *getRawInlinedAt() const {
1194 if (getNumOperands() == 2)
1195 return getOperand(1);
1199 static bool classof(const Metadata *MD) {
1200 return MD->getMetadataID() == DILocationKind;
1204 /// \brief Subprogram description.
1206 /// TODO: Remove DisplayName. It's always equal to Name.
1207 /// TODO: Split up flags.
1208 class DISubprogram : public DILocalScope {
1209 friend class LLVMContextImpl;
1210 friend class MDNode;
1214 unsigned Virtuality;
1215 unsigned VirtualIndex;
1221 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1222 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1223 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1224 bool IsOptimized, ArrayRef<Metadata *> Ops)
1225 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1227 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1228 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1229 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1230 ~DISubprogram() = default;
1232 static DISubprogram *
1233 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1234 StringRef LinkageName, DIFile *File, unsigned Line,
1235 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1236 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1237 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1238 Constant *Function, DITemplateParameterArray TemplateParams,
1239 DISubprogram *Declaration, DILocalVariableArray Variables,
1240 StorageType Storage, bool ShouldCreate = true) {
1241 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1242 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1243 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1244 Virtuality, VirtualIndex, Flags, IsOptimized,
1245 Function ? ConstantAsMetadata::get(Function) : nullptr,
1246 TemplateParams.get(), Declaration, Variables.get(), Storage,
1249 static DISubprogram *
1250 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1251 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1252 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1253 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1254 unsigned Flags, bool IsOptimized, Metadata *Function,
1255 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1256 StorageType Storage, bool ShouldCreate = true);
1258 TempDISubprogram cloneImpl() const {
1259 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1260 getFile(), getLine(), getType(), isLocalToUnit(),
1261 isDefinition(), getScopeLine(), getContainingType(),
1262 getVirtuality(), getVirtualIndex(), getFlags(),
1263 isOptimized(), getFunctionConstant(),
1264 getTemplateParams(), getDeclaration(), getVariables());
1268 DEFINE_MDNODE_GET(DISubprogram,
1269 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1270 DIFile *File, unsigned Line, DISubroutineType *Type,
1271 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1272 DITypeRef ContainingType, unsigned Virtuality,
1273 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1274 Constant *Function = nullptr,
1275 DITemplateParameterArray TemplateParams = nullptr,
1276 DISubprogram *Declaration = nullptr,
1277 DILocalVariableArray Variables = nullptr),
1278 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1279 IsDefinition, ScopeLine, ContainingType, Virtuality,
1280 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1281 Declaration, Variables))
1284 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1285 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1286 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1287 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1288 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1289 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1290 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1291 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1292 Function, TemplateParams, Declaration, Variables))
1294 TempDISubprogram clone() const { return cloneImpl(); }
1297 unsigned getLine() const { return Line; }
1298 unsigned getVirtuality() const { return Virtuality; }
1299 unsigned getVirtualIndex() const { return VirtualIndex; }
1300 unsigned getScopeLine() const { return ScopeLine; }
1301 unsigned getFlags() const { return Flags; }
1302 bool isLocalToUnit() const { return IsLocalToUnit; }
1303 bool isDefinition() const { return IsDefinition; }
1304 bool isOptimized() const { return IsOptimized; }
1306 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1307 bool isPrivate() const {
1308 return (getFlags() & FlagAccessibility) == FlagPrivate;
1310 bool isProtected() const {
1311 return (getFlags() & FlagAccessibility) == FlagProtected;
1313 bool isPublic() const {
1314 return (getFlags() & FlagAccessibility) == FlagPublic;
1316 bool isExplicit() const { return getFlags() & FlagExplicit; }
1317 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1319 /// \brief Check if this is reference-qualified.
1321 /// Return true if this subprogram is a C++11 reference-qualified non-static
1322 /// member function (void foo() &).
1323 unsigned isLValueReference() const {
1324 return getFlags() & FlagLValueReference;
1327 /// \brief Check if this is rvalue-reference-qualified.
1329 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1330 /// non-static member function (void foo() &&).
1331 unsigned isRValueReference() const {
1332 return getFlags() & FlagRValueReference;
1335 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1337 StringRef getName() const { return getStringOperand(2); }
1338 StringRef getDisplayName() const { return getStringOperand(3); }
1339 StringRef getLinkageName() const { return getStringOperand(4); }
1341 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1342 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1344 DISubroutineType *getType() const {
1345 return cast_or_null<DISubroutineType>(getRawType());
1347 DITypeRef getContainingType() const {
1348 return DITypeRef(getRawContainingType());
1351 Constant *getFunctionConstant() const {
1352 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1353 return C->getValue();
1356 DITemplateParameterArray getTemplateParams() const {
1357 return cast_or_null<MDTuple>(getRawTemplateParams());
1359 DISubprogram *getDeclaration() const {
1360 return cast_or_null<DISubprogram>(getRawDeclaration());
1362 DILocalVariableArray getVariables() const {
1363 return cast_or_null<MDTuple>(getRawVariables());
1366 Metadata *getRawScope() const { return getOperand(1); }
1367 Metadata *getRawType() const { return getOperand(5); }
1368 Metadata *getRawContainingType() const { return getOperand(6); }
1369 Metadata *getRawFunction() const { return getOperand(7); }
1370 Metadata *getRawTemplateParams() const { return getOperand(8); }
1371 Metadata *getRawDeclaration() const { return getOperand(9); }
1372 Metadata *getRawVariables() const { return getOperand(10); }
1374 /// \brief Get a pointer to the function this subprogram describes.
1376 /// This dyn_casts \a getFunctionConstant() to \a Function.
1378 /// FIXME: Should this be looking through bitcasts?
1379 Function *getFunction() const;
1381 /// \brief Replace the function.
1383 /// If \a isUniqued() and not \a isResolved(), this could node will be
1384 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1385 /// that's a problem.
1387 void replaceFunction(Function *F);
1388 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1389 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1392 /// \brief Check if this subprogram describes the given function.
1394 /// FIXME: Should this be looking through bitcasts?
1395 bool describes(const Function *F) const;
1397 static bool classof(const Metadata *MD) {
1398 return MD->getMetadataID() == DISubprogramKind;
1402 class DILexicalBlockBase : public DILocalScope {
1404 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1405 ArrayRef<Metadata *> Ops)
1406 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1407 ~DILexicalBlockBase() = default;
1410 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1412 Metadata *getRawScope() const { return getOperand(1); }
1414 /// \brief Forwarding accessors to LexicalBlock.
1416 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1418 inline unsigned getLine() const;
1419 inline unsigned getColumn() const;
1421 static bool classof(const Metadata *MD) {
1422 return MD->getMetadataID() == DILexicalBlockKind ||
1423 MD->getMetadataID() == DILexicalBlockFileKind;
1427 class DILexicalBlock : public DILexicalBlockBase {
1428 friend class LLVMContextImpl;
1429 friend class MDNode;
1434 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1435 unsigned Column, ArrayRef<Metadata *> Ops)
1436 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1438 ~DILexicalBlock() = default;
1440 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1441 DIFile *File, unsigned Line, unsigned Column,
1442 StorageType Storage,
1443 bool ShouldCreate = true) {
1444 return getImpl(Context, static_cast<Metadata *>(Scope),
1445 static_cast<Metadata *>(File), Line, Column, Storage,
1449 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1450 Metadata *File, unsigned Line, unsigned Column,
1451 StorageType Storage, bool ShouldCreate = true);
1453 TempDILexicalBlock cloneImpl() const {
1454 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1459 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1460 unsigned Line, unsigned Column),
1461 (Scope, File, Line, Column))
1462 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1463 unsigned Line, unsigned Column),
1464 (Scope, File, Line, Column))
1466 TempDILexicalBlock clone() const { return cloneImpl(); }
1468 unsigned getLine() const { return Line; }
1469 unsigned getColumn() const { return Column; }
1471 static bool classof(const Metadata *MD) {
1472 return MD->getMetadataID() == DILexicalBlockKind;
1476 unsigned DILexicalBlockBase::getLine() const {
1477 if (auto *N = dyn_cast<DILexicalBlock>(this))
1478 return N->getLine();
1482 unsigned DILexicalBlockBase::getColumn() const {
1483 if (auto *N = dyn_cast<DILexicalBlock>(this))
1484 return N->getColumn();
1488 class DILexicalBlockFile : public DILexicalBlockBase {
1489 friend class LLVMContextImpl;
1490 friend class MDNode;
1492 unsigned Discriminator;
1494 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1495 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1496 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1497 Discriminator(Discriminator) {}
1498 ~DILexicalBlockFile() = default;
1500 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1501 DIFile *File, unsigned Discriminator,
1502 StorageType Storage,
1503 bool ShouldCreate = true) {
1504 return getImpl(Context, static_cast<Metadata *>(Scope),
1505 static_cast<Metadata *>(File), Discriminator, Storage,
1509 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1510 Metadata *File, unsigned Discriminator,
1511 StorageType Storage,
1512 bool ShouldCreate = true);
1514 TempDILexicalBlockFile cloneImpl() const {
1515 return getTemporary(getContext(), getScope(), getFile(),
1516 getDiscriminator());
1520 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1521 unsigned Discriminator),
1522 (Scope, File, Discriminator))
1523 DEFINE_MDNODE_GET(DILexicalBlockFile,
1524 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1525 (Scope, File, Discriminator))
1527 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1529 // TODO: Remove these once they're gone from DILexicalBlockBase.
1530 unsigned getLine() const = delete;
1531 unsigned getColumn() const = delete;
1533 unsigned getDiscriminator() const { return Discriminator; }
1535 static bool classof(const Metadata *MD) {
1536 return MD->getMetadataID() == DILexicalBlockFileKind;
1540 unsigned DILocation::getDiscriminator() const {
1541 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1542 return F->getDiscriminator();
1546 class DINamespace : public DIScope {
1547 friend class LLVMContextImpl;
1548 friend class MDNode;
1552 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1553 ArrayRef<Metadata *> Ops)
1554 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1557 ~DINamespace() = default;
1559 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1560 DIFile *File, StringRef Name, unsigned Line,
1561 StorageType Storage, bool ShouldCreate = true) {
1562 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1563 Line, Storage, ShouldCreate);
1565 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1566 Metadata *File, MDString *Name, unsigned Line,
1567 StorageType Storage, bool ShouldCreate = true);
1569 TempDINamespace cloneImpl() const {
1570 return getTemporary(getContext(), getScope(), getFile(), getName(),
1575 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1577 (Scope, File, Name, Line))
1578 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1579 MDString *Name, unsigned Line),
1580 (Scope, File, Name, Line))
1582 TempDINamespace clone() const { return cloneImpl(); }
1584 unsigned getLine() const { return Line; }
1585 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1586 StringRef getName() const { return getStringOperand(2); }
1588 Metadata *getRawScope() const { return getOperand(1); }
1589 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1591 static bool classof(const Metadata *MD) {
1592 return MD->getMetadataID() == DINamespaceKind;
1596 /// \brief A (clang) module that has been imported by the compile unit.
1598 class DIModule : public DIScope {
1599 friend class LLVMContextImpl;
1600 friend class MDNode;
1602 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1603 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1606 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1607 StringRef Name, StringRef ConfigurationMacros,
1608 StringRef IncludePath, StringRef ISysRoot,
1609 StorageType Storage, bool ShouldCreate = true) {
1610 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1611 getCanonicalMDString(Context, ConfigurationMacros),
1612 getCanonicalMDString(Context, IncludePath),
1613 getCanonicalMDString(Context, ISysRoot),
1614 Storage, ShouldCreate);
1616 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1617 MDString *Name, MDString *ConfigurationMacros,
1618 MDString *IncludePath, MDString *ISysRoot,
1619 StorageType Storage, bool ShouldCreate = true);
1621 TempDIModule cloneImpl() const {
1622 return getTemporary(getContext(), getScope(), getName(),
1623 getConfigurationMacros(), getIncludePath(),
1628 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1629 StringRef ConfigurationMacros, StringRef IncludePath,
1630 StringRef ISysRoot),
1631 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1632 DEFINE_MDNODE_GET(DIModule,
1633 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1634 MDString *IncludePath, MDString *ISysRoot),
1635 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1637 TempDIModule clone() const { return cloneImpl(); }
1639 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1640 StringRef getName() const { return getStringOperand(1); }
1641 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1642 StringRef getIncludePath() const { return getStringOperand(3); }
1643 StringRef getISysRoot() const { return getStringOperand(4); }
1645 Metadata *getRawScope() const { return getOperand(0); }
1646 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1647 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1648 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1649 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1651 static bool classof(const Metadata *MD) {
1652 return MD->getMetadataID() == DIModuleKind;
1656 /// \brief Base class for template parameters.
1657 class DITemplateParameter : public DINode {
1659 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1660 unsigned Tag, ArrayRef<Metadata *> Ops)
1661 : DINode(Context, ID, Storage, Tag, Ops) {}
1662 ~DITemplateParameter() = default;
1665 StringRef getName() const { return getStringOperand(0); }
1666 DITypeRef getType() const { return DITypeRef(getRawType()); }
1668 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1669 Metadata *getRawType() const { return getOperand(1); }
1671 static bool classof(const Metadata *MD) {
1672 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1673 MD->getMetadataID() == DITemplateValueParameterKind;
1677 class DITemplateTypeParameter : public DITemplateParameter {
1678 friend class LLVMContextImpl;
1679 friend class MDNode;
1681 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1682 ArrayRef<Metadata *> Ops)
1683 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1684 dwarf::DW_TAG_template_type_parameter, Ops) {}
1685 ~DITemplateTypeParameter() = default;
1687 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1688 DITypeRef Type, StorageType Storage,
1689 bool ShouldCreate = true) {
1690 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1693 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1694 Metadata *Type, StorageType Storage,
1695 bool ShouldCreate = true);
1697 TempDITemplateTypeParameter cloneImpl() const {
1698 return getTemporary(getContext(), getName(), getType());
1702 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1704 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1707 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1709 static bool classof(const Metadata *MD) {
1710 return MD->getMetadataID() == DITemplateTypeParameterKind;
1714 class DITemplateValueParameter : public DITemplateParameter {
1715 friend class LLVMContextImpl;
1716 friend class MDNode;
1718 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1719 unsigned Tag, ArrayRef<Metadata *> Ops)
1720 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1722 ~DITemplateValueParameter() = default;
1724 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1725 StringRef Name, DITypeRef Type,
1726 Metadata *Value, StorageType Storage,
1727 bool ShouldCreate = true) {
1728 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1729 Value, Storage, ShouldCreate);
1731 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1732 MDString *Name, Metadata *Type,
1733 Metadata *Value, StorageType Storage,
1734 bool ShouldCreate = true);
1736 TempDITemplateValueParameter cloneImpl() const {
1737 return getTemporary(getContext(), getTag(), getName(), getType(),
1742 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1743 DITypeRef Type, Metadata *Value),
1744 (Tag, Name, Type, Value))
1745 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1746 Metadata *Type, Metadata *Value),
1747 (Tag, Name, Type, Value))
1749 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1751 Metadata *getValue() const { return getOperand(2); }
1753 static bool classof(const Metadata *MD) {
1754 return MD->getMetadataID() == DITemplateValueParameterKind;
1758 /// \brief Base class for variables.
1759 class DIVariable : public DINode {
1763 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
1764 ArrayRef<Metadata *> Ops)
1765 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line) {}
1766 ~DIVariable() = default;
1769 unsigned getLine() const { return Line; }
1770 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1771 StringRef getName() const { return getStringOperand(1); }
1772 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1773 DITypeRef getType() const { return DITypeRef(getRawType()); }
1775 StringRef getFilename() const {
1776 if (auto *F = getFile())
1777 return F->getFilename();
1780 StringRef getDirectory() const {
1781 if (auto *F = getFile())
1782 return F->getDirectory();
1786 Metadata *getRawScope() const { return getOperand(0); }
1787 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1788 Metadata *getRawFile() const { return getOperand(2); }
1789 Metadata *getRawType() const { return getOperand(3); }
1791 static bool classof(const Metadata *MD) {
1792 return MD->getMetadataID() == DILocalVariableKind ||
1793 MD->getMetadataID() == DIGlobalVariableKind;
1797 /// \brief Global variables.
1799 /// TODO: Remove DisplayName. It's always equal to Name.
1800 class DIGlobalVariable : public DIVariable {
1801 friend class LLVMContextImpl;
1802 friend class MDNode;
1807 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1808 bool IsLocalToUnit, bool IsDefinition,
1809 ArrayRef<Metadata *> Ops)
1810 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops),
1811 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1812 ~DIGlobalVariable() = default;
1814 static DIGlobalVariable *
1815 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1816 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1817 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1818 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1819 bool ShouldCreate = true) {
1820 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1821 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1822 IsLocalToUnit, IsDefinition,
1823 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1824 StaticDataMemberDeclaration, Storage, ShouldCreate);
1826 static DIGlobalVariable *
1827 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1828 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1829 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1830 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1831 bool ShouldCreate = true);
1833 TempDIGlobalVariable cloneImpl() const {
1834 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1835 getFile(), getLine(), getType(), isLocalToUnit(),
1836 isDefinition(), getVariable(),
1837 getStaticDataMemberDeclaration());
1841 DEFINE_MDNODE_GET(DIGlobalVariable,
1842 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1843 DIFile *File, unsigned Line, DITypeRef Type,
1844 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1845 DIDerivedType *StaticDataMemberDeclaration),
1846 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1847 IsDefinition, Variable, StaticDataMemberDeclaration))
1848 DEFINE_MDNODE_GET(DIGlobalVariable,
1849 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1850 Metadata *File, unsigned Line, Metadata *Type,
1851 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1852 Metadata *StaticDataMemberDeclaration),
1853 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1854 IsDefinition, Variable, StaticDataMemberDeclaration))
1856 TempDIGlobalVariable clone() const { return cloneImpl(); }
1858 bool isLocalToUnit() const { return IsLocalToUnit; }
1859 bool isDefinition() const { return IsDefinition; }
1860 StringRef getDisplayName() const { return getStringOperand(4); }
1861 StringRef getLinkageName() const { return getStringOperand(5); }
1862 Constant *getVariable() const {
1863 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1864 return dyn_cast<Constant>(C->getValue());
1867 DIDerivedType *getStaticDataMemberDeclaration() const {
1868 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1871 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1872 Metadata *getRawVariable() const { return getOperand(6); }
1873 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1875 static bool classof(const Metadata *MD) {
1876 return MD->getMetadataID() == DIGlobalVariableKind;
1880 /// \brief Local variable.
1882 /// TODO: Split up flags.
1883 class DILocalVariable : public DIVariable {
1884 friend class LLVMContextImpl;
1885 friend class MDNode;
1890 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1891 unsigned Arg, unsigned Flags, ArrayRef<Metadata *> Ops)
1892 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops), Arg(Arg),
1894 ~DILocalVariable() = default;
1896 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
1897 StringRef Name, DIFile *File, unsigned Line,
1898 DITypeRef Type, unsigned Arg, unsigned Flags,
1899 StorageType Storage,
1900 bool ShouldCreate = true) {
1901 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
1902 Line, Type, Arg, Flags, Storage, ShouldCreate);
1904 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
1905 MDString *Name, Metadata *File, unsigned Line,
1906 Metadata *Type, unsigned Arg, unsigned Flags,
1907 StorageType Storage,
1908 bool ShouldCreate = true);
1910 TempDILocalVariable cloneImpl() const {
1911 return getTemporary(getContext(), getScope(), getName(), getFile(),
1912 getLine(), getType(), getArg(), getFlags());
1916 DEFINE_MDNODE_GET(DILocalVariable,
1917 (DILocalScope * Scope, StringRef Name, DIFile *File,
1918 unsigned Line, DITypeRef Type, unsigned Arg,
1920 (Scope, Name, File, Line, Type, Arg, Flags))
1921 DEFINE_MDNODE_GET(DILocalVariable,
1922 (Metadata * Scope, MDString *Name, Metadata *File,
1923 unsigned Line, Metadata *Type, unsigned Arg,
1925 (Scope, Name, File, Line, Type, Arg, Flags))
1927 TempDILocalVariable clone() const { return cloneImpl(); }
1929 /// \brief Get the local scope for this variable.
1931 /// Variables must be defined in a local scope.
1932 DILocalScope *getScope() const {
1933 return cast<DILocalScope>(DIVariable::getScope());
1936 bool isParameter() const { return Arg; }
1937 unsigned getArg() const { return Arg; }
1938 unsigned getFlags() const { return Flags; }
1940 bool isArtificial() const { return getFlags() & FlagArtificial; }
1941 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1943 /// \brief Check that a location is valid for this variable.
1945 /// Check that \c DL exists, is in the same subprogram, and has the same
1946 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
1947 /// to a \a DbgInfoIntrinsic.)
1948 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1949 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1952 static bool classof(const Metadata *MD) {
1953 return MD->getMetadataID() == DILocalVariableKind;
1957 /// \brief DWARF expression.
1959 /// This is (almost) a DWARF expression that modifies the location of a
1960 /// variable or (or the location of a single piece of a variable).
1962 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1963 /// and have DW_OP_plus consume the topmost elements on the stack.
1965 /// TODO: Co-allocate the expression elements.
1966 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1968 class DIExpression : public MDNode {
1969 friend class LLVMContextImpl;
1970 friend class MDNode;
1972 std::vector<uint64_t> Elements;
1974 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1975 : MDNode(C, DIExpressionKind, Storage, None),
1976 Elements(Elements.begin(), Elements.end()) {}
1977 ~DIExpression() = default;
1979 static DIExpression *getImpl(LLVMContext &Context,
1980 ArrayRef<uint64_t> Elements, StorageType Storage,
1981 bool ShouldCreate = true);
1983 TempDIExpression cloneImpl() const {
1984 return getTemporary(getContext(), getElements());
1988 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1990 TempDIExpression clone() const { return cloneImpl(); }
1992 ArrayRef<uint64_t> getElements() const { return Elements; }
1994 unsigned getNumElements() const { return Elements.size(); }
1995 uint64_t getElement(unsigned I) const {
1996 assert(I < Elements.size() && "Index out of range");
2000 /// \brief Return whether this is a piece of an aggregate variable.
2001 bool isBitPiece() const;
2003 /// \brief Return the offset of this piece in bits.
2004 uint64_t getBitPieceOffset() const;
2006 /// \brief Return the size of this piece in bits.
2007 uint64_t getBitPieceSize() const;
2009 typedef ArrayRef<uint64_t>::iterator element_iterator;
2010 element_iterator elements_begin() const { return getElements().begin(); }
2011 element_iterator elements_end() const { return getElements().end(); }
2013 /// \brief A lightweight wrapper around an expression operand.
2015 /// TODO: Store arguments directly and change \a DIExpression to store a
2021 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2023 const uint64_t *get() const { return Op; }
2025 /// \brief Get the operand code.
2026 uint64_t getOp() const { return *Op; }
2028 /// \brief Get an argument to the operand.
2030 /// Never returns the operand itself.
2031 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2033 unsigned getNumArgs() const { return getSize() - 1; }
2035 /// \brief Return the size of the operand.
2037 /// Return the number of elements in the operand (1 + args).
2038 unsigned getSize() const;
2041 /// \brief An iterator for expression operands.
2042 class expr_op_iterator
2043 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2047 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2049 element_iterator getBase() const { return Op.get(); }
2050 const ExprOperand &operator*() const { return Op; }
2051 const ExprOperand *operator->() const { return &Op; }
2053 expr_op_iterator &operator++() {
2057 expr_op_iterator operator++(int) {
2058 expr_op_iterator T(*this);
2063 /// \brief Get the next iterator.
2065 /// \a std::next() doesn't work because this is technically an
2066 /// input_iterator, but it's a perfectly valid operation. This is an
2067 /// accessor to provide the same functionality.
2068 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2070 bool operator==(const expr_op_iterator &X) const {
2071 return getBase() == X.getBase();
2073 bool operator!=(const expr_op_iterator &X) const {
2074 return getBase() != X.getBase();
2078 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2081 /// \brief Visit the elements via ExprOperand wrappers.
2083 /// These range iterators visit elements through \a ExprOperand wrappers.
2084 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2087 /// \pre \a isValid() gives \c true.
2089 expr_op_iterator expr_op_begin() const {
2090 return expr_op_iterator(elements_begin());
2092 expr_op_iterator expr_op_end() const {
2093 return expr_op_iterator(elements_end());
2097 bool isValid() const;
2099 static bool classof(const Metadata *MD) {
2100 return MD->getMetadataID() == DIExpressionKind;
2104 class DIObjCProperty : public DINode {
2105 friend class LLVMContextImpl;
2106 friend class MDNode;
2109 unsigned Attributes;
2111 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2112 unsigned Attributes, ArrayRef<Metadata *> Ops)
2113 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2115 Line(Line), Attributes(Attributes) {}
2116 ~DIObjCProperty() = default;
2118 static DIObjCProperty *
2119 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2120 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2121 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2122 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2123 getCanonicalMDString(Context, GetterName),
2124 getCanonicalMDString(Context, SetterName), Attributes, Type,
2125 Storage, ShouldCreate);
2127 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2128 Metadata *File, unsigned Line,
2129 MDString *GetterName, MDString *SetterName,
2130 unsigned Attributes, Metadata *Type,
2131 StorageType Storage, bool ShouldCreate = true);
2133 TempDIObjCProperty cloneImpl() const {
2134 return getTemporary(getContext(), getName(), getFile(), getLine(),
2135 getGetterName(), getSetterName(), getAttributes(),
2140 DEFINE_MDNODE_GET(DIObjCProperty,
2141 (StringRef Name, DIFile *File, unsigned Line,
2142 StringRef GetterName, StringRef SetterName,
2143 unsigned Attributes, DITypeRef Type),
2144 (Name, File, Line, GetterName, SetterName, Attributes,
2146 DEFINE_MDNODE_GET(DIObjCProperty,
2147 (MDString * Name, Metadata *File, unsigned Line,
2148 MDString *GetterName, MDString *SetterName,
2149 unsigned Attributes, Metadata *Type),
2150 (Name, File, Line, GetterName, SetterName, Attributes,
2153 TempDIObjCProperty clone() const { return cloneImpl(); }
2155 unsigned getLine() const { return Line; }
2156 unsigned getAttributes() const { return Attributes; }
2157 StringRef getName() const { return getStringOperand(0); }
2158 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2159 StringRef getGetterName() const { return getStringOperand(2); }
2160 StringRef getSetterName() const { return getStringOperand(3); }
2161 DITypeRef getType() const { return DITypeRef(getRawType()); }
2163 StringRef getFilename() const {
2164 if (auto *F = getFile())
2165 return F->getFilename();
2168 StringRef getDirectory() const {
2169 if (auto *F = getFile())
2170 return F->getDirectory();
2174 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2175 Metadata *getRawFile() const { return getOperand(1); }
2176 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2177 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2178 Metadata *getRawType() const { return getOperand(4); }
2180 static bool classof(const Metadata *MD) {
2181 return MD->getMetadataID() == DIObjCPropertyKind;
2185 /// \brief An imported module (C++ using directive or similar).
2186 class DIImportedEntity : public DINode {
2187 friend class LLVMContextImpl;
2188 friend class MDNode;
2192 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2193 unsigned Line, ArrayRef<Metadata *> Ops)
2194 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2195 ~DIImportedEntity() = default;
2197 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2198 DIScope *Scope, DINodeRef Entity,
2199 unsigned Line, StringRef Name,
2200 StorageType Storage,
2201 bool ShouldCreate = true) {
2202 return getImpl(Context, Tag, Scope, Entity, Line,
2203 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2205 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2206 Metadata *Scope, Metadata *Entity,
2207 unsigned Line, MDString *Name,
2208 StorageType Storage,
2209 bool ShouldCreate = true);
2211 TempDIImportedEntity cloneImpl() const {
2212 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2213 getLine(), getName());
2217 DEFINE_MDNODE_GET(DIImportedEntity,
2218 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2219 unsigned Line, StringRef Name = ""),
2220 (Tag, Scope, Entity, Line, Name))
2221 DEFINE_MDNODE_GET(DIImportedEntity,
2222 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2223 unsigned Line, MDString *Name),
2224 (Tag, Scope, Entity, Line, Name))
2226 TempDIImportedEntity clone() const { return cloneImpl(); }
2228 unsigned getLine() const { return Line; }
2229 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2230 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2231 StringRef getName() const { return getStringOperand(2); }
2233 Metadata *getRawScope() const { return getOperand(0); }
2234 Metadata *getRawEntity() const { return getOperand(1); }
2235 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2237 static bool classof(const Metadata *MD) {
2238 return MD->getMetadataID() == DIImportedEntityKind;
2242 } // end namespace llvm
2244 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2245 #undef DEFINE_MDNODE_GET_UNPACK
2246 #undef DEFINE_MDNODE_GET