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; }
577 bool isExternalTypeRef() const { return getFlags() & FlagExternalTypeRef; }
579 DITypeRef getRef() const { return DITypeRef::get(this); }
581 static bool classof(const Metadata *MD) {
582 switch (MD->getMetadataID()) {
585 case DIBasicTypeKind:
586 case DIDerivedTypeKind:
587 case DICompositeTypeKind:
588 case DISubroutineTypeKind:
594 /// \brief Basic type, like 'int' or 'float'.
596 /// TODO: Split out DW_TAG_unspecified_type.
597 /// TODO: Drop unused accessors.
598 class DIBasicType : public DIType {
599 friend class LLVMContextImpl;
604 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
605 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
606 ArrayRef<Metadata *> Ops)
607 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
609 Encoding(Encoding) {}
610 ~DIBasicType() = default;
612 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
613 StringRef Name, uint64_t SizeInBits,
614 uint64_t AlignInBits, unsigned Encoding,
615 StorageType Storage, bool ShouldCreate = true) {
616 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
617 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
619 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
620 MDString *Name, uint64_t SizeInBits,
621 uint64_t AlignInBits, unsigned Encoding,
622 StorageType Storage, bool ShouldCreate = true);
624 TempDIBasicType cloneImpl() const {
625 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
626 getAlignInBits(), getEncoding());
630 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
631 (Tag, Name, 0, 0, 0))
632 DEFINE_MDNODE_GET(DIBasicType,
633 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
634 uint64_t AlignInBits, unsigned Encoding),
635 (Tag, Name, SizeInBits, AlignInBits, Encoding))
636 DEFINE_MDNODE_GET(DIBasicType,
637 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
638 uint64_t AlignInBits, unsigned Encoding),
639 (Tag, Name, SizeInBits, AlignInBits, Encoding))
641 TempDIBasicType clone() const { return cloneImpl(); }
643 unsigned getEncoding() const { return Encoding; }
645 static bool classof(const Metadata *MD) {
646 return MD->getMetadataID() == DIBasicTypeKind;
650 /// \brief Derived types.
652 /// This includes qualified types, pointers, references, friends, typedefs, and
655 /// TODO: Split out members (inheritance, fields, methods, etc.).
656 class DIDerivedType : public DIType {
657 friend class LLVMContextImpl;
660 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
661 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
662 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
663 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
664 AlignInBits, OffsetInBits, Flags, Ops) {}
665 ~DIDerivedType() = default;
667 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
668 StringRef Name, DIFile *File, unsigned Line,
669 DIScopeRef Scope, DITypeRef BaseType,
670 uint64_t SizeInBits, uint64_t AlignInBits,
671 uint64_t OffsetInBits, unsigned Flags,
672 Metadata *ExtraData, StorageType Storage,
673 bool ShouldCreate = true) {
674 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
675 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
676 Flags, ExtraData, Storage, ShouldCreate);
678 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
679 MDString *Name, Metadata *File, unsigned Line,
680 Metadata *Scope, Metadata *BaseType,
681 uint64_t SizeInBits, uint64_t AlignInBits,
682 uint64_t OffsetInBits, unsigned Flags,
683 Metadata *ExtraData, StorageType Storage,
684 bool ShouldCreate = true);
686 TempDIDerivedType cloneImpl() const {
687 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
688 getScope(), getBaseType(), getSizeInBits(),
689 getAlignInBits(), getOffsetInBits(), getFlags(),
694 DEFINE_MDNODE_GET(DIDerivedType,
695 (unsigned Tag, MDString *Name, Metadata *File,
696 unsigned Line, Metadata *Scope, Metadata *BaseType,
697 uint64_t SizeInBits, uint64_t AlignInBits,
698 uint64_t OffsetInBits, unsigned Flags,
699 Metadata *ExtraData = nullptr),
700 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
701 AlignInBits, OffsetInBits, Flags, ExtraData))
702 DEFINE_MDNODE_GET(DIDerivedType,
703 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
704 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
705 uint64_t AlignInBits, uint64_t OffsetInBits,
706 unsigned Flags, Metadata *ExtraData = nullptr),
707 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
708 AlignInBits, OffsetInBits, Flags, ExtraData))
710 TempDIDerivedType clone() const { return cloneImpl(); }
712 //// Get the base type this is derived from.
713 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
714 Metadata *getRawBaseType() const { return getOperand(3); }
716 /// \brief Get extra data associated with this derived type.
718 /// Class type for pointer-to-members, objective-c property node for ivars,
719 /// or global constant wrapper for static members.
721 /// TODO: Separate out types that need this extra operand: pointer-to-member
722 /// types and member fields (static members and ivars).
723 Metadata *getExtraData() const { return getRawExtraData(); }
724 Metadata *getRawExtraData() const { return getOperand(4); }
726 /// \brief Get casted version of extra data.
728 DITypeRef getClassType() const {
729 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
730 return DITypeRef(getExtraData());
732 DIObjCProperty *getObjCProperty() const {
733 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
735 Constant *getConstant() const {
736 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
737 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
738 return C->getValue();
743 static bool classof(const Metadata *MD) {
744 return MD->getMetadataID() == DIDerivedTypeKind;
748 /// \brief Composite types.
750 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
751 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
752 class DICompositeType : public DIType {
753 friend class LLVMContextImpl;
756 unsigned RuntimeLang;
758 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
759 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
760 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
761 ArrayRef<Metadata *> Ops)
762 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
763 AlignInBits, OffsetInBits, Flags, Ops),
764 RuntimeLang(RuntimeLang) {}
765 ~DICompositeType() = default;
767 static DICompositeType *
768 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
769 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
770 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
771 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
772 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
773 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
775 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
776 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
777 RuntimeLang, VTableHolder, TemplateParams.get(),
778 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
780 static DICompositeType *
781 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
782 unsigned Line, Metadata *Scope, Metadata *BaseType,
783 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
784 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
785 Metadata *VTableHolder, Metadata *TemplateParams,
786 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
788 TempDICompositeType cloneImpl() const {
789 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
790 getScope(), getBaseType(), getSizeInBits(),
791 getAlignInBits(), getOffsetInBits(), getFlags(),
792 getElements(), getRuntimeLang(), getVTableHolder(),
793 getTemplateParams(), getIdentifier());
797 DEFINE_MDNODE_GET(DICompositeType,
798 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
799 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
800 uint64_t AlignInBits, uint64_t OffsetInBits,
801 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
802 DITypeRef VTableHolder,
803 DITemplateParameterArray TemplateParams = nullptr,
804 StringRef Identifier = ""),
805 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
806 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
807 VTableHolder, TemplateParams, Identifier))
808 DEFINE_MDNODE_GET(DICompositeType,
809 (unsigned Tag, MDString *Name, Metadata *File,
810 unsigned Line, Metadata *Scope, Metadata *BaseType,
811 uint64_t SizeInBits, uint64_t AlignInBits,
812 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
813 unsigned RuntimeLang, Metadata *VTableHolder,
814 Metadata *TemplateParams = nullptr,
815 MDString *Identifier = nullptr),
816 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
817 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
818 VTableHolder, TemplateParams, Identifier))
820 TempDICompositeType clone() const { return cloneImpl(); }
822 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
823 DINodeArray getElements() const {
824 return cast_or_null<MDTuple>(getRawElements());
826 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
827 DITemplateParameterArray getTemplateParams() const {
828 return cast_or_null<MDTuple>(getRawTemplateParams());
830 StringRef getIdentifier() const { return getStringOperand(7); }
831 unsigned getRuntimeLang() const { return RuntimeLang; }
833 Metadata *getRawBaseType() const { return getOperand(3); }
834 Metadata *getRawElements() const { return getOperand(4); }
835 Metadata *getRawVTableHolder() const { return getOperand(5); }
836 Metadata *getRawTemplateParams() const { return getOperand(6); }
837 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
839 /// \brief Replace operands.
841 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
842 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
843 /// of its movement if necessary.
845 void replaceElements(DINodeArray Elements) {
847 for (DINode *Op : getElements())
848 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
849 "Lost a member during member list replacement");
851 replaceOperandWith(4, Elements.get());
853 void replaceVTableHolder(DITypeRef VTableHolder) {
854 replaceOperandWith(5, VTableHolder);
856 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
857 replaceOperandWith(6, TemplateParams.get());
861 static bool classof(const Metadata *MD) {
862 return MD->getMetadataID() == DICompositeTypeKind;
866 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
868 if (auto *Composite = dyn_cast<DICompositeType>(N))
869 if (auto *S = Composite->getRawIdentifier())
870 return TypedDINodeRef<T>(S);
871 return TypedDINodeRef<T>(N);
874 /// \brief Type array for a subprogram.
876 /// TODO: Fold the array of types in directly as operands.
877 class DISubroutineType : public DIType {
878 friend class LLVMContextImpl;
881 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
882 ArrayRef<Metadata *> Ops)
883 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
884 0, 0, 0, 0, Flags, Ops) {}
885 ~DISubroutineType() = default;
887 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
888 DITypeRefArray TypeArray,
890 bool ShouldCreate = true) {
891 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
893 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
894 Metadata *TypeArray, StorageType Storage,
895 bool ShouldCreate = true);
897 TempDISubroutineType cloneImpl() const {
898 return getTemporary(getContext(), getFlags(), getTypeArray());
902 DEFINE_MDNODE_GET(DISubroutineType,
903 (unsigned Flags, DITypeRefArray TypeArray),
905 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
908 TempDISubroutineType clone() const { return cloneImpl(); }
910 DITypeRefArray getTypeArray() const {
911 return cast_or_null<MDTuple>(getRawTypeArray());
913 Metadata *getRawTypeArray() const { return getOperand(3); }
915 static bool classof(const Metadata *MD) {
916 return MD->getMetadataID() == DISubroutineTypeKind;
920 /// \brief Compile unit.
921 class DICompileUnit : public DIScope {
922 friend class LLVMContextImpl;
925 unsigned SourceLanguage;
927 unsigned RuntimeVersion;
928 unsigned EmissionKind;
931 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
932 bool IsOptimized, unsigned RuntimeVersion,
933 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
934 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
935 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
936 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
938 ~DICompileUnit() = default;
940 static DICompileUnit *
941 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
942 StringRef Producer, bool IsOptimized, StringRef Flags,
943 unsigned RuntimeVersion, StringRef SplitDebugFilename,
944 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
945 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
946 DIGlobalVariableArray GlobalVariables,
947 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
948 StorageType Storage, bool ShouldCreate = true) {
949 return getImpl(Context, SourceLanguage, File,
950 getCanonicalMDString(Context, Producer), IsOptimized,
951 getCanonicalMDString(Context, Flags), RuntimeVersion,
952 getCanonicalMDString(Context, SplitDebugFilename),
953 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
954 Subprograms.get(), GlobalVariables.get(),
955 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
957 static DICompileUnit *
958 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
959 MDString *Producer, bool IsOptimized, MDString *Flags,
960 unsigned RuntimeVersion, MDString *SplitDebugFilename,
961 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
962 Metadata *Subprograms, Metadata *GlobalVariables,
963 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
964 bool ShouldCreate = true);
966 TempDICompileUnit cloneImpl() const {
968 getContext(), getSourceLanguage(), getFile(), getProducer(),
969 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
970 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
971 getGlobalVariables(), getImportedEntities(), DWOId);
975 DEFINE_MDNODE_GET(DICompileUnit,
976 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
977 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
978 StringRef SplitDebugFilename, unsigned EmissionKind,
979 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
980 DISubprogramArray Subprograms,
981 DIGlobalVariableArray GlobalVariables,
982 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
983 (SourceLanguage, File, Producer, IsOptimized, Flags,
984 RuntimeVersion, SplitDebugFilename, EmissionKind,
985 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
986 ImportedEntities, DWOId))
989 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
990 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
991 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
992 Metadata *RetainedTypes, Metadata *Subprograms,
993 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
994 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
995 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
996 GlobalVariables, ImportedEntities, DWOId))
998 TempDICompileUnit clone() const { return cloneImpl(); }
1000 unsigned getSourceLanguage() const { return SourceLanguage; }
1001 bool isOptimized() const { return IsOptimized; }
1002 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1003 unsigned getEmissionKind() const { return EmissionKind; }
1004 StringRef getProducer() const { return getStringOperand(1); }
1005 StringRef getFlags() const { return getStringOperand(2); }
1006 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1007 DICompositeTypeArray getEnumTypes() const {
1008 return cast_or_null<MDTuple>(getRawEnumTypes());
1010 DITypeArray getRetainedTypes() const {
1011 return cast_or_null<MDTuple>(getRawRetainedTypes());
1013 DISubprogramArray getSubprograms() const {
1014 return cast_or_null<MDTuple>(getRawSubprograms());
1016 DIGlobalVariableArray getGlobalVariables() const {
1017 return cast_or_null<MDTuple>(getRawGlobalVariables());
1019 DIImportedEntityArray getImportedEntities() const {
1020 return cast_or_null<MDTuple>(getRawImportedEntities());
1022 unsigned getDWOId() const { return DWOId; }
1024 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1025 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1026 MDString *getRawSplitDebugFilename() const {
1027 return getOperandAs<MDString>(3);
1029 Metadata *getRawEnumTypes() const { return getOperand(4); }
1030 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1031 Metadata *getRawSubprograms() const { return getOperand(6); }
1032 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1033 Metadata *getRawImportedEntities() const { return getOperand(8); }
1035 /// \brief Replace arrays.
1037 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1038 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1039 /// DICompileUnit should be fairly rare.
1041 void replaceEnumTypes(DICompositeTypeArray N) {
1042 replaceOperandWith(4, N.get());
1044 void replaceRetainedTypes(DITypeArray N) {
1045 replaceOperandWith(5, N.get());
1047 void replaceSubprograms(DISubprogramArray N) {
1048 replaceOperandWith(6, N.get());
1050 void replaceGlobalVariables(DIGlobalVariableArray N) {
1051 replaceOperandWith(7, N.get());
1053 void replaceImportedEntities(DIImportedEntityArray N) {
1054 replaceOperandWith(8, N.get());
1058 static bool classof(const Metadata *MD) {
1059 return MD->getMetadataID() == DICompileUnitKind;
1063 /// \brief A scope for locals.
1065 /// A legal scope for lexical blocks, local variables, and debug info
1066 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1067 /// DILexicalBlockFile.
1068 class DILocalScope : public DIScope {
1070 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1071 ArrayRef<Metadata *> Ops)
1072 : DIScope(C, ID, Storage, Tag, Ops) {}
1073 ~DILocalScope() = default;
1076 /// \brief Get the subprogram for this scope.
1078 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1080 DISubprogram *getSubprogram() const;
1082 static bool classof(const Metadata *MD) {
1083 return MD->getMetadataID() == DISubprogramKind ||
1084 MD->getMetadataID() == DILexicalBlockKind ||
1085 MD->getMetadataID() == DILexicalBlockFileKind;
1089 /// \brief Debug location.
1091 /// A debug location in source code, used for debug info and otherwise.
1092 class DILocation : public MDNode {
1093 friend class LLVMContextImpl;
1094 friend class MDNode;
1096 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1097 unsigned Column, ArrayRef<Metadata *> MDs);
1098 ~DILocation() { dropAllReferences(); }
1100 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1101 unsigned Column, Metadata *Scope,
1102 Metadata *InlinedAt, StorageType Storage,
1103 bool ShouldCreate = true);
1104 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1105 unsigned Column, DILocalScope *Scope,
1106 DILocation *InlinedAt, StorageType Storage,
1107 bool ShouldCreate = true) {
1108 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1109 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1112 TempDILocation cloneImpl() const {
1113 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1117 // Disallow replacing operands.
1118 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1121 DEFINE_MDNODE_GET(DILocation,
1122 (unsigned Line, unsigned Column, Metadata *Scope,
1123 Metadata *InlinedAt = nullptr),
1124 (Line, Column, Scope, InlinedAt))
1125 DEFINE_MDNODE_GET(DILocation,
1126 (unsigned Line, unsigned Column, DILocalScope *Scope,
1127 DILocation *InlinedAt = nullptr),
1128 (Line, Column, Scope, InlinedAt))
1130 /// \brief Return a (temporary) clone of this.
1131 TempDILocation clone() const { return cloneImpl(); }
1133 unsigned getLine() const { return SubclassData32; }
1134 unsigned getColumn() const { return SubclassData16; }
1135 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1136 DILocation *getInlinedAt() const {
1137 return cast_or_null<DILocation>(getRawInlinedAt());
1140 DIFile *getFile() const { return getScope()->getFile(); }
1141 StringRef getFilename() const { return getScope()->getFilename(); }
1142 StringRef getDirectory() const { return getScope()->getDirectory(); }
1144 /// \brief Get the scope where this is inlined.
1146 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1148 DILocalScope *getInlinedAtScope() const {
1149 if (auto *IA = getInlinedAt())
1150 return IA->getInlinedAtScope();
1154 /// \brief Check whether this can be discriminated from another location.
1156 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1157 /// Scope and inlined-at chains are not recorded in the linetable, so they
1158 /// cannot be used to distinguish basic blocks.
1160 /// The current implementation is weaker than it should be, since it just
1161 /// checks filename and line.
1163 /// FIXME: Add a check for getDiscriminator().
1164 /// FIXME: Add a check for getColumn().
1165 /// FIXME: Change the getFilename() check to getFile() (or add one for
1166 /// getDirectory()).
1167 bool canDiscriminate(const DILocation &RHS) const {
1168 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1171 /// \brief Get the DWARF discriminator.
1173 /// DWARF discriminators distinguish identical file locations between
1174 /// instructions that are on different basic blocks.
1175 inline unsigned getDiscriminator() const;
1177 /// \brief Compute new discriminator in the given context.
1179 /// This modifies the \a LLVMContext that \c this is in to increment the next
1180 /// discriminator for \c this's line/filename combination.
1182 /// FIXME: Delete this. See comments in implementation and at the only call
1183 /// site in \a AddDiscriminators::runOnFunction().
1184 unsigned computeNewDiscriminator() const;
1186 Metadata *getRawScope() const { return getOperand(0); }
1187 Metadata *getRawInlinedAt() const {
1188 if (getNumOperands() == 2)
1189 return getOperand(1);
1193 static bool classof(const Metadata *MD) {
1194 return MD->getMetadataID() == DILocationKind;
1198 /// \brief Subprogram description.
1200 /// TODO: Remove DisplayName. It's always equal to Name.
1201 /// TODO: Split up flags.
1202 class DISubprogram : public DILocalScope {
1203 friend class LLVMContextImpl;
1204 friend class MDNode;
1208 unsigned Virtuality;
1209 unsigned VirtualIndex;
1215 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1216 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1217 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1218 bool IsOptimized, ArrayRef<Metadata *> Ops)
1219 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1221 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1222 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1223 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1224 ~DISubprogram() = default;
1226 static DISubprogram *
1227 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1228 StringRef LinkageName, DIFile *File, unsigned Line,
1229 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1230 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1231 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1232 Constant *Function, DITemplateParameterArray TemplateParams,
1233 DISubprogram *Declaration, DILocalVariableArray Variables,
1234 StorageType Storage, bool ShouldCreate = true) {
1235 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1236 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1237 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1238 Virtuality, VirtualIndex, Flags, IsOptimized,
1239 Function ? ConstantAsMetadata::get(Function) : nullptr,
1240 TemplateParams.get(), Declaration, Variables.get(), Storage,
1243 static DISubprogram *
1244 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1245 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1246 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1247 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1248 unsigned Flags, bool IsOptimized, Metadata *Function,
1249 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1250 StorageType Storage, bool ShouldCreate = true);
1252 TempDISubprogram cloneImpl() const {
1253 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1254 getFile(), getLine(), getType(), isLocalToUnit(),
1255 isDefinition(), getScopeLine(), getContainingType(),
1256 getVirtuality(), getVirtualIndex(), getFlags(),
1257 isOptimized(), getFunctionConstant(),
1258 getTemplateParams(), getDeclaration(), getVariables());
1262 DEFINE_MDNODE_GET(DISubprogram,
1263 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1264 DIFile *File, unsigned Line, DISubroutineType *Type,
1265 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1266 DITypeRef ContainingType, unsigned Virtuality,
1267 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1268 Constant *Function = nullptr,
1269 DITemplateParameterArray TemplateParams = nullptr,
1270 DISubprogram *Declaration = nullptr,
1271 DILocalVariableArray Variables = nullptr),
1272 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1273 IsDefinition, ScopeLine, ContainingType, Virtuality,
1274 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1275 Declaration, Variables))
1278 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1279 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1280 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1281 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1282 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1283 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1284 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1285 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1286 Function, TemplateParams, Declaration, Variables))
1288 TempDISubprogram clone() const { return cloneImpl(); }
1291 unsigned getLine() const { return Line; }
1292 unsigned getVirtuality() const { return Virtuality; }
1293 unsigned getVirtualIndex() const { return VirtualIndex; }
1294 unsigned getScopeLine() const { return ScopeLine; }
1295 unsigned getFlags() const { return Flags; }
1296 bool isLocalToUnit() const { return IsLocalToUnit; }
1297 bool isDefinition() const { return IsDefinition; }
1298 bool isOptimized() const { return IsOptimized; }
1300 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1301 bool isPrivate() const {
1302 return (getFlags() & FlagAccessibility) == FlagPrivate;
1304 bool isProtected() const {
1305 return (getFlags() & FlagAccessibility) == FlagProtected;
1307 bool isPublic() const {
1308 return (getFlags() & FlagAccessibility) == FlagPublic;
1310 bool isExplicit() const { return getFlags() & FlagExplicit; }
1311 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1313 /// \brief Check if this is reference-qualified.
1315 /// Return true if this subprogram is a C++11 reference-qualified non-static
1316 /// member function (void foo() &).
1317 unsigned isLValueReference() const {
1318 return getFlags() & FlagLValueReference;
1321 /// \brief Check if this is rvalue-reference-qualified.
1323 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1324 /// non-static member function (void foo() &&).
1325 unsigned isRValueReference() const {
1326 return getFlags() & FlagRValueReference;
1329 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1331 StringRef getName() const { return getStringOperand(2); }
1332 StringRef getDisplayName() const { return getStringOperand(3); }
1333 StringRef getLinkageName() const { return getStringOperand(4); }
1335 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1336 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1338 DISubroutineType *getType() const {
1339 return cast_or_null<DISubroutineType>(getRawType());
1341 DITypeRef getContainingType() const {
1342 return DITypeRef(getRawContainingType());
1345 Constant *getFunctionConstant() const {
1346 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1347 return C->getValue();
1350 DITemplateParameterArray getTemplateParams() const {
1351 return cast_or_null<MDTuple>(getRawTemplateParams());
1353 DISubprogram *getDeclaration() const {
1354 return cast_or_null<DISubprogram>(getRawDeclaration());
1356 DILocalVariableArray getVariables() const {
1357 return cast_or_null<MDTuple>(getRawVariables());
1360 Metadata *getRawScope() const { return getOperand(1); }
1361 Metadata *getRawType() const { return getOperand(5); }
1362 Metadata *getRawContainingType() const { return getOperand(6); }
1363 Metadata *getRawFunction() const { return getOperand(7); }
1364 Metadata *getRawTemplateParams() const { return getOperand(8); }
1365 Metadata *getRawDeclaration() const { return getOperand(9); }
1366 Metadata *getRawVariables() const { return getOperand(10); }
1368 /// \brief Get a pointer to the function this subprogram describes.
1370 /// This dyn_casts \a getFunctionConstant() to \a Function.
1372 /// FIXME: Should this be looking through bitcasts?
1373 Function *getFunction() const;
1375 /// \brief Replace the function.
1377 /// If \a isUniqued() and not \a isResolved(), this could node will be
1378 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1379 /// that's a problem.
1381 void replaceFunction(Function *F);
1382 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1383 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1386 /// \brief Check if this subprogram decribes the given function.
1388 /// FIXME: Should this be looking through bitcasts?
1389 bool describes(const Function *F) const;
1391 static bool classof(const Metadata *MD) {
1392 return MD->getMetadataID() == DISubprogramKind;
1396 class DILexicalBlockBase : public DILocalScope {
1398 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1399 ArrayRef<Metadata *> Ops)
1400 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1401 ~DILexicalBlockBase() = default;
1404 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1406 Metadata *getRawScope() const { return getOperand(1); }
1408 /// \brief Forwarding accessors to LexicalBlock.
1410 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1412 inline unsigned getLine() const;
1413 inline unsigned getColumn() const;
1415 static bool classof(const Metadata *MD) {
1416 return MD->getMetadataID() == DILexicalBlockKind ||
1417 MD->getMetadataID() == DILexicalBlockFileKind;
1421 class DILexicalBlock : public DILexicalBlockBase {
1422 friend class LLVMContextImpl;
1423 friend class MDNode;
1428 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1429 unsigned Column, ArrayRef<Metadata *> Ops)
1430 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1432 ~DILexicalBlock() = default;
1434 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1435 DIFile *File, unsigned Line, unsigned Column,
1436 StorageType Storage,
1437 bool ShouldCreate = true) {
1438 return getImpl(Context, static_cast<Metadata *>(Scope),
1439 static_cast<Metadata *>(File), Line, Column, Storage,
1443 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1444 Metadata *File, unsigned Line, unsigned Column,
1445 StorageType Storage, bool ShouldCreate = true);
1447 TempDILexicalBlock cloneImpl() const {
1448 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1453 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1454 unsigned Line, unsigned Column),
1455 (Scope, File, Line, Column))
1456 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1457 unsigned Line, unsigned Column),
1458 (Scope, File, Line, Column))
1460 TempDILexicalBlock clone() const { return cloneImpl(); }
1462 unsigned getLine() const { return Line; }
1463 unsigned getColumn() const { return Column; }
1465 static bool classof(const Metadata *MD) {
1466 return MD->getMetadataID() == DILexicalBlockKind;
1470 unsigned DILexicalBlockBase::getLine() const {
1471 if (auto *N = dyn_cast<DILexicalBlock>(this))
1472 return N->getLine();
1476 unsigned DILexicalBlockBase::getColumn() const {
1477 if (auto *N = dyn_cast<DILexicalBlock>(this))
1478 return N->getColumn();
1482 class DILexicalBlockFile : public DILexicalBlockBase {
1483 friend class LLVMContextImpl;
1484 friend class MDNode;
1486 unsigned Discriminator;
1488 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1489 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1490 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1491 Discriminator(Discriminator) {}
1492 ~DILexicalBlockFile() = default;
1494 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1495 DIFile *File, unsigned Discriminator,
1496 StorageType Storage,
1497 bool ShouldCreate = true) {
1498 return getImpl(Context, static_cast<Metadata *>(Scope),
1499 static_cast<Metadata *>(File), Discriminator, Storage,
1503 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1504 Metadata *File, unsigned Discriminator,
1505 StorageType Storage,
1506 bool ShouldCreate = true);
1508 TempDILexicalBlockFile cloneImpl() const {
1509 return getTemporary(getContext(), getScope(), getFile(),
1510 getDiscriminator());
1514 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1515 unsigned Discriminator),
1516 (Scope, File, Discriminator))
1517 DEFINE_MDNODE_GET(DILexicalBlockFile,
1518 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1519 (Scope, File, Discriminator))
1521 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1523 // TODO: Remove these once they're gone from DILexicalBlockBase.
1524 unsigned getLine() const = delete;
1525 unsigned getColumn() const = delete;
1527 unsigned getDiscriminator() const { return Discriminator; }
1529 static bool classof(const Metadata *MD) {
1530 return MD->getMetadataID() == DILexicalBlockFileKind;
1534 unsigned DILocation::getDiscriminator() const {
1535 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1536 return F->getDiscriminator();
1540 class DINamespace : public DIScope {
1541 friend class LLVMContextImpl;
1542 friend class MDNode;
1546 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1547 ArrayRef<Metadata *> Ops)
1548 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1551 ~DINamespace() = default;
1553 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1554 DIFile *File, StringRef Name, unsigned Line,
1555 StorageType Storage, bool ShouldCreate = true) {
1556 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1557 Line, Storage, ShouldCreate);
1559 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1560 Metadata *File, MDString *Name, unsigned Line,
1561 StorageType Storage, bool ShouldCreate = true);
1563 TempDINamespace cloneImpl() const {
1564 return getTemporary(getContext(), getScope(), getFile(), getName(),
1569 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1571 (Scope, File, Name, Line))
1572 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1573 MDString *Name, unsigned Line),
1574 (Scope, File, Name, Line))
1576 TempDINamespace clone() const { return cloneImpl(); }
1578 unsigned getLine() const { return Line; }
1579 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1580 StringRef getName() const { return getStringOperand(2); }
1582 Metadata *getRawScope() const { return getOperand(1); }
1583 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1585 static bool classof(const Metadata *MD) {
1586 return MD->getMetadataID() == DINamespaceKind;
1590 /// \brief A (clang) module that has been imported by the compile unit.
1592 class DIModule : public DIScope {
1593 friend class LLVMContextImpl;
1594 friend class MDNode;
1596 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1597 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1600 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1601 StringRef Name, StringRef ConfigurationMacros,
1602 StringRef IncludePath, StringRef ISysRoot,
1603 StorageType Storage, bool ShouldCreate = true) {
1604 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1605 getCanonicalMDString(Context, ConfigurationMacros),
1606 getCanonicalMDString(Context, IncludePath),
1607 getCanonicalMDString(Context, ISysRoot),
1608 Storage, ShouldCreate);
1610 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1611 MDString *Name, MDString *ConfigurationMacros,
1612 MDString *IncludePath, MDString *ISysRoot,
1613 StorageType Storage, bool ShouldCreate = true);
1615 TempDIModule cloneImpl() const {
1616 return getTemporary(getContext(), getScope(), getName(),
1617 getConfigurationMacros(), getIncludePath(),
1622 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1623 StringRef ConfigurationMacros, StringRef IncludePath,
1624 StringRef ISysRoot),
1625 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1626 DEFINE_MDNODE_GET(DIModule,
1627 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1628 MDString *IncludePath, MDString *ISysRoot),
1629 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1631 TempDIModule clone() const { return cloneImpl(); }
1633 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1634 StringRef getName() const { return getStringOperand(1); }
1635 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1636 StringRef getIncludePath() const { return getStringOperand(3); }
1637 StringRef getISysRoot() const { return getStringOperand(4); }
1639 Metadata *getRawScope() const { return getOperand(0); }
1640 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1641 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1642 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1643 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1645 static bool classof(const Metadata *MD) {
1646 return MD->getMetadataID() == DIModuleKind;
1650 /// \brief Base class for template parameters.
1651 class DITemplateParameter : public DINode {
1653 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1654 unsigned Tag, ArrayRef<Metadata *> Ops)
1655 : DINode(Context, ID, Storage, Tag, Ops) {}
1656 ~DITemplateParameter() = default;
1659 StringRef getName() const { return getStringOperand(0); }
1660 DITypeRef getType() const { return DITypeRef(getRawType()); }
1662 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1663 Metadata *getRawType() const { return getOperand(1); }
1665 static bool classof(const Metadata *MD) {
1666 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1667 MD->getMetadataID() == DITemplateValueParameterKind;
1671 class DITemplateTypeParameter : public DITemplateParameter {
1672 friend class LLVMContextImpl;
1673 friend class MDNode;
1675 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1676 ArrayRef<Metadata *> Ops)
1677 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1678 dwarf::DW_TAG_template_type_parameter, Ops) {}
1679 ~DITemplateTypeParameter() = default;
1681 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1682 DITypeRef Type, StorageType Storage,
1683 bool ShouldCreate = true) {
1684 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1687 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1688 Metadata *Type, StorageType Storage,
1689 bool ShouldCreate = true);
1691 TempDITemplateTypeParameter cloneImpl() const {
1692 return getTemporary(getContext(), getName(), getType());
1696 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1698 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1701 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1703 static bool classof(const Metadata *MD) {
1704 return MD->getMetadataID() == DITemplateTypeParameterKind;
1708 class DITemplateValueParameter : public DITemplateParameter {
1709 friend class LLVMContextImpl;
1710 friend class MDNode;
1712 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1713 unsigned Tag, ArrayRef<Metadata *> Ops)
1714 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1716 ~DITemplateValueParameter() = default;
1718 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1719 StringRef Name, DITypeRef Type,
1720 Metadata *Value, StorageType Storage,
1721 bool ShouldCreate = true) {
1722 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1723 Value, Storage, ShouldCreate);
1725 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1726 MDString *Name, Metadata *Type,
1727 Metadata *Value, StorageType Storage,
1728 bool ShouldCreate = true);
1730 TempDITemplateValueParameter cloneImpl() const {
1731 return getTemporary(getContext(), getTag(), getName(), getType(),
1736 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1737 DITypeRef Type, Metadata *Value),
1738 (Tag, Name, Type, Value))
1739 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1740 Metadata *Type, Metadata *Value),
1741 (Tag, Name, Type, Value))
1743 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1745 Metadata *getValue() const { return getOperand(2); }
1747 static bool classof(const Metadata *MD) {
1748 return MD->getMetadataID() == DITemplateValueParameterKind;
1752 /// \brief Base class for variables.
1753 class DIVariable : public DINode {
1757 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
1758 ArrayRef<Metadata *> Ops)
1759 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line) {}
1760 ~DIVariable() = default;
1763 unsigned getLine() const { return Line; }
1764 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1765 StringRef getName() const { return getStringOperand(1); }
1766 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1767 DITypeRef getType() const { return DITypeRef(getRawType()); }
1769 StringRef getFilename() const {
1770 if (auto *F = getFile())
1771 return F->getFilename();
1774 StringRef getDirectory() const {
1775 if (auto *F = getFile())
1776 return F->getDirectory();
1780 Metadata *getRawScope() const { return getOperand(0); }
1781 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1782 Metadata *getRawFile() const { return getOperand(2); }
1783 Metadata *getRawType() const { return getOperand(3); }
1785 static bool classof(const Metadata *MD) {
1786 return MD->getMetadataID() == DILocalVariableKind ||
1787 MD->getMetadataID() == DIGlobalVariableKind;
1791 /// \brief Global variables.
1793 /// TODO: Remove DisplayName. It's always equal to Name.
1794 class DIGlobalVariable : public DIVariable {
1795 friend class LLVMContextImpl;
1796 friend class MDNode;
1801 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1802 bool IsLocalToUnit, bool IsDefinition,
1803 ArrayRef<Metadata *> Ops)
1804 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops),
1805 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1806 ~DIGlobalVariable() = default;
1808 static DIGlobalVariable *
1809 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1810 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1811 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1812 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1813 bool ShouldCreate = true) {
1814 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1815 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1816 IsLocalToUnit, IsDefinition,
1817 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1818 StaticDataMemberDeclaration, Storage, ShouldCreate);
1820 static DIGlobalVariable *
1821 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1822 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1823 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1824 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1825 bool ShouldCreate = true);
1827 TempDIGlobalVariable cloneImpl() const {
1828 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1829 getFile(), getLine(), getType(), isLocalToUnit(),
1830 isDefinition(), getVariable(),
1831 getStaticDataMemberDeclaration());
1835 DEFINE_MDNODE_GET(DIGlobalVariable,
1836 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1837 DIFile *File, unsigned Line, DITypeRef Type,
1838 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1839 DIDerivedType *StaticDataMemberDeclaration),
1840 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1841 IsDefinition, Variable, StaticDataMemberDeclaration))
1842 DEFINE_MDNODE_GET(DIGlobalVariable,
1843 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1844 Metadata *File, unsigned Line, Metadata *Type,
1845 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1846 Metadata *StaticDataMemberDeclaration),
1847 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1848 IsDefinition, Variable, StaticDataMemberDeclaration))
1850 TempDIGlobalVariable clone() const { return cloneImpl(); }
1852 bool isLocalToUnit() const { return IsLocalToUnit; }
1853 bool isDefinition() const { return IsDefinition; }
1854 StringRef getDisplayName() const { return getStringOperand(4); }
1855 StringRef getLinkageName() const { return getStringOperand(5); }
1856 Constant *getVariable() const {
1857 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1858 return dyn_cast<Constant>(C->getValue());
1861 DIDerivedType *getStaticDataMemberDeclaration() const {
1862 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1865 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1866 Metadata *getRawVariable() const { return getOperand(6); }
1867 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1869 static bool classof(const Metadata *MD) {
1870 return MD->getMetadataID() == DIGlobalVariableKind;
1874 /// \brief Local variable.
1876 /// TODO: Split up flags.
1877 class DILocalVariable : public DIVariable {
1878 friend class LLVMContextImpl;
1879 friend class MDNode;
1884 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1885 unsigned Arg, unsigned Flags, ArrayRef<Metadata *> Ops)
1886 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops), Arg(Arg),
1888 ~DILocalVariable() = default;
1890 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
1891 StringRef Name, DIFile *File, unsigned Line,
1892 DITypeRef Type, unsigned Arg, unsigned Flags,
1893 StorageType Storage,
1894 bool ShouldCreate = true) {
1895 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
1896 Line, Type, Arg, Flags, Storage, ShouldCreate);
1898 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
1899 MDString *Name, Metadata *File, unsigned Line,
1900 Metadata *Type, unsigned Arg, unsigned Flags,
1901 StorageType Storage,
1902 bool ShouldCreate = true);
1904 TempDILocalVariable cloneImpl() const {
1905 return getTemporary(getContext(), getScope(), getName(), getFile(),
1906 getLine(), getType(), getArg(), getFlags());
1910 DEFINE_MDNODE_GET(DILocalVariable,
1911 (DILocalScope * Scope, StringRef Name, DIFile *File,
1912 unsigned Line, DITypeRef Type, unsigned Arg,
1914 (Scope, Name, File, Line, Type, Arg, Flags))
1915 DEFINE_MDNODE_GET(DILocalVariable,
1916 (Metadata * Scope, MDString *Name, Metadata *File,
1917 unsigned Line, Metadata *Type, unsigned Arg,
1919 (Scope, Name, File, Line, Type, Arg, Flags))
1921 TempDILocalVariable clone() const { return cloneImpl(); }
1923 /// \brief Get the local scope for this variable.
1925 /// Variables must be defined in a local scope.
1926 DILocalScope *getScope() const {
1927 return cast<DILocalScope>(DIVariable::getScope());
1930 bool isParameter() const { return Arg; }
1931 unsigned getArg() const { return Arg; }
1932 unsigned getFlags() const { return Flags; }
1934 bool isArtificial() const { return getFlags() & FlagArtificial; }
1935 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1937 /// \brief Check that a location is valid for this variable.
1939 /// Check that \c DL exists, is in the same subprogram, and has the same
1940 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1941 /// to a \a DbgInfoIntrinsic.)
1942 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1943 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1946 static bool classof(const Metadata *MD) {
1947 return MD->getMetadataID() == DILocalVariableKind;
1951 /// \brief DWARF expression.
1953 /// This is (almost) a DWARF expression that modifies the location of a
1954 /// variable or (or the location of a single piece of a variable).
1956 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1957 /// and have DW_OP_plus consume the topmost elements on the stack.
1959 /// TODO: Co-allocate the expression elements.
1960 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1962 class DIExpression : public MDNode {
1963 friend class LLVMContextImpl;
1964 friend class MDNode;
1966 std::vector<uint64_t> Elements;
1968 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1969 : MDNode(C, DIExpressionKind, Storage, None),
1970 Elements(Elements.begin(), Elements.end()) {}
1971 ~DIExpression() = default;
1973 static DIExpression *getImpl(LLVMContext &Context,
1974 ArrayRef<uint64_t> Elements, StorageType Storage,
1975 bool ShouldCreate = true);
1977 TempDIExpression cloneImpl() const {
1978 return getTemporary(getContext(), getElements());
1982 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1984 TempDIExpression clone() const { return cloneImpl(); }
1986 ArrayRef<uint64_t> getElements() const { return Elements; }
1988 unsigned getNumElements() const { return Elements.size(); }
1989 uint64_t getElement(unsigned I) const {
1990 assert(I < Elements.size() && "Index out of range");
1994 /// \brief Return whether this is a piece of an aggregate variable.
1995 bool isBitPiece() const;
1997 /// \brief Return the offset of this piece in bits.
1998 uint64_t getBitPieceOffset() const;
2000 /// \brief Return the size of this piece in bits.
2001 uint64_t getBitPieceSize() const;
2003 typedef ArrayRef<uint64_t>::iterator element_iterator;
2004 element_iterator elements_begin() const { return getElements().begin(); }
2005 element_iterator elements_end() const { return getElements().end(); }
2007 /// \brief A lightweight wrapper around an expression operand.
2009 /// TODO: Store arguments directly and change \a DIExpression to store a
2015 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2017 const uint64_t *get() const { return Op; }
2019 /// \brief Get the operand code.
2020 uint64_t getOp() const { return *Op; }
2022 /// \brief Get an argument to the operand.
2024 /// Never returns the operand itself.
2025 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2027 unsigned getNumArgs() const { return getSize() - 1; }
2029 /// \brief Return the size of the operand.
2031 /// Return the number of elements in the operand (1 + args).
2032 unsigned getSize() const;
2035 /// \brief An iterator for expression operands.
2036 class expr_op_iterator
2037 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2041 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2043 element_iterator getBase() const { return Op.get(); }
2044 const ExprOperand &operator*() const { return Op; }
2045 const ExprOperand *operator->() const { return &Op; }
2047 expr_op_iterator &operator++() {
2051 expr_op_iterator operator++(int) {
2052 expr_op_iterator T(*this);
2057 /// \brief Get the next iterator.
2059 /// \a std::next() doesn't work because this is technically an
2060 /// input_iterator, but it's a perfectly valid operation. This is an
2061 /// accessor to provide the same functionality.
2062 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2064 bool operator==(const expr_op_iterator &X) const {
2065 return getBase() == X.getBase();
2067 bool operator!=(const expr_op_iterator &X) const {
2068 return getBase() != X.getBase();
2072 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2075 /// \brief Visit the elements via ExprOperand wrappers.
2077 /// These range iterators visit elements through \a ExprOperand wrappers.
2078 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2081 /// \pre \a isValid() gives \c true.
2083 expr_op_iterator expr_op_begin() const {
2084 return expr_op_iterator(elements_begin());
2086 expr_op_iterator expr_op_end() const {
2087 return expr_op_iterator(elements_end());
2091 bool isValid() const;
2093 static bool classof(const Metadata *MD) {
2094 return MD->getMetadataID() == DIExpressionKind;
2098 class DIObjCProperty : public DINode {
2099 friend class LLVMContextImpl;
2100 friend class MDNode;
2103 unsigned Attributes;
2105 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2106 unsigned Attributes, ArrayRef<Metadata *> Ops)
2107 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2109 Line(Line), Attributes(Attributes) {}
2110 ~DIObjCProperty() = default;
2112 static DIObjCProperty *
2113 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2114 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2115 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2116 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2117 getCanonicalMDString(Context, GetterName),
2118 getCanonicalMDString(Context, SetterName), Attributes, Type,
2119 Storage, ShouldCreate);
2121 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2122 Metadata *File, unsigned Line,
2123 MDString *GetterName, MDString *SetterName,
2124 unsigned Attributes, Metadata *Type,
2125 StorageType Storage, bool ShouldCreate = true);
2127 TempDIObjCProperty cloneImpl() const {
2128 return getTemporary(getContext(), getName(), getFile(), getLine(),
2129 getGetterName(), getSetterName(), getAttributes(),
2134 DEFINE_MDNODE_GET(DIObjCProperty,
2135 (StringRef Name, DIFile *File, unsigned Line,
2136 StringRef GetterName, StringRef SetterName,
2137 unsigned Attributes, DITypeRef Type),
2138 (Name, File, Line, GetterName, SetterName, Attributes,
2140 DEFINE_MDNODE_GET(DIObjCProperty,
2141 (MDString * Name, Metadata *File, unsigned Line,
2142 MDString *GetterName, MDString *SetterName,
2143 unsigned Attributes, Metadata *Type),
2144 (Name, File, Line, GetterName, SetterName, Attributes,
2147 TempDIObjCProperty clone() const { return cloneImpl(); }
2149 unsigned getLine() const { return Line; }
2150 unsigned getAttributes() const { return Attributes; }
2151 StringRef getName() const { return getStringOperand(0); }
2152 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2153 StringRef getGetterName() const { return getStringOperand(2); }
2154 StringRef getSetterName() const { return getStringOperand(3); }
2155 DITypeRef getType() const { return DITypeRef(getRawType()); }
2157 StringRef getFilename() const {
2158 if (auto *F = getFile())
2159 return F->getFilename();
2162 StringRef getDirectory() const {
2163 if (auto *F = getFile())
2164 return F->getDirectory();
2168 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2169 Metadata *getRawFile() const { return getOperand(1); }
2170 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2171 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2172 Metadata *getRawType() const { return getOperand(4); }
2174 static bool classof(const Metadata *MD) {
2175 return MD->getMetadataID() == DIObjCPropertyKind;
2179 /// \brief An imported module (C++ using directive or similar).
2180 class DIImportedEntity : public DINode {
2181 friend class LLVMContextImpl;
2182 friend class MDNode;
2186 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2187 unsigned Line, ArrayRef<Metadata *> Ops)
2188 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2189 ~DIImportedEntity() = default;
2191 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2192 DIScope *Scope, DINodeRef Entity,
2193 unsigned Line, StringRef Name,
2194 StorageType Storage,
2195 bool ShouldCreate = true) {
2196 return getImpl(Context, Tag, Scope, Entity, Line,
2197 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2199 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2200 Metadata *Scope, Metadata *Entity,
2201 unsigned Line, MDString *Name,
2202 StorageType Storage,
2203 bool ShouldCreate = true);
2205 TempDIImportedEntity cloneImpl() const {
2206 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2207 getLine(), getName());
2211 DEFINE_MDNODE_GET(DIImportedEntity,
2212 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2213 unsigned Line, StringRef Name = ""),
2214 (Tag, Scope, Entity, Line, Name))
2215 DEFINE_MDNODE_GET(DIImportedEntity,
2216 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2217 unsigned Line, MDString *Name),
2218 (Tag, Scope, Entity, Line, Name))
2220 TempDIImportedEntity clone() const { return cloneImpl(); }
2222 unsigned getLine() const { return Line; }
2223 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2224 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2225 StringRef getName() const { return getStringOperand(2); }
2227 Metadata *getRawScope() const { return getOperand(0); }
2228 Metadata *getRawEntity() const { return getOperand(1); }
2229 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2231 static bool classof(const Metadata *MD) {
2232 return MD->getMetadataID() == DIImportedEntityKind;
2236 } // end namespace llvm
2238 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2239 #undef DEFINE_MDNODE_GET_UNPACK
2240 #undef DEFINE_MDNODE_GET