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 Base class for DICompositeType and DISubroutineType.
750 /// TODO: Delete; they're not really related.
751 class DICompositeTypeBase : public DIType {
752 unsigned RuntimeLang;
755 DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
756 unsigned Tag, unsigned Line, unsigned RuntimeLang,
757 uint64_t SizeInBits, uint64_t AlignInBits,
758 uint64_t OffsetInBits, unsigned Flags,
759 ArrayRef<Metadata *> Ops)
760 : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
762 RuntimeLang(RuntimeLang) {}
763 ~DICompositeTypeBase() = default;
766 //// Get the base type this is derived from, if any.
767 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
769 /// \brief Get the elements of the composite type.
771 /// \note Calling this is only valid for \a DICompositeType. This assertion
772 /// can be removed once \a DISubroutineType has been separated from
773 /// "composite types".
774 DINodeArray getElements() const {
775 assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
776 return cast_or_null<MDTuple>(getRawElements());
778 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
779 DITemplateParameterArray getTemplateParams() const {
780 return cast_or_null<MDTuple>(getRawTemplateParams());
782 StringRef getIdentifier() const { return getStringOperand(7); }
783 unsigned getRuntimeLang() const { return RuntimeLang; }
785 Metadata *getRawBaseType() const { return getOperand(3); }
786 Metadata *getRawElements() const { return getOperand(4); }
787 Metadata *getRawVTableHolder() const { return getOperand(5); }
788 Metadata *getRawTemplateParams() const { return getOperand(6); }
789 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
791 /// \brief Replace operands.
793 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
794 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
795 /// of its movement if necessary.
797 void replaceElements(DINodeArray Elements) {
799 for (DINode *Op : getElements())
800 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
801 "Lost a member during member list replacement");
803 replaceOperandWith(4, Elements.get());
805 void replaceVTableHolder(DITypeRef VTableHolder) {
806 replaceOperandWith(5, VTableHolder);
808 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
809 replaceOperandWith(6, TemplateParams.get());
813 static bool classof(const Metadata *MD) {
814 return MD->getMetadataID() == DICompositeTypeKind ||
815 MD->getMetadataID() == DISubroutineTypeKind;
819 /// \brief Composite types.
821 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
822 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
823 class DICompositeType : public DICompositeTypeBase {
824 friend class LLVMContextImpl;
827 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
828 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
829 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
830 ArrayRef<Metadata *> Ops)
831 : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
832 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
834 ~DICompositeType() = default;
836 static DICompositeType *
837 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
838 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
839 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
840 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
841 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
842 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
844 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
845 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
846 RuntimeLang, VTableHolder, TemplateParams.get(),
847 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
849 static DICompositeType *
850 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
851 unsigned Line, Metadata *Scope, Metadata *BaseType,
852 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
853 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
854 Metadata *VTableHolder, Metadata *TemplateParams,
855 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
857 TempDICompositeType cloneImpl() const {
858 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
859 getScope(), getBaseType(), getSizeInBits(),
860 getAlignInBits(), getOffsetInBits(), getFlags(),
861 getElements(), getRuntimeLang(), getVTableHolder(),
862 getTemplateParams(), getIdentifier());
866 DEFINE_MDNODE_GET(DICompositeType,
867 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
868 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
869 uint64_t AlignInBits, uint64_t OffsetInBits,
870 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
871 DITypeRef VTableHolder,
872 DITemplateParameterArray TemplateParams = nullptr,
873 StringRef Identifier = ""),
874 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
875 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
876 VTableHolder, TemplateParams, Identifier))
877 DEFINE_MDNODE_GET(DICompositeType,
878 (unsigned Tag, MDString *Name, Metadata *File,
879 unsigned Line, Metadata *Scope, Metadata *BaseType,
880 uint64_t SizeInBits, uint64_t AlignInBits,
881 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
882 unsigned RuntimeLang, Metadata *VTableHolder,
883 Metadata *TemplateParams = nullptr,
884 MDString *Identifier = nullptr),
885 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
886 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
887 VTableHolder, TemplateParams, Identifier))
889 TempDICompositeType clone() const { return cloneImpl(); }
891 static bool classof(const Metadata *MD) {
892 return MD->getMetadataID() == DICompositeTypeKind;
896 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
898 if (auto *Composite = dyn_cast<DICompositeType>(N))
899 if (auto *S = Composite->getRawIdentifier())
900 return TypedDINodeRef<T>(S);
901 return TypedDINodeRef<T>(N);
904 /// \brief Type array for a subprogram.
906 /// TODO: Detach from CompositeType, and fold the array of types in directly
908 class DISubroutineType : public DICompositeTypeBase {
909 friend class LLVMContextImpl;
912 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
913 ArrayRef<Metadata *> Ops)
914 : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
915 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
917 ~DISubroutineType() = default;
919 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
920 DITypeRefArray TypeArray,
922 bool ShouldCreate = true) {
923 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
925 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
926 Metadata *TypeArray, StorageType Storage,
927 bool ShouldCreate = true);
929 TempDISubroutineType cloneImpl() const {
930 return getTemporary(getContext(), getFlags(), getTypeArray());
934 DEFINE_MDNODE_GET(DISubroutineType,
935 (unsigned Flags, DITypeRefArray TypeArray),
937 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
940 TempDISubroutineType clone() const { return cloneImpl(); }
942 DITypeRefArray getTypeArray() const {
943 return cast_or_null<MDTuple>(getRawTypeArray());
945 Metadata *getRawTypeArray() const { return getRawElements(); }
947 static bool classof(const Metadata *MD) {
948 return MD->getMetadataID() == DISubroutineTypeKind;
952 /// \brief Compile unit.
953 class DICompileUnit : public DIScope {
954 friend class LLVMContextImpl;
957 unsigned SourceLanguage;
959 unsigned RuntimeVersion;
960 unsigned EmissionKind;
963 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
964 bool IsOptimized, unsigned RuntimeVersion,
965 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
966 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
967 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
968 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
970 ~DICompileUnit() = default;
972 static DICompileUnit *
973 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
974 StringRef Producer, bool IsOptimized, StringRef Flags,
975 unsigned RuntimeVersion, StringRef SplitDebugFilename,
976 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
977 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
978 DIGlobalVariableArray GlobalVariables,
979 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
980 StorageType Storage, bool ShouldCreate = true) {
981 return getImpl(Context, SourceLanguage, File,
982 getCanonicalMDString(Context, Producer), IsOptimized,
983 getCanonicalMDString(Context, Flags), RuntimeVersion,
984 getCanonicalMDString(Context, SplitDebugFilename),
985 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
986 Subprograms.get(), GlobalVariables.get(),
987 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
989 static DICompileUnit *
990 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
991 MDString *Producer, bool IsOptimized, MDString *Flags,
992 unsigned RuntimeVersion, MDString *SplitDebugFilename,
993 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
994 Metadata *Subprograms, Metadata *GlobalVariables,
995 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
996 bool ShouldCreate = true);
998 TempDICompileUnit cloneImpl() const {
1000 getContext(), getSourceLanguage(), getFile(), getProducer(),
1001 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1002 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1003 getGlobalVariables(), getImportedEntities(), DWOId);
1007 DEFINE_MDNODE_GET(DICompileUnit,
1008 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1009 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1010 StringRef SplitDebugFilename, unsigned EmissionKind,
1011 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1012 DISubprogramArray Subprograms,
1013 DIGlobalVariableArray GlobalVariables,
1014 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1015 (SourceLanguage, File, Producer, IsOptimized, Flags,
1016 RuntimeVersion, SplitDebugFilename, EmissionKind,
1017 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1018 ImportedEntities, DWOId))
1021 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1022 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1023 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1024 Metadata *RetainedTypes, Metadata *Subprograms,
1025 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1026 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1027 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1028 GlobalVariables, ImportedEntities, DWOId))
1030 TempDICompileUnit clone() const { return cloneImpl(); }
1032 unsigned getSourceLanguage() const { return SourceLanguage; }
1033 bool isOptimized() const { return IsOptimized; }
1034 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1035 unsigned getEmissionKind() const { return EmissionKind; }
1036 StringRef getProducer() const { return getStringOperand(1); }
1037 StringRef getFlags() const { return getStringOperand(2); }
1038 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1039 DICompositeTypeArray getEnumTypes() const {
1040 return cast_or_null<MDTuple>(getRawEnumTypes());
1042 DITypeArray getRetainedTypes() const {
1043 return cast_or_null<MDTuple>(getRawRetainedTypes());
1045 DISubprogramArray getSubprograms() const {
1046 return cast_or_null<MDTuple>(getRawSubprograms());
1048 DIGlobalVariableArray getGlobalVariables() const {
1049 return cast_or_null<MDTuple>(getRawGlobalVariables());
1051 DIImportedEntityArray getImportedEntities() const {
1052 return cast_or_null<MDTuple>(getRawImportedEntities());
1054 unsigned getDWOId() const { return DWOId; }
1056 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1057 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1058 MDString *getRawSplitDebugFilename() const {
1059 return getOperandAs<MDString>(3);
1061 Metadata *getRawEnumTypes() const { return getOperand(4); }
1062 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1063 Metadata *getRawSubprograms() const { return getOperand(6); }
1064 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1065 Metadata *getRawImportedEntities() const { return getOperand(8); }
1067 /// \brief Replace arrays.
1069 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1070 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1071 /// DICompileUnit should be fairly rare.
1073 void replaceEnumTypes(DICompositeTypeArray N) {
1074 replaceOperandWith(4, N.get());
1076 void replaceRetainedTypes(DITypeArray N) {
1077 replaceOperandWith(5, N.get());
1079 void replaceSubprograms(DISubprogramArray N) {
1080 replaceOperandWith(6, N.get());
1082 void replaceGlobalVariables(DIGlobalVariableArray N) {
1083 replaceOperandWith(7, N.get());
1085 void replaceImportedEntities(DIImportedEntityArray N) {
1086 replaceOperandWith(8, N.get());
1090 static bool classof(const Metadata *MD) {
1091 return MD->getMetadataID() == DICompileUnitKind;
1095 /// \brief A scope for locals.
1097 /// A legal scope for lexical blocks, local variables, and debug info
1098 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1099 /// DILexicalBlockFile.
1100 class DILocalScope : public DIScope {
1102 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1103 ArrayRef<Metadata *> Ops)
1104 : DIScope(C, ID, Storage, Tag, Ops) {}
1105 ~DILocalScope() = default;
1108 /// \brief Get the subprogram for this scope.
1110 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1112 DISubprogram *getSubprogram() const;
1114 static bool classof(const Metadata *MD) {
1115 return MD->getMetadataID() == DISubprogramKind ||
1116 MD->getMetadataID() == DILexicalBlockKind ||
1117 MD->getMetadataID() == DILexicalBlockFileKind;
1121 /// \brief Debug location.
1123 /// A debug location in source code, used for debug info and otherwise.
1124 class DILocation : public MDNode {
1125 friend class LLVMContextImpl;
1126 friend class MDNode;
1128 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1129 unsigned Column, ArrayRef<Metadata *> MDs);
1130 ~DILocation() { dropAllReferences(); }
1132 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1133 unsigned Column, Metadata *Scope,
1134 Metadata *InlinedAt, StorageType Storage,
1135 bool ShouldCreate = true);
1136 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1137 unsigned Column, DILocalScope *Scope,
1138 DILocation *InlinedAt, StorageType Storage,
1139 bool ShouldCreate = true) {
1140 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1141 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1144 TempDILocation cloneImpl() const {
1145 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1149 // Disallow replacing operands.
1150 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1153 DEFINE_MDNODE_GET(DILocation,
1154 (unsigned Line, unsigned Column, Metadata *Scope,
1155 Metadata *InlinedAt = nullptr),
1156 (Line, Column, Scope, InlinedAt))
1157 DEFINE_MDNODE_GET(DILocation,
1158 (unsigned Line, unsigned Column, DILocalScope *Scope,
1159 DILocation *InlinedAt = nullptr),
1160 (Line, Column, Scope, InlinedAt))
1162 /// \brief Return a (temporary) clone of this.
1163 TempDILocation clone() const { return cloneImpl(); }
1165 unsigned getLine() const { return SubclassData32; }
1166 unsigned getColumn() const { return SubclassData16; }
1167 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1168 DILocation *getInlinedAt() const {
1169 return cast_or_null<DILocation>(getRawInlinedAt());
1172 DIFile *getFile() const { return getScope()->getFile(); }
1173 StringRef getFilename() const { return getScope()->getFilename(); }
1174 StringRef getDirectory() const { return getScope()->getDirectory(); }
1176 /// \brief Get the scope where this is inlined.
1178 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1180 DILocalScope *getInlinedAtScope() const {
1181 if (auto *IA = getInlinedAt())
1182 return IA->getInlinedAtScope();
1186 /// \brief Check whether this can be discriminated from another location.
1188 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1189 /// Scope and inlined-at chains are not recorded in the linetable, so they
1190 /// cannot be used to distinguish basic blocks.
1192 /// The current implementation is weaker than it should be, since it just
1193 /// checks filename and line.
1195 /// FIXME: Add a check for getDiscriminator().
1196 /// FIXME: Add a check for getColumn().
1197 /// FIXME: Change the getFilename() check to getFile() (or add one for
1198 /// getDirectory()).
1199 bool canDiscriminate(const DILocation &RHS) const {
1200 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1203 /// \brief Get the DWARF discriminator.
1205 /// DWARF discriminators distinguish identical file locations between
1206 /// instructions that are on different basic blocks.
1207 inline unsigned getDiscriminator() const;
1209 /// \brief Compute new discriminator in the given context.
1211 /// This modifies the \a LLVMContext that \c this is in to increment the next
1212 /// discriminator for \c this's line/filename combination.
1214 /// FIXME: Delete this. See comments in implementation and at the only call
1215 /// site in \a AddDiscriminators::runOnFunction().
1216 unsigned computeNewDiscriminator() const;
1218 Metadata *getRawScope() const { return getOperand(0); }
1219 Metadata *getRawInlinedAt() const {
1220 if (getNumOperands() == 2)
1221 return getOperand(1);
1225 static bool classof(const Metadata *MD) {
1226 return MD->getMetadataID() == DILocationKind;
1230 /// \brief Subprogram description.
1232 /// TODO: Remove DisplayName. It's always equal to Name.
1233 /// TODO: Split up flags.
1234 class DISubprogram : public DILocalScope {
1235 friend class LLVMContextImpl;
1236 friend class MDNode;
1240 unsigned Virtuality;
1241 unsigned VirtualIndex;
1247 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1248 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1249 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1250 bool IsOptimized, ArrayRef<Metadata *> Ops)
1251 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1253 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1254 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1255 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1256 ~DISubprogram() = default;
1258 static DISubprogram *
1259 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1260 StringRef LinkageName, DIFile *File, unsigned Line,
1261 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1262 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1263 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1264 Constant *Function, DITemplateParameterArray TemplateParams,
1265 DISubprogram *Declaration, DILocalVariableArray Variables,
1266 StorageType Storage, bool ShouldCreate = true) {
1267 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1268 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1269 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1270 Virtuality, VirtualIndex, Flags, IsOptimized,
1271 Function ? ConstantAsMetadata::get(Function) : nullptr,
1272 TemplateParams.get(), Declaration, Variables.get(), Storage,
1275 static DISubprogram *
1276 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1277 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1278 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1279 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1280 unsigned Flags, bool IsOptimized, Metadata *Function,
1281 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1282 StorageType Storage, bool ShouldCreate = true);
1284 TempDISubprogram cloneImpl() const {
1285 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1286 getFile(), getLine(), getType(), isLocalToUnit(),
1287 isDefinition(), getScopeLine(), getContainingType(),
1288 getVirtuality(), getVirtualIndex(), getFlags(),
1289 isOptimized(), getFunctionConstant(),
1290 getTemplateParams(), getDeclaration(), getVariables());
1294 DEFINE_MDNODE_GET(DISubprogram,
1295 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1296 DIFile *File, unsigned Line, DISubroutineType *Type,
1297 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1298 DITypeRef ContainingType, unsigned Virtuality,
1299 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1300 Constant *Function = nullptr,
1301 DITemplateParameterArray TemplateParams = nullptr,
1302 DISubprogram *Declaration = nullptr,
1303 DILocalVariableArray Variables = nullptr),
1304 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1305 IsDefinition, ScopeLine, ContainingType, Virtuality,
1306 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1307 Declaration, Variables))
1310 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1311 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1312 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1313 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1314 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1315 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1316 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1317 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1318 Function, TemplateParams, Declaration, Variables))
1320 TempDISubprogram clone() const { return cloneImpl(); }
1323 unsigned getLine() const { return Line; }
1324 unsigned getVirtuality() const { return Virtuality; }
1325 unsigned getVirtualIndex() const { return VirtualIndex; }
1326 unsigned getScopeLine() const { return ScopeLine; }
1327 unsigned getFlags() const { return Flags; }
1328 bool isLocalToUnit() const { return IsLocalToUnit; }
1329 bool isDefinition() const { return IsDefinition; }
1330 bool isOptimized() const { return IsOptimized; }
1332 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1333 bool isPrivate() const {
1334 return (getFlags() & FlagAccessibility) == FlagPrivate;
1336 bool isProtected() const {
1337 return (getFlags() & FlagAccessibility) == FlagProtected;
1339 bool isPublic() const {
1340 return (getFlags() & FlagAccessibility) == FlagPublic;
1342 bool isExplicit() const { return getFlags() & FlagExplicit; }
1343 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1345 /// \brief Check if this is reference-qualified.
1347 /// Return true if this subprogram is a C++11 reference-qualified non-static
1348 /// member function (void foo() &).
1349 unsigned isLValueReference() const {
1350 return getFlags() & FlagLValueReference;
1353 /// \brief Check if this is rvalue-reference-qualified.
1355 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1356 /// non-static member function (void foo() &&).
1357 unsigned isRValueReference() const {
1358 return getFlags() & FlagRValueReference;
1361 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1363 StringRef getName() const { return getStringOperand(2); }
1364 StringRef getDisplayName() const { return getStringOperand(3); }
1365 StringRef getLinkageName() const { return getStringOperand(4); }
1367 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1368 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1370 DISubroutineType *getType() const {
1371 return cast_or_null<DISubroutineType>(getRawType());
1373 DITypeRef getContainingType() const {
1374 return DITypeRef(getRawContainingType());
1377 Constant *getFunctionConstant() const {
1378 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1379 return C->getValue();
1382 DITemplateParameterArray getTemplateParams() const {
1383 return cast_or_null<MDTuple>(getRawTemplateParams());
1385 DISubprogram *getDeclaration() const {
1386 return cast_or_null<DISubprogram>(getRawDeclaration());
1388 DILocalVariableArray getVariables() const {
1389 return cast_or_null<MDTuple>(getRawVariables());
1392 Metadata *getRawScope() const { return getOperand(1); }
1393 Metadata *getRawType() const { return getOperand(5); }
1394 Metadata *getRawContainingType() const { return getOperand(6); }
1395 Metadata *getRawFunction() const { return getOperand(7); }
1396 Metadata *getRawTemplateParams() const { return getOperand(8); }
1397 Metadata *getRawDeclaration() const { return getOperand(9); }
1398 Metadata *getRawVariables() const { return getOperand(10); }
1400 /// \brief Get a pointer to the function this subprogram describes.
1402 /// This dyn_casts \a getFunctionConstant() to \a Function.
1404 /// FIXME: Should this be looking through bitcasts?
1405 Function *getFunction() const;
1407 /// \brief Replace the function.
1409 /// If \a isUniqued() and not \a isResolved(), this could node will be
1410 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1411 /// that's a problem.
1413 void replaceFunction(Function *F);
1414 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1415 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1418 /// \brief Check if this subprogram decribes the given function.
1420 /// FIXME: Should this be looking through bitcasts?
1421 bool describes(const Function *F) const;
1423 static bool classof(const Metadata *MD) {
1424 return MD->getMetadataID() == DISubprogramKind;
1428 class DILexicalBlockBase : public DILocalScope {
1430 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1431 ArrayRef<Metadata *> Ops)
1432 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1433 ~DILexicalBlockBase() = default;
1436 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1438 Metadata *getRawScope() const { return getOperand(1); }
1440 /// \brief Forwarding accessors to LexicalBlock.
1442 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1444 inline unsigned getLine() const;
1445 inline unsigned getColumn() const;
1447 static bool classof(const Metadata *MD) {
1448 return MD->getMetadataID() == DILexicalBlockKind ||
1449 MD->getMetadataID() == DILexicalBlockFileKind;
1453 class DILexicalBlock : public DILexicalBlockBase {
1454 friend class LLVMContextImpl;
1455 friend class MDNode;
1460 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1461 unsigned Column, ArrayRef<Metadata *> Ops)
1462 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1464 ~DILexicalBlock() = default;
1466 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1467 DIFile *File, unsigned Line, unsigned Column,
1468 StorageType Storage,
1469 bool ShouldCreate = true) {
1470 return getImpl(Context, static_cast<Metadata *>(Scope),
1471 static_cast<Metadata *>(File), Line, Column, Storage,
1475 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1476 Metadata *File, unsigned Line, unsigned Column,
1477 StorageType Storage, bool ShouldCreate = true);
1479 TempDILexicalBlock cloneImpl() const {
1480 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1485 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1486 unsigned Line, unsigned Column),
1487 (Scope, File, Line, Column))
1488 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1489 unsigned Line, unsigned Column),
1490 (Scope, File, Line, Column))
1492 TempDILexicalBlock clone() const { return cloneImpl(); }
1494 unsigned getLine() const { return Line; }
1495 unsigned getColumn() const { return Column; }
1497 static bool classof(const Metadata *MD) {
1498 return MD->getMetadataID() == DILexicalBlockKind;
1502 unsigned DILexicalBlockBase::getLine() const {
1503 if (auto *N = dyn_cast<DILexicalBlock>(this))
1504 return N->getLine();
1508 unsigned DILexicalBlockBase::getColumn() const {
1509 if (auto *N = dyn_cast<DILexicalBlock>(this))
1510 return N->getColumn();
1514 class DILexicalBlockFile : public DILexicalBlockBase {
1515 friend class LLVMContextImpl;
1516 friend class MDNode;
1518 unsigned Discriminator;
1520 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1521 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1522 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1523 Discriminator(Discriminator) {}
1524 ~DILexicalBlockFile() = default;
1526 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1527 DIFile *File, unsigned Discriminator,
1528 StorageType Storage,
1529 bool ShouldCreate = true) {
1530 return getImpl(Context, static_cast<Metadata *>(Scope),
1531 static_cast<Metadata *>(File), Discriminator, Storage,
1535 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1536 Metadata *File, unsigned Discriminator,
1537 StorageType Storage,
1538 bool ShouldCreate = true);
1540 TempDILexicalBlockFile cloneImpl() const {
1541 return getTemporary(getContext(), getScope(), getFile(),
1542 getDiscriminator());
1546 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1547 unsigned Discriminator),
1548 (Scope, File, Discriminator))
1549 DEFINE_MDNODE_GET(DILexicalBlockFile,
1550 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1551 (Scope, File, Discriminator))
1553 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1555 // TODO: Remove these once they're gone from DILexicalBlockBase.
1556 unsigned getLine() const = delete;
1557 unsigned getColumn() const = delete;
1559 unsigned getDiscriminator() const { return Discriminator; }
1561 static bool classof(const Metadata *MD) {
1562 return MD->getMetadataID() == DILexicalBlockFileKind;
1566 unsigned DILocation::getDiscriminator() const {
1567 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1568 return F->getDiscriminator();
1572 class DINamespace : public DIScope {
1573 friend class LLVMContextImpl;
1574 friend class MDNode;
1578 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1579 ArrayRef<Metadata *> Ops)
1580 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1583 ~DINamespace() = default;
1585 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1586 DIFile *File, StringRef Name, unsigned Line,
1587 StorageType Storage, bool ShouldCreate = true) {
1588 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1589 Line, Storage, ShouldCreate);
1591 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1592 Metadata *File, MDString *Name, unsigned Line,
1593 StorageType Storage, bool ShouldCreate = true);
1595 TempDINamespace cloneImpl() const {
1596 return getTemporary(getContext(), getScope(), getFile(), getName(),
1601 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1603 (Scope, File, Name, Line))
1604 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1605 MDString *Name, unsigned Line),
1606 (Scope, File, Name, Line))
1608 TempDINamespace clone() const { return cloneImpl(); }
1610 unsigned getLine() const { return Line; }
1611 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1612 StringRef getName() const { return getStringOperand(2); }
1614 Metadata *getRawScope() const { return getOperand(1); }
1615 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1617 static bool classof(const Metadata *MD) {
1618 return MD->getMetadataID() == DINamespaceKind;
1622 /// \brief A (clang) module that has been imported by the compile unit.
1624 class DIModule : public DIScope {
1625 friend class LLVMContextImpl;
1626 friend class MDNode;
1628 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1629 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1632 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1633 StringRef Name, StringRef ConfigurationMacros,
1634 StringRef IncludePath, StringRef ISysRoot,
1635 StorageType Storage, bool ShouldCreate = true) {
1636 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1637 getCanonicalMDString(Context, ConfigurationMacros),
1638 getCanonicalMDString(Context, IncludePath),
1639 getCanonicalMDString(Context, ISysRoot),
1640 Storage, ShouldCreate);
1642 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1643 MDString *Name, MDString *ConfigurationMacros,
1644 MDString *IncludePath, MDString *ISysRoot,
1645 StorageType Storage, bool ShouldCreate = true);
1647 TempDIModule cloneImpl() const {
1648 return getTemporary(getContext(), getScope(), getName(),
1649 getConfigurationMacros(), getIncludePath(),
1654 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1655 StringRef ConfigurationMacros, StringRef IncludePath,
1656 StringRef ISysRoot),
1657 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1658 DEFINE_MDNODE_GET(DIModule,
1659 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1660 MDString *IncludePath, MDString *ISysRoot),
1661 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1663 TempDIModule clone() const { return cloneImpl(); }
1665 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1666 StringRef getName() const { return getStringOperand(1); }
1667 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1668 StringRef getIncludePath() const { return getStringOperand(3); }
1669 StringRef getISysRoot() const { return getStringOperand(4); }
1671 Metadata *getRawScope() const { return getOperand(0); }
1672 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1673 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1674 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1675 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1677 static bool classof(const Metadata *MD) {
1678 return MD->getMetadataID() == DIModuleKind;
1682 /// \brief Base class for template parameters.
1683 class DITemplateParameter : public DINode {
1685 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1686 unsigned Tag, ArrayRef<Metadata *> Ops)
1687 : DINode(Context, ID, Storage, Tag, Ops) {}
1688 ~DITemplateParameter() = default;
1691 StringRef getName() const { return getStringOperand(0); }
1692 DITypeRef getType() const { return DITypeRef(getRawType()); }
1694 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1695 Metadata *getRawType() const { return getOperand(1); }
1697 static bool classof(const Metadata *MD) {
1698 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1699 MD->getMetadataID() == DITemplateValueParameterKind;
1703 class DITemplateTypeParameter : public DITemplateParameter {
1704 friend class LLVMContextImpl;
1705 friend class MDNode;
1707 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1708 ArrayRef<Metadata *> Ops)
1709 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1710 dwarf::DW_TAG_template_type_parameter, Ops) {}
1711 ~DITemplateTypeParameter() = default;
1713 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1714 DITypeRef Type, StorageType Storage,
1715 bool ShouldCreate = true) {
1716 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1719 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1720 Metadata *Type, StorageType Storage,
1721 bool ShouldCreate = true);
1723 TempDITemplateTypeParameter cloneImpl() const {
1724 return getTemporary(getContext(), getName(), getType());
1728 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1730 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1733 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1735 static bool classof(const Metadata *MD) {
1736 return MD->getMetadataID() == DITemplateTypeParameterKind;
1740 class DITemplateValueParameter : public DITemplateParameter {
1741 friend class LLVMContextImpl;
1742 friend class MDNode;
1744 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1745 unsigned Tag, ArrayRef<Metadata *> Ops)
1746 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1748 ~DITemplateValueParameter() = default;
1750 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1751 StringRef Name, DITypeRef Type,
1752 Metadata *Value, StorageType Storage,
1753 bool ShouldCreate = true) {
1754 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1755 Value, Storage, ShouldCreate);
1757 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1758 MDString *Name, Metadata *Type,
1759 Metadata *Value, StorageType Storage,
1760 bool ShouldCreate = true);
1762 TempDITemplateValueParameter cloneImpl() const {
1763 return getTemporary(getContext(), getTag(), getName(), getType(),
1768 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1769 DITypeRef Type, Metadata *Value),
1770 (Tag, Name, Type, Value))
1771 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1772 Metadata *Type, Metadata *Value),
1773 (Tag, Name, Type, Value))
1775 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1777 Metadata *getValue() const { return getOperand(2); }
1779 static bool classof(const Metadata *MD) {
1780 return MD->getMetadataID() == DITemplateValueParameterKind;
1784 /// \brief Base class for variables.
1786 /// TODO: Hardcode to DW_TAG_variable.
1787 class DIVariable : public DINode {
1791 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1792 unsigned Line, ArrayRef<Metadata *> Ops)
1793 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1794 ~DIVariable() = default;
1797 unsigned getLine() const { return Line; }
1798 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1799 StringRef getName() const { return getStringOperand(1); }
1800 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1801 DITypeRef getType() const { return DITypeRef(getRawType()); }
1803 StringRef getFilename() const {
1804 if (auto *F = getFile())
1805 return F->getFilename();
1808 StringRef getDirectory() const {
1809 if (auto *F = getFile())
1810 return F->getDirectory();
1814 Metadata *getRawScope() const { return getOperand(0); }
1815 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1816 Metadata *getRawFile() const { return getOperand(2); }
1817 Metadata *getRawType() const { return getOperand(3); }
1819 static bool classof(const Metadata *MD) {
1820 return MD->getMetadataID() == DILocalVariableKind ||
1821 MD->getMetadataID() == DIGlobalVariableKind;
1825 /// \brief Global variables.
1827 /// TODO: Remove DisplayName. It's always equal to Name.
1828 class DIGlobalVariable : public DIVariable {
1829 friend class LLVMContextImpl;
1830 friend class MDNode;
1835 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1836 bool IsLocalToUnit, bool IsDefinition,
1837 ArrayRef<Metadata *> Ops)
1838 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1840 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1841 ~DIGlobalVariable() = default;
1843 static DIGlobalVariable *
1844 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1845 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1846 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1847 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1848 bool ShouldCreate = true) {
1849 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1850 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1851 IsLocalToUnit, IsDefinition,
1852 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1853 StaticDataMemberDeclaration, Storage, ShouldCreate);
1855 static DIGlobalVariable *
1856 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1857 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1858 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1859 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1860 bool ShouldCreate = true);
1862 TempDIGlobalVariable cloneImpl() const {
1863 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1864 getFile(), getLine(), getType(), isLocalToUnit(),
1865 isDefinition(), getVariable(),
1866 getStaticDataMemberDeclaration());
1870 DEFINE_MDNODE_GET(DIGlobalVariable,
1871 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1872 DIFile *File, unsigned Line, DITypeRef Type,
1873 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1874 DIDerivedType *StaticDataMemberDeclaration),
1875 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1876 IsDefinition, Variable, StaticDataMemberDeclaration))
1877 DEFINE_MDNODE_GET(DIGlobalVariable,
1878 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1879 Metadata *File, unsigned Line, Metadata *Type,
1880 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1881 Metadata *StaticDataMemberDeclaration),
1882 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1883 IsDefinition, Variable, StaticDataMemberDeclaration))
1885 TempDIGlobalVariable clone() const { return cloneImpl(); }
1887 bool isLocalToUnit() const { return IsLocalToUnit; }
1888 bool isDefinition() const { return IsDefinition; }
1889 StringRef getDisplayName() const { return getStringOperand(4); }
1890 StringRef getLinkageName() const { return getStringOperand(5); }
1891 Constant *getVariable() const {
1892 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1893 return dyn_cast<Constant>(C->getValue());
1896 DIDerivedType *getStaticDataMemberDeclaration() const {
1897 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1900 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1901 Metadata *getRawVariable() const { return getOperand(6); }
1902 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1904 static bool classof(const Metadata *MD) {
1905 return MD->getMetadataID() == DIGlobalVariableKind;
1909 /// \brief Local variable.
1911 /// TODO: Split between arguments and otherwise.
1912 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1913 /// TODO: Split up flags.
1914 class DILocalVariable : public DIVariable {
1915 friend class LLVMContextImpl;
1916 friend class MDNode;
1921 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1922 unsigned Line, unsigned Arg, unsigned Flags,
1923 ArrayRef<Metadata *> Ops)
1924 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1926 ~DILocalVariable() = default;
1928 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1929 DIScope *Scope, StringRef Name, DIFile *File,
1930 unsigned Line, DITypeRef Type, unsigned Arg,
1931 unsigned Flags, StorageType Storage,
1932 bool ShouldCreate = true) {
1933 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1934 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1936 static DILocalVariable *
1937 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1938 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1939 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1941 TempDILocalVariable cloneImpl() const {
1942 return getTemporary(getContext(), getTag(), getScope(), getName(),
1943 getFile(), getLine(), getType(), getArg(), getFlags());
1947 DEFINE_MDNODE_GET(DILocalVariable,
1948 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1949 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1951 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1952 DEFINE_MDNODE_GET(DILocalVariable,
1953 (unsigned Tag, Metadata *Scope, MDString *Name,
1954 Metadata *File, unsigned Line, Metadata *Type,
1955 unsigned Arg, unsigned Flags),
1956 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1958 TempDILocalVariable clone() const { return cloneImpl(); }
1960 /// \brief Get the local scope for this variable.
1962 /// Variables must be defined in a local scope.
1963 DILocalScope *getScope() const {
1964 return cast<DILocalScope>(DIVariable::getScope());
1967 unsigned getArg() const { return Arg; }
1968 unsigned getFlags() const { return Flags; }
1970 bool isArtificial() const { return getFlags() & FlagArtificial; }
1971 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1973 /// \brief Check that a location is valid for this variable.
1975 /// Check that \c DL exists, is in the same subprogram, and has the same
1976 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1977 /// to a \a DbgInfoIntrinsic.)
1978 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1979 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1982 static bool classof(const Metadata *MD) {
1983 return MD->getMetadataID() == DILocalVariableKind;
1987 /// \brief DWARF expression.
1989 /// This is (almost) a DWARF expression that modifies the location of a
1990 /// variable or (or the location of a single piece of a variable).
1992 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1993 /// and have DW_OP_plus consume the topmost elements on the stack.
1995 /// TODO: Co-allocate the expression elements.
1996 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1998 class DIExpression : public MDNode {
1999 friend class LLVMContextImpl;
2000 friend class MDNode;
2002 std::vector<uint64_t> Elements;
2004 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2005 : MDNode(C, DIExpressionKind, Storage, None),
2006 Elements(Elements.begin(), Elements.end()) {}
2007 ~DIExpression() = default;
2009 static DIExpression *getImpl(LLVMContext &Context,
2010 ArrayRef<uint64_t> Elements, StorageType Storage,
2011 bool ShouldCreate = true);
2013 TempDIExpression cloneImpl() const {
2014 return getTemporary(getContext(), getElements());
2018 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2020 TempDIExpression clone() const { return cloneImpl(); }
2022 ArrayRef<uint64_t> getElements() const { return Elements; }
2024 unsigned getNumElements() const { return Elements.size(); }
2025 uint64_t getElement(unsigned I) const {
2026 assert(I < Elements.size() && "Index out of range");
2030 /// \brief Return whether this is a piece of an aggregate variable.
2031 bool isBitPiece() const;
2033 /// \brief Return the offset of this piece in bits.
2034 uint64_t getBitPieceOffset() const;
2036 /// \brief Return the size of this piece in bits.
2037 uint64_t getBitPieceSize() const;
2039 typedef ArrayRef<uint64_t>::iterator element_iterator;
2040 element_iterator elements_begin() const { return getElements().begin(); }
2041 element_iterator elements_end() const { return getElements().end(); }
2043 /// \brief A lightweight wrapper around an expression operand.
2045 /// TODO: Store arguments directly and change \a DIExpression to store a
2051 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2053 const uint64_t *get() const { return Op; }
2055 /// \brief Get the operand code.
2056 uint64_t getOp() const { return *Op; }
2058 /// \brief Get an argument to the operand.
2060 /// Never returns the operand itself.
2061 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2063 unsigned getNumArgs() const { return getSize() - 1; }
2065 /// \brief Return the size of the operand.
2067 /// Return the number of elements in the operand (1 + args).
2068 unsigned getSize() const;
2071 /// \brief An iterator for expression operands.
2072 class expr_op_iterator
2073 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2077 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2079 element_iterator getBase() const { return Op.get(); }
2080 const ExprOperand &operator*() const { return Op; }
2081 const ExprOperand *operator->() const { return &Op; }
2083 expr_op_iterator &operator++() {
2087 expr_op_iterator operator++(int) {
2088 expr_op_iterator T(*this);
2093 /// \brief Get the next iterator.
2095 /// \a std::next() doesn't work because this is technically an
2096 /// input_iterator, but it's a perfectly valid operation. This is an
2097 /// accessor to provide the same functionality.
2098 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2100 bool operator==(const expr_op_iterator &X) const {
2101 return getBase() == X.getBase();
2103 bool operator!=(const expr_op_iterator &X) const {
2104 return getBase() != X.getBase();
2108 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2111 /// \brief Visit the elements via ExprOperand wrappers.
2113 /// These range iterators visit elements through \a ExprOperand wrappers.
2114 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2117 /// \pre \a isValid() gives \c true.
2119 expr_op_iterator expr_op_begin() const {
2120 return expr_op_iterator(elements_begin());
2122 expr_op_iterator expr_op_end() const {
2123 return expr_op_iterator(elements_end());
2127 bool isValid() const;
2129 static bool classof(const Metadata *MD) {
2130 return MD->getMetadataID() == DIExpressionKind;
2134 class DIObjCProperty : public DINode {
2135 friend class LLVMContextImpl;
2136 friend class MDNode;
2139 unsigned Attributes;
2141 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2142 unsigned Attributes, ArrayRef<Metadata *> Ops)
2143 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2145 Line(Line), Attributes(Attributes) {}
2146 ~DIObjCProperty() = default;
2148 static DIObjCProperty *
2149 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2150 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2151 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2152 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2153 getCanonicalMDString(Context, GetterName),
2154 getCanonicalMDString(Context, SetterName), Attributes, Type,
2155 Storage, ShouldCreate);
2157 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2158 Metadata *File, unsigned Line,
2159 MDString *GetterName, MDString *SetterName,
2160 unsigned Attributes, Metadata *Type,
2161 StorageType Storage, bool ShouldCreate = true);
2163 TempDIObjCProperty cloneImpl() const {
2164 return getTemporary(getContext(), getName(), getFile(), getLine(),
2165 getGetterName(), getSetterName(), getAttributes(),
2170 DEFINE_MDNODE_GET(DIObjCProperty,
2171 (StringRef Name, DIFile *File, unsigned Line,
2172 StringRef GetterName, StringRef SetterName,
2173 unsigned Attributes, DITypeRef Type),
2174 (Name, File, Line, GetterName, SetterName, Attributes,
2176 DEFINE_MDNODE_GET(DIObjCProperty,
2177 (MDString * Name, Metadata *File, unsigned Line,
2178 MDString *GetterName, MDString *SetterName,
2179 unsigned Attributes, Metadata *Type),
2180 (Name, File, Line, GetterName, SetterName, Attributes,
2183 TempDIObjCProperty clone() const { return cloneImpl(); }
2185 unsigned getLine() const { return Line; }
2186 unsigned getAttributes() const { return Attributes; }
2187 StringRef getName() const { return getStringOperand(0); }
2188 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2189 StringRef getGetterName() const { return getStringOperand(2); }
2190 StringRef getSetterName() const { return getStringOperand(3); }
2191 DITypeRef getType() const { return DITypeRef(getRawType()); }
2193 StringRef getFilename() const {
2194 if (auto *F = getFile())
2195 return F->getFilename();
2198 StringRef getDirectory() const {
2199 if (auto *F = getFile())
2200 return F->getDirectory();
2204 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2205 Metadata *getRawFile() const { return getOperand(1); }
2206 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2207 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2208 Metadata *getRawType() const { return getOperand(4); }
2210 static bool classof(const Metadata *MD) {
2211 return MD->getMetadataID() == DIObjCPropertyKind;
2215 /// \brief An imported module (C++ using directive or similar).
2216 class DIImportedEntity : public DINode {
2217 friend class LLVMContextImpl;
2218 friend class MDNode;
2222 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2223 unsigned Line, ArrayRef<Metadata *> Ops)
2224 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2225 ~DIImportedEntity() = default;
2227 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2228 DIScope *Scope, DINodeRef Entity,
2229 unsigned Line, StringRef Name,
2230 StorageType Storage,
2231 bool ShouldCreate = true) {
2232 return getImpl(Context, Tag, Scope, Entity, Line,
2233 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2235 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2236 Metadata *Scope, Metadata *Entity,
2237 unsigned Line, MDString *Name,
2238 StorageType Storage,
2239 bool ShouldCreate = true);
2241 TempDIImportedEntity cloneImpl() const {
2242 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2243 getLine(), getName());
2247 DEFINE_MDNODE_GET(DIImportedEntity,
2248 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2249 unsigned Line, StringRef Name = ""),
2250 (Tag, Scope, Entity, Line, Name))
2251 DEFINE_MDNODE_GET(DIImportedEntity,
2252 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2253 unsigned Line, MDString *Name),
2254 (Tag, Scope, Entity, Line, Name))
2256 TempDIImportedEntity clone() const { return cloneImpl(); }
2258 unsigned getLine() const { return Line; }
2259 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2260 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2261 StringRef getName() const { return getStringOperand(2); }
2263 Metadata *getRawScope() const { return getOperand(0); }
2264 Metadata *getRawEntity() const { return getOperand(1); }
2265 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2267 static bool classof(const Metadata *MD) {
2268 return MD->getMetadataID() == DIImportedEntityKind;
2272 } // end namespace llvm
2274 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2275 #undef DEFINE_MDNODE_GET_UNPACK
2276 #undef DEFINE_MDNODE_GET