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:
227 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
228 typedef Metadata *SimpleType;
229 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
235 struct simplify_type<TypedDINodeRef<T>>
236 : simplify_type<const TypedDINodeRef<T>> {};
238 /// \brief Generic tagged DWARF-like metadata node.
240 /// An un-specialized DWARF-like metadata node. The first operand is a
241 /// (possibly empty) null-separated \a MDString header that contains arbitrary
242 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
243 /// to other metadata.
244 class GenericDINode : public DINode {
245 friend class LLVMContextImpl;
248 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
249 unsigned Tag, ArrayRef<Metadata *> Ops1,
250 ArrayRef<Metadata *> Ops2)
251 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
254 ~GenericDINode() { dropAllReferences(); }
256 void setHash(unsigned Hash) { SubclassData32 = Hash; }
257 void recalculateHash();
259 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
260 StringRef Header, ArrayRef<Metadata *> DwarfOps,
261 StorageType Storage, bool ShouldCreate = true) {
262 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
263 DwarfOps, Storage, ShouldCreate);
266 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
267 MDString *Header, ArrayRef<Metadata *> DwarfOps,
268 StorageType Storage, bool ShouldCreate = true);
270 TempGenericDINode cloneImpl() const {
272 getContext(), getTag(), getHeader(),
273 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
277 unsigned getHash() const { return SubclassData32; }
279 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
280 ArrayRef<Metadata *> DwarfOps),
281 (Tag, Header, DwarfOps))
282 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
283 ArrayRef<Metadata *> DwarfOps),
284 (Tag, Header, DwarfOps))
286 /// \brief Return a (temporary) clone of this.
287 TempGenericDINode clone() const { return cloneImpl(); }
289 unsigned getTag() const { return SubclassData16; }
290 StringRef getHeader() const { return getStringOperand(0); }
292 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
293 op_iterator dwarf_op_end() const { return op_end(); }
294 op_range dwarf_operands() const {
295 return op_range(dwarf_op_begin(), dwarf_op_end());
298 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
299 const MDOperand &getDwarfOperand(unsigned I) const {
300 return getOperand(I + 1);
302 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
303 replaceOperandWith(I + 1, New);
306 static bool classof(const Metadata *MD) {
307 return MD->getMetadataID() == GenericDINodeKind;
311 /// \brief Array subrange.
313 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
315 class DISubrange : public DINode {
316 friend class LLVMContextImpl;
322 DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
324 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
325 Count(Count), LowerBound(LowerBound) {}
326 ~DISubrange() = default;
328 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
329 int64_t LowerBound, StorageType Storage,
330 bool ShouldCreate = true);
332 TempDISubrange cloneImpl() const {
333 return getTemporary(getContext(), getCount(), getLowerBound());
337 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
340 TempDISubrange clone() const { return cloneImpl(); }
342 int64_t getLowerBound() const { return LowerBound; }
343 int64_t getCount() const { return Count; }
345 static bool classof(const Metadata *MD) {
346 return MD->getMetadataID() == DISubrangeKind;
350 /// \brief Enumeration value.
352 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
353 /// longer creates a type cycle.
354 class DIEnumerator : public DINode {
355 friend class LLVMContextImpl;
360 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
361 ArrayRef<Metadata *> Ops)
362 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
364 ~DIEnumerator() = default;
366 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
367 StringRef Name, StorageType Storage,
368 bool ShouldCreate = true) {
369 return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
372 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
373 MDString *Name, StorageType Storage,
374 bool ShouldCreate = true);
376 TempDIEnumerator cloneImpl() const {
377 return getTemporary(getContext(), getValue(), getName());
381 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
383 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
386 TempDIEnumerator clone() const { return cloneImpl(); }
388 int64_t getValue() const { return Value; }
389 StringRef getName() const { return getStringOperand(0); }
391 MDString *getRawName() const { return getOperandAs<MDString>(0); }
393 static bool classof(const Metadata *MD) {
394 return MD->getMetadataID() == DIEnumeratorKind;
398 /// \brief Base class for scope-like contexts.
400 /// Base class for lexical scopes and types (which are also declaration
403 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
404 class DIScope : public DINode {
406 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
407 ArrayRef<Metadata *> Ops)
408 : DINode(C, ID, Storage, Tag, Ops) {}
409 ~DIScope() = default;
412 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
414 inline StringRef getFilename() const;
415 inline StringRef getDirectory() const;
417 StringRef getName() const;
418 DIScopeRef getScope() const;
420 /// \brief Return the raw underlying file.
422 /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
423 /// (it\em is the file). If \c this is an \a DIFile, we need to return \c
424 /// this. Otherwise, return the first operand, which is where all other
425 /// subclasses store their file pointer.
426 Metadata *getRawFile() const {
427 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
428 : static_cast<Metadata *>(getOperand(0));
431 DIScopeRef getRef() const { return DIScopeRef::get(this); }
433 static bool classof(const Metadata *MD) {
434 switch (MD->getMetadataID()) {
437 case DIBasicTypeKind:
438 case DIDerivedTypeKind:
439 case DICompositeTypeKind:
440 case DISubroutineTypeKind:
442 case DICompileUnitKind:
443 case DISubprogramKind:
444 case DILexicalBlockKind:
445 case DILexicalBlockFileKind:
446 case DINamespaceKind:
455 /// TODO: Merge with directory/file node (including users).
456 /// TODO: Canonicalize paths on creation.
457 class DIFile : public DIScope {
458 friend class LLVMContextImpl;
461 DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
462 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
465 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
466 StringRef Directory, StorageType Storage,
467 bool ShouldCreate = true) {
468 return getImpl(Context, getCanonicalMDString(Context, Filename),
469 getCanonicalMDString(Context, Directory), Storage,
472 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
473 MDString *Directory, StorageType Storage,
474 bool ShouldCreate = true);
476 TempDIFile cloneImpl() const {
477 return getTemporary(getContext(), getFilename(), getDirectory());
481 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
482 (Filename, Directory))
483 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
484 (Filename, Directory))
486 TempDIFile clone() const { return cloneImpl(); }
488 StringRef getFilename() const { return getStringOperand(0); }
489 StringRef getDirectory() const { return getStringOperand(1); }
491 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
492 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
494 static bool classof(const Metadata *MD) {
495 return MD->getMetadataID() == DIFileKind;
499 StringRef DIScope::getFilename() const {
500 if (auto *F = getFile())
501 return F->getFilename();
505 StringRef DIScope::getDirectory() const {
506 if (auto *F = getFile())
507 return F->getDirectory();
511 /// \brief Base class for types.
513 /// TODO: Remove the hardcoded name and context, since many types don't use
515 /// TODO: Split up flags.
516 class DIType : public DIScope {
520 uint64_t AlignInBits;
521 uint64_t OffsetInBits;
524 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
525 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
526 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
527 : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
528 SizeInBits(SizeInBits), AlignInBits(AlignInBits),
529 OffsetInBits(OffsetInBits) {}
533 TempDIType clone() const {
534 return TempDIType(cast<DIType>(MDNode::clone().release()));
537 unsigned getLine() const { return Line; }
538 uint64_t getSizeInBits() const { return SizeInBits; }
539 uint64_t getAlignInBits() const { return AlignInBits; }
540 uint64_t getOffsetInBits() const { return OffsetInBits; }
541 unsigned getFlags() const { return Flags; }
543 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
544 StringRef getName() const { return getStringOperand(2); }
547 Metadata *getRawScope() const { return getOperand(1); }
548 MDString *getRawName() const { return getOperandAs<MDString>(2); }
550 void setFlags(unsigned NewFlags) {
551 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
555 bool isPrivate() const {
556 return (getFlags() & FlagAccessibility) == FlagPrivate;
558 bool isProtected() const {
559 return (getFlags() & FlagAccessibility) == FlagProtected;
561 bool isPublic() const {
562 return (getFlags() & FlagAccessibility) == FlagPublic;
564 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
565 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
566 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
567 bool isVirtual() const { return getFlags() & FlagVirtual; }
568 bool isArtificial() const { return getFlags() & FlagArtificial; }
569 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
570 bool isObjcClassComplete() const {
571 return getFlags() & FlagObjcClassComplete;
573 bool isVector() const { return getFlags() & FlagVector; }
574 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
575 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
576 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
578 DITypeRef getRef() const { return DITypeRef::get(this); }
580 static bool classof(const Metadata *MD) {
581 switch (MD->getMetadataID()) {
584 case DIBasicTypeKind:
585 case DIDerivedTypeKind:
586 case DICompositeTypeKind:
587 case DISubroutineTypeKind:
593 /// \brief Basic type, like 'int' or 'float'.
595 /// TODO: Split out DW_TAG_unspecified_type.
596 /// TODO: Drop unused accessors.
597 class DIBasicType : public DIType {
598 friend class LLVMContextImpl;
603 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
604 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
605 ArrayRef<Metadata *> Ops)
606 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
608 Encoding(Encoding) {}
609 ~DIBasicType() = default;
611 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
612 StringRef Name, uint64_t SizeInBits,
613 uint64_t AlignInBits, unsigned Encoding,
614 StorageType Storage, bool ShouldCreate = true) {
615 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
616 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
618 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
619 MDString *Name, uint64_t SizeInBits,
620 uint64_t AlignInBits, unsigned Encoding,
621 StorageType Storage, bool ShouldCreate = true);
623 TempDIBasicType cloneImpl() const {
624 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
625 getAlignInBits(), getEncoding());
629 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
630 (Tag, Name, 0, 0, 0))
631 DEFINE_MDNODE_GET(DIBasicType,
632 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
633 uint64_t AlignInBits, unsigned Encoding),
634 (Tag, Name, SizeInBits, AlignInBits, Encoding))
635 DEFINE_MDNODE_GET(DIBasicType,
636 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
637 uint64_t AlignInBits, unsigned Encoding),
638 (Tag, Name, SizeInBits, AlignInBits, Encoding))
640 TempDIBasicType clone() const { return cloneImpl(); }
642 unsigned getEncoding() const { return Encoding; }
644 static bool classof(const Metadata *MD) {
645 return MD->getMetadataID() == DIBasicTypeKind;
649 /// \brief Base class for DIDerivedType and DICompositeType.
651 /// TODO: Delete; they're not really related.
652 class DIDerivedTypeBase : public DIType {
654 DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
655 unsigned Tag, unsigned Line, uint64_t SizeInBits,
656 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
657 ArrayRef<Metadata *> Ops)
658 : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
660 ~DIDerivedTypeBase() = default;
663 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
664 Metadata *getRawBaseType() const { return getOperand(3); }
666 static bool classof(const Metadata *MD) {
667 return MD->getMetadataID() == DIDerivedTypeKind ||
668 MD->getMetadataID() == DICompositeTypeKind ||
669 MD->getMetadataID() == DISubroutineTypeKind;
673 /// \brief Derived types.
675 /// This includes qualified types, pointers, references, friends, typedefs, and
678 /// TODO: Split out members (inheritance, fields, methods, etc.).
679 class DIDerivedType : public DIDerivedTypeBase {
680 friend class LLVMContextImpl;
683 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
684 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
685 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
686 : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
687 AlignInBits, OffsetInBits, Flags, Ops) {}
688 ~DIDerivedType() = default;
690 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
691 StringRef Name, DIFile *File, unsigned Line,
692 DIScopeRef Scope, DITypeRef BaseType,
693 uint64_t SizeInBits, uint64_t AlignInBits,
694 uint64_t OffsetInBits, unsigned Flags,
695 Metadata *ExtraData, StorageType Storage,
696 bool ShouldCreate = true) {
697 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
698 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
699 Flags, ExtraData, Storage, ShouldCreate);
701 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
702 MDString *Name, Metadata *File, unsigned Line,
703 Metadata *Scope, Metadata *BaseType,
704 uint64_t SizeInBits, uint64_t AlignInBits,
705 uint64_t OffsetInBits, unsigned Flags,
706 Metadata *ExtraData, StorageType Storage,
707 bool ShouldCreate = true);
709 TempDIDerivedType cloneImpl() const {
710 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
711 getScope(), getBaseType(), getSizeInBits(),
712 getAlignInBits(), getOffsetInBits(), getFlags(),
717 DEFINE_MDNODE_GET(DIDerivedType,
718 (unsigned Tag, MDString *Name, Metadata *File,
719 unsigned Line, Metadata *Scope, Metadata *BaseType,
720 uint64_t SizeInBits, uint64_t AlignInBits,
721 uint64_t OffsetInBits, unsigned Flags,
722 Metadata *ExtraData = nullptr),
723 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
724 AlignInBits, OffsetInBits, Flags, ExtraData))
725 DEFINE_MDNODE_GET(DIDerivedType,
726 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
727 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
728 uint64_t AlignInBits, uint64_t OffsetInBits,
729 unsigned Flags, Metadata *ExtraData = nullptr),
730 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
731 AlignInBits, OffsetInBits, Flags, ExtraData))
733 TempDIDerivedType clone() const { return cloneImpl(); }
735 /// \brief Get extra data associated with this derived type.
737 /// Class type for pointer-to-members, objective-c property node for ivars,
738 /// or global constant wrapper for static members.
740 /// TODO: Separate out types that need this extra operand: pointer-to-member
741 /// types and member fields (static members and ivars).
742 Metadata *getExtraData() const { return getRawExtraData(); }
743 Metadata *getRawExtraData() const { return getOperand(4); }
745 /// \brief Get casted version of extra data.
747 DITypeRef getClassType() const {
748 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
749 return DITypeRef(getExtraData());
751 DIObjCProperty *getObjCProperty() const {
752 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
754 Constant *getConstant() const {
755 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
756 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
757 return C->getValue();
762 static bool classof(const Metadata *MD) {
763 return MD->getMetadataID() == DIDerivedTypeKind;
767 /// \brief Base class for DICompositeType and DISubroutineType.
769 /// TODO: Delete; they're not really related.
770 class DICompositeTypeBase : public DIDerivedTypeBase {
771 unsigned RuntimeLang;
774 DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
775 unsigned Tag, unsigned Line, unsigned RuntimeLang,
776 uint64_t SizeInBits, uint64_t AlignInBits,
777 uint64_t OffsetInBits, unsigned Flags,
778 ArrayRef<Metadata *> Ops)
779 : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
780 OffsetInBits, Flags, Ops),
781 RuntimeLang(RuntimeLang) {}
782 ~DICompositeTypeBase() = default;
785 /// \brief Get the elements of the composite type.
787 /// \note Calling this is only valid for \a DICompositeType. This assertion
788 /// can be removed once \a DISubroutineType has been separated from
789 /// "composite types".
790 DINodeArray getElements() const {
791 assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
792 return cast_or_null<MDTuple>(getRawElements());
794 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
795 DITemplateParameterArray getTemplateParams() const {
796 return cast_or_null<MDTuple>(getRawTemplateParams());
798 StringRef getIdentifier() const { return getStringOperand(7); }
799 unsigned getRuntimeLang() const { return RuntimeLang; }
801 Metadata *getRawElements() const { return getOperand(4); }
802 Metadata *getRawVTableHolder() const { return getOperand(5); }
803 Metadata *getRawTemplateParams() const { return getOperand(6); }
804 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
806 /// \brief Replace operands.
808 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
809 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
810 /// of its movement if necessary.
812 void replaceElements(DINodeArray Elements) {
814 for (DINode *Op : getElements())
815 assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
816 "Lost a member during member list replacement");
818 replaceOperandWith(4, Elements.get());
820 void replaceVTableHolder(DITypeRef VTableHolder) {
821 replaceOperandWith(5, VTableHolder);
823 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
824 replaceOperandWith(6, TemplateParams.get());
828 static bool classof(const Metadata *MD) {
829 return MD->getMetadataID() == DICompositeTypeKind ||
830 MD->getMetadataID() == DISubroutineTypeKind;
834 /// \brief Composite types.
836 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
837 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
838 class DICompositeType : public DICompositeTypeBase {
839 friend class LLVMContextImpl;
842 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
843 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
844 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
845 ArrayRef<Metadata *> Ops)
846 : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
847 RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
849 ~DICompositeType() = default;
851 static DICompositeType *
852 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
853 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
854 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
855 uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
856 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
857 StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
859 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
860 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
861 RuntimeLang, VTableHolder, TemplateParams.get(),
862 getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
864 static DICompositeType *
865 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
866 unsigned Line, Metadata *Scope, Metadata *BaseType,
867 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
868 unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
869 Metadata *VTableHolder, Metadata *TemplateParams,
870 MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
872 TempDICompositeType cloneImpl() const {
873 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
874 getScope(), getBaseType(), getSizeInBits(),
875 getAlignInBits(), getOffsetInBits(), getFlags(),
876 getElements(), getRuntimeLang(), getVTableHolder(),
877 getTemplateParams(), getIdentifier());
881 DEFINE_MDNODE_GET(DICompositeType,
882 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
883 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
884 uint64_t AlignInBits, uint64_t OffsetInBits,
885 unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
886 DITypeRef VTableHolder,
887 DITemplateParameterArray TemplateParams = nullptr,
888 StringRef Identifier = ""),
889 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
890 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
891 VTableHolder, TemplateParams, Identifier))
892 DEFINE_MDNODE_GET(DICompositeType,
893 (unsigned Tag, MDString *Name, Metadata *File,
894 unsigned Line, Metadata *Scope, Metadata *BaseType,
895 uint64_t SizeInBits, uint64_t AlignInBits,
896 uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
897 unsigned RuntimeLang, Metadata *VTableHolder,
898 Metadata *TemplateParams = nullptr,
899 MDString *Identifier = nullptr),
900 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
901 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
902 VTableHolder, TemplateParams, Identifier))
904 TempDICompositeType clone() const { return cloneImpl(); }
906 static bool classof(const Metadata *MD) {
907 return MD->getMetadataID() == DICompositeTypeKind;
911 template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
913 if (auto *Composite = dyn_cast<DICompositeType>(N))
914 if (auto *S = Composite->getRawIdentifier())
915 return TypedDINodeRef<T>(S);
916 return TypedDINodeRef<T>(N);
919 /// \brief Type array for a subprogram.
921 /// TODO: Detach from CompositeType, and fold the array of types in directly
923 class DISubroutineType : public DICompositeTypeBase {
924 friend class LLVMContextImpl;
927 DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
928 ArrayRef<Metadata *> Ops)
929 : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
930 dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
932 ~DISubroutineType() = default;
934 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
935 DITypeRefArray TypeArray,
937 bool ShouldCreate = true) {
938 return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
940 static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
941 Metadata *TypeArray, StorageType Storage,
942 bool ShouldCreate = true);
944 TempDISubroutineType cloneImpl() const {
945 return getTemporary(getContext(), getFlags(), getTypeArray());
949 DEFINE_MDNODE_GET(DISubroutineType,
950 (unsigned Flags, DITypeRefArray TypeArray),
952 DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
955 TempDISubroutineType clone() const { return cloneImpl(); }
957 DITypeRefArray getTypeArray() const {
958 return cast_or_null<MDTuple>(getRawTypeArray());
960 Metadata *getRawTypeArray() const { return getRawElements(); }
962 static bool classof(const Metadata *MD) {
963 return MD->getMetadataID() == DISubroutineTypeKind;
967 /// \brief Compile unit.
968 class DICompileUnit : public DIScope {
969 friend class LLVMContextImpl;
972 unsigned SourceLanguage;
974 unsigned RuntimeVersion;
975 unsigned EmissionKind;
978 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
979 bool IsOptimized, unsigned RuntimeVersion,
980 unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
981 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
982 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
983 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
985 ~DICompileUnit() = default;
987 static DICompileUnit *
988 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
989 StringRef Producer, bool IsOptimized, StringRef Flags,
990 unsigned RuntimeVersion, StringRef SplitDebugFilename,
991 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
992 DITypeArray RetainedTypes, DISubprogramArray Subprograms,
993 DIGlobalVariableArray GlobalVariables,
994 DIImportedEntityArray ImportedEntities, uint64_t DWOId,
995 StorageType Storage, bool ShouldCreate = true) {
996 return getImpl(Context, SourceLanguage, File,
997 getCanonicalMDString(Context, Producer), IsOptimized,
998 getCanonicalMDString(Context, Flags), RuntimeVersion,
999 getCanonicalMDString(Context, SplitDebugFilename),
1000 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
1001 Subprograms.get(), GlobalVariables.get(),
1002 ImportedEntities.get(), DWOId, Storage, ShouldCreate);
1004 static DICompileUnit *
1005 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1006 MDString *Producer, bool IsOptimized, MDString *Flags,
1007 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1008 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1009 Metadata *Subprograms, Metadata *GlobalVariables,
1010 Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
1011 bool ShouldCreate = true);
1013 TempDICompileUnit cloneImpl() const {
1014 return getTemporary(
1015 getContext(), getSourceLanguage(), getFile(), getProducer(),
1016 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1017 getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1018 getGlobalVariables(), getImportedEntities(), DWOId);
1022 DEFINE_MDNODE_GET(DICompileUnit,
1023 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1024 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1025 StringRef SplitDebugFilename, unsigned EmissionKind,
1026 DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1027 DISubprogramArray Subprograms,
1028 DIGlobalVariableArray GlobalVariables,
1029 DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1030 (SourceLanguage, File, Producer, IsOptimized, Flags,
1031 RuntimeVersion, SplitDebugFilename, EmissionKind,
1032 EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1033 ImportedEntities, DWOId))
1036 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1037 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1038 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1039 Metadata *RetainedTypes, Metadata *Subprograms,
1040 Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1041 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1042 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1043 GlobalVariables, ImportedEntities, DWOId))
1045 TempDICompileUnit clone() const { return cloneImpl(); }
1047 unsigned getSourceLanguage() const { return SourceLanguage; }
1048 bool isOptimized() const { return IsOptimized; }
1049 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1050 unsigned getEmissionKind() const { return EmissionKind; }
1051 StringRef getProducer() const { return getStringOperand(1); }
1052 StringRef getFlags() const { return getStringOperand(2); }
1053 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1054 DICompositeTypeArray getEnumTypes() const {
1055 return cast_or_null<MDTuple>(getRawEnumTypes());
1057 DITypeArray getRetainedTypes() const {
1058 return cast_or_null<MDTuple>(getRawRetainedTypes());
1060 DISubprogramArray getSubprograms() const {
1061 return cast_or_null<MDTuple>(getRawSubprograms());
1063 DIGlobalVariableArray getGlobalVariables() const {
1064 return cast_or_null<MDTuple>(getRawGlobalVariables());
1066 DIImportedEntityArray getImportedEntities() const {
1067 return cast_or_null<MDTuple>(getRawImportedEntities());
1069 unsigned getDWOId() const { return DWOId; }
1071 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1072 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1073 MDString *getRawSplitDebugFilename() const {
1074 return getOperandAs<MDString>(3);
1076 Metadata *getRawEnumTypes() const { return getOperand(4); }
1077 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1078 Metadata *getRawSubprograms() const { return getOperand(6); }
1079 Metadata *getRawGlobalVariables() const { return getOperand(7); }
1080 Metadata *getRawImportedEntities() const { return getOperand(8); }
1082 /// \brief Replace arrays.
1084 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1085 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1086 /// DICompileUnit should be fairly rare.
1088 void replaceEnumTypes(DISubprogramArray N) {
1089 replaceOperandWith(4, N.get());
1091 void replaceRetainedTypes(DISubprogramArray N) {
1092 replaceOperandWith(5, N.get());
1094 void replaceSubprograms(DISubprogramArray N) {
1095 replaceOperandWith(6, N.get());
1097 void replaceGlobalVariables(DIGlobalVariableArray N) {
1098 replaceOperandWith(7, N.get());
1100 void replaceImportedEntities(DIGlobalVariableArray N) {
1101 replaceOperandWith(8, N.get());
1105 static bool classof(const Metadata *MD) {
1106 return MD->getMetadataID() == DICompileUnitKind;
1110 /// \brief A scope for locals.
1112 /// A legal scope for lexical blocks, local variables, and debug info
1113 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1114 /// DILexicalBlockFile.
1115 class DILocalScope : public DIScope {
1117 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1118 ArrayRef<Metadata *> Ops)
1119 : DIScope(C, ID, Storage, Tag, Ops) {}
1120 ~DILocalScope() = default;
1123 /// \brief Get the subprogram for this scope.
1125 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1127 DISubprogram *getSubprogram() const;
1129 static bool classof(const Metadata *MD) {
1130 return MD->getMetadataID() == DISubprogramKind ||
1131 MD->getMetadataID() == DILexicalBlockKind ||
1132 MD->getMetadataID() == DILexicalBlockFileKind;
1136 /// \brief Debug location.
1138 /// A debug location in source code, used for debug info and otherwise.
1139 class DILocation : public MDNode {
1140 friend class LLVMContextImpl;
1141 friend class MDNode;
1143 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1144 unsigned Column, ArrayRef<Metadata *> MDs);
1145 ~DILocation() { dropAllReferences(); }
1147 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1148 unsigned Column, Metadata *Scope,
1149 Metadata *InlinedAt, StorageType Storage,
1150 bool ShouldCreate = true);
1151 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1152 unsigned Column, DILocalScope *Scope,
1153 DILocation *InlinedAt, StorageType Storage,
1154 bool ShouldCreate = true) {
1155 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1156 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1159 TempDILocation cloneImpl() const {
1160 return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1164 // Disallow replacing operands.
1165 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1168 DEFINE_MDNODE_GET(DILocation,
1169 (unsigned Line, unsigned Column, Metadata *Scope,
1170 Metadata *InlinedAt = nullptr),
1171 (Line, Column, Scope, InlinedAt))
1172 DEFINE_MDNODE_GET(DILocation,
1173 (unsigned Line, unsigned Column, DILocalScope *Scope,
1174 DILocation *InlinedAt = nullptr),
1175 (Line, Column, Scope, InlinedAt))
1177 /// \brief Return a (temporary) clone of this.
1178 TempDILocation clone() const { return cloneImpl(); }
1180 unsigned getLine() const { return SubclassData32; }
1181 unsigned getColumn() const { return SubclassData16; }
1182 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1183 DILocation *getInlinedAt() const {
1184 return cast_or_null<DILocation>(getRawInlinedAt());
1187 DIFile *getFile() const { return getScope()->getFile(); }
1188 StringRef getFilename() const { return getScope()->getFilename(); }
1189 StringRef getDirectory() const { return getScope()->getDirectory(); }
1191 /// \brief Get the scope where this is inlined.
1193 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1195 DILocalScope *getInlinedAtScope() const {
1196 if (auto *IA = getInlinedAt())
1197 return IA->getInlinedAtScope();
1201 /// \brief Check whether this can be discriminated from another location.
1203 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1204 /// Scope and inlined-at chains are not recorded in the linetable, so they
1205 /// cannot be used to distinguish basic blocks.
1207 /// The current implementation is weaker than it should be, since it just
1208 /// checks filename and line.
1210 /// FIXME: Add a check for getDiscriminator().
1211 /// FIXME: Add a check for getColumn().
1212 /// FIXME: Change the getFilename() check to getFile() (or add one for
1213 /// getDirectory()).
1214 bool canDiscriminate(const DILocation &RHS) const {
1215 return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1218 /// \brief Get the DWARF discriminator.
1220 /// DWARF discriminators distinguish identical file locations between
1221 /// instructions that are on different basic blocks.
1222 inline unsigned getDiscriminator() const;
1224 /// \brief Compute new discriminator in the given context.
1226 /// This modifies the \a LLVMContext that \c this is in to increment the next
1227 /// discriminator for \c this's line/filename combination.
1229 /// FIXME: Delete this. See comments in implementation and at the only call
1230 /// site in \a AddDiscriminators::runOnFunction().
1231 unsigned computeNewDiscriminator() const;
1233 Metadata *getRawScope() const { return getOperand(0); }
1234 Metadata *getRawInlinedAt() const {
1235 if (getNumOperands() == 2)
1236 return getOperand(1);
1240 static bool classof(const Metadata *MD) {
1241 return MD->getMetadataID() == DILocationKind;
1245 /// \brief Subprogram description.
1247 /// TODO: Remove DisplayName. It's always equal to Name.
1248 /// TODO: Split up flags.
1249 class DISubprogram : public DILocalScope {
1250 friend class LLVMContextImpl;
1251 friend class MDNode;
1255 unsigned Virtuality;
1256 unsigned VirtualIndex;
1262 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1263 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1264 unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1265 bool IsOptimized, ArrayRef<Metadata *> Ops)
1266 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1268 Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1269 VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1270 IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1271 ~DISubprogram() = default;
1273 static DISubprogram *
1274 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1275 StringRef LinkageName, DIFile *File, unsigned Line,
1276 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1277 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1278 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1279 Constant *Function, DITemplateParameterArray TemplateParams,
1280 DISubprogram *Declaration, DILocalVariableArray Variables,
1281 StorageType Storage, bool ShouldCreate = true) {
1282 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1283 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1284 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1285 Virtuality, VirtualIndex, Flags, IsOptimized,
1286 Function ? ConstantAsMetadata::get(Function) : nullptr,
1287 TemplateParams.get(), Declaration, Variables.get(), Storage,
1290 static DISubprogram *
1291 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1292 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1293 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1294 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1295 unsigned Flags, bool IsOptimized, Metadata *Function,
1296 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1297 StorageType Storage, bool ShouldCreate = true);
1299 TempDISubprogram cloneImpl() const {
1300 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1301 getFile(), getLine(), getType(), isLocalToUnit(),
1302 isDefinition(), getScopeLine(), getContainingType(),
1303 getVirtuality(), getVirtualIndex(), getFlags(),
1304 isOptimized(), getFunctionConstant(),
1305 getTemplateParams(), getDeclaration(), getVariables());
1309 DEFINE_MDNODE_GET(DISubprogram,
1310 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1311 DIFile *File, unsigned Line, DISubroutineType *Type,
1312 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1313 DITypeRef ContainingType, unsigned Virtuality,
1314 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1315 Constant *Function = nullptr,
1316 DITemplateParameterArray TemplateParams = nullptr,
1317 DISubprogram *Declaration = nullptr,
1318 DILocalVariableArray Variables = nullptr),
1319 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1320 IsDefinition, ScopeLine, ContainingType, Virtuality,
1321 VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1322 Declaration, Variables))
1325 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1326 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1327 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1328 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1329 Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1330 Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1331 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1332 ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1333 Function, TemplateParams, Declaration, Variables))
1335 TempDISubprogram clone() const { return cloneImpl(); }
1338 unsigned getLine() const { return Line; }
1339 unsigned getVirtuality() const { return Virtuality; }
1340 unsigned getVirtualIndex() const { return VirtualIndex; }
1341 unsigned getScopeLine() const { return ScopeLine; }
1342 unsigned getFlags() const { return Flags; }
1343 bool isLocalToUnit() const { return IsLocalToUnit; }
1344 bool isDefinition() const { return IsDefinition; }
1345 bool isOptimized() const { return IsOptimized; }
1347 unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1348 bool isPrivate() const {
1349 return (getFlags() & FlagAccessibility) == FlagPrivate;
1351 bool isProtected() const {
1352 return (getFlags() & FlagAccessibility) == FlagProtected;
1354 bool isPublic() const {
1355 return (getFlags() & FlagAccessibility) == FlagPublic;
1357 bool isExplicit() const { return getFlags() & FlagExplicit; }
1358 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1360 /// \brief Check if this is reference-qualified.
1362 /// Return true if this subprogram is a C++11 reference-qualified non-static
1363 /// member function (void foo() &).
1364 unsigned isLValueReference() const {
1365 return getFlags() & FlagLValueReference;
1368 /// \brief Check if this is rvalue-reference-qualified.
1370 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1371 /// non-static member function (void foo() &&).
1372 unsigned isRValueReference() const {
1373 return getFlags() & FlagRValueReference;
1376 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1378 StringRef getName() const { return getStringOperand(2); }
1379 StringRef getDisplayName() const { return getStringOperand(3); }
1380 StringRef getLinkageName() const { return getStringOperand(4); }
1382 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1383 MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1385 DISubroutineType *getType() const {
1386 return cast_or_null<DISubroutineType>(getRawType());
1388 DITypeRef getContainingType() const {
1389 return DITypeRef(getRawContainingType());
1392 Constant *getFunctionConstant() const {
1393 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1394 return C->getValue();
1397 DITemplateParameterArray getTemplateParams() const {
1398 return cast_or_null<MDTuple>(getRawTemplateParams());
1400 DISubprogram *getDeclaration() const {
1401 return cast_or_null<DISubprogram>(getRawDeclaration());
1403 DILocalVariableArray getVariables() const {
1404 return cast_or_null<MDTuple>(getRawVariables());
1407 Metadata *getRawScope() const { return getOperand(1); }
1408 Metadata *getRawType() const { return getOperand(5); }
1409 Metadata *getRawContainingType() const { return getOperand(6); }
1410 Metadata *getRawFunction() const { return getOperand(7); }
1411 Metadata *getRawTemplateParams() const { return getOperand(8); }
1412 Metadata *getRawDeclaration() const { return getOperand(9); }
1413 Metadata *getRawVariables() const { return getOperand(10); }
1415 /// \brief Get a pointer to the function this subprogram describes.
1417 /// This dyn_casts \a getFunctionConstant() to \a Function.
1419 /// FIXME: Should this be looking through bitcasts?
1420 Function *getFunction() const;
1422 /// \brief Replace the function.
1424 /// If \a isUniqued() and not \a isResolved(), this could node will be
1425 /// RAUW'ed and deleted out from under the caller. Use a \a TrackingMDRef if
1426 /// that's a problem.
1428 void replaceFunction(Function *F);
1429 void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1430 void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1433 /// \brief Check if this subprogram decribes the given function.
1435 /// FIXME: Should this be looking through bitcasts?
1436 bool describes(const Function *F) const;
1438 static bool classof(const Metadata *MD) {
1439 return MD->getMetadataID() == DISubprogramKind;
1443 class DILexicalBlockBase : public DILocalScope {
1445 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1446 ArrayRef<Metadata *> Ops)
1447 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1448 ~DILexicalBlockBase() = default;
1451 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1453 Metadata *getRawScope() const { return getOperand(1); }
1455 /// \brief Forwarding accessors to LexicalBlock.
1457 /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1459 inline unsigned getLine() const;
1460 inline unsigned getColumn() const;
1462 static bool classof(const Metadata *MD) {
1463 return MD->getMetadataID() == DILexicalBlockKind ||
1464 MD->getMetadataID() == DILexicalBlockFileKind;
1468 class DILexicalBlock : public DILexicalBlockBase {
1469 friend class LLVMContextImpl;
1470 friend class MDNode;
1475 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1476 unsigned Column, ArrayRef<Metadata *> Ops)
1477 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1479 ~DILexicalBlock() = default;
1481 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1482 DIFile *File, unsigned Line, unsigned Column,
1483 StorageType Storage,
1484 bool ShouldCreate = true) {
1485 return getImpl(Context, static_cast<Metadata *>(Scope),
1486 static_cast<Metadata *>(File), Line, Column, Storage,
1490 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1491 Metadata *File, unsigned Line, unsigned Column,
1492 StorageType Storage, bool ShouldCreate = true);
1494 TempDILexicalBlock cloneImpl() const {
1495 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1500 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1501 unsigned Line, unsigned Column),
1502 (Scope, File, Line, Column))
1503 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1504 unsigned Line, unsigned Column),
1505 (Scope, File, Line, Column))
1507 TempDILexicalBlock clone() const { return cloneImpl(); }
1509 unsigned getLine() const { return Line; }
1510 unsigned getColumn() const { return Column; }
1512 static bool classof(const Metadata *MD) {
1513 return MD->getMetadataID() == DILexicalBlockKind;
1517 unsigned DILexicalBlockBase::getLine() const {
1518 if (auto *N = dyn_cast<DILexicalBlock>(this))
1519 return N->getLine();
1523 unsigned DILexicalBlockBase::getColumn() const {
1524 if (auto *N = dyn_cast<DILexicalBlock>(this))
1525 return N->getColumn();
1529 class DILexicalBlockFile : public DILexicalBlockBase {
1530 friend class LLVMContextImpl;
1531 friend class MDNode;
1533 unsigned Discriminator;
1535 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1536 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1537 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1538 Discriminator(Discriminator) {}
1539 ~DILexicalBlockFile() = default;
1541 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1542 DIFile *File, unsigned Discriminator,
1543 StorageType Storage,
1544 bool ShouldCreate = true) {
1545 return getImpl(Context, static_cast<Metadata *>(Scope),
1546 static_cast<Metadata *>(File), Discriminator, Storage,
1550 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1551 Metadata *File, unsigned Discriminator,
1552 StorageType Storage,
1553 bool ShouldCreate = true);
1555 TempDILexicalBlockFile cloneImpl() const {
1556 return getTemporary(getContext(), getScope(), getFile(),
1557 getDiscriminator());
1561 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1562 unsigned Discriminator),
1563 (Scope, File, Discriminator))
1564 DEFINE_MDNODE_GET(DILexicalBlockFile,
1565 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1566 (Scope, File, Discriminator))
1568 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1570 // TODO: Remove these once they're gone from DILexicalBlockBase.
1571 unsigned getLine() const = delete;
1572 unsigned getColumn() const = delete;
1574 unsigned getDiscriminator() const { return Discriminator; }
1576 static bool classof(const Metadata *MD) {
1577 return MD->getMetadataID() == DILexicalBlockFileKind;
1581 unsigned DILocation::getDiscriminator() const {
1582 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1583 return F->getDiscriminator();
1587 class DINamespace : public DIScope {
1588 friend class LLVMContextImpl;
1589 friend class MDNode;
1593 DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1594 ArrayRef<Metadata *> Ops)
1595 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1598 ~DINamespace() = default;
1600 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1601 DIFile *File, StringRef Name, unsigned Line,
1602 StorageType Storage, bool ShouldCreate = true) {
1603 return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1604 Line, Storage, ShouldCreate);
1606 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1607 Metadata *File, MDString *Name, unsigned Line,
1608 StorageType Storage, bool ShouldCreate = true);
1610 TempDINamespace cloneImpl() const {
1611 return getTemporary(getContext(), getScope(), getFile(), getName(),
1616 DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1618 (Scope, File, Name, Line))
1619 DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1620 MDString *Name, unsigned Line),
1621 (Scope, File, Name, Line))
1623 TempDINamespace clone() const { return cloneImpl(); }
1625 unsigned getLine() const { return Line; }
1626 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1627 StringRef getName() const { return getStringOperand(2); }
1629 Metadata *getRawScope() const { return getOperand(1); }
1630 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1632 static bool classof(const Metadata *MD) {
1633 return MD->getMetadataID() == DINamespaceKind;
1637 /// \brief A (clang) module that has been imported by the compile unit.
1639 class DIModule : public DIScope {
1640 friend class LLVMContextImpl;
1641 friend class MDNode;
1643 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
1644 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
1647 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
1648 StringRef Name, StringRef ConfigurationMacros,
1649 StringRef IncludePath, StringRef ISysRoot,
1650 StorageType Storage, bool ShouldCreate = true) {
1651 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1652 getCanonicalMDString(Context, ConfigurationMacros),
1653 getCanonicalMDString(Context, IncludePath),
1654 getCanonicalMDString(Context, ISysRoot),
1655 Storage, ShouldCreate);
1657 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
1658 MDString *Name, MDString *ConfigurationMacros,
1659 MDString *IncludePath, MDString *ISysRoot,
1660 StorageType Storage, bool ShouldCreate = true);
1662 TempDIModule cloneImpl() const {
1663 return getTemporary(getContext(), getScope(), getName(),
1664 getConfigurationMacros(), getIncludePath(),
1669 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
1670 StringRef ConfigurationMacros, StringRef IncludePath,
1671 StringRef ISysRoot),
1672 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1673 DEFINE_MDNODE_GET(DIModule,
1674 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
1675 MDString *IncludePath, MDString *ISysRoot),
1676 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
1678 TempDIModule clone() const { return cloneImpl(); }
1680 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1681 StringRef getName() const { return getStringOperand(1); }
1682 StringRef getConfigurationMacros() const { return getStringOperand(2); }
1683 StringRef getIncludePath() const { return getStringOperand(3); }
1684 StringRef getISysRoot() const { return getStringOperand(4); }
1686 Metadata *getRawScope() const { return getOperand(0); }
1687 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1688 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
1689 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
1690 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
1692 static bool classof(const Metadata *MD) {
1693 return MD->getMetadataID() == DIModuleKind;
1697 /// \brief Base class for template parameters.
1698 class DITemplateParameter : public DINode {
1700 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1701 unsigned Tag, ArrayRef<Metadata *> Ops)
1702 : DINode(Context, ID, Storage, Tag, Ops) {}
1703 ~DITemplateParameter() = default;
1706 StringRef getName() const { return getStringOperand(0); }
1707 DITypeRef getType() const { return DITypeRef(getRawType()); }
1709 MDString *getRawName() const { return getOperandAs<MDString>(0); }
1710 Metadata *getRawType() const { return getOperand(1); }
1712 static bool classof(const Metadata *MD) {
1713 return MD->getMetadataID() == DITemplateTypeParameterKind ||
1714 MD->getMetadataID() == DITemplateValueParameterKind;
1718 class DITemplateTypeParameter : public DITemplateParameter {
1719 friend class LLVMContextImpl;
1720 friend class MDNode;
1722 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1723 ArrayRef<Metadata *> Ops)
1724 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1725 dwarf::DW_TAG_template_type_parameter, Ops) {}
1726 ~DITemplateTypeParameter() = default;
1728 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1729 DITypeRef Type, StorageType Storage,
1730 bool ShouldCreate = true) {
1731 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1734 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1735 Metadata *Type, StorageType Storage,
1736 bool ShouldCreate = true);
1738 TempDITemplateTypeParameter cloneImpl() const {
1739 return getTemporary(getContext(), getName(), getType());
1743 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1745 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1748 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1750 static bool classof(const Metadata *MD) {
1751 return MD->getMetadataID() == DITemplateTypeParameterKind;
1755 class DITemplateValueParameter : public DITemplateParameter {
1756 friend class LLVMContextImpl;
1757 friend class MDNode;
1759 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1760 unsigned Tag, ArrayRef<Metadata *> Ops)
1761 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1763 ~DITemplateValueParameter() = default;
1765 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1766 StringRef Name, DITypeRef Type,
1767 Metadata *Value, StorageType Storage,
1768 bool ShouldCreate = true) {
1769 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1770 Value, Storage, ShouldCreate);
1772 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1773 MDString *Name, Metadata *Type,
1774 Metadata *Value, StorageType Storage,
1775 bool ShouldCreate = true);
1777 TempDITemplateValueParameter cloneImpl() const {
1778 return getTemporary(getContext(), getTag(), getName(), getType(),
1783 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1784 DITypeRef Type, Metadata *Value),
1785 (Tag, Name, Type, Value))
1786 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1787 Metadata *Type, Metadata *Value),
1788 (Tag, Name, Type, Value))
1790 TempDITemplateValueParameter clone() const { return cloneImpl(); }
1792 Metadata *getValue() const { return getOperand(2); }
1794 static bool classof(const Metadata *MD) {
1795 return MD->getMetadataID() == DITemplateValueParameterKind;
1799 /// \brief Base class for variables.
1801 /// TODO: Hardcode to DW_TAG_variable.
1802 class DIVariable : public DINode {
1806 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1807 unsigned Line, ArrayRef<Metadata *> Ops)
1808 : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1809 ~DIVariable() = default;
1812 unsigned getLine() const { return Line; }
1813 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1814 StringRef getName() const { return getStringOperand(1); }
1815 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1816 DITypeRef getType() const { return DITypeRef(getRawType()); }
1818 StringRef getFilename() const {
1819 if (auto *F = getFile())
1820 return F->getFilename();
1823 StringRef getDirectory() const {
1824 if (auto *F = getFile())
1825 return F->getDirectory();
1829 Metadata *getRawScope() const { return getOperand(0); }
1830 MDString *getRawName() const { return getOperandAs<MDString>(1); }
1831 Metadata *getRawFile() const { return getOperand(2); }
1832 Metadata *getRawType() const { return getOperand(3); }
1834 static bool classof(const Metadata *MD) {
1835 return MD->getMetadataID() == DILocalVariableKind ||
1836 MD->getMetadataID() == DIGlobalVariableKind;
1840 /// \brief Global variables.
1842 /// TODO: Remove DisplayName. It's always equal to Name.
1843 class DIGlobalVariable : public DIVariable {
1844 friend class LLVMContextImpl;
1845 friend class MDNode;
1850 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1851 bool IsLocalToUnit, bool IsDefinition,
1852 ArrayRef<Metadata *> Ops)
1853 : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1855 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1856 ~DIGlobalVariable() = default;
1858 static DIGlobalVariable *
1859 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1860 StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1861 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1862 DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1863 bool ShouldCreate = true) {
1864 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1865 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1866 IsLocalToUnit, IsDefinition,
1867 Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1868 StaticDataMemberDeclaration, Storage, ShouldCreate);
1870 static DIGlobalVariable *
1871 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1872 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1873 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1874 Metadata *StaticDataMemberDeclaration, StorageType Storage,
1875 bool ShouldCreate = true);
1877 TempDIGlobalVariable cloneImpl() const {
1878 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1879 getFile(), getLine(), getType(), isLocalToUnit(),
1880 isDefinition(), getVariable(),
1881 getStaticDataMemberDeclaration());
1885 DEFINE_MDNODE_GET(DIGlobalVariable,
1886 (DIScope * Scope, StringRef Name, StringRef LinkageName,
1887 DIFile *File, unsigned Line, DITypeRef Type,
1888 bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1889 DIDerivedType *StaticDataMemberDeclaration),
1890 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1891 IsDefinition, Variable, StaticDataMemberDeclaration))
1892 DEFINE_MDNODE_GET(DIGlobalVariable,
1893 (Metadata * Scope, MDString *Name, MDString *LinkageName,
1894 Metadata *File, unsigned Line, Metadata *Type,
1895 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1896 Metadata *StaticDataMemberDeclaration),
1897 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1898 IsDefinition, Variable, StaticDataMemberDeclaration))
1900 TempDIGlobalVariable clone() const { return cloneImpl(); }
1902 bool isLocalToUnit() const { return IsLocalToUnit; }
1903 bool isDefinition() const { return IsDefinition; }
1904 StringRef getDisplayName() const { return getStringOperand(4); }
1905 StringRef getLinkageName() const { return getStringOperand(5); }
1906 Constant *getVariable() const {
1907 if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1908 return dyn_cast<Constant>(C->getValue());
1911 DIDerivedType *getStaticDataMemberDeclaration() const {
1912 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1915 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1916 Metadata *getRawVariable() const { return getOperand(6); }
1917 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1919 static bool classof(const Metadata *MD) {
1920 return MD->getMetadataID() == DIGlobalVariableKind;
1924 /// \brief Local variable.
1926 /// TODO: Split between arguments and otherwise.
1927 /// TODO: Use \c DW_TAG_variable instead of fake tags.
1928 /// TODO: Split up flags.
1929 class DILocalVariable : public DIVariable {
1930 friend class LLVMContextImpl;
1931 friend class MDNode;
1936 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1937 unsigned Line, unsigned Arg, unsigned Flags,
1938 ArrayRef<Metadata *> Ops)
1939 : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1941 ~DILocalVariable() = default;
1943 static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1944 DIScope *Scope, StringRef Name, DIFile *File,
1945 unsigned Line, DITypeRef Type, unsigned Arg,
1946 unsigned Flags, StorageType Storage,
1947 bool ShouldCreate = true) {
1948 return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1949 File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1951 static DILocalVariable *
1952 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1953 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1954 unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1956 TempDILocalVariable cloneImpl() const {
1957 return getTemporary(getContext(), getTag(), getScope(), getName(),
1958 getFile(), getLine(), getType(), getArg(), getFlags());
1962 DEFINE_MDNODE_GET(DILocalVariable,
1963 (unsigned Tag, DILocalScope *Scope, StringRef Name,
1964 DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1966 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1967 DEFINE_MDNODE_GET(DILocalVariable,
1968 (unsigned Tag, Metadata *Scope, MDString *Name,
1969 Metadata *File, unsigned Line, Metadata *Type,
1970 unsigned Arg, unsigned Flags),
1971 (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1973 TempDILocalVariable clone() const { return cloneImpl(); }
1975 /// \brief Get the local scope for this variable.
1977 /// Variables must be defined in a local scope.
1978 DILocalScope *getScope() const {
1979 return cast<DILocalScope>(DIVariable::getScope());
1982 unsigned getArg() const { return Arg; }
1983 unsigned getFlags() const { return Flags; }
1985 bool isArtificial() const { return getFlags() & FlagArtificial; }
1986 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1988 /// \brief Check that a location is valid for this variable.
1990 /// Check that \c DL exists, is in the same subprogram, and has the same
1991 /// inlined-at location as \c this. (Otherwise, it's not a valid attachemnt
1992 /// to a \a DbgInfoIntrinsic.)
1993 bool isValidLocationForIntrinsic(const DILocation *DL) const {
1994 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1997 static bool classof(const Metadata *MD) {
1998 return MD->getMetadataID() == DILocalVariableKind;
2002 /// \brief DWARF expression.
2004 /// This is (almost) a DWARF expression that modifies the location of a
2005 /// variable or (or the location of a single piece of a variable).
2007 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
2008 /// and have DW_OP_plus consume the topmost elements on the stack.
2010 /// TODO: Co-allocate the expression elements.
2011 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2013 class DIExpression : public MDNode {
2014 friend class LLVMContextImpl;
2015 friend class MDNode;
2017 std::vector<uint64_t> Elements;
2019 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2020 : MDNode(C, DIExpressionKind, Storage, None),
2021 Elements(Elements.begin(), Elements.end()) {}
2022 ~DIExpression() = default;
2024 static DIExpression *getImpl(LLVMContext &Context,
2025 ArrayRef<uint64_t> Elements, StorageType Storage,
2026 bool ShouldCreate = true);
2028 TempDIExpression cloneImpl() const {
2029 return getTemporary(getContext(), getElements());
2033 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2035 TempDIExpression clone() const { return cloneImpl(); }
2037 ArrayRef<uint64_t> getElements() const { return Elements; }
2039 unsigned getNumElements() const { return Elements.size(); }
2040 uint64_t getElement(unsigned I) const {
2041 assert(I < Elements.size() && "Index out of range");
2045 /// \brief Return whether this is a piece of an aggregate variable.
2046 bool isBitPiece() const;
2048 /// \brief Return the offset of this piece in bits.
2049 uint64_t getBitPieceOffset() const;
2051 /// \brief Return the size of this piece in bits.
2052 uint64_t getBitPieceSize() const;
2054 typedef ArrayRef<uint64_t>::iterator element_iterator;
2055 element_iterator elements_begin() const { return getElements().begin(); }
2056 element_iterator elements_end() const { return getElements().end(); }
2058 /// \brief A lightweight wrapper around an expression operand.
2060 /// TODO: Store arguments directly and change \a DIExpression to store a
2066 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2068 const uint64_t *get() const { return Op; }
2070 /// \brief Get the operand code.
2071 uint64_t getOp() const { return *Op; }
2073 /// \brief Get an argument to the operand.
2075 /// Never returns the operand itself.
2076 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2078 unsigned getNumArgs() const { return getSize() - 1; }
2080 /// \brief Return the size of the operand.
2082 /// Return the number of elements in the operand (1 + args).
2083 unsigned getSize() const;
2086 /// \brief An iterator for expression operands.
2087 class expr_op_iterator
2088 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2092 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2094 element_iterator getBase() const { return Op.get(); }
2095 const ExprOperand &operator*() const { return Op; }
2096 const ExprOperand *operator->() const { return &Op; }
2098 expr_op_iterator &operator++() {
2102 expr_op_iterator operator++(int) {
2103 expr_op_iterator T(*this);
2108 /// \brief Get the next iterator.
2110 /// \a std::next() doesn't work because this is technically an
2111 /// input_iterator, but it's a perfectly valid operation. This is an
2112 /// accessor to provide the same functionality.
2113 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2115 bool operator==(const expr_op_iterator &X) const {
2116 return getBase() == X.getBase();
2118 bool operator!=(const expr_op_iterator &X) const {
2119 return getBase() != X.getBase();
2123 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2126 /// \brief Visit the elements via ExprOperand wrappers.
2128 /// These range iterators visit elements through \a ExprOperand wrappers.
2129 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2132 /// \pre \a isValid() gives \c true.
2134 expr_op_iterator expr_op_begin() const {
2135 return expr_op_iterator(elements_begin());
2137 expr_op_iterator expr_op_end() const {
2138 return expr_op_iterator(elements_end());
2142 bool isValid() const;
2144 static bool classof(const Metadata *MD) {
2145 return MD->getMetadataID() == DIExpressionKind;
2149 class DIObjCProperty : public DINode {
2150 friend class LLVMContextImpl;
2151 friend class MDNode;
2154 unsigned Attributes;
2156 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2157 unsigned Attributes, ArrayRef<Metadata *> Ops)
2158 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2160 Line(Line), Attributes(Attributes) {}
2161 ~DIObjCProperty() = default;
2163 static DIObjCProperty *
2164 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2165 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2166 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2167 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2168 getCanonicalMDString(Context, GetterName),
2169 getCanonicalMDString(Context, SetterName), Attributes, Type,
2170 Storage, ShouldCreate);
2172 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2173 Metadata *File, unsigned Line,
2174 MDString *GetterName, MDString *SetterName,
2175 unsigned Attributes, Metadata *Type,
2176 StorageType Storage, bool ShouldCreate = true);
2178 TempDIObjCProperty cloneImpl() const {
2179 return getTemporary(getContext(), getName(), getFile(), getLine(),
2180 getGetterName(), getSetterName(), getAttributes(),
2185 DEFINE_MDNODE_GET(DIObjCProperty,
2186 (StringRef Name, DIFile *File, unsigned Line,
2187 StringRef GetterName, StringRef SetterName,
2188 unsigned Attributes, DITypeRef Type),
2189 (Name, File, Line, GetterName, SetterName, Attributes,
2191 DEFINE_MDNODE_GET(DIObjCProperty,
2192 (MDString * Name, Metadata *File, unsigned Line,
2193 MDString *GetterName, MDString *SetterName,
2194 unsigned Attributes, Metadata *Type),
2195 (Name, File, Line, GetterName, SetterName, Attributes,
2198 TempDIObjCProperty clone() const { return cloneImpl(); }
2200 unsigned getLine() const { return Line; }
2201 unsigned getAttributes() const { return Attributes; }
2202 StringRef getName() const { return getStringOperand(0); }
2203 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2204 StringRef getGetterName() const { return getStringOperand(2); }
2205 StringRef getSetterName() const { return getStringOperand(3); }
2206 DITypeRef getType() const { return DITypeRef(getRawType()); }
2208 StringRef getFilename() const {
2209 if (auto *F = getFile())
2210 return F->getFilename();
2213 StringRef getDirectory() const {
2214 if (auto *F = getFile())
2215 return F->getDirectory();
2219 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2220 Metadata *getRawFile() const { return getOperand(1); }
2221 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2222 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2223 Metadata *getRawType() const { return getOperand(4); }
2225 static bool classof(const Metadata *MD) {
2226 return MD->getMetadataID() == DIObjCPropertyKind;
2230 /// \brief An imported module (C++ using directive or similar).
2231 class DIImportedEntity : public DINode {
2232 friend class LLVMContextImpl;
2233 friend class MDNode;
2237 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2238 unsigned Line, ArrayRef<Metadata *> Ops)
2239 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2240 ~DIImportedEntity() = default;
2242 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2243 DIScope *Scope, DINodeRef Entity,
2244 unsigned Line, StringRef Name,
2245 StorageType Storage,
2246 bool ShouldCreate = true) {
2247 return getImpl(Context, Tag, Scope, Entity, Line,
2248 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2250 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2251 Metadata *Scope, Metadata *Entity,
2252 unsigned Line, MDString *Name,
2253 StorageType Storage,
2254 bool ShouldCreate = true);
2256 TempDIImportedEntity cloneImpl() const {
2257 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2258 getLine(), getName());
2262 DEFINE_MDNODE_GET(DIImportedEntity,
2263 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2264 unsigned Line, StringRef Name = ""),
2265 (Tag, Scope, Entity, Line, Name))
2266 DEFINE_MDNODE_GET(DIImportedEntity,
2267 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2268 unsigned Line, MDString *Name),
2269 (Tag, Scope, Entity, Line, Name))
2271 TempDIImportedEntity clone() const { return cloneImpl(); }
2273 unsigned getLine() const { return Line; }
2274 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2275 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2276 StringRef getName() const { return getStringOperand(2); }
2278 Metadata *getRawScope() const { return getOperand(0); }
2279 Metadata *getRawEntity() const { return getOperand(1); }
2280 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2282 static bool classof(const Metadata *MD) {
2283 return MD->getMetadataID() == DIImportedEntityKind;
2287 } // end namespace llvm
2289 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2290 #undef DEFINE_MDNODE_GET_UNPACK
2291 #undef DEFINE_MDNODE_GET