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 /// Holds a subclass of DINode.
65 /// FIXME: This class doesn't currently make much sense. Previously it was a
66 /// union beteen MDString (for ODR-uniqued types) and things like DIType. To
67 /// support CodeView work, it wasn't deleted outright when MDString-based type
68 /// references were deleted; we'll soon need a similar concept for CodeView
70 template <class T
> class TypedDINodeRef
{
71 const Metadata
*MD
= nullptr;
74 TypedDINodeRef() = default;
75 TypedDINodeRef(std::nullptr_t
) {}
76 TypedDINodeRef(const T
*MD
) : MD(MD
) {}
78 explicit TypedDINodeRef(const Metadata
*MD
) : MD(MD
) {
79 assert((!MD
|| isa
<T
>(MD
)) && "Expected valid type ref");
84 const TypedDINodeRef
<U
> &X
,
85 typename
std::enable_if
<std::is_convertible
<U
*, T
*>::value
>::type
* =
89 operator Metadata
*() const { return const_cast<Metadata
*>(MD
); }
91 T
*resolve() const { return const_cast<T
*>(cast_or_null
<T
>(MD
)); }
93 bool operator==(const TypedDINodeRef
<T
> &X
) const { return MD
== X
.MD
; }
94 bool operator!=(const TypedDINodeRef
<T
> &X
) const { return MD
!= X
.MD
; }
97 using DINodeRef
= TypedDINodeRef
<DINode
>;
98 using DIScopeRef
= TypedDINodeRef
<DIScope
>;
99 using DITypeRef
= TypedDINodeRef
<DIType
>;
101 class DITypeRefArray
{
102 const MDTuple
*N
= nullptr;
105 DITypeRefArray() = default;
106 DITypeRefArray(const MDTuple
*N
) : N(N
) {}
108 explicit operator bool() const { return get(); }
109 explicit operator MDTuple
*() const { return get(); }
111 MDTuple
*get() const { return const_cast<MDTuple
*>(N
); }
112 MDTuple
*operator->() const { return get(); }
113 MDTuple
&operator*() const { return *get(); }
115 // FIXME: Fix callers and remove condition on N.
116 unsigned size() const { return N
? N
->getNumOperands() : 0u; }
117 DITypeRef
operator[](unsigned I
) const { return DITypeRef(N
->getOperand(I
)); }
119 class iterator
: std::iterator
<std::input_iterator_tag
, DITypeRef
,
120 std::ptrdiff_t, void, DITypeRef
> {
121 MDNode::op_iterator I
= nullptr;
124 iterator() = default;
125 explicit iterator(MDNode::op_iterator I
) : I(I
) {}
127 DITypeRef
operator*() const { return DITypeRef(*I
); }
129 iterator
&operator++() {
134 iterator
operator++(int) {
135 iterator
Temp(*this);
140 bool operator==(const iterator
&X
) const { return I
== X
.I
; }
141 bool operator!=(const iterator
&X
) const { return I
!= X
.I
; }
144 // FIXME: Fix callers and remove condition on N.
145 iterator
begin() const { return N
? iterator(N
->op_begin()) : iterator(); }
146 iterator
end() const { return N
? iterator(N
->op_end()) : iterator(); }
149 /// Tagged DWARF-like metadata node.
151 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
152 /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
153 /// potentially used for non-DWARF output.
154 class DINode
: public MDNode
{
155 friend class LLVMContextImpl
;
159 DINode(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned Tag
,
160 ArrayRef
<Metadata
*> Ops1
, ArrayRef
<Metadata
*> Ops2
= None
)
161 : MDNode(C
, ID
, Storage
, Ops1
, Ops2
) {
162 assert(Tag
< 1u << 16);
163 SubclassData16
= Tag
;
167 template <class Ty
> Ty
*getOperandAs(unsigned I
) const {
168 return cast_or_null
<Ty
>(getOperand(I
));
171 StringRef
getStringOperand(unsigned I
) const {
172 if (auto *S
= getOperandAs
<MDString
>(I
))
173 return S
->getString();
177 static MDString
*getCanonicalMDString(LLVMContext
&Context
, StringRef S
) {
180 return MDString::get(Context
, S
);
183 /// Allow subclasses to mutate the tag.
184 void setTag(unsigned Tag
) { SubclassData16
= Tag
; }
187 unsigned getTag() const { return SubclassData16
; }
189 /// Debug info flags.
191 /// The three accessibility flags are mutually exclusive and rolled together
192 /// in the first two bits.
193 enum DIFlags
: uint32_t {
194 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
195 #define DI_FLAG_LARGEST_NEEDED
196 #include "llvm/IR/DebugInfoFlags.def"
197 FlagAccessibility
= FlagPrivate
| FlagProtected
| FlagPublic
,
198 FlagPtrToMemberRep
= FlagSingleInheritance
| FlagMultipleInheritance
|
199 FlagVirtualInheritance
,
200 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest
)
203 static DIFlags
getFlag(StringRef Flag
);
204 static StringRef
getFlagString(DIFlags Flag
);
206 /// Split up a flags bitfield.
208 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
209 /// any remaining (unrecognized) bits.
210 static DIFlags
splitFlags(DIFlags Flags
,
211 SmallVectorImpl
<DIFlags
> &SplitFlags
);
213 static bool classof(const Metadata
*MD
) {
214 switch (MD
->getMetadataID()) {
217 case GenericDINodeKind
:
219 case DIEnumeratorKind
:
220 case DIBasicTypeKind
:
221 case DIDerivedTypeKind
:
222 case DICompositeTypeKind
:
223 case DISubroutineTypeKind
:
225 case DICompileUnitKind
:
226 case DISubprogramKind
:
227 case DILexicalBlockKind
:
228 case DILexicalBlockFileKind
:
229 case DINamespaceKind
:
230 case DITemplateTypeParameterKind
:
231 case DITemplateValueParameterKind
:
232 case DIGlobalVariableKind
:
233 case DILocalVariableKind
:
235 case DIObjCPropertyKind
:
236 case DIImportedEntityKind
:
243 template <class T
> struct simplify_type
<const TypedDINodeRef
<T
>> {
244 using SimpleType
= Metadata
*;
246 static SimpleType
getSimplifiedValue(const TypedDINodeRef
<T
> &MD
) {
252 struct simplify_type
<TypedDINodeRef
<T
>>
253 : simplify_type
<const TypedDINodeRef
<T
>> {};
255 /// Generic tagged DWARF-like metadata node.
257 /// An un-specialized DWARF-like metadata node. The first operand is a
258 /// (possibly empty) null-separated \a MDString header that contains arbitrary
259 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
260 /// to other metadata.
261 class GenericDINode
: public DINode
{
262 friend class LLVMContextImpl
;
265 GenericDINode(LLVMContext
&C
, StorageType Storage
, unsigned Hash
,
266 unsigned Tag
, ArrayRef
<Metadata
*> Ops1
,
267 ArrayRef
<Metadata
*> Ops2
)
268 : DINode(C
, GenericDINodeKind
, Storage
, Tag
, Ops1
, Ops2
) {
271 ~GenericDINode() { dropAllReferences(); }
273 void setHash(unsigned Hash
) { SubclassData32
= Hash
; }
274 void recalculateHash();
276 static GenericDINode
*getImpl(LLVMContext
&Context
, unsigned Tag
,
277 StringRef Header
, ArrayRef
<Metadata
*> DwarfOps
,
278 StorageType Storage
, bool ShouldCreate
= true) {
279 return getImpl(Context
, Tag
, getCanonicalMDString(Context
, Header
),
280 DwarfOps
, Storage
, ShouldCreate
);
283 static GenericDINode
*getImpl(LLVMContext
&Context
, unsigned Tag
,
284 MDString
*Header
, ArrayRef
<Metadata
*> DwarfOps
,
285 StorageType Storage
, bool ShouldCreate
= true);
287 TempGenericDINode
cloneImpl() const {
289 getContext(), getTag(), getHeader(),
290 SmallVector
<Metadata
*, 4>(dwarf_op_begin(), dwarf_op_end()));
294 unsigned getHash() const { return SubclassData32
; }
296 DEFINE_MDNODE_GET(GenericDINode
, (unsigned Tag
, StringRef Header
,
297 ArrayRef
<Metadata
*> DwarfOps
),
298 (Tag
, Header
, DwarfOps
))
299 DEFINE_MDNODE_GET(GenericDINode
, (unsigned Tag
, MDString
*Header
,
300 ArrayRef
<Metadata
*> DwarfOps
),
301 (Tag
, Header
, DwarfOps
))
303 /// Return a (temporary) clone of this.
304 TempGenericDINode
clone() const { return cloneImpl(); }
306 unsigned getTag() const { return SubclassData16
; }
307 StringRef
getHeader() const { return getStringOperand(0); }
308 MDString
*getRawHeader() const { return getOperandAs
<MDString
>(0); }
310 op_iterator
dwarf_op_begin() const { return op_begin() + 1; }
311 op_iterator
dwarf_op_end() const { return op_end(); }
312 op_range
dwarf_operands() const {
313 return op_range(dwarf_op_begin(), dwarf_op_end());
316 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
317 const MDOperand
&getDwarfOperand(unsigned I
) const {
318 return getOperand(I
+ 1);
320 void replaceDwarfOperandWith(unsigned I
, Metadata
*New
) {
321 replaceOperandWith(I
+ 1, New
);
324 static bool classof(const Metadata
*MD
) {
325 return MD
->getMetadataID() == GenericDINodeKind
;
331 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
333 class DISubrange
: public DINode
{
334 friend class LLVMContextImpl
;
339 DISubrange(LLVMContext
&C
, StorageType Storage
, Metadata
*Node
,
340 int64_t LowerBound
, ArrayRef
<Metadata
*> Ops
)
341 : DINode(C
, DISubrangeKind
, Storage
, dwarf::DW_TAG_subrange_type
, Ops
),
342 LowerBound(LowerBound
) {}
344 ~DISubrange() = default;
346 static DISubrange
*getImpl(LLVMContext
&Context
, int64_t Count
,
347 int64_t LowerBound
, StorageType Storage
,
348 bool ShouldCreate
= true);
350 static DISubrange
*getImpl(LLVMContext
&Context
, Metadata
*CountNode
,
351 int64_t LowerBound
, StorageType Storage
,
352 bool ShouldCreate
= true);
354 TempDISubrange
cloneImpl() const {
355 return getTemporary(getContext(), getRawCountNode(), getLowerBound());
359 DEFINE_MDNODE_GET(DISubrange
, (int64_t Count
, int64_t LowerBound
= 0),
362 DEFINE_MDNODE_GET(DISubrange
, (Metadata
*CountNode
, int64_t LowerBound
= 0),
363 (CountNode
, LowerBound
))
365 TempDISubrange
clone() const { return cloneImpl(); }
367 int64_t getLowerBound() const { return LowerBound
; }
369 Metadata
*getRawCountNode() const {
370 return getOperand(0).get();
373 typedef PointerUnion
<ConstantInt
*, DIVariable
*> CountType
;
375 CountType
getCount() const {
376 if (auto *MD
= dyn_cast
<ConstantAsMetadata
>(getRawCountNode()))
377 return CountType(cast
<ConstantInt
>(MD
->getValue()));
379 if (auto *DV
= dyn_cast
<DIVariable
>(getRawCountNode()))
380 return CountType(DV
);
385 static bool classof(const Metadata
*MD
) {
386 return MD
->getMetadataID() == DISubrangeKind
;
390 /// Enumeration value.
392 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
393 /// longer creates a type cycle.
394 class DIEnumerator
: public DINode
{
395 friend class LLVMContextImpl
;
399 DIEnumerator(LLVMContext
&C
, StorageType Storage
, int64_t Value
,
400 bool IsUnsigned
, ArrayRef
<Metadata
*> Ops
)
401 : DINode(C
, DIEnumeratorKind
, Storage
, dwarf::DW_TAG_enumerator
, Ops
),
403 SubclassData32
= IsUnsigned
;
405 ~DIEnumerator() = default;
407 static DIEnumerator
*getImpl(LLVMContext
&Context
, int64_t Value
,
408 bool IsUnsigned
, StringRef Name
,
409 StorageType Storage
, bool ShouldCreate
= true) {
410 return getImpl(Context
, Value
, IsUnsigned
,
411 getCanonicalMDString(Context
, Name
), Storage
, ShouldCreate
);
413 static DIEnumerator
*getImpl(LLVMContext
&Context
, int64_t Value
,
414 bool IsUnsigned
, MDString
*Name
,
415 StorageType Storage
, bool ShouldCreate
= true);
417 TempDIEnumerator
cloneImpl() const {
418 return getTemporary(getContext(), getValue(), isUnsigned(), getName());
422 DEFINE_MDNODE_GET(DIEnumerator
, (int64_t Value
, bool IsUnsigned
, StringRef Name
),
423 (Value
, IsUnsigned
, Name
))
424 DEFINE_MDNODE_GET(DIEnumerator
, (int64_t Value
, bool IsUnsigned
, MDString
*Name
),
425 (Value
, IsUnsigned
, Name
))
427 TempDIEnumerator
clone() const { return cloneImpl(); }
429 int64_t getValue() const { return Value
; }
430 bool isUnsigned() const { return SubclassData32
; }
431 StringRef
getName() const { return getStringOperand(0); }
433 MDString
*getRawName() const { return getOperandAs
<MDString
>(0); }
435 static bool classof(const Metadata
*MD
) {
436 return MD
->getMetadataID() == DIEnumeratorKind
;
440 /// Base class for scope-like contexts.
442 /// Base class for lexical scopes and types (which are also declaration
445 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
446 class DIScope
: public DINode
{
448 DIScope(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned Tag
,
449 ArrayRef
<Metadata
*> Ops
)
450 : DINode(C
, ID
, Storage
, Tag
, Ops
) {}
451 ~DIScope() = default;
454 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
456 inline StringRef
getFilename() const;
457 inline StringRef
getDirectory() const;
458 inline Optional
<StringRef
> getSource() const;
460 StringRef
getName() const;
461 DIScopeRef
getScope() const;
463 /// Return the raw underlying file.
465 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
466 /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
467 /// Otherwise, return the first operand, which is where all other subclasses
468 /// store their file pointer.
469 Metadata
*getRawFile() const {
470 return isa
<DIFile
>(this) ? const_cast<DIScope
*>(this)
471 : static_cast<Metadata
*>(getOperand(0));
474 static bool classof(const Metadata
*MD
) {
475 switch (MD
->getMetadataID()) {
478 case DIBasicTypeKind
:
479 case DIDerivedTypeKind
:
480 case DICompositeTypeKind
:
481 case DISubroutineTypeKind
:
483 case DICompileUnitKind
:
484 case DISubprogramKind
:
485 case DILexicalBlockKind
:
486 case DILexicalBlockFileKind
:
487 case DINamespaceKind
:
496 /// TODO: Merge with directory/file node (including users).
497 /// TODO: Canonicalize paths on creation.
498 class DIFile
: public DIScope
{
499 friend class LLVMContextImpl
;
503 /// Which algorithm (e.g. MD5) a checksum was generated with.
505 /// The encoding is explicit because it is used directly in Bitcode. The
506 /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
508 // The first variant was originally CSK_None, encoded as 0. The new
509 // internal representation removes the need for this by wrapping the
510 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
511 // encoding is reserved.
514 CSK_Last
= CSK_SHA1
// Should be last enumeration.
517 /// A single checksum, represented by a \a Kind and a \a Value (a string).
518 template <typename T
>
519 struct ChecksumInfo
{
520 /// The kind of checksum which \a Value encodes.
522 /// The string value of the checksum.
525 ChecksumInfo(ChecksumKind Kind
, T Value
) : Kind(Kind
), Value(Value
) { }
526 ~ChecksumInfo() = default;
527 bool operator==(const ChecksumInfo
<T
> &X
) const {
528 return Kind
== X
.Kind
&& Value
== X
.Value
;
530 bool operator!=(const ChecksumInfo
<T
> &X
) const { return !(*this == X
); }
531 StringRef
getKindAsString() const { return getChecksumKindAsString(Kind
); }
535 Optional
<ChecksumInfo
<MDString
*>> Checksum
;
536 Optional
<MDString
*> Source
;
538 DIFile(LLVMContext
&C
, StorageType Storage
,
539 Optional
<ChecksumInfo
<MDString
*>> CS
, Optional
<MDString
*> Src
,
540 ArrayRef
<Metadata
*> Ops
)
541 : DIScope(C
, DIFileKind
, Storage
, dwarf::DW_TAG_file_type
, Ops
),
542 Checksum(CS
), Source(Src
) {}
545 static DIFile
*getImpl(LLVMContext
&Context
, StringRef Filename
,
547 Optional
<ChecksumInfo
<StringRef
>> CS
,
548 Optional
<StringRef
> Source
,
549 StorageType Storage
, bool ShouldCreate
= true) {
550 Optional
<ChecksumInfo
<MDString
*>> MDChecksum
;
552 MDChecksum
.emplace(CS
->Kind
, getCanonicalMDString(Context
, CS
->Value
));
553 return getImpl(Context
, getCanonicalMDString(Context
, Filename
),
554 getCanonicalMDString(Context
, Directory
), MDChecksum
,
555 Source
? Optional
<MDString
*>(getCanonicalMDString(Context
, *Source
)) : None
,
556 Storage
, ShouldCreate
);
558 static DIFile
*getImpl(LLVMContext
&Context
, MDString
*Filename
,
560 Optional
<ChecksumInfo
<MDString
*>> CS
,
561 Optional
<MDString
*> Source
, StorageType Storage
,
562 bool ShouldCreate
= true);
564 TempDIFile
cloneImpl() const {
565 return getTemporary(getContext(), getFilename(), getDirectory(),
566 getChecksum(), getSource());
570 DEFINE_MDNODE_GET(DIFile
, (StringRef Filename
, StringRef Directory
,
571 Optional
<ChecksumInfo
<StringRef
>> CS
= None
,
572 Optional
<StringRef
> Source
= None
),
573 (Filename
, Directory
, CS
, Source
))
574 DEFINE_MDNODE_GET(DIFile
, (MDString
* Filename
, MDString
*Directory
,
575 Optional
<ChecksumInfo
<MDString
*>> CS
= None
,
576 Optional
<MDString
*> Source
= None
),
577 (Filename
, Directory
, CS
, Source
))
579 TempDIFile
clone() const { return cloneImpl(); }
581 StringRef
getFilename() const { return getStringOperand(0); }
582 StringRef
getDirectory() const { return getStringOperand(1); }
583 Optional
<ChecksumInfo
<StringRef
>> getChecksum() const {
584 Optional
<ChecksumInfo
<StringRef
>> StringRefChecksum
;
586 StringRefChecksum
.emplace(Checksum
->Kind
, Checksum
->Value
->getString());
587 return StringRefChecksum
;
589 Optional
<StringRef
> getSource() const {
590 return Source
? Optional
<StringRef
>((*Source
)->getString()) : None
;
593 MDString
*getRawFilename() const { return getOperandAs
<MDString
>(0); }
594 MDString
*getRawDirectory() const { return getOperandAs
<MDString
>(1); }
595 Optional
<ChecksumInfo
<MDString
*>> getRawChecksum() const { return Checksum
; }
596 Optional
<MDString
*> getRawSource() const { return Source
; }
598 static StringRef
getChecksumKindAsString(ChecksumKind CSKind
);
599 static Optional
<ChecksumKind
> getChecksumKind(StringRef CSKindStr
);
601 static bool classof(const Metadata
*MD
) {
602 return MD
->getMetadataID() == DIFileKind
;
606 StringRef
DIScope::getFilename() const {
607 if (auto *F
= getFile())
608 return F
->getFilename();
612 StringRef
DIScope::getDirectory() const {
613 if (auto *F
= getFile())
614 return F
->getDirectory();
618 Optional
<StringRef
> DIScope::getSource() const {
619 if (auto *F
= getFile())
620 return F
->getSource();
624 /// Base class for types.
626 /// TODO: Remove the hardcoded name and context, since many types don't use
628 /// TODO: Split up flags.
629 class DIType
: public DIScope
{
633 uint64_t OffsetInBits
;
634 uint32_t AlignInBits
;
637 DIType(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned Tag
,
638 unsigned Line
, uint64_t SizeInBits
, uint32_t AlignInBits
,
639 uint64_t OffsetInBits
, DIFlags Flags
, ArrayRef
<Metadata
*> Ops
)
640 : DIScope(C
, ID
, Storage
, Tag
, Ops
) {
641 init(Line
, SizeInBits
, AlignInBits
, OffsetInBits
, Flags
);
645 void init(unsigned Line
, uint64_t SizeInBits
, uint32_t AlignInBits
,
646 uint64_t OffsetInBits
, DIFlags Flags
) {
649 this->SizeInBits
= SizeInBits
;
650 this->AlignInBits
= AlignInBits
;
651 this->OffsetInBits
= OffsetInBits
;
654 /// Change fields in place.
655 void mutate(unsigned Tag
, unsigned Line
, uint64_t SizeInBits
,
656 uint32_t AlignInBits
, uint64_t OffsetInBits
, DIFlags Flags
) {
657 assert(isDistinct() && "Only distinct nodes can mutate");
659 init(Line
, SizeInBits
, AlignInBits
, OffsetInBits
, Flags
);
663 TempDIType
clone() const {
664 return TempDIType(cast
<DIType
>(MDNode::clone().release()));
667 unsigned getLine() const { return Line
; }
668 uint64_t getSizeInBits() const { return SizeInBits
; }
669 uint32_t getAlignInBits() const { return AlignInBits
; }
670 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT
; }
671 uint64_t getOffsetInBits() const { return OffsetInBits
; }
672 DIFlags
getFlags() const { return Flags
; }
674 DIScopeRef
getScope() const { return DIScopeRef(getRawScope()); }
675 StringRef
getName() const { return getStringOperand(2); }
678 Metadata
*getRawScope() const { return getOperand(1); }
679 MDString
*getRawName() const { return getOperandAs
<MDString
>(2); }
681 /// Returns a new temporary DIType with updated Flags
682 TempDIType
cloneWithFlags(DIFlags NewFlags
) const {
683 auto NewTy
= clone();
684 NewTy
->Flags
= NewFlags
;
688 bool isPrivate() const {
689 return (getFlags() & FlagAccessibility
) == FlagPrivate
;
691 bool isProtected() const {
692 return (getFlags() & FlagAccessibility
) == FlagProtected
;
694 bool isPublic() const {
695 return (getFlags() & FlagAccessibility
) == FlagPublic
;
697 bool isForwardDecl() const { return getFlags() & FlagFwdDecl
; }
698 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock
; }
699 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct
; }
700 bool isVirtual() const { return getFlags() & FlagVirtual
; }
701 bool isArtificial() const { return getFlags() & FlagArtificial
; }
702 bool isObjectPointer() const { return getFlags() & FlagObjectPointer
; }
703 bool isObjcClassComplete() const {
704 return getFlags() & FlagObjcClassComplete
;
706 bool isVector() const { return getFlags() & FlagVector
; }
707 bool isBitField() const { return getFlags() & FlagBitField
; }
708 bool isStaticMember() const { return getFlags() & FlagStaticMember
; }
709 bool isLValueReference() const { return getFlags() & FlagLValueReference
; }
710 bool isRValueReference() const { return getFlags() & FlagRValueReference
; }
711 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue
; }
712 bool isTypePassByReference() const {
713 return getFlags() & FlagTypePassByReference
;
715 bool isBigEndian() const { return getFlags() & FlagBigEndian
; }
716 bool isLittleEndian() const { return getFlags() & FlagLittleEndian
; }
718 static bool classof(const Metadata
*MD
) {
719 switch (MD
->getMetadataID()) {
722 case DIBasicTypeKind
:
723 case DIDerivedTypeKind
:
724 case DICompositeTypeKind
:
725 case DISubroutineTypeKind
:
731 /// Basic type, like 'int' or 'float'.
733 /// TODO: Split out DW_TAG_unspecified_type.
734 /// TODO: Drop unused accessors.
735 class DIBasicType
: public DIType
{
736 friend class LLVMContextImpl
;
741 DIBasicType(LLVMContext
&C
, StorageType Storage
, unsigned Tag
,
742 uint64_t SizeInBits
, uint32_t AlignInBits
, unsigned Encoding
,
743 DIFlags Flags
, ArrayRef
<Metadata
*> Ops
)
744 : DIType(C
, DIBasicTypeKind
, Storage
, Tag
, 0, SizeInBits
, AlignInBits
, 0,
746 Encoding(Encoding
) {}
747 ~DIBasicType() = default;
749 static DIBasicType
*getImpl(LLVMContext
&Context
, unsigned Tag
,
750 StringRef Name
, uint64_t SizeInBits
,
751 uint32_t AlignInBits
, unsigned Encoding
,
752 DIFlags Flags
, StorageType Storage
,
753 bool ShouldCreate
= true) {
754 return getImpl(Context
, Tag
, getCanonicalMDString(Context
, Name
),
755 SizeInBits
, AlignInBits
, Encoding
, Flags
, Storage
,
758 static DIBasicType
*getImpl(LLVMContext
&Context
, unsigned Tag
,
759 MDString
*Name
, uint64_t SizeInBits
,
760 uint32_t AlignInBits
, unsigned Encoding
,
761 DIFlags Flags
, StorageType Storage
,
762 bool ShouldCreate
= true);
764 TempDIBasicType
cloneImpl() const {
765 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
766 getAlignInBits(), getEncoding(), getFlags());
770 DEFINE_MDNODE_GET(DIBasicType
, (unsigned Tag
, StringRef Name
),
771 (Tag
, Name
, 0, 0, 0, FlagZero
))
772 DEFINE_MDNODE_GET(DIBasicType
,
773 (unsigned Tag
, StringRef Name
, uint64_t SizeInBits
,
774 uint32_t AlignInBits
, unsigned Encoding
, DIFlags Flags
),
775 (Tag
, Name
, SizeInBits
, AlignInBits
, Encoding
, Flags
))
776 DEFINE_MDNODE_GET(DIBasicType
,
777 (unsigned Tag
, MDString
*Name
, uint64_t SizeInBits
,
778 uint32_t AlignInBits
, unsigned Encoding
, DIFlags Flags
),
779 (Tag
, Name
, SizeInBits
, AlignInBits
, Encoding
, Flags
))
781 TempDIBasicType
clone() const { return cloneImpl(); }
783 unsigned getEncoding() const { return Encoding
; }
785 enum class Signedness
{ Signed
, Unsigned
};
787 /// Return the signedness of this type, or None if this type is neither
788 /// signed nor unsigned.
789 Optional
<Signedness
> getSignedness() const;
791 static bool classof(const Metadata
*MD
) {
792 return MD
->getMetadataID() == DIBasicTypeKind
;
798 /// This includes qualified types, pointers, references, friends, typedefs, and
801 /// TODO: Split out members (inheritance, fields, methods, etc.).
802 class DIDerivedType
: public DIType
{
803 friend class LLVMContextImpl
;
806 /// The DWARF address space of the memory pointed to or referenced by a
807 /// pointer or reference type respectively.
808 Optional
<unsigned> DWARFAddressSpace
;
810 DIDerivedType(LLVMContext
&C
, StorageType Storage
, unsigned Tag
,
811 unsigned Line
, uint64_t SizeInBits
, uint32_t AlignInBits
,
812 uint64_t OffsetInBits
, Optional
<unsigned> DWARFAddressSpace
,
813 DIFlags Flags
, ArrayRef
<Metadata
*> Ops
)
814 : DIType(C
, DIDerivedTypeKind
, Storage
, Tag
, Line
, SizeInBits
,
815 AlignInBits
, OffsetInBits
, Flags
, Ops
),
816 DWARFAddressSpace(DWARFAddressSpace
) {}
817 ~DIDerivedType() = default;
819 static DIDerivedType
*getImpl(LLVMContext
&Context
, unsigned Tag
,
820 StringRef Name
, DIFile
*File
, unsigned Line
,
821 DIScopeRef Scope
, DITypeRef BaseType
,
822 uint64_t SizeInBits
, uint32_t AlignInBits
,
823 uint64_t OffsetInBits
,
824 Optional
<unsigned> DWARFAddressSpace
,
825 DIFlags Flags
, Metadata
*ExtraData
,
826 StorageType Storage
, bool ShouldCreate
= true) {
827 return getImpl(Context
, Tag
, getCanonicalMDString(Context
, Name
), File
,
828 Line
, Scope
, BaseType
, SizeInBits
, AlignInBits
, OffsetInBits
,
829 DWARFAddressSpace
, Flags
, ExtraData
, Storage
, ShouldCreate
);
831 static DIDerivedType
*getImpl(LLVMContext
&Context
, unsigned Tag
,
832 MDString
*Name
, Metadata
*File
, unsigned Line
,
833 Metadata
*Scope
, Metadata
*BaseType
,
834 uint64_t SizeInBits
, uint32_t AlignInBits
,
835 uint64_t OffsetInBits
,
836 Optional
<unsigned> DWARFAddressSpace
,
837 DIFlags Flags
, Metadata
*ExtraData
,
838 StorageType Storage
, bool ShouldCreate
= true);
840 TempDIDerivedType
cloneImpl() const {
841 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
842 getScope(), getBaseType(), getSizeInBits(),
843 getAlignInBits(), getOffsetInBits(),
844 getDWARFAddressSpace(), getFlags(), getExtraData());
848 DEFINE_MDNODE_GET(DIDerivedType
,
849 (unsigned Tag
, MDString
*Name
, Metadata
*File
,
850 unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
,
851 uint64_t SizeInBits
, uint32_t AlignInBits
,
852 uint64_t OffsetInBits
,
853 Optional
<unsigned> DWARFAddressSpace
, DIFlags Flags
,
854 Metadata
*ExtraData
= nullptr),
855 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
856 AlignInBits
, OffsetInBits
, DWARFAddressSpace
, Flags
,
858 DEFINE_MDNODE_GET(DIDerivedType
,
859 (unsigned Tag
, StringRef Name
, DIFile
*File
, unsigned Line
,
860 DIScopeRef Scope
, DITypeRef BaseType
, uint64_t SizeInBits
,
861 uint32_t AlignInBits
, uint64_t OffsetInBits
,
862 Optional
<unsigned> DWARFAddressSpace
, DIFlags Flags
,
863 Metadata
*ExtraData
= nullptr),
864 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
865 AlignInBits
, OffsetInBits
, DWARFAddressSpace
, Flags
,
868 TempDIDerivedType
clone() const { return cloneImpl(); }
870 /// Get the base type this is derived from.
871 DITypeRef
getBaseType() const { return DITypeRef(getRawBaseType()); }
872 Metadata
*getRawBaseType() const { return getOperand(3); }
874 /// \returns The DWARF address space of the memory pointed to or referenced by
875 /// a pointer or reference type respectively.
876 Optional
<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace
; }
878 /// Get extra data associated with this derived type.
880 /// Class type for pointer-to-members, objective-c property node for ivars,
881 /// global constant wrapper for static members, or virtual base pointer offset
884 /// TODO: Separate out types that need this extra operand: pointer-to-member
885 /// types and member fields (static members and ivars).
886 Metadata
*getExtraData() const { return getRawExtraData(); }
887 Metadata
*getRawExtraData() const { return getOperand(4); }
889 /// Get casted version of extra data.
891 DITypeRef
getClassType() const {
892 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type
);
893 return DITypeRef(getExtraData());
896 DIObjCProperty
*getObjCProperty() const {
897 return dyn_cast_or_null
<DIObjCProperty
>(getExtraData());
900 uint32_t getVBPtrOffset() const {
901 assert(getTag() == dwarf::DW_TAG_inheritance
);
902 if (auto *CM
= cast_or_null
<ConstantAsMetadata
>(getExtraData()))
903 if (auto *CI
= dyn_cast_or_null
<ConstantInt
>(CM
->getValue()))
904 return static_cast<uint32_t>(CI
->getZExtValue());
908 Constant
*getStorageOffsetInBits() const {
909 assert(getTag() == dwarf::DW_TAG_member
&& isBitField());
910 if (auto *C
= cast_or_null
<ConstantAsMetadata
>(getExtraData()))
911 return C
->getValue();
915 Constant
*getConstant() const {
916 assert(getTag() == dwarf::DW_TAG_member
&& isStaticMember());
917 if (auto *C
= cast_or_null
<ConstantAsMetadata
>(getExtraData()))
918 return C
->getValue();
921 Constant
*getDiscriminantValue() const {
922 assert(getTag() == dwarf::DW_TAG_member
&& !isStaticMember());
923 if (auto *C
= cast_or_null
<ConstantAsMetadata
>(getExtraData()))
924 return C
->getValue();
929 static bool classof(const Metadata
*MD
) {
930 return MD
->getMetadataID() == DIDerivedTypeKind
;
936 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
937 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
938 class DICompositeType
: public DIType
{
939 friend class LLVMContextImpl
;
942 unsigned RuntimeLang
;
944 DICompositeType(LLVMContext
&C
, StorageType Storage
, unsigned Tag
,
945 unsigned Line
, unsigned RuntimeLang
, uint64_t SizeInBits
,
946 uint32_t AlignInBits
, uint64_t OffsetInBits
, DIFlags Flags
,
947 ArrayRef
<Metadata
*> Ops
)
948 : DIType(C
, DICompositeTypeKind
, Storage
, Tag
, Line
, SizeInBits
,
949 AlignInBits
, OffsetInBits
, Flags
, Ops
),
950 RuntimeLang(RuntimeLang
) {}
951 ~DICompositeType() = default;
953 /// Change fields in place.
954 void mutate(unsigned Tag
, unsigned Line
, unsigned RuntimeLang
,
955 uint64_t SizeInBits
, uint32_t AlignInBits
,
956 uint64_t OffsetInBits
, DIFlags Flags
) {
957 assert(isDistinct() && "Only distinct nodes can mutate");
958 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
959 this->RuntimeLang
= RuntimeLang
;
960 DIType::mutate(Tag
, Line
, SizeInBits
, AlignInBits
, OffsetInBits
, Flags
);
963 static DICompositeType
*
964 getImpl(LLVMContext
&Context
, unsigned Tag
, StringRef Name
, Metadata
*File
,
965 unsigned Line
, DIScopeRef Scope
, DITypeRef BaseType
,
966 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
967 DIFlags Flags
, DINodeArray Elements
, unsigned RuntimeLang
,
968 DITypeRef VTableHolder
, DITemplateParameterArray TemplateParams
,
969 StringRef Identifier
, DIDerivedType
*Discriminator
,
970 StorageType Storage
, bool ShouldCreate
= true) {
972 Context
, Tag
, getCanonicalMDString(Context
, Name
), File
, Line
, Scope
,
973 BaseType
, SizeInBits
, AlignInBits
, OffsetInBits
, Flags
, Elements
.get(),
974 RuntimeLang
, VTableHolder
, TemplateParams
.get(),
975 getCanonicalMDString(Context
, Identifier
), Discriminator
, Storage
, ShouldCreate
);
977 static DICompositeType
*
978 getImpl(LLVMContext
&Context
, unsigned Tag
, MDString
*Name
, Metadata
*File
,
979 unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
,
980 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
981 DIFlags Flags
, Metadata
*Elements
, unsigned RuntimeLang
,
982 Metadata
*VTableHolder
, Metadata
*TemplateParams
,
983 MDString
*Identifier
, Metadata
*Discriminator
,
984 StorageType Storage
, bool ShouldCreate
= true);
986 TempDICompositeType
cloneImpl() const {
987 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
988 getScope(), getBaseType(), getSizeInBits(),
989 getAlignInBits(), getOffsetInBits(), getFlags(),
990 getElements(), getRuntimeLang(), getVTableHolder(),
991 getTemplateParams(), getIdentifier(), getDiscriminator());
995 DEFINE_MDNODE_GET(DICompositeType
,
996 (unsigned Tag
, StringRef Name
, DIFile
*File
, unsigned Line
,
997 DIScopeRef Scope
, DITypeRef BaseType
, uint64_t SizeInBits
,
998 uint32_t AlignInBits
, uint64_t OffsetInBits
,
999 DIFlags Flags
, DINodeArray Elements
, unsigned RuntimeLang
,
1000 DITypeRef VTableHolder
,
1001 DITemplateParameterArray TemplateParams
= nullptr,
1002 StringRef Identifier
= "", DIDerivedType
*Discriminator
= nullptr),
1003 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
1004 AlignInBits
, OffsetInBits
, Flags
, Elements
, RuntimeLang
,
1005 VTableHolder
, TemplateParams
, Identifier
, Discriminator
))
1006 DEFINE_MDNODE_GET(DICompositeType
,
1007 (unsigned Tag
, MDString
*Name
, Metadata
*File
,
1008 unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
,
1009 uint64_t SizeInBits
, uint32_t AlignInBits
,
1010 uint64_t OffsetInBits
, DIFlags Flags
, Metadata
*Elements
,
1011 unsigned RuntimeLang
, Metadata
*VTableHolder
,
1012 Metadata
*TemplateParams
= nullptr,
1013 MDString
*Identifier
= nullptr,
1014 Metadata
*Discriminator
= nullptr),
1015 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
1016 AlignInBits
, OffsetInBits
, Flags
, Elements
, RuntimeLang
,
1017 VTableHolder
, TemplateParams
, Identifier
, Discriminator
))
1019 TempDICompositeType
clone() const { return cloneImpl(); }
1021 /// Get a DICompositeType with the given ODR identifier.
1023 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
1024 /// DICompositeType for the given ODR \c Identifier. If none exists, creates
1027 /// Else, returns \c nullptr.
1028 static DICompositeType
*
1029 getODRType(LLVMContext
&Context
, MDString
&Identifier
, unsigned Tag
,
1030 MDString
*Name
, Metadata
*File
, unsigned Line
, Metadata
*Scope
,
1031 Metadata
*BaseType
, uint64_t SizeInBits
, uint32_t AlignInBits
,
1032 uint64_t OffsetInBits
, DIFlags Flags
, Metadata
*Elements
,
1033 unsigned RuntimeLang
, Metadata
*VTableHolder
,
1034 Metadata
*TemplateParams
, Metadata
*Discriminator
);
1035 static DICompositeType
*getODRTypeIfExists(LLVMContext
&Context
,
1036 MDString
&Identifier
);
1038 /// Build a DICompositeType with the given ODR identifier.
1040 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
1041 /// it doesn't exist, creates a new one. If it does exist and \a
1042 /// isForwardDecl(), and the new arguments would be a definition, mutates the
1043 /// the type in place. In either case, returns the type.
1045 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
1047 static DICompositeType
*
1048 buildODRType(LLVMContext
&Context
, MDString
&Identifier
, unsigned Tag
,
1049 MDString
*Name
, Metadata
*File
, unsigned Line
, Metadata
*Scope
,
1050 Metadata
*BaseType
, uint64_t SizeInBits
, uint32_t AlignInBits
,
1051 uint64_t OffsetInBits
, DIFlags Flags
, Metadata
*Elements
,
1052 unsigned RuntimeLang
, Metadata
*VTableHolder
,
1053 Metadata
*TemplateParams
, Metadata
*Discriminator
);
1055 DITypeRef
getBaseType() const { return DITypeRef(getRawBaseType()); }
1056 DINodeArray
getElements() const {
1057 return cast_or_null
<MDTuple
>(getRawElements());
1059 DITypeRef
getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
1060 DITemplateParameterArray
getTemplateParams() const {
1061 return cast_or_null
<MDTuple
>(getRawTemplateParams());
1063 StringRef
getIdentifier() const { return getStringOperand(7); }
1064 unsigned getRuntimeLang() const { return RuntimeLang
; }
1066 Metadata
*getRawBaseType() const { return getOperand(3); }
1067 Metadata
*getRawElements() const { return getOperand(4); }
1068 Metadata
*getRawVTableHolder() const { return getOperand(5); }
1069 Metadata
*getRawTemplateParams() const { return getOperand(6); }
1070 MDString
*getRawIdentifier() const { return getOperandAs
<MDString
>(7); }
1071 Metadata
*getRawDiscriminator() const { return getOperand(8); }
1072 DIDerivedType
*getDiscriminator() const { return getOperandAs
<DIDerivedType
>(8); }
1074 /// Replace operands.
1076 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
1077 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
1078 /// of its movement if necessary.
1080 void replaceElements(DINodeArray Elements
) {
1082 for (DINode
*Op
: getElements())
1083 assert(is_contained(Elements
->operands(), Op
) &&
1084 "Lost a member during member list replacement");
1086 replaceOperandWith(4, Elements
.get());
1089 void replaceVTableHolder(DITypeRef VTableHolder
) {
1090 replaceOperandWith(5, VTableHolder
);
1093 void replaceTemplateParams(DITemplateParameterArray TemplateParams
) {
1094 replaceOperandWith(6, TemplateParams
.get());
1098 static bool classof(const Metadata
*MD
) {
1099 return MD
->getMetadataID() == DICompositeTypeKind
;
1103 /// Type array for a subprogram.
1105 /// TODO: Fold the array of types in directly as operands.
1106 class DISubroutineType
: public DIType
{
1107 friend class LLVMContextImpl
;
1108 friend class MDNode
;
1110 /// The calling convention used with DW_AT_calling_convention. Actually of
1111 /// type dwarf::CallingConvention.
1114 DISubroutineType(LLVMContext
&C
, StorageType Storage
, DIFlags Flags
,
1115 uint8_t CC
, ArrayRef
<Metadata
*> Ops
)
1116 : DIType(C
, DISubroutineTypeKind
, Storage
, dwarf::DW_TAG_subroutine_type
,
1117 0, 0, 0, 0, Flags
, Ops
),
1119 ~DISubroutineType() = default;
1121 static DISubroutineType
*getImpl(LLVMContext
&Context
, DIFlags Flags
,
1122 uint8_t CC
, DITypeRefArray TypeArray
,
1123 StorageType Storage
,
1124 bool ShouldCreate
= true) {
1125 return getImpl(Context
, Flags
, CC
, TypeArray
.get(), Storage
, ShouldCreate
);
1127 static DISubroutineType
*getImpl(LLVMContext
&Context
, DIFlags Flags
,
1128 uint8_t CC
, Metadata
*TypeArray
,
1129 StorageType Storage
,
1130 bool ShouldCreate
= true);
1132 TempDISubroutineType
cloneImpl() const {
1133 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
1137 DEFINE_MDNODE_GET(DISubroutineType
,
1138 (DIFlags Flags
, uint8_t CC
, DITypeRefArray TypeArray
),
1139 (Flags
, CC
, TypeArray
))
1140 DEFINE_MDNODE_GET(DISubroutineType
,
1141 (DIFlags Flags
, uint8_t CC
, Metadata
*TypeArray
),
1142 (Flags
, CC
, TypeArray
))
1144 TempDISubroutineType
clone() const { return cloneImpl(); }
1146 uint8_t getCC() const { return CC
; }
1148 DITypeRefArray
getTypeArray() const {
1149 return cast_or_null
<MDTuple
>(getRawTypeArray());
1152 Metadata
*getRawTypeArray() const { return getOperand(3); }
1154 static bool classof(const Metadata
*MD
) {
1155 return MD
->getMetadataID() == DISubroutineTypeKind
;
1160 class DICompileUnit
: public DIScope
{
1161 friend class LLVMContextImpl
;
1162 friend class MDNode
;
1165 enum DebugEmissionKind
: unsigned {
1169 DebugDirectivesOnly
,
1170 LastEmissionKind
= DebugDirectivesOnly
1173 enum class DebugNameTableKind
: unsigned {
1177 LastDebugNameTableKind
= None
1180 static Optional
<DebugEmissionKind
> getEmissionKind(StringRef Str
);
1181 static const char *emissionKindString(DebugEmissionKind EK
);
1182 static Optional
<DebugNameTableKind
> getNameTableKind(StringRef Str
);
1183 static const char *nameTableKindString(DebugNameTableKind PK
);
1186 unsigned SourceLanguage
;
1188 unsigned RuntimeVersion
;
1189 unsigned EmissionKind
;
1191 bool SplitDebugInlining
;
1192 bool DebugInfoForProfiling
;
1193 unsigned NameTableKind
;
1194 bool RangesBaseAddress
;
1196 DICompileUnit(LLVMContext
&C
, StorageType Storage
, unsigned SourceLanguage
,
1197 bool IsOptimized
, unsigned RuntimeVersion
,
1198 unsigned EmissionKind
, uint64_t DWOId
, bool SplitDebugInlining
,
1199 bool DebugInfoForProfiling
, unsigned NameTableKind
,
1200 bool RangesBaseAddress
, ArrayRef
<Metadata
*> Ops
)
1201 : DIScope(C
, DICompileUnitKind
, Storage
, dwarf::DW_TAG_compile_unit
, Ops
),
1202 SourceLanguage(SourceLanguage
), IsOptimized(IsOptimized
),
1203 RuntimeVersion(RuntimeVersion
), EmissionKind(EmissionKind
),
1204 DWOId(DWOId
), SplitDebugInlining(SplitDebugInlining
),
1205 DebugInfoForProfiling(DebugInfoForProfiling
),
1206 NameTableKind(NameTableKind
), RangesBaseAddress(RangesBaseAddress
) {
1207 assert(Storage
!= Uniqued
);
1209 ~DICompileUnit() = default;
1211 static DICompileUnit
*
1212 getImpl(LLVMContext
&Context
, unsigned SourceLanguage
, DIFile
*File
,
1213 StringRef Producer
, bool IsOptimized
, StringRef Flags
,
1214 unsigned RuntimeVersion
, StringRef SplitDebugFilename
,
1215 unsigned EmissionKind
, DICompositeTypeArray EnumTypes
,
1216 DIScopeArray RetainedTypes
,
1217 DIGlobalVariableExpressionArray GlobalVariables
,
1218 DIImportedEntityArray ImportedEntities
, DIMacroNodeArray Macros
,
1219 uint64_t DWOId
, bool SplitDebugInlining
, bool DebugInfoForProfiling
,
1220 unsigned NameTableKind
, bool RangesBaseAddress
, StorageType Storage
,
1221 bool ShouldCreate
= true) {
1222 return getImpl(Context
, SourceLanguage
, File
,
1223 getCanonicalMDString(Context
, Producer
), IsOptimized
,
1224 getCanonicalMDString(Context
, Flags
), RuntimeVersion
,
1225 getCanonicalMDString(Context
, SplitDebugFilename
),
1226 EmissionKind
, EnumTypes
.get(), RetainedTypes
.get(),
1227 GlobalVariables
.get(), ImportedEntities
.get(), Macros
.get(),
1228 DWOId
, SplitDebugInlining
, DebugInfoForProfiling
,
1229 NameTableKind
, RangesBaseAddress
, Storage
, ShouldCreate
);
1231 static DICompileUnit
*
1232 getImpl(LLVMContext
&Context
, unsigned SourceLanguage
, Metadata
*File
,
1233 MDString
*Producer
, bool IsOptimized
, MDString
*Flags
,
1234 unsigned RuntimeVersion
, MDString
*SplitDebugFilename
,
1235 unsigned EmissionKind
, Metadata
*EnumTypes
, Metadata
*RetainedTypes
,
1236 Metadata
*GlobalVariables
, Metadata
*ImportedEntities
,
1237 Metadata
*Macros
, uint64_t DWOId
, bool SplitDebugInlining
,
1238 bool DebugInfoForProfiling
, unsigned NameTableKind
,
1239 bool RangesBaseAddress
, StorageType Storage
, bool ShouldCreate
= true);
1241 TempDICompileUnit
cloneImpl() const {
1242 return getTemporary(
1243 getContext(), getSourceLanguage(), getFile(), getProducer(),
1244 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1245 getEmissionKind(), getEnumTypes(), getRetainedTypes(),
1246 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId
,
1247 getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(),
1248 getRangesBaseAddress());
1252 static void get() = delete;
1253 static void getIfExists() = delete;
1255 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1257 (unsigned SourceLanguage
, DIFile
*File
, StringRef Producer
,
1258 bool IsOptimized
, StringRef Flags
, unsigned RuntimeVersion
,
1259 StringRef SplitDebugFilename
, DebugEmissionKind EmissionKind
,
1260 DICompositeTypeArray EnumTypes
, DIScopeArray RetainedTypes
,
1261 DIGlobalVariableExpressionArray GlobalVariables
,
1262 DIImportedEntityArray ImportedEntities
, DIMacroNodeArray Macros
,
1263 uint64_t DWOId
, bool SplitDebugInlining
, bool DebugInfoForProfiling
,
1264 DebugNameTableKind NameTableKind
, bool RangesBaseAddress
),
1265 (SourceLanguage
, File
, Producer
, IsOptimized
, Flags
, RuntimeVersion
,
1266 SplitDebugFilename
, EmissionKind
, EnumTypes
, RetainedTypes
,
1267 GlobalVariables
, ImportedEntities
, Macros
, DWOId
, SplitDebugInlining
,
1268 DebugInfoForProfiling
, (unsigned)NameTableKind
, RangesBaseAddress
))
1269 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1271 (unsigned SourceLanguage
, Metadata
*File
, MDString
*Producer
,
1272 bool IsOptimized
, MDString
*Flags
, unsigned RuntimeVersion
,
1273 MDString
*SplitDebugFilename
, unsigned EmissionKind
, Metadata
*EnumTypes
,
1274 Metadata
*RetainedTypes
, Metadata
*GlobalVariables
,
1275 Metadata
*ImportedEntities
, Metadata
*Macros
, uint64_t DWOId
,
1276 bool SplitDebugInlining
, bool DebugInfoForProfiling
,
1277 unsigned NameTableKind
, bool RangesBaseAddress
),
1278 (SourceLanguage
, File
, Producer
, IsOptimized
, Flags
, RuntimeVersion
,
1279 SplitDebugFilename
, EmissionKind
, EnumTypes
, RetainedTypes
,
1280 GlobalVariables
, ImportedEntities
, Macros
, DWOId
, SplitDebugInlining
,
1281 DebugInfoForProfiling
, NameTableKind
, RangesBaseAddress
))
1283 TempDICompileUnit
clone() const { return cloneImpl(); }
1285 unsigned getSourceLanguage() const { return SourceLanguage
; }
1286 bool isOptimized() const { return IsOptimized
; }
1287 unsigned getRuntimeVersion() const { return RuntimeVersion
; }
1288 DebugEmissionKind
getEmissionKind() const {
1289 return (DebugEmissionKind
)EmissionKind
;
1291 bool isDebugDirectivesOnly() const {
1292 return EmissionKind
== DebugDirectivesOnly
;
1294 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling
; }
1295 DebugNameTableKind
getNameTableKind() const {
1296 return (DebugNameTableKind
)NameTableKind
;
1298 bool getRangesBaseAddress() const {
1299 return RangesBaseAddress
; }
1300 StringRef
getProducer() const {
1301 return getStringOperand(1); }
1302 StringRef
getFlags() const {
1303 return getStringOperand(2); }
1304 StringRef
getSplitDebugFilename() const {
1305 return getStringOperand(3); }
1306 DICompositeTypeArray
getEnumTypes() const {
1307 return cast_or_null
<MDTuple
>(getRawEnumTypes());
1309 DIScopeArray
getRetainedTypes() const {
1310 return cast_or_null
<MDTuple
>(getRawRetainedTypes());
1312 DIGlobalVariableExpressionArray
getGlobalVariables() const {
1313 return cast_or_null
<MDTuple
>(getRawGlobalVariables());
1315 DIImportedEntityArray
getImportedEntities() const {
1316 return cast_or_null
<MDTuple
>(getRawImportedEntities());
1318 DIMacroNodeArray
getMacros() const {
1319 return cast_or_null
<MDTuple
>(getRawMacros());
1321 uint64_t getDWOId() const { return DWOId
; }
1322 void setDWOId(uint64_t DwoId
) { DWOId
= DwoId
; }
1323 bool getSplitDebugInlining() const { return SplitDebugInlining
; }
1324 void setSplitDebugInlining(bool SplitDebugInlining
) {
1325 this->SplitDebugInlining
= SplitDebugInlining
;
1328 MDString
*getRawProducer() const { return getOperandAs
<MDString
>(1); }
1329 MDString
*getRawFlags() const { return getOperandAs
<MDString
>(2); }
1330 MDString
*getRawSplitDebugFilename() const {
1331 return getOperandAs
<MDString
>(3);
1333 Metadata
*getRawEnumTypes() const { return getOperand(4); }
1334 Metadata
*getRawRetainedTypes() const { return getOperand(5); }
1335 Metadata
*getRawGlobalVariables() const { return getOperand(6); }
1336 Metadata
*getRawImportedEntities() const { return getOperand(7); }
1337 Metadata
*getRawMacros() const { return getOperand(8); }
1341 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1342 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1343 /// DICompileUnit should be fairly rare.
1345 void replaceEnumTypes(DICompositeTypeArray N
) {
1346 replaceOperandWith(4, N
.get());
1348 void replaceRetainedTypes(DITypeArray N
) {
1349 replaceOperandWith(5, N
.get());
1351 void replaceGlobalVariables(DIGlobalVariableExpressionArray N
) {
1352 replaceOperandWith(6, N
.get());
1354 void replaceImportedEntities(DIImportedEntityArray N
) {
1355 replaceOperandWith(7, N
.get());
1357 void replaceMacros(DIMacroNodeArray N
) { replaceOperandWith(8, N
.get()); }
1360 static bool classof(const Metadata
*MD
) {
1361 return MD
->getMetadataID() == DICompileUnitKind
;
1365 /// A scope for locals.
1367 /// A legal scope for lexical blocks, local variables, and debug info
1368 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1369 /// DILexicalBlockFile.
1370 class DILocalScope
: public DIScope
{
1372 DILocalScope(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned Tag
,
1373 ArrayRef
<Metadata
*> Ops
)
1374 : DIScope(C
, ID
, Storage
, Tag
, Ops
) {}
1375 ~DILocalScope() = default;
1378 /// Get the subprogram for this scope.
1380 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1382 DISubprogram
*getSubprogram() const;
1384 /// Get the first non DILexicalBlockFile scope of this scope.
1386 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1388 DILocalScope
*getNonLexicalBlockFileScope() const;
1390 static bool classof(const Metadata
*MD
) {
1391 return MD
->getMetadataID() == DISubprogramKind
||
1392 MD
->getMetadataID() == DILexicalBlockKind
||
1393 MD
->getMetadataID() == DILexicalBlockFileKind
;
1399 /// A debug location in source code, used for debug info and otherwise.
1400 class DILocation
: public MDNode
{
1401 friend class LLVMContextImpl
;
1402 friend class MDNode
;
1404 DILocation(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
1405 unsigned Column
, ArrayRef
<Metadata
*> MDs
, bool ImplicitCode
);
1406 ~DILocation() { dropAllReferences(); }
1408 static DILocation
*getImpl(LLVMContext
&Context
, unsigned Line
,
1409 unsigned Column
, Metadata
*Scope
,
1410 Metadata
*InlinedAt
, bool ImplicitCode
,
1411 StorageType Storage
, bool ShouldCreate
= true);
1412 static DILocation
*getImpl(LLVMContext
&Context
, unsigned Line
,
1413 unsigned Column
, DILocalScope
*Scope
,
1414 DILocation
*InlinedAt
, bool ImplicitCode
,
1415 StorageType Storage
, bool ShouldCreate
= true) {
1416 return getImpl(Context
, Line
, Column
, static_cast<Metadata
*>(Scope
),
1417 static_cast<Metadata
*>(InlinedAt
), ImplicitCode
, Storage
,
1421 /// With a given unsigned int \p U, use up to 13 bits to represent it.
1422 /// old_bit 1~5 --> new_bit 1~5
1423 /// old_bit 6~12 --> new_bit 7~13
1424 /// new_bit_6 is 0 if higher bits (7~13) are all 0
1425 static unsigned getPrefixEncodingFromUnsigned(unsigned U
) {
1427 return U
> 0x1f ? (((U
& 0xfe0) << 1) | (U
& 0x1f) | 0x20) : U
;
1430 /// Reverse transformation as getPrefixEncodingFromUnsigned.
1431 static unsigned getUnsignedFromPrefixEncoding(unsigned U
) {
1435 return (U
& 0x20) ? (((U
>> 1) & 0xfe0) | (U
& 0x1f)) : (U
& 0x1f);
1438 /// Returns the next component stored in discriminator.
1439 static unsigned getNextComponentInDiscriminator(unsigned D
) {
1441 return D
>> ((D
& 0x40) ? 14 : 7);
1446 TempDILocation
cloneImpl() const {
1447 // Get the raw scope/inlinedAt since it is possible to invoke this on
1448 // a DILocation containing temporary metadata.
1449 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1450 getRawInlinedAt(), isImplicitCode());
1453 static unsigned encodeComponent(unsigned C
) {
1454 return (C
== 0) ? 1U : (getPrefixEncodingFromUnsigned(C
) << 1);
1457 static unsigned encodingBits(unsigned C
) {
1458 return (C
== 0) ? 1 : (C
> 0x1f ? 14 : 7);
1462 // Disallow replacing operands.
1463 void replaceOperandWith(unsigned I
, Metadata
*New
) = delete;
1465 DEFINE_MDNODE_GET(DILocation
,
1466 (unsigned Line
, unsigned Column
, Metadata
*Scope
,
1467 Metadata
*InlinedAt
= nullptr, bool ImplicitCode
= false),
1468 (Line
, Column
, Scope
, InlinedAt
, ImplicitCode
))
1469 DEFINE_MDNODE_GET(DILocation
,
1470 (unsigned Line
, unsigned Column
, DILocalScope
*Scope
,
1471 DILocation
*InlinedAt
= nullptr,
1472 bool ImplicitCode
= false),
1473 (Line
, Column
, Scope
, InlinedAt
, ImplicitCode
))
1475 /// Return a (temporary) clone of this.
1476 TempDILocation
clone() const { return cloneImpl(); }
1478 unsigned getLine() const { return SubclassData32
; }
1479 unsigned getColumn() const { return SubclassData16
; }
1480 DILocalScope
*getScope() const { return cast
<DILocalScope
>(getRawScope()); }
1482 DILocation
*getInlinedAt() const {
1483 return cast_or_null
<DILocation
>(getRawInlinedAt());
1486 /// Check if the location corresponds to an implicit code.
1487 /// When the ImplicitCode flag is true, it means that the Instruction
1488 /// with this DILocation has been added by the front-end but it hasn't been
1489 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
1490 /// bracket). It's useful for code coverage to not show a counter on "empty"
1492 bool isImplicitCode() const { return ImplicitCode
; }
1493 void setImplicitCode(bool ImplicitCode
) { this->ImplicitCode
= ImplicitCode
; }
1495 DIFile
*getFile() const { return getScope()->getFile(); }
1496 StringRef
getFilename() const { return getScope()->getFilename(); }
1497 StringRef
getDirectory() const { return getScope()->getDirectory(); }
1498 Optional
<StringRef
> getSource() const { return getScope()->getSource(); }
1500 /// Get the scope where this is inlined.
1502 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1504 DILocalScope
*getInlinedAtScope() const {
1505 if (auto *IA
= getInlinedAt())
1506 return IA
->getInlinedAtScope();
1510 /// Get the DWARF discriminator.
1512 /// DWARF discriminators distinguish identical file locations between
1513 /// instructions that are on different basic blocks.
1515 /// There are 3 components stored in discriminator, from lower bits:
1517 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1518 /// that are defined by the same source line, but
1519 /// different basic blocks.
1520 /// Duplication factor: assigned by optimizations that will scale down
1521 /// the execution frequency of the original IR.
1522 /// Copy Identifier: assigned by optimizations that clones the IR.
1523 /// Each copy of the IR will be assigned an identifier.
1527 /// The above 3 components are encoded into a 32bit unsigned integer in
1528 /// order. If the lowest bit is 1, the current component is empty, and the
1529 /// next component will start in the next bit. Otherwise, the current
1530 /// component is non-empty, and its content starts in the next bit. The
1531 /// value of each components is either 5 bit or 12 bit: if the 7th bit
1532 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1533 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1534 /// represent the component. Thus, the number of bits used for a component
1535 /// is either 0 (if it and all the next components are empty); 1 - if it is
1536 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1537 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1538 /// component is also capped at 0x1ff, even in the case when both first
1539 /// components are 0, and we'd technically have 29 bits available.
1541 /// For precise control over the data being encoded in the discriminator,
1542 /// use encodeDiscriminator/decodeDiscriminator.
1544 inline unsigned getDiscriminator() const;
1546 /// Returns a new DILocation with updated \p Discriminator.
1547 inline const DILocation
*cloneWithDiscriminator(unsigned Discriminator
) const;
1549 /// Returns a new DILocation with updated base discriminator \p BD. Only the
1550 /// base discriminator is set in the new DILocation, the other encoded values
1552 /// If the discriminator cannot be encoded, the function returns None.
1553 inline Optional
<const DILocation
*> cloneWithBaseDiscriminator(unsigned BD
) const;
1555 /// Returns the duplication factor stored in the discriminator, or 1 if no
1556 /// duplication factor (or 0) is encoded.
1557 inline unsigned getDuplicationFactor() const;
1559 /// Returns the copy identifier stored in the discriminator.
1560 inline unsigned getCopyIdentifier() const;
1562 /// Returns the base discriminator stored in the discriminator.
1563 inline unsigned getBaseDiscriminator() const;
1565 /// Returns a new DILocation with duplication factor \p DF * current
1566 /// duplication factor encoded in the discriminator. The current duplication
1567 /// factor is as defined by getDuplicationFactor().
1568 /// Returns None if encoding failed.
1569 inline Optional
<const DILocation
*> cloneByMultiplyingDuplicationFactor(unsigned DF
) const;
1571 /// When two instructions are combined into a single instruction we also
1572 /// need to combine the original locations into a single location.
1574 /// When the locations are the same we can use either location. When they
1575 /// differ, we need a third location which is distinct from either. If they
1576 /// have the same file/line but have a different discriminator we could
1577 /// create a location with a new discriminator. If they are from different
1578 /// files/lines the location is ambiguous and can't be represented in a line
1579 /// entry. In this case, if \p GenerateLocation is true, we will set the
1580 /// merged debug location as line 0 of the nearest common scope where the two
1581 /// locations are inlined from.
1583 /// \p GenerateLocation: Whether the merged location can be generated when
1584 /// \p LocA and \p LocB differ.
1585 static const DILocation
*getMergedLocation(const DILocation
*LocA
,
1586 const DILocation
*LocB
);
1588 /// Returns the base discriminator for a given encoded discriminator \p D.
1589 static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D
) {
1590 return getUnsignedFromPrefixEncoding(D
);
1593 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
1594 /// have certain special case behavior (e.g. treating empty duplication factor
1595 /// as the value '1').
1596 /// This API, in conjunction with cloneWithDiscriminator, may be used to encode
1597 /// the raw values provided. \p BD: base discriminator \p DF: duplication factor
1598 /// \p CI: copy index
1599 /// The return is None if the values cannot be encoded in 32 bits - for
1600 /// example, values for BD or DF larger than 12 bits. Otherwise, the return
1601 /// is the encoded value.
1602 static Optional
<unsigned> encodeDiscriminator(unsigned BD
, unsigned DF
, unsigned CI
);
1604 /// Raw decoder for values in an encoded discriminator D.
1605 static void decodeDiscriminator(unsigned D
, unsigned &BD
, unsigned &DF
,
1608 /// Returns the duplication factor for a given encoded discriminator \p D, or
1609 /// 1 if no value or 0 is encoded.
1610 static unsigned getDuplicationFactorFromDiscriminator(unsigned D
) {
1611 D
= getNextComponentInDiscriminator(D
);
1612 unsigned Ret
= getUnsignedFromPrefixEncoding(D
);
1618 /// Returns the copy identifier for a given encoded discriminator \p D.
1619 static unsigned getCopyIdentifierFromDiscriminator(unsigned D
) {
1620 return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
1621 getNextComponentInDiscriminator(D
)));
1625 Metadata
*getRawScope() const { return getOperand(0); }
1626 Metadata
*getRawInlinedAt() const {
1627 if (getNumOperands() == 2)
1628 return getOperand(1);
1632 static bool classof(const Metadata
*MD
) {
1633 return MD
->getMetadataID() == DILocationKind
;
1637 /// Subprogram description.
1639 /// TODO: Remove DisplayName. It's always equal to Name.
1640 /// TODO: Split up flags.
1641 class DISubprogram
: public DILocalScope
{
1642 friend class LLVMContextImpl
;
1643 friend class MDNode
;
1647 unsigned VirtualIndex
;
1649 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1650 /// of method overrides from secondary bases by this amount. It may be
1655 /// Debug info subprogram flags.
1656 enum DISPFlags
: uint32_t {
1657 #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
1658 #define DISP_FLAG_LARGEST_NEEDED
1659 #include "llvm/IR/DebugInfoFlags.def"
1660 SPFlagNonvirtual
= SPFlagZero
,
1661 SPFlagVirtuality
= SPFlagVirtual
| SPFlagPureVirtual
,
1662 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest
)
1665 static DISPFlags
getFlag(StringRef Flag
);
1666 static StringRef
getFlagString(DISPFlags Flag
);
1668 /// Split up a flags bitfield for easier printing.
1670 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
1671 /// any remaining (unrecognized) bits.
1672 static DISPFlags
splitFlags(DISPFlags Flags
,
1673 SmallVectorImpl
<DISPFlags
> &SplitFlags
);
1675 // Helper for converting old bitfields to new flags word.
1676 static DISPFlags
toSPFlags(bool IsLocalToUnit
, bool IsDefinition
,
1678 unsigned Virtuality
= SPFlagNonvirtual
) {
1679 // We're assuming virtuality is the low-order field.
1681 int(SPFlagVirtual
) == int(dwarf::DW_VIRTUALITY_virtual
) &&
1682 int(SPFlagPureVirtual
) == int(dwarf::DW_VIRTUALITY_pure_virtual
),
1683 "Virtuality constant mismatch");
1684 return static_cast<DISPFlags
>(
1685 (Virtuality
& SPFlagVirtuality
) |
1686 (IsLocalToUnit
? SPFlagLocalToUnit
: SPFlagZero
) |
1687 (IsDefinition
? SPFlagDefinition
: SPFlagZero
) |
1688 (IsOptimized
? SPFlagOptimized
: SPFlagZero
));
1695 DISubprogram(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
1696 unsigned ScopeLine
, unsigned VirtualIndex
, int ThisAdjustment
,
1697 DIFlags Flags
, DISPFlags SPFlags
, ArrayRef
<Metadata
*> Ops
)
1698 : DILocalScope(C
, DISubprogramKind
, Storage
, dwarf::DW_TAG_subprogram
,
1700 Line(Line
), ScopeLine(ScopeLine
), VirtualIndex(VirtualIndex
),
1701 ThisAdjustment(ThisAdjustment
), Flags(Flags
), SPFlags(SPFlags
) {
1702 static_assert(dwarf::DW_VIRTUALITY_max
< 4, "Virtuality out of range");
1704 ~DISubprogram() = default;
1706 static DISubprogram
*
1707 getImpl(LLVMContext
&Context
, DIScopeRef Scope
, StringRef Name
,
1708 StringRef LinkageName
, DIFile
*File
, unsigned Line
,
1709 DISubroutineType
*Type
, unsigned ScopeLine
, DITypeRef ContainingType
,
1710 unsigned VirtualIndex
, int ThisAdjustment
, DIFlags Flags
,
1711 DISPFlags SPFlags
, DICompileUnit
*Unit
,
1712 DITemplateParameterArray TemplateParams
, DISubprogram
*Declaration
,
1713 DINodeArray RetainedNodes
, DITypeArray ThrownTypes
,
1714 StorageType Storage
, bool ShouldCreate
= true) {
1715 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
),
1716 getCanonicalMDString(Context
, LinkageName
), File
, Line
, Type
,
1717 ScopeLine
, ContainingType
, VirtualIndex
, ThisAdjustment
,
1718 Flags
, SPFlags
, Unit
, TemplateParams
.get(), Declaration
,
1719 RetainedNodes
.get(), ThrownTypes
.get(), Storage
,
1722 static DISubprogram
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
1723 MDString
*Name
, MDString
*LinkageName
,
1724 Metadata
*File
, unsigned Line
, Metadata
*Type
,
1725 unsigned ScopeLine
, Metadata
*ContainingType
,
1726 unsigned VirtualIndex
, int ThisAdjustment
,
1727 DIFlags Flags
, DISPFlags SPFlags
, Metadata
*Unit
,
1728 Metadata
*TemplateParams
, Metadata
*Declaration
,
1729 Metadata
*RetainedNodes
, Metadata
*ThrownTypes
,
1730 StorageType Storage
, bool ShouldCreate
= true);
1732 TempDISubprogram
cloneImpl() const {
1733 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1734 getFile(), getLine(), getType(), getScopeLine(),
1735 getContainingType(), getVirtualIndex(),
1736 getThisAdjustment(), getFlags(), getSPFlags(),
1737 getUnit(), getTemplateParams(), getDeclaration(),
1738 getRetainedNodes(), getThrownTypes());
1744 (DIScopeRef Scope
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
1745 unsigned Line
, DISubroutineType
*Type
, unsigned ScopeLine
,
1746 DITypeRef ContainingType
, unsigned VirtualIndex
, int ThisAdjustment
,
1747 DIFlags Flags
, DISPFlags SPFlags
, DICompileUnit
*Unit
,
1748 DITemplateParameterArray TemplateParams
= nullptr,
1749 DISubprogram
*Declaration
= nullptr, DINodeArray RetainedNodes
= nullptr,
1750 DITypeArray ThrownTypes
= nullptr),
1751 (Scope
, Name
, LinkageName
, File
, Line
, Type
, ScopeLine
, ContainingType
,
1752 VirtualIndex
, ThisAdjustment
, Flags
, SPFlags
, Unit
, TemplateParams
,
1753 Declaration
, RetainedNodes
, ThrownTypes
))
1757 (Metadata
* Scope
, MDString
*Name
, MDString
*LinkageName
, Metadata
*File
,
1758 unsigned Line
, Metadata
*Type
, unsigned ScopeLine
,
1759 Metadata
*ContainingType
, unsigned VirtualIndex
, int ThisAdjustment
,
1760 DIFlags Flags
, DISPFlags SPFlags
, Metadata
*Unit
,
1761 Metadata
*TemplateParams
= nullptr, Metadata
*Declaration
= nullptr,
1762 Metadata
*RetainedNodes
= nullptr, Metadata
*ThrownTypes
= nullptr),
1763 (Scope
, Name
, LinkageName
, File
, Line
, Type
, ScopeLine
, ContainingType
,
1764 VirtualIndex
, ThisAdjustment
, Flags
, SPFlags
, Unit
, TemplateParams
,
1765 Declaration
, RetainedNodes
, ThrownTypes
))
1767 TempDISubprogram
clone() const { return cloneImpl(); }
1769 /// Returns a new temporary DISubprogram with updated Flags
1770 TempDISubprogram
cloneWithFlags(DIFlags NewFlags
) const {
1771 auto NewSP
= clone();
1772 NewSP
->Flags
= NewFlags
;
1777 unsigned getLine() const { return Line
; }
1778 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality
; }
1779 unsigned getVirtualIndex() const { return VirtualIndex
; }
1780 int getThisAdjustment() const { return ThisAdjustment
; }
1781 unsigned getScopeLine() const { return ScopeLine
; }
1782 DIFlags
getFlags() const { return Flags
; }
1783 DISPFlags
getSPFlags() const { return SPFlags
; }
1784 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit
; }
1785 bool isDefinition() const { return getSPFlags() & SPFlagDefinition
; }
1786 bool isOptimized() const { return getSPFlags() & SPFlagOptimized
; }
1788 bool isArtificial() const { return getFlags() & FlagArtificial
; }
1789 bool isPrivate() const {
1790 return (getFlags() & FlagAccessibility
) == FlagPrivate
;
1792 bool isProtected() const {
1793 return (getFlags() & FlagAccessibility
) == FlagProtected
;
1795 bool isPublic() const {
1796 return (getFlags() & FlagAccessibility
) == FlagPublic
;
1798 bool isExplicit() const { return getFlags() & FlagExplicit
; }
1799 bool isPrototyped() const { return getFlags() & FlagPrototyped
; }
1800 bool areAllCallsDescribed() const {
1801 return getFlags() & FlagAllCallsDescribed
;
1803 bool isMainSubprogram() const { return getFlags() & FlagMainSubprogram
; }
1805 /// Check if this is reference-qualified.
1807 /// Return true if this subprogram is a C++11 reference-qualified non-static
1808 /// member function (void foo() &).
1809 bool isLValueReference() const { return getFlags() & FlagLValueReference
; }
1811 /// Check if this is rvalue-reference-qualified.
1813 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1814 /// non-static member function (void foo() &&).
1815 bool isRValueReference() const { return getFlags() & FlagRValueReference
; }
1817 /// Check if this is marked as noreturn.
1819 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1820 bool isNoReturn() const { return getFlags() & FlagNoReturn
; }
1822 // Check if this routine is a compiler-generated thunk.
1824 // Returns true if this subprogram is a thunk generated by the compiler.
1825 bool isThunk() const { return getFlags() & FlagThunk
; }
1827 DIScopeRef
getScope() const { return DIScopeRef(getRawScope()); }
1829 StringRef
getName() const { return getStringOperand(2); }
1830 StringRef
getLinkageName() const { return getStringOperand(3); }
1832 DISubroutineType
*getType() const {
1833 return cast_or_null
<DISubroutineType
>(getRawType());
1835 DITypeRef
getContainingType() const {
1836 return DITypeRef(getRawContainingType());
1839 DICompileUnit
*getUnit() const {
1840 return cast_or_null
<DICompileUnit
>(getRawUnit());
1842 void replaceUnit(DICompileUnit
*CU
) { replaceOperandWith(5, CU
); }
1843 DITemplateParameterArray
getTemplateParams() const {
1844 return cast_or_null
<MDTuple
>(getRawTemplateParams());
1846 DISubprogram
*getDeclaration() const {
1847 return cast_or_null
<DISubprogram
>(getRawDeclaration());
1849 DINodeArray
getRetainedNodes() const {
1850 return cast_or_null
<MDTuple
>(getRawRetainedNodes());
1852 DITypeArray
getThrownTypes() const {
1853 return cast_or_null
<MDTuple
>(getRawThrownTypes());
1856 Metadata
*getRawScope() const { return getOperand(1); }
1857 MDString
*getRawName() const { return getOperandAs
<MDString
>(2); }
1858 MDString
*getRawLinkageName() const { return getOperandAs
<MDString
>(3); }
1859 Metadata
*getRawType() const { return getOperand(4); }
1860 Metadata
*getRawUnit() const { return getOperand(5); }
1861 Metadata
*getRawDeclaration() const { return getOperand(6); }
1862 Metadata
*getRawRetainedNodes() const { return getOperand(7); }
1863 Metadata
*getRawContainingType() const {
1864 return getNumOperands() > 8 ? getOperandAs
<Metadata
>(8) : nullptr;
1866 Metadata
*getRawTemplateParams() const {
1867 return getNumOperands() > 9 ? getOperandAs
<Metadata
>(9) : nullptr;
1869 Metadata
*getRawThrownTypes() const {
1870 return getNumOperands() > 10 ? getOperandAs
<Metadata
>(10) : nullptr;
1873 /// Check if this subprogram describes the given function.
1875 /// FIXME: Should this be looking through bitcasts?
1876 bool describes(const Function
*F
) const;
1878 static bool classof(const Metadata
*MD
) {
1879 return MD
->getMetadataID() == DISubprogramKind
;
1883 class DILexicalBlockBase
: public DILocalScope
{
1885 DILexicalBlockBase(LLVMContext
&C
, unsigned ID
, StorageType Storage
,
1886 ArrayRef
<Metadata
*> Ops
)
1887 : DILocalScope(C
, ID
, Storage
, dwarf::DW_TAG_lexical_block
, Ops
) {}
1888 ~DILexicalBlockBase() = default;
1891 DILocalScope
*getScope() const { return cast
<DILocalScope
>(getRawScope()); }
1893 Metadata
*getRawScope() const { return getOperand(1); }
1895 static bool classof(const Metadata
*MD
) {
1896 return MD
->getMetadataID() == DILexicalBlockKind
||
1897 MD
->getMetadataID() == DILexicalBlockFileKind
;
1901 class DILexicalBlock
: public DILexicalBlockBase
{
1902 friend class LLVMContextImpl
;
1903 friend class MDNode
;
1908 DILexicalBlock(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
1909 unsigned Column
, ArrayRef
<Metadata
*> Ops
)
1910 : DILexicalBlockBase(C
, DILexicalBlockKind
, Storage
, Ops
), Line(Line
),
1912 assert(Column
< (1u << 16) && "Expected 16-bit column");
1914 ~DILexicalBlock() = default;
1916 static DILexicalBlock
*getImpl(LLVMContext
&Context
, DILocalScope
*Scope
,
1917 DIFile
*File
, unsigned Line
, unsigned Column
,
1918 StorageType Storage
,
1919 bool ShouldCreate
= true) {
1920 return getImpl(Context
, static_cast<Metadata
*>(Scope
),
1921 static_cast<Metadata
*>(File
), Line
, Column
, Storage
,
1925 static DILexicalBlock
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
1926 Metadata
*File
, unsigned Line
, unsigned Column
,
1927 StorageType Storage
, bool ShouldCreate
= true);
1929 TempDILexicalBlock
cloneImpl() const {
1930 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1935 DEFINE_MDNODE_GET(DILexicalBlock
, (DILocalScope
* Scope
, DIFile
*File
,
1936 unsigned Line
, unsigned Column
),
1937 (Scope
, File
, Line
, Column
))
1938 DEFINE_MDNODE_GET(DILexicalBlock
, (Metadata
* Scope
, Metadata
*File
,
1939 unsigned Line
, unsigned Column
),
1940 (Scope
, File
, Line
, Column
))
1942 TempDILexicalBlock
clone() const { return cloneImpl(); }
1944 unsigned getLine() const { return Line
; }
1945 unsigned getColumn() const { return Column
; }
1947 static bool classof(const Metadata
*MD
) {
1948 return MD
->getMetadataID() == DILexicalBlockKind
;
1952 class DILexicalBlockFile
: public DILexicalBlockBase
{
1953 friend class LLVMContextImpl
;
1954 friend class MDNode
;
1956 unsigned Discriminator
;
1958 DILexicalBlockFile(LLVMContext
&C
, StorageType Storage
,
1959 unsigned Discriminator
, ArrayRef
<Metadata
*> Ops
)
1960 : DILexicalBlockBase(C
, DILexicalBlockFileKind
, Storage
, Ops
),
1961 Discriminator(Discriminator
) {}
1962 ~DILexicalBlockFile() = default;
1964 static DILexicalBlockFile
*getImpl(LLVMContext
&Context
, DILocalScope
*Scope
,
1965 DIFile
*File
, unsigned Discriminator
,
1966 StorageType Storage
,
1967 bool ShouldCreate
= true) {
1968 return getImpl(Context
, static_cast<Metadata
*>(Scope
),
1969 static_cast<Metadata
*>(File
), Discriminator
, Storage
,
1973 static DILexicalBlockFile
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
1974 Metadata
*File
, unsigned Discriminator
,
1975 StorageType Storage
,
1976 bool ShouldCreate
= true);
1978 TempDILexicalBlockFile
cloneImpl() const {
1979 return getTemporary(getContext(), getScope(), getFile(),
1980 getDiscriminator());
1984 DEFINE_MDNODE_GET(DILexicalBlockFile
, (DILocalScope
* Scope
, DIFile
*File
,
1985 unsigned Discriminator
),
1986 (Scope
, File
, Discriminator
))
1987 DEFINE_MDNODE_GET(DILexicalBlockFile
,
1988 (Metadata
* Scope
, Metadata
*File
, unsigned Discriminator
),
1989 (Scope
, File
, Discriminator
))
1991 TempDILexicalBlockFile
clone() const { return cloneImpl(); }
1993 // TODO: Remove these once they're gone from DILexicalBlockBase.
1994 unsigned getLine() const = delete;
1995 unsigned getColumn() const = delete;
1997 unsigned getDiscriminator() const { return Discriminator
; }
1999 static bool classof(const Metadata
*MD
) {
2000 return MD
->getMetadataID() == DILexicalBlockFileKind
;
2004 unsigned DILocation::getDiscriminator() const {
2005 if (auto *F
= dyn_cast
<DILexicalBlockFile
>(getScope()))
2006 return F
->getDiscriminator();
2011 DILocation::cloneWithDiscriminator(unsigned Discriminator
) const {
2012 DIScope
*Scope
= getScope();
2013 // Skip all parent DILexicalBlockFile that already have a discriminator
2014 // assigned. We do not want to have nested DILexicalBlockFiles that have
2015 // mutliple discriminators because only the leaf DILexicalBlockFile's
2016 // dominator will be used.
2017 for (auto *LBF
= dyn_cast
<DILexicalBlockFile
>(Scope
);
2018 LBF
&& LBF
->getDiscriminator() != 0;
2019 LBF
= dyn_cast
<DILexicalBlockFile
>(Scope
))
2020 Scope
= LBF
->getScope();
2021 DILexicalBlockFile
*NewScope
=
2022 DILexicalBlockFile::get(getContext(), Scope
, getFile(), Discriminator
);
2023 return DILocation::get(getContext(), getLine(), getColumn(), NewScope
,
2027 unsigned DILocation::getBaseDiscriminator() const {
2028 return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
2031 unsigned DILocation::getDuplicationFactor() const {
2032 return getDuplicationFactorFromDiscriminator(getDiscriminator());
2035 unsigned DILocation::getCopyIdentifier() const {
2036 return getCopyIdentifierFromDiscriminator(getDiscriminator());
2039 Optional
<const DILocation
*> DILocation::cloneWithBaseDiscriminator(unsigned D
) const {
2040 unsigned BD
, DF
, CI
;
2041 decodeDiscriminator(getDiscriminator(), BD
, DF
, CI
);
2044 if (Optional
<unsigned> Encoded
= encodeDiscriminator(D
, DF
, CI
))
2045 return cloneWithDiscriminator(*Encoded
);
2049 Optional
<const DILocation
*> DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF
) const {
2050 DF
*= getDuplicationFactor();
2054 unsigned BD
= getBaseDiscriminator();
2055 unsigned CI
= getCopyIdentifier();
2056 if (Optional
<unsigned> D
= encodeDiscriminator(BD
, DF
, CI
))
2057 return cloneWithDiscriminator(*D
);
2061 class DINamespace
: public DIScope
{
2062 friend class LLVMContextImpl
;
2063 friend class MDNode
;
2065 unsigned ExportSymbols
: 1;
2067 DINamespace(LLVMContext
&Context
, StorageType Storage
, bool ExportSymbols
,
2068 ArrayRef
<Metadata
*> Ops
)
2069 : DIScope(Context
, DINamespaceKind
, Storage
, dwarf::DW_TAG_namespace
,
2071 ExportSymbols(ExportSymbols
) {}
2072 ~DINamespace() = default;
2074 static DINamespace
*getImpl(LLVMContext
&Context
, DIScope
*Scope
,
2075 StringRef Name
, bool ExportSymbols
,
2076 StorageType Storage
, bool ShouldCreate
= true) {
2077 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
),
2078 ExportSymbols
, Storage
, ShouldCreate
);
2080 static DINamespace
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
2081 MDString
*Name
, bool ExportSymbols
,
2082 StorageType Storage
, bool ShouldCreate
= true);
2084 TempDINamespace
cloneImpl() const {
2085 return getTemporary(getContext(), getScope(), getName(),
2086 getExportSymbols());
2090 DEFINE_MDNODE_GET(DINamespace
,
2091 (DIScope
*Scope
, StringRef Name
, bool ExportSymbols
),
2092 (Scope
, Name
, ExportSymbols
))
2093 DEFINE_MDNODE_GET(DINamespace
,
2094 (Metadata
*Scope
, MDString
*Name
, bool ExportSymbols
),
2095 (Scope
, Name
, ExportSymbols
))
2097 TempDINamespace
clone() const { return cloneImpl(); }
2099 bool getExportSymbols() const { return ExportSymbols
; }
2100 DIScope
*getScope() const { return cast_or_null
<DIScope
>(getRawScope()); }
2101 StringRef
getName() const { return getStringOperand(2); }
2103 Metadata
*getRawScope() const { return getOperand(1); }
2104 MDString
*getRawName() const { return getOperandAs
<MDString
>(2); }
2106 static bool classof(const Metadata
*MD
) {
2107 return MD
->getMetadataID() == DINamespaceKind
;
2111 /// A (clang) module that has been imported by the compile unit.
2113 class DIModule
: public DIScope
{
2114 friend class LLVMContextImpl
;
2115 friend class MDNode
;
2117 DIModule(LLVMContext
&Context
, StorageType Storage
, ArrayRef
<Metadata
*> Ops
)
2118 : DIScope(Context
, DIModuleKind
, Storage
, dwarf::DW_TAG_module
, Ops
) {}
2119 ~DIModule() = default;
2121 static DIModule
*getImpl(LLVMContext
&Context
, DIScope
*Scope
,
2122 StringRef Name
, StringRef ConfigurationMacros
,
2123 StringRef IncludePath
, StringRef ISysRoot
,
2124 StorageType Storage
, bool ShouldCreate
= true) {
2125 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
),
2126 getCanonicalMDString(Context
, ConfigurationMacros
),
2127 getCanonicalMDString(Context
, IncludePath
),
2128 getCanonicalMDString(Context
, ISysRoot
),
2129 Storage
, ShouldCreate
);
2131 static DIModule
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
2132 MDString
*Name
, MDString
*ConfigurationMacros
,
2133 MDString
*IncludePath
, MDString
*ISysRoot
,
2134 StorageType Storage
, bool ShouldCreate
= true);
2136 TempDIModule
cloneImpl() const {
2137 return getTemporary(getContext(), getScope(), getName(),
2138 getConfigurationMacros(), getIncludePath(),
2143 DEFINE_MDNODE_GET(DIModule
, (DIScope
*Scope
, StringRef Name
,
2144 StringRef ConfigurationMacros
, StringRef IncludePath
,
2145 StringRef ISysRoot
),
2146 (Scope
, Name
, ConfigurationMacros
, IncludePath
, ISysRoot
))
2147 DEFINE_MDNODE_GET(DIModule
,
2148 (Metadata
*Scope
, MDString
*Name
, MDString
*ConfigurationMacros
,
2149 MDString
*IncludePath
, MDString
*ISysRoot
),
2150 (Scope
, Name
, ConfigurationMacros
, IncludePath
, ISysRoot
))
2152 TempDIModule
clone() const { return cloneImpl(); }
2154 DIScope
*getScope() const { return cast_or_null
<DIScope
>(getRawScope()); }
2155 StringRef
getName() const { return getStringOperand(1); }
2156 StringRef
getConfigurationMacros() const { return getStringOperand(2); }
2157 StringRef
getIncludePath() const { return getStringOperand(3); }
2158 StringRef
getISysRoot() const { return getStringOperand(4); }
2160 Metadata
*getRawScope() const { return getOperand(0); }
2161 MDString
*getRawName() const { return getOperandAs
<MDString
>(1); }
2162 MDString
*getRawConfigurationMacros() const { return getOperandAs
<MDString
>(2); }
2163 MDString
*getRawIncludePath() const { return getOperandAs
<MDString
>(3); }
2164 MDString
*getRawISysRoot() const { return getOperandAs
<MDString
>(4); }
2166 static bool classof(const Metadata
*MD
) {
2167 return MD
->getMetadataID() == DIModuleKind
;
2171 /// Base class for template parameters.
2172 class DITemplateParameter
: public DINode
{
2174 DITemplateParameter(LLVMContext
&Context
, unsigned ID
, StorageType Storage
,
2175 unsigned Tag
, ArrayRef
<Metadata
*> Ops
)
2176 : DINode(Context
, ID
, Storage
, Tag
, Ops
) {}
2177 ~DITemplateParameter() = default;
2180 StringRef
getName() const { return getStringOperand(0); }
2181 DITypeRef
getType() const { return DITypeRef(getRawType()); }
2183 MDString
*getRawName() const { return getOperandAs
<MDString
>(0); }
2184 Metadata
*getRawType() const { return getOperand(1); }
2186 static bool classof(const Metadata
*MD
) {
2187 return MD
->getMetadataID() == DITemplateTypeParameterKind
||
2188 MD
->getMetadataID() == DITemplateValueParameterKind
;
2192 class DITemplateTypeParameter
: public DITemplateParameter
{
2193 friend class LLVMContextImpl
;
2194 friend class MDNode
;
2196 DITemplateTypeParameter(LLVMContext
&Context
, StorageType Storage
,
2197 ArrayRef
<Metadata
*> Ops
)
2198 : DITemplateParameter(Context
, DITemplateTypeParameterKind
, Storage
,
2199 dwarf::DW_TAG_template_type_parameter
, Ops
) {}
2200 ~DITemplateTypeParameter() = default;
2202 static DITemplateTypeParameter
*getImpl(LLVMContext
&Context
, StringRef Name
,
2203 DITypeRef Type
, StorageType Storage
,
2204 bool ShouldCreate
= true) {
2205 return getImpl(Context
, getCanonicalMDString(Context
, Name
), Type
, Storage
,
2208 static DITemplateTypeParameter
*getImpl(LLVMContext
&Context
, MDString
*Name
,
2209 Metadata
*Type
, StorageType Storage
,
2210 bool ShouldCreate
= true);
2212 TempDITemplateTypeParameter
cloneImpl() const {
2213 return getTemporary(getContext(), getName(), getType());
2217 DEFINE_MDNODE_GET(DITemplateTypeParameter
, (StringRef Name
, DITypeRef Type
),
2219 DEFINE_MDNODE_GET(DITemplateTypeParameter
, (MDString
* Name
, Metadata
*Type
),
2222 TempDITemplateTypeParameter
clone() const { return cloneImpl(); }
2224 static bool classof(const Metadata
*MD
) {
2225 return MD
->getMetadataID() == DITemplateTypeParameterKind
;
2229 class DITemplateValueParameter
: public DITemplateParameter
{
2230 friend class LLVMContextImpl
;
2231 friend class MDNode
;
2233 DITemplateValueParameter(LLVMContext
&Context
, StorageType Storage
,
2234 unsigned Tag
, ArrayRef
<Metadata
*> Ops
)
2235 : DITemplateParameter(Context
, DITemplateValueParameterKind
, Storage
, Tag
,
2237 ~DITemplateValueParameter() = default;
2239 static DITemplateValueParameter
*getImpl(LLVMContext
&Context
, unsigned Tag
,
2240 StringRef Name
, DITypeRef Type
,
2241 Metadata
*Value
, StorageType Storage
,
2242 bool ShouldCreate
= true) {
2243 return getImpl(Context
, Tag
, getCanonicalMDString(Context
, Name
), Type
,
2244 Value
, Storage
, ShouldCreate
);
2246 static DITemplateValueParameter
*getImpl(LLVMContext
&Context
, unsigned Tag
,
2247 MDString
*Name
, Metadata
*Type
,
2248 Metadata
*Value
, StorageType Storage
,
2249 bool ShouldCreate
= true);
2251 TempDITemplateValueParameter
cloneImpl() const {
2252 return getTemporary(getContext(), getTag(), getName(), getType(),
2257 DEFINE_MDNODE_GET(DITemplateValueParameter
, (unsigned Tag
, StringRef Name
,
2258 DITypeRef Type
, Metadata
*Value
),
2259 (Tag
, Name
, Type
, Value
))
2260 DEFINE_MDNODE_GET(DITemplateValueParameter
, (unsigned Tag
, MDString
*Name
,
2261 Metadata
*Type
, Metadata
*Value
),
2262 (Tag
, Name
, Type
, Value
))
2264 TempDITemplateValueParameter
clone() const { return cloneImpl(); }
2266 Metadata
*getValue() const { return getOperand(2); }
2268 static bool classof(const Metadata
*MD
) {
2269 return MD
->getMetadataID() == DITemplateValueParameterKind
;
2273 /// Base class for variables.
2274 class DIVariable
: public DINode
{
2276 uint32_t AlignInBits
;
2279 DIVariable(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned Line
,
2280 ArrayRef
<Metadata
*> Ops
, uint32_t AlignInBits
= 0)
2281 : DINode(C
, ID
, Storage
, dwarf::DW_TAG_variable
, Ops
), Line(Line
),
2282 AlignInBits(AlignInBits
) {}
2283 ~DIVariable() = default;
2286 unsigned getLine() const { return Line
; }
2287 DIScope
*getScope() const { return cast_or_null
<DIScope
>(getRawScope()); }
2288 StringRef
getName() const { return getStringOperand(1); }
2289 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
2290 DITypeRef
getType() const { return DITypeRef(getRawType()); }
2291 uint32_t getAlignInBits() const { return AlignInBits
; }
2292 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT
; }
2293 /// Determines the size of the variable's type.
2294 Optional
<uint64_t> getSizeInBits() const;
2296 /// Return the signedness of this variable's type, or None if this type is
2297 /// neither signed nor unsigned.
2298 Optional
<DIBasicType::Signedness
> getSignedness() const {
2299 if (auto *BT
= dyn_cast
<DIBasicType
>(getType().resolve()))
2300 return BT
->getSignedness();
2304 StringRef
getFilename() const {
2305 if (auto *F
= getFile())
2306 return F
->getFilename();
2310 StringRef
getDirectory() const {
2311 if (auto *F
= getFile())
2312 return F
->getDirectory();
2316 Optional
<StringRef
> getSource() const {
2317 if (auto *F
= getFile())
2318 return F
->getSource();
2322 Metadata
*getRawScope() const { return getOperand(0); }
2323 MDString
*getRawName() const { return getOperandAs
<MDString
>(1); }
2324 Metadata
*getRawFile() const { return getOperand(2); }
2325 Metadata
*getRawType() const { return getOperand(3); }
2327 static bool classof(const Metadata
*MD
) {
2328 return MD
->getMetadataID() == DILocalVariableKind
||
2329 MD
->getMetadataID() == DIGlobalVariableKind
;
2333 /// DWARF expression.
2335 /// This is (almost) a DWARF expression that modifies the location of a
2336 /// variable, or the location of a single piece of a variable, or (when using
2337 /// DW_OP_stack_value) is the constant variable value.
2339 /// TODO: Co-allocate the expression elements.
2340 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2342 class DIExpression
: public MDNode
{
2343 friend class LLVMContextImpl
;
2344 friend class MDNode
;
2346 std::vector
<uint64_t> Elements
;
2348 DIExpression(LLVMContext
&C
, StorageType Storage
, ArrayRef
<uint64_t> Elements
)
2349 : MDNode(C
, DIExpressionKind
, Storage
, None
),
2350 Elements(Elements
.begin(), Elements
.end()) {}
2351 ~DIExpression() = default;
2353 static DIExpression
*getImpl(LLVMContext
&Context
,
2354 ArrayRef
<uint64_t> Elements
, StorageType Storage
,
2355 bool ShouldCreate
= true);
2357 TempDIExpression
cloneImpl() const {
2358 return getTemporary(getContext(), getElements());
2362 DEFINE_MDNODE_GET(DIExpression
, (ArrayRef
<uint64_t> Elements
), (Elements
))
2364 TempDIExpression
clone() const { return cloneImpl(); }
2366 ArrayRef
<uint64_t> getElements() const { return Elements
; }
2368 unsigned getNumElements() const { return Elements
.size(); }
2370 uint64_t getElement(unsigned I
) const {
2371 assert(I
< Elements
.size() && "Index out of range");
2375 /// Determine whether this represents a standalone constant value.
2376 bool isConstant() const;
2378 using element_iterator
= ArrayRef
<uint64_t>::iterator
;
2380 element_iterator
elements_begin() const { return getElements().begin(); }
2381 element_iterator
elements_end() const { return getElements().end(); }
2383 /// A lightweight wrapper around an expression operand.
2385 /// TODO: Store arguments directly and change \a DIExpression to store a
2388 const uint64_t *Op
= nullptr;
2391 ExprOperand() = default;
2392 explicit ExprOperand(const uint64_t *Op
) : Op(Op
) {}
2394 const uint64_t *get() const { return Op
; }
2396 /// Get the operand code.
2397 uint64_t getOp() const { return *Op
; }
2399 /// Get an argument to the operand.
2401 /// Never returns the operand itself.
2402 uint64_t getArg(unsigned I
) const { return Op
[I
+ 1]; }
2404 unsigned getNumArgs() const { return getSize() - 1; }
2406 /// Return the size of the operand.
2408 /// Return the number of elements in the operand (1 + args).
2409 unsigned getSize() const;
2411 /// Append the elements of this operand to \p V.
2412 void appendToVector(SmallVectorImpl
<uint64_t> &V
) const {
2413 V
.append(get(), get() + getSize());
2417 /// An iterator for expression operands.
2418 class expr_op_iterator
2419 : public std::iterator
<std::input_iterator_tag
, ExprOperand
> {
2423 expr_op_iterator() = default;
2424 explicit expr_op_iterator(element_iterator I
) : Op(I
) {}
2426 element_iterator
getBase() const { return Op
.get(); }
2427 const ExprOperand
&operator*() const { return Op
; }
2428 const ExprOperand
*operator->() const { return &Op
; }
2430 expr_op_iterator
&operator++() {
2434 expr_op_iterator
operator++(int) {
2435 expr_op_iterator
T(*this);
2440 /// Get the next iterator.
2442 /// \a std::next() doesn't work because this is technically an
2443 /// input_iterator, but it's a perfectly valid operation. This is an
2444 /// accessor to provide the same functionality.
2445 expr_op_iterator
getNext() const { return ++expr_op_iterator(*this); }
2447 bool operator==(const expr_op_iterator
&X
) const {
2448 return getBase() == X
.getBase();
2450 bool operator!=(const expr_op_iterator
&X
) const {
2451 return getBase() != X
.getBase();
2455 void increment() { Op
= ExprOperand(getBase() + Op
.getSize()); }
2458 /// Visit the elements via ExprOperand wrappers.
2460 /// These range iterators visit elements through \a ExprOperand wrappers.
2461 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2464 /// \pre \a isValid() gives \c true.
2466 expr_op_iterator
expr_op_begin() const {
2467 return expr_op_iterator(elements_begin());
2469 expr_op_iterator
expr_op_end() const {
2470 return expr_op_iterator(elements_end());
2472 iterator_range
<expr_op_iterator
> expr_ops() const {
2473 return {expr_op_begin(), expr_op_end()};
2477 bool isValid() const;
2479 static bool classof(const Metadata
*MD
) {
2480 return MD
->getMetadataID() == DIExpressionKind
;
2483 /// Return whether the first element a DW_OP_deref.
2484 bool startsWithDeref() const {
2485 return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref
;
2488 /// Holds the characteristics of one fragment of a larger variable.
2489 struct FragmentInfo
{
2490 uint64_t SizeInBits
;
2491 uint64_t OffsetInBits
;
2494 /// Retrieve the details of this fragment expression.
2495 static Optional
<FragmentInfo
> getFragmentInfo(expr_op_iterator Start
,
2496 expr_op_iterator End
);
2498 /// Retrieve the details of this fragment expression.
2499 Optional
<FragmentInfo
> getFragmentInfo() const {
2500 return getFragmentInfo(expr_op_begin(), expr_op_end());
2503 /// Return whether this is a piece of an aggregate variable.
2504 bool isFragment() const { return getFragmentInfo().hasValue(); }
2506 /// Append \p Ops with operations to apply the \p Offset.
2507 static void appendOffset(SmallVectorImpl
<uint64_t> &Ops
, int64_t Offset
);
2509 /// If this is a constant offset, extract it. If there is no expression,
2510 /// return true with an offset of zero.
2511 bool extractIfOffset(int64_t &Offset
) const;
2513 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
2514 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
2516 static const DIExpression
*extractAddressClass(const DIExpression
*Expr
,
2517 unsigned &AddrClass
);
2519 /// Constants for DIExpression::prepend.
2520 enum { NoDeref
= false, WithDeref
= true, WithStackValue
= true };
2522 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2523 /// into a stack value.
2524 static DIExpression
*prepend(const DIExpression
*Expr
, bool DerefBefore
,
2525 int64_t Offset
= 0, bool DerefAfter
= false,
2526 bool StackValue
= false);
2528 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2530 static DIExpression
*prependOpcodes(const DIExpression
*Expr
,
2531 SmallVectorImpl
<uint64_t> &Ops
,
2532 bool StackValue
= false);
2534 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
2535 /// returned expression is a stack value only if \p DIExpr is a stack value.
2536 /// If \p DIExpr describes a fragment, the returned expression will describe
2537 /// the same fragment.
2538 static DIExpression
*append(const DIExpression
*Expr
, ArrayRef
<uint64_t> Ops
);
2540 /// Convert \p DIExpr into a stack value if it isn't one already by appending
2541 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
2542 /// If \p DIExpr describes a fragment, the returned expression will describe
2543 /// the same fragment.
2544 static DIExpression
*appendToStack(const DIExpression
*Expr
,
2545 ArrayRef
<uint64_t> Ops
);
2547 /// Create a DIExpression to describe one part of an aggregate variable that
2548 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2549 /// will be appended to the elements of \c Expr. If \c Expr already contains
2550 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2551 /// into the existing fragment.
2553 /// \param OffsetInBits Offset of the piece in bits.
2554 /// \param SizeInBits Size of the piece in bits.
2555 /// \return Creating a fragment expression may fail if \c Expr
2556 /// contains arithmetic operations that would be truncated.
2557 static Optional
<DIExpression
*>
2558 createFragmentExpression(const DIExpression
*Expr
, unsigned OffsetInBits
,
2559 unsigned SizeInBits
);
2561 /// Determine the relative position of the fragments described by this
2562 /// DIExpression and \p Other.
2563 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2564 /// 1 if this is entirely after Other.
2565 int fragmentCmp(const DIExpression
*Other
) const {
2566 auto Fragment1
= *getFragmentInfo();
2567 auto Fragment2
= *Other
->getFragmentInfo();
2568 unsigned l1
= Fragment1
.OffsetInBits
;
2569 unsigned l2
= Fragment2
.OffsetInBits
;
2570 unsigned r1
= l1
+ Fragment1
.SizeInBits
;
2571 unsigned r2
= l2
+ Fragment2
.SizeInBits
;
2580 /// Check if fragments overlap between this DIExpression and \p Other.
2581 bool fragmentsOverlap(const DIExpression
*Other
) const {
2582 if (!isFragment() || !Other
->isFragment())
2584 return fragmentCmp(Other
) == 0;
2588 /// Global variables.
2590 /// TODO: Remove DisplayName. It's always equal to Name.
2591 class DIGlobalVariable
: public DIVariable
{
2592 friend class LLVMContextImpl
;
2593 friend class MDNode
;
2598 DIGlobalVariable(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
2599 bool IsLocalToUnit
, bool IsDefinition
, uint32_t AlignInBits
,
2600 ArrayRef
<Metadata
*> Ops
)
2601 : DIVariable(C
, DIGlobalVariableKind
, Storage
, Line
, Ops
, AlignInBits
),
2602 IsLocalToUnit(IsLocalToUnit
), IsDefinition(IsDefinition
) {}
2603 ~DIGlobalVariable() = default;
2605 static DIGlobalVariable
*
2606 getImpl(LLVMContext
&Context
, DIScope
*Scope
, StringRef Name
,
2607 StringRef LinkageName
, DIFile
*File
, unsigned Line
, DITypeRef Type
,
2608 bool IsLocalToUnit
, bool IsDefinition
,
2609 DIDerivedType
*StaticDataMemberDeclaration
, MDTuple
*TemplateParams
,
2610 uint32_t AlignInBits
, StorageType Storage
, bool ShouldCreate
= true) {
2611 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
),
2612 getCanonicalMDString(Context
, LinkageName
), File
, Line
, Type
,
2613 IsLocalToUnit
, IsDefinition
, StaticDataMemberDeclaration
,
2614 cast_or_null
<Metadata
>(TemplateParams
), AlignInBits
, Storage
,
2617 static DIGlobalVariable
*
2618 getImpl(LLVMContext
&Context
, Metadata
*Scope
, MDString
*Name
,
2619 MDString
*LinkageName
, Metadata
*File
, unsigned Line
, Metadata
*Type
,
2620 bool IsLocalToUnit
, bool IsDefinition
,
2621 Metadata
*StaticDataMemberDeclaration
, Metadata
*TemplateParams
,
2622 uint32_t AlignInBits
, StorageType Storage
, bool ShouldCreate
= true);
2624 TempDIGlobalVariable
cloneImpl() const {
2625 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
2626 getFile(), getLine(), getType(), isLocalToUnit(),
2627 isDefinition(), getStaticDataMemberDeclaration(),
2628 getTemplateParams(), getAlignInBits());
2632 DEFINE_MDNODE_GET(DIGlobalVariable
,
2633 (DIScope
* Scope
, StringRef Name
, StringRef LinkageName
,
2634 DIFile
*File
, unsigned Line
, DITypeRef Type
,
2635 bool IsLocalToUnit
, bool IsDefinition
,
2636 DIDerivedType
*StaticDataMemberDeclaration
,
2637 MDTuple
*TemplateParams
, uint32_t AlignInBits
),
2638 (Scope
, Name
, LinkageName
, File
, Line
, Type
, IsLocalToUnit
,
2639 IsDefinition
, StaticDataMemberDeclaration
, TemplateParams
,
2641 DEFINE_MDNODE_GET(DIGlobalVariable
,
2642 (Metadata
* Scope
, MDString
*Name
, MDString
*LinkageName
,
2643 Metadata
*File
, unsigned Line
, Metadata
*Type
,
2644 bool IsLocalToUnit
, bool IsDefinition
,
2645 Metadata
*StaticDataMemberDeclaration
,
2646 Metadata
*TemplateParams
, uint32_t AlignInBits
),
2647 (Scope
, Name
, LinkageName
, File
, Line
, Type
, IsLocalToUnit
,
2648 IsDefinition
, StaticDataMemberDeclaration
, TemplateParams
,
2651 TempDIGlobalVariable
clone() const { return cloneImpl(); }
2653 bool isLocalToUnit() const { return IsLocalToUnit
; }
2654 bool isDefinition() const { return IsDefinition
; }
2655 StringRef
getDisplayName() const { return getStringOperand(4); }
2656 StringRef
getLinkageName() const { return getStringOperand(5); }
2657 DIDerivedType
*getStaticDataMemberDeclaration() const {
2658 return cast_or_null
<DIDerivedType
>(getRawStaticDataMemberDeclaration());
2661 MDString
*getRawLinkageName() const { return getOperandAs
<MDString
>(5); }
2662 Metadata
*getRawStaticDataMemberDeclaration() const { return getOperand(6); }
2663 Metadata
*getRawTemplateParams() const { return getOperand(7); }
2664 MDTuple
*getTemplateParams() const { return getOperandAs
<MDTuple
>(7); }
2666 static bool classof(const Metadata
*MD
) {
2667 return MD
->getMetadataID() == DIGlobalVariableKind
;
2673 /// TODO: Split up flags.
2674 class DILocalVariable
: public DIVariable
{
2675 friend class LLVMContextImpl
;
2676 friend class MDNode
;
2681 DILocalVariable(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
2682 unsigned Arg
, DIFlags Flags
, uint32_t AlignInBits
,
2683 ArrayRef
<Metadata
*> Ops
)
2684 : DIVariable(C
, DILocalVariableKind
, Storage
, Line
, Ops
, AlignInBits
),
2685 Arg(Arg
), Flags(Flags
) {
2686 assert(Arg
< (1 << 16) && "DILocalVariable: Arg out of range");
2688 ~DILocalVariable() = default;
2690 static DILocalVariable
*getImpl(LLVMContext
&Context
, DIScope
*Scope
,
2691 StringRef Name
, DIFile
*File
, unsigned Line
,
2692 DITypeRef Type
, unsigned Arg
, DIFlags Flags
,
2693 uint32_t AlignInBits
, StorageType Storage
,
2694 bool ShouldCreate
= true) {
2695 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
), File
,
2696 Line
, Type
, Arg
, Flags
, AlignInBits
, Storage
, ShouldCreate
);
2698 static DILocalVariable
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
2699 MDString
*Name
, Metadata
*File
, unsigned Line
,
2700 Metadata
*Type
, unsigned Arg
, DIFlags Flags
,
2701 uint32_t AlignInBits
, StorageType Storage
,
2702 bool ShouldCreate
= true);
2704 TempDILocalVariable
cloneImpl() const {
2705 return getTemporary(getContext(), getScope(), getName(), getFile(),
2706 getLine(), getType(), getArg(), getFlags(),
2711 DEFINE_MDNODE_GET(DILocalVariable
,
2712 (DILocalScope
* Scope
, StringRef Name
, DIFile
*File
,
2713 unsigned Line
, DITypeRef Type
, unsigned Arg
,
2714 DIFlags Flags
, uint32_t AlignInBits
),
2715 (Scope
, Name
, File
, Line
, Type
, Arg
, Flags
, AlignInBits
))
2716 DEFINE_MDNODE_GET(DILocalVariable
,
2717 (Metadata
* Scope
, MDString
*Name
, Metadata
*File
,
2718 unsigned Line
, Metadata
*Type
, unsigned Arg
,
2719 DIFlags Flags
, uint32_t AlignInBits
),
2720 (Scope
, Name
, File
, Line
, Type
, Arg
, Flags
, AlignInBits
))
2722 TempDILocalVariable
clone() const { return cloneImpl(); }
2724 /// Get the local scope for this variable.
2726 /// Variables must be defined in a local scope.
2727 DILocalScope
*getScope() const {
2728 return cast
<DILocalScope
>(DIVariable::getScope());
2731 bool isParameter() const { return Arg
; }
2732 unsigned getArg() const { return Arg
; }
2733 DIFlags
getFlags() const { return Flags
; }
2735 bool isArtificial() const { return getFlags() & FlagArtificial
; }
2736 bool isObjectPointer() const { return getFlags() & FlagObjectPointer
; }
2738 /// Check that a location is valid for this variable.
2740 /// Check that \c DL exists, is in the same subprogram, and has the same
2741 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2742 /// to a \a DbgInfoIntrinsic.)
2743 bool isValidLocationForIntrinsic(const DILocation
*DL
) const {
2744 return DL
&& getScope()->getSubprogram() == DL
->getScope()->getSubprogram();
2747 static bool classof(const Metadata
*MD
) {
2748 return MD
->getMetadataID() == DILocalVariableKind
;
2754 class DILabel
: public DINode
{
2755 friend class LLVMContextImpl
;
2756 friend class MDNode
;
2760 DILabel(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
2761 ArrayRef
<Metadata
*> Ops
)
2762 : DINode(C
, DILabelKind
, Storage
, dwarf::DW_TAG_label
, Ops
), Line(Line
) {}
2763 ~DILabel() = default;
2765 static DILabel
*getImpl(LLVMContext
&Context
, DIScope
*Scope
,
2766 StringRef Name
, DIFile
*File
, unsigned Line
,
2767 StorageType Storage
,
2768 bool ShouldCreate
= true) {
2769 return getImpl(Context
, Scope
, getCanonicalMDString(Context
, Name
), File
,
2770 Line
, Storage
, ShouldCreate
);
2772 static DILabel
*getImpl(LLVMContext
&Context
, Metadata
*Scope
,
2773 MDString
*Name
, Metadata
*File
, unsigned Line
,
2774 StorageType Storage
,
2775 bool ShouldCreate
= true);
2777 TempDILabel
cloneImpl() const {
2778 return getTemporary(getContext(), getScope(), getName(), getFile(),
2783 DEFINE_MDNODE_GET(DILabel
,
2784 (DILocalScope
* Scope
, StringRef Name
, DIFile
*File
,
2786 (Scope
, Name
, File
, Line
))
2787 DEFINE_MDNODE_GET(DILabel
,
2788 (Metadata
* Scope
, MDString
*Name
, Metadata
*File
,
2790 (Scope
, Name
, File
, Line
))
2792 TempDILabel
clone() const { return cloneImpl(); }
2794 /// Get the local scope for this label.
2796 /// Labels must be defined in a local scope.
2797 DILocalScope
*getScope() const {
2798 return cast_or_null
<DILocalScope
>(getRawScope());
2800 unsigned getLine() const { return Line
; }
2801 StringRef
getName() const { return getStringOperand(1); }
2802 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
2804 Metadata
*getRawScope() const { return getOperand(0); }
2805 MDString
*getRawName() const { return getOperandAs
<MDString
>(1); }
2806 Metadata
*getRawFile() const { return getOperand(2); }
2808 /// Check that a location is valid for this label.
2810 /// Check that \c DL exists, is in the same subprogram, and has the same
2811 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2812 /// to a \a DbgInfoIntrinsic.)
2813 bool isValidLocationForIntrinsic(const DILocation
*DL
) const {
2814 return DL
&& getScope()->getSubprogram() == DL
->getScope()->getSubprogram();
2817 static bool classof(const Metadata
*MD
) {
2818 return MD
->getMetadataID() == DILabelKind
;
2822 class DIObjCProperty
: public DINode
{
2823 friend class LLVMContextImpl
;
2824 friend class MDNode
;
2827 unsigned Attributes
;
2829 DIObjCProperty(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
2830 unsigned Attributes
, ArrayRef
<Metadata
*> Ops
)
2831 : DINode(C
, DIObjCPropertyKind
, Storage
, dwarf::DW_TAG_APPLE_property
,
2833 Line(Line
), Attributes(Attributes
) {}
2834 ~DIObjCProperty() = default;
2836 static DIObjCProperty
*
2837 getImpl(LLVMContext
&Context
, StringRef Name
, DIFile
*File
, unsigned Line
,
2838 StringRef GetterName
, StringRef SetterName
, unsigned Attributes
,
2839 DITypeRef Type
, StorageType Storage
, bool ShouldCreate
= true) {
2840 return getImpl(Context
, getCanonicalMDString(Context
, Name
), File
, Line
,
2841 getCanonicalMDString(Context
, GetterName
),
2842 getCanonicalMDString(Context
, SetterName
), Attributes
, Type
,
2843 Storage
, ShouldCreate
);
2845 static DIObjCProperty
*getImpl(LLVMContext
&Context
, MDString
*Name
,
2846 Metadata
*File
, unsigned Line
,
2847 MDString
*GetterName
, MDString
*SetterName
,
2848 unsigned Attributes
, Metadata
*Type
,
2849 StorageType Storage
, bool ShouldCreate
= true);
2851 TempDIObjCProperty
cloneImpl() const {
2852 return getTemporary(getContext(), getName(), getFile(), getLine(),
2853 getGetterName(), getSetterName(), getAttributes(),
2858 DEFINE_MDNODE_GET(DIObjCProperty
,
2859 (StringRef Name
, DIFile
*File
, unsigned Line
,
2860 StringRef GetterName
, StringRef SetterName
,
2861 unsigned Attributes
, DITypeRef Type
),
2862 (Name
, File
, Line
, GetterName
, SetterName
, Attributes
,
2864 DEFINE_MDNODE_GET(DIObjCProperty
,
2865 (MDString
* Name
, Metadata
*File
, unsigned Line
,
2866 MDString
*GetterName
, MDString
*SetterName
,
2867 unsigned Attributes
, Metadata
*Type
),
2868 (Name
, File
, Line
, GetterName
, SetterName
, Attributes
,
2871 TempDIObjCProperty
clone() const { return cloneImpl(); }
2873 unsigned getLine() const { return Line
; }
2874 unsigned getAttributes() const { return Attributes
; }
2875 StringRef
getName() const { return getStringOperand(0); }
2876 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
2877 StringRef
getGetterName() const { return getStringOperand(2); }
2878 StringRef
getSetterName() const { return getStringOperand(3); }
2879 DITypeRef
getType() const { return DITypeRef(getRawType()); }
2881 StringRef
getFilename() const {
2882 if (auto *F
= getFile())
2883 return F
->getFilename();
2887 StringRef
getDirectory() const {
2888 if (auto *F
= getFile())
2889 return F
->getDirectory();
2893 Optional
<StringRef
> getSource() const {
2894 if (auto *F
= getFile())
2895 return F
->getSource();
2899 MDString
*getRawName() const { return getOperandAs
<MDString
>(0); }
2900 Metadata
*getRawFile() const { return getOperand(1); }
2901 MDString
*getRawGetterName() const { return getOperandAs
<MDString
>(2); }
2902 MDString
*getRawSetterName() const { return getOperandAs
<MDString
>(3); }
2903 Metadata
*getRawType() const { return getOperand(4); }
2905 static bool classof(const Metadata
*MD
) {
2906 return MD
->getMetadataID() == DIObjCPropertyKind
;
2910 /// An imported module (C++ using directive or similar).
2911 class DIImportedEntity
: public DINode
{
2912 friend class LLVMContextImpl
;
2913 friend class MDNode
;
2917 DIImportedEntity(LLVMContext
&C
, StorageType Storage
, unsigned Tag
,
2918 unsigned Line
, ArrayRef
<Metadata
*> Ops
)
2919 : DINode(C
, DIImportedEntityKind
, Storage
, Tag
, Ops
), Line(Line
) {}
2920 ~DIImportedEntity() = default;
2922 static DIImportedEntity
*getImpl(LLVMContext
&Context
, unsigned Tag
,
2923 DIScope
*Scope
, DINodeRef Entity
,
2924 DIFile
*File
, unsigned Line
, StringRef Name
,
2925 StorageType Storage
,
2926 bool ShouldCreate
= true) {
2927 return getImpl(Context
, Tag
, Scope
, Entity
, File
, Line
,
2928 getCanonicalMDString(Context
, Name
), Storage
, ShouldCreate
);
2930 static DIImportedEntity
*getImpl(LLVMContext
&Context
, unsigned Tag
,
2931 Metadata
*Scope
, Metadata
*Entity
,
2932 Metadata
*File
, unsigned Line
,
2933 MDString
*Name
, StorageType Storage
,
2934 bool ShouldCreate
= true);
2936 TempDIImportedEntity
cloneImpl() const {
2937 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2938 getFile(), getLine(), getName());
2942 DEFINE_MDNODE_GET(DIImportedEntity
,
2943 (unsigned Tag
, DIScope
*Scope
, DINodeRef Entity
,
2944 DIFile
*File
, unsigned Line
, StringRef Name
= ""),
2945 (Tag
, Scope
, Entity
, File
, Line
, Name
))
2946 DEFINE_MDNODE_GET(DIImportedEntity
,
2947 (unsigned Tag
, Metadata
*Scope
, Metadata
*Entity
,
2948 Metadata
*File
, unsigned Line
, MDString
*Name
),
2949 (Tag
, Scope
, Entity
, File
, Line
, Name
))
2951 TempDIImportedEntity
clone() const { return cloneImpl(); }
2953 unsigned getLine() const { return Line
; }
2954 DIScope
*getScope() const { return cast_or_null
<DIScope
>(getRawScope()); }
2955 DINodeRef
getEntity() const { return DINodeRef(getRawEntity()); }
2956 StringRef
getName() const { return getStringOperand(2); }
2957 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
2959 Metadata
*getRawScope() const { return getOperand(0); }
2960 Metadata
*getRawEntity() const { return getOperand(1); }
2961 MDString
*getRawName() const { return getOperandAs
<MDString
>(2); }
2962 Metadata
*getRawFile() const { return getOperand(3); }
2964 static bool classof(const Metadata
*MD
) {
2965 return MD
->getMetadataID() == DIImportedEntityKind
;
2969 /// A pair of DIGlobalVariable and DIExpression.
2970 class DIGlobalVariableExpression
: public MDNode
{
2971 friend class LLVMContextImpl
;
2972 friend class MDNode
;
2974 DIGlobalVariableExpression(LLVMContext
&C
, StorageType Storage
,
2975 ArrayRef
<Metadata
*> Ops
)
2976 : MDNode(C
, DIGlobalVariableExpressionKind
, Storage
, Ops
) {}
2977 ~DIGlobalVariableExpression() = default;
2979 static DIGlobalVariableExpression
*
2980 getImpl(LLVMContext
&Context
, Metadata
*Variable
, Metadata
*Expression
,
2981 StorageType Storage
, bool ShouldCreate
= true);
2983 TempDIGlobalVariableExpression
cloneImpl() const {
2984 return getTemporary(getContext(), getVariable(), getExpression());
2988 DEFINE_MDNODE_GET(DIGlobalVariableExpression
,
2989 (Metadata
* Variable
, Metadata
*Expression
),
2990 (Variable
, Expression
))
2992 TempDIGlobalVariableExpression
clone() const { return cloneImpl(); }
2994 Metadata
*getRawVariable() const { return getOperand(0); }
2996 DIGlobalVariable
*getVariable() const {
2997 return cast_or_null
<DIGlobalVariable
>(getRawVariable());
3000 Metadata
*getRawExpression() const { return getOperand(1); }
3002 DIExpression
*getExpression() const {
3003 return cast
<DIExpression
>(getRawExpression());
3006 static bool classof(const Metadata
*MD
) {
3007 return MD
->getMetadataID() == DIGlobalVariableExpressionKind
;
3011 /// Macro Info DWARF-like metadata node.
3013 /// A metadata node with a DWARF macro info (i.e., a constant named
3014 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
3016 /// because it's potentially used for non-DWARF output.
3017 class DIMacroNode
: public MDNode
{
3018 friend class LLVMContextImpl
;
3019 friend class MDNode
;
3022 DIMacroNode(LLVMContext
&C
, unsigned ID
, StorageType Storage
, unsigned MIType
,
3023 ArrayRef
<Metadata
*> Ops1
, ArrayRef
<Metadata
*> Ops2
= None
)
3024 : MDNode(C
, ID
, Storage
, Ops1
, Ops2
) {
3025 assert(MIType
< 1u << 16);
3026 SubclassData16
= MIType
;
3028 ~DIMacroNode() = default;
3030 template <class Ty
> Ty
*getOperandAs(unsigned I
) const {
3031 return cast_or_null
<Ty
>(getOperand(I
));
3034 StringRef
getStringOperand(unsigned I
) const {
3035 if (auto *S
= getOperandAs
<MDString
>(I
))
3036 return S
->getString();
3040 static MDString
*getCanonicalMDString(LLVMContext
&Context
, StringRef S
) {
3043 return MDString::get(Context
, S
);
3047 unsigned getMacinfoType() const { return SubclassData16
; }
3049 static bool classof(const Metadata
*MD
) {
3050 switch (MD
->getMetadataID()) {
3054 case DIMacroFileKind
:
3060 class DIMacro
: public DIMacroNode
{
3061 friend class LLVMContextImpl
;
3062 friend class MDNode
;
3066 DIMacro(LLVMContext
&C
, StorageType Storage
, unsigned MIType
, unsigned Line
,
3067 ArrayRef
<Metadata
*> Ops
)
3068 : DIMacroNode(C
, DIMacroKind
, Storage
, MIType
, Ops
), Line(Line
) {}
3069 ~DIMacro() = default;
3071 static DIMacro
*getImpl(LLVMContext
&Context
, unsigned MIType
, unsigned Line
,
3072 StringRef Name
, StringRef Value
, StorageType Storage
,
3073 bool ShouldCreate
= true) {
3074 return getImpl(Context
, MIType
, Line
, getCanonicalMDString(Context
, Name
),
3075 getCanonicalMDString(Context
, Value
), Storage
, ShouldCreate
);
3077 static DIMacro
*getImpl(LLVMContext
&Context
, unsigned MIType
, unsigned Line
,
3078 MDString
*Name
, MDString
*Value
, StorageType Storage
,
3079 bool ShouldCreate
= true);
3081 TempDIMacro
cloneImpl() const {
3082 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
3087 DEFINE_MDNODE_GET(DIMacro
, (unsigned MIType
, unsigned Line
, StringRef Name
,
3088 StringRef Value
= ""),
3089 (MIType
, Line
, Name
, Value
))
3090 DEFINE_MDNODE_GET(DIMacro
, (unsigned MIType
, unsigned Line
, MDString
*Name
,
3092 (MIType
, Line
, Name
, Value
))
3094 TempDIMacro
clone() const { return cloneImpl(); }
3096 unsigned getLine() const { return Line
; }
3098 StringRef
getName() const { return getStringOperand(0); }
3099 StringRef
getValue() const { return getStringOperand(1); }
3101 MDString
*getRawName() const { return getOperandAs
<MDString
>(0); }
3102 MDString
*getRawValue() const { return getOperandAs
<MDString
>(1); }
3104 static bool classof(const Metadata
*MD
) {
3105 return MD
->getMetadataID() == DIMacroKind
;
3109 class DIMacroFile
: public DIMacroNode
{
3110 friend class LLVMContextImpl
;
3111 friend class MDNode
;
3115 DIMacroFile(LLVMContext
&C
, StorageType Storage
, unsigned MIType
,
3116 unsigned Line
, ArrayRef
<Metadata
*> Ops
)
3117 : DIMacroNode(C
, DIMacroFileKind
, Storage
, MIType
, Ops
), Line(Line
) {}
3118 ~DIMacroFile() = default;
3120 static DIMacroFile
*getImpl(LLVMContext
&Context
, unsigned MIType
,
3121 unsigned Line
, DIFile
*File
,
3122 DIMacroNodeArray Elements
, StorageType Storage
,
3123 bool ShouldCreate
= true) {
3124 return getImpl(Context
, MIType
, Line
, static_cast<Metadata
*>(File
),
3125 Elements
.get(), Storage
, ShouldCreate
);
3128 static DIMacroFile
*getImpl(LLVMContext
&Context
, unsigned MIType
,
3129 unsigned Line
, Metadata
*File
, Metadata
*Elements
,
3130 StorageType Storage
, bool ShouldCreate
= true);
3132 TempDIMacroFile
cloneImpl() const {
3133 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
3138 DEFINE_MDNODE_GET(DIMacroFile
, (unsigned MIType
, unsigned Line
, DIFile
*File
,
3139 DIMacroNodeArray Elements
),
3140 (MIType
, Line
, File
, Elements
))
3141 DEFINE_MDNODE_GET(DIMacroFile
, (unsigned MIType
, unsigned Line
,
3142 Metadata
*File
, Metadata
*Elements
),
3143 (MIType
, Line
, File
, Elements
))
3145 TempDIMacroFile
clone() const { return cloneImpl(); }
3147 void replaceElements(DIMacroNodeArray Elements
) {
3149 for (DIMacroNode
*Op
: getElements())
3150 assert(is_contained(Elements
->operands(), Op
) &&
3151 "Lost a macro node during macro node list replacement");
3153 replaceOperandWith(1, Elements
.get());
3156 unsigned getLine() const { return Line
; }
3157 DIFile
*getFile() const { return cast_or_null
<DIFile
>(getRawFile()); }
3159 DIMacroNodeArray
getElements() const {
3160 return cast_or_null
<MDTuple
>(getRawElements());
3163 Metadata
*getRawFile() const { return getOperand(0); }
3164 Metadata
*getRawElements() const { return getOperand(1); }
3166 static bool classof(const Metadata
*MD
) {
3167 return MD
->getMetadataID() == DIMacroFileKind
;
3171 } // end namespace llvm
3173 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
3174 #undef DEFINE_MDNODE_GET_UNPACK
3175 #undef DEFINE_MDNODE_GET
3177 #endif // LLVM_IR_DEBUGINFOMETADATA_H