1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Declarations for metadata specific to debug info.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
15 #define LLVM_IR_DEBUGINFOMETADATA_H
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Dwarf.h"
20 // Helper macros for defining get() overrides.
21 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
22 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
23 #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
24 static CLASS *getDistinct(LLVMContext &Context, \
25 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
26 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
28 static Temp##CLASS getTemporary(LLVMContext &Context, \
29 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
31 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
33 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
34 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
35 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
37 static CLASS *getIfExists(LLVMContext &Context, \
38 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
39 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
40 /* ShouldCreate */ false); \
42 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
46 /// \brief Pointer union between a subclass of DINode and MDString.
48 /// \a DICompositeType can be referenced via an \a MDString unique identifier.
49 /// This class allows some type safety in the face of that, requiring either a
50 /// node of a particular type or an \a MDString.
51 template <class T> class TypedDINodeRef {
52 const Metadata *MD = nullptr;
55 TypedDINodeRef() = default;
56 TypedDINodeRef(std::nullptr_t) {}
58 /// \brief Construct from a raw pointer.
59 explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
60 assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
65 const TypedDINodeRef<U> &X,
66 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
70 operator Metadata *() const { return const_cast<Metadata *>(MD); }
72 bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; }
73 bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; }
75 /// \brief Create a reference.
77 /// Get a reference to \c N, using an \a MDString reference if available.
78 static TypedDINodeRef get(const T *N);
80 template <class MapTy> T *resolve(const MapTy &Map) const {
84 if (auto *Typed = dyn_cast<T>(MD))
85 return const_cast<T *>(Typed);
87 auto *S = cast<MDString>(MD);
89 assert(I != Map.end() && "Missing identifier in type map");
90 return cast<T>(I->second);
94 typedef TypedDINodeRef<DINode> DINodeRef;
95 typedef TypedDINodeRef<DIScope> DIScopeRef;
96 typedef TypedDINodeRef<DIType> DITypeRef;
98 class DITypeRefArray {
99 const MDTuple *N = nullptr;
102 DITypeRefArray() = default;
103 DITypeRefArray(const MDTuple *N) : N(N) {}
105 explicit operator bool() const { return get(); }
106 explicit operator MDTuple *() const { return get(); }
108 MDTuple *get() const { return const_cast<MDTuple *>(N); }
109 MDTuple *operator->() const { return get(); }
110 MDTuple &operator*() const { return *get(); }
112 // FIXME: Fix callers and remove condition on N.
113 unsigned size() const { return N ? N->getNumOperands() : 0u; }
114 DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
116 class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
117 std::ptrdiff_t, void, DITypeRef> {
118 MDNode::op_iterator I = nullptr;
121 iterator() = default;
122 explicit iterator(MDNode::op_iterator I) : I(I) {}
123 DITypeRef operator*() const { return DITypeRef(*I); }
124 iterator &operator++() {
128 iterator operator++(int) {
129 iterator Temp(*this);
133 bool operator==(const iterator &X) const { return I == X.I; }
134 bool operator!=(const iterator &X) const { return I != X.I; }
137 // FIXME: Fix callers and remove condition on N.
138 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
139 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
142 /// \brief Tagged DWARF-like metadata node.
144 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
145 /// defined in llvm/Support/Dwarf.h). Called \a DINode because it's
146 /// potentially used for non-DWARF output.
147 class DINode : public MDNode {
148 friend class LLVMContextImpl;
152 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
153 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
154 : MDNode(C, ID, Storage, Ops1, Ops2) {
155 assert(Tag < 1u << 16);
156 SubclassData16 = Tag;
160 template <class Ty> Ty *getOperandAs(unsigned I) const {
161 return cast_or_null<Ty>(getOperand(I));
164 StringRef getStringOperand(unsigned I) const {
165 if (auto *S = getOperandAs<MDString>(I))
166 return S->getString();
170 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
173 return MDString::get(Context, S);
177 unsigned getTag() const { return SubclassData16; }
179 /// \brief Debug info flags.
181 /// The three accessibility flags are mutually exclusive and rolled together
182 /// in the first two bits.
184 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
185 #include "llvm/IR/DebugInfoFlags.def"
186 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
189 static unsigned getFlag(StringRef Flag);
190 static const char *getFlagString(unsigned Flag);
192 /// \brief Split up a flags bitfield.
194 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
195 /// any remaining (unrecognized) bits.
196 static unsigned splitFlags(unsigned Flags,
197 SmallVectorImpl<unsigned> &SplitFlags);
199 DINodeRef getRef() const { return DINodeRef::get(this); }
201 static bool classof(const Metadata *MD) {
202 switch (MD->getMetadataID()) {
205 case GenericDINodeKind:
207 case DIEnumeratorKind:
208 case DIBasicTypeKind:
209 case DIDerivedTypeKind:
210 case DICompositeTypeKind:
211 case DISubroutineTypeKind:
213 case DICompileUnitKind:
214 case DISubprogramKind:
215 case DILexicalBlockKind:
216 case DILexicalBlockFileKind:
217 case DINamespaceKind:
218 case DITemplateTypeParameterKind:
219 case DITemplateValueParameterKind:
220 case DIGlobalVariableKind:
221 case DILocalVariableKind:
222 case DIObjCPropertyKind:
223 case DIImportedEntityKind:
230 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
231 typedef Metadata *SimpleType;
232 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
238 struct simplify_type<TypedDINodeRef<T>>
239 : simplify_type<const TypedDINodeRef<T>> {};
241 /// \brief Generic tagged DWARF-like metadata node.
243 /// An un-specialized DWARF-like metadata node. The first operand is a
244 /// (possibly empty) null-separated \a MDString header that contains arbitrary
245 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
246 /// to other metadata.
247 class GenericDINode : public DINode {
248 friend class LLVMContextImpl;
251 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
252 unsigned Tag, ArrayRef<Metadata *> Ops1,
253 ArrayRef<Metadata *> Ops2)
254 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
257 ~GenericDINode() { dropAllReferences(); }
259 void setHash(unsigned Hash) { SubclassData32 = Hash; }
260 void recalculateHash();
262 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
263 StringRef Header, ArrayRef<Metadata *> DwarfOps,
264 StorageType Storage, bool ShouldCreate = true) {
265 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
266 DwarfOps, Storage, ShouldCreate);
269 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
270 MDString *Header, ArrayRef<Metadata *> DwarfOps,
271 StorageType Storage, bool ShouldCreate = true);
273 TempGenericDINode cloneImpl() const {
275 getContext(), getTag(), getHeader(),
276 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
280 unsigned getHash() const { return SubclassData32; }
282 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
283 ArrayRef<Metadata *> DwarfOps),
284 (Tag, Header, DwarfOps))
285 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
286 ArrayRef<Metadata *> DwarfOps),
287 (Tag, Header, DwarfOps))
289 /// \brief Return a (temporary) clone of this.
290 TempGenericDINode clone() const { return cloneImpl(); }
292 unsigned getTag() const { return SubclassData16; }
293 StringRef getHeader() const { return getStringOperand(0); }
295 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
296 op_iterator dwarf_op_end() const { return op_end(); }
297 op_range dwarf_operands() const {
298 return op_range(dwarf_op_begin(), dwarf_op_end());
301 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
302 const MDOperand &getDwarfOperand(unsigned I) const {
303 return getOperand(I + 1);
305 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
306 replaceOperandWith(I + 1, New);
309 static bool classof(const Metadata *MD) {
310 return MD->getMetadataID() == GenericDINodeKind;
314 /// \brief Array subrange.
316 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
318 class DISubrange : public DINode {
319 friend class LLVMContextImpl;
325 DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
327 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
328 Count(Count), LowerBound(LowerBound) {}
329 ~DISubrange() = default;
331 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
332 int64_t LowerBound, StorageType Storage,
333 bool ShouldCreate = true);
335 TempDISubrange cloneImpl() const {
336 return getTemporary(getContext(), getCount(), getLowerBound());
340 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
343 TempDISubrange clone() const { return cloneImpl(); }
345 int64_t getLowerBound() const { return LowerBound; }
346 int64_t getCount() const { return Count; }
348 static bool classof(const Metadata *MD) {
349 return MD->getMetadataID() == DISubrangeKind;
353 /// \brief Enumeration value.
355 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
356 /// longer creates a type cycle.
357 class DIEnumerator : public DINode {
358 friend class LLVMContextImpl;
363 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
364 ArrayRef<Metadata *> Ops)
365 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
367 ~DIEnumerator() = default;
369 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
370 StringRef Name, StorageType Storage,
371 bool ShouldCreate = true) {
372 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
375 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
376 MDString *Name, StorageType Storage,
377 bool ShouldCreate = true);
379 TempDIEnumerator cloneImpl() const {
380 return getTemporary(getContext(), getValue(), getName());
384 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
386 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
389 TempDIEnumerator clone() const { return cloneImpl(); }
391 int64_t getValue() const { return Value; }
392 StringRef getName() const { return getStringOperand(0); }
394 MDString *getRawName() const { return getOperandAs<MDString>(0); }
396 static bool classof(const Metadata *MD) {
397 return MD->getMetadataID() == DIEnumeratorKind;
401 /// \brief Base class for scope-like contexts.
403 /// Base class for lexical scopes and types (which are also declaration
406 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
407 class DIScope : public DINode {
409 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
410 ArrayRef<Metadata *> Ops)
411 : DINode(C, ID, Storage, Tag, Ops) {}
412 ~DIScope() = default;
415 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
417 inline StringRef getFilename() const;
418 inline StringRef getDirectory() const;
420 StringRef getName() const;
421 DIScopeRef getScope() const;
423 /// \brief Return the raw underlying file.
425 /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
426 /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
427 /// this. Otherwise, return the first operand, which is where all other
428 /// subclasses store their file pointer.
429 Metadata *getRawFile() const {
430 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
431 : static_cast<Metadata *>(getOperand(0));
434 DIScopeRef getRef() const { return DIScopeRef::get(this); }
436 static bool classof(const Metadata *MD) {
437 switch (MD->getMetadataID()) {
440 case DIBasicTypeKind:
441 case DIDerivedTypeKind:
442 case DICompositeTypeKind:
443 case DISubroutineTypeKind:
445 case DICompileUnitKind:
446 case DISubprogramKind:
447 case DILexicalBlockKind:
448 case DILexicalBlockFileKind:
449 case DINamespaceKind:
458 /// TODO: Merge with directory/file node (including users).
459 /// TODO: Canonicalize paths on creation.
460 class DIFile : public DIScope {
461 friend class LLVMContextImpl;
464 DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
465 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
468 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
469 StringRef Directory, StorageType Storage,
470 bool ShouldCreate = true) {
471 return getImpl(Context, getCanonicalMDString(Context, Filename),
472 getCanonicalMDString(Context, Directory), Storage,
475 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
476 MDString *Directory, StorageType Storage,
477 bool ShouldCreate = true);
479 TempDIFile cloneImpl() const {
480 return getTemporary(getContext(), getFilename(), getDirectory());
484 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
485 (Filename, Directory))
486 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
487 (Filename, Directory))
489 TempDIFile clone() const { return cloneImpl(); }
491 StringRef getFilename() const { return getStringOperand(0); }
492 StringRef getDirectory() const { return getStringOperand(1); }
494 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
495 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
497 static bool classof(const Metadata *MD) {
498 return MD->getMetadataID() == DIFileKind;
502 StringRef DIScope::getFilename() const {
503 if (auto *F = getFile())
504 return F->getFilename();
508 StringRef DIScope::getDirectory() const {
509 if (auto *F = getFile())
510 return F->getDirectory();
514 /// \brief Base class for types.
516 /// TODO: Remove the hardcoded name and context, since many types don't use
518 /// TODO: Split up flags.
519 class DIType : public DIScope {
523 uint64_t AlignInBits;
524 uint64_t OffsetInBits;
527 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
528 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
529 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
530 : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
531 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
532 OffsetInBits(OffsetInBits) {}
536 TempDIType clone() const {
537 return TempDIType(cast<DIType>(MDNode::clone().release()));
540 unsigned getLine() const { return Line; }
541 uint64_t getSizeInBits() const { return SizeInBits; }
542 uint64_t getAlignInBits() const { return AlignInBits; }
543 uint64_t getOffsetInBits() const { return OffsetInBits; }
544 unsigned getFlags() const { return Flags; }
546 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
547 StringRef getName() const { return getStringOperand(2); }
550 Metadata *getRawScope() const { return getOperand(1); }
551 MDString *getRawName() const { return getOperandAs<MDString>(2); }
553 void setFlags(unsigned NewFlags) {
554 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
558 bool isPrivate() const {
559 return (getFlags() & FlagAccessibility) == FlagPrivate;
561 bool isProtected() const {
562 return (getFlags() & FlagAccessibility) == FlagProtected;
564 bool isPublic() const {
565 return (getFlags() & FlagAccessibility) == FlagPublic;
567 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
568 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
569 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
570 bool isVirtual() const { return getFlags() & FlagVirtual; }
571 bool isArtificial() const { return getFlags() & FlagArtificial; }
572 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
573 bool isObjcClassComplete() const {
574 return getFlags() & FlagObjcClassComplete;
576 bool isVector() const { return getFlags() & FlagVector; }
577 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
578 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
579 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
580 bool isExternalTypeRef() const { return getFlags() & FlagExternalTypeRef; }
582 DITypeRef getRef() const { return DITypeRef::get(this); }
584 static bool classof(const Metadata *MD) {
585 switch (MD->getMetadataID()) {
588 case DIBasicTypeKind:
589 case DIDerivedTypeKind:
590 case DICompositeTypeKind:
591 case DISubroutineTypeKind:
597 /// \brief Basic type, like 'int' or 'float'.
599 /// TODO: Split out DW_TAG_unspecified_type.
600 /// TODO: Drop unused accessors.
601 class DIBasicType : public DIType {
602 friend class LLVMContextImpl;
607 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
608 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
609 ArrayRef<Metadata *> Ops)
610 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
612 Encoding(Encoding) {}
613 ~DIBasicType() = default;
615 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
616 StringRef Name, uint64_t SizeInBits,
617 uint64_t AlignInBits, unsigned Encoding,
618 StorageType Storage, bool ShouldCreate = true) {
619 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
620 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
622 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
623 MDString *Name, uint64_t SizeInBits,
624 uint64_t AlignInBits, unsigned Encoding,
625 StorageType Storage, bool ShouldCreate = true);
627 TempDIBasicType cloneImpl() const {
628 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
629 getAlignInBits(), getEncoding());
633 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
634 (Tag, Name, 0, 0, 0))
635 DEFINE_MDNODE_GET(DIBasicType,
636 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
637 uint64_t AlignInBits, unsigned Encoding),
638 (Tag, Name, SizeInBits, AlignInBits, Encoding))
639 DEFINE_MDNODE_GET(DIBasicType,
640 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
641 uint64_t AlignInBits, unsigned Encoding),
642 (Tag, Name, SizeInBits, AlignInBits, Encoding))
644 TempDIBasicType clone() const { return cloneImpl(); }
646 unsigned getEncoding() const { return Encoding; }
648 static bool classof(const Metadata *MD) {
649 return MD->getMetadataID() == DIBasicTypeKind;
653 /// \brief Derived types.
655 /// This includes qualified types, pointers, references, friends, typedefs, and
658 /// TODO: Split out members (inheritance, fields, methods, etc.).
659 class DIDerivedType : public DIType {
660 friend class LLVMContextImpl;
663 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
664 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
665 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
666 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
667 AlignInBits, OffsetInBits, Flags, Ops) {}
668 ~DIDerivedType() = default;
670 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
671 StringRef Name, DIFile *File, unsigned Line,
672 DIScopeRef Scope, DITypeRef BaseType,
673 uint64_t SizeInBits, uint64_t AlignInBits,
674 uint64_t OffsetInBits, unsigned Flags,
675 Metadata *ExtraData, StorageType Storage,
676 bool ShouldCreate = true) {
677 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
678 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
679 Flags, ExtraData, Storage, ShouldCreate);
681 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
682 MDString *Name, Metadata *File, unsigned Line,
683 Metadata *Scope, Metadata *BaseType,
684 uint64_t SizeInBits, uint64_t AlignInBits,
685 uint64_t OffsetInBits, unsigned Flags,
686 Metadata *ExtraData, StorageType Storage,
687 bool ShouldCreate = true);
689 TempDIDerivedType cloneImpl() const {
690 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
691 getScope(), getBaseType(), getSizeInBits(),
692 getAlignInBits(), getOffsetInBits(), getFlags(),
697 DEFINE_MDNODE_GET(DIDerivedType,
698 (unsigned Tag, MDString *Name, Metadata *File,
699 unsigned Line, Metadata *Scope, Metadata *BaseType,
700 uint64_t SizeInBits, uint64_t AlignInBits,
701 uint64_t OffsetInBits, unsigned Flags,
702 Metadata *ExtraData = nullptr),
703 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
704 AlignInBits, OffsetInBits, Flags, ExtraData))
705 DEFINE_MDNODE_GET(DIDerivedType,
706 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
707 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
708 uint64_t AlignInBits, uint64_t OffsetInBits,
709 unsigned Flags, Metadata *ExtraData = nullptr),
710 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
711 AlignInBits, OffsetInBits, Flags, ExtraData))
713 TempDIDerivedType clone() const { return cloneImpl(); }
715 //// Get the base type this is derived from.
716 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
717 Metadata *getRawBaseType() const { return getOperand(3); }
719 /// \brief Get extra data associated with this derived type.
721 /// Class type for pointer-to-members, objective-c property node for ivars,
722 /// or global constant wrapper for static members.
724 /// TODO: Separate out types that need this extra operand: pointer-to-member
725 /// types and member fields (static members and ivars).
726 Metadata *getExtraData() const { return getRawExtraData(); }
727 Metadata *getRawExtraData() const { return getOperand(4); }
729 /// \brief Get casted version of extra data.
731 DITypeRef getClassType() const {
732 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
733 return DITypeRef(getExtraData());
735 DIObjCProperty *getObjCProperty() const {
736 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
738 Constant *getConstant() const {
739 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
740 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
741 return C->getValue();
746 static bool classof(const Metadata *MD) {
747 return MD->getMetadataID() == DIDerivedTypeKind;
751 /// \brief Composite types.
753 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
754 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
755 class DICompositeType : public DIType {
756 friend class LLVMContextImpl;
759 unsigned RuntimeLang;
761 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
762 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
763 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
764 ArrayRef<Metadata *> Ops)
765 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
766 AlignInBits, OffsetInBits, Flags, Ops),
767 RuntimeLang(RuntimeLang) {}
768 ~DICompositeType() = default;
770 static DICompositeType *
771 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
772 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
773 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
774 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
775 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
776 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
778 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
779 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
780 RuntimeLang, VTableHolder, TemplateParams.get(),
781 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
783 static DICompositeType *
784 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
785 unsigned Line, Metadata *Scope, Metadata *BaseType,
786 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
787 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
788 Metadata *VTableHolder, Metadata *TemplateParams,
789 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
791 TempDICompositeType cloneImpl() const {
792 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
793 getScope(), getBaseType(), getSizeInBits(),
794 getAlignInBits(), getOffsetInBits(), getFlags(),
795 getElements(), getRuntimeLang(), getVTableHolder(),
796 getTemplateParams(), getIdentifier());
800 DEFINE_MDNODE_GET(DICompositeType,
801 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
802 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
803 uint64_t AlignInBits, uint64_t OffsetInBits,
804 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
805 DITypeRef VTableHolder,
806 DITemplateParameterArray TemplateParams = nullptr,
807 StringRef Identifier = ""),
808 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
809 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
810 VTableHolder, TemplateParams, Identifier))
811 DEFINE_MDNODE_GET(DICompositeType,
812 (unsigned Tag, MDString *Name, Metadata *File,
813 unsigned Line, Metadata *Scope, Metadata *BaseType,
814 uint64_t SizeInBits, uint64_t AlignInBits,
815 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
816 unsigned RuntimeLang, Metadata *VTableHolder,
817 Metadata *TemplateParams = nullptr,
818 MDString *Identifier = nullptr),
819 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
820 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
821 VTableHolder, TemplateParams, Identifier))
823 TempDICompositeType clone() const { return cloneImpl(); }
825 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
826 DINodeArray getElements() const {
827 return cast_or_null<MDTuple>(getRawElements());
829 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
830 DITemplateParameterArray getTemplateParams() const {
831 return cast_or_null<MDTuple>(getRawTemplateParams());
833 StringRef getIdentifier() const { return getStringOperand(7); }
834 unsigned getRuntimeLang() const { return RuntimeLang; }
836 Metadata *getRawBaseType() const { return getOperand(3); }
837 Metadata *getRawElements() const { return getOperand(4); }
838 Metadata *getRawVTableHolder() const { return getOperand(5); }
839 Metadata *getRawTemplateParams() const { return getOperand(6); }
840 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
842 /// \brief Replace operands.
844 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
845 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
846 /// of its movement if necessary.
848 void replaceElements(DINodeArray Elements) {
850 for (DINode *Op : getElements())
851 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
852 "Lost a member during member list replacement");
854 replaceOperandWith(4, Elements.get());
856 void replaceVTableHolder(DITypeRef VTableHolder) {
857 replaceOperandWith(5, VTableHolder);
859 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
860 replaceOperandWith(6, TemplateParams.get());
864 static bool classof(const Metadata *MD) {
865 return MD->getMetadataID() == DICompositeTypeKind;
869 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
871 if (auto *Composite = dyn_cast<DICompositeType>(N))
872 if (auto *S = Composite->getRawIdentifier())
873 return TypedDINodeRef<T>(S);
874 return TypedDINodeRef<T>(N);
877 /// \brief Type array for a subprogram.
879 /// TODO: Fold the array of types in directly as operands.
880 class DISubroutineType : public DIType {
881 friend class LLVMContextImpl;
884 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
885 ArrayRef<Metadata *> Ops)
886 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
887 0, 0, 0, 0, Flags, Ops) {}
888 ~DISubroutineType() = default;
890 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
891 DITypeRefArray TypeArray,
893 bool ShouldCreate = true) {
894 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
896 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
897 Metadata *TypeArray, StorageType Storage,
898 bool ShouldCreate = true);
900 TempDISubroutineType cloneImpl() const {
901 return getTemporary(getContext(), getFlags(), getTypeArray());
905 DEFINE_MDNODE_GET(DISubroutineType,
906 (unsigned Flags, DITypeRefArray TypeArray),
908 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
911 TempDISubroutineType clone() const { return cloneImpl(); }
913 DITypeRefArray getTypeArray() const {
914 return cast_or_null<MDTuple>(getRawTypeArray());
916 Metadata *getRawTypeArray() const { return getOperand(3); }
918 static bool classof(const Metadata *MD) {
919 return MD->getMetadataID() == DISubroutineTypeKind;
923 /// \brief Compile unit.
924 class DICompileUnit : public DIScope {
925 friend class LLVMContextImpl;
928 unsigned SourceLanguage;
930 unsigned RuntimeVersion;
931 unsigned EmissionKind;
934 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
935 bool IsOptimized, unsigned RuntimeVersion,
936 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
937 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
938 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
939 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
941 assert(Storage != Uniqued);
943 ~DICompileUnit() = default;
945 static DICompileUnit *
946 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
947 StringRef Producer, bool IsOptimized, StringRef Flags,
948 unsigned RuntimeVersion, StringRef SplitDebugFilename,
949 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
950 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
951 DIGlobalVariableArray GlobalVariables,
952 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
953 StorageType Storage, bool ShouldCreate = true) {
954 return getImpl(Context, SourceLanguage, File,
955 getCanonicalMDString(Context, Producer), IsOptimized,
956 getCanonicalMDString(Context, Flags), RuntimeVersion,
957 getCanonicalMDString(Context, SplitDebugFilename),
958 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
959 Subprograms.get(), GlobalVariables.get(),
960 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
962 static DICompileUnit *
963 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
964 MDString *Producer, bool IsOptimized, MDString *Flags,
965 unsigned RuntimeVersion, MDString *SplitDebugFilename,
966 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
967 Metadata *Subprograms, Metadata *GlobalVariables,
968 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
969 bool ShouldCreate = true);
971 TempDICompileUnit cloneImpl() const {
973 getContext(), getSourceLanguage(), getFile(), getProducer(),
974 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
975 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
976 getGlobalVariables(), getImportedEntities(), DWOId);
979 static void get() = delete;
980 static void getIfExists() = delete;
983 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
985 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
986 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
987 StringRef SplitDebugFilename, unsigned EmissionKind,
988 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
989 DISubprogramArray Subprograms, DIGlobalVariableArray GlobalVariables,
990 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
991 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
992 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
993 GlobalVariables, ImportedEntities, DWOId))
994 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
996 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
997 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
998 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
999 Metadata *RetainedTypes, Metadata *Subprograms,
1000 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1001 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1002 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1003 GlobalVariables, ImportedEntities, DWOId))
1005 TempDICompileUnit clone() const { return cloneImpl(); }
1007 unsigned getSourceLanguage() const { return SourceLanguage; }
1008 bool isOptimized() const { return IsOptimized; }
1009 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1010 unsigned getEmissionKind() const { return EmissionKind; }
1011 StringRef getProducer() const { return getStringOperand(1); }
1012 StringRef getFlags() const { return getStringOperand(2); }
1013 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1014 DICompositeTypeArray getEnumTypes() const {
1015 return cast_or_null<MDTuple>(getRawEnumTypes());
1017 DITypeArray getRetainedTypes() const {
1018 return cast_or_null<MDTuple>(getRawRetainedTypes());
1020 DISubprogramArray getSubprograms() const {
1021 return cast_or_null<MDTuple>(getRawSubprograms());
1023 DIGlobalVariableArray getGlobalVariables() const {
1024 return cast_or_null<MDTuple>(getRawGlobalVariables());
1026 DIImportedEntityArray getImportedEntities() const {
1027 return cast_or_null<MDTuple>(getRawImportedEntities());
1029 unsigned getDWOId() const { return DWOId; }
1031 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1032 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1033 MDString *getRawSplitDebugFilename() const {
1034 return getOperandAs<MDString>(3);
1036 Metadata *getRawEnumTypes() const { return getOperand(4); }
1037 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1038 Metadata *getRawSubprograms() const { return getOperand(6); }
1039 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1040 Metadata *getRawImportedEntities() const { return getOperand(8); }
1042 /// \brief Replace arrays.
1044 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1045 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1046 /// DICompileUnit should be fairly rare.
1048 void replaceEnumTypes(DICompositeTypeArray N) {
1049 replaceOperandWith(4, N.get());
1051 void replaceRetainedTypes(DITypeArray N) {
1052 replaceOperandWith(5, N.get());
1054 void replaceSubprograms(DISubprogramArray N) {
1055 replaceOperandWith(6, N.get());
1057 void replaceGlobalVariables(DIGlobalVariableArray N) {
1058 replaceOperandWith(7, N.get());
1060 void replaceImportedEntities(DIImportedEntityArray N) {
1061 replaceOperandWith(8, N.get());
1065 static bool classof(const Metadata *MD) {
1066 return MD->getMetadataID() == DICompileUnitKind;
1070 /// \brief A scope for locals.
1072 /// A legal scope for lexical blocks, local variables, and debug info
1073 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1074 /// DILexicalBlockFile.
1075 class DILocalScope : public DIScope {
1077 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1078 ArrayRef<Metadata *> Ops)
1079 : DIScope(C, ID, Storage, Tag, Ops) {}
1080 ~DILocalScope() = default;
1083 /// \brief Get the subprogram for this scope.
1085 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1087 DISubprogram *getSubprogram() const;
1089 static bool classof(const Metadata *MD) {
1090 return MD->getMetadataID() == DISubprogramKind ||
1091 MD->getMetadataID() == DILexicalBlockKind ||
1092 MD->getMetadataID() == DILexicalBlockFileKind;
1096 /// \brief Debug location.
1098 /// A debug location in source code, used for debug info and otherwise.
1099 class DILocation : public MDNode {
1100 friend class LLVMContextImpl;
1101 friend class MDNode;
1103 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1104 unsigned Column, ArrayRef<Metadata *> MDs);
1105 ~DILocation() { dropAllReferences(); }
1107 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1108 unsigned Column, Metadata *Scope,
1109 Metadata *InlinedAt, StorageType Storage,
1110 bool ShouldCreate = true);
1111 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1112 unsigned Column, DILocalScope *Scope,
1113 DILocation *InlinedAt, StorageType Storage,
1114 bool ShouldCreate = true) {
1115 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1116 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1119 TempDILocation cloneImpl() const {
1120 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1124 // Disallow replacing operands.
1125 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1128 DEFINE_MDNODE_GET(DILocation,
1129 (unsigned Line, unsigned Column, Metadata *Scope,
1130 Metadata *InlinedAt = nullptr),
1131 (Line, Column, Scope, InlinedAt))
1132 DEFINE_MDNODE_GET(DILocation,
1133 (unsigned Line, unsigned Column, DILocalScope *Scope,
1134 DILocation *InlinedAt = nullptr),
1135 (Line, Column, Scope, InlinedAt))
1137 /// \brief Return a (temporary) clone of this.
1138 TempDILocation clone() const { return cloneImpl(); }
1140 unsigned getLine() const { return SubclassData32; }
1141 unsigned getColumn() const { return SubclassData16; }
1142 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1143 DILocation *getInlinedAt() const {
1144 return cast_or_null<DILocation>(getRawInlinedAt());
1147 DIFile *getFile() const { return getScope()->getFile(); }
1148 StringRef getFilename() const { return getScope()->getFilename(); }
1149 StringRef getDirectory() const { return getScope()->getDirectory(); }
1151 /// \brief Get the scope where this is inlined.
1153 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1155 DILocalScope *getInlinedAtScope() const {
1156 if (auto *IA = getInlinedAt())
1157 return IA->getInlinedAtScope();
1161 /// \brief Check whether this can be discriminated from another location.
1163 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1164 /// Scope and inlined-at chains are not recorded in the linetable, so they
1165 /// cannot be used to distinguish basic blocks.
1167 /// The current implementation is weaker than it should be, since it just
1168 /// checks filename and line.
1170 /// FIXME: Add a check for getDiscriminator().
1171 /// FIXME: Add a check for getColumn().
1172 /// FIXME: Change the getFilename() check to getFile() (or add one for
1173 /// getDirectory()).
1174 bool canDiscriminate(const DILocation &RHS) const {
1175 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1178 /// \brief Get the DWARF discriminator.
1180 /// DWARF discriminators distinguish identical file locations between
1181 /// instructions that are on different basic blocks.
1182 inline unsigned getDiscriminator() const;
1184 /// \brief Compute new discriminator in the given context.
1186 /// This modifies the \a LLVMContext that \c this is in to increment the next
1187 /// discriminator for \c this's line/filename combination.
1189 /// FIXME: Delete this. See comments in implementation and at the only call
1190 /// site in \a AddDiscriminators::runOnFunction().
1191 unsigned computeNewDiscriminator() const;
1193 Metadata *getRawScope() const { return getOperand(0); }
1194 Metadata *getRawInlinedAt() const {
1195 if (getNumOperands() == 2)
1196 return getOperand(1);
1200 static bool classof(const Metadata *MD) {
1201 return MD->getMetadataID() == DILocationKind;
1205 /// \brief Subprogram description.
1207 /// TODO: Remove DisplayName. It's always equal to Name.
1208 /// TODO: Split up flags.
1209 class DISubprogram : public DILocalScope {
1210 friend class LLVMContextImpl;
1211 friend class MDNode;
1215 unsigned Virtuality;
1216 unsigned VirtualIndex;
1222 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1223 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1224 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1225 bool IsOptimized, ArrayRef<Metadata *> Ops)
1226 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1228 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1229 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1230 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1231 ~DISubprogram() = default;
1233 static DISubprogram *
1234 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1235 StringRef LinkageName, DIFile *File, unsigned Line,
1236 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1237 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1238 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1239 Constant *Function, DITemplateParameterArray TemplateParams,
1240 DISubprogram *Declaration, DILocalVariableArray Variables,
1241 StorageType Storage, bool ShouldCreate = true) {
1242 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1243 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1244 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1245 Virtuality, VirtualIndex, Flags, IsOptimized,
1246 Function ? ConstantAsMetadata::get(Function) : nullptr,
1247 TemplateParams.get(), Declaration, Variables.get(), Storage,
1250 static DISubprogram *
1251 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1252 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1253 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1254 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1255 unsigned Flags, bool IsOptimized, Metadata *Function,
1256 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1257 StorageType Storage, bool ShouldCreate = true);
1259 TempDISubprogram cloneImpl() const {
1260 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1261 getFile(), getLine(), getType(), isLocalToUnit(),
1262 isDefinition(), getScopeLine(), getContainingType(),
1263 getVirtuality(), getVirtualIndex(), getFlags(),
1264 isOptimized(), getFunctionConstant(),
1265 getTemplateParams(), getDeclaration(), getVariables());
1269 DEFINE_MDNODE_GET(DISubprogram,
1270 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1271 DIFile *File, unsigned Line, DISubroutineType *Type,
1272 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1273 DITypeRef ContainingType, unsigned Virtuality,
1274 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1275 Constant *Function = nullptr,
1276 DITemplateParameterArray TemplateParams = nullptr,
1277 DISubprogram *Declaration = nullptr,
1278 DILocalVariableArray Variables = nullptr),
1279 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1280 IsDefinition, ScopeLine, ContainingType, Virtuality,
1281 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1282 Declaration, Variables))
1285 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1286 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1287 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1288 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1289 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1290 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1291 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1292 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1293 Function, TemplateParams, Declaration, Variables))
1295 TempDISubprogram clone() const { return cloneImpl(); }
1298 unsigned getLine() const { return Line; }
1299 unsigned getVirtuality() const { return Virtuality; }
1300 unsigned getVirtualIndex() const { return VirtualIndex; }
1301 unsigned getScopeLine() const { return ScopeLine; }
1302 unsigned getFlags() const { return Flags; }
1303 bool isLocalToUnit() const { return IsLocalToUnit; }
1304 bool isDefinition() const { return IsDefinition; }
1305 bool isOptimized() const { return IsOptimized; }
1307 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1308 bool isPrivate() const {
1309 return (getFlags() & FlagAccessibility) == FlagPrivate;
1311 bool isProtected() const {
1312 return (getFlags() & FlagAccessibility) == FlagProtected;
1314 bool isPublic() const {
1315 return (getFlags() & FlagAccessibility) == FlagPublic;
1317 bool isExplicit() const { return getFlags() & FlagExplicit; }
1318 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1320 /// \brief Check if this is reference-qualified.
1322 /// Return true if this subprogram is a C++11 reference-qualified non-static
1323 /// member function (void foo() &).
1324 unsigned isLValueReference() const {
1325 return getFlags() & FlagLValueReference;
1328 /// \brief Check if this is rvalue-reference-qualified.
1330 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1331 /// non-static member function (void foo() &&).
1332 unsigned isRValueReference() const {
1333 return getFlags() & FlagRValueReference;
1336 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1338 StringRef getName() const { return getStringOperand(2); }
1339 StringRef getDisplayName() const { return getStringOperand(3); }
1340 StringRef getLinkageName() const { return getStringOperand(4); }
1342 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1343 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1345 DISubroutineType *getType() const {
1346 return cast_or_null<DISubroutineType>(getRawType());
1348 DITypeRef getContainingType() const {
1349 return DITypeRef(getRawContainingType());
1352 Constant *getFunctionConstant() const {
1353 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1354 return C->getValue();
1357 DITemplateParameterArray getTemplateParams() const {
1358 return cast_or_null<MDTuple>(getRawTemplateParams());
1360 DISubprogram *getDeclaration() const {
1361 return cast_or_null<DISubprogram>(getRawDeclaration());
1363 DILocalVariableArray getVariables() const {
1364 return cast_or_null<MDTuple>(getRawVariables());
1367 Metadata *getRawScope() const { return getOperand(1); }
1368 Metadata *getRawType() const { return getOperand(5); }
1369 Metadata *getRawContainingType() const { return getOperand(6); }
1370 Metadata *getRawFunction() const { return getOperand(7); }
1371 Metadata *getRawTemplateParams() const { return getOperand(8); }
1372 Metadata *getRawDeclaration() const { return getOperand(9); }
1373 Metadata *getRawVariables() const { return getOperand(10); }
1375 /// \brief Get a pointer to the function this subprogram describes.
1377 /// This dyn_casts \a getFunctionConstant() to \a Function.
1379 /// FIXME: Should this be looking through bitcasts?
1380 Function *getFunction() const;
1382 /// \brief Replace the function.
1384 /// If \a isUniqued() and not \a isResolved(), this could node will be
1385 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1386 /// that's a problem.
1388 void replaceFunction(Function *F);
1389 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1390 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1393 /// \brief Check if this subprogram describes the given function.
1395 /// FIXME: Should this be looking through bitcasts?
1396 bool describes(const Function *F) const;
1398 static bool classof(const Metadata *MD) {
1399 return MD->getMetadataID() == DISubprogramKind;
1403 class DILexicalBlockBase : public DILocalScope {
1405 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1406 ArrayRef<Metadata *> Ops)
1407 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1408 ~DILexicalBlockBase() = default;
1411 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1413 Metadata *getRawScope() const { return getOperand(1); }
1415 /// \brief Forwarding accessors to LexicalBlock.
1417 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1419 inline unsigned getLine() const;
1420 inline unsigned getColumn() const;
1422 static bool classof(const Metadata *MD) {
1423 return MD->getMetadataID() == DILexicalBlockKind ||
1424 MD->getMetadataID() == DILexicalBlockFileKind;
1428 class DILexicalBlock : public DILexicalBlockBase {
1429 friend class LLVMContextImpl;
1430 friend class MDNode;
1435 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1436 unsigned Column, ArrayRef<Metadata *> Ops)
1437 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1439 ~DILexicalBlock() = default;
1441 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1442 DIFile *File, unsigned Line, unsigned Column,
1443 StorageType Storage,
1444 bool ShouldCreate = true) {
1445 return getImpl(Context, static_cast<Metadata *>(Scope),
1446 static_cast<Metadata *>(File), Line, Column, Storage,
1450 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1451 Metadata *File, unsigned Line, unsigned Column,
1452 StorageType Storage, bool ShouldCreate = true);
1454 TempDILexicalBlock cloneImpl() const {
1455 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1460 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1461 unsigned Line, unsigned Column),
1462 (Scope, File, Line, Column))
1463 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1464 unsigned Line, unsigned Column),
1465 (Scope, File, Line, Column))
1467 TempDILexicalBlock clone() const { return cloneImpl(); }
1469 unsigned getLine() const { return Line; }
1470 unsigned getColumn() const { return Column; }
1472 static bool classof(const Metadata *MD) {
1473 return MD->getMetadataID() == DILexicalBlockKind;
1477 unsigned DILexicalBlockBase::getLine() const {
1478 if (auto *N = dyn_cast<DILexicalBlock>(this))
1479 return N->getLine();
1483 unsigned DILexicalBlockBase::getColumn() const {
1484 if (auto *N = dyn_cast<DILexicalBlock>(this))
1485 return N->getColumn();
1489 class DILexicalBlockFile : public DILexicalBlockBase {
1490 friend class LLVMContextImpl;
1491 friend class MDNode;
1493 unsigned Discriminator;
1495 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1496 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1497 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1498 Discriminator(Discriminator) {}
1499 ~DILexicalBlockFile() = default;
1501 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1502 DIFile *File, unsigned Discriminator,
1503 StorageType Storage,
1504 bool ShouldCreate = true) {
1505 return getImpl(Context, static_cast<Metadata *>(Scope),
1506 static_cast<Metadata *>(File), Discriminator, Storage,
1510 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1511 Metadata *File, unsigned Discriminator,
1512 StorageType Storage,
1513 bool ShouldCreate = true);
1515 TempDILexicalBlockFile cloneImpl() const {
1516 return getTemporary(getContext(), getScope(), getFile(),
1517 getDiscriminator());
1521 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1522 unsigned Discriminator),
1523 (Scope, File, Discriminator))
1524 DEFINE_MDNODE_GET(DILexicalBlockFile,
1525 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1526 (Scope, File, Discriminator))
1528 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1530 // TODO: Remove these once they're gone from DILexicalBlockBase.
1531 unsigned getLine() const = delete;
1532 unsigned getColumn() const = delete;
1534 unsigned getDiscriminator() const { return Discriminator; }
1536 static bool classof(const Metadata *MD) {
1537 return MD->getMetadataID() == DILexicalBlockFileKind;
1541 unsigned DILocation::getDiscriminator() const {
1542 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1543 return F->getDiscriminator();
1547 class DINamespace : public DIScope {
1548 friend class LLVMContextImpl;
1549 friend class MDNode;
1553 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1554 ArrayRef<Metadata *> Ops)
1555 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1558 ~DINamespace() = default;
1560 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1561 DIFile *File, StringRef Name, unsigned Line,
1562 StorageType Storage, bool ShouldCreate = true) {
1563 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1564 Line, Storage, ShouldCreate);
1566 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1567 Metadata *File, MDString *Name, unsigned Line,
1568 StorageType Storage, bool ShouldCreate = true);
1570 TempDINamespace cloneImpl() const {
1571 return getTemporary(getContext(), getScope(), getFile(), getName(),
1576 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1578 (Scope, File, Name, Line))
1579 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1580 MDString *Name, unsigned Line),
1581 (Scope, File, Name, Line))
1583 TempDINamespace clone() const { return cloneImpl(); }
1585 unsigned getLine() const { return Line; }
1586 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1587 StringRef getName() const { return getStringOperand(2); }
1589 Metadata *getRawScope() const { return getOperand(1); }
1590 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1592 static bool classof(const Metadata *MD) {
1593 return MD->getMetadataID() == DINamespaceKind;
1597 /// \brief A (clang) module that has been imported by the compile unit.
1599 class DIModule : public DIScope {
1600 friend class LLVMContextImpl;
1601 friend class MDNode;
1603 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1604 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1607 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1608 StringRef Name, StringRef ConfigurationMacros,
1609 StringRef IncludePath, StringRef ISysRoot,
1610 StorageType Storage, bool ShouldCreate = true) {
1611 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1612 getCanonicalMDString(Context, ConfigurationMacros),
1613 getCanonicalMDString(Context, IncludePath),
1614 getCanonicalMDString(Context, ISysRoot),
1615 Storage, ShouldCreate);
1617 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1618 MDString *Name, MDString *ConfigurationMacros,
1619 MDString *IncludePath, MDString *ISysRoot,
1620 StorageType Storage, bool ShouldCreate = true);
1622 TempDIModule cloneImpl() const {
1623 return getTemporary(getContext(), getScope(), getName(),
1624 getConfigurationMacros(), getIncludePath(),
1629 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1630 StringRef ConfigurationMacros, StringRef IncludePath,
1631 StringRef ISysRoot),
1632 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1633 DEFINE_MDNODE_GET(DIModule,
1634 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1635 MDString *IncludePath, MDString *ISysRoot),
1636 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1638 TempDIModule clone() const { return cloneImpl(); }
1640 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1641 StringRef getName() const { return getStringOperand(1); }
1642 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1643 StringRef getIncludePath() const { return getStringOperand(3); }
1644 StringRef getISysRoot() const { return getStringOperand(4); }
1646 Metadata *getRawScope() const { return getOperand(0); }
1647 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1648 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1649 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1650 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1652 static bool classof(const Metadata *MD) {
1653 return MD->getMetadataID() == DIModuleKind;
1657 /// \brief Base class for template parameters.
1658 class DITemplateParameter : public DINode {
1660 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1661 unsigned Tag, ArrayRef<Metadata *> Ops)
1662 : DINode(Context, ID, Storage, Tag, Ops) {}
1663 ~DITemplateParameter() = default;
1666 StringRef getName() const { return getStringOperand(0); }
1667 DITypeRef getType() const { return DITypeRef(getRawType()); }
1669 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1670 Metadata *getRawType() const { return getOperand(1); }
1672 static bool classof(const Metadata *MD) {
1673 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1674 MD->getMetadataID() == DITemplateValueParameterKind;
1678 class DITemplateTypeParameter : public DITemplateParameter {
1679 friend class LLVMContextImpl;
1680 friend class MDNode;
1682 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1683 ArrayRef<Metadata *> Ops)
1684 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1685 dwarf::DW_TAG_template_type_parameter, Ops) {}
1686 ~DITemplateTypeParameter() = default;
1688 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1689 DITypeRef Type, StorageType Storage,
1690 bool ShouldCreate = true) {
1691 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1694 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1695 Metadata *Type, StorageType Storage,
1696 bool ShouldCreate = true);
1698 TempDITemplateTypeParameter cloneImpl() const {
1699 return getTemporary(getContext(), getName(), getType());
1703 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1705 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1708 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1710 static bool classof(const Metadata *MD) {
1711 return MD->getMetadataID() == DITemplateTypeParameterKind;
1715 class DITemplateValueParameter : public DITemplateParameter {
1716 friend class LLVMContextImpl;
1717 friend class MDNode;
1719 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1720 unsigned Tag, ArrayRef<Metadata *> Ops)
1721 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1723 ~DITemplateValueParameter() = default;
1725 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1726 StringRef Name, DITypeRef Type,
1727 Metadata *Value, StorageType Storage,
1728 bool ShouldCreate = true) {
1729 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1730 Value, Storage, ShouldCreate);
1732 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1733 MDString *Name, Metadata *Type,
1734 Metadata *Value, StorageType Storage,
1735 bool ShouldCreate = true);
1737 TempDITemplateValueParameter cloneImpl() const {
1738 return getTemporary(getContext(), getTag(), getName(), getType(),
1743 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1744 DITypeRef Type, Metadata *Value),
1745 (Tag, Name, Type, Value))
1746 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1747 Metadata *Type, Metadata *Value),
1748 (Tag, Name, Type, Value))
1750 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1752 Metadata *getValue() const { return getOperand(2); }
1754 static bool classof(const Metadata *MD) {
1755 return MD->getMetadataID() == DITemplateValueParameterKind;
1759 /// \brief Base class for variables.
1760 class DIVariable : public DINode {
1764 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
1765 ArrayRef<Metadata *> Ops)
1766 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line) {}
1767 ~DIVariable() = default;
1770 unsigned getLine() const { return Line; }
1771 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1772 StringRef getName() const { return getStringOperand(1); }
1773 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1774 DITypeRef getType() const { return DITypeRef(getRawType()); }
1776 StringRef getFilename() const {
1777 if (auto *F = getFile())
1778 return F->getFilename();
1781 StringRef getDirectory() const {
1782 if (auto *F = getFile())
1783 return F->getDirectory();
1787 Metadata *getRawScope() const { return getOperand(0); }
1788 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1789 Metadata *getRawFile() const { return getOperand(2); }
1790 Metadata *getRawType() const { return getOperand(3); }
1792 static bool classof(const Metadata *MD) {
1793 return MD->getMetadataID() == DILocalVariableKind ||
1794 MD->getMetadataID() == DIGlobalVariableKind;
1798 /// \brief Global variables.
1800 /// TODO: Remove DisplayName. It's always equal to Name.
1801 class DIGlobalVariable : public DIVariable {
1802 friend class LLVMContextImpl;
1803 friend class MDNode;
1808 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1809 bool IsLocalToUnit, bool IsDefinition,
1810 ArrayRef<Metadata *> Ops)
1811 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops),
1812 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1813 ~DIGlobalVariable() = default;
1815 static DIGlobalVariable *
1816 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1817 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1818 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1819 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1820 bool ShouldCreate = true) {
1821 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1822 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1823 IsLocalToUnit, IsDefinition,
1824 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1825 StaticDataMemberDeclaration, Storage, ShouldCreate);
1827 static DIGlobalVariable *
1828 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1829 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1830 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1831 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1832 bool ShouldCreate = true);
1834 TempDIGlobalVariable cloneImpl() const {
1835 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1836 getFile(), getLine(), getType(), isLocalToUnit(),
1837 isDefinition(), getVariable(),
1838 getStaticDataMemberDeclaration());
1842 DEFINE_MDNODE_GET(DIGlobalVariable,
1843 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1844 DIFile *File, unsigned Line, DITypeRef Type,
1845 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1846 DIDerivedType *StaticDataMemberDeclaration),
1847 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1848 IsDefinition, Variable, StaticDataMemberDeclaration))
1849 DEFINE_MDNODE_GET(DIGlobalVariable,
1850 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1851 Metadata *File, unsigned Line, Metadata *Type,
1852 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1853 Metadata *StaticDataMemberDeclaration),
1854 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1855 IsDefinition, Variable, StaticDataMemberDeclaration))
1857 TempDIGlobalVariable clone() const { return cloneImpl(); }
1859 bool isLocalToUnit() const { return IsLocalToUnit; }
1860 bool isDefinition() const { return IsDefinition; }
1861 StringRef getDisplayName() const { return getStringOperand(4); }
1862 StringRef getLinkageName() const { return getStringOperand(5); }
1863 Constant *getVariable() const {
1864 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1865 return dyn_cast<Constant>(C->getValue());
1868 DIDerivedType *getStaticDataMemberDeclaration() const {
1869 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1872 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1873 Metadata *getRawVariable() const { return getOperand(6); }
1874 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1876 static bool classof(const Metadata *MD) {
1877 return MD->getMetadataID() == DIGlobalVariableKind;
1881 /// \brief Local variable.
1883 /// TODO: Split up flags.
1884 class DILocalVariable : public DIVariable {
1885 friend class LLVMContextImpl;
1886 friend class MDNode;
1891 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1892 unsigned Arg, unsigned Flags, ArrayRef<Metadata *> Ops)
1893 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops), Arg(Arg),
1895 ~DILocalVariable() = default;
1897 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
1898 StringRef Name, DIFile *File, unsigned Line,
1899 DITypeRef Type, unsigned Arg, unsigned Flags,
1900 StorageType Storage,
1901 bool ShouldCreate = true) {
1902 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
1903 Line, Type, Arg, Flags, Storage, ShouldCreate);
1905 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
1906 MDString *Name, Metadata *File, unsigned Line,
1907 Metadata *Type, unsigned Arg, unsigned Flags,
1908 StorageType Storage,
1909 bool ShouldCreate = true);
1911 TempDILocalVariable cloneImpl() const {
1912 return getTemporary(getContext(), getScope(), getName(), getFile(),
1913 getLine(), getType(), getArg(), getFlags());
1917 DEFINE_MDNODE_GET(DILocalVariable,
1918 (DILocalScope * Scope, StringRef Name, DIFile *File,
1919 unsigned Line, DITypeRef Type, unsigned Arg,
1921 (Scope, Name, File, Line, Type, Arg, Flags))
1922 DEFINE_MDNODE_GET(DILocalVariable,
1923 (Metadata * Scope, MDString *Name, Metadata *File,
1924 unsigned Line, Metadata *Type, unsigned Arg,
1926 (Scope, Name, File, Line, Type, Arg, Flags))
1928 TempDILocalVariable clone() const { return cloneImpl(); }
1930 /// \brief Get the local scope for this variable.
1932 /// Variables must be defined in a local scope.
1933 DILocalScope *getScope() const {
1934 return cast<DILocalScope>(DIVariable::getScope());
1937 bool isParameter() const { return Arg; }
1938 unsigned getArg() const { return Arg; }
1939 unsigned getFlags() const { return Flags; }
1941 bool isArtificial() const { return getFlags() & FlagArtificial; }
1942 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1944 /// \brief Check that a location is valid for this variable.
1946 /// Check that \c DL exists, is in the same subprogram, and has the same
1947 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
1948 /// to a \a DbgInfoIntrinsic.)
1949 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1950 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1953 static bool classof(const Metadata *MD) {
1954 return MD->getMetadataID() == DILocalVariableKind;
1958 /// \brief DWARF expression.
1960 /// This is (almost) a DWARF expression that modifies the location of a
1961 /// variable or (or the location of a single piece of a variable).
1963 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1964 /// and have DW_OP_plus consume the topmost elements on the stack.
1966 /// TODO: Co-allocate the expression elements.
1967 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1969 class DIExpression : public MDNode {
1970 friend class LLVMContextImpl;
1971 friend class MDNode;
1973 std::vector<uint64_t> Elements;
1975 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1976 : MDNode(C, DIExpressionKind, Storage, None),
1977 Elements(Elements.begin(), Elements.end()) {}
1978 ~DIExpression() = default;
1980 static DIExpression *getImpl(LLVMContext &Context,
1981 ArrayRef<uint64_t> Elements, StorageType Storage,
1982 bool ShouldCreate = true);
1984 TempDIExpression cloneImpl() const {
1985 return getTemporary(getContext(), getElements());
1989 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1991 TempDIExpression clone() const { return cloneImpl(); }
1993 ArrayRef<uint64_t> getElements() const { return Elements; }
1995 unsigned getNumElements() const { return Elements.size(); }
1996 uint64_t getElement(unsigned I) const {
1997 assert(I < Elements.size() && "Index out of range");
2001 /// \brief Return whether this is a piece of an aggregate variable.
2002 bool isBitPiece() const;
2004 /// \brief Return the offset of this piece in bits.
2005 uint64_t getBitPieceOffset() const;
2007 /// \brief Return the size of this piece in bits.
2008 uint64_t getBitPieceSize() const;
2010 typedef ArrayRef<uint64_t>::iterator element_iterator;
2011 element_iterator elements_begin() const { return getElements().begin(); }
2012 element_iterator elements_end() const { return getElements().end(); }
2014 /// \brief A lightweight wrapper around an expression operand.
2016 /// TODO: Store arguments directly and change \a DIExpression to store a
2022 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2024 const uint64_t *get() const { return Op; }
2026 /// \brief Get the operand code.
2027 uint64_t getOp() const { return *Op; }
2029 /// \brief Get an argument to the operand.
2031 /// Never returns the operand itself.
2032 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2034 unsigned getNumArgs() const { return getSize() - 1; }
2036 /// \brief Return the size of the operand.
2038 /// Return the number of elements in the operand (1 + args).
2039 unsigned getSize() const;
2042 /// \brief An iterator for expression operands.
2043 class expr_op_iterator
2044 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2048 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2050 element_iterator getBase() const { return Op.get(); }
2051 const ExprOperand &operator*() const { return Op; }
2052 const ExprOperand *operator->() const { return &Op; }
2054 expr_op_iterator &operator++() {
2058 expr_op_iterator operator++(int) {
2059 expr_op_iterator T(*this);
2064 /// \brief Get the next iterator.
2066 /// \a std::next() doesn't work because this is technically an
2067 /// input_iterator, but it's a perfectly valid operation. This is an
2068 /// accessor to provide the same functionality.
2069 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2071 bool operator==(const expr_op_iterator &X) const {
2072 return getBase() == X.getBase();
2074 bool operator!=(const expr_op_iterator &X) const {
2075 return getBase() != X.getBase();
2079 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2082 /// \brief Visit the elements via ExprOperand wrappers.
2084 /// These range iterators visit elements through \a ExprOperand wrappers.
2085 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2088 /// \pre \a isValid() gives \c true.
2090 expr_op_iterator expr_op_begin() const {
2091 return expr_op_iterator(elements_begin());
2093 expr_op_iterator expr_op_end() const {
2094 return expr_op_iterator(elements_end());
2098 bool isValid() const;
2100 static bool classof(const Metadata *MD) {
2101 return MD->getMetadataID() == DIExpressionKind;
2105 class DIObjCProperty : public DINode {
2106 friend class LLVMContextImpl;
2107 friend class MDNode;
2110 unsigned Attributes;
2112 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2113 unsigned Attributes, ArrayRef<Metadata *> Ops)
2114 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2116 Line(Line), Attributes(Attributes) {}
2117 ~DIObjCProperty() = default;
2119 static DIObjCProperty *
2120 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2121 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2122 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2123 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2124 getCanonicalMDString(Context, GetterName),
2125 getCanonicalMDString(Context, SetterName), Attributes, Type,
2126 Storage, ShouldCreate);
2128 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2129 Metadata *File, unsigned Line,
2130 MDString *GetterName, MDString *SetterName,
2131 unsigned Attributes, Metadata *Type,
2132 StorageType Storage, bool ShouldCreate = true);
2134 TempDIObjCProperty cloneImpl() const {
2135 return getTemporary(getContext(), getName(), getFile(), getLine(),
2136 getGetterName(), getSetterName(), getAttributes(),
2141 DEFINE_MDNODE_GET(DIObjCProperty,
2142 (StringRef Name, DIFile *File, unsigned Line,
2143 StringRef GetterName, StringRef SetterName,
2144 unsigned Attributes, DITypeRef Type),
2145 (Name, File, Line, GetterName, SetterName, Attributes,
2147 DEFINE_MDNODE_GET(DIObjCProperty,
2148 (MDString * Name, Metadata *File, unsigned Line,
2149 MDString *GetterName, MDString *SetterName,
2150 unsigned Attributes, Metadata *Type),
2151 (Name, File, Line, GetterName, SetterName, Attributes,
2154 TempDIObjCProperty clone() const { return cloneImpl(); }
2156 unsigned getLine() const { return Line; }
2157 unsigned getAttributes() const { return Attributes; }
2158 StringRef getName() const { return getStringOperand(0); }
2159 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2160 StringRef getGetterName() const { return getStringOperand(2); }
2161 StringRef getSetterName() const { return getStringOperand(3); }
2162 DITypeRef getType() const { return DITypeRef(getRawType()); }
2164 StringRef getFilename() const {
2165 if (auto *F = getFile())
2166 return F->getFilename();
2169 StringRef getDirectory() const {
2170 if (auto *F = getFile())
2171 return F->getDirectory();
2175 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2176 Metadata *getRawFile() const { return getOperand(1); }
2177 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2178 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2179 Metadata *getRawType() const { return getOperand(4); }
2181 static bool classof(const Metadata *MD) {
2182 return MD->getMetadataID() == DIObjCPropertyKind;
2186 /// \brief An imported module (C++ using directive or similar).
2187 class DIImportedEntity : public DINode {
2188 friend class LLVMContextImpl;
2189 friend class MDNode;
2193 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2194 unsigned Line, ArrayRef<Metadata *> Ops)
2195 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2196 ~DIImportedEntity() = default;
2198 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2199 DIScope *Scope, DINodeRef Entity,
2200 unsigned Line, StringRef Name,
2201 StorageType Storage,
2202 bool ShouldCreate = true) {
2203 return getImpl(Context, Tag, Scope, Entity, Line,
2204 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2206 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2207 Metadata *Scope, Metadata *Entity,
2208 unsigned Line, MDString *Name,
2209 StorageType Storage,
2210 bool ShouldCreate = true);
2212 TempDIImportedEntity cloneImpl() const {
2213 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2214 getLine(), getName());
2218 DEFINE_MDNODE_GET(DIImportedEntity,
2219 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2220 unsigned Line, StringRef Name = ""),
2221 (Tag, Scope, Entity, Line, Name))
2222 DEFINE_MDNODE_GET(DIImportedEntity,
2223 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2224 unsigned Line, MDString *Name),
2225 (Tag, Scope, Entity, Line, Name))
2227 TempDIImportedEntity clone() const { return cloneImpl(); }
2229 unsigned getLine() const { return Line; }
2230 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2231 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2232 StringRef getName() const { return getStringOperand(2); }
2234 Metadata *getRawScope() const { return getOperand(0); }
2235 Metadata *getRawEntity() const { return getOperand(1); }
2236 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2238 static bool classof(const Metadata *MD) {
2239 return MD->getMetadataID() == DIImportedEntityKind;
2243 } // end namespace llvm
2245 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2246 #undef DEFINE_MDNODE_GET_UNPACK
2247 #undef DEFINE_MDNODE_GET