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 Base class for DIDerivedType and DICompositeType.
652 /// TODO: Delete; they're not really related.
653 class DIDerivedTypeBase : public DIType {
655 DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
656 unsigned Tag, unsigned Line, uint64_t SizeInBits,
657 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
658 ArrayRef<Metadata *> Ops)
659 : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
661 ~DIDerivedTypeBase() = default;
664 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
665 Metadata *getRawBaseType() const { return getOperand(3); }
667 static bool classof(const Metadata *MD) {
668 return MD->getMetadataID() == DIDerivedTypeKind ||
669 MD->getMetadataID() == DICompositeTypeKind ||
670 MD->getMetadataID() == DISubroutineTypeKind;
674 /// \brief Derived types.
676 /// This includes qualified types, pointers, references, friends, typedefs, and
679 /// TODO: Split out members (inheritance, fields, methods, etc.).
680 class DIDerivedType : public DIDerivedTypeBase {
681 friend class LLVMContextImpl;
684 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
685 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
686 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
687 : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
688 AlignInBits, OffsetInBits, Flags, Ops) {}
689 ~DIDerivedType() = default;
691 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
692 StringRef Name, DIFile *File, unsigned Line,
693 DIScopeRef Scope, DITypeRef BaseType,
694 uint64_t SizeInBits, uint64_t AlignInBits,
695 uint64_t OffsetInBits, unsigned Flags,
696 Metadata *ExtraData, StorageType Storage,
697 bool ShouldCreate = true) {
698 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
699 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
700 Flags, ExtraData, Storage, ShouldCreate);
702 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
703 MDString *Name, Metadata *File, unsigned Line,
704 Metadata *Scope, Metadata *BaseType,
705 uint64_t SizeInBits, uint64_t AlignInBits,
706 uint64_t OffsetInBits, unsigned Flags,
707 Metadata *ExtraData, StorageType Storage,
708 bool ShouldCreate = true);
710 TempDIDerivedType cloneImpl() const {
711 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
712 getScope(), getBaseType(), getSizeInBits(),
713 getAlignInBits(), getOffsetInBits(), getFlags(),
718 DEFINE_MDNODE_GET(DIDerivedType,
719 (unsigned Tag, MDString *Name, Metadata *File,
720 unsigned Line, Metadata *Scope, Metadata *BaseType,
721 uint64_t SizeInBits, uint64_t AlignInBits,
722 uint64_t OffsetInBits, unsigned Flags,
723 Metadata *ExtraData = nullptr),
724 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
725 AlignInBits, OffsetInBits, Flags, ExtraData))
726 DEFINE_MDNODE_GET(DIDerivedType,
727 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
728 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
729 uint64_t AlignInBits, uint64_t OffsetInBits,
730 unsigned Flags, Metadata *ExtraData = nullptr),
731 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
732 AlignInBits, OffsetInBits, Flags, ExtraData))
734 TempDIDerivedType clone() const { return cloneImpl(); }
736 /// \brief Get extra data associated with this derived type.
738 /// Class type for pointer-to-members, objective-c property node for ivars,
739 /// or global constant wrapper for static members.
741 /// TODO: Separate out types that need this extra operand: pointer-to-member
742 /// types and member fields (static members and ivars).
743 Metadata *getExtraData() const { return getRawExtraData(); }
744 Metadata *getRawExtraData() const { return getOperand(4); }
746 /// \brief Get casted version of extra data.
748 DITypeRef getClassType() const {
749 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
750 return DITypeRef(getExtraData());
752 DIObjCProperty *getObjCProperty() const {
753 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
755 Constant *getConstant() const {
756 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
757 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
758 return C->getValue();
763 static bool classof(const Metadata *MD) {
764 return MD->getMetadataID() == DIDerivedTypeKind;
768 /// \brief Base class for DICompositeType and DISubroutineType.
770 /// TODO: Delete; they're not really related.
771 class DICompositeTypeBase : public DIDerivedTypeBase {
772 unsigned RuntimeLang;
775 DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
776 unsigned Tag, unsigned Line, unsigned RuntimeLang,
777 uint64_t SizeInBits, uint64_t AlignInBits,
778 uint64_t OffsetInBits, unsigned Flags,
779 ArrayRef<Metadata *> Ops)
780 : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
781 OffsetInBits, Flags, Ops),
782 RuntimeLang(RuntimeLang) {}
783 ~DICompositeTypeBase() = default;
786 /// \brief Get the elements of the composite type.
788 /// \note Calling this is only valid for \a DICompositeType. This assertion
789 /// can be removed once \a DISubroutineType has been separated from
790 /// "composite types".
791 DINodeArray getElements() const {
792 assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
793 return cast_or_null<MDTuple>(getRawElements());
795 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
796 DITemplateParameterArray getTemplateParams() const {
797 return cast_or_null<MDTuple>(getRawTemplateParams());
799 StringRef getIdentifier() const { return getStringOperand(7); }
800 unsigned getRuntimeLang() const { return RuntimeLang; }
802 Metadata *getRawElements() const { return getOperand(4); }
803 Metadata *getRawVTableHolder() const { return getOperand(5); }
804 Metadata *getRawTemplateParams() const { return getOperand(6); }
805 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
807 /// \brief Replace operands.
809 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
810 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
811 /// of its movement if necessary.
813 void replaceElements(DINodeArray Elements) {
815 for (DINode *Op : getElements())
816 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
817 "Lost a member during member list replacement");
819 replaceOperandWith(4, Elements.get());
821 void replaceVTableHolder(DITypeRef VTableHolder) {
822 replaceOperandWith(5, VTableHolder);
824 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
825 replaceOperandWith(6, TemplateParams.get());
829 static bool classof(const Metadata *MD) {
830 return MD->getMetadataID() == DICompositeTypeKind ||
831 MD->getMetadataID() == DISubroutineTypeKind;
835 /// \brief Composite types.
837 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
838 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
839 class DICompositeType : public DICompositeTypeBase {
840 friend class LLVMContextImpl;
843 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
844 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
845 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
846 ArrayRef<Metadata *> Ops)
847 : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
848 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
850 ~DICompositeType() = default;
852 static DICompositeType *
853 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
854 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
855 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
856 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
857 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
858 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
860 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
861 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
862 RuntimeLang, VTableHolder, TemplateParams.get(),
863 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
865 static DICompositeType *
866 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
867 unsigned Line, Metadata *Scope, Metadata *BaseType,
868 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
869 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
870 Metadata *VTableHolder, Metadata *TemplateParams,
871 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
873 TempDICompositeType cloneImpl() const {
874 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
875 getScope(), getBaseType(), getSizeInBits(),
876 getAlignInBits(), getOffsetInBits(), getFlags(),
877 getElements(), getRuntimeLang(), getVTableHolder(),
878 getTemplateParams(), getIdentifier());
882 DEFINE_MDNODE_GET(DICompositeType,
883 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
884 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
885 uint64_t AlignInBits, uint64_t OffsetInBits,
886 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
887 DITypeRef VTableHolder,
888 DITemplateParameterArray TemplateParams = nullptr,
889 StringRef Identifier = ""),
890 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
891 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
892 VTableHolder, TemplateParams, Identifier))
893 DEFINE_MDNODE_GET(DICompositeType,
894 (unsigned Tag, MDString *Name, Metadata *File,
895 unsigned Line, Metadata *Scope, Metadata *BaseType,
896 uint64_t SizeInBits, uint64_t AlignInBits,
897 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
898 unsigned RuntimeLang, Metadata *VTableHolder,
899 Metadata *TemplateParams = nullptr,
900 MDString *Identifier = nullptr),
901 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
902 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
903 VTableHolder, TemplateParams, Identifier))
905 TempDICompositeType clone() const { return cloneImpl(); }
907 static bool classof(const Metadata *MD) {
908 return MD->getMetadataID() == DICompositeTypeKind;
912 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
914 if (auto *Composite = dyn_cast<DICompositeType>(N))
915 if (auto *S = Composite->getRawIdentifier())
916 return TypedDINodeRef<T>(S);
917 return TypedDINodeRef<T>(N);
920 /// \brief Type array for a subprogram.
922 /// TODO: Detach from CompositeType, and fold the array of types in directly
924 class DISubroutineType : public DICompositeTypeBase {
925 friend class LLVMContextImpl;
928 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
929 ArrayRef<Metadata *> Ops)
930 : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
931 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
933 ~DISubroutineType() = default;
935 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
936 DITypeRefArray TypeArray,
938 bool ShouldCreate = true) {
939 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
941 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
942 Metadata *TypeArray, StorageType Storage,
943 bool ShouldCreate = true);
945 TempDISubroutineType cloneImpl() const {
946 return getTemporary(getContext(), getFlags(), getTypeArray());
950 DEFINE_MDNODE_GET(DISubroutineType,
951 (unsigned Flags, DITypeRefArray TypeArray),
953 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
956 TempDISubroutineType clone() const { return cloneImpl(); }
958 DITypeRefArray getTypeArray() const {
959 return cast_or_null<MDTuple>(getRawTypeArray());
961 Metadata *getRawTypeArray() const { return getRawElements(); }
963 static bool classof(const Metadata *MD) {
964 return MD->getMetadataID() == DISubroutineTypeKind;
968 /// \brief Compile unit.
969 class DICompileUnit : public DIScope {
970 friend class LLVMContextImpl;
973 unsigned SourceLanguage;
975 unsigned RuntimeVersion;
976 unsigned EmissionKind;
979 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
980 bool IsOptimized, unsigned RuntimeVersion,
981 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
982 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
983 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
984 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
986 ~DICompileUnit() = default;
988 static DICompileUnit *
989 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
990 StringRef Producer, bool IsOptimized, StringRef Flags,
991 unsigned RuntimeVersion, StringRef SplitDebugFilename,
992 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
993 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
994 DIGlobalVariableArray GlobalVariables,
995 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
996 StorageType Storage, bool ShouldCreate = true) {
997 return getImpl(Context, SourceLanguage, File,
998 getCanonicalMDString(Context, Producer), IsOptimized,
999 getCanonicalMDString(Context, Flags), RuntimeVersion,
1000 getCanonicalMDString(Context, SplitDebugFilename),
1001 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
1002 Subprograms.get(), GlobalVariables.get(),
1003 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
1005 static DICompileUnit *
1006 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1007 MDString *Producer, bool IsOptimized, MDString *Flags,
1008 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1009 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1010 Metadata *Subprograms, Metadata *GlobalVariables,
1011 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
1012 bool ShouldCreate = true);
1014 TempDICompileUnit cloneImpl() const {
1015 return getTemporary(
1016 getContext(), getSourceLanguage(), getFile(), getProducer(),
1017 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1018 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1019 getGlobalVariables(), getImportedEntities(), DWOId);
1023 DEFINE_MDNODE_GET(DICompileUnit,
1024 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1025 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1026 StringRef SplitDebugFilename, unsigned EmissionKind,
1027 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1028 DISubprogramArray Subprograms,
1029 DIGlobalVariableArray GlobalVariables,
1030 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1031 (SourceLanguage, File, Producer, IsOptimized, Flags,
1032 RuntimeVersion, SplitDebugFilename, EmissionKind,
1033 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1034 ImportedEntities, DWOId))
1037 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1038 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1039 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1040 Metadata *RetainedTypes, Metadata *Subprograms,
1041 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1042 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1043 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1044 GlobalVariables, ImportedEntities, DWOId))
1046 TempDICompileUnit clone() const { return cloneImpl(); }
1048 unsigned getSourceLanguage() const { return SourceLanguage; }
1049 bool isOptimized() const { return IsOptimized; }
1050 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1051 unsigned getEmissionKind() const { return EmissionKind; }
1052 StringRef getProducer() const { return getStringOperand(1); }
1053 StringRef getFlags() const { return getStringOperand(2); }
1054 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1055 DICompositeTypeArray getEnumTypes() const {
1056 return cast_or_null<MDTuple>(getRawEnumTypes());
1058 DITypeArray getRetainedTypes() const {
1059 return cast_or_null<MDTuple>(getRawRetainedTypes());
1061 DISubprogramArray getSubprograms() const {
1062 return cast_or_null<MDTuple>(getRawSubprograms());
1064 DIGlobalVariableArray getGlobalVariables() const {
1065 return cast_or_null<MDTuple>(getRawGlobalVariables());
1067 DIImportedEntityArray getImportedEntities() const {
1068 return cast_or_null<MDTuple>(getRawImportedEntities());
1070 unsigned getDWOId() const { return DWOId; }
1072 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1073 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1074 MDString *getRawSplitDebugFilename() const {
1075 return getOperandAs<MDString>(3);
1077 Metadata *getRawEnumTypes() const { return getOperand(4); }
1078 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1079 Metadata *getRawSubprograms() const { return getOperand(6); }
1080 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1081 Metadata *getRawImportedEntities() const { return getOperand(8); }
1083 /// \brief Replace arrays.
1085 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1086 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1087 /// DICompileUnit should be fairly rare.
1089 void replaceEnumTypes(DICompositeTypeArray N) {
1090 replaceOperandWith(4, N.get());
1092 void replaceRetainedTypes(DITypeArray N) {
1093 replaceOperandWith(5, N.get());
1095 void replaceSubprograms(DISubprogramArray N) {
1096 replaceOperandWith(6, N.get());
1098 void replaceGlobalVariables(DIGlobalVariableArray N) {
1099 replaceOperandWith(7, N.get());
1101 void replaceImportedEntities(DIImportedEntityArray N) {
1102 replaceOperandWith(8, N.get());
1106 static bool classof(const Metadata *MD) {
1107 return MD->getMetadataID() == DICompileUnitKind;
1111 /// \brief A scope for locals.
1113 /// A legal scope for lexical blocks, local variables, and debug info
1114 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1115 /// DILexicalBlockFile.
1116 class DILocalScope : public DIScope {
1118 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1119 ArrayRef<Metadata *> Ops)
1120 : DIScope(C, ID, Storage, Tag, Ops) {}
1121 ~DILocalScope() = default;
1124 /// \brief Get the subprogram for this scope.
1126 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1128 DISubprogram *getSubprogram() const;
1130 static bool classof(const Metadata *MD) {
1131 return MD->getMetadataID() == DISubprogramKind ||
1132 MD->getMetadataID() == DILexicalBlockKind ||
1133 MD->getMetadataID() == DILexicalBlockFileKind;
1137 /// \brief Debug location.
1139 /// A debug location in source code, used for debug info and otherwise.
1140 class DILocation : public MDNode {
1141 friend class LLVMContextImpl;
1142 friend class MDNode;
1144 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1145 unsigned Column, ArrayRef<Metadata *> MDs);
1146 ~DILocation() { dropAllReferences(); }
1148 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1149 unsigned Column, Metadata *Scope,
1150 Metadata *InlinedAt, StorageType Storage,
1151 bool ShouldCreate = true);
1152 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1153 unsigned Column, DILocalScope *Scope,
1154 DILocation *InlinedAt, StorageType Storage,
1155 bool ShouldCreate = true) {
1156 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1157 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1160 TempDILocation cloneImpl() const {
1161 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1165 // Disallow replacing operands.
1166 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1169 DEFINE_MDNODE_GET(DILocation,
1170 (unsigned Line, unsigned Column, Metadata *Scope,
1171 Metadata *InlinedAt = nullptr),
1172 (Line, Column, Scope, InlinedAt))
1173 DEFINE_MDNODE_GET(DILocation,
1174 (unsigned Line, unsigned Column, DILocalScope *Scope,
1175 DILocation *InlinedAt = nullptr),
1176 (Line, Column, Scope, InlinedAt))
1178 /// \brief Return a (temporary) clone of this.
1179 TempDILocation clone() const { return cloneImpl(); }
1181 unsigned getLine() const { return SubclassData32; }
1182 unsigned getColumn() const { return SubclassData16; }
1183 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1184 DILocation *getInlinedAt() const {
1185 return cast_or_null<DILocation>(getRawInlinedAt());
1188 DIFile *getFile() const { return getScope()->getFile(); }
1189 StringRef getFilename() const { return getScope()->getFilename(); }
1190 StringRef getDirectory() const { return getScope()->getDirectory(); }
1192 /// \brief Get the scope where this is inlined.
1194 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1196 DILocalScope *getInlinedAtScope() const {
1197 if (auto *IA = getInlinedAt())
1198 return IA->getInlinedAtScope();
1202 /// \brief Check whether this can be discriminated from another location.
1204 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1205 /// Scope and inlined-at chains are not recorded in the linetable, so they
1206 /// cannot be used to distinguish basic blocks.
1208 /// The current implementation is weaker than it should be, since it just
1209 /// checks filename and line.
1211 /// FIXME: Add a check for getDiscriminator().
1212 /// FIXME: Add a check for getColumn().
1213 /// FIXME: Change the getFilename() check to getFile() (or add one for
1214 /// getDirectory()).
1215 bool canDiscriminate(const DILocation &RHS) const {
1216 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1219 /// \brief Get the DWARF discriminator.
1221 /// DWARF discriminators distinguish identical file locations between
1222 /// instructions that are on different basic blocks.
1223 inline unsigned getDiscriminator() const;
1225 /// \brief Compute new discriminator in the given context.
1227 /// This modifies the \a LLVMContext that \c this is in to increment the next
1228 /// discriminator for \c this's line/filename combination.
1230 /// FIXME: Delete this. See comments in implementation and at the only call
1231 /// site in \a AddDiscriminators::runOnFunction().
1232 unsigned computeNewDiscriminator() const;
1234 Metadata *getRawScope() const { return getOperand(0); }
1235 Metadata *getRawInlinedAt() const {
1236 if (getNumOperands() == 2)
1237 return getOperand(1);
1241 static bool classof(const Metadata *MD) {
1242 return MD->getMetadataID() == DILocationKind;
1246 /// \brief Subprogram description.
1248 /// TODO: Remove DisplayName. It's always equal to Name.
1249 /// TODO: Split up flags.
1250 class DISubprogram : public DILocalScope {
1251 friend class LLVMContextImpl;
1252 friend class MDNode;
1256 unsigned Virtuality;
1257 unsigned VirtualIndex;
1263 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1264 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1265 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1266 bool IsOptimized, ArrayRef<Metadata *> Ops)
1267 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1269 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1270 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1271 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1272 ~DISubprogram() = default;
1274 static DISubprogram *
1275 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1276 StringRef LinkageName, DIFile *File, unsigned Line,
1277 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1278 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1279 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1280 Constant *Function, DITemplateParameterArray TemplateParams,
1281 DISubprogram *Declaration, DILocalVariableArray Variables,
1282 StorageType Storage, bool ShouldCreate = true) {
1283 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1284 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1285 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1286 Virtuality, VirtualIndex, Flags, IsOptimized,
1287 Function ? ConstantAsMetadata::get(Function) : nullptr,
1288 TemplateParams.get(), Declaration, Variables.get(), Storage,
1291 static DISubprogram *
1292 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1293 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1294 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1295 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1296 unsigned Flags, bool IsOptimized, Metadata *Function,
1297 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1298 StorageType Storage, bool ShouldCreate = true);
1300 TempDISubprogram cloneImpl() const {
1301 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1302 getFile(), getLine(), getType(), isLocalToUnit(),
1303 isDefinition(), getScopeLine(), getContainingType(),
1304 getVirtuality(), getVirtualIndex(), getFlags(),
1305 isOptimized(), getFunctionConstant(),
1306 getTemplateParams(), getDeclaration(), getVariables());
1310 DEFINE_MDNODE_GET(DISubprogram,
1311 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1312 DIFile *File, unsigned Line, DISubroutineType *Type,
1313 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1314 DITypeRef ContainingType, unsigned Virtuality,
1315 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1316 Constant *Function = nullptr,
1317 DITemplateParameterArray TemplateParams = nullptr,
1318 DISubprogram *Declaration = nullptr,
1319 DILocalVariableArray Variables = nullptr),
1320 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1321 IsDefinition, ScopeLine, ContainingType, Virtuality,
1322 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1323 Declaration, Variables))
1326 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1327 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1328 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1329 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1330 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1331 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1332 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1333 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1334 Function, TemplateParams, Declaration, Variables))
1336 TempDISubprogram clone() const { return cloneImpl(); }
1339 unsigned getLine() const { return Line; }
1340 unsigned getVirtuality() const { return Virtuality; }
1341 unsigned getVirtualIndex() const { return VirtualIndex; }
1342 unsigned getScopeLine() const { return ScopeLine; }
1343 unsigned getFlags() const { return Flags; }
1344 bool isLocalToUnit() const { return IsLocalToUnit; }
1345 bool isDefinition() const { return IsDefinition; }
1346 bool isOptimized() const { return IsOptimized; }
1348 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1349 bool isPrivate() const {
1350 return (getFlags() & FlagAccessibility) == FlagPrivate;
1352 bool isProtected() const {
1353 return (getFlags() & FlagAccessibility) == FlagProtected;
1355 bool isPublic() const {
1356 return (getFlags() & FlagAccessibility) == FlagPublic;
1358 bool isExplicit() const { return getFlags() & FlagExplicit; }
1359 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1361 /// \brief Check if this is reference-qualified.
1363 /// Return true if this subprogram is a C++11 reference-qualified non-static
1364 /// member function (void foo() &).
1365 unsigned isLValueReference() const {
1366 return getFlags() & FlagLValueReference;
1369 /// \brief Check if this is rvalue-reference-qualified.
1371 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1372 /// non-static member function (void foo() &&).
1373 unsigned isRValueReference() const {
1374 return getFlags() & FlagRValueReference;
1377 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1379 StringRef getName() const { return getStringOperand(2); }
1380 StringRef getDisplayName() const { return getStringOperand(3); }
1381 StringRef getLinkageName() const { return getStringOperand(4); }
1383 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1384 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1386 DISubroutineType *getType() const {
1387 return cast_or_null<DISubroutineType>(getRawType());
1389 DITypeRef getContainingType() const {
1390 return DITypeRef(getRawContainingType());
1393 Constant *getFunctionConstant() const {
1394 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1395 return C->getValue();
1398 DITemplateParameterArray getTemplateParams() const {
1399 return cast_or_null<MDTuple>(getRawTemplateParams());
1401 DISubprogram *getDeclaration() const {
1402 return cast_or_null<DISubprogram>(getRawDeclaration());
1404 DILocalVariableArray getVariables() const {
1405 return cast_or_null<MDTuple>(getRawVariables());
1408 Metadata *getRawScope() const { return getOperand(1); }
1409 Metadata *getRawType() const { return getOperand(5); }
1410 Metadata *getRawContainingType() const { return getOperand(6); }
1411 Metadata *getRawFunction() const { return getOperand(7); }
1412 Metadata *getRawTemplateParams() const { return getOperand(8); }
1413 Metadata *getRawDeclaration() const { return getOperand(9); }
1414 Metadata *getRawVariables() const { return getOperand(10); }
1416 /// \brief Get a pointer to the function this subprogram describes.
1418 /// This dyn_casts \a getFunctionConstant() to \a Function.
1420 /// FIXME: Should this be looking through bitcasts?
1421 Function *getFunction() const;
1423 /// \brief Replace the function.
1425 /// If \a isUniqued() and not \a isResolved(), this could node will be
1426 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1427 /// that's a problem.
1429 void replaceFunction(Function *F);
1430 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1431 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1434 /// \brief Check if this subprogram decribes the given function.
1436 /// FIXME: Should this be looking through bitcasts?
1437 bool describes(const Function *F) const;
1439 static bool classof(const Metadata *MD) {
1440 return MD->getMetadataID() == DISubprogramKind;
1444 class DILexicalBlockBase : public DILocalScope {
1446 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1447 ArrayRef<Metadata *> Ops)
1448 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1449 ~DILexicalBlockBase() = default;
1452 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1454 Metadata *getRawScope() const { return getOperand(1); }
1456 /// \brief Forwarding accessors to LexicalBlock.
1458 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1460 inline unsigned getLine() const;
1461 inline unsigned getColumn() const;
1463 static bool classof(const Metadata *MD) {
1464 return MD->getMetadataID() == DILexicalBlockKind ||
1465 MD->getMetadataID() == DILexicalBlockFileKind;
1469 class DILexicalBlock : public DILexicalBlockBase {
1470 friend class LLVMContextImpl;
1471 friend class MDNode;
1476 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1477 unsigned Column, ArrayRef<Metadata *> Ops)
1478 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1480 ~DILexicalBlock() = default;
1482 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1483 DIFile *File, unsigned Line, unsigned Column,
1484 StorageType Storage,
1485 bool ShouldCreate = true) {
1486 return getImpl(Context, static_cast<Metadata *>(Scope),
1487 static_cast<Metadata *>(File), Line, Column, Storage,
1491 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1492 Metadata *File, unsigned Line, unsigned Column,
1493 StorageType Storage, bool ShouldCreate = true);
1495 TempDILexicalBlock cloneImpl() const {
1496 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1501 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1502 unsigned Line, unsigned Column),
1503 (Scope, File, Line, Column))
1504 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1505 unsigned Line, unsigned Column),
1506 (Scope, File, Line, Column))
1508 TempDILexicalBlock clone() const { return cloneImpl(); }
1510 unsigned getLine() const { return Line; }
1511 unsigned getColumn() const { return Column; }
1513 static bool classof(const Metadata *MD) {
1514 return MD->getMetadataID() == DILexicalBlockKind;
1518 unsigned DILexicalBlockBase::getLine() const {
1519 if (auto *N = dyn_cast<DILexicalBlock>(this))
1520 return N->getLine();
1524 unsigned DILexicalBlockBase::getColumn() const {
1525 if (auto *N = dyn_cast<DILexicalBlock>(this))
1526 return N->getColumn();
1530 class DILexicalBlockFile : public DILexicalBlockBase {
1531 friend class LLVMContextImpl;
1532 friend class MDNode;
1534 unsigned Discriminator;
1536 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1537 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1538 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1539 Discriminator(Discriminator) {}
1540 ~DILexicalBlockFile() = default;
1542 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1543 DIFile *File, unsigned Discriminator,
1544 StorageType Storage,
1545 bool ShouldCreate = true) {
1546 return getImpl(Context, static_cast<Metadata *>(Scope),
1547 static_cast<Metadata *>(File), Discriminator, Storage,
1551 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1552 Metadata *File, unsigned Discriminator,
1553 StorageType Storage,
1554 bool ShouldCreate = true);
1556 TempDILexicalBlockFile cloneImpl() const {
1557 return getTemporary(getContext(), getScope(), getFile(),
1558 getDiscriminator());
1562 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1563 unsigned Discriminator),
1564 (Scope, File, Discriminator))
1565 DEFINE_MDNODE_GET(DILexicalBlockFile,
1566 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1567 (Scope, File, Discriminator))
1569 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1571 // TODO: Remove these once they're gone from DILexicalBlockBase.
1572 unsigned getLine() const = delete;
1573 unsigned getColumn() const = delete;
1575 unsigned getDiscriminator() const { return Discriminator; }
1577 static bool classof(const Metadata *MD) {
1578 return MD->getMetadataID() == DILexicalBlockFileKind;
1582 unsigned DILocation::getDiscriminator() const {
1583 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1584 return F->getDiscriminator();
1588 class DINamespace : public DIScope {
1589 friend class LLVMContextImpl;
1590 friend class MDNode;
1594 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1595 ArrayRef<Metadata *> Ops)
1596 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1599 ~DINamespace() = default;
1601 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1602 DIFile *File, StringRef Name, unsigned Line,
1603 StorageType Storage, bool ShouldCreate = true) {
1604 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1605 Line, Storage, ShouldCreate);
1607 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1608 Metadata *File, MDString *Name, unsigned Line,
1609 StorageType Storage, bool ShouldCreate = true);
1611 TempDINamespace cloneImpl() const {
1612 return getTemporary(getContext(), getScope(), getFile(), getName(),
1617 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1619 (Scope, File, Name, Line))
1620 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1621 MDString *Name, unsigned Line),
1622 (Scope, File, Name, Line))
1624 TempDINamespace clone() const { return cloneImpl(); }
1626 unsigned getLine() const { return Line; }
1627 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1628 StringRef getName() const { return getStringOperand(2); }
1630 Metadata *getRawScope() const { return getOperand(1); }
1631 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1633 static bool classof(const Metadata *MD) {
1634 return MD->getMetadataID() == DINamespaceKind;
1638 /// \brief A (clang) module that has been imported by the compile unit.
1640 class DIModule : public DIScope {
1641 friend class LLVMContextImpl;
1642 friend class MDNode;
1644 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1645 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1648 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1649 StringRef Name, StringRef ConfigurationMacros,
1650 StringRef IncludePath, StringRef ISysRoot,
1651 StorageType Storage, bool ShouldCreate = true) {
1652 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1653 getCanonicalMDString(Context, ConfigurationMacros),
1654 getCanonicalMDString(Context, IncludePath),
1655 getCanonicalMDString(Context, ISysRoot),
1656 Storage, ShouldCreate);
1658 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1659 MDString *Name, MDString *ConfigurationMacros,
1660 MDString *IncludePath, MDString *ISysRoot,
1661 StorageType Storage, bool ShouldCreate = true);
1663 TempDIModule cloneImpl() const {
1664 return getTemporary(getContext(), getScope(), getName(),
1665 getConfigurationMacros(), getIncludePath(),
1670 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1671 StringRef ConfigurationMacros, StringRef IncludePath,
1672 StringRef ISysRoot),
1673 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1674 DEFINE_MDNODE_GET(DIModule,
1675 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1676 MDString *IncludePath, MDString *ISysRoot),
1677 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1679 TempDIModule clone() const { return cloneImpl(); }
1681 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1682 StringRef getName() const { return getStringOperand(1); }
1683 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1684 StringRef getIncludePath() const { return getStringOperand(3); }
1685 StringRef getISysRoot() const { return getStringOperand(4); }
1687 Metadata *getRawScope() const { return getOperand(0); }
1688 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1689 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1690 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1691 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1693 static bool classof(const Metadata *MD) {
1694 return MD->getMetadataID() == DIModuleKind;
1698 /// \brief Base class for template parameters.
1699 class DITemplateParameter : public DINode {
1701 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1702 unsigned Tag, ArrayRef<Metadata *> Ops)
1703 : DINode(Context, ID, Storage, Tag, Ops) {}
1704 ~DITemplateParameter() = default;
1707 StringRef getName() const { return getStringOperand(0); }
1708 DITypeRef getType() const { return DITypeRef(getRawType()); }
1710 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1711 Metadata *getRawType() const { return getOperand(1); }
1713 static bool classof(const Metadata *MD) {
1714 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1715 MD->getMetadataID() == DITemplateValueParameterKind;
1719 class DITemplateTypeParameter : public DITemplateParameter {
1720 friend class LLVMContextImpl;
1721 friend class MDNode;
1723 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1724 ArrayRef<Metadata *> Ops)
1725 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1726 dwarf::DW_TAG_template_type_parameter, Ops) {}
1727 ~DITemplateTypeParameter() = default;
1729 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1730 DITypeRef Type, StorageType Storage,
1731 bool ShouldCreate = true) {
1732 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1735 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1736 Metadata *Type, StorageType Storage,
1737 bool ShouldCreate = true);
1739 TempDITemplateTypeParameter cloneImpl() const {
1740 return getTemporary(getContext(), getName(), getType());
1744 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1746 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1749 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1751 static bool classof(const Metadata *MD) {
1752 return MD->getMetadataID() == DITemplateTypeParameterKind;
1756 class DITemplateValueParameter : public DITemplateParameter {
1757 friend class LLVMContextImpl;
1758 friend class MDNode;
1760 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1761 unsigned Tag, ArrayRef<Metadata *> Ops)
1762 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1764 ~DITemplateValueParameter() = default;
1766 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1767 StringRef Name, DITypeRef Type,
1768 Metadata *Value, StorageType Storage,
1769 bool ShouldCreate = true) {
1770 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1771 Value, Storage, ShouldCreate);
1773 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1774 MDString *Name, Metadata *Type,
1775 Metadata *Value, StorageType Storage,
1776 bool ShouldCreate = true);
1778 TempDITemplateValueParameter cloneImpl() const {
1779 return getTemporary(getContext(), getTag(), getName(), getType(),
1784 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1785 DITypeRef Type, Metadata *Value),
1786 (Tag, Name, Type, Value))
1787 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1788 Metadata *Type, Metadata *Value),
1789 (Tag, Name, Type, Value))
1791 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1793 Metadata *getValue() const { return getOperand(2); }
1795 static bool classof(const Metadata *MD) {
1796 return MD->getMetadataID() == DITemplateValueParameterKind;
1800 /// \brief Base class for variables.
1802 /// TODO: Hardcode to DW_TAG_variable.
1803 class DIVariable : public DINode {
1807 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1808 unsigned Line, ArrayRef<Metadata *> Ops)
1809 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1810 ~DIVariable() = default;
1813 unsigned getLine() const { return Line; }
1814 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1815 StringRef getName() const { return getStringOperand(1); }
1816 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1817 DITypeRef getType() const { return DITypeRef(getRawType()); }
1819 StringRef getFilename() const {
1820 if (auto *F = getFile())
1821 return F->getFilename();
1824 StringRef getDirectory() const {
1825 if (auto *F = getFile())
1826 return F->getDirectory();
1830 Metadata *getRawScope() const { return getOperand(0); }
1831 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1832 Metadata *getRawFile() const { return getOperand(2); }
1833 Metadata *getRawType() const { return getOperand(3); }
1835 static bool classof(const Metadata *MD) {
1836 return MD->getMetadataID() == DILocalVariableKind ||
1837 MD->getMetadataID() == DIGlobalVariableKind;
1841 /// \brief Global variables.
1843 /// TODO: Remove DisplayName. It's always equal to Name.
1844 class DIGlobalVariable : public DIVariable {
1845 friend class LLVMContextImpl;
1846 friend class MDNode;
1851 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1852 bool IsLocalToUnit, bool IsDefinition,
1853 ArrayRef<Metadata *> Ops)
1854 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1856 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1857 ~DIGlobalVariable() = default;
1859 static DIGlobalVariable *
1860 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1861 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1862 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1863 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1864 bool ShouldCreate = true) {
1865 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1866 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1867 IsLocalToUnit, IsDefinition,
1868 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1869 StaticDataMemberDeclaration, Storage, ShouldCreate);
1871 static DIGlobalVariable *
1872 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1873 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1874 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1875 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1876 bool ShouldCreate = true);
1878 TempDIGlobalVariable cloneImpl() const {
1879 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1880 getFile(), getLine(), getType(), isLocalToUnit(),
1881 isDefinition(), getVariable(),
1882 getStaticDataMemberDeclaration());
1886 DEFINE_MDNODE_GET(DIGlobalVariable,
1887 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1888 DIFile *File, unsigned Line, DITypeRef Type,
1889 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1890 DIDerivedType *StaticDataMemberDeclaration),
1891 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1892 IsDefinition, Variable, StaticDataMemberDeclaration))
1893 DEFINE_MDNODE_GET(DIGlobalVariable,
1894 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1895 Metadata *File, unsigned Line, Metadata *Type,
1896 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1897 Metadata *StaticDataMemberDeclaration),
1898 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1899 IsDefinition, Variable, StaticDataMemberDeclaration))
1901 TempDIGlobalVariable clone() const { return cloneImpl(); }
1903 bool isLocalToUnit() const { return IsLocalToUnit; }
1904 bool isDefinition() const { return IsDefinition; }
1905 StringRef getDisplayName() const { return getStringOperand(4); }
1906 StringRef getLinkageName() const { return getStringOperand(5); }
1907 Constant *getVariable() const {
1908 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1909 return dyn_cast<Constant>(C->getValue());
1912 DIDerivedType *getStaticDataMemberDeclaration() const {
1913 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1916 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1917 Metadata *getRawVariable() const { return getOperand(6); }
1918 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1920 static bool classof(const Metadata *MD) {
1921 return MD->getMetadataID() == DIGlobalVariableKind;
1925 /// \brief Local variable.
1927 /// TODO: Split between arguments and otherwise.
1928 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1929 /// TODO: Split up flags.
1930 class DILocalVariable : public DIVariable {
1931 friend class LLVMContextImpl;
1932 friend class MDNode;
1937 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1938 unsigned Line, unsigned Arg, unsigned Flags,
1939 ArrayRef<Metadata *> Ops)
1940 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1942 ~DILocalVariable() = default;
1944 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1945 DIScope *Scope, StringRef Name, DIFile *File,
1946 unsigned Line, DITypeRef Type, unsigned Arg,
1947 unsigned Flags, StorageType Storage,
1948 bool ShouldCreate = true) {
1949 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1950 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1952 static DILocalVariable *
1953 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1954 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1955 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1957 TempDILocalVariable cloneImpl() const {
1958 return getTemporary(getContext(), getTag(), getScope(), getName(),
1959 getFile(), getLine(), getType(), getArg(), getFlags());
1963 DEFINE_MDNODE_GET(DILocalVariable,
1964 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1965 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1967 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1968 DEFINE_MDNODE_GET(DILocalVariable,
1969 (unsigned Tag, Metadata *Scope, MDString *Name,
1970 Metadata *File, unsigned Line, Metadata *Type,
1971 unsigned Arg, unsigned Flags),
1972 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1974 TempDILocalVariable clone() const { return cloneImpl(); }
1976 /// \brief Get the local scope for this variable.
1978 /// Variables must be defined in a local scope.
1979 DILocalScope *getScope() const {
1980 return cast<DILocalScope>(DIVariable::getScope());
1983 unsigned getArg() const { return Arg; }
1984 unsigned getFlags() const { return Flags; }
1986 bool isArtificial() const { return getFlags() & FlagArtificial; }
1987 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1989 /// \brief Check that a location is valid for this variable.
1991 /// Check that \c DL exists, is in the same subprogram, and has the same
1992 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1993 /// to a \a DbgInfoIntrinsic.)
1994 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1995 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1998 static bool classof(const Metadata *MD) {
1999 return MD->getMetadataID() == DILocalVariableKind;
2003 /// \brief DWARF expression.
2005 /// This is (almost) a DWARF expression that modifies the location of a
2006 /// variable or (or the location of a single piece of a variable).
2008 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
2009 /// and have DW_OP_plus consume the topmost elements on the stack.
2011 /// TODO: Co-allocate the expression elements.
2012 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2014 class DIExpression : public MDNode {
2015 friend class LLVMContextImpl;
2016 friend class MDNode;
2018 std::vector<uint64_t> Elements;
2020 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2021 : MDNode(C, DIExpressionKind, Storage, None),
2022 Elements(Elements.begin(), Elements.end()) {}
2023 ~DIExpression() = default;
2025 static DIExpression *getImpl(LLVMContext &Context,
2026 ArrayRef<uint64_t> Elements, StorageType Storage,
2027 bool ShouldCreate = true);
2029 TempDIExpression cloneImpl() const {
2030 return getTemporary(getContext(), getElements());
2034 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2036 TempDIExpression clone() const { return cloneImpl(); }
2038 ArrayRef<uint64_t> getElements() const { return Elements; }
2040 unsigned getNumElements() const { return Elements.size(); }
2041 uint64_t getElement(unsigned I) const {
2042 assert(I < Elements.size() && "Index out of range");
2046 /// \brief Return whether this is a piece of an aggregate variable.
2047 bool isBitPiece() const;
2049 /// \brief Return the offset of this piece in bits.
2050 uint64_t getBitPieceOffset() const;
2052 /// \brief Return the size of this piece in bits.
2053 uint64_t getBitPieceSize() const;
2055 typedef ArrayRef<uint64_t>::iterator element_iterator;
2056 element_iterator elements_begin() const { return getElements().begin(); }
2057 element_iterator elements_end() const { return getElements().end(); }
2059 /// \brief A lightweight wrapper around an expression operand.
2061 /// TODO: Store arguments directly and change \a DIExpression to store a
2067 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2069 const uint64_t *get() const { return Op; }
2071 /// \brief Get the operand code.
2072 uint64_t getOp() const { return *Op; }
2074 /// \brief Get an argument to the operand.
2076 /// Never returns the operand itself.
2077 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2079 unsigned getNumArgs() const { return getSize() - 1; }
2081 /// \brief Return the size of the operand.
2083 /// Return the number of elements in the operand (1 + args).
2084 unsigned getSize() const;
2087 /// \brief An iterator for expression operands.
2088 class expr_op_iterator
2089 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2093 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2095 element_iterator getBase() const { return Op.get(); }
2096 const ExprOperand &operator*() const { return Op; }
2097 const ExprOperand *operator->() const { return &Op; }
2099 expr_op_iterator &operator++() {
2103 expr_op_iterator operator++(int) {
2104 expr_op_iterator T(*this);
2109 /// \brief Get the next iterator.
2111 /// \a std::next() doesn't work because this is technically an
2112 /// input_iterator, but it's a perfectly valid operation. This is an
2113 /// accessor to provide the same functionality.
2114 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2116 bool operator==(const expr_op_iterator &X) const {
2117 return getBase() == X.getBase();
2119 bool operator!=(const expr_op_iterator &X) const {
2120 return getBase() != X.getBase();
2124 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2127 /// \brief Visit the elements via ExprOperand wrappers.
2129 /// These range iterators visit elements through \a ExprOperand wrappers.
2130 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2133 /// \pre \a isValid() gives \c true.
2135 expr_op_iterator expr_op_begin() const {
2136 return expr_op_iterator(elements_begin());
2138 expr_op_iterator expr_op_end() const {
2139 return expr_op_iterator(elements_end());
2143 bool isValid() const;
2145 static bool classof(const Metadata *MD) {
2146 return MD->getMetadataID() == DIExpressionKind;
2150 class DIObjCProperty : public DINode {
2151 friend class LLVMContextImpl;
2152 friend class MDNode;
2155 unsigned Attributes;
2157 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2158 unsigned Attributes, ArrayRef<Metadata *> Ops)
2159 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2161 Line(Line), Attributes(Attributes) {}
2162 ~DIObjCProperty() = default;
2164 static DIObjCProperty *
2165 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2166 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2167 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2168 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2169 getCanonicalMDString(Context, GetterName),
2170 getCanonicalMDString(Context, SetterName), Attributes, Type,
2171 Storage, ShouldCreate);
2173 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2174 Metadata *File, unsigned Line,
2175 MDString *GetterName, MDString *SetterName,
2176 unsigned Attributes, Metadata *Type,
2177 StorageType Storage, bool ShouldCreate = true);
2179 TempDIObjCProperty cloneImpl() const {
2180 return getTemporary(getContext(), getName(), getFile(), getLine(),
2181 getGetterName(), getSetterName(), getAttributes(),
2186 DEFINE_MDNODE_GET(DIObjCProperty,
2187 (StringRef Name, DIFile *File, unsigned Line,
2188 StringRef GetterName, StringRef SetterName,
2189 unsigned Attributes, DITypeRef Type),
2190 (Name, File, Line, GetterName, SetterName, Attributes,
2192 DEFINE_MDNODE_GET(DIObjCProperty,
2193 (MDString * Name, Metadata *File, unsigned Line,
2194 MDString *GetterName, MDString *SetterName,
2195 unsigned Attributes, Metadata *Type),
2196 (Name, File, Line, GetterName, SetterName, Attributes,
2199 TempDIObjCProperty clone() const { return cloneImpl(); }
2201 unsigned getLine() const { return Line; }
2202 unsigned getAttributes() const { return Attributes; }
2203 StringRef getName() const { return getStringOperand(0); }
2204 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2205 StringRef getGetterName() const { return getStringOperand(2); }
2206 StringRef getSetterName() const { return getStringOperand(3); }
2207 DITypeRef getType() const { return DITypeRef(getRawType()); }
2209 StringRef getFilename() const {
2210 if (auto *F = getFile())
2211 return F->getFilename();
2214 StringRef getDirectory() const {
2215 if (auto *F = getFile())
2216 return F->getDirectory();
2220 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2221 Metadata *getRawFile() const { return getOperand(1); }
2222 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2223 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2224 Metadata *getRawType() const { return getOperand(4); }
2226 static bool classof(const Metadata *MD) {
2227 return MD->getMetadataID() == DIObjCPropertyKind;
2231 /// \brief An imported module (C++ using directive or similar).
2232 class DIImportedEntity : public DINode {
2233 friend class LLVMContextImpl;
2234 friend class MDNode;
2238 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2239 unsigned Line, ArrayRef<Metadata *> Ops)
2240 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2241 ~DIImportedEntity() = default;
2243 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2244 DIScope *Scope, DINodeRef Entity,
2245 unsigned Line, StringRef Name,
2246 StorageType Storage,
2247 bool ShouldCreate = true) {
2248 return getImpl(Context, Tag, Scope, Entity, Line,
2249 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2251 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2252 Metadata *Scope, Metadata *Entity,
2253 unsigned Line, MDString *Name,
2254 StorageType Storage,
2255 bool ShouldCreate = true);
2257 TempDIImportedEntity cloneImpl() const {
2258 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2259 getLine(), getName());
2263 DEFINE_MDNODE_GET(DIImportedEntity,
2264 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2265 unsigned Line, StringRef Name = ""),
2266 (Tag, Scope, Entity, Line, Name))
2267 DEFINE_MDNODE_GET(DIImportedEntity,
2268 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2269 unsigned Line, MDString *Name),
2270 (Tag, Scope, Entity, Line, Name))
2272 TempDIImportedEntity clone() const { return cloneImpl(); }
2274 unsigned getLine() const { return Line; }
2275 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2276 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2277 StringRef getName() const { return getStringOperand(2); }
2279 Metadata *getRawScope() const { return getOperand(0); }
2280 Metadata *getRawEntity() const { return getOperand(1); }
2281 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2283 static bool classof(const Metadata *MD) {
2284 return MD->getMetadataID() == DIImportedEntityKind;
2288 } // end namespace llvm
2290 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2291 #undef DEFINE_MDNODE_GET_UNPACK
2292 #undef DEFINE_MDNODE_GET