1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Declarations for metadata specific to debug info.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
15 #define LLVM_IR_DEBUGINFOMETADATA_H
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Dwarf.h"
20 // Helper macros for defining get() overrides.
21 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
22 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
23 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
24 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
25 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
27 static CLASS *getIfExists(LLVMContext &Context, \
28 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
29 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
30 /* ShouldCreate */ false); \
32 static CLASS *getDistinct(LLVMContext &Context, \
33 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
34 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
36 static Temp##CLASS getTemporary(LLVMContext &Context, \
37 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
39 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
44 /// \brief Pointer union between a subclass of DINode and MDString.
46 /// \a DICompositeType can be referenced via an \a MDString unique identifier.
47 /// This class allows some type safety in the face of that, requiring either a
48 /// node of a particular type or an \a MDString.
49 template <class T> class TypedDINodeRef {
50 const Metadata *MD = nullptr;
53 TypedDINodeRef() = default;
54 TypedDINodeRef(std::nullptr_t) {}
56 /// \brief Construct from a raw pointer.
57 explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
58 assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
63 const TypedDINodeRef<U> &X,
64 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
68 operator Metadata *() const { return const_cast<Metadata *>(MD); }
70 bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; };
71 bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; };
73 /// \brief Create a reference.
75 /// Get a reference to \c N, using an \a MDString reference if available.
76 static TypedDINodeRef get(const T *N);
78 template <class MapTy> T *resolve(const MapTy &Map) const {
82 if (auto *Typed = dyn_cast<T>(MD))
83 return const_cast<T *>(Typed);
85 auto *S = cast<MDString>(MD);
87 assert(I != Map.end() && "Missing identifier in type map");
88 return cast<T>(I->second);
92 typedef TypedDINodeRef<DINode> DINodeRef;
93 typedef TypedDINodeRef<DIScope> DIScopeRef;
94 typedef TypedDINodeRef<DIType> DITypeRef;
96 class DITypeRefArray {
97 const MDTuple *N = nullptr;
100 DITypeRefArray(const MDTuple *N) : N(N) {}
102 explicit operator bool() const { return get(); }
103 explicit operator MDTuple *() const { return get(); }
105 MDTuple *get() const { return const_cast<MDTuple *>(N); }
106 MDTuple *operator->() const { return get(); }
107 MDTuple &operator*() const { return *get(); }
109 // FIXME: Fix callers and remove condition on N.
110 unsigned size() const { return N ? N->getNumOperands() : 0u; }
111 DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
113 class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
114 std::ptrdiff_t, void, DITypeRef> {
115 MDNode::op_iterator I = nullptr;
118 iterator() = default;
119 explicit iterator(MDNode::op_iterator I) : I(I) {}
120 DITypeRef operator*() const { return DITypeRef(*I); }
121 iterator &operator++() {
125 iterator operator++(int) {
126 iterator Temp(*this);
130 bool operator==(const iterator &X) const { return I == X.I; }
131 bool operator!=(const iterator &X) const { return I != X.I; }
134 // FIXME: Fix callers and remove condition on N.
135 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
136 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
139 /// \brief Tagged DWARF-like metadata node.
141 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
142 /// defined in llvm/Support/Dwarf.h). Called \a DINode because it's
143 /// potentially used for non-DWARF output.
144 class DINode : public MDNode {
145 friend class LLVMContextImpl;
149 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
150 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
151 : MDNode(C, ID, Storage, Ops1, Ops2) {
152 assert(Tag < 1u << 16);
153 SubclassData16 = Tag;
157 template <class Ty> Ty *getOperandAs(unsigned I) const {
158 return cast_or_null<Ty>(getOperand(I));
161 StringRef getStringOperand(unsigned I) const {
162 if (auto *S = getOperandAs<MDString>(I))
163 return S->getString();
167 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
170 return MDString::get(Context, S);
174 unsigned getTag() const { return SubclassData16; }
176 /// \brief Debug info flags.
178 /// The three accessibility flags are mutually exclusive and rolled together
179 /// in the first two bits.
181 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
182 #include "llvm/IR/DebugInfoFlags.def"
183 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
186 static unsigned getFlag(StringRef Flag);
187 static const char *getFlagString(unsigned Flag);
189 /// \brief Split up a flags bitfield.
191 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
192 /// any remaining (unrecognized) bits.
193 static unsigned splitFlags(unsigned Flags,
194 SmallVectorImpl<unsigned> &SplitFlags);
196 DINodeRef getRef() const { return DINodeRef::get(this); }
198 static bool classof(const Metadata *MD) {
199 switch (MD->getMetadataID()) {
202 case GenericDINodeKind:
204 case DIEnumeratorKind:
205 case DIBasicTypeKind:
206 case DIDerivedTypeKind:
207 case DICompositeTypeKind:
208 case DISubroutineTypeKind:
210 case DICompileUnitKind:
211 case DISubprogramKind:
212 case DILexicalBlockKind:
213 case DILexicalBlockFileKind:
214 case DINamespaceKind:
215 case DITemplateTypeParameterKind:
216 case DITemplateValueParameterKind:
217 case DIGlobalVariableKind:
218 case DILocalVariableKind:
219 case DIObjCPropertyKind:
220 case DIImportedEntityKind:
226 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
227 typedef Metadata *SimpleType;
228 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
234 struct simplify_type<TypedDINodeRef<T>>
235 : simplify_type<const TypedDINodeRef<T>> {};
237 /// \brief Generic tagged DWARF-like metadata node.
239 /// An un-specialized DWARF-like metadata node. The first operand is a
240 /// (possibly empty) null-separated \a MDString header that contains arbitrary
241 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
242 /// to other metadata.
243 class GenericDINode : public DINode {
244 friend class LLVMContextImpl;
247 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
248 unsigned Tag, ArrayRef<Metadata *> Ops1,
249 ArrayRef<Metadata *> Ops2)
250 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
253 ~GenericDINode() { dropAllReferences(); }
255 void setHash(unsigned Hash) { SubclassData32 = Hash; }
256 void recalculateHash();
258 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
259 StringRef Header, ArrayRef<Metadata *> DwarfOps,
260 StorageType Storage, bool ShouldCreate = true) {
261 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
262 DwarfOps, Storage, ShouldCreate);
265 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
266 MDString *Header, ArrayRef<Metadata *> DwarfOps,
267 StorageType Storage, bool ShouldCreate = true);
269 TempGenericDINode cloneImpl() const {
271 getContext(), getTag(), getHeader(),
272 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
276 unsigned getHash() const { return SubclassData32; }
278 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
279 ArrayRef<Metadata *> DwarfOps),
280 (Tag, Header, DwarfOps))
281 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
282 ArrayRef<Metadata *> DwarfOps),
283 (Tag, Header, DwarfOps))
285 /// \brief Return a (temporary) clone of this.
286 TempGenericDINode clone() const { return cloneImpl(); }
288 unsigned getTag() const { return SubclassData16; }
289 StringRef getHeader() const { return getStringOperand(0); }
291 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
292 op_iterator dwarf_op_end() const { return op_end(); }
293 op_range dwarf_operands() const {
294 return op_range(dwarf_op_begin(), dwarf_op_end());
297 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
298 const MDOperand &getDwarfOperand(unsigned I) const {
299 return getOperand(I + 1);
301 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
302 replaceOperandWith(I + 1, New);
305 static bool classof(const Metadata *MD) {
306 return MD->getMetadataID() == GenericDINodeKind;
310 /// \brief Array subrange.
312 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
314 class DISubrange : public DINode {
315 friend class LLVMContextImpl;
321 DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
323 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
324 Count(Count), LowerBound(LowerBound) {}
325 ~DISubrange() = default;
327 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
328 int64_t LowerBound, StorageType Storage,
329 bool ShouldCreate = true);
331 TempDISubrange cloneImpl() const {
332 return getTemporary(getContext(), getCount(), getLowerBound());
336 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
339 TempDISubrange clone() const { return cloneImpl(); }
341 int64_t getLowerBound() const { return LowerBound; }
342 int64_t getCount() const { return Count; }
344 static bool classof(const Metadata *MD) {
345 return MD->getMetadataID() == DISubrangeKind;
349 /// \brief Enumeration value.
351 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
352 /// longer creates a type cycle.
353 class DIEnumerator : public DINode {
354 friend class LLVMContextImpl;
359 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
360 ArrayRef<Metadata *> Ops)
361 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
363 ~DIEnumerator() = default;
365 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
366 StringRef Name, StorageType Storage,
367 bool ShouldCreate = true) {
368 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
371 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
372 MDString *Name, StorageType Storage,
373 bool ShouldCreate = true);
375 TempDIEnumerator cloneImpl() const {
376 return getTemporary(getContext(), getValue(), getName());
380 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
382 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
385 TempDIEnumerator clone() const { return cloneImpl(); }
387 int64_t getValue() const { return Value; }
388 StringRef getName() const { return getStringOperand(0); }
390 MDString *getRawName() const { return getOperandAs<MDString>(0); }
392 static bool classof(const Metadata *MD) {
393 return MD->getMetadataID() == DIEnumeratorKind;
397 /// \brief Base class for scope-like contexts.
399 /// Base class for lexical scopes and types (which are also declaration
402 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
403 class DIScope : public DINode {
405 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
406 ArrayRef<Metadata *> Ops)
407 : DINode(C, ID, Storage, Tag, Ops) {}
408 ~DIScope() = default;
411 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
413 inline StringRef getFilename() const;
414 inline StringRef getDirectory() const;
416 StringRef getName() const;
417 DIScopeRef getScope() const;
419 /// \brief Return the raw underlying file.
421 /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
422 /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
423 /// this. Otherwise, return the first operand, which is where all other
424 /// subclasses store their file pointer.
425 Metadata *getRawFile() const {
426 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
427 : static_cast<Metadata *>(getOperand(0));
430 DIScopeRef getRef() const { return DIScopeRef::get(this); }
432 static bool classof(const Metadata *MD) {
433 switch (MD->getMetadataID()) {
436 case DIBasicTypeKind:
437 case DIDerivedTypeKind:
438 case DICompositeTypeKind:
439 case DISubroutineTypeKind:
441 case DICompileUnitKind:
442 case DISubprogramKind:
443 case DILexicalBlockKind:
444 case DILexicalBlockFileKind:
445 case DINamespaceKind:
453 /// TODO: Merge with directory/file node (including users).
454 /// TODO: Canonicalize paths on creation.
455 class DIFile : public DIScope {
456 friend class LLVMContextImpl;
459 DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
460 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
463 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
464 StringRef Directory, StorageType Storage,
465 bool ShouldCreate = true) {
466 return getImpl(Context, getCanonicalMDString(Context, Filename),
467 getCanonicalMDString(Context, Directory), Storage,
470 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
471 MDString *Directory, StorageType Storage,
472 bool ShouldCreate = true);
474 TempDIFile cloneImpl() const {
475 return getTemporary(getContext(), getFilename(), getDirectory());
479 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
480 (Filename, Directory))
481 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
482 (Filename, Directory))
484 TempDIFile clone() const { return cloneImpl(); }
486 StringRef getFilename() const { return getStringOperand(0); }
487 StringRef getDirectory() const { return getStringOperand(1); }
489 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
490 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
492 static bool classof(const Metadata *MD) {
493 return MD->getMetadataID() == DIFileKind;
497 StringRef DIScope::getFilename() const {
498 if (auto *F = getFile())
499 return F->getFilename();
503 StringRef DIScope::getDirectory() const {
504 if (auto *F = getFile())
505 return F->getDirectory();
509 /// \brief Base class for types.
511 /// TODO: Remove the hardcoded name and context, since many types don't use
513 /// TODO: Split up flags.
514 class DIType : public DIScope {
518 uint64_t AlignInBits;
519 uint64_t OffsetInBits;
522 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
523 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
524 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
525 : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
526 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
527 OffsetInBits(OffsetInBits) {}
531 TempDIType clone() const {
532 return TempDIType(cast<DIType>(MDNode::clone().release()));
535 unsigned getLine() const { return Line; }
536 uint64_t getSizeInBits() const { return SizeInBits; }
537 uint64_t getAlignInBits() const { return AlignInBits; }
538 uint64_t getOffsetInBits() const { return OffsetInBits; }
539 unsigned getFlags() const { return Flags; }
541 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
542 StringRef getName() const { return getStringOperand(2); }
545 Metadata *getRawScope() const { return getOperand(1); }
546 MDString *getRawName() const { return getOperandAs<MDString>(2); }
548 void setFlags(unsigned NewFlags) {
549 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
553 bool isPrivate() const {
554 return (getFlags() & FlagAccessibility) == FlagPrivate;
556 bool isProtected() const {
557 return (getFlags() & FlagAccessibility) == FlagProtected;
559 bool isPublic() const {
560 return (getFlags() & FlagAccessibility) == FlagPublic;
562 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
563 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
564 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
565 bool isVirtual() const { return getFlags() & FlagVirtual; }
566 bool isArtificial() const { return getFlags() & FlagArtificial; }
567 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
568 bool isObjcClassComplete() const {
569 return getFlags() & FlagObjcClassComplete;
571 bool isVector() const { return getFlags() & FlagVector; }
572 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
573 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
574 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
576 DITypeRef getRef() const { return DITypeRef::get(this); }
578 static bool classof(const Metadata *MD) {
579 switch (MD->getMetadataID()) {
582 case DIBasicTypeKind:
583 case DIDerivedTypeKind:
584 case DICompositeTypeKind:
585 case DISubroutineTypeKind:
591 /// \brief Basic type, like 'int' or 'float'.
593 /// TODO: Split out DW_TAG_unspecified_type.
594 /// TODO: Drop unused accessors.
595 class DIBasicType : public DIType {
596 friend class LLVMContextImpl;
601 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
602 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
603 ArrayRef<Metadata *> Ops)
604 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
606 Encoding(Encoding) {}
607 ~DIBasicType() = default;
609 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
610 StringRef Name, uint64_t SizeInBits,
611 uint64_t AlignInBits, unsigned Encoding,
612 StorageType Storage, bool ShouldCreate = true) {
613 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
614 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
616 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
617 MDString *Name, uint64_t SizeInBits,
618 uint64_t AlignInBits, unsigned Encoding,
619 StorageType Storage, bool ShouldCreate = true);
621 TempDIBasicType cloneImpl() const {
622 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
623 getAlignInBits(), getEncoding());
627 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
628 (Tag, Name, 0, 0, 0))
629 DEFINE_MDNODE_GET(DIBasicType,
630 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
631 uint64_t AlignInBits, unsigned Encoding),
632 (Tag, Name, SizeInBits, AlignInBits, Encoding))
633 DEFINE_MDNODE_GET(DIBasicType,
634 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
635 uint64_t AlignInBits, unsigned Encoding),
636 (Tag, Name, SizeInBits, AlignInBits, Encoding))
638 TempDIBasicType clone() const { return cloneImpl(); }
640 unsigned getEncoding() const { return Encoding; }
642 static bool classof(const Metadata *MD) {
643 return MD->getMetadataID() == DIBasicTypeKind;
647 /// \brief Base class for DIDerivedType and DICompositeType.
649 /// TODO: Delete; they're not really related.
650 class DIDerivedTypeBase : public DIType {
652 DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
653 unsigned Tag, unsigned Line, uint64_t SizeInBits,
654 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
655 ArrayRef<Metadata *> Ops)
656 : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
658 ~DIDerivedTypeBase() = default;
661 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
662 Metadata *getRawBaseType() const { return getOperand(3); }
664 static bool classof(const Metadata *MD) {
665 return MD->getMetadataID() == DIDerivedTypeKind ||
666 MD->getMetadataID() == DICompositeTypeKind ||
667 MD->getMetadataID() == DISubroutineTypeKind;
671 /// \brief Derived types.
673 /// This includes qualified types, pointers, references, friends, typedefs, and
676 /// TODO: Split out members (inheritance, fields, methods, etc.).
677 class DIDerivedType : public DIDerivedTypeBase {
678 friend class LLVMContextImpl;
681 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
682 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
683 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
684 : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
685 AlignInBits, OffsetInBits, Flags, Ops) {}
686 ~DIDerivedType() = default;
688 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
689 StringRef Name, DIFile *File, unsigned Line,
690 DIScopeRef Scope, DITypeRef BaseType,
691 uint64_t SizeInBits, uint64_t AlignInBits,
692 uint64_t OffsetInBits, unsigned Flags,
693 Metadata *ExtraData, StorageType Storage,
694 bool ShouldCreate = true) {
695 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
696 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
697 Flags, ExtraData, Storage, ShouldCreate);
699 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
700 MDString *Name, Metadata *File, unsigned Line,
701 Metadata *Scope, Metadata *BaseType,
702 uint64_t SizeInBits, uint64_t AlignInBits,
703 uint64_t OffsetInBits, unsigned Flags,
704 Metadata *ExtraData, StorageType Storage,
705 bool ShouldCreate = true);
707 TempDIDerivedType cloneImpl() const {
708 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
709 getScope(), getBaseType(), getSizeInBits(),
710 getAlignInBits(), getOffsetInBits(), getFlags(),
715 DEFINE_MDNODE_GET(DIDerivedType,
716 (unsigned Tag, MDString *Name, Metadata *File,
717 unsigned Line, Metadata *Scope, Metadata *BaseType,
718 uint64_t SizeInBits, uint64_t AlignInBits,
719 uint64_t OffsetInBits, unsigned Flags,
720 Metadata *ExtraData = nullptr),
721 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
722 AlignInBits, OffsetInBits, Flags, ExtraData))
723 DEFINE_MDNODE_GET(DIDerivedType,
724 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
725 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
726 uint64_t AlignInBits, uint64_t OffsetInBits,
727 unsigned Flags, Metadata *ExtraData = nullptr),
728 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
729 AlignInBits, OffsetInBits, Flags, ExtraData))
731 TempDIDerivedType clone() const { return cloneImpl(); }
733 /// \brief Get extra data associated with this derived type.
735 /// Class type for pointer-to-members, objective-c property node for ivars,
736 /// or global constant wrapper for static members.
738 /// TODO: Separate out types that need this extra operand: pointer-to-member
739 /// types and member fields (static members and ivars).
740 Metadata *getExtraData() const { return getRawExtraData(); }
741 Metadata *getRawExtraData() const { return getOperand(4); }
743 /// \brief Get casted version of extra data.
745 DITypeRef getClassType() const {
746 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
747 return DITypeRef(getExtraData());
749 DIObjCProperty *getObjCProperty() const {
750 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
752 Constant *getConstant() const {
753 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
754 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
755 return C->getValue();
760 static bool classof(const Metadata *MD) {
761 return MD->getMetadataID() == DIDerivedTypeKind;
765 /// \brief Base class for DICompositeType and DISubroutineType.
767 /// TODO: Delete; they're not really related.
768 class DICompositeTypeBase : public DIDerivedTypeBase {
769 unsigned RuntimeLang;
772 DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
773 unsigned Tag, unsigned Line, unsigned RuntimeLang,
774 uint64_t SizeInBits, uint64_t AlignInBits,
775 uint64_t OffsetInBits, unsigned Flags,
776 ArrayRef<Metadata *> Ops)
777 : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
778 OffsetInBits, Flags, Ops),
779 RuntimeLang(RuntimeLang) {}
780 ~DICompositeTypeBase() = default;
783 /// \brief Get the elements of the composite type.
785 /// \note Calling this is only valid for \a DICompositeType. This assertion
786 /// can be removed once \a DISubroutineType has been separated from
787 /// "composite types".
788 DINodeArray getElements() const {
789 assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
790 return cast_or_null<MDTuple>(getRawElements());
792 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
793 DITemplateParameterArray 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(DINodeArray Elements) {
812 for (DINode *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(DITypeRef VTableHolder) {
819 replaceOperandWith(5, VTableHolder);
821 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
822 replaceOperandWith(6, TemplateParams.get());
826 static bool classof(const Metadata *MD) {
827 return MD->getMetadataID() == DICompositeTypeKind ||
828 MD->getMetadataID() == DISubroutineTypeKind;
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 DICompositeType : public DICompositeTypeBase {
837 friend class LLVMContextImpl;
840 DICompositeType(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 : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
845 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
847 ~DICompositeType() = default;
849 static DICompositeType *
850 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
851 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
852 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
853 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
854 DITypeRef VTableHolder, DITemplateParameterArray 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 DICompositeType *
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 TempDICompositeType 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(DICompositeType,
880 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
881 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
882 uint64_t AlignInBits, uint64_t OffsetInBits,
883 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
884 DITypeRef VTableHolder,
885 DITemplateParameterArray 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(DICompositeType,
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 TempDICompositeType clone() const { return cloneImpl(); }
904 static bool classof(const Metadata *MD) {
905 return MD->getMetadataID() == DICompositeTypeKind;
909 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
911 if (auto *Composite = dyn_cast<DICompositeType>(N))
912 if (auto *S = Composite->getRawIdentifier())
913 return TypedDINodeRef<T>(S);
914 return TypedDINodeRef<T>(N);
917 /// \brief Type array for a subprogram.
919 /// TODO: Detach from CompositeType, and fold the array of types in directly
921 class DISubroutineType : public DICompositeTypeBase {
922 friend class LLVMContextImpl;
925 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
926 ArrayRef<Metadata *> Ops)
927 : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
928 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
930 ~DISubroutineType() = default;
932 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
933 DITypeRefArray TypeArray,
935 bool ShouldCreate = true) {
936 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
938 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
939 Metadata *TypeArray, StorageType Storage,
940 bool ShouldCreate = true);
942 TempDISubroutineType cloneImpl() const {
943 return getTemporary(getContext(), getFlags(), getTypeArray());
947 DEFINE_MDNODE_GET(DISubroutineType,
948 (unsigned Flags, DITypeRefArray TypeArray),
950 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
953 TempDISubroutineType clone() const { return cloneImpl(); }
955 DITypeRefArray 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() == DISubroutineTypeKind;
965 /// \brief Compile unit.
966 class DICompileUnit : public DIScope {
967 friend class LLVMContextImpl;
970 unsigned SourceLanguage;
972 unsigned RuntimeVersion;
973 unsigned EmissionKind;
975 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
976 bool IsOptimized, unsigned RuntimeVersion,
977 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
978 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
979 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
980 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
981 ~DICompileUnit() = default;
983 static DICompileUnit *
984 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
985 StringRef Producer, bool IsOptimized, StringRef Flags,
986 unsigned RuntimeVersion, StringRef SplitDebugFilename,
987 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
988 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
989 DIGlobalVariableArray GlobalVariables,
990 DIImportedEntityArray 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 DICompileUnit *
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 TempDICompileUnit 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(DICompileUnit,
1018 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1019 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1020 StringRef SplitDebugFilename, unsigned EmissionKind,
1021 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1022 DISubprogramArray Subprograms,
1023 DIGlobalVariableArray GlobalVariables,
1024 DIImportedEntityArray ImportedEntities),
1025 (SourceLanguage, File, Producer, IsOptimized, Flags,
1026 RuntimeVersion, SplitDebugFilename, EmissionKind,
1027 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1029 DEFINE_MDNODE_GET(DICompileUnit,
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 TempDICompileUnit 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 DICompositeTypeArray getEnumTypes() const {
1051 return cast_or_null<MDTuple>(getRawEnumTypes());
1053 DITypeArray getRetainedTypes() const {
1054 return cast_or_null<MDTuple>(getRawRetainedTypes());
1056 DISubprogramArray getSubprograms() const {
1057 return cast_or_null<MDTuple>(getRawSubprograms());
1059 DIGlobalVariableArray getGlobalVariables() const {
1060 return cast_or_null<MDTuple>(getRawGlobalVariables());
1062 DIImportedEntityArray 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 /// DICompileUnit should be fairly rare.
1083 void replaceSubprograms(DISubprogramArray N) {
1084 replaceOperandWith(6, N.get());
1086 void replaceGlobalVariables(DIGlobalVariableArray N) {
1087 replaceOperandWith(7, N.get());
1091 static bool classof(const Metadata *MD) {
1092 return MD->getMetadataID() == DICompileUnitKind;
1096 /// \brief A scope for locals.
1098 /// A legal scope for lexical blocks, local variables, and debug info
1099 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1100 /// DILexicalBlockFile.
1101 class DILocalScope : public DIScope {
1103 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1104 ArrayRef<Metadata *> Ops)
1105 : DIScope(C, ID, Storage, Tag, Ops) {}
1106 ~DILocalScope() = default;
1109 /// \brief Get the subprogram for this scope.
1111 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1113 DISubprogram *getSubprogram() const;
1115 static bool classof(const Metadata *MD) {
1116 return MD->getMetadataID() == DISubprogramKind ||
1117 MD->getMetadataID() == DILexicalBlockKind ||
1118 MD->getMetadataID() == DILexicalBlockFileKind;
1122 /// \brief Debug location.
1124 /// A debug location in source code, used for debug info and otherwise.
1125 class DILocation : public MDNode {
1126 friend class LLVMContextImpl;
1127 friend class MDNode;
1129 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1130 unsigned Column, ArrayRef<Metadata *> MDs);
1131 ~DILocation() { dropAllReferences(); }
1133 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1134 unsigned Column, Metadata *Scope,
1135 Metadata *InlinedAt, StorageType Storage,
1136 bool ShouldCreate = true);
1137 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1138 unsigned Column, DILocalScope *Scope,
1139 DILocation *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 TempDILocation 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(DILocation,
1155 (unsigned Line, unsigned Column, Metadata *Scope,
1156 Metadata *InlinedAt = nullptr),
1157 (Line, Column, Scope, InlinedAt))
1158 DEFINE_MDNODE_GET(DILocation,
1159 (unsigned Line, unsigned Column, DILocalScope *Scope,
1160 DILocation *InlinedAt = nullptr),
1161 (Line, Column, Scope, InlinedAt))
1163 /// \brief Return a (temporary) clone of this.
1164 TempDILocation clone() const { return cloneImpl(); }
1166 unsigned getLine() const { return SubclassData32; }
1167 unsigned getColumn() const { return SubclassData16; }
1168 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1169 DILocation *getInlinedAt() const {
1170 return cast_or_null<DILocation>(getRawInlinedAt());
1173 DIFile *getFile() const { return getScope()->getFile(); }
1174 StringRef getFilename() const { return getScope()->getFilename(); }
1175 StringRef getDirectory() const { return getScope()->getDirectory(); }
1177 /// \brief Get the scope where this is inlined.
1179 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1181 DILocalScope *getInlinedAtScope() const {
1182 if (auto *IA = getInlinedAt())
1183 return IA->getInlinedAtScope();
1187 /// \brief Check whether this can be discriminated from another location.
1189 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1190 /// Scope and inlined-at chains are not recorded in the linetable, so they
1191 /// cannot be used to distinguish basic blocks.
1193 /// The current implementation is weaker than it should be, since it just
1194 /// checks filename and line.
1196 /// FIXME: Add a check for getDiscriminator().
1197 /// FIXME: Add a check for getColumn().
1198 /// FIXME: Change the getFilename() check to getFile() (or add one for
1199 /// getDirectory()).
1200 bool canDiscriminate(const DILocation &RHS) const {
1201 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1204 /// \brief Get the DWARF discriminator.
1206 /// DWARF discriminators distinguish identical file locations between
1207 /// instructions that are on different basic blocks.
1208 inline unsigned getDiscriminator() const;
1210 /// \brief Compute new discriminator in the given context.
1212 /// This modifies the \a LLVMContext that \c this is in to increment the next
1213 /// discriminator for \c this's line/filename combination.
1215 /// FIXME: Delete this. See comments in implementation and at the only call
1216 /// site in \a AddDiscriminators::runOnFunction().
1217 unsigned computeNewDiscriminator() const;
1219 Metadata *getRawScope() const { return getOperand(0); }
1220 Metadata *getRawInlinedAt() const {
1221 if (getNumOperands() == 2)
1222 return getOperand(1);
1226 static bool classof(const Metadata *MD) {
1227 return MD->getMetadataID() == DILocationKind;
1231 /// \brief Subprogram description.
1233 /// TODO: Remove DisplayName. It's always equal to Name.
1234 /// TODO: Split up flags.
1235 class DISubprogram : public DILocalScope {
1236 friend class LLVMContextImpl;
1237 friend class MDNode;
1241 unsigned Virtuality;
1242 unsigned VirtualIndex;
1248 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1249 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1250 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1251 bool IsOptimized, ArrayRef<Metadata *> Ops)
1252 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1254 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1255 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1256 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1257 ~DISubprogram() = default;
1259 static DISubprogram *
1260 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1261 StringRef LinkageName, DIFile *File, unsigned Line,
1262 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1263 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1264 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1265 Constant *Function, DITemplateParameterArray TemplateParams,
1266 DISubprogram *Declaration, DILocalVariableArray Variables,
1267 StorageType Storage, bool ShouldCreate = true) {
1268 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1269 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1270 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1271 Virtuality, VirtualIndex, Flags, IsOptimized,
1272 Function ? ConstantAsMetadata::get(Function) : nullptr,
1273 TemplateParams.get(), Declaration, Variables.get(), Storage,
1276 static DISubprogram *
1277 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1278 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1279 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1280 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1281 unsigned Flags, bool IsOptimized, Metadata *Function,
1282 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1283 StorageType Storage, bool ShouldCreate = true);
1285 TempDISubprogram cloneImpl() const {
1286 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1287 getFile(), getLine(), getType(), isLocalToUnit(),
1288 isDefinition(), getScopeLine(), getContainingType(),
1289 getVirtuality(), getVirtualIndex(), getFlags(),
1290 isOptimized(), getFunctionConstant(),
1291 getTemplateParams(), getDeclaration(), getVariables());
1295 DEFINE_MDNODE_GET(DISubprogram,
1296 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1297 DIFile *File, unsigned Line, DISubroutineType *Type,
1298 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1299 DITypeRef ContainingType, unsigned Virtuality,
1300 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1301 Constant *Function = nullptr,
1302 DITemplateParameterArray TemplateParams = nullptr,
1303 DISubprogram *Declaration = nullptr,
1304 DILocalVariableArray Variables = nullptr),
1305 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1306 IsDefinition, ScopeLine, ContainingType, Virtuality,
1307 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1308 Declaration, Variables))
1311 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1312 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1313 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1314 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1315 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1316 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1317 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1318 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1319 Function, TemplateParams, Declaration, Variables))
1321 TempDISubprogram clone() const { return cloneImpl(); }
1324 unsigned getLine() const { return Line; }
1325 unsigned getVirtuality() const { return Virtuality; }
1326 unsigned getVirtualIndex() const { return VirtualIndex; }
1327 unsigned getScopeLine() const { return ScopeLine; }
1328 unsigned getFlags() const { return Flags; }
1329 bool isLocalToUnit() const { return IsLocalToUnit; }
1330 bool isDefinition() const { return IsDefinition; }
1331 bool isOptimized() const { return IsOptimized; }
1333 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1334 bool isPrivate() const {
1335 return (getFlags() & FlagAccessibility) == FlagPrivate;
1337 bool isProtected() const {
1338 return (getFlags() & FlagAccessibility) == FlagProtected;
1340 bool isPublic() const {
1341 return (getFlags() & FlagAccessibility) == FlagPublic;
1343 bool isExplicit() const { return getFlags() & FlagExplicit; }
1344 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1346 /// \brief Check if this is reference-qualified.
1348 /// Return true if this subprogram is a C++11 reference-qualified non-static
1349 /// member function (void foo() &).
1350 unsigned isLValueReference() const {
1351 return getFlags() & FlagLValueReference;
1354 /// \brief Check if this is rvalue-reference-qualified.
1356 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1357 /// non-static member function (void foo() &&).
1358 unsigned isRValueReference() const {
1359 return getFlags() & FlagRValueReference;
1362 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1364 StringRef getName() const { return getStringOperand(2); }
1365 StringRef getDisplayName() const { return getStringOperand(3); }
1366 StringRef getLinkageName() const { return getStringOperand(4); }
1368 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1369 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1371 DISubroutineType *getType() const {
1372 return cast_or_null<DISubroutineType>(getRawType());
1374 DITypeRef getContainingType() const {
1375 return DITypeRef(getRawContainingType());
1378 Constant *getFunctionConstant() const {
1379 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1380 return C->getValue();
1383 DITemplateParameterArray getTemplateParams() const {
1384 return cast_or_null<MDTuple>(getRawTemplateParams());
1386 DISubprogram *getDeclaration() const {
1387 return cast_or_null<DISubprogram>(getRawDeclaration());
1389 DILocalVariableArray getVariables() const {
1390 return cast_or_null<MDTuple>(getRawVariables());
1393 Metadata *getRawScope() const { return getOperand(1); }
1394 Metadata *getRawType() const { return getOperand(5); }
1395 Metadata *getRawContainingType() const { return getOperand(6); }
1396 Metadata *getRawFunction() const { return getOperand(7); }
1397 Metadata *getRawTemplateParams() const { return getOperand(8); }
1398 Metadata *getRawDeclaration() const { return getOperand(9); }
1399 Metadata *getRawVariables() const { return getOperand(10); }
1401 /// \brief Get a pointer to the function this subprogram describes.
1403 /// This dyn_casts \a getFunctionConstant() to \a Function.
1405 /// FIXME: Should this be looking through bitcasts?
1406 Function *getFunction() const;
1408 /// \brief Replace the function.
1410 /// If \a isUniqued() and not \a isResolved(), this could node will be
1411 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1412 /// that's a problem.
1414 void replaceFunction(Function *F);
1415 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1416 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1419 /// \brief Check if this subprogram decribes the given function.
1421 /// FIXME: Should this be looking through bitcasts?
1422 bool describes(const Function *F) const;
1424 static bool classof(const Metadata *MD) {
1425 return MD->getMetadataID() == DISubprogramKind;
1429 class DILexicalBlockBase : public DILocalScope {
1431 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1432 ArrayRef<Metadata *> Ops)
1433 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1434 ~DILexicalBlockBase() = default;
1437 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1439 Metadata *getRawScope() const { return getOperand(1); }
1441 /// \brief Forwarding accessors to LexicalBlock.
1443 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1445 inline unsigned getLine() const;
1446 inline unsigned getColumn() const;
1448 static bool classof(const Metadata *MD) {
1449 return MD->getMetadataID() == DILexicalBlockKind ||
1450 MD->getMetadataID() == DILexicalBlockFileKind;
1454 class DILexicalBlock : public DILexicalBlockBase {
1455 friend class LLVMContextImpl;
1456 friend class MDNode;
1461 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1462 unsigned Column, ArrayRef<Metadata *> Ops)
1463 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1465 ~DILexicalBlock() = default;
1467 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1468 DIFile *File, unsigned Line, unsigned Column,
1469 StorageType Storage,
1470 bool ShouldCreate = true) {
1471 return getImpl(Context, static_cast<Metadata *>(Scope),
1472 static_cast<Metadata *>(File), Line, Column, Storage,
1476 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1477 Metadata *File, unsigned Line, unsigned Column,
1478 StorageType Storage, bool ShouldCreate = true);
1480 TempDILexicalBlock cloneImpl() const {
1481 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1486 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1487 unsigned Line, unsigned Column),
1488 (Scope, File, Line, Column))
1489 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1490 unsigned Line, unsigned Column),
1491 (Scope, File, Line, Column))
1493 TempDILexicalBlock clone() const { return cloneImpl(); }
1495 unsigned getLine() const { return Line; }
1496 unsigned getColumn() const { return Column; }
1498 static bool classof(const Metadata *MD) {
1499 return MD->getMetadataID() == DILexicalBlockKind;
1503 unsigned DILexicalBlockBase::getLine() const {
1504 if (auto *N = dyn_cast<DILexicalBlock>(this))
1505 return N->getLine();
1509 unsigned DILexicalBlockBase::getColumn() const {
1510 if (auto *N = dyn_cast<DILexicalBlock>(this))
1511 return N->getColumn();
1515 class DILexicalBlockFile : public DILexicalBlockBase {
1516 friend class LLVMContextImpl;
1517 friend class MDNode;
1519 unsigned Discriminator;
1521 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1522 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1523 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1524 Discriminator(Discriminator) {}
1525 ~DILexicalBlockFile() = default;
1527 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1528 DIFile *File, unsigned Discriminator,
1529 StorageType Storage,
1530 bool ShouldCreate = true) {
1531 return getImpl(Context, static_cast<Metadata *>(Scope),
1532 static_cast<Metadata *>(File), Discriminator, Storage,
1536 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1537 Metadata *File, unsigned Discriminator,
1538 StorageType Storage,
1539 bool ShouldCreate = true);
1541 TempDILexicalBlockFile cloneImpl() const {
1542 return getTemporary(getContext(), getScope(), getFile(),
1543 getDiscriminator());
1547 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1548 unsigned Discriminator),
1549 (Scope, File, Discriminator))
1550 DEFINE_MDNODE_GET(DILexicalBlockFile,
1551 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1552 (Scope, File, Discriminator))
1554 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1556 // TODO: Remove these once they're gone from DILexicalBlockBase.
1557 unsigned getLine() const = delete;
1558 unsigned getColumn() const = delete;
1560 unsigned getDiscriminator() const { return Discriminator; }
1562 static bool classof(const Metadata *MD) {
1563 return MD->getMetadataID() == DILexicalBlockFileKind;
1567 unsigned DILocation::getDiscriminator() const {
1568 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1569 return F->getDiscriminator();
1573 class DINamespace : public DIScope {
1574 friend class LLVMContextImpl;
1575 friend class MDNode;
1579 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1580 ArrayRef<Metadata *> Ops)
1581 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1584 ~DINamespace() = default;
1586 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1587 DIFile *File, StringRef Name, unsigned Line,
1588 StorageType Storage, bool ShouldCreate = true) {
1589 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1590 Line, Storage, ShouldCreate);
1592 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1593 Metadata *File, MDString *Name, unsigned Line,
1594 StorageType Storage, bool ShouldCreate = true);
1596 TempDINamespace cloneImpl() const {
1597 return getTemporary(getContext(), getScope(), getFile(), getName(),
1602 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1604 (Scope, File, Name, Line))
1605 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1606 MDString *Name, unsigned Line),
1607 (Scope, File, Name, Line))
1609 TempDINamespace clone() const { return cloneImpl(); }
1611 unsigned getLine() const { return Line; }
1612 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1613 StringRef getName() const { return getStringOperand(2); }
1615 Metadata *getRawScope() const { return getOperand(1); }
1616 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1618 static bool classof(const Metadata *MD) {
1619 return MD->getMetadataID() == DINamespaceKind;
1623 /// \brief Base class for template parameters.
1624 class DITemplateParameter : public DINode {
1626 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1627 unsigned Tag, ArrayRef<Metadata *> Ops)
1628 : DINode(Context, ID, Storage, Tag, Ops) {}
1629 ~DITemplateParameter() = default;
1632 StringRef getName() const { return getStringOperand(0); }
1633 DITypeRef getType() const { return DITypeRef(getRawType()); }
1635 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1636 Metadata *getRawType() const { return getOperand(1); }
1638 static bool classof(const Metadata *MD) {
1639 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1640 MD->getMetadataID() == DITemplateValueParameterKind;
1644 class DITemplateTypeParameter : public DITemplateParameter {
1645 friend class LLVMContextImpl;
1646 friend class MDNode;
1648 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1649 ArrayRef<Metadata *> Ops)
1650 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1651 dwarf::DW_TAG_template_type_parameter, Ops) {}
1652 ~DITemplateTypeParameter() = default;
1654 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1655 DITypeRef Type, StorageType Storage,
1656 bool ShouldCreate = true) {
1657 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1660 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1661 Metadata *Type, StorageType Storage,
1662 bool ShouldCreate = true);
1664 TempDITemplateTypeParameter cloneImpl() const {
1665 return getTemporary(getContext(), getName(), getType());
1669 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1671 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1674 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1676 static bool classof(const Metadata *MD) {
1677 return MD->getMetadataID() == DITemplateTypeParameterKind;
1681 class DITemplateValueParameter : public DITemplateParameter {
1682 friend class LLVMContextImpl;
1683 friend class MDNode;
1685 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1686 unsigned Tag, ArrayRef<Metadata *> Ops)
1687 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1689 ~DITemplateValueParameter() = default;
1691 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1692 StringRef Name, DITypeRef Type,
1693 Metadata *Value, StorageType Storage,
1694 bool ShouldCreate = true) {
1695 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1696 Value, Storage, ShouldCreate);
1698 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1699 MDString *Name, Metadata *Type,
1700 Metadata *Value, StorageType Storage,
1701 bool ShouldCreate = true);
1703 TempDITemplateValueParameter cloneImpl() const {
1704 return getTemporary(getContext(), getTag(), getName(), getType(),
1709 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1710 DITypeRef Type, Metadata *Value),
1711 (Tag, Name, Type, Value))
1712 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1713 Metadata *Type, Metadata *Value),
1714 (Tag, Name, Type, Value))
1716 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1718 Metadata *getValue() const { return getOperand(2); }
1720 static bool classof(const Metadata *MD) {
1721 return MD->getMetadataID() == DITemplateValueParameterKind;
1725 /// \brief Base class for variables.
1727 /// TODO: Hardcode to DW_TAG_variable.
1728 class DIVariable : public DINode {
1732 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1733 unsigned Line, ArrayRef<Metadata *> Ops)
1734 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1735 ~DIVariable() = default;
1738 unsigned getLine() const { return Line; }
1739 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1740 StringRef getName() const { return getStringOperand(1); }
1741 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1742 DITypeRef getType() const { return DITypeRef(getRawType()); }
1744 StringRef getFilename() const {
1745 if (auto *F = getFile())
1746 return F->getFilename();
1749 StringRef getDirectory() const {
1750 if (auto *F = getFile())
1751 return F->getDirectory();
1755 Metadata *getRawScope() const { return getOperand(0); }
1756 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1757 Metadata *getRawFile() const { return getOperand(2); }
1758 Metadata *getRawType() const { return getOperand(3); }
1760 static bool classof(const Metadata *MD) {
1761 return MD->getMetadataID() == DILocalVariableKind ||
1762 MD->getMetadataID() == DIGlobalVariableKind;
1766 /// \brief Global variables.
1768 /// TODO: Remove DisplayName. It's always equal to Name.
1769 class DIGlobalVariable : public DIVariable {
1770 friend class LLVMContextImpl;
1771 friend class MDNode;
1776 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1777 bool IsLocalToUnit, bool IsDefinition,
1778 ArrayRef<Metadata *> Ops)
1779 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1781 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1782 ~DIGlobalVariable() = default;
1784 static DIGlobalVariable *
1785 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1786 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1787 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1788 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1789 bool ShouldCreate = true) {
1790 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1791 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1792 IsLocalToUnit, IsDefinition,
1793 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1794 StaticDataMemberDeclaration, Storage, ShouldCreate);
1796 static DIGlobalVariable *
1797 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1798 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1799 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1800 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1801 bool ShouldCreate = true);
1803 TempDIGlobalVariable cloneImpl() const {
1804 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1805 getFile(), getLine(), getType(), isLocalToUnit(),
1806 isDefinition(), getVariable(),
1807 getStaticDataMemberDeclaration());
1811 DEFINE_MDNODE_GET(DIGlobalVariable,
1812 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1813 DIFile *File, unsigned Line, DITypeRef Type,
1814 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1815 DIDerivedType *StaticDataMemberDeclaration),
1816 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1817 IsDefinition, Variable, StaticDataMemberDeclaration))
1818 DEFINE_MDNODE_GET(DIGlobalVariable,
1819 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1820 Metadata *File, unsigned Line, Metadata *Type,
1821 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1822 Metadata *StaticDataMemberDeclaration),
1823 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1824 IsDefinition, Variable, StaticDataMemberDeclaration))
1826 TempDIGlobalVariable clone() const { return cloneImpl(); }
1828 bool isLocalToUnit() const { return IsLocalToUnit; }
1829 bool isDefinition() const { return IsDefinition; }
1830 StringRef getDisplayName() const { return getStringOperand(4); }
1831 StringRef getLinkageName() const { return getStringOperand(5); }
1832 Constant *getVariable() const {
1833 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1834 return dyn_cast<Constant>(C->getValue());
1837 DIDerivedType *getStaticDataMemberDeclaration() const {
1838 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1841 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1842 Metadata *getRawVariable() const { return getOperand(6); }
1843 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1845 static bool classof(const Metadata *MD) {
1846 return MD->getMetadataID() == DIGlobalVariableKind;
1850 /// \brief Local variable.
1852 /// TODO: Split between arguments and otherwise.
1853 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1854 /// TODO: Split up flags.
1855 class DILocalVariable : public DIVariable {
1856 friend class LLVMContextImpl;
1857 friend class MDNode;
1862 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1863 unsigned Line, unsigned Arg, unsigned Flags,
1864 ArrayRef<Metadata *> Ops)
1865 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1867 ~DILocalVariable() = default;
1869 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1870 DIScope *Scope, StringRef Name, DIFile *File,
1871 unsigned Line, DITypeRef Type, unsigned Arg,
1872 unsigned Flags, StorageType Storage,
1873 bool ShouldCreate = true) {
1874 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1875 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1877 static DILocalVariable *
1878 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1879 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1880 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1882 TempDILocalVariable cloneImpl() const {
1883 return getTemporary(getContext(), getTag(), getScope(), getName(),
1884 getFile(), getLine(), getType(), getArg(), getFlags());
1888 DEFINE_MDNODE_GET(DILocalVariable,
1889 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1890 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1892 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1893 DEFINE_MDNODE_GET(DILocalVariable,
1894 (unsigned Tag, Metadata *Scope, MDString *Name,
1895 Metadata *File, unsigned Line, Metadata *Type,
1896 unsigned Arg, unsigned Flags),
1897 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1899 TempDILocalVariable clone() const { return cloneImpl(); }
1901 /// \brief Get the local scope for this variable.
1903 /// Variables must be defined in a local scope.
1904 DILocalScope *getScope() const {
1905 return cast<DILocalScope>(DIVariable::getScope());
1908 unsigned getArg() const { return Arg; }
1909 unsigned getFlags() const { return Flags; }
1911 bool isArtificial() const { return getFlags() & FlagArtificial; }
1912 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1914 /// \brief Check that a location is valid for this variable.
1916 /// Check that \c DL exists, is in the same subprogram, and has the same
1917 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1918 /// to a \a DbgInfoIntrinsic.)
1919 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1920 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1923 static bool classof(const Metadata *MD) {
1924 return MD->getMetadataID() == DILocalVariableKind;
1928 /// \brief DWARF expression.
1930 /// This is (almost) a DWARF expression that modifies the location of a
1931 /// variable or (or the location of a single piece of a variable).
1933 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1934 /// and have DW_OP_plus consume the topmost elements on the stack.
1936 /// TODO: Co-allocate the expression elements.
1937 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1939 class DIExpression : public MDNode {
1940 friend class LLVMContextImpl;
1941 friend class MDNode;
1943 std::vector<uint64_t> Elements;
1945 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1946 : MDNode(C, DIExpressionKind, Storage, None),
1947 Elements(Elements.begin(), Elements.end()) {}
1948 ~DIExpression() = default;
1950 static DIExpression *getImpl(LLVMContext &Context,
1951 ArrayRef<uint64_t> Elements, StorageType Storage,
1952 bool ShouldCreate = true);
1954 TempDIExpression cloneImpl() const {
1955 return getTemporary(getContext(), getElements());
1959 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1961 TempDIExpression clone() const { return cloneImpl(); }
1963 ArrayRef<uint64_t> getElements() const { return Elements; }
1965 unsigned getNumElements() const { return Elements.size(); }
1966 uint64_t getElement(unsigned I) const {
1967 assert(I < Elements.size() && "Index out of range");
1971 /// \brief Return whether this is a piece of an aggregate variable.
1972 bool isBitPiece() const;
1974 /// \brief Return the offset of this piece in bits.
1975 uint64_t getBitPieceOffset() const;
1977 /// \brief Return the size of this piece in bits.
1978 uint64_t getBitPieceSize() const;
1980 typedef ArrayRef<uint64_t>::iterator element_iterator;
1981 element_iterator elements_begin() const { return getElements().begin(); }
1982 element_iterator elements_end() const { return getElements().end(); }
1984 /// \brief A lightweight wrapper around an expression operand.
1986 /// TODO: Store arguments directly and change \a DIExpression to store a
1992 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1994 const uint64_t *get() const { return Op; }
1996 /// \brief Get the operand code.
1997 uint64_t getOp() const { return *Op; }
1999 /// \brief Get an argument to the operand.
2001 /// Never returns the operand itself.
2002 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2004 unsigned getNumArgs() const { return getSize() - 1; }
2006 /// \brief Return the size of the operand.
2008 /// Return the number of elements in the operand (1 + args).
2009 unsigned getSize() const;
2012 /// \brief An iterator for expression operands.
2013 class expr_op_iterator
2014 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2018 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2020 element_iterator getBase() const { return Op.get(); }
2021 const ExprOperand &operator*() const { return Op; }
2022 const ExprOperand *operator->() const { return &Op; }
2024 expr_op_iterator &operator++() {
2028 expr_op_iterator operator++(int) {
2029 expr_op_iterator T(*this);
2034 /// \brief Get the next iterator.
2036 /// \a std::next() doesn't work because this is technically an
2037 /// input_iterator, but it's a perfectly valid operation. This is an
2038 /// accessor to provide the same functionality.
2039 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2041 bool operator==(const expr_op_iterator &X) const {
2042 return getBase() == X.getBase();
2044 bool operator!=(const expr_op_iterator &X) const {
2045 return getBase() != X.getBase();
2049 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2052 /// \brief Visit the elements via ExprOperand wrappers.
2054 /// These range iterators visit elements through \a ExprOperand wrappers.
2055 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2058 /// \pre \a isValid() gives \c true.
2060 expr_op_iterator expr_op_begin() const {
2061 return expr_op_iterator(elements_begin());
2063 expr_op_iterator expr_op_end() const {
2064 return expr_op_iterator(elements_end());
2068 bool isValid() const;
2070 static bool classof(const Metadata *MD) {
2071 return MD->getMetadataID() == DIExpressionKind;
2075 class DIObjCProperty : public DINode {
2076 friend class LLVMContextImpl;
2077 friend class MDNode;
2080 unsigned Attributes;
2082 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2083 unsigned Attributes, ArrayRef<Metadata *> Ops)
2084 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2086 Line(Line), Attributes(Attributes) {}
2087 ~DIObjCProperty() = default;
2089 static DIObjCProperty *
2090 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2091 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2092 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2093 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2094 getCanonicalMDString(Context, GetterName),
2095 getCanonicalMDString(Context, SetterName), Attributes, Type,
2096 Storage, ShouldCreate);
2098 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2099 Metadata *File, unsigned Line,
2100 MDString *GetterName, MDString *SetterName,
2101 unsigned Attributes, Metadata *Type,
2102 StorageType Storage, bool ShouldCreate = true);
2104 TempDIObjCProperty cloneImpl() const {
2105 return getTemporary(getContext(), getName(), getFile(), getLine(),
2106 getGetterName(), getSetterName(), getAttributes(),
2111 DEFINE_MDNODE_GET(DIObjCProperty,
2112 (StringRef Name, DIFile *File, unsigned Line,
2113 StringRef GetterName, StringRef SetterName,
2114 unsigned Attributes, DIType *Type),
2115 (Name, File, Line, GetterName, SetterName, Attributes,
2117 DEFINE_MDNODE_GET(DIObjCProperty,
2118 (MDString * Name, Metadata *File, unsigned Line,
2119 MDString *GetterName, MDString *SetterName,
2120 unsigned Attributes, Metadata *Type),
2121 (Name, File, Line, GetterName, SetterName, Attributes,
2124 TempDIObjCProperty clone() const { return cloneImpl(); }
2126 unsigned getLine() const { return Line; }
2127 unsigned getAttributes() const { return Attributes; }
2128 StringRef getName() const { return getStringOperand(0); }
2129 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2130 StringRef getGetterName() const { return getStringOperand(2); }
2131 StringRef getSetterName() const { return getStringOperand(3); }
2133 /// \brief Get the type.
2135 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
2136 /// a type ref here.
2137 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2139 StringRef getFilename() const {
2140 if (auto *F = getFile())
2141 return F->getFilename();
2144 StringRef getDirectory() const {
2145 if (auto *F = getFile())
2146 return F->getDirectory();
2150 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2151 Metadata *getRawFile() const { return getOperand(1); }
2152 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2153 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2154 Metadata *getRawType() const { return getOperand(4); }
2156 static bool classof(const Metadata *MD) {
2157 return MD->getMetadataID() == DIObjCPropertyKind;
2161 /// \brief An imported module (C++ using directive or similar).
2162 class DIImportedEntity : public DINode {
2163 friend class LLVMContextImpl;
2164 friend class MDNode;
2168 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2169 unsigned Line, ArrayRef<Metadata *> Ops)
2170 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2171 ~DIImportedEntity() = default;
2173 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2174 DIScope *Scope, DINodeRef Entity,
2175 unsigned Line, StringRef Name,
2176 StorageType Storage,
2177 bool ShouldCreate = true) {
2178 return getImpl(Context, Tag, Scope, Entity, Line,
2179 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2181 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2182 Metadata *Scope, Metadata *Entity,
2183 unsigned Line, MDString *Name,
2184 StorageType Storage,
2185 bool ShouldCreate = true);
2187 TempDIImportedEntity cloneImpl() const {
2188 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2189 getLine(), getName());
2193 DEFINE_MDNODE_GET(DIImportedEntity,
2194 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2195 unsigned Line, StringRef Name = ""),
2196 (Tag, Scope, Entity, Line, Name))
2197 DEFINE_MDNODE_GET(DIImportedEntity,
2198 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2199 unsigned Line, MDString *Name),
2200 (Tag, Scope, Entity, Line, Name))
2202 TempDIImportedEntity clone() const { return cloneImpl(); }
2204 unsigned getLine() const { return Line; }
2205 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2206 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2207 StringRef getName() const { return getStringOperand(2); }
2209 Metadata *getRawScope() const { return getOperand(0); }
2210 Metadata *getRawEntity() const { return getOperand(1); }
2211 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2213 static bool classof(const Metadata *MD) {
2214 return MD->getMetadataID() == DIImportedEntityKind;
2218 } // end namespace llvm
2220 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2221 #undef DEFINE_MDNODE_GET_UNPACK
2222 #undef DEFINE_MDNODE_GET