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 static bool classof(const Metadata *MD) {
1450 return MD->getMetadataID() == MDLexicalBlockKind ||
1451 MD->getMetadataID() == MDLexicalBlockFileKind;
1455 class MDLexicalBlock : public MDLexicalBlockBase {
1456 friend class LLVMContextImpl;
1457 friend class MDNode;
1462 MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1463 unsigned Column, ArrayRef<Metadata *> Ops)
1464 : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
1466 ~MDLexicalBlock() = default;
1468 static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1469 MDFile *File, unsigned Line, unsigned Column,
1470 StorageType Storage,
1471 bool ShouldCreate = true) {
1472 return getImpl(Context, static_cast<Metadata *>(Scope),
1473 static_cast<Metadata *>(File), Line, Column, Storage,
1477 static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1478 Metadata *File, unsigned Line, unsigned Column,
1479 StorageType Storage, bool ShouldCreate = true);
1481 TempMDLexicalBlock cloneImpl() const {
1482 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1487 DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
1488 unsigned Line, unsigned Column),
1489 (Scope, File, Line, Column))
1490 DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
1491 unsigned Line, unsigned Column),
1492 (Scope, File, Line, Column))
1494 TempMDLexicalBlock clone() const { return cloneImpl(); }
1496 unsigned getLine() const { return Line; }
1497 unsigned getColumn() const { return Column; }
1499 static bool classof(const Metadata *MD) {
1500 return MD->getMetadataID() == MDLexicalBlockKind;
1504 class MDLexicalBlockFile : public MDLexicalBlockBase {
1505 friend class LLVMContextImpl;
1506 friend class MDNode;
1508 unsigned Discriminator;
1510 MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
1511 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1512 : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
1513 Discriminator(Discriminator) {}
1514 ~MDLexicalBlockFile() = default;
1516 static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
1517 MDFile *File, unsigned Discriminator,
1518 StorageType Storage,
1519 bool ShouldCreate = true) {
1520 return getImpl(Context, static_cast<Metadata *>(Scope),
1521 static_cast<Metadata *>(File), Discriminator, Storage,
1525 static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1526 Metadata *File, unsigned Discriminator,
1527 StorageType Storage,
1528 bool ShouldCreate = true);
1530 TempMDLexicalBlockFile cloneImpl() const {
1531 return getTemporary(getContext(), getScope(), getFile(),
1532 getDiscriminator());
1536 DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
1537 unsigned Discriminator),
1538 (Scope, File, Discriminator))
1539 DEFINE_MDNODE_GET(MDLexicalBlockFile,
1540 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1541 (Scope, File, Discriminator))
1543 TempMDLexicalBlockFile clone() const { return cloneImpl(); }
1545 unsigned getDiscriminator() const { return Discriminator; }
1547 static bool classof(const Metadata *MD) {
1548 return MD->getMetadataID() == MDLexicalBlockFileKind;
1552 unsigned MDLocation::getDiscriminator() const {
1553 if (auto *F = dyn_cast<MDLexicalBlockFile>(getScope()))
1554 return F->getDiscriminator();
1558 class MDNamespace : public MDScope {
1559 friend class LLVMContextImpl;
1560 friend class MDNode;
1564 MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1565 ArrayRef<Metadata *> Ops)
1566 : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
1569 ~MDNamespace() = default;
1571 static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
1572 MDFile *File, StringRef Name, unsigned Line,
1573 StorageType Storage, bool ShouldCreate = true) {
1574 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1575 Line, Storage, ShouldCreate);
1577 static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1578 Metadata *File, MDString *Name, unsigned Line,
1579 StorageType Storage, bool ShouldCreate = true);
1581 TempMDNamespace cloneImpl() const {
1582 return getTemporary(getContext(), getScope(), getFile(), getName(),
1587 DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
1589 (Scope, File, Name, Line))
1590 DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
1591 MDString *Name, unsigned Line),
1592 (Scope, File, Name, Line))
1594 TempMDNamespace clone() const { return cloneImpl(); }
1596 unsigned getLine() const { return Line; }
1597 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1598 StringRef getName() const { return getStringOperand(2); }
1600 Metadata *getRawScope() const { return getOperand(1); }
1601 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1603 static bool classof(const Metadata *MD) {
1604 return MD->getMetadataID() == MDNamespaceKind;
1608 /// \brief Base class for template parameters.
1609 class MDTemplateParameter : public DebugNode {
1611 MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1612 unsigned Tag, ArrayRef<Metadata *> Ops)
1613 : DebugNode(Context, ID, Storage, Tag, Ops) {}
1614 ~MDTemplateParameter() = default;
1617 StringRef getName() const { return getStringOperand(0); }
1618 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1620 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1621 Metadata *getRawType() const { return getOperand(1); }
1623 static bool classof(const Metadata *MD) {
1624 return MD->getMetadataID() == MDTemplateTypeParameterKind ||
1625 MD->getMetadataID() == MDTemplateValueParameterKind;
1629 class MDTemplateTypeParameter : public MDTemplateParameter {
1630 friend class LLVMContextImpl;
1631 friend class MDNode;
1633 MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1634 ArrayRef<Metadata *> Ops)
1635 : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
1636 dwarf::DW_TAG_template_type_parameter, Ops) {}
1637 ~MDTemplateTypeParameter() = default;
1639 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1640 MDTypeRef Type, StorageType Storage,
1641 bool ShouldCreate = true) {
1642 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1645 static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1646 Metadata *Type, StorageType Storage,
1647 bool ShouldCreate = true);
1649 TempMDTemplateTypeParameter cloneImpl() const {
1650 return getTemporary(getContext(), getName(), getType());
1654 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
1656 DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
1659 TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
1661 static bool classof(const Metadata *MD) {
1662 return MD->getMetadataID() == MDTemplateTypeParameterKind;
1666 class MDTemplateValueParameter : public MDTemplateParameter {
1667 friend class LLVMContextImpl;
1668 friend class MDNode;
1670 MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
1671 unsigned Tag, ArrayRef<Metadata *> Ops)
1672 : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
1674 ~MDTemplateValueParameter() = default;
1676 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1677 StringRef Name, MDTypeRef Type,
1678 Metadata *Value, StorageType Storage,
1679 bool ShouldCreate = true) {
1680 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1681 Value, Storage, ShouldCreate);
1683 static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1684 MDString *Name, Metadata *Type,
1685 Metadata *Value, StorageType Storage,
1686 bool ShouldCreate = true);
1688 TempMDTemplateValueParameter cloneImpl() const {
1689 return getTemporary(getContext(), getTag(), getName(), getType(),
1694 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
1695 MDTypeRef Type, Metadata *Value),
1696 (Tag, Name, Type, Value))
1697 DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
1698 Metadata *Type, Metadata *Value),
1699 (Tag, Name, Type, Value))
1701 TempMDTemplateValueParameter clone() const { return cloneImpl(); }
1703 Metadata *getValue() const { return getOperand(2); }
1705 static bool classof(const Metadata *MD) {
1706 return MD->getMetadataID() == MDTemplateValueParameterKind;
1710 /// \brief Base class for variables.
1712 /// TODO: Hardcode to DW_TAG_variable.
1713 class MDVariable : public DebugNode {
1717 MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1718 unsigned Line, ArrayRef<Metadata *> Ops)
1719 : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
1720 ~MDVariable() = default;
1723 unsigned getLine() const { return Line; }
1724 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
1725 StringRef getName() const { return getStringOperand(1); }
1726 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
1727 MDTypeRef getType() const { return MDTypeRef(getRawType()); }
1729 StringRef getFilename() const {
1730 if (auto *F = getFile())
1731 return F->getFilename();
1734 StringRef getDirectory() const {
1735 if (auto *F = getFile())
1736 return F->getDirectory();
1740 Metadata *getRawScope() const { return getOperand(0); }
1741 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1742 Metadata *getRawFile() const { return getOperand(2); }
1743 Metadata *getRawType() const { return getOperand(3); }
1745 static bool classof(const Metadata *MD) {
1746 return MD->getMetadataID() == MDLocalVariableKind ||
1747 MD->getMetadataID() == MDGlobalVariableKind;
1751 /// \brief Global variables.
1753 /// TODO: Remove DisplayName. It's always equal to Name.
1754 class MDGlobalVariable : public MDVariable {
1755 friend class LLVMContextImpl;
1756 friend class MDNode;
1761 MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1762 bool IsLocalToUnit, bool IsDefinition,
1763 ArrayRef<Metadata *> Ops)
1764 : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1766 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1767 ~MDGlobalVariable() = default;
1769 static MDGlobalVariable *
1770 getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
1771 StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
1772 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1773 MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1774 bool ShouldCreate = true) {
1775 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1776 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1777 IsLocalToUnit, IsDefinition,
1778 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1779 StaticDataMemberDeclaration, Storage, ShouldCreate);
1781 static MDGlobalVariable *
1782 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1783 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1784 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1785 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1786 bool ShouldCreate = true);
1788 TempMDGlobalVariable cloneImpl() const {
1789 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1790 getFile(), getLine(), getType(), isLocalToUnit(),
1791 isDefinition(), getVariable(),
1792 getStaticDataMemberDeclaration());
1796 DEFINE_MDNODE_GET(MDGlobalVariable,
1797 (MDScope * Scope, StringRef Name, StringRef LinkageName,
1798 MDFile *File, unsigned Line, MDTypeRef Type,
1799 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1800 MDDerivedType *StaticDataMemberDeclaration),
1801 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1802 IsDefinition, Variable, StaticDataMemberDeclaration))
1803 DEFINE_MDNODE_GET(MDGlobalVariable,
1804 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1805 Metadata *File, unsigned Line, Metadata *Type,
1806 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1807 Metadata *StaticDataMemberDeclaration),
1808 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1809 IsDefinition, Variable, StaticDataMemberDeclaration))
1811 TempMDGlobalVariable clone() const { return cloneImpl(); }
1813 bool isLocalToUnit() const { return IsLocalToUnit; }
1814 bool isDefinition() const { return IsDefinition; }
1815 StringRef getDisplayName() const { return getStringOperand(4); }
1816 StringRef getLinkageName() const { return getStringOperand(5); }
1817 Constant *getVariable() const {
1818 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1819 return dyn_cast<Constant>(C->getValue());
1822 MDDerivedType *getStaticDataMemberDeclaration() const {
1823 return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
1826 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1827 Metadata *getRawVariable() const { return getOperand(6); }
1828 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1830 static bool classof(const Metadata *MD) {
1831 return MD->getMetadataID() == MDGlobalVariableKind;
1835 /// \brief Local variable.
1837 /// TODO: Split between arguments and otherwise.
1838 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1839 /// TODO: Split up flags.
1840 class MDLocalVariable : public MDVariable {
1841 friend class LLVMContextImpl;
1842 friend class MDNode;
1847 MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1848 unsigned Line, unsigned Arg, unsigned Flags,
1849 ArrayRef<Metadata *> Ops)
1850 : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1852 ~MDLocalVariable() = default;
1854 static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1855 MDScope *Scope, StringRef Name, MDFile *File,
1856 unsigned Line, MDTypeRef Type, unsigned Arg,
1857 unsigned Flags, MDLocation *InlinedAt,
1858 StorageType Storage,
1859 bool ShouldCreate = true) {
1860 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1861 File, Line, Type, Arg, Flags, InlinedAt, Storage,
1864 static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1865 Metadata *Scope, MDString *Name,
1866 Metadata *File, unsigned Line, Metadata *Type,
1867 unsigned Arg, unsigned Flags,
1868 Metadata *InlinedAt, StorageType Storage,
1869 bool ShouldCreate = true);
1871 TempMDLocalVariable cloneImpl() const {
1872 return getTemporary(getContext(), getTag(), getScope(), getName(),
1873 getFile(), getLine(), getType(), getArg(), getFlags(),
1878 DEFINE_MDNODE_GET(MDLocalVariable,
1879 (unsigned Tag, MDLocalScope *Scope, StringRef Name,
1880 MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
1881 unsigned Flags, MDLocation *InlinedAt = nullptr),
1882 (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1883 DEFINE_MDNODE_GET(MDLocalVariable,
1884 (unsigned Tag, Metadata *Scope, MDString *Name,
1885 Metadata *File, unsigned Line, Metadata *Type,
1886 unsigned Arg, unsigned Flags,
1887 Metadata *InlinedAt = nullptr),
1888 (Tag, Scope, Name, File, Line, Type, Arg, Flags, InlinedAt))
1890 TempMDLocalVariable clone() const { return cloneImpl(); }
1892 /// \brief Get the local scope for this variable.
1894 /// Variables must be defined in a local scope.
1895 MDLocalScope *getScope() const {
1896 return cast<MDLocalScope>(MDVariable::getScope());
1899 unsigned getArg() const { return Arg; }
1900 unsigned getFlags() const { return Flags; }
1901 MDLocation *getInlinedAt() const {
1902 return cast_or_null<MDLocation>(getRawInlinedAt());
1905 Metadata *getRawInlinedAt() const { return getOperand(4); }
1907 bool isArtificial() const { return getFlags() & FlagArtificial; }
1908 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1910 /// \brief Check that a location is valid for this variable.
1912 /// Check that \c DL has the same inlined-at location as this variable,
1913 /// making them valid for the same \a DbgInfoIntrinsic.
1914 bool isValidLocationForIntrinsic(const MDLocation *DL) const {
1915 return getInlinedAt() == (DL ? DL->getInlinedAt() : nullptr);
1918 /// \brief Get an inlined version of this variable.
1920 /// Returns a version of this with \a getAlinedAt() set to \c InlinedAt.
1921 MDLocalVariable *withInline(MDLocation *InlinedAt) const {
1922 if (InlinedAt == getInlinedAt())
1923 return const_cast<MDLocalVariable *>(this);
1924 auto Temp = clone();
1925 Temp->replaceOperandWith(4, InlinedAt);
1926 return replaceWithUniqued(std::move(Temp));
1928 MDLocalVariable *withoutInline() const { return withInline(nullptr); }
1930 static bool classof(const Metadata *MD) {
1931 return MD->getMetadataID() == MDLocalVariableKind;
1935 /// \brief DWARF expression.
1937 /// This is (almost) a DWARF expression that modifies the location of a
1938 /// variable or (or the location of a single piece of a variable).
1940 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1941 /// and have DW_OP_plus consume the topmost elements on the stack.
1943 /// TODO: Co-allocate the expression elements.
1944 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1946 class MDExpression : public MDNode {
1947 friend class LLVMContextImpl;
1948 friend class MDNode;
1950 std::vector<uint64_t> Elements;
1952 MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1953 : MDNode(C, MDExpressionKind, Storage, None),
1954 Elements(Elements.begin(), Elements.end()) {}
1955 ~MDExpression() = default;
1957 static MDExpression *getImpl(LLVMContext &Context,
1958 ArrayRef<uint64_t> Elements, StorageType Storage,
1959 bool ShouldCreate = true);
1961 TempMDExpression cloneImpl() const {
1962 return getTemporary(getContext(), getElements());
1966 DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
1968 TempMDExpression clone() const { return cloneImpl(); }
1970 ArrayRef<uint64_t> getElements() const { return Elements; }
1972 unsigned getNumElements() const { return Elements.size(); }
1973 uint64_t getElement(unsigned I) const {
1974 assert(I < Elements.size() && "Index out of range");
1978 /// \brief Return whether this is a piece of an aggregate variable.
1979 bool isBitPiece() const;
1981 /// \brief Return the offset of this piece in bits.
1982 uint64_t getBitPieceOffset() const;
1984 /// \brief Return the size of this piece in bits.
1985 uint64_t getBitPieceSize() const;
1987 typedef ArrayRef<uint64_t>::iterator element_iterator;
1988 element_iterator elements_begin() const { return getElements().begin(); }
1989 element_iterator elements_end() const { return getElements().end(); }
1991 /// \brief A lightweight wrapper around an expression operand.
1993 /// TODO: Store arguments directly and change \a MDExpression to store a
1999 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2001 const uint64_t *get() const { return Op; }
2003 /// \brief Get the operand code.
2004 uint64_t getOp() const { return *Op; }
2006 /// \brief Get an argument to the operand.
2008 /// Never returns the operand itself.
2009 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2011 unsigned getNumArgs() const { return getSize() - 1; }
2013 /// \brief Return the size of the operand.
2015 /// Return the number of elements in the operand (1 + args).
2016 unsigned getSize() const;
2019 /// \brief An iterator for expression operands.
2020 class expr_op_iterator
2021 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2025 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2027 element_iterator getBase() const { return Op.get(); }
2028 const ExprOperand &operator*() const { return Op; }
2029 const ExprOperand *operator->() const { return &Op; }
2031 expr_op_iterator &operator++() {
2035 expr_op_iterator operator++(int) {
2036 expr_op_iterator T(*this);
2041 /// \brief Get the next iterator.
2043 /// \a std::next() doesn't work because this is technically an
2044 /// input_iterator, but it's a perfectly valid operation. This is an
2045 /// accessor to provide the same functionality.
2046 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2048 bool operator==(const expr_op_iterator &X) const {
2049 return getBase() == X.getBase();
2051 bool operator!=(const expr_op_iterator &X) const {
2052 return getBase() != X.getBase();
2056 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2059 /// \brief Visit the elements via ExprOperand wrappers.
2061 /// These range iterators visit elements through \a ExprOperand wrappers.
2062 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2065 /// \pre \a isValid() gives \c true.
2067 expr_op_iterator expr_op_begin() const {
2068 return expr_op_iterator(elements_begin());
2070 expr_op_iterator expr_op_end() const {
2071 return expr_op_iterator(elements_end());
2075 bool isValid() const;
2077 static bool classof(const Metadata *MD) {
2078 return MD->getMetadataID() == MDExpressionKind;
2082 class MDObjCProperty : public DebugNode {
2083 friend class LLVMContextImpl;
2084 friend class MDNode;
2087 unsigned Attributes;
2089 MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2090 unsigned Attributes, ArrayRef<Metadata *> Ops)
2091 : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2093 Line(Line), Attributes(Attributes) {}
2094 ~MDObjCProperty() = default;
2096 static MDObjCProperty *
2097 getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
2098 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2099 MDType *Type, StorageType Storage, bool ShouldCreate = true) {
2100 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2101 getCanonicalMDString(Context, GetterName),
2102 getCanonicalMDString(Context, SetterName), Attributes, Type,
2103 Storage, ShouldCreate);
2105 static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2106 Metadata *File, unsigned Line,
2107 MDString *GetterName, MDString *SetterName,
2108 unsigned Attributes, Metadata *Type,
2109 StorageType Storage, bool ShouldCreate = true);
2111 TempMDObjCProperty cloneImpl() const {
2112 return getTemporary(getContext(), getName(), getFile(), getLine(),
2113 getGetterName(), getSetterName(), getAttributes(),
2118 DEFINE_MDNODE_GET(MDObjCProperty,
2119 (StringRef Name, MDFile *File, unsigned Line,
2120 StringRef GetterName, StringRef SetterName,
2121 unsigned Attributes, MDType *Type),
2122 (Name, File, Line, GetterName, SetterName, Attributes,
2124 DEFINE_MDNODE_GET(MDObjCProperty,
2125 (MDString * Name, Metadata *File, unsigned Line,
2126 MDString *GetterName, MDString *SetterName,
2127 unsigned Attributes, Metadata *Type),
2128 (Name, File, Line, GetterName, SetterName, Attributes,
2131 TempMDObjCProperty clone() const { return cloneImpl(); }
2133 unsigned getLine() const { return Line; }
2134 unsigned getAttributes() const { return Attributes; }
2135 StringRef getName() const { return getStringOperand(0); }
2136 MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
2137 StringRef getGetterName() const { return getStringOperand(2); }
2138 StringRef getSetterName() const { return getStringOperand(3); }
2139 MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
2141 StringRef getFilename() const {
2142 if (auto *F = getFile())
2143 return F->getFilename();
2146 StringRef getDirectory() const {
2147 if (auto *F = getFile())
2148 return F->getDirectory();
2152 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2153 Metadata *getRawFile() const { return getOperand(1); }
2154 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2155 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2156 Metadata *getRawType() const { return getOperand(4); }
2158 static bool classof(const Metadata *MD) {
2159 return MD->getMetadataID() == MDObjCPropertyKind;
2163 class MDImportedEntity : public DebugNode {
2164 friend class LLVMContextImpl;
2165 friend class MDNode;
2169 MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2170 unsigned Line, ArrayRef<Metadata *> Ops)
2171 : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2172 ~MDImportedEntity() = default;
2174 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2175 MDScope *Scope, DebugNodeRef Entity,
2176 unsigned Line, StringRef Name,
2177 StorageType Storage,
2178 bool ShouldCreate = true) {
2179 return getImpl(Context, Tag, Scope, Entity, Line,
2180 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2182 static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2183 Metadata *Scope, Metadata *Entity,
2184 unsigned Line, MDString *Name,
2185 StorageType Storage,
2186 bool ShouldCreate = true);
2188 TempMDImportedEntity cloneImpl() const {
2189 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2190 getLine(), getName());
2194 DEFINE_MDNODE_GET(MDImportedEntity,
2195 (unsigned Tag, MDScope *Scope, DebugNodeRef Entity,
2196 unsigned Line, StringRef Name = ""),
2197 (Tag, Scope, Entity, Line, Name))
2198 DEFINE_MDNODE_GET(MDImportedEntity,
2199 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2200 unsigned Line, MDString *Name),
2201 (Tag, Scope, Entity, Line, Name))
2203 TempMDImportedEntity clone() const { return cloneImpl(); }
2205 unsigned getLine() const { return Line; }
2206 MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
2207 DebugNodeRef getEntity() const { return DebugNodeRef(getRawEntity()); }
2208 StringRef getName() const { return getStringOperand(2); }
2210 Metadata *getRawScope() const { return getOperand(0); }
2211 Metadata *getRawEntity() const { return getOperand(1); }
2212 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2214 static bool classof(const Metadata *MD) {
2215 return MD->getMetadataID() == MDImportedEntityKind;
2219 } // end namespace llvm
2221 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2222 #undef DEFINE_MDNODE_GET_UNPACK
2223 #undef DEFINE_MDNODE_GET