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 DebugNodeArray getElements() const {
790 return cast_or_null<MDTuple>(getRawElements());
792 MDTypeRef getVTableHolder() const { return MDTypeRef(getRawVTableHolder()); }
793 MDTemplateParameterArray getTemplateParams() const {
794 return cast_or_null<MDTuple>(getRawTemplateParams());
796 StringRef getIdentifier() const { return getStringOperand(7); }
797 unsigned getRuntimeLang() const { return RuntimeLang; }
799 Metadata *getRawElements() const { return getOperand(4); }
800 Metadata *getRawVTableHolder() const { return getOperand(5); }
801 Metadata *getRawTemplateParams() const { return getOperand(6); }
802 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
804 /// \brief Replace operands.
806 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
807 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
808 /// of its movement if necessary.
810 void replaceElements(DebugNodeArray Elements) {
812 for (DebugNode *Op : getElements())
813 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
814 "Lost a member during member list replacement");
816 replaceOperandWith(4, Elements.get());
818 void replaceVTableHolder(MDTypeRef VTableHolder) {
819 replaceOperandWith(5, VTableHolder);
821 void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
822 replaceOperandWith(6, TemplateParams.get());
826 static bool classof(const Metadata *MD) {
827 return MD->getMetadataID() == MDCompositeTypeKind ||
828 MD->getMetadataID() == MDSubroutineTypeKind;
832 /// \brief Composite types.
834 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
835 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
836 class MDCompositeType : public MDCompositeTypeBase {
837 friend class LLVMContextImpl;
840 MDCompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
841 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
842 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
843 ArrayRef<Metadata *> Ops)
844 : MDCompositeTypeBase(C, MDCompositeTypeKind, Storage, Tag, Line,
845 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
847 ~MDCompositeType() = default;
849 static MDCompositeType *
850 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
851 unsigned Line, MDScopeRef Scope, MDTypeRef BaseType,
852 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
853 uint64_t Flags, DebugNodeArray Elements, unsigned RuntimeLang,
854 MDTypeRef VTableHolder, MDTemplateParameterArray TemplateParams,
855 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
857 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
858 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
859 RuntimeLang, VTableHolder, TemplateParams.get(),
860 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
862 static MDCompositeType *
863 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
864 unsigned Line, Metadata *Scope, Metadata *BaseType,
865 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
866 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
867 Metadata *VTableHolder, Metadata *TemplateParams,
868 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
870 TempMDCompositeType cloneImpl() const {
871 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
872 getScope(), getBaseType(), getSizeInBits(),
873 getAlignInBits(), getOffsetInBits(), getFlags(),
874 getElements(), getRuntimeLang(), getVTableHolder(),
875 getTemplateParams(), getIdentifier());
879 DEFINE_MDNODE_GET(MDCompositeType,
880 (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
881 MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
882 uint64_t AlignInBits, uint64_t OffsetInBits,
883 unsigned Flags, DebugNodeArray Elements,
884 unsigned RuntimeLang, MDTypeRef VTableHolder,
885 MDTemplateParameterArray TemplateParams = nullptr,
886 StringRef Identifier = ""),
887 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
888 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
889 VTableHolder, TemplateParams, Identifier))
890 DEFINE_MDNODE_GET(MDCompositeType,
891 (unsigned Tag, MDString *Name, Metadata *File,
892 unsigned Line, Metadata *Scope, Metadata *BaseType,
893 uint64_t SizeInBits, uint64_t AlignInBits,
894 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
895 unsigned RuntimeLang, Metadata *VTableHolder,
896 Metadata *TemplateParams = nullptr,
897 MDString *Identifier = nullptr),
898 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
899 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
900 VTableHolder, TemplateParams, Identifier))
902 TempMDCompositeType clone() const { return cloneImpl(); }
904 static bool classof(const Metadata *MD) {
905 return MD->getMetadataID() == MDCompositeTypeKind;
909 template <class T> TypedDebugNodeRef<T> TypedDebugNodeRef<T>::get(const T *N) {
911 if (auto *Composite = dyn_cast<MDCompositeType>(N))
912 if (auto *S = Composite->getRawIdentifier())
913 return TypedDebugNodeRef<T>(S);
914 return TypedDebugNodeRef<T>(N);
917 /// \brief Type array for a subprogram.
919 /// TODO: Detach from CompositeType, and fold the array of types in directly
921 class MDSubroutineType : public MDCompositeTypeBase {
922 friend class LLVMContextImpl;
925 MDSubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
926 ArrayRef<Metadata *> Ops)
927 : MDCompositeTypeBase(C, MDSubroutineTypeKind, Storage,
928 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
930 ~MDSubroutineType() = default;
932 static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
933 MDTypeRefArray TypeArray,
935 bool ShouldCreate = true) {
936 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
938 static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
939 Metadata *TypeArray, StorageType Storage,
940 bool ShouldCreate = true);
942 TempMDSubroutineType cloneImpl() const {
943 return getTemporary(getContext(), getFlags(), getTypeArray());
947 DEFINE_MDNODE_GET(MDSubroutineType,
948 (unsigned Flags, MDTypeRefArray TypeArray),
950 DEFINE_MDNODE_GET(MDSubroutineType, (unsigned Flags, Metadata *TypeArray),
953 TempMDSubroutineType clone() const { return cloneImpl(); }
955 MDTypeRefArray getTypeArray() const {
956 return cast_or_null<MDTuple>(getRawTypeArray());
958 Metadata *getRawTypeArray() const { return getRawElements(); }
960 static bool classof(const Metadata *MD) {
961 return MD->getMetadataID() == MDSubroutineTypeKind;
965 /// \brief Compile unit.
966 class MDCompileUnit : public MDScope {
967 friend class LLVMContextImpl;
970 unsigned SourceLanguage;
972 unsigned RuntimeVersion;
973 unsigned EmissionKind;
975 MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
976 bool IsOptimized, unsigned RuntimeVersion,
977 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
978 : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
979 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
980 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
981 ~MDCompileUnit() = default;
983 static MDCompileUnit *
984 getImpl(LLVMContext &Context, unsigned SourceLanguage, MDFile *File,
985 StringRef Producer, bool IsOptimized, StringRef Flags,
986 unsigned RuntimeVersion, StringRef SplitDebugFilename,
987 unsigned EmissionKind, MDCompositeTypeArray EnumTypes,
988 MDTypeArray RetainedTypes, MDSubprogramArray Subprograms,
989 MDGlobalVariableArray GlobalVariables,
990 MDImportedEntityArray ImportedEntities, StorageType Storage,
991 bool ShouldCreate = true) {
993 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
994 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
995 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
996 EnumTypes.get(), RetainedTypes.get(), Subprograms.get(),
997 GlobalVariables.get(), ImportedEntities.get(), Storage, ShouldCreate);
999 static MDCompileUnit *
1000 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1001 MDString *Producer, bool IsOptimized, MDString *Flags,
1002 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1003 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1004 Metadata *Subprograms, Metadata *GlobalVariables,
1005 Metadata *ImportedEntities, StorageType Storage,
1006 bool ShouldCreate = true);
1008 TempMDCompileUnit cloneImpl() const {
1009 return getTemporary(
1010 getContext(), getSourceLanguage(), getFile(), getProducer(),
1011 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1012 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1013 getGlobalVariables(), getImportedEntities());
1017 DEFINE_MDNODE_GET(MDCompileUnit,
1018 (unsigned SourceLanguage, MDFile *File, StringRef Producer,
1019 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1020 StringRef SplitDebugFilename, unsigned EmissionKind,
1021 MDCompositeTypeArray EnumTypes, MDTypeArray RetainedTypes,
1022 MDSubprogramArray Subprograms,
1023 MDGlobalVariableArray GlobalVariables,
1024 MDImportedEntityArray ImportedEntities),
1025 (SourceLanguage, File, Producer, IsOptimized, Flags,
1026 RuntimeVersion, SplitDebugFilename, EmissionKind,
1027 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1029 DEFINE_MDNODE_GET(MDCompileUnit,
1030 (unsigned SourceLanguage, Metadata *File,
1031 MDString *Producer, bool IsOptimized, MDString *Flags,
1032 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1033 unsigned EmissionKind, Metadata *EnumTypes,
1034 Metadata *RetainedTypes, Metadata *Subprograms,
1035 Metadata *GlobalVariables, Metadata *ImportedEntities),
1036 (SourceLanguage, File, Producer, IsOptimized, Flags,
1037 RuntimeVersion, SplitDebugFilename, EmissionKind,
1038 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1041 TempMDCompileUnit clone() const { return cloneImpl(); }
1043 unsigned getSourceLanguage() const { return SourceLanguage; }
1044 bool isOptimized() const { return IsOptimized; }
1045 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1046 unsigned getEmissionKind() const { return EmissionKind; }
1047 StringRef getProducer() const { return getStringOperand(1); }
1048 StringRef getFlags() const { return getStringOperand(2); }
1049 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1050 MDCompositeTypeArray getEnumTypes() const {
1051 return cast_or_null<MDTuple>(getRawEnumTypes());
1053 MDTypeArray getRetainedTypes() const {
1054 return cast_or_null<MDTuple>(getRawRetainedTypes());
1056 MDSubprogramArray getSubprograms() const {
1057 return cast_or_null<MDTuple>(getRawSubprograms());
1059 MDGlobalVariableArray getGlobalVariables() const {
1060 return cast_or_null<MDTuple>(getRawGlobalVariables());
1062 MDImportedEntityArray getImportedEntities() const {
1063 return cast_or_null<MDTuple>(getRawImportedEntities());
1066 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1067 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1068 MDString *getRawSplitDebugFilename() const {
1069 return getOperandAs<MDString>(3);
1071 Metadata *getRawEnumTypes() const { return getOperand(4); }
1072 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1073 Metadata *getRawSubprograms() const { return getOperand(6); }
1074 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1075 Metadata *getRawImportedEntities() const { return getOperand(8); }
1077 /// \brief Replace arrays.
1079 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1080 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1081 /// MDCompileUnit should be fairly rare.
1083 void replaceSubprograms(MDSubprogramArray N) {
1084 replaceOperandWith(6, N.get());
1086 void replaceGlobalVariables(MDGlobalVariableArray N) {
1087 replaceOperandWith(7, N.get());
1091 static bool classof(const Metadata *MD) {
1092 return MD->getMetadataID() == MDCompileUnitKind;
1096 /// \brief A scope for locals.
1098 /// A legal scope for lexical blocks, local variables, and debug info
1099 /// locations. Subclasses are \a MDSubprogram, \a MDLexicalBlock, and \a
1100 /// MDLexicalBlockFile.
1101 class MDLocalScope : public MDScope {
1103 MDLocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1104 ArrayRef<Metadata *> Ops)
1105 : MDScope(C, ID, Storage, Tag, Ops) {}
1106 ~MDLocalScope() = default;
1109 /// \brief Get the subprogram for this scope.
1111 /// Return this if it's an \a MDSubprogram; otherwise, look up the scope
1113 MDSubprogram *getSubprogram() const;
1115 static bool classof(const Metadata *MD) {
1116 return MD->getMetadataID() == MDSubprogramKind ||
1117 MD->getMetadataID() == MDLexicalBlockKind ||
1118 MD->getMetadataID() == MDLexicalBlockFileKind;
1122 /// \brief Debug location.
1124 /// A debug location in source code, used for debug info and otherwise.
1125 class MDLocation : public MDNode {
1126 friend class LLVMContextImpl;
1127 friend class MDNode;
1129 MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
1130 unsigned Column, ArrayRef<Metadata *> MDs);
1131 ~MDLocation() { dropAllReferences(); }
1133 static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1134 unsigned Column, Metadata *Scope,
1135 Metadata *InlinedAt, StorageType Storage,
1136 bool ShouldCreate = true);
1137 static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
1138 unsigned Column, MDLocalScope *Scope,
1139 MDLocation *InlinedAt, StorageType Storage,
1140 bool ShouldCreate = true) {
1141 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1142 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1145 TempMDLocation cloneImpl() const {
1146 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1150 // Disallow replacing operands.
1151 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1154 DEFINE_MDNODE_GET(MDLocation,
1155 (unsigned Line, unsigned Column, Metadata *Scope,
1156 Metadata *InlinedAt = nullptr),
1157 (Line, Column, Scope, InlinedAt))
1158 DEFINE_MDNODE_GET(MDLocation,
1159 (unsigned Line, unsigned Column, MDLocalScope *Scope,
1160 MDLocation *InlinedAt = nullptr),
1161 (Line, Column, Scope, InlinedAt))
1163 /// \brief Return a (temporary) clone of this.
1164 TempMDLocation clone() const { return cloneImpl(); }
1166 unsigned getLine() const { return SubclassData32; }
1167 unsigned getColumn() const { return SubclassData16; }
1168 MDLocalScope *getScope() const {
1169 return cast<MDLocalScope>(getRawScope());
1171 MDLocation *getInlinedAt() const {
1172 return cast_or_null<MDLocation>(getRawInlinedAt());
1175 MDFile *getFile() const { return getScope()->getFile(); }
1176 StringRef getFilename() const { return getScope()->getFilename(); }
1177 StringRef getDirectory() const { return getScope()->getDirectory(); }
1179 /// \brief Get the scope where this is inlined.
1181 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1183 MDLocalScope *getInlinedAtScope() const {
1184 if (auto *IA = getInlinedAt())
1185 return IA->getInlinedAtScope();
1189 /// \brief Check whether this can be discriminated from another location.
1191 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1192 /// Scope and inlined-at chains are not recorded in the linetable, so they
1193 /// cannot be used to distinguish basic blocks.
1195 /// The current implementation is weaker than it should be, since it just
1196 /// checks filename and line.
1198 /// FIXME: Add a check for getDiscriminator().
1199 /// FIXME: Add a check for getColumn().
1200 /// FIXME: Change the getFilename() check to getFile() (or add one for
1201 /// getDirectory()).
1202 bool canDiscriminate(const MDLocation &RHS) const {
1203 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1206 /// \brief Get the DWARF discriminator.
1208 /// DWARF discriminators distinguish identical file locations between
1209 /// instructions that are on different basic blocks.
1210 inline unsigned getDiscriminator() const;
1212 Metadata *getRawScope() const { return getOperand(0); }
1213 Metadata *getRawInlinedAt() const {
1214 if (getNumOperands() == 2)
1215 return getOperand(1);
1219 static bool classof(const Metadata *MD) {
1220 return MD->getMetadataID() == MDLocationKind;
1224 /// \brief Subprogram description.
1226 /// TODO: Remove DisplayName. It's always equal to Name.
1227 /// TODO: Split up flags.
1228 class MDSubprogram : public MDLocalScope {
1229 friend class LLVMContextImpl;
1230 friend class MDNode;
1234 unsigned Virtuality;
1235 unsigned VirtualIndex;
1241 MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1242 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1243 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1244 bool IsOptimized, ArrayRef<Metadata *> Ops)
1245 : MDLocalScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1247 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1248 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1249 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1250 ~MDSubprogram() = default;
1252 static MDSubprogram *
1253 getImpl(LLVMContext &Context, MDScopeRef Scope, StringRef Name,
1254 StringRef LinkageName, MDFile *File, unsigned Line,
1255 MDSubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1256 unsigned ScopeLine, MDTypeRef ContainingType, unsigned Virtuality,
1257 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1258 Constant *Function, MDTemplateParameterArray TemplateParams,
1259 MDSubprogram *Declaration, MDLocalVariableArray Variables,
1260 StorageType Storage, bool ShouldCreate = true) {
1261 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1262 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1263 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1264 Virtuality, VirtualIndex, Flags, IsOptimized,
1265 Function ? ConstantAsMetadata::get(Function) : nullptr,
1266 TemplateParams.get(), Declaration, Variables.get(), Storage,
1269 static MDSubprogram *
1270 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1271 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1272 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1273 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1274 unsigned Flags, bool IsOptimized, Metadata *Function,
1275 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1276 StorageType Storage, bool ShouldCreate = true);
1278 TempMDSubprogram cloneImpl() const {
1279 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1280 getFile(), getLine(), getType(), isLocalToUnit(),
1281 isDefinition(), getScopeLine(), getContainingType(),
1282 getVirtuality(), getVirtualIndex(), getFlags(),
1283 isOptimized(), getFunctionConstant(),
1284 getTemplateParams(), getDeclaration(), getVariables());
1288 DEFINE_MDNODE_GET(MDSubprogram,
1289 (MDScopeRef Scope, StringRef Name, StringRef LinkageName,
1290 MDFile *File, unsigned Line, MDSubroutineType *Type,
1291 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1292 MDTypeRef ContainingType, unsigned Virtuality,
1293 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1294 Constant *Function = nullptr,
1295 MDTemplateParameterArray TemplateParams = nullptr,
1296 MDSubprogram *Declaration = nullptr,
1297 MDLocalVariableArray Variables = nullptr),
1298 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1299 IsDefinition, ScopeLine, ContainingType, Virtuality,
1300 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1301 Declaration, Variables))
1304 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1305 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1306 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1307 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1308 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1309 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1310 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1311 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1312 Function, TemplateParams, Declaration, Variables))
1314 TempMDSubprogram clone() const { return cloneImpl(); }
1317 unsigned getLine() const { return Line; }
1318 unsigned getVirtuality() const { return Virtuality; }
1319 unsigned getVirtualIndex() const { return VirtualIndex; }
1320 unsigned getScopeLine() const { return ScopeLine; }
1321 unsigned getFlags() const { return Flags; }
1322 bool isLocalToUnit() const { return IsLocalToUnit; }
1323 bool isDefinition() const { return IsDefinition; }
1324 bool isOptimized() const { return IsOptimized; }
1326 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1327 bool isPrivate() const {
1328 return (getFlags() & FlagAccessibility) == FlagPrivate;
1330 bool isProtected() const {
1331 return (getFlags() & FlagAccessibility) == FlagProtected;
1333 bool isPublic() const {
1334 return (getFlags() & FlagAccessibility) == FlagPublic;
1336 bool isExplicit() const { return getFlags() & FlagExplicit; }
1337 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1339 /// \brief Check if this is reference-qualified.
1341 /// Return true if this subprogram is a C++11 reference-qualified non-static
1342 /// member function (void foo() &).
1343 unsigned isLValueReference() const {
1344 return getFlags() & FlagLValueReference;
1347 /// \brief Check if this is rvalue-reference-qualified.
1349 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1350 /// non-static member function (void foo() &&).
1351 unsigned isRValueReference() const {
1352 return getFlags() & FlagRValueReference;
1355 MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
1357 StringRef getName() const { return getStringOperand(2); }
1358 StringRef getDisplayName() const { return getStringOperand(3); }
1359 StringRef getLinkageName() const { return getStringOperand(4); }
1361 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1362 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1364 MDSubroutineType *getType() const {
1365 return cast_or_null<MDSubroutineType>(getRawType());
1367 MDTypeRef getContainingType() const {
1368 return MDTypeRef(getRawContainingType());
1371 Constant *getFunctionConstant() const {
1372 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1373 return C->getValue();
1376 MDTemplateParameterArray getTemplateParams() const {
1377 return cast_or_null<MDTuple>(getRawTemplateParams());
1379 MDSubprogram *getDeclaration() const {
1380 return cast_or_null<MDSubprogram>(getRawDeclaration());
1382 MDLocalVariableArray getVariables() const {
1383 return cast_or_null<MDTuple>(getRawVariables());
1386 Metadata *getRawScope() const { return getOperand(1); }
1387 Metadata *getRawType() const { return getOperand(5); }
1388 Metadata *getRawContainingType() const { return getOperand(6); }
1389 Metadata *getRawFunction() const { return getOperand(7); }
1390 Metadata *getRawTemplateParams() const { return getOperand(8); }
1391 Metadata *getRawDeclaration() const { return getOperand(9); }
1392 Metadata *getRawVariables() const { return getOperand(10); }
1394 /// \brief Get a pointer to the function this subprogram describes.
1396 /// This dyn_casts \a getFunctionConstant() to \a Function.
1398 /// FIXME: Should this be looking through bitcasts?
1399 Function *getFunction() const;
1401 /// \brief Replace the function.
1403 /// If \a isUniqued() and not \a isResolved(), this could node will be
1404 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1405 /// that's a problem.
1407 void replaceFunction(Function *F);
1408 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1409 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1412 /// \brief Check if this subprogram decribes the given function.
1414 /// FIXME: Should this be looking through bitcasts?
1415 bool describes(const Function *F) const;
1417 static bool classof(const Metadata *MD) {
1418 return MD->getMetadataID() == MDSubprogramKind;
1422 class MDLexicalBlockBase : public MDLocalScope {
1424 MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1425 ArrayRef<Metadata *> Ops)
1426 : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1427 ~MDLexicalBlockBase() = default;
1430 MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
1432 Metadata *getRawScope() const { return getOperand(1); }
1434 static bool classof(const Metadata *MD) {
1435 return MD->getMetadataID() == MDLexicalBlockKind ||
1436 MD->getMetadataID() == MDLexicalBlockFileKind;
1440 class MDLexicalBlock : public MDLexicalBlockBase {
1441 friend class LLVMContextImpl;
1442 friend class MDNode;
1447 MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1448 unsigned Column, ArrayRef<Metadata *> Ops)
1449 : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1451 ~MDLexicalBlock() = default;
1453 static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1454 MDFile *File, unsigned Line, unsigned Column,
1455 StorageType Storage,
1456 bool ShouldCreate = true) {
1457 return getImpl(Context, static_cast<Metadata *>(Scope),
1458 static_cast<Metadata *>(File), Line, Column, Storage,
1462 static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1463 Metadata *File, unsigned Line, unsigned Column,
1464 StorageType Storage, bool ShouldCreate = true);
1466 TempMDLexicalBlock cloneImpl() const {
1467 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1472 DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1473 unsigned Line, unsigned Column),
1474 (Scope, File, Line, Column))
1475 DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1476 unsigned Line, unsigned Column),
1477 (Scope, File, Line, Column))
1479 TempMDLexicalBlock clone() const { return cloneImpl(); }
1481 unsigned getLine() const { return Line; }
1482 unsigned getColumn() const { return Column; }
1484 static bool classof(const Metadata *MD) {
1485 return MD->getMetadataID() == MDLexicalBlockKind;
1489 class MDLexicalBlockFile : public MDLexicalBlockBase {
1490 friend class LLVMContextImpl;
1491 friend class MDNode;
1493 unsigned Discriminator;
1495 MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1496 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1497 : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1498 Discriminator(Discriminator) {}
1499 ~MDLexicalBlockFile() = default;
1501 static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1502 MDFile *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 MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1511 Metadata *File, unsigned Discriminator,
1512 StorageType Storage,
1513 bool ShouldCreate = true);
1515 TempMDLexicalBlockFile cloneImpl() const {
1516 return getTemporary(getContext(), getScope(), getFile(),
1517 getDiscriminator());
1521 DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1522 unsigned Discriminator),
1523 (Scope, File, Discriminator))
1524 DEFINE_MDNODE_GET(MDLexicalBlockFile,
1525 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1526 (Scope, File, Discriminator))
1528 TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1530 unsigned getDiscriminator() const { return Discriminator; }
1532 static bool classof(const Metadata *MD) {
1533 return MD->getMetadataID() == MDLexicalBlockFileKind;
1537 unsigned MDLocation::getDiscriminator() const {
1538 if (auto *F = dyn_cast<MDLexicalBlockFile>(getScope()))
1539 return F->getDiscriminator();
1543 class MDNamespace : public MDScope {
1544 friend class LLVMContextImpl;
1545 friend class MDNode;
1549 MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1550 ArrayRef<Metadata *> Ops)
1551 : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1554 ~MDNamespace() = default;
1556 static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1557 MDFile *File, StringRef Name, unsigned Line,
1558 StorageType Storage, bool ShouldCreate = true) {
1559 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1560 Line, Storage, ShouldCreate);
1562 static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1563 Metadata *File, MDString *Name, unsigned Line,
1564 StorageType Storage, bool ShouldCreate = true);
1566 TempMDNamespace cloneImpl() const {
1567 return getTemporary(getContext(), getScope(), getFile(), getName(),
1572 DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1574 (Scope, File, Name, Line))
1575 DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1576 MDString *Name, unsigned Line),
1577 (Scope, File, Name, Line))
1579 TempMDNamespace clone() const { return cloneImpl(); }
1581 unsigned getLine() const { return Line; }
1582 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1583 StringRef getName() const { return getStringOperand(2); }
1585 Metadata *getRawScope() const { return getOperand(1); }
1586 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1588 static bool classof(const Metadata *MD) {
1589 return MD->getMetadataID() == MDNamespaceKind;
1593 /// \brief Base class for template parameters.
1594 class MDTemplateParameter : public DebugNode {
1596 MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1597 unsigned Tag, ArrayRef<Metadata *> Ops)
1598 : DebugNode(Context, ID, Storage, Tag, Ops) {}
1599 ~MDTemplateParameter() = default;
1602 StringRef getName() const { return getStringOperand(0); }
1603 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1605 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1606 Metadata *getRawType() const { return getOperand(1); }
1608 static bool classof(const Metadata *MD) {
1609 return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1610 MD->getMetadataID() == MDTemplateValueParameterKind;
1614 class MDTemplateTypeParameter : public MDTemplateParameter {
1615 friend class LLVMContextImpl;
1616 friend class MDNode;
1618 MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1619 ArrayRef<Metadata *> Ops)
1620 : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1621 dwarf::DW_TAG_template_type_parameter, Ops) {}
1622 ~MDTemplateTypeParameter() = default;
1624 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1625 MDTypeRef Type, StorageType Storage,
1626 bool ShouldCreate = true) {
1627 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1630 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1631 Metadata *Type, StorageType Storage,
1632 bool ShouldCreate = true);
1634 TempMDTemplateTypeParameter cloneImpl() const {
1635 return getTemporary(getContext(), getName(), getType());
1639 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
1641 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1644 TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1646 static bool classof(const Metadata *MD) {
1647 return MD->getMetadataID() == MDTemplateTypeParameterKind;
1651 class MDTemplateValueParameter : public MDTemplateParameter {
1652 friend class LLVMContextImpl;
1653 friend class MDNode;
1655 MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1656 unsigned Tag, ArrayRef<Metadata *> Ops)
1657 : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1659 ~MDTemplateValueParameter() = default;
1661 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1662 StringRef Name, MDTypeRef Type,
1663 Metadata *Value, StorageType Storage,
1664 bool ShouldCreate = true) {
1665 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1666 Value, Storage, ShouldCreate);
1668 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1669 MDString *Name, Metadata *Type,
1670 Metadata *Value, StorageType Storage,
1671 bool ShouldCreate = true);
1673 TempMDTemplateValueParameter cloneImpl() const {
1674 return getTemporary(getContext(), getTag(), getName(), getType(),
1679 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1680 MDTypeRef Type, Metadata *Value),
1681 (Tag, Name, Type, Value))
1682 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1683 Metadata *Type, Metadata *Value),
1684 (Tag, Name, Type, Value))
1686 TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1688 Metadata *getValue() const { return getOperand(2); }
1690 static bool classof(const Metadata *MD) {
1691 return MD->getMetadataID() == MDTemplateValueParameterKind;
1695 /// \brief Base class for variables.
1697 /// TODO: Hardcode to DW_TAG_variable.
1698 class MDVariable : public DebugNode {
1702 MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1703 unsigned Line, ArrayRef<Metadata *> Ops)
1704 : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1705 ~MDVariable() = default;
1708 unsigned getLine() const { return Line; }
1709 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1710 StringRef getName() const { return getStringOperand(1); }
1711 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1712 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1714 StringRef getFilename() const {
1715 if (auto *F = getFile())
1716 return F->getFilename();
1719 StringRef getDirectory() const {
1720 if (auto *F = getFile())
1721 return F->getDirectory();
1725 Metadata *getRawScope() const { return getOperand(0); }
1726 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1727 Metadata *getRawFile() const { return getOperand(2); }
1728 Metadata *getRawType() const { return getOperand(3); }
1730 static bool classof(const Metadata *MD) {
1731 return MD->getMetadataID() == MDLocalVariableKind ||
1732 MD->getMetadataID() == MDGlobalVariableKind;
1736 /// \brief Global variables.
1738 /// TODO: Remove DisplayName. It's always equal to Name.
1739 class MDGlobalVariable : public MDVariable {
1740 friend class LLVMContextImpl;
1741 friend class MDNode;
1746 MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1747 bool IsLocalToUnit, bool IsDefinition,
1748 ArrayRef<Metadata *> Ops)
1749 : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1751 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1752 ~MDGlobalVariable() = default;
1754 static MDGlobalVariable *
1755 getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1756 StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
1757 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1758 MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1759 bool ShouldCreate = true) {
1760 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1761 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1762 IsLocalToUnit, IsDefinition,
1763 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1764 StaticDataMemberDeclaration, Storage, ShouldCreate);
1766 static MDGlobalVariable *
1767 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1768 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1769 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1770 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1771 bool ShouldCreate = true);
1773 TempMDGlobalVariable cloneImpl() const {
1774 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1775 getFile(), getLine(), getType(), isLocalToUnit(),
1776 isDefinition(), getVariable(),
1777 getStaticDataMemberDeclaration());
1781 DEFINE_MDNODE_GET(MDGlobalVariable,
1782 (MDScope * Scope, StringRef Name, StringRef LinkageName,
1783 MDFile *File, unsigned Line, MDTypeRef Type,
1784 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1785 MDDerivedType *StaticDataMemberDeclaration),
1786 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1787 IsDefinition, Variable, StaticDataMemberDeclaration))
1788 DEFINE_MDNODE_GET(MDGlobalVariable,
1789 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1790 Metadata *File, unsigned Line, Metadata *Type,
1791 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1792 Metadata *StaticDataMemberDeclaration),
1793 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1794 IsDefinition, Variable, StaticDataMemberDeclaration))
1796 TempMDGlobalVariable clone() const { return cloneImpl(); }
1798 bool isLocalToUnit() const { return IsLocalToUnit; }
1799 bool isDefinition() const { return IsDefinition; }
1800 StringRef getDisplayName() const { return getStringOperand(4); }
1801 StringRef getLinkageName() const { return getStringOperand(5); }
1802 Constant *getVariable() const {
1803 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1804 return dyn_cast<Constant>(C->getValue());
1807 MDDerivedType *getStaticDataMemberDeclaration() const {
1808 return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1811 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1812 Metadata *getRawVariable() const { return getOperand(6); }
1813 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1815 static bool classof(const Metadata *MD) {
1816 return MD->getMetadataID() == MDGlobalVariableKind;
1820 /// \brief Local variable.
1822 /// TODO: Split between arguments and otherwise.
1823 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1824 /// TODO: Split up flags.
1825 class MDLocalVariable : public MDVariable {
1826 friend class LLVMContextImpl;
1827 friend class MDNode;
1832 MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1833 unsigned Line, unsigned Arg, unsigned Flags,
1834 ArrayRef<Metadata *> Ops)
1835 : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1837 ~MDLocalVariable() = default;
1839 static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1840 MDScope *Scope, StringRef Name, MDFile *File,
1841 unsigned Line, MDTypeRef Type, unsigned Arg,
1842 unsigned Flags, MDLocation *InlinedAt,
1843 StorageType Storage,
1844 bool ShouldCreate = true) {
1845 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1846 File, Line, Type, Arg, Flags, InlinedAt, Storage,
1849 static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1850 Metadata *Scope, MDString *Name,
1851 Metadata *File, unsigned Line, Metadata *Type,
1852 unsigned Arg, unsigned Flags,
1853 Metadata *InlinedAt, StorageType Storage,
1854 bool ShouldCreate = true);
1856 TempMDLocalVariable cloneImpl() const {
1857 return getTemporary(getContext(), getTag(), getScope(), getName(),
1858 getFile(), getLine(), getType(), getArg(), getFlags(),
1863 DEFINE_MDNODE_GET(MDLocalVariable,
1864 (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1865 MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
1866 unsigned Flags, MDLocation *InlinedAt = nullptr),
1867 (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1868 DEFINE_MDNODE_GET(MDLocalVariable,
1869 (unsigned Tag, Metadata *Scope, MDString *Name,
1870 Metadata *File, unsigned Line, Metadata *Type,
1871 unsigned Arg, unsigned Flags,
1872 Metadata *InlinedAt = nullptr),
1873 (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1875 TempMDLocalVariable clone() const { return cloneImpl(); }
1877 /// \brief Get the local scope for this variable.
1879 /// Variables must be defined in a local scope.
1880 MDLocalScope *getScope() const {
1881 return cast<MDLocalScope>(MDVariable::getScope());
1884 unsigned getArg() const { return Arg; }
1885 unsigned getFlags() const { return Flags; }
1886 MDLocation *getInlinedAt() const {
1887 return cast_or_null<MDLocation>(getRawInlinedAt());
1890 Metadata *getRawInlinedAt() const { return getOperand(4); }
1892 bool isArtificial() const { return getFlags() & FlagArtificial; }
1893 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1895 /// \brief Check that a location is valid for this variable.
1897 /// Check that \c DL has the same inlined-at location as this variable,
1898 /// making them valid for the same \a DbgInfoIntrinsic.
1899 bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1900 return getInlinedAt() == (DL ? DL->getInlinedAt() : nullptr);
1903 /// \brief Get an inlined version of this variable.
1905 /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1906 MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1907 if (InlinedAt == getInlinedAt())
1908 return const_cast<MDLocalVariable *>(this);
1909 auto Temp = clone();
1910 Temp->replaceOperandWith(4, InlinedAt);
1911 return replaceWithUniqued(std::move(Temp));
1913 MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1915 static bool classof(const Metadata *MD) {
1916 return MD->getMetadataID() == MDLocalVariableKind;
1920 /// \brief DWARF expression.
1922 /// TODO: Co-allocate the expression elements.
1923 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1925 class MDExpression : public MDNode {
1926 friend class LLVMContextImpl;
1927 friend class MDNode;
1929 std::vector<uint64_t> Elements;
1931 MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1932 : MDNode(C, MDExpressionKind, Storage, None),
1933 Elements(Elements.begin(), Elements.end()) {}
1934 ~MDExpression() = default;
1936 static MDExpression *getImpl(LLVMContext &Context,
1937 ArrayRef<uint64_t> Elements, StorageType Storage,
1938 bool ShouldCreate = true);
1940 TempMDExpression cloneImpl() const {
1941 return getTemporary(getContext(), getElements());
1945 DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1947 TempMDExpression clone() const { return cloneImpl(); }
1949 ArrayRef<uint64_t> getElements() const { return Elements; }
1951 unsigned getNumElements() const { return Elements.size(); }
1952 uint64_t getElement(unsigned I) const {
1953 assert(I < Elements.size() && "Index out of range");
1957 /// \brief Return whether this is a piece of an aggregate variable.
1958 bool isBitPiece() const;
1960 /// \brief Return the offset of this piece in bits.
1961 uint64_t getBitPieceOffset() const;
1963 /// \brief Return the size of this piece in bits.
1964 uint64_t getBitPieceSize() const;
1966 typedef ArrayRef<uint64_t>::iterator element_iterator;
1967 element_iterator elements_begin() const { return getElements().begin(); }
1968 element_iterator elements_end() const { return getElements().end(); }
1970 /// \brief A lightweight wrapper around an expression operand.
1972 /// TODO: Store arguments directly and change \a MDExpression to store a
1978 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1980 const uint64_t *get() const { return Op; }
1982 /// \brief Get the operand code.
1983 uint64_t getOp() const { return *Op; }
1985 /// \brief Get an argument to the operand.
1987 /// Never returns the operand itself.
1988 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
1990 unsigned getNumArgs() const { return getSize() - 1; }
1992 /// \brief Return the size of the operand.
1994 /// Return the number of elements in the operand (1 + args).
1995 unsigned getSize() const;
1998 /// \brief An iterator for expression operands.
1999 class expr_op_iterator
2000 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2004 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2006 element_iterator getBase() const { return Op.get(); }
2007 const ExprOperand &operator*() const { return Op; }
2008 const ExprOperand *operator->() const { return &Op; }
2010 expr_op_iterator &operator++() {
2014 expr_op_iterator operator++(int) {
2015 expr_op_iterator T(*this);
2020 /// \brief Get the next iterator.
2022 /// \a std::next() doesn't work because this is technically an
2023 /// input_iterator, but it's a perfectly valid operation. This is an
2024 /// accessor to provide the same functionality.
2025 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2027 bool operator==(const expr_op_iterator &X) const {
2028 return getBase() == X.getBase();
2030 bool operator!=(const expr_op_iterator &X) const {
2031 return getBase() != X.getBase();
2035 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2038 /// \brief Visit the elements via ExprOperand wrappers.
2040 /// These range iterators visit elements through \a ExprOperand wrappers.
2041 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2044 /// \pre \a isValid() gives \c true.
2046 expr_op_iterator expr_op_begin() const {
2047 return expr_op_iterator(elements_begin());
2049 expr_op_iterator expr_op_end() const {
2050 return expr_op_iterator(elements_end());
2054 bool isValid() const;
2056 static bool classof(const Metadata *MD) {
2057 return MD->getMetadataID() == MDExpressionKind;
2061 class MDObjCProperty : public DebugNode {
2062 friend class LLVMContextImpl;
2063 friend class MDNode;
2066 unsigned Attributes;
2068 MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2069 unsigned Attributes, ArrayRef<Metadata *> Ops)
2070 : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2072 Line(Line), Attributes(Attributes) {}
2073 ~MDObjCProperty() = default;
2075 static MDObjCProperty *
2076 getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
2077 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2078 MDType *Type, StorageType Storage, bool ShouldCreate = true) {
2079 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2080 getCanonicalMDString(Context, GetterName),
2081 getCanonicalMDString(Context, SetterName), Attributes, Type,
2082 Storage, ShouldCreate);
2084 static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2085 Metadata *File, unsigned Line,
2086 MDString *GetterName, MDString *SetterName,
2087 unsigned Attributes, Metadata *Type,
2088 StorageType Storage, bool ShouldCreate = true);
2090 TempMDObjCProperty cloneImpl() const {
2091 return getTemporary(getContext(), getName(), getFile(), getLine(),
2092 getGetterName(), getSetterName(), getAttributes(),
2097 DEFINE_MDNODE_GET(MDObjCProperty,
2098 (StringRef Name, MDFile *File, unsigned Line,
2099 StringRef GetterName, StringRef SetterName,
2100 unsigned Attributes, MDType *Type),
2101 (Name, File, Line, GetterName, SetterName, Attributes,
2103 DEFINE_MDNODE_GET(MDObjCProperty,
2104 (MDString * Name, Metadata *File, unsigned Line,
2105 MDString *GetterName, MDString *SetterName,
2106 unsigned Attributes, Metadata *Type),
2107 (Name, File, Line, GetterName, SetterName, Attributes,
2110 TempMDObjCProperty clone() const { return cloneImpl(); }
2112 unsigned getLine() const { return Line; }
2113 unsigned getAttributes() const { return Attributes; }
2114 StringRef getName() const { return getStringOperand(0); }
2115 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
2116 StringRef getGetterName() const { return getStringOperand(2); }
2117 StringRef getSetterName() const { return getStringOperand(3); }
2118 MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
2120 StringRef getFilename() const {
2121 if (auto *F = getFile())
2122 return F->getFilename();
2125 StringRef getDirectory() const {
2126 if (auto *F = getFile())
2127 return F->getDirectory();
2131 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2132 Metadata *getRawFile() const { return getOperand(1); }
2133 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2134 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2135 Metadata *getRawType() const { return getOperand(4); }
2137 static bool classof(const Metadata *MD) {
2138 return MD->getMetadataID() == MDObjCPropertyKind;
2142 class MDImportedEntity : public DebugNode {
2143 friend class LLVMContextImpl;
2144 friend class MDNode;
2148 MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2149 unsigned Line, ArrayRef<Metadata *> Ops)
2150 : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2151 ~MDImportedEntity() = default;
2153 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2154 MDScope *Scope, DebugNodeRef Entity,
2155 unsigned Line, StringRef Name,
2156 StorageType Storage,
2157 bool ShouldCreate = true) {
2158 return getImpl(Context, Tag, Scope, Entity, Line,
2159 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2161 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2162 Metadata *Scope, Metadata *Entity,
2163 unsigned Line, MDString *Name,
2164 StorageType Storage,
2165 bool ShouldCreate = true);
2167 TempMDImportedEntity cloneImpl() const {
2168 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2169 getLine(), getName());
2173 DEFINE_MDNODE_GET(MDImportedEntity,
2174 (unsigned Tag, MDScope *Scope, DebugNodeRef Entity,
2175 unsigned Line, StringRef Name = ""),
2176 (Tag, Scope, Entity, Line, Name))
2177 DEFINE_MDNODE_GET(MDImportedEntity,
2178 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2179 unsigned Line, MDString *Name),
2180 (Tag, Scope, Entity, Line, Name))
2182 TempMDImportedEntity clone() const { return cloneImpl(); }
2184 unsigned getLine() const { return Line; }
2185 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
2186 DebugNodeRef getEntity() const { return DebugNodeRef(getRawEntity()); }
2187 StringRef getName() const { return getStringOperand(2); }
2189 Metadata *getRawScope() const { return getOperand(0); }
2190 Metadata *getRawEntity() const { return getOperand(1); }
2191 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2193 static bool classof(const Metadata *MD) {
2194 return MD->getMetadataID() == MDImportedEntityKind;
2198 } // end namespace llvm
2200 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2201 #undef DEFINE_MDNODE_GET_UNPACK
2202 #undef DEFINE_MDNODE_GET