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;
976 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
977 bool IsOptimized, unsigned RuntimeVersion,
978 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
979 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
980 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
981 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
983 ~DICompileUnit() = default;
985 static DICompileUnit *
986 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
987 StringRef Producer, bool IsOptimized, StringRef Flags,
988 unsigned RuntimeVersion, StringRef SplitDebugFilename,
989 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
990 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
991 DIGlobalVariableArray GlobalVariables,
992 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
993 StorageType Storage, bool ShouldCreate = true) {
994 return getImpl(Context, SourceLanguage, File,
995 getCanonicalMDString(Context, Producer), IsOptimized,
996 getCanonicalMDString(Context, Flags), RuntimeVersion,
997 getCanonicalMDString(Context, SplitDebugFilename),
998 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
999 Subprograms.get(), GlobalVariables.get(),
1000 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
1002 static DICompileUnit *
1003 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1004 MDString *Producer, bool IsOptimized, MDString *Flags,
1005 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1006 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1007 Metadata *Subprograms, Metadata *GlobalVariables,
1008 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
1009 bool ShouldCreate = true);
1011 TempDICompileUnit cloneImpl() const {
1012 return getTemporary(
1013 getContext(), getSourceLanguage(), getFile(), getProducer(),
1014 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1015 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1016 getGlobalVariables(), getImportedEntities(), DWOId);
1020 DEFINE_MDNODE_GET(DICompileUnit,
1021 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1022 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1023 StringRef SplitDebugFilename, unsigned EmissionKind,
1024 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1025 DISubprogramArray Subprograms,
1026 DIGlobalVariableArray GlobalVariables,
1027 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1028 (SourceLanguage, File, Producer, IsOptimized, Flags,
1029 RuntimeVersion, SplitDebugFilename, EmissionKind,
1030 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1031 ImportedEntities, DWOId))
1034 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1035 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1036 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1037 Metadata *RetainedTypes, Metadata *Subprograms,
1038 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1039 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1040 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1041 GlobalVariables, ImportedEntities, DWOId))
1043 TempDICompileUnit clone() const { return cloneImpl(); }
1045 unsigned getSourceLanguage() const { return SourceLanguage; }
1046 bool isOptimized() const { return IsOptimized; }
1047 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1048 unsigned getEmissionKind() const { return EmissionKind; }
1049 StringRef getProducer() const { return getStringOperand(1); }
1050 StringRef getFlags() const { return getStringOperand(2); }
1051 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1052 DICompositeTypeArray getEnumTypes() const {
1053 return cast_or_null<MDTuple>(getRawEnumTypes());
1055 DITypeArray getRetainedTypes() const {
1056 return cast_or_null<MDTuple>(getRawRetainedTypes());
1058 DISubprogramArray getSubprograms() const {
1059 return cast_or_null<MDTuple>(getRawSubprograms());
1061 DIGlobalVariableArray getGlobalVariables() const {
1062 return cast_or_null<MDTuple>(getRawGlobalVariables());
1064 DIImportedEntityArray getImportedEntities() const {
1065 return cast_or_null<MDTuple>(getRawImportedEntities());
1067 unsigned getDWOId() const { return DWOId; }
1069 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1070 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1071 MDString *getRawSplitDebugFilename() const {
1072 return getOperandAs<MDString>(3);
1074 Metadata *getRawEnumTypes() const { return getOperand(4); }
1075 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1076 Metadata *getRawSubprograms() const { return getOperand(6); }
1077 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1078 Metadata *getRawImportedEntities() const { return getOperand(8); }
1080 /// \brief Replace arrays.
1082 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1083 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1084 /// DICompileUnit should be fairly rare.
1086 void replaceSubprograms(DISubprogramArray N) {
1087 replaceOperandWith(6, N.get());
1089 void replaceGlobalVariables(DIGlobalVariableArray N) {
1090 replaceOperandWith(7, N.get());
1094 static bool classof(const Metadata *MD) {
1095 return MD->getMetadataID() == DICompileUnitKind;
1099 /// \brief A scope for locals.
1101 /// A legal scope for lexical blocks, local variables, and debug info
1102 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1103 /// DILexicalBlockFile.
1104 class DILocalScope : public DIScope {
1106 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1107 ArrayRef<Metadata *> Ops)
1108 : DIScope(C, ID, Storage, Tag, Ops) {}
1109 ~DILocalScope() = default;
1112 /// \brief Get the subprogram for this scope.
1114 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1116 DISubprogram *getSubprogram() const;
1118 static bool classof(const Metadata *MD) {
1119 return MD->getMetadataID() == DISubprogramKind ||
1120 MD->getMetadataID() == DILexicalBlockKind ||
1121 MD->getMetadataID() == DILexicalBlockFileKind;
1125 /// \brief Debug location.
1127 /// A debug location in source code, used for debug info and otherwise.
1128 class DILocation : public MDNode {
1129 friend class LLVMContextImpl;
1130 friend class MDNode;
1132 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1133 unsigned Column, ArrayRef<Metadata *> MDs);
1134 ~DILocation() { dropAllReferences(); }
1136 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1137 unsigned Column, Metadata *Scope,
1138 Metadata *InlinedAt, StorageType Storage,
1139 bool ShouldCreate = true);
1140 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1141 unsigned Column, DILocalScope *Scope,
1142 DILocation *InlinedAt, StorageType Storage,
1143 bool ShouldCreate = true) {
1144 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1145 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1148 TempDILocation cloneImpl() const {
1149 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1153 // Disallow replacing operands.
1154 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1157 DEFINE_MDNODE_GET(DILocation,
1158 (unsigned Line, unsigned Column, Metadata *Scope,
1159 Metadata *InlinedAt = nullptr),
1160 (Line, Column, Scope, InlinedAt))
1161 DEFINE_MDNODE_GET(DILocation,
1162 (unsigned Line, unsigned Column, DILocalScope *Scope,
1163 DILocation *InlinedAt = nullptr),
1164 (Line, Column, Scope, InlinedAt))
1166 /// \brief Return a (temporary) clone of this.
1167 TempDILocation clone() const { return cloneImpl(); }
1169 unsigned getLine() const { return SubclassData32; }
1170 unsigned getColumn() const { return SubclassData16; }
1171 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1172 DILocation *getInlinedAt() const {
1173 return cast_or_null<DILocation>(getRawInlinedAt());
1176 DIFile *getFile() const { return getScope()->getFile(); }
1177 StringRef getFilename() const { return getScope()->getFilename(); }
1178 StringRef getDirectory() const { return getScope()->getDirectory(); }
1180 /// \brief Get the scope where this is inlined.
1182 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1184 DILocalScope *getInlinedAtScope() const {
1185 if (auto *IA = getInlinedAt())
1186 return IA->getInlinedAtScope();
1190 /// \brief Check whether this can be discriminated from another location.
1192 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1193 /// Scope and inlined-at chains are not recorded in the linetable, so they
1194 /// cannot be used to distinguish basic blocks.
1196 /// The current implementation is weaker than it should be, since it just
1197 /// checks filename and line.
1199 /// FIXME: Add a check for getDiscriminator().
1200 /// FIXME: Add a check for getColumn().
1201 /// FIXME: Change the getFilename() check to getFile() (or add one for
1202 /// getDirectory()).
1203 bool canDiscriminate(const DILocation &RHS) const {
1204 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1207 /// \brief Get the DWARF discriminator.
1209 /// DWARF discriminators distinguish identical file locations between
1210 /// instructions that are on different basic blocks.
1211 inline unsigned getDiscriminator() const;
1213 /// \brief Compute new discriminator in the given context.
1215 /// This modifies the \a LLVMContext that \c this is in to increment the next
1216 /// discriminator for \c this's line/filename combination.
1218 /// FIXME: Delete this. See comments in implementation and at the only call
1219 /// site in \a AddDiscriminators::runOnFunction().
1220 unsigned computeNewDiscriminator() const;
1222 Metadata *getRawScope() const { return getOperand(0); }
1223 Metadata *getRawInlinedAt() const {
1224 if (getNumOperands() == 2)
1225 return getOperand(1);
1229 static bool classof(const Metadata *MD) {
1230 return MD->getMetadataID() == DILocationKind;
1234 /// \brief Subprogram description.
1236 /// TODO: Remove DisplayName. It's always equal to Name.
1237 /// TODO: Split up flags.
1238 class DISubprogram : public DILocalScope {
1239 friend class LLVMContextImpl;
1240 friend class MDNode;
1244 unsigned Virtuality;
1245 unsigned VirtualIndex;
1251 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1252 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1253 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1254 bool IsOptimized, ArrayRef<Metadata *> Ops)
1255 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1257 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1258 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1259 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1260 ~DISubprogram() = default;
1262 static DISubprogram *
1263 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1264 StringRef LinkageName, DIFile *File, unsigned Line,
1265 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1266 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1267 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1268 Constant *Function, DITemplateParameterArray TemplateParams,
1269 DISubprogram *Declaration, DILocalVariableArray Variables,
1270 StorageType Storage, bool ShouldCreate = true) {
1271 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1272 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1273 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1274 Virtuality, VirtualIndex, Flags, IsOptimized,
1275 Function ? ConstantAsMetadata::get(Function) : nullptr,
1276 TemplateParams.get(), Declaration, Variables.get(), Storage,
1279 static DISubprogram *
1280 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1281 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1282 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1283 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1284 unsigned Flags, bool IsOptimized, Metadata *Function,
1285 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1286 StorageType Storage, bool ShouldCreate = true);
1288 TempDISubprogram cloneImpl() const {
1289 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1290 getFile(), getLine(), getType(), isLocalToUnit(),
1291 isDefinition(), getScopeLine(), getContainingType(),
1292 getVirtuality(), getVirtualIndex(), getFlags(),
1293 isOptimized(), getFunctionConstant(),
1294 getTemplateParams(), getDeclaration(), getVariables());
1298 DEFINE_MDNODE_GET(DISubprogram,
1299 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1300 DIFile *File, unsigned Line, DISubroutineType *Type,
1301 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1302 DITypeRef ContainingType, unsigned Virtuality,
1303 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1304 Constant *Function = nullptr,
1305 DITemplateParameterArray TemplateParams = nullptr,
1306 DISubprogram *Declaration = nullptr,
1307 DILocalVariableArray Variables = nullptr),
1308 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1309 IsDefinition, ScopeLine, ContainingType, Virtuality,
1310 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1311 Declaration, Variables))
1314 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1315 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1316 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1317 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1318 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1319 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1320 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1321 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1322 Function, TemplateParams, Declaration, Variables))
1324 TempDISubprogram clone() const { return cloneImpl(); }
1327 unsigned getLine() const { return Line; }
1328 unsigned getVirtuality() const { return Virtuality; }
1329 unsigned getVirtualIndex() const { return VirtualIndex; }
1330 unsigned getScopeLine() const { return ScopeLine; }
1331 unsigned getFlags() const { return Flags; }
1332 bool isLocalToUnit() const { return IsLocalToUnit; }
1333 bool isDefinition() const { return IsDefinition; }
1334 bool isOptimized() const { return IsOptimized; }
1336 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1337 bool isPrivate() const {
1338 return (getFlags() & FlagAccessibility) == FlagPrivate;
1340 bool isProtected() const {
1341 return (getFlags() & FlagAccessibility) == FlagProtected;
1343 bool isPublic() const {
1344 return (getFlags() & FlagAccessibility) == FlagPublic;
1346 bool isExplicit() const { return getFlags() & FlagExplicit; }
1347 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1349 /// \brief Check if this is reference-qualified.
1351 /// Return true if this subprogram is a C++11 reference-qualified non-static
1352 /// member function (void foo() &).
1353 unsigned isLValueReference() const {
1354 return getFlags() & FlagLValueReference;
1357 /// \brief Check if this is rvalue-reference-qualified.
1359 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1360 /// non-static member function (void foo() &&).
1361 unsigned isRValueReference() const {
1362 return getFlags() & FlagRValueReference;
1365 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1367 StringRef getName() const { return getStringOperand(2); }
1368 StringRef getDisplayName() const { return getStringOperand(3); }
1369 StringRef getLinkageName() const { return getStringOperand(4); }
1371 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1372 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1374 DISubroutineType *getType() const {
1375 return cast_or_null<DISubroutineType>(getRawType());
1377 DITypeRef getContainingType() const {
1378 return DITypeRef(getRawContainingType());
1381 Constant *getFunctionConstant() const {
1382 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1383 return C->getValue();
1386 DITemplateParameterArray getTemplateParams() const {
1387 return cast_or_null<MDTuple>(getRawTemplateParams());
1389 DISubprogram *getDeclaration() const {
1390 return cast_or_null<DISubprogram>(getRawDeclaration());
1392 DILocalVariableArray getVariables() const {
1393 return cast_or_null<MDTuple>(getRawVariables());
1396 Metadata *getRawScope() const { return getOperand(1); }
1397 Metadata *getRawType() const { return getOperand(5); }
1398 Metadata *getRawContainingType() const { return getOperand(6); }
1399 Metadata *getRawFunction() const { return getOperand(7); }
1400 Metadata *getRawTemplateParams() const { return getOperand(8); }
1401 Metadata *getRawDeclaration() const { return getOperand(9); }
1402 Metadata *getRawVariables() const { return getOperand(10); }
1404 /// \brief Get a pointer to the function this subprogram describes.
1406 /// This dyn_casts \a getFunctionConstant() to \a Function.
1408 /// FIXME: Should this be looking through bitcasts?
1409 Function *getFunction() const;
1411 /// \brief Replace the function.
1413 /// If \a isUniqued() and not \a isResolved(), this could node will be
1414 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1415 /// that's a problem.
1417 void replaceFunction(Function *F);
1418 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1419 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1422 /// \brief Check if this subprogram decribes the given function.
1424 /// FIXME: Should this be looking through bitcasts?
1425 bool describes(const Function *F) const;
1427 static bool classof(const Metadata *MD) {
1428 return MD->getMetadataID() == DISubprogramKind;
1432 class DILexicalBlockBase : public DILocalScope {
1434 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1435 ArrayRef<Metadata *> Ops)
1436 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1437 ~DILexicalBlockBase() = default;
1440 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1442 Metadata *getRawScope() const { return getOperand(1); }
1444 /// \brief Forwarding accessors to LexicalBlock.
1446 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1448 inline unsigned getLine() const;
1449 inline unsigned getColumn() const;
1451 static bool classof(const Metadata *MD) {
1452 return MD->getMetadataID() == DILexicalBlockKind ||
1453 MD->getMetadataID() == DILexicalBlockFileKind;
1457 class DILexicalBlock : public DILexicalBlockBase {
1458 friend class LLVMContextImpl;
1459 friend class MDNode;
1464 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1465 unsigned Column, ArrayRef<Metadata *> Ops)
1466 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1468 ~DILexicalBlock() = default;
1470 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1471 DIFile *File, unsigned Line, unsigned Column,
1472 StorageType Storage,
1473 bool ShouldCreate = true) {
1474 return getImpl(Context, static_cast<Metadata *>(Scope),
1475 static_cast<Metadata *>(File), Line, Column, Storage,
1479 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1480 Metadata *File, unsigned Line, unsigned Column,
1481 StorageType Storage, bool ShouldCreate = true);
1483 TempDILexicalBlock cloneImpl() const {
1484 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1489 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1490 unsigned Line, unsigned Column),
1491 (Scope, File, Line, Column))
1492 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1493 unsigned Line, unsigned Column),
1494 (Scope, File, Line, Column))
1496 TempDILexicalBlock clone() const { return cloneImpl(); }
1498 unsigned getLine() const { return Line; }
1499 unsigned getColumn() const { return Column; }
1501 static bool classof(const Metadata *MD) {
1502 return MD->getMetadataID() == DILexicalBlockKind;
1506 unsigned DILexicalBlockBase::getLine() const {
1507 if (auto *N = dyn_cast<DILexicalBlock>(this))
1508 return N->getLine();
1512 unsigned DILexicalBlockBase::getColumn() const {
1513 if (auto *N = dyn_cast<DILexicalBlock>(this))
1514 return N->getColumn();
1518 class DILexicalBlockFile : public DILexicalBlockBase {
1519 friend class LLVMContextImpl;
1520 friend class MDNode;
1522 unsigned Discriminator;
1524 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1525 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1526 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1527 Discriminator(Discriminator) {}
1528 ~DILexicalBlockFile() = default;
1530 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1531 DIFile *File, unsigned Discriminator,
1532 StorageType Storage,
1533 bool ShouldCreate = true) {
1534 return getImpl(Context, static_cast<Metadata *>(Scope),
1535 static_cast<Metadata *>(File), Discriminator, Storage,
1539 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1540 Metadata *File, unsigned Discriminator,
1541 StorageType Storage,
1542 bool ShouldCreate = true);
1544 TempDILexicalBlockFile cloneImpl() const {
1545 return getTemporary(getContext(), getScope(), getFile(),
1546 getDiscriminator());
1550 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1551 unsigned Discriminator),
1552 (Scope, File, Discriminator))
1553 DEFINE_MDNODE_GET(DILexicalBlockFile,
1554 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1555 (Scope, File, Discriminator))
1557 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1559 // TODO: Remove these once they're gone from DILexicalBlockBase.
1560 unsigned getLine() const = delete;
1561 unsigned getColumn() const = delete;
1563 unsigned getDiscriminator() const { return Discriminator; }
1565 static bool classof(const Metadata *MD) {
1566 return MD->getMetadataID() == DILexicalBlockFileKind;
1570 unsigned DILocation::getDiscriminator() const {
1571 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1572 return F->getDiscriminator();
1576 class DINamespace : public DIScope {
1577 friend class LLVMContextImpl;
1578 friend class MDNode;
1582 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1583 ArrayRef<Metadata *> Ops)
1584 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1587 ~DINamespace() = default;
1589 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1590 DIFile *File, StringRef Name, unsigned Line,
1591 StorageType Storage, bool ShouldCreate = true) {
1592 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1593 Line, Storage, ShouldCreate);
1595 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1596 Metadata *File, MDString *Name, unsigned Line,
1597 StorageType Storage, bool ShouldCreate = true);
1599 TempDINamespace cloneImpl() const {
1600 return getTemporary(getContext(), getScope(), getFile(), getName(),
1605 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1607 (Scope, File, Name, Line))
1608 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1609 MDString *Name, unsigned Line),
1610 (Scope, File, Name, Line))
1612 TempDINamespace clone() const { return cloneImpl(); }
1614 unsigned getLine() const { return Line; }
1615 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1616 StringRef getName() const { return getStringOperand(2); }
1618 Metadata *getRawScope() const { return getOperand(1); }
1619 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1621 static bool classof(const Metadata *MD) {
1622 return MD->getMetadataID() == DINamespaceKind;
1626 /// \brief Base class for template parameters.
1627 class DITemplateParameter : public DINode {
1629 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1630 unsigned Tag, ArrayRef<Metadata *> Ops)
1631 : DINode(Context, ID, Storage, Tag, Ops) {}
1632 ~DITemplateParameter() = default;
1635 StringRef getName() const { return getStringOperand(0); }
1636 DITypeRef getType() const { return DITypeRef(getRawType()); }
1638 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1639 Metadata *getRawType() const { return getOperand(1); }
1641 static bool classof(const Metadata *MD) {
1642 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1643 MD->getMetadataID() == DITemplateValueParameterKind;
1647 class DITemplateTypeParameter : public DITemplateParameter {
1648 friend class LLVMContextImpl;
1649 friend class MDNode;
1651 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1652 ArrayRef<Metadata *> Ops)
1653 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1654 dwarf::DW_TAG_template_type_parameter, Ops) {}
1655 ~DITemplateTypeParameter() = default;
1657 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1658 DITypeRef Type, StorageType Storage,
1659 bool ShouldCreate = true) {
1660 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1663 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1664 Metadata *Type, StorageType Storage,
1665 bool ShouldCreate = true);
1667 TempDITemplateTypeParameter cloneImpl() const {
1668 return getTemporary(getContext(), getName(), getType());
1672 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1674 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1677 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1679 static bool classof(const Metadata *MD) {
1680 return MD->getMetadataID() == DITemplateTypeParameterKind;
1684 class DITemplateValueParameter : public DITemplateParameter {
1685 friend class LLVMContextImpl;
1686 friend class MDNode;
1688 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1689 unsigned Tag, ArrayRef<Metadata *> Ops)
1690 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1692 ~DITemplateValueParameter() = default;
1694 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1695 StringRef Name, DITypeRef Type,
1696 Metadata *Value, StorageType Storage,
1697 bool ShouldCreate = true) {
1698 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1699 Value, Storage, ShouldCreate);
1701 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1702 MDString *Name, Metadata *Type,
1703 Metadata *Value, StorageType Storage,
1704 bool ShouldCreate = true);
1706 TempDITemplateValueParameter cloneImpl() const {
1707 return getTemporary(getContext(), getTag(), getName(), getType(),
1712 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1713 DITypeRef Type, Metadata *Value),
1714 (Tag, Name, Type, Value))
1715 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1716 Metadata *Type, Metadata *Value),
1717 (Tag, Name, Type, Value))
1719 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1721 Metadata *getValue() const { return getOperand(2); }
1723 static bool classof(const Metadata *MD) {
1724 return MD->getMetadataID() == DITemplateValueParameterKind;
1728 /// \brief Base class for variables.
1730 /// TODO: Hardcode to DW_TAG_variable.
1731 class DIVariable : public DINode {
1735 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1736 unsigned Line, ArrayRef<Metadata *> Ops)
1737 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1738 ~DIVariable() = default;
1741 unsigned getLine() const { return Line; }
1742 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1743 StringRef getName() const { return getStringOperand(1); }
1744 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1745 DITypeRef getType() const { return DITypeRef(getRawType()); }
1747 StringRef getFilename() const {
1748 if (auto *F = getFile())
1749 return F->getFilename();
1752 StringRef getDirectory() const {
1753 if (auto *F = getFile())
1754 return F->getDirectory();
1758 Metadata *getRawScope() const { return getOperand(0); }
1759 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1760 Metadata *getRawFile() const { return getOperand(2); }
1761 Metadata *getRawType() const { return getOperand(3); }
1763 static bool classof(const Metadata *MD) {
1764 return MD->getMetadataID() == DILocalVariableKind ||
1765 MD->getMetadataID() == DIGlobalVariableKind;
1769 /// \brief Global variables.
1771 /// TODO: Remove DisplayName. It's always equal to Name.
1772 class DIGlobalVariable : public DIVariable {
1773 friend class LLVMContextImpl;
1774 friend class MDNode;
1779 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1780 bool IsLocalToUnit, bool IsDefinition,
1781 ArrayRef<Metadata *> Ops)
1782 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1784 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1785 ~DIGlobalVariable() = default;
1787 static DIGlobalVariable *
1788 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1789 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1790 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1791 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1792 bool ShouldCreate = true) {
1793 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1794 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1795 IsLocalToUnit, IsDefinition,
1796 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1797 StaticDataMemberDeclaration, Storage, ShouldCreate);
1799 static DIGlobalVariable *
1800 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1801 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1802 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1803 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1804 bool ShouldCreate = true);
1806 TempDIGlobalVariable cloneImpl() const {
1807 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1808 getFile(), getLine(), getType(), isLocalToUnit(),
1809 isDefinition(), getVariable(),
1810 getStaticDataMemberDeclaration());
1814 DEFINE_MDNODE_GET(DIGlobalVariable,
1815 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1816 DIFile *File, unsigned Line, DITypeRef Type,
1817 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1818 DIDerivedType *StaticDataMemberDeclaration),
1819 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1820 IsDefinition, Variable, StaticDataMemberDeclaration))
1821 DEFINE_MDNODE_GET(DIGlobalVariable,
1822 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1823 Metadata *File, unsigned Line, Metadata *Type,
1824 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1825 Metadata *StaticDataMemberDeclaration),
1826 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1827 IsDefinition, Variable, StaticDataMemberDeclaration))
1829 TempDIGlobalVariable clone() const { return cloneImpl(); }
1831 bool isLocalToUnit() const { return IsLocalToUnit; }
1832 bool isDefinition() const { return IsDefinition; }
1833 StringRef getDisplayName() const { return getStringOperand(4); }
1834 StringRef getLinkageName() const { return getStringOperand(5); }
1835 Constant *getVariable() const {
1836 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1837 return dyn_cast<Constant>(C->getValue());
1840 DIDerivedType *getStaticDataMemberDeclaration() const {
1841 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1844 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1845 Metadata *getRawVariable() const { return getOperand(6); }
1846 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1848 static bool classof(const Metadata *MD) {
1849 return MD->getMetadataID() == DIGlobalVariableKind;
1853 /// \brief Local variable.
1855 /// TODO: Split between arguments and otherwise.
1856 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1857 /// TODO: Split up flags.
1858 class DILocalVariable : public DIVariable {
1859 friend class LLVMContextImpl;
1860 friend class MDNode;
1865 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1866 unsigned Line, unsigned Arg, unsigned Flags,
1867 ArrayRef<Metadata *> Ops)
1868 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1870 ~DILocalVariable() = default;
1872 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1873 DIScope *Scope, StringRef Name, DIFile *File,
1874 unsigned Line, DITypeRef Type, unsigned Arg,
1875 unsigned Flags, StorageType Storage,
1876 bool ShouldCreate = true) {
1877 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1878 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1880 static DILocalVariable *
1881 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1882 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1883 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1885 TempDILocalVariable cloneImpl() const {
1886 return getTemporary(getContext(), getTag(), getScope(), getName(),
1887 getFile(), getLine(), getType(), getArg(), getFlags());
1891 DEFINE_MDNODE_GET(DILocalVariable,
1892 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1893 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1895 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1896 DEFINE_MDNODE_GET(DILocalVariable,
1897 (unsigned Tag, Metadata *Scope, MDString *Name,
1898 Metadata *File, unsigned Line, Metadata *Type,
1899 unsigned Arg, unsigned Flags),
1900 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1902 TempDILocalVariable clone() const { return cloneImpl(); }
1904 /// \brief Get the local scope for this variable.
1906 /// Variables must be defined in a local scope.
1907 DILocalScope *getScope() const {
1908 return cast<DILocalScope>(DIVariable::getScope());
1911 unsigned getArg() const { return Arg; }
1912 unsigned getFlags() const { return Flags; }
1914 bool isArtificial() const { return getFlags() & FlagArtificial; }
1915 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1917 /// \brief Check that a location is valid for this variable.
1919 /// Check that \c DL exists, is in the same subprogram, and has the same
1920 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1921 /// to a \a DbgInfoIntrinsic.)
1922 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1923 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1926 static bool classof(const Metadata *MD) {
1927 return MD->getMetadataID() == DILocalVariableKind;
1931 /// \brief DWARF expression.
1933 /// This is (almost) a DWARF expression that modifies the location of a
1934 /// variable or (or the location of a single piece of a variable).
1936 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1937 /// and have DW_OP_plus consume the topmost elements on the stack.
1939 /// TODO: Co-allocate the expression elements.
1940 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1942 class DIExpression : public MDNode {
1943 friend class LLVMContextImpl;
1944 friend class MDNode;
1946 std::vector<uint64_t> Elements;
1948 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1949 : MDNode(C, DIExpressionKind, Storage, None),
1950 Elements(Elements.begin(), Elements.end()) {}
1951 ~DIExpression() = default;
1953 static DIExpression *getImpl(LLVMContext &Context,
1954 ArrayRef<uint64_t> Elements, StorageType Storage,
1955 bool ShouldCreate = true);
1957 TempDIExpression cloneImpl() const {
1958 return getTemporary(getContext(), getElements());
1962 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1964 TempDIExpression clone() const { return cloneImpl(); }
1966 ArrayRef<uint64_t> getElements() const { return Elements; }
1968 unsigned getNumElements() const { return Elements.size(); }
1969 uint64_t getElement(unsigned I) const {
1970 assert(I < Elements.size() && "Index out of range");
1974 /// \brief Return whether this is a piece of an aggregate variable.
1975 bool isBitPiece() const;
1977 /// \brief Return the offset of this piece in bits.
1978 uint64_t getBitPieceOffset() const;
1980 /// \brief Return the size of this piece in bits.
1981 uint64_t getBitPieceSize() const;
1983 typedef ArrayRef<uint64_t>::iterator element_iterator;
1984 element_iterator elements_begin() const { return getElements().begin(); }
1985 element_iterator elements_end() const { return getElements().end(); }
1987 /// \brief A lightweight wrapper around an expression operand.
1989 /// TODO: Store arguments directly and change \a DIExpression to store a
1995 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1997 const uint64_t *get() const { return Op; }
1999 /// \brief Get the operand code.
2000 uint64_t getOp() const { return *Op; }
2002 /// \brief Get an argument to the operand.
2004 /// Never returns the operand itself.
2005 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2007 unsigned getNumArgs() const { return getSize() - 1; }
2009 /// \brief Return the size of the operand.
2011 /// Return the number of elements in the operand (1 + args).
2012 unsigned getSize() const;
2015 /// \brief An iterator for expression operands.
2016 class expr_op_iterator
2017 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2021 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2023 element_iterator getBase() const { return Op.get(); }
2024 const ExprOperand &operator*() const { return Op; }
2025 const ExprOperand *operator->() const { return &Op; }
2027 expr_op_iterator &operator++() {
2031 expr_op_iterator operator++(int) {
2032 expr_op_iterator T(*this);
2037 /// \brief Get the next iterator.
2039 /// \a std::next() doesn't work because this is technically an
2040 /// input_iterator, but it's a perfectly valid operation. This is an
2041 /// accessor to provide the same functionality.
2042 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2044 bool operator==(const expr_op_iterator &X) const {
2045 return getBase() == X.getBase();
2047 bool operator!=(const expr_op_iterator &X) const {
2048 return getBase() != X.getBase();
2052 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2055 /// \brief Visit the elements via ExprOperand wrappers.
2057 /// These range iterators visit elements through \a ExprOperand wrappers.
2058 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2061 /// \pre \a isValid() gives \c true.
2063 expr_op_iterator expr_op_begin() const {
2064 return expr_op_iterator(elements_begin());
2066 expr_op_iterator expr_op_end() const {
2067 return expr_op_iterator(elements_end());
2071 bool isValid() const;
2073 static bool classof(const Metadata *MD) {
2074 return MD->getMetadataID() == DIExpressionKind;
2078 class DIObjCProperty : public DINode {
2079 friend class LLVMContextImpl;
2080 friend class MDNode;
2083 unsigned Attributes;
2085 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2086 unsigned Attributes, ArrayRef<Metadata *> Ops)
2087 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2089 Line(Line), Attributes(Attributes) {}
2090 ~DIObjCProperty() = default;
2092 static DIObjCProperty *
2093 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2094 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2095 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2096 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2097 getCanonicalMDString(Context, GetterName),
2098 getCanonicalMDString(Context, SetterName), Attributes, Type,
2099 Storage, ShouldCreate);
2101 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2102 Metadata *File, unsigned Line,
2103 MDString *GetterName, MDString *SetterName,
2104 unsigned Attributes, Metadata *Type,
2105 StorageType Storage, bool ShouldCreate = true);
2107 TempDIObjCProperty cloneImpl() const {
2108 return getTemporary(getContext(), getName(), getFile(), getLine(),
2109 getGetterName(), getSetterName(), getAttributes(),
2114 DEFINE_MDNODE_GET(DIObjCProperty,
2115 (StringRef Name, DIFile *File, unsigned Line,
2116 StringRef GetterName, StringRef SetterName,
2117 unsigned Attributes, DIType *Type),
2118 (Name, File, Line, GetterName, SetterName, Attributes,
2120 DEFINE_MDNODE_GET(DIObjCProperty,
2121 (MDString * Name, Metadata *File, unsigned Line,
2122 MDString *GetterName, MDString *SetterName,
2123 unsigned Attributes, Metadata *Type),
2124 (Name, File, Line, GetterName, SetterName, Attributes,
2127 TempDIObjCProperty clone() const { return cloneImpl(); }
2129 unsigned getLine() const { return Line; }
2130 unsigned getAttributes() const { return Attributes; }
2131 StringRef getName() const { return getStringOperand(0); }
2132 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2133 StringRef getGetterName() const { return getStringOperand(2); }
2134 StringRef getSetterName() const { return getStringOperand(3); }
2136 /// \brief Get the type.
2138 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
2139 /// a type ref here.
2140 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2142 StringRef getFilename() const {
2143 if (auto *F = getFile())
2144 return F->getFilename();
2147 StringRef getDirectory() const {
2148 if (auto *F = getFile())
2149 return F->getDirectory();
2153 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2154 Metadata *getRawFile() const { return getOperand(1); }
2155 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2156 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2157 Metadata *getRawType() const { return getOperand(4); }
2159 static bool classof(const Metadata *MD) {
2160 return MD->getMetadataID() == DIObjCPropertyKind;
2164 /// \brief An imported module (C++ using directive or similar).
2165 class DIImportedEntity : public DINode {
2166 friend class LLVMContextImpl;
2167 friend class MDNode;
2171 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2172 unsigned Line, ArrayRef<Metadata *> Ops)
2173 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2174 ~DIImportedEntity() = default;
2176 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2177 DIScope *Scope, DINodeRef Entity,
2178 unsigned Line, StringRef Name,
2179 StorageType Storage,
2180 bool ShouldCreate = true) {
2181 return getImpl(Context, Tag, Scope, Entity, Line,
2182 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2184 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2185 Metadata *Scope, Metadata *Entity,
2186 unsigned Line, MDString *Name,
2187 StorageType Storage,
2188 bool ShouldCreate = true);
2190 TempDIImportedEntity cloneImpl() const {
2191 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2192 getLine(), getName());
2196 DEFINE_MDNODE_GET(DIImportedEntity,
2197 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2198 unsigned Line, StringRef Name = ""),
2199 (Tag, Scope, Entity, Line, Name))
2200 DEFINE_MDNODE_GET(DIImportedEntity,
2201 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2202 unsigned Line, MDString *Name),
2203 (Tag, Scope, Entity, Line, Name))
2205 TempDIImportedEntity clone() const { return cloneImpl(); }
2207 unsigned getLine() const { return Line; }
2208 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2209 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2210 StringRef getName() const { return getStringOperand(2); }
2212 Metadata *getRawScope() const { return getOperand(0); }
2213 Metadata *getRawEntity() const { return getOperand(1); }
2214 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2216 static bool classof(const Metadata *MD) {
2217 return MD->getMetadataID() == DIImportedEntityKind;
2221 } // end namespace llvm
2223 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2224 #undef DEFINE_MDNODE_GET_UNPACK
2225 #undef DEFINE_MDNODE_GET