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 DebugNode and MDString.
46 /// \a MDCompositeType 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 TypedDebugNodeRef {
50 const Metadata *MD = nullptr;
53 TypedDebugNodeRef() = default;
54 TypedDebugNodeRef(std::nullptr_t) {}
56 /// \brief Construct from a raw pointer.
57 explicit TypedDebugNodeRef(const Metadata *MD) : MD(MD) {
58 assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
63 const TypedDebugNodeRef<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 TypedDebugNodeRef<T> &X) const { return MD == X.MD; };
71 bool operator!=(const TypedDebugNodeRef<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 TypedDebugNodeRef 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 TypedDebugNodeRef<DebugNode> DebugNodeRef;
93 typedef TypedDebugNodeRef<MDScope> MDScopeRef;
94 typedef TypedDebugNodeRef<MDType> MDTypeRef;
96 class MDTypeRefArray {
97 const MDTuple *N = nullptr;
100 MDTypeRefArray(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 MDTypeRef operator[](unsigned I) const { return MDTypeRef(N->getOperand(I)); }
113 class iterator : std::iterator<std::input_iterator_tag, MDTypeRef,
114 std::ptrdiff_t, void, MDTypeRef> {
115 MDNode::op_iterator I = nullptr;
118 iterator() = default;
119 explicit iterator(MDNode::op_iterator I) : I(I) {}
120 MDTypeRef operator*() const { return MDTypeRef(*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 DebugNode because it's
143 /// potentially used for non-DWARF output.
144 class DebugNode : public MDNode {
145 friend class LLVMContextImpl;
149 DebugNode(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;
155 ~DebugNode() = default;
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 DebugNodeRef getRef() const { return DebugNodeRef::get(this); }
198 static bool classof(const Metadata *MD) {
199 switch (MD->getMetadataID()) {
202 case GenericDebugNodeKind:
204 case MDEnumeratorKind:
205 case MDBasicTypeKind:
206 case MDDerivedTypeKind:
207 case MDCompositeTypeKind:
208 case MDSubroutineTypeKind:
210 case MDCompileUnitKind:
211 case MDSubprogramKind:
212 case MDLexicalBlockKind:
213 case MDLexicalBlockFileKind:
214 case MDNamespaceKind:
215 case MDTemplateTypeParameterKind:
216 case MDTemplateValueParameterKind:
217 case MDGlobalVariableKind:
218 case MDLocalVariableKind:
219 case MDObjCPropertyKind:
220 case MDImportedEntityKind:
227 struct simplify_type<const TypedDebugNodeRef<T>> {
228 typedef Metadata *SimpleType;
229 static SimpleType getSimplifiedValue(const TypedDebugNodeRef<T> &MD) {
235 struct simplify_type<TypedDebugNodeRef<T>>
236 : simplify_type<const TypedDebugNodeRef<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 GenericDebugNode : public DebugNode {
245 friend class LLVMContextImpl;
248 GenericDebugNode(LLVMContext &C, StorageType Storage, unsigned Hash,
249 unsigned Tag, ArrayRef<Metadata *> Ops1,
250 ArrayRef<Metadata *> Ops2)
251 : DebugNode(C, GenericDebugNodeKind, Storage, Tag, Ops1, Ops2) {
254 ~GenericDebugNode() { dropAllReferences(); }
256 void setHash(unsigned Hash) { SubclassData32 = Hash; }
257 void recalculateHash();
259 static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
261 ArrayRef<Metadata *> DwarfOps,
263 bool ShouldCreate = true) {
264 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
265 DwarfOps, Storage, ShouldCreate);
268 static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
270 ArrayRef<Metadata *> DwarfOps,
272 bool ShouldCreate = true);
274 TempGenericDebugNode cloneImpl() const {
276 getContext(), getTag(), getHeader(),
277 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
281 unsigned getHash() const { return SubclassData32; }
283 DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, StringRef Header,
284 ArrayRef<Metadata *> DwarfOps),
285 (Tag, Header, DwarfOps))
286 DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, MDString *Header,
287 ArrayRef<Metadata *> DwarfOps),
288 (Tag, Header, DwarfOps))
290 /// \brief Return a (temporary) clone of this.
291 TempGenericDebugNode clone() const { return cloneImpl(); }
293 unsigned getTag() const { return SubclassData16; }
294 StringRef getHeader() const { return getStringOperand(0); }
296 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
297 op_iterator dwarf_op_end() const { return op_end(); }
298 op_range dwarf_operands() const {
299 return op_range(dwarf_op_begin(), dwarf_op_end());
302 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
303 const MDOperand &getDwarfOperand(unsigned I) const {
304 return getOperand(I + 1);
306 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
307 replaceOperandWith(I + 1, New);
310 static bool classof(const Metadata *MD) {
311 return MD->getMetadataID() == GenericDebugNodeKind;
315 /// \brief Array subrange.
317 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
319 class MDSubrange : public DebugNode {
320 friend class LLVMContextImpl;
326 MDSubrange(LLVMContext &C, StorageType Storage, int64_t Count,
328 : DebugNode(C, MDSubrangeKind, Storage, dwarf::DW_TAG_subrange_type,
330 Count(Count), LowerBound(LowerBound) {}
331 ~MDSubrange() = default;
333 static MDSubrange *getImpl(LLVMContext &Context, int64_t Count,
334 int64_t LowerBound, StorageType Storage,
335 bool ShouldCreate = true);
337 TempMDSubrange cloneImpl() const {
338 return getTemporary(getContext(), getCount(), getLowerBound());
342 DEFINE_MDNODE_GET(MDSubrange, (int64_t Count, int64_t LowerBound = 0),
345 TempMDSubrange clone() const { return cloneImpl(); }
347 int64_t getLowerBound() const { return LowerBound; }
348 int64_t getCount() const { return Count; }
350 static bool classof(const Metadata *MD) {
351 return MD->getMetadataID() == MDSubrangeKind;
355 /// \brief Enumeration value.
357 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
358 /// longer creates a type cycle.
359 class MDEnumerator : public DebugNode {
360 friend class LLVMContextImpl;
365 MDEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
366 ArrayRef<Metadata *> Ops)
367 : DebugNode(C, MDEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
369 ~MDEnumerator() = default;
371 static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
372 StringRef Name, StorageType Storage,
373 bool ShouldCreate = true) {
374 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
377 static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
378 MDString *Name, StorageType Storage,
379 bool ShouldCreate = true);
381 TempMDEnumerator cloneImpl() const {
382 return getTemporary(getContext(), getValue(), getName());
386 DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, StringRef Name),
388 DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, MDString *Name),
391 TempMDEnumerator clone() const { return cloneImpl(); }
393 int64_t getValue() const { return Value; }
394 StringRef getName() const { return getStringOperand(0); }
396 MDString *getRawName() const { return getOperandAs<MDString>(0); }
398 static bool classof(const Metadata *MD) {
399 return MD->getMetadataID() == MDEnumeratorKind;
403 /// \brief Base class for scope-like contexts.
405 /// Base class for lexical scopes and types (which are also declaration
408 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
409 class MDScope : public DebugNode {
411 MDScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
412 ArrayRef<Metadata *> Ops)
413 : DebugNode(C, ID, Storage, Tag, Ops) {}
414 ~MDScope() = default;
417 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
419 inline StringRef getFilename() const;
420 inline StringRef getDirectory() const;
422 StringRef getName() const;
423 MDScopeRef getScope() const;
425 /// \brief Return the raw underlying file.
427 /// An \a MDFile is an \a MDScope, but it doesn't point at a separate file
428 /// (it\em is the file). If \c this is an \a MDFile, we need to return \c
429 /// this. Otherwise, return the first operand, which is where all other
430 /// subclasses store their file pointer.
431 Metadata *getRawFile() const {
432 return isa<MDFile>(this) ? const_cast<MDScope *>(this)
433 : static_cast<Metadata *>(getOperand(0));
436 MDScopeRef getRef() const { return MDScopeRef::get(this); }
438 static bool classof(const Metadata *MD) {
439 switch (MD->getMetadataID()) {
442 case MDBasicTypeKind:
443 case MDDerivedTypeKind:
444 case MDCompositeTypeKind:
445 case MDSubroutineTypeKind:
447 case MDCompileUnitKind:
448 case MDSubprogramKind:
449 case MDLexicalBlockKind:
450 case MDLexicalBlockFileKind:
451 case MDNamespaceKind:
459 /// TODO: Merge with directory/file node (including users).
460 /// TODO: Canonicalize paths on creation.
461 class MDFile : public MDScope {
462 friend class LLVMContextImpl;
465 MDFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
466 : MDScope(C, MDFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
469 static MDFile *getImpl(LLVMContext &Context, StringRef Filename,
470 StringRef Directory, StorageType Storage,
471 bool ShouldCreate = true) {
472 return getImpl(Context, getCanonicalMDString(Context, Filename),
473 getCanonicalMDString(Context, Directory), Storage,
476 static MDFile *getImpl(LLVMContext &Context, MDString *Filename,
477 MDString *Directory, StorageType Storage,
478 bool ShouldCreate = true);
480 TempMDFile cloneImpl() const {
481 return getTemporary(getContext(), getFilename(), getDirectory());
485 DEFINE_MDNODE_GET(MDFile, (StringRef Filename, StringRef Directory),
486 (Filename, Directory))
487 DEFINE_MDNODE_GET(MDFile, (MDString * Filename, MDString *Directory),
488 (Filename, Directory))
490 TempMDFile clone() const { return cloneImpl(); }
492 StringRef getFilename() const { return getStringOperand(0); }
493 StringRef getDirectory() const { return getStringOperand(1); }
495 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
496 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
498 static bool classof(const Metadata *MD) {
499 return MD->getMetadataID() == MDFileKind;
503 StringRef MDScope::getFilename() const {
504 if (auto *F = getFile())
505 return F->getFilename();
509 StringRef MDScope::getDirectory() const {
510 if (auto *F = getFile())
511 return F->getDirectory();
515 /// \brief Base class for types.
517 /// TODO: Remove the hardcoded name and context, since many types don't use
519 /// TODO: Split up flags.
520 class MDType : public MDScope {
524 uint64_t AlignInBits;
525 uint64_t OffsetInBits;
528 MDType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
529 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
530 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
531 : MDScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
532 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
533 OffsetInBits(OffsetInBits) {}
537 TempMDType clone() const {
538 return TempMDType(cast<MDType>(MDNode::clone().release()));
541 unsigned getLine() const { return Line; }
542 uint64_t getSizeInBits() const { return SizeInBits; }
543 uint64_t getAlignInBits() const { return AlignInBits; }
544 uint64_t getOffsetInBits() const { return OffsetInBits; }
545 unsigned getFlags() const { return Flags; }
547 MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
548 StringRef getName() const { return getStringOperand(2); }
551 Metadata *getRawScope() const { return getOperand(1); }
552 MDString *getRawName() const { return getOperandAs<MDString>(2); }
554 void setFlags(unsigned NewFlags) {
555 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
559 bool isPrivate() const {
560 return (getFlags() & FlagAccessibility) == FlagPrivate;
562 bool isProtected() const {
563 return (getFlags() & FlagAccessibility) == FlagProtected;
565 bool isPublic() const {
566 return (getFlags() & FlagAccessibility) == FlagPublic;
568 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
569 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
570 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
571 bool isVirtual() const { return getFlags() & FlagVirtual; }
572 bool isArtificial() const { return getFlags() & FlagArtificial; }
573 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
574 bool isObjcClassComplete() const {
575 return getFlags() & FlagObjcClassComplete;
577 bool isVector() const { return getFlags() & FlagVector; }
578 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
579 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
580 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
582 MDTypeRef getRef() const { return MDTypeRef::get(this); }
584 static bool classof(const Metadata *MD) {
585 switch (MD->getMetadataID()) {
588 case MDBasicTypeKind:
589 case MDDerivedTypeKind:
590 case MDCompositeTypeKind:
591 case MDSubroutineTypeKind:
597 /// \brief Basic type.
599 /// TODO: Split out DW_TAG_unspecified_type.
600 /// TODO: Drop unused accessors.
601 class MDBasicType : public MDType {
602 friend class LLVMContextImpl;
607 MDBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
608 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
609 ArrayRef<Metadata *> Ops)
610 : MDType(C, MDBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
612 Encoding(Encoding) {}
613 ~MDBasicType() = default;
615 static MDBasicType *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 MDBasicType *getImpl(LLVMContext &Context, unsigned Tag,
623 MDString *Name, uint64_t SizeInBits,
624 uint64_t AlignInBits, unsigned Encoding,
625 StorageType Storage, bool ShouldCreate = true);
627 TempMDBasicType cloneImpl() const {
628 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
629 getAlignInBits(), getEncoding());
633 DEFINE_MDNODE_GET(MDBasicType, (unsigned Tag, StringRef Name),
634 (Tag, Name, 0, 0, 0))
635 DEFINE_MDNODE_GET(MDBasicType,
636 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
637 uint64_t AlignInBits, unsigned Encoding),
638 (Tag, Name, SizeInBits, AlignInBits, Encoding))
639 DEFINE_MDNODE_GET(MDBasicType,
640 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
641 uint64_t AlignInBits, unsigned Encoding),
642 (Tag, Name, SizeInBits, AlignInBits, Encoding))
644 TempMDBasicType clone() const { return cloneImpl(); }
646 unsigned getEncoding() const { return Encoding; }
648 static bool classof(const Metadata *MD) {
649 return MD->getMetadataID() == MDBasicTypeKind;
653 /// \brief Base class for MDDerivedType and MDCompositeType.
655 /// TODO: Delete; they're not really related.
656 class MDDerivedTypeBase : public MDType {
658 MDDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
659 unsigned Tag, unsigned Line, uint64_t SizeInBits,
660 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
661 ArrayRef<Metadata *> Ops)
662 : MDType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
664 ~MDDerivedTypeBase() = default;
667 MDTypeRef getBaseType() const { return MDTypeRef(getRawBaseType()); }
668 Metadata *getRawBaseType() const { return getOperand(3); }
670 static bool classof(const Metadata *MD) {
671 return MD->getMetadataID() == MDDerivedTypeKind ||
672 MD->getMetadataID() == MDCompositeTypeKind ||
673 MD->getMetadataID() == MDSubroutineTypeKind;
677 /// \brief Derived types.
679 /// This includes qualified types, pointers, references, friends, typedefs, and
682 /// TODO: Split out members (inheritance, fields, methods, etc.).
683 class MDDerivedType : public MDDerivedTypeBase {
684 friend class LLVMContextImpl;
687 MDDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
688 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
689 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
690 : MDDerivedTypeBase(C, MDDerivedTypeKind, Storage, Tag, Line, SizeInBits,
691 AlignInBits, OffsetInBits, Flags, Ops) {}
692 ~MDDerivedType() = default;
694 static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
695 StringRef Name, MDFile *File, unsigned Line,
696 MDScopeRef Scope, MDTypeRef BaseType,
697 uint64_t SizeInBits, uint64_t AlignInBits,
698 uint64_t OffsetInBits, unsigned Flags,
699 Metadata *ExtraData, StorageType Storage,
700 bool ShouldCreate = true) {
701 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
702 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
703 Flags, ExtraData, Storage, ShouldCreate);
705 static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
706 MDString *Name, Metadata *File, unsigned Line,
707 Metadata *Scope, Metadata *BaseType,
708 uint64_t SizeInBits, uint64_t AlignInBits,
709 uint64_t OffsetInBits, unsigned Flags,
710 Metadata *ExtraData, StorageType Storage,
711 bool ShouldCreate = true);
713 TempMDDerivedType cloneImpl() const {
714 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
715 getScope(), getBaseType(), getSizeInBits(),
716 getAlignInBits(), getOffsetInBits(), getFlags(),
721 DEFINE_MDNODE_GET(MDDerivedType,
722 (unsigned Tag, MDString *Name, Metadata *File,
723 unsigned Line, Metadata *Scope, Metadata *BaseType,
724 uint64_t SizeInBits, uint64_t AlignInBits,
725 uint64_t OffsetInBits, unsigned Flags,
726 Metadata *ExtraData = nullptr),
727 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
728 AlignInBits, OffsetInBits, Flags, ExtraData))
729 DEFINE_MDNODE_GET(MDDerivedType,
730 (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
731 MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
732 uint64_t AlignInBits, uint64_t OffsetInBits,
733 unsigned Flags, Metadata *ExtraData = nullptr),
734 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
735 AlignInBits, OffsetInBits, Flags, ExtraData))
737 TempMDDerivedType clone() const { return cloneImpl(); }
739 /// \brief Get extra data associated with this derived type.
741 /// Class type for pointer-to-members, objective-c property node for ivars,
742 /// or global constant wrapper for static members.
744 /// TODO: Separate out types that need this extra operand: pointer-to-member
745 /// types and member fields (static members and ivars).
746 Metadata *getExtraData() const { return getRawExtraData(); }
747 Metadata *getRawExtraData() const { return getOperand(4); }
749 /// \brief Get casted version of extra data.
751 MDTypeRef getClassType() const {
752 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
753 return MDTypeRef(getExtraData());
755 MDObjCProperty *getObjCProperty() const {
756 return dyn_cast_or_null<MDObjCProperty>(getExtraData());
758 Constant *getConstant() const {
759 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
760 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
761 return C->getValue();
766 static bool classof(const Metadata *MD) {
767 return MD->getMetadataID() == MDDerivedTypeKind;
771 /// \brief Base class for MDCompositeType and MDSubroutineType.
773 /// TODO: Delete; they're not really related.
774 class MDCompositeTypeBase : public MDDerivedTypeBase {
775 unsigned RuntimeLang;
778 MDCompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
779 unsigned Tag, unsigned Line, unsigned RuntimeLang,
780 uint64_t SizeInBits, uint64_t AlignInBits,
781 uint64_t OffsetInBits, unsigned Flags,
782 ArrayRef<Metadata *> Ops)
783 : MDDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
784 OffsetInBits, Flags, Ops),
785 RuntimeLang(RuntimeLang) {}
786 ~MDCompositeTypeBase() = default;
789 /// \brief Get the elements of the composite type.
791 /// \note Calling this is only valid for \a MDCompositeType. This assertion
792 /// can be removed once \a MDSubroutineType has been separated from
793 /// "composite types".
794 DebugNodeArray getElements() const {
795 assert(!isa<MDSubroutineType>(this) && "no elements for DISubroutineType");
796 return cast_or_null<MDTuple>(getRawElements());
798 MDTypeRef getVTableHolder() const { return MDTypeRef(getRawVTableHolder()); }
799 MDTemplateParameterArray getTemplateParams() const {
800 return cast_or_null<MDTuple>(getRawTemplateParams());
802 StringRef getIdentifier() const { return getStringOperand(7); }
803 unsigned getRuntimeLang() const { return RuntimeLang; }
805 Metadata *getRawElements() const { return getOperand(4); }
806 Metadata *getRawVTableHolder() const { return getOperand(5); }
807 Metadata *getRawTemplateParams() const { return getOperand(6); }
808 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
810 /// \brief Replace operands.
812 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
813 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
814 /// of its movement if necessary.
816 void replaceElements(DebugNodeArray Elements) {
818 for (DebugNode *Op : getElements())
819 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
820 "Lost a member during member list replacement");
822 replaceOperandWith(4, Elements.get());
824 void replaceVTableHolder(MDTypeRef VTableHolder) {
825 replaceOperandWith(5, VTableHolder);
827 void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
828 replaceOperandWith(6, TemplateParams.get());
832 static bool classof(const Metadata *MD) {
833 return MD->getMetadataID() == MDCompositeTypeKind ||
834 MD->getMetadataID() == MDSubroutineTypeKind;
838 /// \brief Composite types.
840 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
841 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
842 class MDCompositeType : public MDCompositeTypeBase {
843 friend class LLVMContextImpl;
846 MDCompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
847 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
848 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
849 ArrayRef<Metadata *> Ops)
850 : MDCompositeTypeBase(C, MDCompositeTypeKind, Storage, Tag, Line,
851 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
853 ~MDCompositeType() = default;
855 static MDCompositeType *
856 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
857 unsigned Line, MDScopeRef Scope, MDTypeRef BaseType,
858 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
859 uint64_t Flags, DebugNodeArray Elements, unsigned RuntimeLang,
860 MDTypeRef VTableHolder, MDTemplateParameterArray TemplateParams,
861 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
863 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
864 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
865 RuntimeLang, VTableHolder, TemplateParams.get(),
866 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
868 static MDCompositeType *
869 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
870 unsigned Line, Metadata *Scope, Metadata *BaseType,
871 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
872 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
873 Metadata *VTableHolder, Metadata *TemplateParams,
874 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
876 TempMDCompositeType cloneImpl() const {
877 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
878 getScope(), getBaseType(), getSizeInBits(),
879 getAlignInBits(), getOffsetInBits(), getFlags(),
880 getElements(), getRuntimeLang(), getVTableHolder(),
881 getTemplateParams(), getIdentifier());
885 DEFINE_MDNODE_GET(MDCompositeType,
886 (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
887 MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
888 uint64_t AlignInBits, uint64_t OffsetInBits,
889 unsigned Flags, DebugNodeArray Elements,
890 unsigned RuntimeLang, MDTypeRef VTableHolder,
891 MDTemplateParameterArray TemplateParams = nullptr,
892 StringRef Identifier = ""),
893 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
894 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
895 VTableHolder, TemplateParams, Identifier))
896 DEFINE_MDNODE_GET(MDCompositeType,
897 (unsigned Tag, MDString *Name, Metadata *File,
898 unsigned Line, Metadata *Scope, Metadata *BaseType,
899 uint64_t SizeInBits, uint64_t AlignInBits,
900 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
901 unsigned RuntimeLang, Metadata *VTableHolder,
902 Metadata *TemplateParams = nullptr,
903 MDString *Identifier = nullptr),
904 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
905 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
906 VTableHolder, TemplateParams, Identifier))
908 TempMDCompositeType clone() const { return cloneImpl(); }
910 static bool classof(const Metadata *MD) {
911 return MD->getMetadataID() == MDCompositeTypeKind;
915 template <class T> TypedDebugNodeRef<T> TypedDebugNodeRef<T>::get(const T *N) {
917 if (auto *Composite = dyn_cast<MDCompositeType>(N))
918 if (auto *S = Composite->getRawIdentifier())
919 return TypedDebugNodeRef<T>(S);
920 return TypedDebugNodeRef<T>(N);
923 /// \brief Type array for a subprogram.
925 /// TODO: Detach from CompositeType, and fold the array of types in directly
927 class MDSubroutineType : public MDCompositeTypeBase {
928 friend class LLVMContextImpl;
931 MDSubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
932 ArrayRef<Metadata *> Ops)
933 : MDCompositeTypeBase(C, MDSubroutineTypeKind, Storage,
934 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
936 ~MDSubroutineType() = default;
938 static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
939 MDTypeRefArray TypeArray,
941 bool ShouldCreate = true) {
942 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
944 static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
945 Metadata *TypeArray, StorageType Storage,
946 bool ShouldCreate = true);
948 TempMDSubroutineType cloneImpl() const {
949 return getTemporary(getContext(), getFlags(), getTypeArray());
953 DEFINE_MDNODE_GET(MDSubroutineType,
954 (unsigned Flags, MDTypeRefArray TypeArray),
956 DEFINE_MDNODE_GET(MDSubroutineType, (unsigned Flags, Metadata *TypeArray),
959 TempMDSubroutineType clone() const { return cloneImpl(); }
961 MDTypeRefArray getTypeArray() const {
962 return cast_or_null<MDTuple>(getRawTypeArray());
964 Metadata *getRawTypeArray() const { return getRawElements(); }
966 static bool classof(const Metadata *MD) {
967 return MD->getMetadataID() == MDSubroutineTypeKind;
971 /// \brief Compile unit.
972 class MDCompileUnit : public MDScope {
973 friend class LLVMContextImpl;
976 unsigned SourceLanguage;
978 unsigned RuntimeVersion;
979 unsigned EmissionKind;
981 MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
982 bool IsOptimized, unsigned RuntimeVersion,
983 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
984 : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
985 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
986 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
987 ~MDCompileUnit() = default;
989 static MDCompileUnit *
990 getImpl(LLVMContext &Context, unsigned SourceLanguage, MDFile *File,
991 StringRef Producer, bool IsOptimized, StringRef Flags,
992 unsigned RuntimeVersion, StringRef SplitDebugFilename,
993 unsigned EmissionKind, MDCompositeTypeArray EnumTypes,
994 MDTypeArray RetainedTypes, MDSubprogramArray Subprograms,
995 MDGlobalVariableArray GlobalVariables,
996 MDImportedEntityArray ImportedEntities, StorageType Storage,
997 bool ShouldCreate = true) {
999 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
1000 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
1001 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
1002 EnumTypes.get(), RetainedTypes.get(), Subprograms.get(),
1003 GlobalVariables.get(), ImportedEntities.get(), Storage, ShouldCreate);
1005 static MDCompileUnit *
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, StorageType Storage,
1012 bool ShouldCreate = true);
1014 TempMDCompileUnit cloneImpl() const {
1015 return getTemporary(
1016 getContext(), getSourceLanguage(), getFile(), getProducer(),
1017 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1018 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1019 getGlobalVariables(), getImportedEntities());
1023 DEFINE_MDNODE_GET(MDCompileUnit,
1024 (unsigned SourceLanguage, MDFile *File, StringRef Producer,
1025 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1026 StringRef SplitDebugFilename, unsigned EmissionKind,
1027 MDCompositeTypeArray EnumTypes, MDTypeArray RetainedTypes,
1028 MDSubprogramArray Subprograms,
1029 MDGlobalVariableArray GlobalVariables,
1030 MDImportedEntityArray ImportedEntities),
1031 (SourceLanguage, File, Producer, IsOptimized, Flags,
1032 RuntimeVersion, SplitDebugFilename, EmissionKind,
1033 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1035 DEFINE_MDNODE_GET(MDCompileUnit,
1036 (unsigned SourceLanguage, Metadata *File,
1037 MDString *Producer, bool IsOptimized, MDString *Flags,
1038 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1039 unsigned EmissionKind, Metadata *EnumTypes,
1040 Metadata *RetainedTypes, Metadata *Subprograms,
1041 Metadata *GlobalVariables, Metadata *ImportedEntities),
1042 (SourceLanguage, File, Producer, IsOptimized, Flags,
1043 RuntimeVersion, SplitDebugFilename, EmissionKind,
1044 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1047 TempMDCompileUnit clone() const { return cloneImpl(); }
1049 unsigned getSourceLanguage() const { return SourceLanguage; }
1050 bool isOptimized() const { return IsOptimized; }
1051 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1052 unsigned getEmissionKind() const { return EmissionKind; }
1053 StringRef getProducer() const { return getStringOperand(1); }
1054 StringRef getFlags() const { return getStringOperand(2); }
1055 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1056 MDCompositeTypeArray getEnumTypes() const {
1057 return cast_or_null<MDTuple>(getRawEnumTypes());
1059 MDTypeArray getRetainedTypes() const {
1060 return cast_or_null<MDTuple>(getRawRetainedTypes());
1062 MDSubprogramArray getSubprograms() const {
1063 return cast_or_null<MDTuple>(getRawSubprograms());
1065 MDGlobalVariableArray getGlobalVariables() const {
1066 return cast_or_null<MDTuple>(getRawGlobalVariables());
1068 MDImportedEntityArray getImportedEntities() const {
1069 return cast_or_null<MDTuple>(getRawImportedEntities());
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 /// MDCompileUnit should be fairly rare.
1089 void replaceSubprograms(MDSubprogramArray N) {
1090 replaceOperandWith(6, N.get());
1092 void replaceGlobalVariables(MDGlobalVariableArray N) {
1093 replaceOperandWith(7, N.get());
1097 static bool classof(const Metadata *MD) {
1098 return MD->getMetadataID() == MDCompileUnitKind;
1102 /// \brief A scope for locals.
1104 /// A legal scope for lexical blocks, local variables, and debug info
1105 /// locations. Subclasses are \a MDSubprogram, \a MDLexicalBlock, and \a
1106 /// MDLexicalBlockFile.
1107 class MDLocalScope : public MDScope {
1109 MDLocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1110 ArrayRef<Metadata *> Ops)
1111 : MDScope(C, ID, Storage, Tag, Ops) {}
1112 ~MDLocalScope() = default;
1115 /// \brief Get the subprogram for this scope.
1117 /// Return this if it's an \a MDSubprogram; otherwise, look up the scope
1119 MDSubprogram *getSubprogram() const;
1121 static bool classof(const Metadata *MD) {
1122 return MD->getMetadataID() == MDSubprogramKind ||
1123 MD->getMetadataID() == MDLexicalBlockKind ||
1124 MD->getMetadataID() == MDLexicalBlockFileKind;
1128 /// \brief Debug location.
1130 /// A debug location in source code, used for debug info and otherwise.
1131 class MDLocation : public MDNode {
1132 friend class LLVMContextImpl;
1133 friend class MDNode;
1135 MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
1136 unsigned Column, ArrayRef<Metadata *> MDs);
1137 ~MDLocation() { dropAllReferences(); }
1139 static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1140 unsigned Column, Metadata *Scope,
1141 Metadata *InlinedAt, StorageType Storage,
1142 bool ShouldCreate = true);
1143 static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1144 unsigned Column, MDLocalScope *Scope,
1145 MDLocation *InlinedAt, StorageType Storage,
1146 bool ShouldCreate = true) {
1147 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1148 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1151 TempMDLocation cloneImpl() const {
1152 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1156 // Disallow replacing operands.
1157 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1160 DEFINE_MDNODE_GET(MDLocation,
1161 (unsigned Line, unsigned Column, Metadata *Scope,
1162 Metadata *InlinedAt = nullptr),
1163 (Line, Column, Scope, InlinedAt))
1164 DEFINE_MDNODE_GET(MDLocation,
1165 (unsigned Line, unsigned Column, MDLocalScope *Scope,
1166 MDLocation *InlinedAt = nullptr),
1167 (Line, Column, Scope, InlinedAt))
1169 /// \brief Return a (temporary) clone of this.
1170 TempMDLocation clone() const { return cloneImpl(); }
1172 unsigned getLine() const { return SubclassData32; }
1173 unsigned getColumn() const { return SubclassData16; }
1174 MDLocalScope *getScope() const {
1175 return cast<MDLocalScope>(getRawScope());
1177 MDLocation *getInlinedAt() const {
1178 return cast_or_null<MDLocation>(getRawInlinedAt());
1181 MDFile *getFile() const { return getScope()->getFile(); }
1182 StringRef getFilename() const { return getScope()->getFilename(); }
1183 StringRef getDirectory() const { return getScope()->getDirectory(); }
1185 /// \brief Get the scope where this is inlined.
1187 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1189 MDLocalScope *getInlinedAtScope() const {
1190 if (auto *IA = getInlinedAt())
1191 return IA->getInlinedAtScope();
1195 /// \brief Check whether this can be discriminated from another location.
1197 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1198 /// Scope and inlined-at chains are not recorded in the linetable, so they
1199 /// cannot be used to distinguish basic blocks.
1201 /// The current implementation is weaker than it should be, since it just
1202 /// checks filename and line.
1204 /// FIXME: Add a check for getDiscriminator().
1205 /// FIXME: Add a check for getColumn().
1206 /// FIXME: Change the getFilename() check to getFile() (or add one for
1207 /// getDirectory()).
1208 bool canDiscriminate(const MDLocation &RHS) const {
1209 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1212 /// \brief Get the DWARF discriminator.
1214 /// DWARF discriminators distinguish identical file locations between
1215 /// instructions that are on different basic blocks.
1216 inline unsigned getDiscriminator() const;
1218 /// \brief Compute new discriminator in the given context.
1220 /// This modifies the \a LLVMContext that \c this is in to increment the next
1221 /// discriminator for \c this's line/filename combination.
1223 /// FIXME: Delete this. See comments in implementation and at the only call
1224 /// site in \a AddDiscriminators::runOnFunction().
1225 unsigned computeNewDiscriminator() const;
1227 Metadata *getRawScope() const { return getOperand(0); }
1228 Metadata *getRawInlinedAt() const {
1229 if (getNumOperands() == 2)
1230 return getOperand(1);
1234 static bool classof(const Metadata *MD) {
1235 return MD->getMetadataID() == MDLocationKind;
1239 /// \brief Subprogram description.
1241 /// TODO: Remove DisplayName. It's always equal to Name.
1242 /// TODO: Split up flags.
1243 class MDSubprogram : public MDLocalScope {
1244 friend class LLVMContextImpl;
1245 friend class MDNode;
1249 unsigned Virtuality;
1250 unsigned VirtualIndex;
1256 MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1257 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1258 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1259 bool IsOptimized, ArrayRef<Metadata *> Ops)
1260 : MDLocalScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1262 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1263 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1264 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1265 ~MDSubprogram() = default;
1267 static MDSubprogram *
1268 getImpl(LLVMContext &Context, MDScopeRef Scope, StringRef Name,
1269 StringRef LinkageName, MDFile *File, unsigned Line,
1270 MDSubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1271 unsigned ScopeLine, MDTypeRef ContainingType, unsigned Virtuality,
1272 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1273 Constant *Function, MDTemplateParameterArray TemplateParams,
1274 MDSubprogram *Declaration, MDLocalVariableArray Variables,
1275 StorageType Storage, bool ShouldCreate = true) {
1276 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1277 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1278 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1279 Virtuality, VirtualIndex, Flags, IsOptimized,
1280 Function ? ConstantAsMetadata::get(Function) : nullptr,
1281 TemplateParams.get(), Declaration, Variables.get(), Storage,
1284 static MDSubprogram *
1285 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1286 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1287 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1288 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1289 unsigned Flags, bool IsOptimized, Metadata *Function,
1290 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1291 StorageType Storage, bool ShouldCreate = true);
1293 TempMDSubprogram cloneImpl() const {
1294 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1295 getFile(), getLine(), getType(), isLocalToUnit(),
1296 isDefinition(), getScopeLine(), getContainingType(),
1297 getVirtuality(), getVirtualIndex(), getFlags(),
1298 isOptimized(), getFunctionConstant(),
1299 getTemplateParams(), getDeclaration(), getVariables());
1303 DEFINE_MDNODE_GET(MDSubprogram,
1304 (MDScopeRef Scope, StringRef Name, StringRef LinkageName,
1305 MDFile *File, unsigned Line, MDSubroutineType *Type,
1306 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1307 MDTypeRef ContainingType, unsigned Virtuality,
1308 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1309 Constant *Function = nullptr,
1310 MDTemplateParameterArray TemplateParams = nullptr,
1311 MDSubprogram *Declaration = nullptr,
1312 MDLocalVariableArray Variables = nullptr),
1313 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1314 IsDefinition, ScopeLine, ContainingType, Virtuality,
1315 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1316 Declaration, Variables))
1319 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1320 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1321 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1322 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1323 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1324 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1325 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1326 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1327 Function, TemplateParams, Declaration, Variables))
1329 TempMDSubprogram clone() const { return cloneImpl(); }
1332 unsigned getLine() const { return Line; }
1333 unsigned getVirtuality() const { return Virtuality; }
1334 unsigned getVirtualIndex() const { return VirtualIndex; }
1335 unsigned getScopeLine() const { return ScopeLine; }
1336 unsigned getFlags() const { return Flags; }
1337 bool isLocalToUnit() const { return IsLocalToUnit; }
1338 bool isDefinition() const { return IsDefinition; }
1339 bool isOptimized() const { return IsOptimized; }
1341 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1342 bool isPrivate() const {
1343 return (getFlags() & FlagAccessibility) == FlagPrivate;
1345 bool isProtected() const {
1346 return (getFlags() & FlagAccessibility) == FlagProtected;
1348 bool isPublic() const {
1349 return (getFlags() & FlagAccessibility) == FlagPublic;
1351 bool isExplicit() const { return getFlags() & FlagExplicit; }
1352 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1354 /// \brief Check if this is reference-qualified.
1356 /// Return true if this subprogram is a C++11 reference-qualified non-static
1357 /// member function (void foo() &).
1358 unsigned isLValueReference() const {
1359 return getFlags() & FlagLValueReference;
1362 /// \brief Check if this is rvalue-reference-qualified.
1364 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1365 /// non-static member function (void foo() &&).
1366 unsigned isRValueReference() const {
1367 return getFlags() & FlagRValueReference;
1370 MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
1372 StringRef getName() const { return getStringOperand(2); }
1373 StringRef getDisplayName() const { return getStringOperand(3); }
1374 StringRef getLinkageName() const { return getStringOperand(4); }
1376 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1377 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1379 MDSubroutineType *getType() const {
1380 return cast_or_null<MDSubroutineType>(getRawType());
1382 MDTypeRef getContainingType() const {
1383 return MDTypeRef(getRawContainingType());
1386 Constant *getFunctionConstant() const {
1387 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1388 return C->getValue();
1391 MDTemplateParameterArray getTemplateParams() const {
1392 return cast_or_null<MDTuple>(getRawTemplateParams());
1394 MDSubprogram *getDeclaration() const {
1395 return cast_or_null<MDSubprogram>(getRawDeclaration());
1397 MDLocalVariableArray getVariables() const {
1398 return cast_or_null<MDTuple>(getRawVariables());
1401 Metadata *getRawScope() const { return getOperand(1); }
1402 Metadata *getRawType() const { return getOperand(5); }
1403 Metadata *getRawContainingType() const { return getOperand(6); }
1404 Metadata *getRawFunction() const { return getOperand(7); }
1405 Metadata *getRawTemplateParams() const { return getOperand(8); }
1406 Metadata *getRawDeclaration() const { return getOperand(9); }
1407 Metadata *getRawVariables() const { return getOperand(10); }
1409 /// \brief Get a pointer to the function this subprogram describes.
1411 /// This dyn_casts \a getFunctionConstant() to \a Function.
1413 /// FIXME: Should this be looking through bitcasts?
1414 Function *getFunction() const;
1416 /// \brief Replace the function.
1418 /// If \a isUniqued() and not \a isResolved(), this could node will be
1419 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1420 /// that's a problem.
1422 void replaceFunction(Function *F);
1423 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1424 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1427 /// \brief Check if this subprogram decribes the given function.
1429 /// FIXME: Should this be looking through bitcasts?
1430 bool describes(const Function *F) const;
1432 static bool classof(const Metadata *MD) {
1433 return MD->getMetadataID() == MDSubprogramKind;
1437 class MDLexicalBlockBase : public MDLocalScope {
1439 MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1440 ArrayRef<Metadata *> Ops)
1441 : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1442 ~MDLexicalBlockBase() = default;
1445 MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
1447 Metadata *getRawScope() const { return getOperand(1); }
1449 /// \brief Forwarding accessors to LexicalBlock.
1451 /// TODO: Remove these and update code to use \a MDLexicalBlock directly.
1453 inline unsigned getLine() const;
1454 inline unsigned getColumn() const;
1456 static bool classof(const Metadata *MD) {
1457 return MD->getMetadataID() == MDLexicalBlockKind ||
1458 MD->getMetadataID() == MDLexicalBlockFileKind;
1462 class MDLexicalBlock : public MDLexicalBlockBase {
1463 friend class LLVMContextImpl;
1464 friend class MDNode;
1469 MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1470 unsigned Column, ArrayRef<Metadata *> Ops)
1471 : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1473 ~MDLexicalBlock() = default;
1475 static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1476 MDFile *File, unsigned Line, unsigned Column,
1477 StorageType Storage,
1478 bool ShouldCreate = true) {
1479 return getImpl(Context, static_cast<Metadata *>(Scope),
1480 static_cast<Metadata *>(File), Line, Column, Storage,
1484 static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1485 Metadata *File, unsigned Line, unsigned Column,
1486 StorageType Storage, bool ShouldCreate = true);
1488 TempMDLexicalBlock cloneImpl() const {
1489 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1494 DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1495 unsigned Line, unsigned Column),
1496 (Scope, File, Line, Column))
1497 DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1498 unsigned Line, unsigned Column),
1499 (Scope, File, Line, Column))
1501 TempMDLexicalBlock clone() const { return cloneImpl(); }
1503 unsigned getLine() const { return Line; }
1504 unsigned getColumn() const { return Column; }
1506 static bool classof(const Metadata *MD) {
1507 return MD->getMetadataID() == MDLexicalBlockKind;
1511 unsigned MDLexicalBlockBase::getLine() const {
1512 if (auto *N = dyn_cast<MDLexicalBlock>(this))
1513 return N->getLine();
1517 unsigned MDLexicalBlockBase::getColumn() const {
1518 if (auto *N = dyn_cast<MDLexicalBlock>(this))
1519 return N->getColumn();
1523 class MDLexicalBlockFile : public MDLexicalBlockBase {
1524 friend class LLVMContextImpl;
1525 friend class MDNode;
1527 unsigned Discriminator;
1529 MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1530 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1531 : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1532 Discriminator(Discriminator) {}
1533 ~MDLexicalBlockFile() = default;
1535 static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1536 MDFile *File, unsigned Discriminator,
1537 StorageType Storage,
1538 bool ShouldCreate = true) {
1539 return getImpl(Context, static_cast<Metadata *>(Scope),
1540 static_cast<Metadata *>(File), Discriminator, Storage,
1544 static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1545 Metadata *File, unsigned Discriminator,
1546 StorageType Storage,
1547 bool ShouldCreate = true);
1549 TempMDLexicalBlockFile cloneImpl() const {
1550 return getTemporary(getContext(), getScope(), getFile(),
1551 getDiscriminator());
1555 DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1556 unsigned Discriminator),
1557 (Scope, File, Discriminator))
1558 DEFINE_MDNODE_GET(MDLexicalBlockFile,
1559 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1560 (Scope, File, Discriminator))
1562 TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1564 // TODO: Remove these once they're gone from MDLexicalBlockBase.
1565 unsigned getLine() const = delete;
1566 unsigned getColumn() const = delete;
1568 unsigned getDiscriminator() const { return Discriminator; }
1570 static bool classof(const Metadata *MD) {
1571 return MD->getMetadataID() == MDLexicalBlockFileKind;
1575 unsigned MDLocation::getDiscriminator() const {
1576 if (auto *F = dyn_cast<MDLexicalBlockFile>(getScope()))
1577 return F->getDiscriminator();
1581 class MDNamespace : public MDScope {
1582 friend class LLVMContextImpl;
1583 friend class MDNode;
1587 MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1588 ArrayRef<Metadata *> Ops)
1589 : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1592 ~MDNamespace() = default;
1594 static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1595 MDFile *File, StringRef Name, unsigned Line,
1596 StorageType Storage, bool ShouldCreate = true) {
1597 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1598 Line, Storage, ShouldCreate);
1600 static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1601 Metadata *File, MDString *Name, unsigned Line,
1602 StorageType Storage, bool ShouldCreate = true);
1604 TempMDNamespace cloneImpl() const {
1605 return getTemporary(getContext(), getScope(), getFile(), getName(),
1610 DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1612 (Scope, File, Name, Line))
1613 DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1614 MDString *Name, unsigned Line),
1615 (Scope, File, Name, Line))
1617 TempMDNamespace clone() const { return cloneImpl(); }
1619 unsigned getLine() const { return Line; }
1620 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1621 StringRef getName() const { return getStringOperand(2); }
1623 Metadata *getRawScope() const { return getOperand(1); }
1624 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1626 static bool classof(const Metadata *MD) {
1627 return MD->getMetadataID() == MDNamespaceKind;
1631 /// \brief Base class for template parameters.
1632 class MDTemplateParameter : public DebugNode {
1634 MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1635 unsigned Tag, ArrayRef<Metadata *> Ops)
1636 : DebugNode(Context, ID, Storage, Tag, Ops) {}
1637 ~MDTemplateParameter() = default;
1640 StringRef getName() const { return getStringOperand(0); }
1641 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1643 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1644 Metadata *getRawType() const { return getOperand(1); }
1646 static bool classof(const Metadata *MD) {
1647 return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1648 MD->getMetadataID() == MDTemplateValueParameterKind;
1652 class MDTemplateTypeParameter : public MDTemplateParameter {
1653 friend class LLVMContextImpl;
1654 friend class MDNode;
1656 MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1657 ArrayRef<Metadata *> Ops)
1658 : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1659 dwarf::DW_TAG_template_type_parameter, Ops) {}
1660 ~MDTemplateTypeParameter() = default;
1662 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1663 MDTypeRef Type, StorageType Storage,
1664 bool ShouldCreate = true) {
1665 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1668 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1669 Metadata *Type, StorageType Storage,
1670 bool ShouldCreate = true);
1672 TempMDTemplateTypeParameter cloneImpl() const {
1673 return getTemporary(getContext(), getName(), getType());
1677 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
1679 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1682 TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1684 static bool classof(const Metadata *MD) {
1685 return MD->getMetadataID() == MDTemplateTypeParameterKind;
1689 class MDTemplateValueParameter : public MDTemplateParameter {
1690 friend class LLVMContextImpl;
1691 friend class MDNode;
1693 MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1694 unsigned Tag, ArrayRef<Metadata *> Ops)
1695 : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1697 ~MDTemplateValueParameter() = default;
1699 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1700 StringRef Name, MDTypeRef Type,
1701 Metadata *Value, StorageType Storage,
1702 bool ShouldCreate = true) {
1703 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1704 Value, Storage, ShouldCreate);
1706 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1707 MDString *Name, Metadata *Type,
1708 Metadata *Value, StorageType Storage,
1709 bool ShouldCreate = true);
1711 TempMDTemplateValueParameter cloneImpl() const {
1712 return getTemporary(getContext(), getTag(), getName(), getType(),
1717 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1718 MDTypeRef Type, Metadata *Value),
1719 (Tag, Name, Type, Value))
1720 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1721 Metadata *Type, Metadata *Value),
1722 (Tag, Name, Type, Value))
1724 TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1726 Metadata *getValue() const { return getOperand(2); }
1728 static bool classof(const Metadata *MD) {
1729 return MD->getMetadataID() == MDTemplateValueParameterKind;
1733 /// \brief Base class for variables.
1735 /// TODO: Hardcode to DW_TAG_variable.
1736 class MDVariable : public DebugNode {
1740 MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1741 unsigned Line, ArrayRef<Metadata *> Ops)
1742 : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1743 ~MDVariable() = default;
1746 unsigned getLine() const { return Line; }
1747 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1748 StringRef getName() const { return getStringOperand(1); }
1749 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1750 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1752 StringRef getFilename() const {
1753 if (auto *F = getFile())
1754 return F->getFilename();
1757 StringRef getDirectory() const {
1758 if (auto *F = getFile())
1759 return F->getDirectory();
1763 Metadata *getRawScope() const { return getOperand(0); }
1764 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1765 Metadata *getRawFile() const { return getOperand(2); }
1766 Metadata *getRawType() const { return getOperand(3); }
1768 static bool classof(const Metadata *MD) {
1769 return MD->getMetadataID() == MDLocalVariableKind ||
1770 MD->getMetadataID() == MDGlobalVariableKind;
1774 /// \brief Global variables.
1776 /// TODO: Remove DisplayName. It's always equal to Name.
1777 class MDGlobalVariable : public MDVariable {
1778 friend class LLVMContextImpl;
1779 friend class MDNode;
1784 MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1785 bool IsLocalToUnit, bool IsDefinition,
1786 ArrayRef<Metadata *> Ops)
1787 : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1789 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1790 ~MDGlobalVariable() = default;
1792 static MDGlobalVariable *
1793 getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1794 StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
1795 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1796 MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1797 bool ShouldCreate = true) {
1798 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1799 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1800 IsLocalToUnit, IsDefinition,
1801 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1802 StaticDataMemberDeclaration, Storage, ShouldCreate);
1804 static MDGlobalVariable *
1805 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1806 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1807 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1808 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1809 bool ShouldCreate = true);
1811 TempMDGlobalVariable cloneImpl() const {
1812 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1813 getFile(), getLine(), getType(), isLocalToUnit(),
1814 isDefinition(), getVariable(),
1815 getStaticDataMemberDeclaration());
1819 DEFINE_MDNODE_GET(MDGlobalVariable,
1820 (MDScope * Scope, StringRef Name, StringRef LinkageName,
1821 MDFile *File, unsigned Line, MDTypeRef Type,
1822 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1823 MDDerivedType *StaticDataMemberDeclaration),
1824 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1825 IsDefinition, Variable, StaticDataMemberDeclaration))
1826 DEFINE_MDNODE_GET(MDGlobalVariable,
1827 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1828 Metadata *File, unsigned Line, Metadata *Type,
1829 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1830 Metadata *StaticDataMemberDeclaration),
1831 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1832 IsDefinition, Variable, StaticDataMemberDeclaration))
1834 TempMDGlobalVariable clone() const { return cloneImpl(); }
1836 bool isLocalToUnit() const { return IsLocalToUnit; }
1837 bool isDefinition() const { return IsDefinition; }
1838 StringRef getDisplayName() const { return getStringOperand(4); }
1839 StringRef getLinkageName() const { return getStringOperand(5); }
1840 Constant *getVariable() const {
1841 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1842 return dyn_cast<Constant>(C->getValue());
1845 MDDerivedType *getStaticDataMemberDeclaration() const {
1846 return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1849 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1850 Metadata *getRawVariable() const { return getOperand(6); }
1851 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1853 static bool classof(const Metadata *MD) {
1854 return MD->getMetadataID() == MDGlobalVariableKind;
1858 /// \brief Local variable.
1860 /// TODO: Split between arguments and otherwise.
1861 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1862 /// TODO: Split up flags.
1863 class MDLocalVariable : public MDVariable {
1864 friend class LLVMContextImpl;
1865 friend class MDNode;
1870 MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1871 unsigned Line, unsigned Arg, unsigned Flags,
1872 ArrayRef<Metadata *> Ops)
1873 : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1875 ~MDLocalVariable() = default;
1877 static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1878 MDScope *Scope, StringRef Name, MDFile *File,
1879 unsigned Line, MDTypeRef Type, unsigned Arg,
1880 unsigned Flags, StorageType Storage,
1881 bool ShouldCreate = true) {
1882 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1883 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1885 static MDLocalVariable *
1886 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1887 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1888 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1890 TempMDLocalVariable cloneImpl() const {
1891 return getTemporary(getContext(), getTag(), getScope(), getName(),
1892 getFile(), getLine(), getType(), getArg(), getFlags());
1896 DEFINE_MDNODE_GET(MDLocalVariable,
1897 (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1898 MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
1900 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1901 DEFINE_MDNODE_GET(MDLocalVariable,
1902 (unsigned Tag, Metadata *Scope, MDString *Name,
1903 Metadata *File, unsigned Line, Metadata *Type,
1904 unsigned Arg, unsigned Flags),
1905 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1907 TempMDLocalVariable clone() const { return cloneImpl(); }
1909 /// \brief Get the local scope for this variable.
1911 /// Variables must be defined in a local scope.
1912 MDLocalScope *getScope() const {
1913 return cast<MDLocalScope>(MDVariable::getScope());
1916 unsigned getArg() const { return Arg; }
1917 unsigned getFlags() const { return Flags; }
1919 bool isArtificial() const { return getFlags() & FlagArtificial; }
1920 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1922 /// \brief Check that a location is valid for this variable.
1924 /// Check that \c DL exists, is in the same subprogram, and has the same
1925 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1926 /// to a \a DbgInfoIntrinsic.)
1927 bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1928 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1931 static bool classof(const Metadata *MD) {
1932 return MD->getMetadataID() == MDLocalVariableKind;
1936 /// \brief DWARF expression.
1938 /// This is (almost) a DWARF expression that modifies the location of a
1939 /// variable or (or the location of a single piece of a variable).
1941 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1942 /// and have DW_OP_plus consume the topmost elements on the stack.
1944 /// TODO: Co-allocate the expression elements.
1945 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1947 class MDExpression : public MDNode {
1948 friend class LLVMContextImpl;
1949 friend class MDNode;
1951 std::vector<uint64_t> Elements;
1953 MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1954 : MDNode(C, MDExpressionKind, Storage, None),
1955 Elements(Elements.begin(), Elements.end()) {}
1956 ~MDExpression() = default;
1958 static MDExpression *getImpl(LLVMContext &Context,
1959 ArrayRef<uint64_t> Elements, StorageType Storage,
1960 bool ShouldCreate = true);
1962 TempMDExpression cloneImpl() const {
1963 return getTemporary(getContext(), getElements());
1967 DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1969 TempMDExpression clone() const { return cloneImpl(); }
1971 ArrayRef<uint64_t> getElements() const { return Elements; }
1973 unsigned getNumElements() const { return Elements.size(); }
1974 uint64_t getElement(unsigned I) const {
1975 assert(I < Elements.size() && "Index out of range");
1979 /// \brief Return whether this is a piece of an aggregate variable.
1980 bool isBitPiece() const;
1982 /// \brief Return the offset of this piece in bits.
1983 uint64_t getBitPieceOffset() const;
1985 /// \brief Return the size of this piece in bits.
1986 uint64_t getBitPieceSize() const;
1988 typedef ArrayRef<uint64_t>::iterator element_iterator;
1989 element_iterator elements_begin() const { return getElements().begin(); }
1990 element_iterator elements_end() const { return getElements().end(); }
1992 /// \brief A lightweight wrapper around an expression operand.
1994 /// TODO: Store arguments directly and change \a MDExpression to store a
2000 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2002 const uint64_t *get() const { return Op; }
2004 /// \brief Get the operand code.
2005 uint64_t getOp() const { return *Op; }
2007 /// \brief Get an argument to the operand.
2009 /// Never returns the operand itself.
2010 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2012 unsigned getNumArgs() const { return getSize() - 1; }
2014 /// \brief Return the size of the operand.
2016 /// Return the number of elements in the operand (1 + args).
2017 unsigned getSize() const;
2020 /// \brief An iterator for expression operands.
2021 class expr_op_iterator
2022 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2026 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2028 element_iterator getBase() const { return Op.get(); }
2029 const ExprOperand &operator*() const { return Op; }
2030 const ExprOperand *operator->() const { return &Op; }
2032 expr_op_iterator &operator++() {
2036 expr_op_iterator operator++(int) {
2037 expr_op_iterator T(*this);
2042 /// \brief Get the next iterator.
2044 /// \a std::next() doesn't work because this is technically an
2045 /// input_iterator, but it's a perfectly valid operation. This is an
2046 /// accessor to provide the same functionality.
2047 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2049 bool operator==(const expr_op_iterator &X) const {
2050 return getBase() == X.getBase();
2052 bool operator!=(const expr_op_iterator &X) const {
2053 return getBase() != X.getBase();
2057 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2060 /// \brief Visit the elements via ExprOperand wrappers.
2062 /// These range iterators visit elements through \a ExprOperand wrappers.
2063 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2066 /// \pre \a isValid() gives \c true.
2068 expr_op_iterator expr_op_begin() const {
2069 return expr_op_iterator(elements_begin());
2071 expr_op_iterator expr_op_end() const {
2072 return expr_op_iterator(elements_end());
2076 bool isValid() const;
2078 static bool classof(const Metadata *MD) {
2079 return MD->getMetadataID() == MDExpressionKind;
2083 class MDObjCProperty : public DebugNode {
2084 friend class LLVMContextImpl;
2085 friend class MDNode;
2088 unsigned Attributes;
2090 MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2091 unsigned Attributes, ArrayRef<Metadata *> Ops)
2092 : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2094 Line(Line), Attributes(Attributes) {}
2095 ~MDObjCProperty() = default;
2097 static MDObjCProperty *
2098 getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
2099 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2100 MDType *Type, StorageType Storage, bool ShouldCreate = true) {
2101 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2102 getCanonicalMDString(Context, GetterName),
2103 getCanonicalMDString(Context, SetterName), Attributes, Type,
2104 Storage, ShouldCreate);
2106 static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2107 Metadata *File, unsigned Line,
2108 MDString *GetterName, MDString *SetterName,
2109 unsigned Attributes, Metadata *Type,
2110 StorageType Storage, bool ShouldCreate = true);
2112 TempMDObjCProperty cloneImpl() const {
2113 return getTemporary(getContext(), getName(), getFile(), getLine(),
2114 getGetterName(), getSetterName(), getAttributes(),
2119 DEFINE_MDNODE_GET(MDObjCProperty,
2120 (StringRef Name, MDFile *File, unsigned Line,
2121 StringRef GetterName, StringRef SetterName,
2122 unsigned Attributes, MDType *Type),
2123 (Name, File, Line, GetterName, SetterName, Attributes,
2125 DEFINE_MDNODE_GET(MDObjCProperty,
2126 (MDString * Name, Metadata *File, unsigned Line,
2127 MDString *GetterName, MDString *SetterName,
2128 unsigned Attributes, Metadata *Type),
2129 (Name, File, Line, GetterName, SetterName, Attributes,
2132 TempMDObjCProperty clone() const { return cloneImpl(); }
2134 unsigned getLine() const { return Line; }
2135 unsigned getAttributes() const { return Attributes; }
2136 StringRef getName() const { return getStringOperand(0); }
2137 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
2138 StringRef getGetterName() const { return getStringOperand(2); }
2139 StringRef getSetterName() const { return getStringOperand(3); }
2141 /// \brief Get the type.
2143 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
2144 /// the type as a DITypeRef here.
2145 MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
2147 StringRef getFilename() const {
2148 if (auto *F = getFile())
2149 return F->getFilename();
2152 StringRef getDirectory() const {
2153 if (auto *F = getFile())
2154 return F->getDirectory();
2158 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2159 Metadata *getRawFile() const { return getOperand(1); }
2160 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2161 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2162 Metadata *getRawType() const { return getOperand(4); }
2164 static bool classof(const Metadata *MD) {
2165 return MD->getMetadataID() == MDObjCPropertyKind;
2169 /// \brief An imported module (C++ using directive or similar).
2170 class MDImportedEntity : public DebugNode {
2171 friend class LLVMContextImpl;
2172 friend class MDNode;
2176 MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2177 unsigned Line, ArrayRef<Metadata *> Ops)
2178 : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2179 ~MDImportedEntity() = default;
2181 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2182 MDScope *Scope, DebugNodeRef Entity,
2183 unsigned Line, StringRef Name,
2184 StorageType Storage,
2185 bool ShouldCreate = true) {
2186 return getImpl(Context, Tag, Scope, Entity, Line,
2187 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2189 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2190 Metadata *Scope, Metadata *Entity,
2191 unsigned Line, MDString *Name,
2192 StorageType Storage,
2193 bool ShouldCreate = true);
2195 TempMDImportedEntity cloneImpl() const {
2196 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2197 getLine(), getName());
2201 DEFINE_MDNODE_GET(MDImportedEntity,
2202 (unsigned Tag, MDScope *Scope, DebugNodeRef Entity,
2203 unsigned Line, StringRef Name = ""),
2204 (Tag, Scope, Entity, Line, Name))
2205 DEFINE_MDNODE_GET(MDImportedEntity,
2206 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2207 unsigned Line, MDString *Name),
2208 (Tag, Scope, Entity, Line, Name))
2210 TempMDImportedEntity clone() const { return cloneImpl(); }
2212 unsigned getLine() const { return Line; }
2213 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
2214 DebugNodeRef getEntity() const { return DebugNodeRef(getRawEntity()); }
2215 StringRef getName() const { return getStringOperand(2); }
2217 Metadata *getRawScope() const { return getOperand(0); }
2218 Metadata *getRawEntity() const { return getOperand(1); }
2219 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2221 static bool classof(const Metadata *MD) {
2222 return MD->getMetadataID() == MDImportedEntityKind;
2226 } // end namespace llvm
2228 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2229 #undef DEFINE_MDNODE_GET_UNPACK
2230 #undef DEFINE_MDNODE_GET