1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Declarations for metadata specific to debug info.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
14 #define LLVM_IR_DEBUGINFOMETADATA_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/BitmaskEnum.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/BinaryFormat/Dwarf.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/Metadata.h"
28 #include "llvm/Support/Casting.h"
34 #include <type_traits>
37 // Helper macros for defining get() overrides.
38 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
39 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
40 #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
41 static CLASS *getDistinct(LLVMContext &Context, \
42 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
43 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
45 static Temp##CLASS getTemporary(LLVMContext &Context, \
46 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
48 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
50 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
51 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
52 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
54 static CLASS *getIfExists(LLVMContext &Context, \
55 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
56 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
57 /* ShouldCreate */ false); \
59 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
63 class DITypeRefArray {
64 const MDTuple *N = nullptr;
67 DITypeRefArray() = default;
68 DITypeRefArray(const MDTuple *N) : N(N) {}
70 explicit operator bool() const { return get(); }
71 explicit operator MDTuple *() const { return get(); }
73 MDTuple *get() const { return const_cast<MDTuple *>(N); }
74 MDTuple *operator->() const { return get(); }
75 MDTuple &operator*() const { return *get(); }
77 // FIXME: Fix callers and remove condition on N.
78 unsigned size() const { return N ? N->getNumOperands() : 0u; }
79 DIType *operator[](unsigned I) const {
80 return cast_or_null<DIType>(N->getOperand(I));
83 class iterator : std::iterator<std::input_iterator_tag, DIType *,
84 std::ptrdiff_t, void, DIType *> {
85 MDNode::op_iterator I = nullptr;
89 explicit iterator(MDNode::op_iterator I) : I(I) {}
91 DIType *operator*() const { return cast_or_null<DIType>(*I); }
93 iterator &operator++() {
98 iterator operator++(int) {
104 bool operator==(const iterator &X) const { return I == X.I; }
105 bool operator!=(const iterator &X) const { return I != X.I; }
108 // FIXME: Fix callers and remove condition on N.
109 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
110 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
113 /// Tagged DWARF-like metadata node.
115 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
116 /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
117 /// potentially used for non-DWARF output.
118 class DINode : public MDNode {
119 friend class LLVMContextImpl;
123 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
124 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
125 : MDNode(C, ID, Storage, Ops1, Ops2) {
126 assert(Tag < 1u << 16);
127 SubclassData16 = Tag;
131 template <class Ty> Ty *getOperandAs(unsigned I) const {
132 return cast_or_null<Ty>(getOperand(I));
135 StringRef getStringOperand(unsigned I) const {
136 if (auto *S = getOperandAs<MDString>(I))
137 return S->getString();
141 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
144 return MDString::get(Context, S);
147 /// Allow subclasses to mutate the tag.
148 void setTag(unsigned Tag) { SubclassData16 = Tag; }
151 unsigned getTag() const { return SubclassData16; }
153 /// Debug info flags.
155 /// The three accessibility flags are mutually exclusive and rolled together
156 /// in the first two bits.
157 enum DIFlags : uint32_t {
158 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
159 #define DI_FLAG_LARGEST_NEEDED
160 #include "llvm/IR/DebugInfoFlags.def"
161 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
162 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
163 FlagVirtualInheritance,
164 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
167 static DIFlags getFlag(StringRef Flag);
168 static StringRef getFlagString(DIFlags Flag);
170 /// Split up a flags bitfield.
172 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
173 /// any remaining (unrecognized) bits.
174 static DIFlags splitFlags(DIFlags Flags,
175 SmallVectorImpl<DIFlags> &SplitFlags);
177 static bool classof(const Metadata *MD) {
178 switch (MD->getMetadataID()) {
181 case GenericDINodeKind:
183 case DIEnumeratorKind:
184 case DIBasicTypeKind:
185 case DIDerivedTypeKind:
186 case DICompositeTypeKind:
187 case DISubroutineTypeKind:
189 case DICompileUnitKind:
190 case DISubprogramKind:
191 case DILexicalBlockKind:
192 case DILexicalBlockFileKind:
193 case DINamespaceKind:
194 case DICommonBlockKind:
195 case DITemplateTypeParameterKind:
196 case DITemplateValueParameterKind:
197 case DIGlobalVariableKind:
198 case DILocalVariableKind:
200 case DIObjCPropertyKind:
201 case DIImportedEntityKind:
208 /// Generic tagged DWARF-like metadata node.
210 /// An un-specialized DWARF-like metadata node. The first operand is a
211 /// (possibly empty) null-separated \a MDString header that contains arbitrary
212 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
213 /// to other metadata.
214 class GenericDINode : public DINode {
215 friend class LLVMContextImpl;
218 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
219 unsigned Tag, ArrayRef<Metadata *> Ops1,
220 ArrayRef<Metadata *> Ops2)
221 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
224 ~GenericDINode() { dropAllReferences(); }
226 void setHash(unsigned Hash) { SubclassData32 = Hash; }
227 void recalculateHash();
229 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
230 StringRef Header, ArrayRef<Metadata *> DwarfOps,
231 StorageType Storage, bool ShouldCreate = true) {
232 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
233 DwarfOps, Storage, ShouldCreate);
236 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
237 MDString *Header, ArrayRef<Metadata *> DwarfOps,
238 StorageType Storage, bool ShouldCreate = true);
240 TempGenericDINode cloneImpl() const {
242 getContext(), getTag(), getHeader(),
243 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
247 unsigned getHash() const { return SubclassData32; }
249 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
250 ArrayRef<Metadata *> DwarfOps),
251 (Tag, Header, DwarfOps))
252 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
253 ArrayRef<Metadata *> DwarfOps),
254 (Tag, Header, DwarfOps))
256 /// Return a (temporary) clone of this.
257 TempGenericDINode clone() const { return cloneImpl(); }
259 unsigned getTag() const { return SubclassData16; }
260 StringRef getHeader() const { return getStringOperand(0); }
261 MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
263 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
264 op_iterator dwarf_op_end() const { return op_end(); }
265 op_range dwarf_operands() const {
266 return op_range(dwarf_op_begin(), dwarf_op_end());
269 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
270 const MDOperand &getDwarfOperand(unsigned I) const {
271 return getOperand(I + 1);
273 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
274 replaceOperandWith(I + 1, New);
277 static bool classof(const Metadata *MD) {
278 return MD->getMetadataID() == GenericDINodeKind;
284 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
286 class DISubrange : public DINode {
287 friend class LLVMContextImpl;
292 DISubrange(LLVMContext &C, StorageType Storage, Metadata *Node,
293 int64_t LowerBound, ArrayRef<Metadata *> Ops)
294 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops),
295 LowerBound(LowerBound) {}
297 ~DISubrange() = default;
299 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
300 int64_t LowerBound, StorageType Storage,
301 bool ShouldCreate = true);
303 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
304 int64_t LowerBound, StorageType Storage,
305 bool ShouldCreate = true);
307 TempDISubrange cloneImpl() const {
308 return getTemporary(getContext(), getRawCountNode(), getLowerBound());
312 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
315 DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
316 (CountNode, LowerBound))
318 TempDISubrange clone() const { return cloneImpl(); }
320 int64_t getLowerBound() const { return LowerBound; }
322 Metadata *getRawCountNode() const {
323 return getOperand(0).get();
326 typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
328 CountType getCount() const {
329 if (auto *MD = dyn_cast<ConstantAsMetadata>(getRawCountNode()))
330 return CountType(cast<ConstantInt>(MD->getValue()));
332 if (auto *DV = dyn_cast<DIVariable>(getRawCountNode()))
333 return CountType(DV);
338 static bool classof(const Metadata *MD) {
339 return MD->getMetadataID() == DISubrangeKind;
343 /// Enumeration value.
345 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
346 /// longer creates a type cycle.
347 class DIEnumerator : public DINode {
348 friend class LLVMContextImpl;
352 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
353 bool IsUnsigned, ArrayRef<Metadata *> Ops)
354 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
356 SubclassData32 = IsUnsigned;
358 ~DIEnumerator() = default;
360 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
361 bool IsUnsigned, StringRef Name,
362 StorageType Storage, bool ShouldCreate = true) {
363 return getImpl(Context, Value, IsUnsigned,
364 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
366 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
367 bool IsUnsigned, MDString *Name,
368 StorageType Storage, bool ShouldCreate = true);
370 TempDIEnumerator cloneImpl() const {
371 return getTemporary(getContext(), getValue(), isUnsigned(), getName());
375 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name),
376 (Value, IsUnsigned, Name))
377 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, MDString *Name),
378 (Value, IsUnsigned, Name))
380 TempDIEnumerator clone() const { return cloneImpl(); }
382 int64_t getValue() const { return Value; }
383 bool isUnsigned() const { return SubclassData32; }
384 StringRef getName() const { return getStringOperand(0); }
386 MDString *getRawName() const { return getOperandAs<MDString>(0); }
388 static bool classof(const Metadata *MD) {
389 return MD->getMetadataID() == DIEnumeratorKind;
393 /// Base class for scope-like contexts.
395 /// Base class for lexical scopes and types (which are also declaration
398 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
399 class DIScope : public DINode {
401 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
402 ArrayRef<Metadata *> Ops)
403 : DINode(C, ID, Storage, Tag, Ops) {}
404 ~DIScope() = default;
407 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
409 inline StringRef getFilename() const;
410 inline StringRef getDirectory() const;
411 inline Optional<StringRef> getSource() const;
413 StringRef getName() const;
414 DIScope *getScope() const;
416 /// Return the raw underlying file.
418 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
419 /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
420 /// Otherwise, return the first operand, which is where all other subclasses
421 /// store their file pointer.
422 Metadata *getRawFile() const {
423 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
424 : static_cast<Metadata *>(getOperand(0));
427 static bool classof(const Metadata *MD) {
428 switch (MD->getMetadataID()) {
431 case DIBasicTypeKind:
432 case DIDerivedTypeKind:
433 case DICompositeTypeKind:
434 case DISubroutineTypeKind:
436 case DICompileUnitKind:
437 case DISubprogramKind:
438 case DILexicalBlockKind:
439 case DILexicalBlockFileKind:
440 case DINamespaceKind:
441 case DICommonBlockKind:
450 /// TODO: Merge with directory/file node (including users).
451 /// TODO: Canonicalize paths on creation.
452 class DIFile : public DIScope {
453 friend class LLVMContextImpl;
457 /// Which algorithm (e.g. MD5) a checksum was generated with.
459 /// The encoding is explicit because it is used directly in Bitcode. The
460 /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
462 // The first variant was originally CSK_None, encoded as 0. The new
463 // internal representation removes the need for this by wrapping the
464 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
465 // encoding is reserved.
468 CSK_Last = CSK_SHA1 // Should be last enumeration.
471 /// A single checksum, represented by a \a Kind and a \a Value (a string).
472 template <typename T>
473 struct ChecksumInfo {
474 /// The kind of checksum which \a Value encodes.
476 /// The string value of the checksum.
479 ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
480 ~ChecksumInfo() = default;
481 bool operator==(const ChecksumInfo<T> &X) const {
482 return Kind == X.Kind && Value == X.Value;
484 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
485 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
489 Optional<ChecksumInfo<MDString *>> Checksum;
490 Optional<MDString *> Source;
492 DIFile(LLVMContext &C, StorageType Storage,
493 Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src,
494 ArrayRef<Metadata *> Ops)
495 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
496 Checksum(CS), Source(Src) {}
499 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
501 Optional<ChecksumInfo<StringRef>> CS,
502 Optional<StringRef> Source,
503 StorageType Storage, bool ShouldCreate = true) {
504 Optional<ChecksumInfo<MDString *>> MDChecksum;
506 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
507 return getImpl(Context, getCanonicalMDString(Context, Filename),
508 getCanonicalMDString(Context, Directory), MDChecksum,
509 Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) : None,
510 Storage, ShouldCreate);
512 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
514 Optional<ChecksumInfo<MDString *>> CS,
515 Optional<MDString *> Source, StorageType Storage,
516 bool ShouldCreate = true);
518 TempDIFile cloneImpl() const {
519 return getTemporary(getContext(), getFilename(), getDirectory(),
520 getChecksum(), getSource());
524 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
525 Optional<ChecksumInfo<StringRef>> CS = None,
526 Optional<StringRef> Source = None),
527 (Filename, Directory, CS, Source))
528 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
529 Optional<ChecksumInfo<MDString *>> CS = None,
530 Optional<MDString *> Source = None),
531 (Filename, Directory, CS, Source))
533 TempDIFile clone() const { return cloneImpl(); }
535 StringRef getFilename() const { return getStringOperand(0); }
536 StringRef getDirectory() const { return getStringOperand(1); }
537 Optional<ChecksumInfo<StringRef>> getChecksum() const {
538 Optional<ChecksumInfo<StringRef>> StringRefChecksum;
540 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
541 return StringRefChecksum;
543 Optional<StringRef> getSource() const {
544 return Source ? Optional<StringRef>((*Source)->getString()) : None;
547 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
548 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
549 Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }
550 Optional<MDString *> getRawSource() const { return Source; }
552 static StringRef getChecksumKindAsString(ChecksumKind CSKind);
553 static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
555 static bool classof(const Metadata *MD) {
556 return MD->getMetadataID() == DIFileKind;
560 StringRef DIScope::getFilename() const {
561 if (auto *F = getFile())
562 return F->getFilename();
566 StringRef DIScope::getDirectory() const {
567 if (auto *F = getFile())
568 return F->getDirectory();
572 Optional<StringRef> DIScope::getSource() const {
573 if (auto *F = getFile())
574 return F->getSource();
578 /// Base class for types.
580 /// TODO: Remove the hardcoded name and context, since many types don't use
582 /// TODO: Split up flags.
583 class DIType : public DIScope {
587 uint64_t OffsetInBits;
588 uint32_t AlignInBits;
591 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
592 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
593 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
594 : DIScope(C, ID, Storage, Tag, Ops) {
595 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
599 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
600 uint64_t OffsetInBits, DIFlags Flags) {
603 this->SizeInBits = SizeInBits;
604 this->AlignInBits = AlignInBits;
605 this->OffsetInBits = OffsetInBits;
608 /// Change fields in place.
609 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
610 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
611 assert(isDistinct() && "Only distinct nodes can mutate");
613 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
617 TempDIType clone() const {
618 return TempDIType(cast<DIType>(MDNode::clone().release()));
621 unsigned getLine() const { return Line; }
622 uint64_t getSizeInBits() const { return SizeInBits; }
623 uint32_t getAlignInBits() const { return AlignInBits; }
624 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
625 uint64_t getOffsetInBits() const { return OffsetInBits; }
626 DIFlags getFlags() const { return Flags; }
628 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
629 StringRef getName() const { return getStringOperand(2); }
632 Metadata *getRawScope() const { return getOperand(1); }
633 MDString *getRawName() const { return getOperandAs<MDString>(2); }
635 /// Returns a new temporary DIType with updated Flags
636 TempDIType cloneWithFlags(DIFlags NewFlags) const {
637 auto NewTy = clone();
638 NewTy->Flags = NewFlags;
642 bool isPrivate() const {
643 return (getFlags() & FlagAccessibility) == FlagPrivate;
645 bool isProtected() const {
646 return (getFlags() & FlagAccessibility) == FlagProtected;
648 bool isPublic() const {
649 return (getFlags() & FlagAccessibility) == FlagPublic;
651 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
652 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
653 bool isVirtual() const { return getFlags() & FlagVirtual; }
654 bool isArtificial() const { return getFlags() & FlagArtificial; }
655 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
656 bool isObjcClassComplete() const {
657 return getFlags() & FlagObjcClassComplete;
659 bool isVector() const { return getFlags() & FlagVector; }
660 bool isBitField() const { return getFlags() & FlagBitField; }
661 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
662 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
663 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
664 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
665 bool isTypePassByReference() const {
666 return getFlags() & FlagTypePassByReference;
668 bool isBigEndian() const { return getFlags() & FlagBigEndian; }
669 bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
670 bool getExportSymbols() const { return getFlags() & FlagExportSymbols; }
672 static bool classof(const Metadata *MD) {
673 switch (MD->getMetadataID()) {
676 case DIBasicTypeKind:
677 case DIDerivedTypeKind:
678 case DICompositeTypeKind:
679 case DISubroutineTypeKind:
685 /// Basic type, like 'int' or 'float'.
687 /// TODO: Split out DW_TAG_unspecified_type.
688 /// TODO: Drop unused accessors.
689 class DIBasicType : public DIType {
690 friend class LLVMContextImpl;
695 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
696 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
697 DIFlags Flags, ArrayRef<Metadata *> Ops)
698 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
700 Encoding(Encoding) {}
701 ~DIBasicType() = default;
703 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
704 StringRef Name, uint64_t SizeInBits,
705 uint32_t AlignInBits, unsigned Encoding,
706 DIFlags Flags, StorageType Storage,
707 bool ShouldCreate = true) {
708 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
709 SizeInBits, AlignInBits, Encoding, Flags, Storage,
712 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
713 MDString *Name, uint64_t SizeInBits,
714 uint32_t AlignInBits, unsigned Encoding,
715 DIFlags Flags, StorageType Storage,
716 bool ShouldCreate = true);
718 TempDIBasicType cloneImpl() const {
719 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
720 getAlignInBits(), getEncoding(), getFlags());
724 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
725 (Tag, Name, 0, 0, 0, FlagZero))
726 DEFINE_MDNODE_GET(DIBasicType,
727 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
728 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
729 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
730 DEFINE_MDNODE_GET(DIBasicType,
731 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
732 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
733 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
735 TempDIBasicType clone() const { return cloneImpl(); }
737 unsigned getEncoding() const { return Encoding; }
739 enum class Signedness { Signed, Unsigned };
741 /// Return the signedness of this type, or None if this type is neither
742 /// signed nor unsigned.
743 Optional<Signedness> getSignedness() const;
745 static bool classof(const Metadata *MD) {
746 return MD->getMetadataID() == DIBasicTypeKind;
752 /// This includes qualified types, pointers, references, friends, typedefs, and
755 /// TODO: Split out members (inheritance, fields, methods, etc.).
756 class DIDerivedType : public DIType {
757 friend class LLVMContextImpl;
760 /// The DWARF address space of the memory pointed to or referenced by a
761 /// pointer or reference type respectively.
762 Optional<unsigned> DWARFAddressSpace;
764 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
765 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
766 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
767 DIFlags Flags, ArrayRef<Metadata *> Ops)
768 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
769 AlignInBits, OffsetInBits, Flags, Ops),
770 DWARFAddressSpace(DWARFAddressSpace) {}
771 ~DIDerivedType() = default;
773 static DIDerivedType *
774 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
775 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
776 uint32_t AlignInBits, uint64_t OffsetInBits,
777 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
778 Metadata *ExtraData, StorageType Storage, bool ShouldCreate = true) {
779 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
780 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
781 DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
783 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
784 MDString *Name, Metadata *File, unsigned Line,
785 Metadata *Scope, Metadata *BaseType,
786 uint64_t SizeInBits, uint32_t AlignInBits,
787 uint64_t OffsetInBits,
788 Optional<unsigned> DWARFAddressSpace,
789 DIFlags Flags, Metadata *ExtraData,
790 StorageType Storage, bool ShouldCreate = true);
792 TempDIDerivedType cloneImpl() const {
793 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
794 getScope(), getBaseType(), getSizeInBits(),
795 getAlignInBits(), getOffsetInBits(),
796 getDWARFAddressSpace(), getFlags(), getExtraData());
800 DEFINE_MDNODE_GET(DIDerivedType,
801 (unsigned Tag, MDString *Name, Metadata *File,
802 unsigned Line, Metadata *Scope, Metadata *BaseType,
803 uint64_t SizeInBits, uint32_t AlignInBits,
804 uint64_t OffsetInBits,
805 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
806 Metadata *ExtraData = nullptr),
807 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
808 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
810 DEFINE_MDNODE_GET(DIDerivedType,
811 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
812 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
813 uint32_t AlignInBits, uint64_t OffsetInBits,
814 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
815 Metadata *ExtraData = nullptr),
816 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
817 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
820 TempDIDerivedType clone() const { return cloneImpl(); }
822 /// Get the base type this is derived from.
823 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
824 Metadata *getRawBaseType() const { return getOperand(3); }
826 /// \returns The DWARF address space of the memory pointed to or referenced by
827 /// a pointer or reference type respectively.
828 Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
830 /// Get extra data associated with this derived type.
832 /// Class type for pointer-to-members, objective-c property node for ivars,
833 /// global constant wrapper for static members, or virtual base pointer offset
836 /// TODO: Separate out types that need this extra operand: pointer-to-member
837 /// types and member fields (static members and ivars).
838 Metadata *getExtraData() const { return getRawExtraData(); }
839 Metadata *getRawExtraData() const { return getOperand(4); }
841 /// Get casted version of extra data.
843 DIType *getClassType() const {
844 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
845 return cast_or_null<DIType>(getExtraData());
848 DIObjCProperty *getObjCProperty() const {
849 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
852 uint32_t getVBPtrOffset() const {
853 assert(getTag() == dwarf::DW_TAG_inheritance);
854 if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData()))
855 if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue()))
856 return static_cast<uint32_t>(CI->getZExtValue());
860 Constant *getStorageOffsetInBits() const {
861 assert(getTag() == dwarf::DW_TAG_member && isBitField());
862 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
863 return C->getValue();
867 Constant *getConstant() const {
868 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
869 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
870 return C->getValue();
873 Constant *getDiscriminantValue() const {
874 assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
875 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
876 return C->getValue();
881 static bool classof(const Metadata *MD) {
882 return MD->getMetadataID() == DIDerivedTypeKind;
888 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
889 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
890 class DICompositeType : public DIType {
891 friend class LLVMContextImpl;
894 unsigned RuntimeLang;
896 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
897 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
898 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
899 ArrayRef<Metadata *> Ops)
900 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
901 AlignInBits, OffsetInBits, Flags, Ops),
902 RuntimeLang(RuntimeLang) {}
903 ~DICompositeType() = default;
905 /// Change fields in place.
906 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
907 uint64_t SizeInBits, uint32_t AlignInBits,
908 uint64_t OffsetInBits, DIFlags Flags) {
909 assert(isDistinct() && "Only distinct nodes can mutate");
910 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
911 this->RuntimeLang = RuntimeLang;
912 DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
915 static DICompositeType *
916 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
917 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
918 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
919 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
920 DITemplateParameterArray TemplateParams, StringRef Identifier,
921 DIDerivedType *Discriminator, StorageType Storage,
922 bool ShouldCreate = true) {
924 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
925 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
926 RuntimeLang, VTableHolder, TemplateParams.get(),
927 getCanonicalMDString(Context, Identifier), Discriminator, Storage, ShouldCreate);
929 static DICompositeType *
930 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
931 unsigned Line, Metadata *Scope, Metadata *BaseType,
932 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
933 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
934 Metadata *VTableHolder, Metadata *TemplateParams,
935 MDString *Identifier, Metadata *Discriminator,
936 StorageType Storage, bool ShouldCreate = true);
938 TempDICompositeType cloneImpl() const {
939 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
940 getScope(), getBaseType(), getSizeInBits(),
941 getAlignInBits(), getOffsetInBits(), getFlags(),
942 getElements(), getRuntimeLang(), getVTableHolder(),
943 getTemplateParams(), getIdentifier(), getDiscriminator());
947 DEFINE_MDNODE_GET(DICompositeType,
948 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
949 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
950 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
951 DINodeArray Elements, unsigned RuntimeLang,
952 DIType *VTableHolder,
953 DITemplateParameterArray TemplateParams = nullptr,
954 StringRef Identifier = "",
955 DIDerivedType *Discriminator = nullptr),
956 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
957 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
958 VTableHolder, TemplateParams, Identifier, Discriminator))
959 DEFINE_MDNODE_GET(DICompositeType,
960 (unsigned Tag, MDString *Name, Metadata *File,
961 unsigned Line, Metadata *Scope, Metadata *BaseType,
962 uint64_t SizeInBits, uint32_t AlignInBits,
963 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
964 unsigned RuntimeLang, Metadata *VTableHolder,
965 Metadata *TemplateParams = nullptr,
966 MDString *Identifier = nullptr,
967 Metadata *Discriminator = nullptr),
968 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
969 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
970 VTableHolder, TemplateParams, Identifier, Discriminator))
972 TempDICompositeType clone() const { return cloneImpl(); }
974 /// Get a DICompositeType with the given ODR identifier.
976 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
977 /// DICompositeType for the given ODR \c Identifier. If none exists, creates
980 /// Else, returns \c nullptr.
981 static DICompositeType *
982 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
983 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
984 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
985 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
986 unsigned RuntimeLang, Metadata *VTableHolder,
987 Metadata *TemplateParams, Metadata *Discriminator);
988 static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
989 MDString &Identifier);
991 /// Build a DICompositeType with the given ODR identifier.
993 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
994 /// it doesn't exist, creates a new one. If it does exist and \a
995 /// isForwardDecl(), and the new arguments would be a definition, mutates the
996 /// the type in place. In either case, returns the type.
998 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
1000 static DICompositeType *
1001 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1002 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1003 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1004 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
1005 unsigned RuntimeLang, Metadata *VTableHolder,
1006 Metadata *TemplateParams, Metadata *Discriminator);
1008 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1009 DINodeArray getElements() const {
1010 return cast_or_null<MDTuple>(getRawElements());
1012 DIType *getVTableHolder() const {
1013 return cast_or_null<DIType>(getRawVTableHolder());
1015 DITemplateParameterArray getTemplateParams() const {
1016 return cast_or_null<MDTuple>(getRawTemplateParams());
1018 StringRef getIdentifier() const { return getStringOperand(7); }
1019 unsigned getRuntimeLang() const { return RuntimeLang; }
1021 Metadata *getRawBaseType() const { return getOperand(3); }
1022 Metadata *getRawElements() const { return getOperand(4); }
1023 Metadata *getRawVTableHolder() const { return getOperand(5); }
1024 Metadata *getRawTemplateParams() const { return getOperand(6); }
1025 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1026 Metadata *getRawDiscriminator() const { return getOperand(8); }
1027 DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
1029 /// Replace operands.
1031 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
1032 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
1033 /// of its movement if necessary.
1035 void replaceElements(DINodeArray Elements) {
1037 for (DINode *Op : getElements())
1038 assert(is_contained(Elements->operands(), Op) &&
1039 "Lost a member during member list replacement");
1041 replaceOperandWith(4, Elements.get());
1044 void replaceVTableHolder(DIType *VTableHolder) {
1045 replaceOperandWith(5, VTableHolder);
1048 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
1049 replaceOperandWith(6, TemplateParams.get());
1053 static bool classof(const Metadata *MD) {
1054 return MD->getMetadataID() == DICompositeTypeKind;
1058 /// Type array for a subprogram.
1060 /// TODO: Fold the array of types in directly as operands.
1061 class DISubroutineType : public DIType {
1062 friend class LLVMContextImpl;
1063 friend class MDNode;
1065 /// The calling convention used with DW_AT_calling_convention. Actually of
1066 /// type dwarf::CallingConvention.
1069 DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
1070 uint8_t CC, ArrayRef<Metadata *> Ops)
1071 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
1072 0, 0, 0, 0, Flags, Ops),
1074 ~DISubroutineType() = default;
1076 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1077 uint8_t CC, DITypeRefArray TypeArray,
1078 StorageType Storage,
1079 bool ShouldCreate = true) {
1080 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
1082 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1083 uint8_t CC, Metadata *TypeArray,
1084 StorageType Storage,
1085 bool ShouldCreate = true);
1087 TempDISubroutineType cloneImpl() const {
1088 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
1092 DEFINE_MDNODE_GET(DISubroutineType,
1093 (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
1094 (Flags, CC, TypeArray))
1095 DEFINE_MDNODE_GET(DISubroutineType,
1096 (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
1097 (Flags, CC, TypeArray))
1099 TempDISubroutineType clone() const { return cloneImpl(); }
1101 uint8_t getCC() const { return CC; }
1103 DITypeRefArray getTypeArray() const {
1104 return cast_or_null<MDTuple>(getRawTypeArray());
1107 Metadata *getRawTypeArray() const { return getOperand(3); }
1109 static bool classof(const Metadata *MD) {
1110 return MD->getMetadataID() == DISubroutineTypeKind;
1115 class DICompileUnit : public DIScope {
1116 friend class LLVMContextImpl;
1117 friend class MDNode;
1120 enum DebugEmissionKind : unsigned {
1124 DebugDirectivesOnly,
1125 LastEmissionKind = DebugDirectivesOnly
1128 enum class DebugNameTableKind : unsigned {
1132 LastDebugNameTableKind = None
1135 static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1136 static const char *emissionKindString(DebugEmissionKind EK);
1137 static Optional<DebugNameTableKind> getNameTableKind(StringRef Str);
1138 static const char *nameTableKindString(DebugNameTableKind PK);
1141 unsigned SourceLanguage;
1143 unsigned RuntimeVersion;
1144 unsigned EmissionKind;
1146 bool SplitDebugInlining;
1147 bool DebugInfoForProfiling;
1148 unsigned NameTableKind;
1149 bool RangesBaseAddress;
1151 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
1152 bool IsOptimized, unsigned RuntimeVersion,
1153 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
1154 bool DebugInfoForProfiling, unsigned NameTableKind,
1155 bool RangesBaseAddress, ArrayRef<Metadata *> Ops)
1156 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
1157 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
1158 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
1159 DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
1160 DebugInfoForProfiling(DebugInfoForProfiling),
1161 NameTableKind(NameTableKind), RangesBaseAddress(RangesBaseAddress) {
1162 assert(Storage != Uniqued);
1164 ~DICompileUnit() = default;
1166 static DICompileUnit *
1167 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
1168 StringRef Producer, bool IsOptimized, StringRef Flags,
1169 unsigned RuntimeVersion, StringRef SplitDebugFilename,
1170 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
1171 DIScopeArray RetainedTypes,
1172 DIGlobalVariableExpressionArray GlobalVariables,
1173 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1174 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1175 unsigned NameTableKind, bool RangesBaseAddress, StorageType Storage,
1176 bool ShouldCreate = true) {
1177 return getImpl(Context, SourceLanguage, File,
1178 getCanonicalMDString(Context, Producer), IsOptimized,
1179 getCanonicalMDString(Context, Flags), RuntimeVersion,
1180 getCanonicalMDString(Context, SplitDebugFilename),
1181 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
1182 GlobalVariables.get(), ImportedEntities.get(), Macros.get(),
1183 DWOId, SplitDebugInlining, DebugInfoForProfiling,
1184 NameTableKind, RangesBaseAddress, Storage, ShouldCreate);
1186 static DICompileUnit *
1187 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1188 MDString *Producer, bool IsOptimized, MDString *Flags,
1189 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1190 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1191 Metadata *GlobalVariables, Metadata *ImportedEntities,
1192 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
1193 bool DebugInfoForProfiling, unsigned NameTableKind,
1194 bool RangesBaseAddress, StorageType Storage, bool ShouldCreate = true);
1196 TempDICompileUnit cloneImpl() const {
1197 return getTemporary(
1198 getContext(), getSourceLanguage(), getFile(), getProducer(),
1199 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1200 getEmissionKind(), getEnumTypes(), getRetainedTypes(),
1201 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId,
1202 getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(),
1203 getRangesBaseAddress());
1207 static void get() = delete;
1208 static void getIfExists() = delete;
1210 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1212 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1213 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1214 StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
1215 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
1216 DIGlobalVariableExpressionArray GlobalVariables,
1217 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1218 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1219 DebugNameTableKind NameTableKind, bool RangesBaseAddress),
1220 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1221 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1222 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1223 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress))
1224 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1226 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1227 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1228 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1229 Metadata *RetainedTypes, Metadata *GlobalVariables,
1230 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
1231 bool SplitDebugInlining, bool DebugInfoForProfiling,
1232 unsigned NameTableKind, bool RangesBaseAddress),
1233 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1234 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1235 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1236 DebugInfoForProfiling, NameTableKind, RangesBaseAddress))
1238 TempDICompileUnit clone() const { return cloneImpl(); }
1240 unsigned getSourceLanguage() const { return SourceLanguage; }
1241 bool isOptimized() const { return IsOptimized; }
1242 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1243 DebugEmissionKind getEmissionKind() const {
1244 return (DebugEmissionKind)EmissionKind;
1246 bool isDebugDirectivesOnly() const {
1247 return EmissionKind == DebugDirectivesOnly;
1249 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
1250 DebugNameTableKind getNameTableKind() const {
1251 return (DebugNameTableKind)NameTableKind;
1253 bool getRangesBaseAddress() const {
1254 return RangesBaseAddress; }
1255 StringRef getProducer() const {
1256 return getStringOperand(1); }
1257 StringRef getFlags() const {
1258 return getStringOperand(2); }
1259 StringRef getSplitDebugFilename() const {
1260 return getStringOperand(3); }
1261 DICompositeTypeArray getEnumTypes() const {
1262 return cast_or_null<MDTuple>(getRawEnumTypes());
1264 DIScopeArray getRetainedTypes() const {
1265 return cast_or_null<MDTuple>(getRawRetainedTypes());
1267 DIGlobalVariableExpressionArray getGlobalVariables() const {
1268 return cast_or_null<MDTuple>(getRawGlobalVariables());
1270 DIImportedEntityArray getImportedEntities() const {
1271 return cast_or_null<MDTuple>(getRawImportedEntities());
1273 DIMacroNodeArray getMacros() const {
1274 return cast_or_null<MDTuple>(getRawMacros());
1276 uint64_t getDWOId() const { return DWOId; }
1277 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
1278 bool getSplitDebugInlining() const { return SplitDebugInlining; }
1279 void setSplitDebugInlining(bool SplitDebugInlining) {
1280 this->SplitDebugInlining = SplitDebugInlining;
1283 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1284 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1285 MDString *getRawSplitDebugFilename() const {
1286 return getOperandAs<MDString>(3);
1288 Metadata *getRawEnumTypes() const { return getOperand(4); }
1289 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1290 Metadata *getRawGlobalVariables() const { return getOperand(6); }
1291 Metadata *getRawImportedEntities() const { return getOperand(7); }
1292 Metadata *getRawMacros() const { return getOperand(8); }
1296 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1297 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1298 /// DICompileUnit should be fairly rare.
1300 void replaceEnumTypes(DICompositeTypeArray N) {
1301 replaceOperandWith(4, N.get());
1303 void replaceRetainedTypes(DITypeArray N) {
1304 replaceOperandWith(5, N.get());
1306 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
1307 replaceOperandWith(6, N.get());
1309 void replaceImportedEntities(DIImportedEntityArray N) {
1310 replaceOperandWith(7, N.get());
1312 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
1315 static bool classof(const Metadata *MD) {
1316 return MD->getMetadataID() == DICompileUnitKind;
1320 /// A scope for locals.
1322 /// A legal scope for lexical blocks, local variables, and debug info
1323 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1324 /// DILexicalBlockFile.
1325 class DILocalScope : public DIScope {
1327 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1328 ArrayRef<Metadata *> Ops)
1329 : DIScope(C, ID, Storage, Tag, Ops) {}
1330 ~DILocalScope() = default;
1333 /// Get the subprogram for this scope.
1335 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1337 DISubprogram *getSubprogram() const;
1339 /// Get the first non DILexicalBlockFile scope of this scope.
1341 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1343 DILocalScope *getNonLexicalBlockFileScope() const;
1345 static bool classof(const Metadata *MD) {
1346 return MD->getMetadataID() == DISubprogramKind ||
1347 MD->getMetadataID() == DILexicalBlockKind ||
1348 MD->getMetadataID() == DILexicalBlockFileKind;
1354 /// A debug location in source code, used for debug info and otherwise.
1355 class DILocation : public MDNode {
1356 friend class LLVMContextImpl;
1357 friend class MDNode;
1359 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1360 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
1361 ~DILocation() { dropAllReferences(); }
1363 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1364 unsigned Column, Metadata *Scope,
1365 Metadata *InlinedAt, bool ImplicitCode,
1366 StorageType Storage, bool ShouldCreate = true);
1367 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1368 unsigned Column, DILocalScope *Scope,
1369 DILocation *InlinedAt, bool ImplicitCode,
1370 StorageType Storage, bool ShouldCreate = true) {
1371 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1372 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
1376 /// With a given unsigned int \p U, use up to 13 bits to represent it.
1377 /// old_bit 1~5 --> new_bit 1~5
1378 /// old_bit 6~12 --> new_bit 7~13
1379 /// new_bit_6 is 0 if higher bits (7~13) are all 0
1380 static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
1382 return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
1385 /// Reverse transformation as getPrefixEncodingFromUnsigned.
1386 static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
1390 return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
1393 /// Returns the next component stored in discriminator.
1394 static unsigned getNextComponentInDiscriminator(unsigned D) {
1396 return D >> ((D & 0x40) ? 14 : 7);
1401 TempDILocation cloneImpl() const {
1402 // Get the raw scope/inlinedAt since it is possible to invoke this on
1403 // a DILocation containing temporary metadata.
1404 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1405 getRawInlinedAt(), isImplicitCode());
1408 static unsigned encodeComponent(unsigned C) {
1409 return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
1412 static unsigned encodingBits(unsigned C) {
1413 return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
1417 // Disallow replacing operands.
1418 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1420 DEFINE_MDNODE_GET(DILocation,
1421 (unsigned Line, unsigned Column, Metadata *Scope,
1422 Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
1423 (Line, Column, Scope, InlinedAt, ImplicitCode))
1424 DEFINE_MDNODE_GET(DILocation,
1425 (unsigned Line, unsigned Column, DILocalScope *Scope,
1426 DILocation *InlinedAt = nullptr,
1427 bool ImplicitCode = false),
1428 (Line, Column, Scope, InlinedAt, ImplicitCode))
1430 /// Return a (temporary) clone of this.
1431 TempDILocation clone() const { return cloneImpl(); }
1433 unsigned getLine() const { return SubclassData32; }
1434 unsigned getColumn() const { return SubclassData16; }
1435 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1437 DILocation *getInlinedAt() const {
1438 return cast_or_null<DILocation>(getRawInlinedAt());
1441 /// Check if the location corresponds to an implicit code.
1442 /// When the ImplicitCode flag is true, it means that the Instruction
1443 /// with this DILocation has been added by the front-end but it hasn't been
1444 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
1445 /// bracket). It's useful for code coverage to not show a counter on "empty"
1447 bool isImplicitCode() const { return ImplicitCode; }
1448 void setImplicitCode(bool ImplicitCode) { this->ImplicitCode = ImplicitCode; }
1450 DIFile *getFile() const { return getScope()->getFile(); }
1451 StringRef getFilename() const { return getScope()->getFilename(); }
1452 StringRef getDirectory() const { return getScope()->getDirectory(); }
1453 Optional<StringRef> getSource() const { return getScope()->getSource(); }
1455 /// Get the scope where this is inlined.
1457 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1459 DILocalScope *getInlinedAtScope() const {
1460 if (auto *IA = getInlinedAt())
1461 return IA->getInlinedAtScope();
1465 /// Get the DWARF discriminator.
1467 /// DWARF discriminators distinguish identical file locations between
1468 /// instructions that are on different basic blocks.
1470 /// There are 3 components stored in discriminator, from lower bits:
1472 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1473 /// that are defined by the same source line, but
1474 /// different basic blocks.
1475 /// Duplication factor: assigned by optimizations that will scale down
1476 /// the execution frequency of the original IR.
1477 /// Copy Identifier: assigned by optimizations that clones the IR.
1478 /// Each copy of the IR will be assigned an identifier.
1482 /// The above 3 components are encoded into a 32bit unsigned integer in
1483 /// order. If the lowest bit is 1, the current component is empty, and the
1484 /// next component will start in the next bit. Otherwise, the current
1485 /// component is non-empty, and its content starts in the next bit. The
1486 /// value of each components is either 5 bit or 12 bit: if the 7th bit
1487 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1488 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1489 /// represent the component. Thus, the number of bits used for a component
1490 /// is either 0 (if it and all the next components are empty); 1 - if it is
1491 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1492 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1493 /// component is also capped at 0x1ff, even in the case when both first
1494 /// components are 0, and we'd technically have 29 bits available.
1496 /// For precise control over the data being encoded in the discriminator,
1497 /// use encodeDiscriminator/decodeDiscriminator.
1499 inline unsigned getDiscriminator() const;
1501 /// Returns a new DILocation with updated \p Discriminator.
1502 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
1504 /// Returns a new DILocation with updated base discriminator \p BD. Only the
1505 /// base discriminator is set in the new DILocation, the other encoded values
1507 /// If the discriminator cannot be encoded, the function returns None.
1508 inline Optional<const DILocation *> cloneWithBaseDiscriminator(unsigned BD) const;
1510 /// Returns the duplication factor stored in the discriminator, or 1 if no
1511 /// duplication factor (or 0) is encoded.
1512 inline unsigned getDuplicationFactor() const;
1514 /// Returns the copy identifier stored in the discriminator.
1515 inline unsigned getCopyIdentifier() const;
1517 /// Returns the base discriminator stored in the discriminator.
1518 inline unsigned getBaseDiscriminator() const;
1520 /// Returns a new DILocation with duplication factor \p DF * current
1521 /// duplication factor encoded in the discriminator. The current duplication
1522 /// factor is as defined by getDuplicationFactor().
1523 /// Returns None if encoding failed.
1524 inline Optional<const DILocation *> cloneByMultiplyingDuplicationFactor(unsigned DF) const;
1526 /// When two instructions are combined into a single instruction we also
1527 /// need to combine the original locations into a single location.
1529 /// When the locations are the same we can use either location. When they
1530 /// differ, we need a third location which is distinct from either. If they
1531 /// have the same file/line but have a different discriminator we could
1532 /// create a location with a new discriminator. If they are from different
1533 /// files/lines the location is ambiguous and can't be represented in a line
1534 /// entry. In this case, if \p GenerateLocation is true, we will set the
1535 /// merged debug location as line 0 of the nearest common scope where the two
1536 /// locations are inlined from.
1538 /// \p GenerateLocation: Whether the merged location can be generated when
1539 /// \p LocA and \p LocB differ.
1540 static const DILocation *getMergedLocation(const DILocation *LocA,
1541 const DILocation *LocB);
1543 /// Returns the base discriminator for a given encoded discriminator \p D.
1544 static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
1545 return getUnsignedFromPrefixEncoding(D);
1548 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
1549 /// have certain special case behavior (e.g. treating empty duplication factor
1550 /// as the value '1').
1551 /// This API, in conjunction with cloneWithDiscriminator, may be used to encode
1552 /// the raw values provided. \p BD: base discriminator \p DF: duplication factor
1553 /// \p CI: copy index
1554 /// The return is None if the values cannot be encoded in 32 bits - for
1555 /// example, values for BD or DF larger than 12 bits. Otherwise, the return
1556 /// is the encoded value.
1557 static Optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI);
1559 /// Raw decoder for values in an encoded discriminator D.
1560 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
1563 /// Returns the duplication factor for a given encoded discriminator \p D, or
1564 /// 1 if no value or 0 is encoded.
1565 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
1566 D = getNextComponentInDiscriminator(D);
1567 unsigned Ret = getUnsignedFromPrefixEncoding(D);
1573 /// Returns the copy identifier for a given encoded discriminator \p D.
1574 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
1575 return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
1576 getNextComponentInDiscriminator(D)));
1580 Metadata *getRawScope() const { return getOperand(0); }
1581 Metadata *getRawInlinedAt() const {
1582 if (getNumOperands() == 2)
1583 return getOperand(1);
1587 static bool classof(const Metadata *MD) {
1588 return MD->getMetadataID() == DILocationKind;
1592 /// Subprogram description.
1593 class DISubprogram : public DILocalScope {
1594 friend class LLVMContextImpl;
1595 friend class MDNode;
1599 unsigned VirtualIndex;
1601 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1602 /// of method overrides from secondary bases by this amount. It may be
1607 /// Debug info subprogram flags.
1608 enum DISPFlags : uint32_t {
1609 #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
1610 #define DISP_FLAG_LARGEST_NEEDED
1611 #include "llvm/IR/DebugInfoFlags.def"
1612 SPFlagNonvirtual = SPFlagZero,
1613 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
1614 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
1617 static DISPFlags getFlag(StringRef Flag);
1618 static StringRef getFlagString(DISPFlags Flag);
1620 /// Split up a flags bitfield for easier printing.
1622 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
1623 /// any remaining (unrecognized) bits.
1624 static DISPFlags splitFlags(DISPFlags Flags,
1625 SmallVectorImpl<DISPFlags> &SplitFlags);
1627 // Helper for converting old bitfields to new flags word.
1628 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
1630 unsigned Virtuality = SPFlagNonvirtual,
1631 bool IsMainSubprogram = false) {
1632 // We're assuming virtuality is the low-order field.
1634 int(SPFlagVirtual) == int(dwarf::DW_VIRTUALITY_virtual) &&
1635 int(SPFlagPureVirtual) == int(dwarf::DW_VIRTUALITY_pure_virtual),
1636 "Virtuality constant mismatch");
1637 return static_cast<DISPFlags>(
1638 (Virtuality & SPFlagVirtuality) |
1639 (IsLocalToUnit ? SPFlagLocalToUnit : SPFlagZero) |
1640 (IsDefinition ? SPFlagDefinition : SPFlagZero) |
1641 (IsOptimized ? SPFlagOptimized : SPFlagZero) |
1642 (IsMainSubprogram ? SPFlagMainSubprogram : SPFlagZero));
1649 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1650 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
1651 DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops)
1652 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1654 Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
1655 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) {
1656 static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
1658 ~DISubprogram() = default;
1660 static DISubprogram *
1661 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1662 StringRef LinkageName, DIFile *File, unsigned Line,
1663 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
1664 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1665 DISPFlags SPFlags, DICompileUnit *Unit,
1666 DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1667 DINodeArray RetainedNodes, DITypeArray ThrownTypes,
1668 StorageType Storage, bool ShouldCreate = true) {
1669 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1670 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1671 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1672 Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
1673 RetainedNodes.get(), ThrownTypes.get(), Storage,
1676 static DISubprogram *getImpl(LLVMContext &Context, Metadata *Scope,
1677 MDString *Name, MDString *LinkageName,
1678 Metadata *File, unsigned Line, Metadata *Type,
1679 unsigned ScopeLine, Metadata *ContainingType,
1680 unsigned VirtualIndex, int ThisAdjustment,
1681 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1682 Metadata *TemplateParams, Metadata *Declaration,
1683 Metadata *RetainedNodes, Metadata *ThrownTypes,
1684 StorageType Storage, bool ShouldCreate = true);
1686 TempDISubprogram cloneImpl() const {
1687 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1688 getFile(), getLine(), getType(), getScopeLine(),
1689 getContainingType(), getVirtualIndex(),
1690 getThisAdjustment(), getFlags(), getSPFlags(),
1691 getUnit(), getTemplateParams(), getDeclaration(),
1692 getRetainedNodes(), getThrownTypes());
1698 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File,
1699 unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
1700 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1701 DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
1702 DITemplateParameterArray TemplateParams = nullptr,
1703 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
1704 DITypeArray ThrownTypes = nullptr),
1705 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1706 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1707 Declaration, RetainedNodes, ThrownTypes))
1711 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1712 unsigned Line, Metadata *Type, unsigned ScopeLine,
1713 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1714 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1715 Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr,
1716 Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr),
1717 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1718 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1719 Declaration, RetainedNodes, ThrownTypes))
1721 TempDISubprogram clone() const { return cloneImpl(); }
1723 /// Returns a new temporary DISubprogram with updated Flags
1724 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
1725 auto NewSP = clone();
1726 NewSP->Flags = NewFlags;
1731 unsigned getLine() const { return Line; }
1732 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
1733 unsigned getVirtualIndex() const { return VirtualIndex; }
1734 int getThisAdjustment() const { return ThisAdjustment; }
1735 unsigned getScopeLine() const { return ScopeLine; }
1736 DIFlags getFlags() const { return Flags; }
1737 DISPFlags getSPFlags() const { return SPFlags; }
1738 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
1739 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
1740 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
1741 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
1743 bool isArtificial() const { return getFlags() & FlagArtificial; }
1744 bool isPrivate() const {
1745 return (getFlags() & FlagAccessibility) == FlagPrivate;
1747 bool isProtected() const {
1748 return (getFlags() & FlagAccessibility) == FlagProtected;
1750 bool isPublic() const {
1751 return (getFlags() & FlagAccessibility) == FlagPublic;
1753 bool isExplicit() const { return getFlags() & FlagExplicit; }
1754 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1755 bool areAllCallsDescribed() const {
1756 return getFlags() & FlagAllCallsDescribed;
1758 bool isPure() const { return getSPFlags() & SPFlagPure; }
1759 bool isElemental() const { return getSPFlags() & SPFlagElemental; }
1760 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
1761 bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; }
1763 /// Check if this is deleted member function.
1765 /// Return true if this subprogram is a C++11 special
1766 /// member function declared deleted.
1767 bool isDeleted() const { return getSPFlags() & SPFlagDeleted; }
1769 /// Check if this is reference-qualified.
1771 /// Return true if this subprogram is a C++11 reference-qualified non-static
1772 /// member function (void foo() &).
1773 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
1775 /// Check if this is rvalue-reference-qualified.
1777 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1778 /// non-static member function (void foo() &&).
1779 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
1781 /// Check if this is marked as noreturn.
1783 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1784 bool isNoReturn() const { return getFlags() & FlagNoReturn; }
1786 // Check if this routine is a compiler-generated thunk.
1788 // Returns true if this subprogram is a thunk generated by the compiler.
1789 bool isThunk() const { return getFlags() & FlagThunk; }
1791 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1793 StringRef getName() const { return getStringOperand(2); }
1794 StringRef getLinkageName() const { return getStringOperand(3); }
1796 DISubroutineType *getType() const {
1797 return cast_or_null<DISubroutineType>(getRawType());
1799 DIType *getContainingType() const {
1800 return cast_or_null<DIType>(getRawContainingType());
1803 DICompileUnit *getUnit() const {
1804 return cast_or_null<DICompileUnit>(getRawUnit());
1806 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
1807 DITemplateParameterArray getTemplateParams() const {
1808 return cast_or_null<MDTuple>(getRawTemplateParams());
1810 DISubprogram *getDeclaration() const {
1811 return cast_or_null<DISubprogram>(getRawDeclaration());
1813 DINodeArray getRetainedNodes() const {
1814 return cast_or_null<MDTuple>(getRawRetainedNodes());
1816 DITypeArray getThrownTypes() const {
1817 return cast_or_null<MDTuple>(getRawThrownTypes());
1820 Metadata *getRawScope() const { return getOperand(1); }
1821 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1822 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
1823 Metadata *getRawType() const { return getOperand(4); }
1824 Metadata *getRawUnit() const { return getOperand(5); }
1825 Metadata *getRawDeclaration() const { return getOperand(6); }
1826 Metadata *getRawRetainedNodes() const { return getOperand(7); }
1827 Metadata *getRawContainingType() const {
1828 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
1830 Metadata *getRawTemplateParams() const {
1831 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
1833 Metadata *getRawThrownTypes() const {
1834 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
1837 /// Check if this subprogram describes the given function.
1839 /// FIXME: Should this be looking through bitcasts?
1840 bool describes(const Function *F) const;
1842 static bool classof(const Metadata *MD) {
1843 return MD->getMetadataID() == DISubprogramKind;
1847 class DILexicalBlockBase : public DILocalScope {
1849 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1850 ArrayRef<Metadata *> Ops)
1851 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1852 ~DILexicalBlockBase() = default;
1855 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1857 Metadata *getRawScope() const { return getOperand(1); }
1859 static bool classof(const Metadata *MD) {
1860 return MD->getMetadataID() == DILexicalBlockKind ||
1861 MD->getMetadataID() == DILexicalBlockFileKind;
1865 class DILexicalBlock : public DILexicalBlockBase {
1866 friend class LLVMContextImpl;
1867 friend class MDNode;
1872 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1873 unsigned Column, ArrayRef<Metadata *> Ops)
1874 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1876 assert(Column < (1u << 16) && "Expected 16-bit column");
1878 ~DILexicalBlock() = default;
1880 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1881 DIFile *File, unsigned Line, unsigned Column,
1882 StorageType Storage,
1883 bool ShouldCreate = true) {
1884 return getImpl(Context, static_cast<Metadata *>(Scope),
1885 static_cast<Metadata *>(File), Line, Column, Storage,
1889 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1890 Metadata *File, unsigned Line, unsigned Column,
1891 StorageType Storage, bool ShouldCreate = true);
1893 TempDILexicalBlock cloneImpl() const {
1894 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1899 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1900 unsigned Line, unsigned Column),
1901 (Scope, File, Line, Column))
1902 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1903 unsigned Line, unsigned Column),
1904 (Scope, File, Line, Column))
1906 TempDILexicalBlock clone() const { return cloneImpl(); }
1908 unsigned getLine() const { return Line; }
1909 unsigned getColumn() const { return Column; }
1911 static bool classof(const Metadata *MD) {
1912 return MD->getMetadataID() == DILexicalBlockKind;
1916 class DILexicalBlockFile : public DILexicalBlockBase {
1917 friend class LLVMContextImpl;
1918 friend class MDNode;
1920 unsigned Discriminator;
1922 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1923 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1924 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1925 Discriminator(Discriminator) {}
1926 ~DILexicalBlockFile() = default;
1928 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1929 DIFile *File, unsigned Discriminator,
1930 StorageType Storage,
1931 bool ShouldCreate = true) {
1932 return getImpl(Context, static_cast<Metadata *>(Scope),
1933 static_cast<Metadata *>(File), Discriminator, Storage,
1937 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1938 Metadata *File, unsigned Discriminator,
1939 StorageType Storage,
1940 bool ShouldCreate = true);
1942 TempDILexicalBlockFile cloneImpl() const {
1943 return getTemporary(getContext(), getScope(), getFile(),
1944 getDiscriminator());
1948 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1949 unsigned Discriminator),
1950 (Scope, File, Discriminator))
1951 DEFINE_MDNODE_GET(DILexicalBlockFile,
1952 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1953 (Scope, File, Discriminator))
1955 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1957 // TODO: Remove these once they're gone from DILexicalBlockBase.
1958 unsigned getLine() const = delete;
1959 unsigned getColumn() const = delete;
1961 unsigned getDiscriminator() const { return Discriminator; }
1963 static bool classof(const Metadata *MD) {
1964 return MD->getMetadataID() == DILexicalBlockFileKind;
1968 unsigned DILocation::getDiscriminator() const {
1969 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1970 return F->getDiscriminator();
1975 DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
1976 DIScope *Scope = getScope();
1977 // Skip all parent DILexicalBlockFile that already have a discriminator
1978 // assigned. We do not want to have nested DILexicalBlockFiles that have
1979 // mutliple discriminators because only the leaf DILexicalBlockFile's
1980 // dominator will be used.
1981 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
1982 LBF && LBF->getDiscriminator() != 0;
1983 LBF = dyn_cast<DILexicalBlockFile>(Scope))
1984 Scope = LBF->getScope();
1985 DILexicalBlockFile *NewScope =
1986 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
1987 return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
1991 unsigned DILocation::getBaseDiscriminator() const {
1992 return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
1995 unsigned DILocation::getDuplicationFactor() const {
1996 return getDuplicationFactorFromDiscriminator(getDiscriminator());
1999 unsigned DILocation::getCopyIdentifier() const {
2000 return getCopyIdentifierFromDiscriminator(getDiscriminator());
2003 Optional<const DILocation *> DILocation::cloneWithBaseDiscriminator(unsigned D) const {
2004 unsigned BD, DF, CI;
2005 decodeDiscriminator(getDiscriminator(), BD, DF, CI);
2008 if (Optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
2009 return cloneWithDiscriminator(*Encoded);
2013 Optional<const DILocation *> DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const {
2014 DF *= getDuplicationFactor();
2018 unsigned BD = getBaseDiscriminator();
2019 unsigned CI = getCopyIdentifier();
2020 if (Optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
2021 return cloneWithDiscriminator(*D);
2025 class DINamespace : public DIScope {
2026 friend class LLVMContextImpl;
2027 friend class MDNode;
2029 unsigned ExportSymbols : 1;
2031 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
2032 ArrayRef<Metadata *> Ops)
2033 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
2035 ExportSymbols(ExportSymbols) {}
2036 ~DINamespace() = default;
2038 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
2039 StringRef Name, bool ExportSymbols,
2040 StorageType Storage, bool ShouldCreate = true) {
2041 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2042 ExportSymbols, Storage, ShouldCreate);
2044 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
2045 MDString *Name, bool ExportSymbols,
2046 StorageType Storage, bool ShouldCreate = true);
2048 TempDINamespace cloneImpl() const {
2049 return getTemporary(getContext(), getScope(), getName(),
2050 getExportSymbols());
2054 DEFINE_MDNODE_GET(DINamespace,
2055 (DIScope *Scope, StringRef Name, bool ExportSymbols),
2056 (Scope, Name, ExportSymbols))
2057 DEFINE_MDNODE_GET(DINamespace,
2058 (Metadata *Scope, MDString *Name, bool ExportSymbols),
2059 (Scope, Name, ExportSymbols))
2061 TempDINamespace clone() const { return cloneImpl(); }
2063 bool getExportSymbols() const { return ExportSymbols; }
2064 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2065 StringRef getName() const { return getStringOperand(2); }
2067 Metadata *getRawScope() const { return getOperand(1); }
2068 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2070 static bool classof(const Metadata *MD) {
2071 return MD->getMetadataID() == DINamespaceKind;
2075 /// A (clang) module that has been imported by the compile unit.
2077 class DIModule : public DIScope {
2078 friend class LLVMContextImpl;
2079 friend class MDNode;
2081 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
2082 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
2083 ~DIModule() = default;
2085 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
2086 StringRef Name, StringRef ConfigurationMacros,
2087 StringRef IncludePath, StringRef SysRoot,
2088 StorageType Storage, bool ShouldCreate = true) {
2089 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2090 getCanonicalMDString(Context, ConfigurationMacros),
2091 getCanonicalMDString(Context, IncludePath),
2092 getCanonicalMDString(Context, SysRoot),
2093 Storage, ShouldCreate);
2095 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
2096 MDString *Name, MDString *ConfigurationMacros,
2097 MDString *IncludePath, MDString *SysRoot,
2098 StorageType Storage, bool ShouldCreate = true);
2100 TempDIModule cloneImpl() const {
2101 return getTemporary(getContext(), getScope(), getName(),
2102 getConfigurationMacros(), getIncludePath(),
2107 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
2108 StringRef ConfigurationMacros, StringRef IncludePath,
2110 (Scope, Name, ConfigurationMacros, IncludePath, SysRoot))
2111 DEFINE_MDNODE_GET(DIModule,
2112 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
2113 MDString *IncludePath, MDString *SysRoot),
2114 (Scope, Name, ConfigurationMacros, IncludePath, SysRoot))
2116 TempDIModule clone() const { return cloneImpl(); }
2118 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2119 StringRef getName() const { return getStringOperand(1); }
2120 StringRef getConfigurationMacros() const { return getStringOperand(2); }
2121 StringRef getIncludePath() const { return getStringOperand(3); }
2122 StringRef getSysRoot() const { return getStringOperand(4); }
2124 Metadata *getRawScope() const { return getOperand(0); }
2125 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2126 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
2127 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
2128 MDString *getRawSysRoot() const { return getOperandAs<MDString>(4); }
2130 static bool classof(const Metadata *MD) {
2131 return MD->getMetadataID() == DIModuleKind;
2135 /// Base class for template parameters.
2136 class DITemplateParameter : public DINode {
2138 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
2139 unsigned Tag, ArrayRef<Metadata *> Ops)
2140 : DINode(Context, ID, Storage, Tag, Ops) {}
2141 ~DITemplateParameter() = default;
2144 StringRef getName() const { return getStringOperand(0); }
2145 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2147 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2148 Metadata *getRawType() const { return getOperand(1); }
2150 static bool classof(const Metadata *MD) {
2151 return MD->getMetadataID() == DITemplateTypeParameterKind ||
2152 MD->getMetadataID() == DITemplateValueParameterKind;
2156 class DITemplateTypeParameter : public DITemplateParameter {
2157 friend class LLVMContextImpl;
2158 friend class MDNode;
2160 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
2161 ArrayRef<Metadata *> Ops)
2162 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
2163 dwarf::DW_TAG_template_type_parameter, Ops) {}
2164 ~DITemplateTypeParameter() = default;
2166 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
2167 DIType *Type, StorageType Storage,
2168 bool ShouldCreate = true) {
2169 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
2172 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
2173 Metadata *Type, StorageType Storage,
2174 bool ShouldCreate = true);
2176 TempDITemplateTypeParameter cloneImpl() const {
2177 return getTemporary(getContext(), getName(), getType());
2181 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DIType *Type),
2183 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
2186 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2188 static bool classof(const Metadata *MD) {
2189 return MD->getMetadataID() == DITemplateTypeParameterKind;
2193 class DITemplateValueParameter : public DITemplateParameter {
2194 friend class LLVMContextImpl;
2195 friend class MDNode;
2197 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
2198 unsigned Tag, ArrayRef<Metadata *> Ops)
2199 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2201 ~DITemplateValueParameter() = default;
2203 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2204 StringRef Name, DIType *Type,
2205 Metadata *Value, StorageType Storage,
2206 bool ShouldCreate = true) {
2207 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2208 Value, Storage, ShouldCreate);
2210 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2211 MDString *Name, Metadata *Type,
2212 Metadata *Value, StorageType Storage,
2213 bool ShouldCreate = true);
2215 TempDITemplateValueParameter cloneImpl() const {
2216 return getTemporary(getContext(), getTag(), getName(), getType(),
2221 DEFINE_MDNODE_GET(DITemplateValueParameter,
2222 (unsigned Tag, StringRef Name, DIType *Type,
2224 (Tag, Name, Type, Value))
2225 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
2226 Metadata *Type, Metadata *Value),
2227 (Tag, Name, Type, Value))
2229 TempDITemplateValueParameter clone() const { return cloneImpl(); }
2231 Metadata *getValue() const { return getOperand(2); }
2233 static bool classof(const Metadata *MD) {
2234 return MD->getMetadataID() == DITemplateValueParameterKind;
2238 /// Base class for variables.
2239 class DIVariable : public DINode {
2241 uint32_t AlignInBits;
2244 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
2245 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
2246 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
2247 AlignInBits(AlignInBits) {}
2248 ~DIVariable() = default;
2251 unsigned getLine() const { return Line; }
2252 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2253 StringRef getName() const { return getStringOperand(1); }
2254 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2255 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2256 uint32_t getAlignInBits() const { return AlignInBits; }
2257 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2258 /// Determines the size of the variable's type.
2259 Optional<uint64_t> getSizeInBits() const;
2261 /// Return the signedness of this variable's type, or None if this type is
2262 /// neither signed nor unsigned.
2263 Optional<DIBasicType::Signedness> getSignedness() const {
2264 if (auto *BT = dyn_cast<DIBasicType>(getType()))
2265 return BT->getSignedness();
2269 StringRef getFilename() const {
2270 if (auto *F = getFile())
2271 return F->getFilename();
2275 StringRef getDirectory() const {
2276 if (auto *F = getFile())
2277 return F->getDirectory();
2281 Optional<StringRef> getSource() const {
2282 if (auto *F = getFile())
2283 return F->getSource();
2287 Metadata *getRawScope() const { return getOperand(0); }
2288 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2289 Metadata *getRawFile() const { return getOperand(2); }
2290 Metadata *getRawType() const { return getOperand(3); }
2292 static bool classof(const Metadata *MD) {
2293 return MD->getMetadataID() == DILocalVariableKind ||
2294 MD->getMetadataID() == DIGlobalVariableKind;
2298 /// DWARF expression.
2300 /// This is (almost) a DWARF expression that modifies the location of a
2301 /// variable, or the location of a single piece of a variable, or (when using
2302 /// DW_OP_stack_value) is the constant variable value.
2304 /// TODO: Co-allocate the expression elements.
2305 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2307 class DIExpression : public MDNode {
2308 friend class LLVMContextImpl;
2309 friend class MDNode;
2311 std::vector<uint64_t> Elements;
2313 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2314 : MDNode(C, DIExpressionKind, Storage, None),
2315 Elements(Elements.begin(), Elements.end()) {}
2316 ~DIExpression() = default;
2318 static DIExpression *getImpl(LLVMContext &Context,
2319 ArrayRef<uint64_t> Elements, StorageType Storage,
2320 bool ShouldCreate = true);
2322 TempDIExpression cloneImpl() const {
2323 return getTemporary(getContext(), getElements());
2327 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2329 TempDIExpression clone() const { return cloneImpl(); }
2331 ArrayRef<uint64_t> getElements() const { return Elements; }
2333 unsigned getNumElements() const { return Elements.size(); }
2335 uint64_t getElement(unsigned I) const {
2336 assert(I < Elements.size() && "Index out of range");
2340 /// Determine whether this represents a standalone constant value.
2341 bool isConstant() const;
2343 using element_iterator = ArrayRef<uint64_t>::iterator;
2345 element_iterator elements_begin() const { return getElements().begin(); }
2346 element_iterator elements_end() const { return getElements().end(); }
2348 /// A lightweight wrapper around an expression operand.
2350 /// TODO: Store arguments directly and change \a DIExpression to store a
2353 const uint64_t *Op = nullptr;
2356 ExprOperand() = default;
2357 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2359 const uint64_t *get() const { return Op; }
2361 /// Get the operand code.
2362 uint64_t getOp() const { return *Op; }
2364 /// Get an argument to the operand.
2366 /// Never returns the operand itself.
2367 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2369 unsigned getNumArgs() const { return getSize() - 1; }
2371 /// Return the size of the operand.
2373 /// Return the number of elements in the operand (1 + args).
2374 unsigned getSize() const;
2376 /// Append the elements of this operand to \p V.
2377 void appendToVector(SmallVectorImpl<uint64_t> &V) const {
2378 V.append(get(), get() + getSize());
2382 /// An iterator for expression operands.
2383 class expr_op_iterator
2384 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2388 expr_op_iterator() = default;
2389 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2391 element_iterator getBase() const { return Op.get(); }
2392 const ExprOperand &operator*() const { return Op; }
2393 const ExprOperand *operator->() const { return &Op; }
2395 expr_op_iterator &operator++() {
2399 expr_op_iterator operator++(int) {
2400 expr_op_iterator T(*this);
2405 /// Get the next iterator.
2407 /// \a std::next() doesn't work because this is technically an
2408 /// input_iterator, but it's a perfectly valid operation. This is an
2409 /// accessor to provide the same functionality.
2410 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2412 bool operator==(const expr_op_iterator &X) const {
2413 return getBase() == X.getBase();
2415 bool operator!=(const expr_op_iterator &X) const {
2416 return getBase() != X.getBase();
2420 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2423 /// Visit the elements via ExprOperand wrappers.
2425 /// These range iterators visit elements through \a ExprOperand wrappers.
2426 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2429 /// \pre \a isValid() gives \c true.
2431 expr_op_iterator expr_op_begin() const {
2432 return expr_op_iterator(elements_begin());
2434 expr_op_iterator expr_op_end() const {
2435 return expr_op_iterator(elements_end());
2437 iterator_range<expr_op_iterator> expr_ops() const {
2438 return {expr_op_begin(), expr_op_end()};
2442 bool isValid() const;
2444 static bool classof(const Metadata *MD) {
2445 return MD->getMetadataID() == DIExpressionKind;
2448 /// Return whether the first element a DW_OP_deref.
2449 bool startsWithDeref() const {
2450 return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
2453 /// Holds the characteristics of one fragment of a larger variable.
2454 struct FragmentInfo {
2455 uint64_t SizeInBits;
2456 uint64_t OffsetInBits;
2459 /// Retrieve the details of this fragment expression.
2460 static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2461 expr_op_iterator End);
2463 /// Retrieve the details of this fragment expression.
2464 Optional<FragmentInfo> getFragmentInfo() const {
2465 return getFragmentInfo(expr_op_begin(), expr_op_end());
2468 /// Return whether this is a piece of an aggregate variable.
2469 bool isFragment() const { return getFragmentInfo().hasValue(); }
2471 /// Return whether this is an implicit location description.
2472 bool isImplicit() const;
2474 /// Return whether the location is computed on the expression stack, meaning
2475 /// it cannot be a simple register location.
2476 bool isComplex() const;
2478 /// Append \p Ops with operations to apply the \p Offset.
2479 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2481 /// If this is a constant offset, extract it. If there is no expression,
2482 /// return true with an offset of zero.
2483 bool extractIfOffset(int64_t &Offset) const;
2485 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
2486 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
2488 static const DIExpression *extractAddressClass(const DIExpression *Expr,
2489 unsigned &AddrClass);
2491 /// Used for DIExpression::prepend.
2492 enum PrependOps : uint8_t {
2494 DerefBefore = 1 << 0,
2495 DerefAfter = 1 << 1,
2496 StackValue = 1 << 2,
2500 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2501 /// into a stack value or/and an entry value.
2502 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2503 int64_t Offset = 0);
2505 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2507 static DIExpression *prependOpcodes(const DIExpression *Expr,
2508 SmallVectorImpl<uint64_t> &Ops,
2509 bool StackValue = false,
2510 bool EntryValue = false);
2512 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
2513 /// returned expression is a stack value only if \p DIExpr is a stack value.
2514 /// If \p DIExpr describes a fragment, the returned expression will describe
2515 /// the same fragment.
2516 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
2518 /// Convert \p DIExpr into a stack value if it isn't one already by appending
2519 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
2520 /// If \p DIExpr describes a fragment, the returned expression will describe
2521 /// the same fragment.
2522 static DIExpression *appendToStack(const DIExpression *Expr,
2523 ArrayRef<uint64_t> Ops);
2525 /// Create a DIExpression to describe one part of an aggregate variable that
2526 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2527 /// will be appended to the elements of \c Expr. If \c Expr already contains
2528 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2529 /// into the existing fragment.
2531 /// \param OffsetInBits Offset of the piece in bits.
2532 /// \param SizeInBits Size of the piece in bits.
2533 /// \return Creating a fragment expression may fail if \c Expr
2534 /// contains arithmetic operations that would be truncated.
2535 static Optional<DIExpression *>
2536 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2537 unsigned SizeInBits);
2539 /// Determine the relative position of the fragments passed in.
2540 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2541 /// 1 if this is entirely after Other.
2542 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
2543 uint64_t l1 = A.OffsetInBits;
2544 uint64_t l2 = B.OffsetInBits;
2545 uint64_t r1 = l1 + A.SizeInBits;
2546 uint64_t r2 = l2 + B.SizeInBits;
2555 using ExtOps = std::array<uint64_t, 6>;
2557 /// Returns the ops for a zero- or sign-extension in a DIExpression.
2558 static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed);
2560 /// Append a zero- or sign-extension to \p Expr. Converts the expression to a
2561 /// stack value if it isn't one already.
2562 static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize,
2563 unsigned ToSize, bool Signed);
2565 /// Check if fragments overlap between a pair of FragmentInfos.
2566 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
2567 return fragmentCmp(A, B) == 0;
2570 /// Determine the relative position of the fragments described by this
2571 /// DIExpression and \p Other. Calls static fragmentCmp implementation.
2572 int fragmentCmp(const DIExpression *Other) const {
2573 auto Fragment1 = *getFragmentInfo();
2574 auto Fragment2 = *Other->getFragmentInfo();
2575 return fragmentCmp(Fragment1, Fragment2);
2578 /// Check if fragments overlap between this DIExpression and \p Other.
2579 bool fragmentsOverlap(const DIExpression *Other) const {
2580 if (!isFragment() || !Other->isFragment())
2582 return fragmentCmp(Other) == 0;
2585 /// Check if the expression consists of exactly one entry value operand.
2586 /// (This is the only configuration of entry values that is supported.)
2587 bool isEntryValue() const {
2588 return getNumElements() > 0 &&
2589 getElement(0) == dwarf::DW_OP_LLVM_entry_value;
2593 inline bool operator==(const DIExpression::FragmentInfo &A,
2594 const DIExpression::FragmentInfo &B) {
2595 return std::tie(A.SizeInBits, A.OffsetInBits) ==
2596 std::tie(B.SizeInBits, B.OffsetInBits);
2599 inline bool operator<(const DIExpression::FragmentInfo &A,
2600 const DIExpression::FragmentInfo &B) {
2601 return std::tie(A.SizeInBits, A.OffsetInBits) <
2602 std::tie(B.SizeInBits, B.OffsetInBits);
2605 template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
2606 using FragInfo = DIExpression::FragmentInfo;
2607 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
2609 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
2611 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
2613 static unsigned getHashValue(const FragInfo &Frag) {
2614 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
2617 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
2620 /// Global variables.
2622 /// TODO: Remove DisplayName. It's always equal to Name.
2623 class DIGlobalVariable : public DIVariable {
2624 friend class LLVMContextImpl;
2625 friend class MDNode;
2630 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2631 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
2632 ArrayRef<Metadata *> Ops)
2633 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
2634 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
2635 ~DIGlobalVariable() = default;
2637 static DIGlobalVariable *
2638 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
2639 StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type,
2640 bool IsLocalToUnit, bool IsDefinition,
2641 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
2642 uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true) {
2643 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2644 getCanonicalMDString(Context, LinkageName), File, Line, Type,
2645 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
2646 cast_or_null<Metadata>(TemplateParams), AlignInBits, Storage,
2649 static DIGlobalVariable *
2650 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
2651 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
2652 bool IsLocalToUnit, bool IsDefinition,
2653 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
2654 uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true);
2656 TempDIGlobalVariable cloneImpl() const {
2657 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
2658 getFile(), getLine(), getType(), isLocalToUnit(),
2659 isDefinition(), getStaticDataMemberDeclaration(),
2660 getTemplateParams(), getAlignInBits());
2664 DEFINE_MDNODE_GET(DIGlobalVariable,
2665 (DIScope * Scope, StringRef Name, StringRef LinkageName,
2666 DIFile *File, unsigned Line, DIType *Type,
2667 bool IsLocalToUnit, bool IsDefinition,
2668 DIDerivedType *StaticDataMemberDeclaration,
2669 MDTuple *TemplateParams, uint32_t AlignInBits),
2670 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2671 IsDefinition, StaticDataMemberDeclaration, TemplateParams,
2673 DEFINE_MDNODE_GET(DIGlobalVariable,
2674 (Metadata * Scope, MDString *Name, MDString *LinkageName,
2675 Metadata *File, unsigned Line, Metadata *Type,
2676 bool IsLocalToUnit, bool IsDefinition,
2677 Metadata *StaticDataMemberDeclaration,
2678 Metadata *TemplateParams, uint32_t AlignInBits),
2679 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2680 IsDefinition, StaticDataMemberDeclaration, TemplateParams,
2683 TempDIGlobalVariable clone() const { return cloneImpl(); }
2685 bool isLocalToUnit() const { return IsLocalToUnit; }
2686 bool isDefinition() const { return IsDefinition; }
2687 StringRef getDisplayName() const { return getStringOperand(4); }
2688 StringRef getLinkageName() const { return getStringOperand(5); }
2689 DIDerivedType *getStaticDataMemberDeclaration() const {
2690 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
2693 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
2694 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
2695 Metadata *getRawTemplateParams() const { return getOperand(7); }
2696 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
2698 static bool classof(const Metadata *MD) {
2699 return MD->getMetadataID() == DIGlobalVariableKind;
2703 class DICommonBlock : public DIScope {
2706 friend class LLVMContextImpl;
2707 friend class MDNode;
2709 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
2710 ArrayRef<Metadata *> Ops)
2711 : DIScope(Context, DICommonBlockKind, Storage, dwarf::DW_TAG_common_block,
2712 Ops), LineNo(LineNo) {}
2714 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
2715 DIGlobalVariable *Decl, StringRef Name,
2716 DIFile *File, unsigned LineNo,
2717 StorageType Storage,
2718 bool ShouldCreate = true) {
2719 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
2720 File, LineNo, Storage, ShouldCreate);
2722 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
2723 Metadata *Decl, MDString *Name, Metadata *File,
2725 StorageType Storage, bool ShouldCreate = true);
2727 TempDICommonBlock cloneImpl() const {
2728 return getTemporary(getContext(), getScope(), getDecl(), getName(),
2729 getFile(), getLineNo());
2733 DEFINE_MDNODE_GET(DICommonBlock,
2734 (DIScope *Scope, DIGlobalVariable *Decl, StringRef Name,
2735 DIFile *File, unsigned LineNo),
2736 (Scope, Decl, Name, File, LineNo))
2737 DEFINE_MDNODE_GET(DICommonBlock,
2738 (Metadata *Scope, Metadata *Decl, MDString *Name,
2739 Metadata *File, unsigned LineNo),
2740 (Scope, Decl, Name, File, LineNo))
2742 TempDICommonBlock clone() const { return cloneImpl(); }
2744 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2745 DIGlobalVariable *getDecl() const {
2746 return cast_or_null<DIGlobalVariable>(getRawDecl());
2748 StringRef getName() const { return getStringOperand(2); }
2749 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2750 unsigned getLineNo() const { return LineNo; }
2752 Metadata *getRawScope() const { return getOperand(0); }
2753 Metadata *getRawDecl() const { return getOperand(1); }
2754 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2755 Metadata *getRawFile() const { return getOperand(3); }
2757 static bool classof(const Metadata *MD) {
2758 return MD->getMetadataID() == DICommonBlockKind;
2764 /// TODO: Split up flags.
2765 class DILocalVariable : public DIVariable {
2766 friend class LLVMContextImpl;
2767 friend class MDNode;
2772 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2773 unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
2774 ArrayRef<Metadata *> Ops)
2775 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
2776 Arg(Arg), Flags(Flags) {
2777 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
2779 ~DILocalVariable() = default;
2781 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2782 StringRef Name, DIFile *File, unsigned Line,
2783 DIType *Type, unsigned Arg, DIFlags Flags,
2784 uint32_t AlignInBits, StorageType Storage,
2785 bool ShouldCreate = true) {
2786 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2787 Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
2789 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
2790 MDString *Name, Metadata *File, unsigned Line,
2791 Metadata *Type, unsigned Arg, DIFlags Flags,
2792 uint32_t AlignInBits, StorageType Storage,
2793 bool ShouldCreate = true);
2795 TempDILocalVariable cloneImpl() const {
2796 return getTemporary(getContext(), getScope(), getName(), getFile(),
2797 getLine(), getType(), getArg(), getFlags(),
2802 DEFINE_MDNODE_GET(DILocalVariable,
2803 (DILocalScope * Scope, StringRef Name, DIFile *File,
2804 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
2805 uint32_t AlignInBits),
2806 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2807 DEFINE_MDNODE_GET(DILocalVariable,
2808 (Metadata * Scope, MDString *Name, Metadata *File,
2809 unsigned Line, Metadata *Type, unsigned Arg,
2810 DIFlags Flags, uint32_t AlignInBits),
2811 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2813 TempDILocalVariable clone() const { return cloneImpl(); }
2815 /// Get the local scope for this variable.
2817 /// Variables must be defined in a local scope.
2818 DILocalScope *getScope() const {
2819 return cast<DILocalScope>(DIVariable::getScope());
2822 bool isParameter() const { return Arg; }
2823 unsigned getArg() const { return Arg; }
2824 DIFlags getFlags() const { return Flags; }
2826 bool isArtificial() const { return getFlags() & FlagArtificial; }
2827 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
2829 /// Check that a location is valid for this variable.
2831 /// Check that \c DL exists, is in the same subprogram, and has the same
2832 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2833 /// to a \a DbgInfoIntrinsic.)
2834 bool isValidLocationForIntrinsic(const DILocation *DL) const {
2835 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2838 static bool classof(const Metadata *MD) {
2839 return MD->getMetadataID() == DILocalVariableKind;
2845 class DILabel : public DINode {
2846 friend class LLVMContextImpl;
2847 friend class MDNode;
2851 DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
2852 ArrayRef<Metadata *> Ops)
2853 : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops), Line(Line) {}
2854 ~DILabel() = default;
2856 static DILabel *getImpl(LLVMContext &Context, DIScope *Scope,
2857 StringRef Name, DIFile *File, unsigned Line,
2858 StorageType Storage,
2859 bool ShouldCreate = true) {
2860 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2861 Line, Storage, ShouldCreate);
2863 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope,
2864 MDString *Name, Metadata *File, unsigned Line,
2865 StorageType Storage,
2866 bool ShouldCreate = true);
2868 TempDILabel cloneImpl() const {
2869 return getTemporary(getContext(), getScope(), getName(), getFile(),
2874 DEFINE_MDNODE_GET(DILabel,
2875 (DILocalScope * Scope, StringRef Name, DIFile *File,
2877 (Scope, Name, File, Line))
2878 DEFINE_MDNODE_GET(DILabel,
2879 (Metadata * Scope, MDString *Name, Metadata *File,
2881 (Scope, Name, File, Line))
2883 TempDILabel clone() const { return cloneImpl(); }
2885 /// Get the local scope for this label.
2887 /// Labels must be defined in a local scope.
2888 DILocalScope *getScope() const {
2889 return cast_or_null<DILocalScope>(getRawScope());
2891 unsigned getLine() const { return Line; }
2892 StringRef getName() const { return getStringOperand(1); }
2893 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2895 Metadata *getRawScope() const { return getOperand(0); }
2896 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2897 Metadata *getRawFile() const { return getOperand(2); }
2899 /// Check that a location is valid for this label.
2901 /// Check that \c DL exists, is in the same subprogram, and has the same
2902 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2903 /// to a \a DbgInfoIntrinsic.)
2904 bool isValidLocationForIntrinsic(const DILocation *DL) const {
2905 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2908 static bool classof(const Metadata *MD) {
2909 return MD->getMetadataID() == DILabelKind;
2913 class DIObjCProperty : public DINode {
2914 friend class LLVMContextImpl;
2915 friend class MDNode;
2918 unsigned Attributes;
2920 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2921 unsigned Attributes, ArrayRef<Metadata *> Ops)
2922 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2924 Line(Line), Attributes(Attributes) {}
2925 ~DIObjCProperty() = default;
2927 static DIObjCProperty *
2928 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2929 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2930 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2931 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2932 getCanonicalMDString(Context, GetterName),
2933 getCanonicalMDString(Context, SetterName), Attributes, Type,
2934 Storage, ShouldCreate);
2936 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2937 Metadata *File, unsigned Line,
2938 MDString *GetterName, MDString *SetterName,
2939 unsigned Attributes, Metadata *Type,
2940 StorageType Storage, bool ShouldCreate = true);
2942 TempDIObjCProperty cloneImpl() const {
2943 return getTemporary(getContext(), getName(), getFile(), getLine(),
2944 getGetterName(), getSetterName(), getAttributes(),
2949 DEFINE_MDNODE_GET(DIObjCProperty,
2950 (StringRef Name, DIFile *File, unsigned Line,
2951 StringRef GetterName, StringRef SetterName,
2952 unsigned Attributes, DIType *Type),
2953 (Name, File, Line, GetterName, SetterName, Attributes,
2955 DEFINE_MDNODE_GET(DIObjCProperty,
2956 (MDString * Name, Metadata *File, unsigned Line,
2957 MDString *GetterName, MDString *SetterName,
2958 unsigned Attributes, Metadata *Type),
2959 (Name, File, Line, GetterName, SetterName, Attributes,
2962 TempDIObjCProperty clone() const { return cloneImpl(); }
2964 unsigned getLine() const { return Line; }
2965 unsigned getAttributes() const { return Attributes; }
2966 StringRef getName() const { return getStringOperand(0); }
2967 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2968 StringRef getGetterName() const { return getStringOperand(2); }
2969 StringRef getSetterName() const { return getStringOperand(3); }
2970 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2972 StringRef getFilename() const {
2973 if (auto *F = getFile())
2974 return F->getFilename();
2978 StringRef getDirectory() const {
2979 if (auto *F = getFile())
2980 return F->getDirectory();
2984 Optional<StringRef> getSource() const {
2985 if (auto *F = getFile())
2986 return F->getSource();
2990 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2991 Metadata *getRawFile() const { return getOperand(1); }
2992 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2993 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2994 Metadata *getRawType() const { return getOperand(4); }
2996 static bool classof(const Metadata *MD) {
2997 return MD->getMetadataID() == DIObjCPropertyKind;
3001 /// An imported module (C++ using directive or similar).
3002 class DIImportedEntity : public DINode {
3003 friend class LLVMContextImpl;
3004 friend class MDNode;
3008 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
3009 unsigned Line, ArrayRef<Metadata *> Ops)
3010 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
3011 ~DIImportedEntity() = default;
3013 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3014 DIScope *Scope, DINode *Entity, DIFile *File,
3015 unsigned Line, StringRef Name,
3016 StorageType Storage,
3017 bool ShouldCreate = true) {
3018 return getImpl(Context, Tag, Scope, Entity, File, Line,
3019 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
3021 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3022 Metadata *Scope, Metadata *Entity,
3023 Metadata *File, unsigned Line,
3024 MDString *Name, StorageType Storage,
3025 bool ShouldCreate = true);
3027 TempDIImportedEntity cloneImpl() const {
3028 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3029 getFile(), getLine(), getName());
3033 DEFINE_MDNODE_GET(DIImportedEntity,
3034 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3035 unsigned Line, StringRef Name = ""),
3036 (Tag, Scope, Entity, File, Line, Name))
3037 DEFINE_MDNODE_GET(DIImportedEntity,
3038 (unsigned Tag, Metadata *Scope, Metadata *Entity,
3039 Metadata *File, unsigned Line, MDString *Name),
3040 (Tag, Scope, Entity, File, Line, Name))
3042 TempDIImportedEntity clone() const { return cloneImpl(); }
3044 unsigned getLine() const { return Line; }
3045 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3046 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
3047 StringRef getName() const { return getStringOperand(2); }
3048 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3050 Metadata *getRawScope() const { return getOperand(0); }
3051 Metadata *getRawEntity() const { return getOperand(1); }
3052 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3053 Metadata *getRawFile() const { return getOperand(3); }
3055 static bool classof(const Metadata *MD) {
3056 return MD->getMetadataID() == DIImportedEntityKind;
3060 /// A pair of DIGlobalVariable and DIExpression.
3061 class DIGlobalVariableExpression : public MDNode {
3062 friend class LLVMContextImpl;
3063 friend class MDNode;
3065 DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
3066 ArrayRef<Metadata *> Ops)
3067 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
3068 ~DIGlobalVariableExpression() = default;
3070 static DIGlobalVariableExpression *
3071 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
3072 StorageType Storage, bool ShouldCreate = true);
3074 TempDIGlobalVariableExpression cloneImpl() const {
3075 return getTemporary(getContext(), getVariable(), getExpression());
3079 DEFINE_MDNODE_GET(DIGlobalVariableExpression,
3080 (Metadata * Variable, Metadata *Expression),
3081 (Variable, Expression))
3083 TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
3085 Metadata *getRawVariable() const { return getOperand(0); }
3087 DIGlobalVariable *getVariable() const {
3088 return cast_or_null<DIGlobalVariable>(getRawVariable());
3091 Metadata *getRawExpression() const { return getOperand(1); }
3093 DIExpression *getExpression() const {
3094 return cast<DIExpression>(getRawExpression());
3097 static bool classof(const Metadata *MD) {
3098 return MD->getMetadataID() == DIGlobalVariableExpressionKind;
3102 /// Macro Info DWARF-like metadata node.
3104 /// A metadata node with a DWARF macro info (i.e., a constant named
3105 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
3107 /// because it's potentially used for non-DWARF output.
3108 class DIMacroNode : public MDNode {
3109 friend class LLVMContextImpl;
3110 friend class MDNode;
3113 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
3114 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
3115 : MDNode(C, ID, Storage, Ops1, Ops2) {
3116 assert(MIType < 1u << 16);
3117 SubclassData16 = MIType;
3119 ~DIMacroNode() = default;
3121 template <class Ty> Ty *getOperandAs(unsigned I) const {
3122 return cast_or_null<Ty>(getOperand(I));
3125 StringRef getStringOperand(unsigned I) const {
3126 if (auto *S = getOperandAs<MDString>(I))
3127 return S->getString();
3131 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
3134 return MDString::get(Context, S);
3138 unsigned getMacinfoType() const { return SubclassData16; }
3140 static bool classof(const Metadata *MD) {
3141 switch (MD->getMetadataID()) {
3145 case DIMacroFileKind:
3151 class DIMacro : public DIMacroNode {
3152 friend class LLVMContextImpl;
3153 friend class MDNode;
3157 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
3158 ArrayRef<Metadata *> Ops)
3159 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
3160 ~DIMacro() = default;
3162 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3163 StringRef Name, StringRef Value, StorageType Storage,
3164 bool ShouldCreate = true) {
3165 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
3166 getCanonicalMDString(Context, Value), Storage, ShouldCreate);
3168 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3169 MDString *Name, MDString *Value, StorageType Storage,
3170 bool ShouldCreate = true);
3172 TempDIMacro cloneImpl() const {
3173 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
3178 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
3179 StringRef Value = ""),
3180 (MIType, Line, Name, Value))
3181 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
3183 (MIType, Line, Name, Value))
3185 TempDIMacro clone() const { return cloneImpl(); }
3187 unsigned getLine() const { return Line; }
3189 StringRef getName() const { return getStringOperand(0); }
3190 StringRef getValue() const { return getStringOperand(1); }
3192 MDString *getRawName() const { return getOperandAs<MDString>(0); }
3193 MDString *getRawValue() const { return getOperandAs<MDString>(1); }
3195 static bool classof(const Metadata *MD) {
3196 return MD->getMetadataID() == DIMacroKind;
3200 class DIMacroFile : public DIMacroNode {
3201 friend class LLVMContextImpl;
3202 friend class MDNode;
3206 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
3207 unsigned Line, ArrayRef<Metadata *> Ops)
3208 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
3209 ~DIMacroFile() = default;
3211 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
3212 unsigned Line, DIFile *File,
3213 DIMacroNodeArray Elements, StorageType Storage,
3214 bool ShouldCreate = true) {
3215 return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
3216 Elements.get(), Storage, ShouldCreate);
3219 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
3220 unsigned Line, Metadata *File, Metadata *Elements,
3221 StorageType Storage, bool ShouldCreate = true);
3223 TempDIMacroFile cloneImpl() const {
3224 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
3229 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
3230 DIMacroNodeArray Elements),
3231 (MIType, Line, File, Elements))
3232 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
3233 Metadata *File, Metadata *Elements),
3234 (MIType, Line, File, Elements))
3236 TempDIMacroFile clone() const { return cloneImpl(); }
3238 void replaceElements(DIMacroNodeArray Elements) {
3240 for (DIMacroNode *Op : getElements())
3241 assert(is_contained(Elements->operands(), Op) &&
3242 "Lost a macro node during macro node list replacement");
3244 replaceOperandWith(1, Elements.get());
3247 unsigned getLine() const { return Line; }
3248 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3250 DIMacroNodeArray getElements() const {
3251 return cast_or_null<MDTuple>(getRawElements());
3254 Metadata *getRawFile() const { return getOperand(0); }
3255 Metadata *getRawElements() const { return getOperand(1); }
3257 static bool classof(const Metadata *MD) {
3258 return MD->getMetadataID() == DIMacroFileKind;
3262 /// Identifies a unique instance of a variable.
3264 /// Storage for identifying a potentially inlined instance of a variable,
3265 /// or a fragment thereof. This guarantees that exactly one variable instance
3266 /// may be identified by this class, even when that variable is a fragment of
3267 /// an aggregate variable and/or there is another inlined instance of the same
3268 /// source code variable nearby.
3269 /// This class does not necessarily uniquely identify that variable: it is
3270 /// possible that a DebugVariable with different parameters may point to the
3271 /// same variable instance, but not that one DebugVariable points to multiple
3272 /// variable instances.
3273 class DebugVariable {
3274 using FragmentInfo = DIExpression::FragmentInfo;
3276 const DILocalVariable *Variable;
3277 Optional<FragmentInfo> Fragment;
3278 const DILocation *InlinedAt;
3280 /// Fragment that will overlap all other fragments. Used as default when
3281 /// caller demands a fragment.
3282 static const FragmentInfo DefaultFragment;
3285 DebugVariable(const DILocalVariable *Var, Optional<FragmentInfo> FragmentInfo,
3286 const DILocation *InlinedAt)
3287 : Variable(Var), Fragment(FragmentInfo), InlinedAt(InlinedAt) {}
3289 DebugVariable(const DILocalVariable *Var, const DIExpression *DIExpr,
3290 const DILocation *InlinedAt)
3292 Fragment(DIExpr ? DIExpr->getFragmentInfo() : NoneType()),
3293 InlinedAt(InlinedAt) {}
3295 const DILocalVariable *getVariable() const { return Variable; }
3296 const Optional<FragmentInfo> getFragment() const { return Fragment; }
3297 const DILocation *getInlinedAt() const { return InlinedAt; }
3299 const FragmentInfo getFragmentOrDefault() const {
3300 return Fragment.getValueOr(DefaultFragment);
3303 static bool isDefaultFragment(const FragmentInfo F) {
3304 return F == DefaultFragment;
3307 bool operator==(const DebugVariable &Other) const {
3308 return std::tie(Variable, Fragment, InlinedAt) ==
3309 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
3312 bool operator<(const DebugVariable &Other) const {
3313 return std::tie(Variable, Fragment, InlinedAt) <
3314 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
3318 template <> struct DenseMapInfo<DebugVariable> {
3319 using FragmentInfo = DIExpression::FragmentInfo;
3321 /// Empty key: no key should be generated that has no DILocalVariable.
3322 static inline DebugVariable getEmptyKey() {
3323 return DebugVariable(nullptr, NoneType(), nullptr);
3326 /// Difference in tombstone is that the Optional is meaningful.
3327 static inline DebugVariable getTombstoneKey() {
3328 return DebugVariable(nullptr, {{0, 0}}, nullptr);
3331 static unsigned getHashValue(const DebugVariable &D) {
3333 const Optional<FragmentInfo> Fragment = D.getFragment();
3335 HV = DenseMapInfo<FragmentInfo>::getHashValue(*Fragment);
3337 return hash_combine(D.getVariable(), HV, D.getInlinedAt());
3340 static bool isEqual(const DebugVariable &A, const DebugVariable &B) {
3345 } // end namespace llvm
3347 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
3348 #undef DEFINE_MDNODE_GET_UNPACK
3349 #undef DEFINE_MDNODE_GET
3351 #endif // LLVM_IR_DEBUGINFOMETADATA_H