[SimplifyCFG] FoldTwoEntryPHINode(): consider *total* speculation cost, not per-BB...
[llvm-complete.git] / include / llvm / IR / DebugInfoMetadata.h
blob1f0533d98f63adad7b05666a5f924e2234b3e1c0
1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
29 #include <cassert>
30 #include <climits>
31 #include <cstddef>
32 #include <cstdint>
33 #include <iterator>
34 #include <type_traits>
35 #include <vector>
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); \
44 } \
45 static Temp##CLASS getTemporary(LLVMContext &Context, \
46 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
47 return Temp##CLASS( \
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); \
53 } \
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); \
58 } \
59 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
61 namespace llvm {
63 class DITypeRefArray {
64 const MDTuple *N = nullptr;
66 public:
67 DITypeRefArray() = default;
68 DITypeRefArray(const MDTuple *N) : N(N) {}
70 explicit operator bool() const { return get(); }
71 explicit operator MDTuple *() const { return get(); }
73 MDTuple *get() const { return const_cast<MDTuple *>(N); }
74 MDTuple *operator->() const { return get(); }
75 MDTuple &operator*() const { return *get(); }
77 // FIXME: Fix callers and remove condition on N.
78 unsigned size() const { return N ? N->getNumOperands() : 0u; }
79 DIType *operator[](unsigned I) const {
80 return cast_or_null<DIType>(N->getOperand(I));
83 class iterator : std::iterator<std::input_iterator_tag, DIType *,
84 std::ptrdiff_t, void, DIType *> {
85 MDNode::op_iterator I = nullptr;
87 public:
88 iterator() = default;
89 explicit iterator(MDNode::op_iterator I) : I(I) {}
91 DIType *operator*() const { return cast_or_null<DIType>(*I); }
93 iterator &operator++() {
94 ++I;
95 return *this;
98 iterator operator++(int) {
99 iterator Temp(*this);
100 ++I;
101 return Temp;
104 bool operator==(const iterator &X) const { return I == X.I; }
105 bool operator!=(const iterator &X) const { return I != X.I; }
108 // FIXME: Fix callers and remove condition on N.
109 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
110 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
113 /// Tagged DWARF-like metadata node.
115 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
116 /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
117 /// potentially used for non-DWARF output.
118 class DINode : public MDNode {
119 friend class LLVMContextImpl;
120 friend class MDNode;
122 protected:
123 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
124 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
125 : MDNode(C, ID, Storage, Ops1, Ops2) {
126 assert(Tag < 1u << 16);
127 SubclassData16 = Tag;
129 ~DINode() = default;
131 template <class Ty> Ty *getOperandAs(unsigned I) const {
132 return cast_or_null<Ty>(getOperand(I));
135 StringRef getStringOperand(unsigned I) const {
136 if (auto *S = getOperandAs<MDString>(I))
137 return S->getString();
138 return StringRef();
141 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
142 if (S.empty())
143 return nullptr;
144 return MDString::get(Context, S);
147 /// Allow subclasses to mutate the tag.
148 void setTag(unsigned Tag) { SubclassData16 = Tag; }
150 public:
151 unsigned getTag() const { return SubclassData16; }
153 /// Debug info flags.
155 /// The three accessibility flags are mutually exclusive and rolled together
156 /// in the first two bits.
157 enum DIFlags : uint32_t {
158 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
159 #define DI_FLAG_LARGEST_NEEDED
160 #include "llvm/IR/DebugInfoFlags.def"
161 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
162 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
163 FlagVirtualInheritance,
164 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
167 static DIFlags getFlag(StringRef Flag);
168 static StringRef getFlagString(DIFlags Flag);
170 /// Split up a flags bitfield.
172 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
173 /// any remaining (unrecognized) bits.
174 static DIFlags splitFlags(DIFlags Flags,
175 SmallVectorImpl<DIFlags> &SplitFlags);
177 static bool classof(const Metadata *MD) {
178 switch (MD->getMetadataID()) {
179 default:
180 return false;
181 case GenericDINodeKind:
182 case DISubrangeKind:
183 case DIEnumeratorKind:
184 case DIBasicTypeKind:
185 case DIDerivedTypeKind:
186 case DICompositeTypeKind:
187 case DISubroutineTypeKind:
188 case DIFileKind:
189 case DICompileUnitKind:
190 case DISubprogramKind:
191 case DILexicalBlockKind:
192 case DILexicalBlockFileKind:
193 case DINamespaceKind:
194 case DICommonBlockKind:
195 case DITemplateTypeParameterKind:
196 case DITemplateValueParameterKind:
197 case DIGlobalVariableKind:
198 case DILocalVariableKind:
199 case DILabelKind:
200 case DIObjCPropertyKind:
201 case DIImportedEntityKind:
202 case DIModuleKind:
203 return true;
208 /// Generic tagged DWARF-like metadata node.
210 /// An un-specialized DWARF-like metadata node. The first operand is a
211 /// (possibly empty) null-separated \a MDString header that contains arbitrary
212 /// fields. The remaining operands are \a dwarf_operands(), and are pointers
213 /// to other metadata.
214 class GenericDINode : public DINode {
215 friend class LLVMContextImpl;
216 friend class MDNode;
218 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
219 unsigned Tag, ArrayRef<Metadata *> Ops1,
220 ArrayRef<Metadata *> Ops2)
221 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
222 setHash(Hash);
224 ~GenericDINode() { dropAllReferences(); }
226 void setHash(unsigned Hash) { SubclassData32 = Hash; }
227 void recalculateHash();
229 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
230 StringRef Header, ArrayRef<Metadata *> DwarfOps,
231 StorageType Storage, bool ShouldCreate = true) {
232 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
233 DwarfOps, Storage, ShouldCreate);
236 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
237 MDString *Header, ArrayRef<Metadata *> DwarfOps,
238 StorageType Storage, bool ShouldCreate = true);
240 TempGenericDINode cloneImpl() const {
241 return getTemporary(
242 getContext(), getTag(), getHeader(),
243 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
246 public:
247 unsigned getHash() const { return SubclassData32; }
249 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
250 ArrayRef<Metadata *> DwarfOps),
251 (Tag, Header, DwarfOps))
252 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
253 ArrayRef<Metadata *> DwarfOps),
254 (Tag, Header, DwarfOps))
256 /// Return a (temporary) clone of this.
257 TempGenericDINode clone() const { return cloneImpl(); }
259 unsigned getTag() const { return SubclassData16; }
260 StringRef getHeader() const { return getStringOperand(0); }
261 MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
263 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
264 op_iterator dwarf_op_end() const { return op_end(); }
265 op_range dwarf_operands() const {
266 return op_range(dwarf_op_begin(), dwarf_op_end());
269 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
270 const MDOperand &getDwarfOperand(unsigned I) const {
271 return getOperand(I + 1);
273 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
274 replaceOperandWith(I + 1, New);
277 static bool classof(const Metadata *MD) {
278 return MD->getMetadataID() == GenericDINodeKind;
282 /// Array subrange.
284 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
285 /// type.
286 class DISubrange : public DINode {
287 friend class LLVMContextImpl;
288 friend class MDNode;
290 int64_t LowerBound;
292 DISubrange(LLVMContext &C, StorageType Storage, Metadata *Node,
293 int64_t LowerBound, ArrayRef<Metadata *> Ops)
294 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops),
295 LowerBound(LowerBound) {}
297 ~DISubrange() = default;
299 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
300 int64_t LowerBound, StorageType Storage,
301 bool ShouldCreate = true);
303 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
304 int64_t LowerBound, StorageType Storage,
305 bool ShouldCreate = true);
307 TempDISubrange cloneImpl() const {
308 return getTemporary(getContext(), getRawCountNode(), getLowerBound());
311 public:
312 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
313 (Count, LowerBound))
315 DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
316 (CountNode, LowerBound))
318 TempDISubrange clone() const { return cloneImpl(); }
320 int64_t getLowerBound() const { return LowerBound; }
322 Metadata *getRawCountNode() const {
323 return getOperand(0).get();
326 typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
328 CountType getCount() const {
329 if (auto *MD = dyn_cast<ConstantAsMetadata>(getRawCountNode()))
330 return CountType(cast<ConstantInt>(MD->getValue()));
332 if (auto *DV = dyn_cast<DIVariable>(getRawCountNode()))
333 return CountType(DV);
335 return CountType();
338 static bool classof(const Metadata *MD) {
339 return MD->getMetadataID() == DISubrangeKind;
343 /// Enumeration value.
345 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
346 /// longer creates a type cycle.
347 class DIEnumerator : public DINode {
348 friend class LLVMContextImpl;
349 friend class MDNode;
351 int64_t Value;
352 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
353 bool IsUnsigned, ArrayRef<Metadata *> Ops)
354 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
355 Value(Value) {
356 SubclassData32 = IsUnsigned;
358 ~DIEnumerator() = default;
360 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
361 bool IsUnsigned, StringRef Name,
362 StorageType Storage, bool ShouldCreate = true) {
363 return getImpl(Context, Value, IsUnsigned,
364 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
366 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
367 bool IsUnsigned, MDString *Name,
368 StorageType Storage, bool ShouldCreate = true);
370 TempDIEnumerator cloneImpl() const {
371 return getTemporary(getContext(), getValue(), isUnsigned(), getName());
374 public:
375 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name),
376 (Value, IsUnsigned, Name))
377 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, MDString *Name),
378 (Value, IsUnsigned, Name))
380 TempDIEnumerator clone() const { return cloneImpl(); }
382 int64_t getValue() const { return Value; }
383 bool isUnsigned() const { return SubclassData32; }
384 StringRef getName() const { return getStringOperand(0); }
386 MDString *getRawName() const { return getOperandAs<MDString>(0); }
388 static bool classof(const Metadata *MD) {
389 return MD->getMetadataID() == DIEnumeratorKind;
393 /// Base class for scope-like contexts.
395 /// Base class for lexical scopes and types (which are also declaration
396 /// contexts).
398 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
399 class DIScope : public DINode {
400 protected:
401 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
402 ArrayRef<Metadata *> Ops)
403 : DINode(C, ID, Storage, Tag, Ops) {}
404 ~DIScope() = default;
406 public:
407 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
409 inline StringRef getFilename() const;
410 inline StringRef getDirectory() const;
411 inline Optional<StringRef> getSource() const;
413 StringRef getName() const;
414 DIScope *getScope() const;
416 /// Return the raw underlying file.
418 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
419 /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
420 /// Otherwise, return the first operand, which is where all other subclasses
421 /// store their file pointer.
422 Metadata *getRawFile() const {
423 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
424 : static_cast<Metadata *>(getOperand(0));
427 static bool classof(const Metadata *MD) {
428 switch (MD->getMetadataID()) {
429 default:
430 return false;
431 case DIBasicTypeKind:
432 case DIDerivedTypeKind:
433 case DICompositeTypeKind:
434 case DISubroutineTypeKind:
435 case DIFileKind:
436 case DICompileUnitKind:
437 case DISubprogramKind:
438 case DILexicalBlockKind:
439 case DILexicalBlockFileKind:
440 case DINamespaceKind:
441 case DICommonBlockKind:
442 case DIModuleKind:
443 return true;
448 /// File.
450 /// TODO: Merge with directory/file node (including users).
451 /// TODO: Canonicalize paths on creation.
452 class DIFile : public DIScope {
453 friend class LLVMContextImpl;
454 friend class MDNode;
456 public:
457 /// Which algorithm (e.g. MD5) a checksum was generated with.
459 /// The encoding is explicit because it is used directly in Bitcode. The
460 /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
461 enum ChecksumKind {
462 // The first variant was originally CSK_None, encoded as 0. The new
463 // internal representation removes the need for this by wrapping the
464 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
465 // encoding is reserved.
466 CSK_MD5 = 1,
467 CSK_SHA1 = 2,
468 CSK_Last = CSK_SHA1 // Should be last enumeration.
471 /// A single checksum, represented by a \a Kind and a \a Value (a string).
472 template <typename T>
473 struct ChecksumInfo {
474 /// The kind of checksum which \a Value encodes.
475 ChecksumKind Kind;
476 /// The string value of the checksum.
477 T Value;
479 ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
480 ~ChecksumInfo() = default;
481 bool operator==(const ChecksumInfo<T> &X) const {
482 return Kind == X.Kind && Value == X.Value;
484 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
485 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
488 private:
489 Optional<ChecksumInfo<MDString *>> Checksum;
490 Optional<MDString *> Source;
492 DIFile(LLVMContext &C, StorageType Storage,
493 Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src,
494 ArrayRef<Metadata *> Ops)
495 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
496 Checksum(CS), Source(Src) {}
497 ~DIFile() = default;
499 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
500 StringRef Directory,
501 Optional<ChecksumInfo<StringRef>> CS,
502 Optional<StringRef> Source,
503 StorageType Storage, bool ShouldCreate = true) {
504 Optional<ChecksumInfo<MDString *>> MDChecksum;
505 if (CS)
506 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
507 return getImpl(Context, getCanonicalMDString(Context, Filename),
508 getCanonicalMDString(Context, Directory), MDChecksum,
509 Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) : None,
510 Storage, ShouldCreate);
512 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
513 MDString *Directory,
514 Optional<ChecksumInfo<MDString *>> CS,
515 Optional<MDString *> Source, StorageType Storage,
516 bool ShouldCreate = true);
518 TempDIFile cloneImpl() const {
519 return getTemporary(getContext(), getFilename(), getDirectory(),
520 getChecksum(), getSource());
523 public:
524 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
525 Optional<ChecksumInfo<StringRef>> CS = None,
526 Optional<StringRef> Source = None),
527 (Filename, Directory, CS, Source))
528 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
529 Optional<ChecksumInfo<MDString *>> CS = None,
530 Optional<MDString *> Source = None),
531 (Filename, Directory, CS, Source))
533 TempDIFile clone() const { return cloneImpl(); }
535 StringRef getFilename() const { return getStringOperand(0); }
536 StringRef getDirectory() const { return getStringOperand(1); }
537 Optional<ChecksumInfo<StringRef>> getChecksum() const {
538 Optional<ChecksumInfo<StringRef>> StringRefChecksum;
539 if (Checksum)
540 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
541 return StringRefChecksum;
543 Optional<StringRef> getSource() const {
544 return Source ? Optional<StringRef>((*Source)->getString()) : None;
547 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
548 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
549 Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }
550 Optional<MDString *> getRawSource() const { return Source; }
552 static StringRef getChecksumKindAsString(ChecksumKind CSKind);
553 static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
555 static bool classof(const Metadata *MD) {
556 return MD->getMetadataID() == DIFileKind;
560 StringRef DIScope::getFilename() const {
561 if (auto *F = getFile())
562 return F->getFilename();
563 return "";
566 StringRef DIScope::getDirectory() const {
567 if (auto *F = getFile())
568 return F->getDirectory();
569 return "";
572 Optional<StringRef> DIScope::getSource() const {
573 if (auto *F = getFile())
574 return F->getSource();
575 return None;
578 /// Base class for types.
580 /// TODO: Remove the hardcoded name and context, since many types don't use
581 /// them.
582 /// TODO: Split up flags.
583 class DIType : public DIScope {
584 unsigned Line;
585 DIFlags Flags;
586 uint64_t SizeInBits;
587 uint64_t OffsetInBits;
588 uint32_t AlignInBits;
590 protected:
591 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
592 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
593 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
594 : DIScope(C, ID, Storage, Tag, Ops) {
595 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
597 ~DIType() = default;
599 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
600 uint64_t OffsetInBits, DIFlags Flags) {
601 this->Line = Line;
602 this->Flags = Flags;
603 this->SizeInBits = SizeInBits;
604 this->AlignInBits = AlignInBits;
605 this->OffsetInBits = OffsetInBits;
608 /// Change fields in place.
609 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
610 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
611 assert(isDistinct() && "Only distinct nodes can mutate");
612 setTag(Tag);
613 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
616 public:
617 TempDIType clone() const {
618 return TempDIType(cast<DIType>(MDNode::clone().release()));
621 unsigned getLine() const { return Line; }
622 uint64_t getSizeInBits() const { return SizeInBits; }
623 uint32_t getAlignInBits() const { return AlignInBits; }
624 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
625 uint64_t getOffsetInBits() const { return OffsetInBits; }
626 DIFlags getFlags() const { return Flags; }
628 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
629 StringRef getName() const { return getStringOperand(2); }
632 Metadata *getRawScope() const { return getOperand(1); }
633 MDString *getRawName() const { return getOperandAs<MDString>(2); }
635 /// Returns a new temporary DIType with updated Flags
636 TempDIType cloneWithFlags(DIFlags NewFlags) const {
637 auto NewTy = clone();
638 NewTy->Flags = NewFlags;
639 return NewTy;
642 bool isPrivate() const {
643 return (getFlags() & FlagAccessibility) == FlagPrivate;
645 bool isProtected() const {
646 return (getFlags() & FlagAccessibility) == FlagProtected;
648 bool isPublic() const {
649 return (getFlags() & FlagAccessibility) == FlagPublic;
651 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
652 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
653 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
654 bool isVirtual() const { return getFlags() & FlagVirtual; }
655 bool isArtificial() const { return getFlags() & FlagArtificial; }
656 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
657 bool isObjcClassComplete() const {
658 return getFlags() & FlagObjcClassComplete;
660 bool isVector() const { return getFlags() & FlagVector; }
661 bool isBitField() const { return getFlags() & FlagBitField; }
662 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
663 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
664 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
665 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
666 bool isTypePassByReference() const {
667 return getFlags() & FlagTypePassByReference;
669 bool isBigEndian() const { return getFlags() & FlagBigEndian; }
670 bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
671 bool getExportSymbols() const { return getFlags() & FlagExportSymbols; }
673 static bool classof(const Metadata *MD) {
674 switch (MD->getMetadataID()) {
675 default:
676 return false;
677 case DIBasicTypeKind:
678 case DIDerivedTypeKind:
679 case DICompositeTypeKind:
680 case DISubroutineTypeKind:
681 return true;
686 /// Basic type, like 'int' or 'float'.
688 /// TODO: Split out DW_TAG_unspecified_type.
689 /// TODO: Drop unused accessors.
690 class DIBasicType : public DIType {
691 friend class LLVMContextImpl;
692 friend class MDNode;
694 unsigned Encoding;
696 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
697 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
698 DIFlags Flags, ArrayRef<Metadata *> Ops)
699 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
700 Flags, Ops),
701 Encoding(Encoding) {}
702 ~DIBasicType() = default;
704 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
705 StringRef Name, uint64_t SizeInBits,
706 uint32_t AlignInBits, unsigned Encoding,
707 DIFlags Flags, StorageType Storage,
708 bool ShouldCreate = true) {
709 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
710 SizeInBits, AlignInBits, Encoding, Flags, Storage,
711 ShouldCreate);
713 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
714 MDString *Name, uint64_t SizeInBits,
715 uint32_t AlignInBits, unsigned Encoding,
716 DIFlags Flags, StorageType Storage,
717 bool ShouldCreate = true);
719 TempDIBasicType cloneImpl() const {
720 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
721 getAlignInBits(), getEncoding(), getFlags());
724 public:
725 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
726 (Tag, Name, 0, 0, 0, FlagZero))
727 DEFINE_MDNODE_GET(DIBasicType,
728 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
729 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
730 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
731 DEFINE_MDNODE_GET(DIBasicType,
732 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
733 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
734 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
736 TempDIBasicType clone() const { return cloneImpl(); }
738 unsigned getEncoding() const { return Encoding; }
740 enum class Signedness { Signed, Unsigned };
742 /// Return the signedness of this type, or None if this type is neither
743 /// signed nor unsigned.
744 Optional<Signedness> getSignedness() const;
746 static bool classof(const Metadata *MD) {
747 return MD->getMetadataID() == DIBasicTypeKind;
751 /// Derived types.
753 /// This includes qualified types, pointers, references, friends, typedefs, and
754 /// class members.
756 /// TODO: Split out members (inheritance, fields, methods, etc.).
757 class DIDerivedType : public DIType {
758 friend class LLVMContextImpl;
759 friend class MDNode;
761 /// The DWARF address space of the memory pointed to or referenced by a
762 /// pointer or reference type respectively.
763 Optional<unsigned> DWARFAddressSpace;
765 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
766 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
767 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
768 DIFlags Flags, ArrayRef<Metadata *> Ops)
769 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
770 AlignInBits, OffsetInBits, Flags, Ops),
771 DWARFAddressSpace(DWARFAddressSpace) {}
772 ~DIDerivedType() = default;
774 static DIDerivedType *
775 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
776 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
777 uint32_t AlignInBits, uint64_t OffsetInBits,
778 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
779 Metadata *ExtraData, StorageType Storage, bool ShouldCreate = true) {
780 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
781 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
782 DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
784 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
785 MDString *Name, Metadata *File, unsigned Line,
786 Metadata *Scope, Metadata *BaseType,
787 uint64_t SizeInBits, uint32_t AlignInBits,
788 uint64_t OffsetInBits,
789 Optional<unsigned> DWARFAddressSpace,
790 DIFlags Flags, Metadata *ExtraData,
791 StorageType Storage, bool ShouldCreate = true);
793 TempDIDerivedType cloneImpl() const {
794 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
795 getScope(), getBaseType(), getSizeInBits(),
796 getAlignInBits(), getOffsetInBits(),
797 getDWARFAddressSpace(), getFlags(), getExtraData());
800 public:
801 DEFINE_MDNODE_GET(DIDerivedType,
802 (unsigned Tag, MDString *Name, Metadata *File,
803 unsigned Line, Metadata *Scope, Metadata *BaseType,
804 uint64_t SizeInBits, uint32_t AlignInBits,
805 uint64_t OffsetInBits,
806 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
807 Metadata *ExtraData = nullptr),
808 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
809 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
810 ExtraData))
811 DEFINE_MDNODE_GET(DIDerivedType,
812 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
813 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
814 uint32_t AlignInBits, uint64_t OffsetInBits,
815 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
816 Metadata *ExtraData = nullptr),
817 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
818 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
819 ExtraData))
821 TempDIDerivedType clone() const { return cloneImpl(); }
823 /// Get the base type this is derived from.
824 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
825 Metadata *getRawBaseType() const { return getOperand(3); }
827 /// \returns The DWARF address space of the memory pointed to or referenced by
828 /// a pointer or reference type respectively.
829 Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
831 /// Get extra data associated with this derived type.
833 /// Class type for pointer-to-members, objective-c property node for ivars,
834 /// global constant wrapper for static members, or virtual base pointer offset
835 /// for inheritance.
837 /// TODO: Separate out types that need this extra operand: pointer-to-member
838 /// types and member fields (static members and ivars).
839 Metadata *getExtraData() const { return getRawExtraData(); }
840 Metadata *getRawExtraData() const { return getOperand(4); }
842 /// Get casted version of extra data.
843 /// @{
844 DIType *getClassType() const {
845 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
846 return cast_or_null<DIType>(getExtraData());
849 DIObjCProperty *getObjCProperty() const {
850 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
853 uint32_t getVBPtrOffset() const {
854 assert(getTag() == dwarf::DW_TAG_inheritance);
855 if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData()))
856 if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue()))
857 return static_cast<uint32_t>(CI->getZExtValue());
858 return 0;
861 Constant *getStorageOffsetInBits() const {
862 assert(getTag() == dwarf::DW_TAG_member && isBitField());
863 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
864 return C->getValue();
865 return nullptr;
868 Constant *getConstant() const {
869 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
870 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
871 return C->getValue();
872 return nullptr;
874 Constant *getDiscriminantValue() const {
875 assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
876 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
877 return C->getValue();
878 return nullptr;
880 /// @}
882 static bool classof(const Metadata *MD) {
883 return MD->getMetadataID() == DIDerivedTypeKind;
887 /// Composite types.
889 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
890 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
891 class DICompositeType : public DIType {
892 friend class LLVMContextImpl;
893 friend class MDNode;
895 unsigned RuntimeLang;
897 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
898 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
899 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
900 ArrayRef<Metadata *> Ops)
901 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
902 AlignInBits, OffsetInBits, Flags, Ops),
903 RuntimeLang(RuntimeLang) {}
904 ~DICompositeType() = default;
906 /// Change fields in place.
907 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
908 uint64_t SizeInBits, uint32_t AlignInBits,
909 uint64_t OffsetInBits, DIFlags Flags) {
910 assert(isDistinct() && "Only distinct nodes can mutate");
911 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
912 this->RuntimeLang = RuntimeLang;
913 DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
916 static DICompositeType *
917 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
918 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
919 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
920 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
921 DITemplateParameterArray TemplateParams, StringRef Identifier,
922 DIDerivedType *Discriminator, StorageType Storage,
923 bool ShouldCreate = true) {
924 return getImpl(
925 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
926 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
927 RuntimeLang, VTableHolder, TemplateParams.get(),
928 getCanonicalMDString(Context, Identifier), Discriminator, Storage, ShouldCreate);
930 static DICompositeType *
931 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
932 unsigned Line, Metadata *Scope, Metadata *BaseType,
933 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
934 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
935 Metadata *VTableHolder, Metadata *TemplateParams,
936 MDString *Identifier, Metadata *Discriminator,
937 StorageType Storage, bool ShouldCreate = true);
939 TempDICompositeType cloneImpl() const {
940 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
941 getScope(), getBaseType(), getSizeInBits(),
942 getAlignInBits(), getOffsetInBits(), getFlags(),
943 getElements(), getRuntimeLang(), getVTableHolder(),
944 getTemplateParams(), getIdentifier(), getDiscriminator());
947 public:
948 DEFINE_MDNODE_GET(DICompositeType,
949 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
950 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
951 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
952 DINodeArray Elements, unsigned RuntimeLang,
953 DIType *VTableHolder,
954 DITemplateParameterArray TemplateParams = nullptr,
955 StringRef Identifier = "",
956 DIDerivedType *Discriminator = nullptr),
957 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
958 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
959 VTableHolder, TemplateParams, Identifier, Discriminator))
960 DEFINE_MDNODE_GET(DICompositeType,
961 (unsigned Tag, MDString *Name, Metadata *File,
962 unsigned Line, Metadata *Scope, Metadata *BaseType,
963 uint64_t SizeInBits, uint32_t AlignInBits,
964 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
965 unsigned RuntimeLang, Metadata *VTableHolder,
966 Metadata *TemplateParams = nullptr,
967 MDString *Identifier = nullptr,
968 Metadata *Discriminator = nullptr),
969 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
970 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
971 VTableHolder, TemplateParams, Identifier, Discriminator))
973 TempDICompositeType clone() const { return cloneImpl(); }
975 /// Get a DICompositeType with the given ODR identifier.
977 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
978 /// DICompositeType for the given ODR \c Identifier. If none exists, creates
979 /// a new node.
981 /// Else, returns \c nullptr.
982 static DICompositeType *
983 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
984 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
985 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
986 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
987 unsigned RuntimeLang, Metadata *VTableHolder,
988 Metadata *TemplateParams, Metadata *Discriminator);
989 static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
990 MDString &Identifier);
992 /// Build a DICompositeType with the given ODR identifier.
994 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
995 /// it doesn't exist, creates a new one. If it does exist and \a
996 /// isForwardDecl(), and the new arguments would be a definition, mutates the
997 /// the type in place. In either case, returns the type.
999 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
1000 /// nullptr.
1001 static DICompositeType *
1002 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1003 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1004 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1005 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
1006 unsigned RuntimeLang, Metadata *VTableHolder,
1007 Metadata *TemplateParams, Metadata *Discriminator);
1009 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1010 DINodeArray getElements() const {
1011 return cast_or_null<MDTuple>(getRawElements());
1013 DIType *getVTableHolder() const {
1014 return cast_or_null<DIType>(getRawVTableHolder());
1016 DITemplateParameterArray getTemplateParams() const {
1017 return cast_or_null<MDTuple>(getRawTemplateParams());
1019 StringRef getIdentifier() const { return getStringOperand(7); }
1020 unsigned getRuntimeLang() const { return RuntimeLang; }
1022 Metadata *getRawBaseType() const { return getOperand(3); }
1023 Metadata *getRawElements() const { return getOperand(4); }
1024 Metadata *getRawVTableHolder() const { return getOperand(5); }
1025 Metadata *getRawTemplateParams() const { return getOperand(6); }
1026 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1027 Metadata *getRawDiscriminator() const { return getOperand(8); }
1028 DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
1030 /// Replace operands.
1032 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
1033 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
1034 /// of its movement if necessary.
1035 /// @{
1036 void replaceElements(DINodeArray Elements) {
1037 #ifndef NDEBUG
1038 for (DINode *Op : getElements())
1039 assert(is_contained(Elements->operands(), Op) &&
1040 "Lost a member during member list replacement");
1041 #endif
1042 replaceOperandWith(4, Elements.get());
1045 void replaceVTableHolder(DIType *VTableHolder) {
1046 replaceOperandWith(5, VTableHolder);
1049 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
1050 replaceOperandWith(6, TemplateParams.get());
1052 /// @}
1054 static bool classof(const Metadata *MD) {
1055 return MD->getMetadataID() == DICompositeTypeKind;
1059 /// Type array for a subprogram.
1061 /// TODO: Fold the array of types in directly as operands.
1062 class DISubroutineType : public DIType {
1063 friend class LLVMContextImpl;
1064 friend class MDNode;
1066 /// The calling convention used with DW_AT_calling_convention. Actually of
1067 /// type dwarf::CallingConvention.
1068 uint8_t CC;
1070 DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
1071 uint8_t CC, ArrayRef<Metadata *> Ops)
1072 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
1073 0, 0, 0, 0, Flags, Ops),
1074 CC(CC) {}
1075 ~DISubroutineType() = default;
1077 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1078 uint8_t CC, DITypeRefArray TypeArray,
1079 StorageType Storage,
1080 bool ShouldCreate = true) {
1081 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
1083 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1084 uint8_t CC, Metadata *TypeArray,
1085 StorageType Storage,
1086 bool ShouldCreate = true);
1088 TempDISubroutineType cloneImpl() const {
1089 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
1092 public:
1093 DEFINE_MDNODE_GET(DISubroutineType,
1094 (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
1095 (Flags, CC, TypeArray))
1096 DEFINE_MDNODE_GET(DISubroutineType,
1097 (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
1098 (Flags, CC, TypeArray))
1100 TempDISubroutineType clone() const { return cloneImpl(); }
1102 uint8_t getCC() const { return CC; }
1104 DITypeRefArray getTypeArray() const {
1105 return cast_or_null<MDTuple>(getRawTypeArray());
1108 Metadata *getRawTypeArray() const { return getOperand(3); }
1110 static bool classof(const Metadata *MD) {
1111 return MD->getMetadataID() == DISubroutineTypeKind;
1115 /// Compile unit.
1116 class DICompileUnit : public DIScope {
1117 friend class LLVMContextImpl;
1118 friend class MDNode;
1120 public:
1121 enum DebugEmissionKind : unsigned {
1122 NoDebug = 0,
1123 FullDebug,
1124 LineTablesOnly,
1125 DebugDirectivesOnly,
1126 LastEmissionKind = DebugDirectivesOnly
1129 enum class DebugNameTableKind : unsigned {
1130 Default = 0,
1131 GNU = 1,
1132 None = 2,
1133 LastDebugNameTableKind = None
1136 static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1137 static const char *emissionKindString(DebugEmissionKind EK);
1138 static Optional<DebugNameTableKind> getNameTableKind(StringRef Str);
1139 static const char *nameTableKindString(DebugNameTableKind PK);
1141 private:
1142 unsigned SourceLanguage;
1143 bool IsOptimized;
1144 unsigned RuntimeVersion;
1145 unsigned EmissionKind;
1146 uint64_t DWOId;
1147 bool SplitDebugInlining;
1148 bool DebugInfoForProfiling;
1149 unsigned NameTableKind;
1150 bool RangesBaseAddress;
1152 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
1153 bool IsOptimized, unsigned RuntimeVersion,
1154 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
1155 bool DebugInfoForProfiling, unsigned NameTableKind,
1156 bool RangesBaseAddress, ArrayRef<Metadata *> Ops)
1157 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
1158 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
1159 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
1160 DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
1161 DebugInfoForProfiling(DebugInfoForProfiling),
1162 NameTableKind(NameTableKind), RangesBaseAddress(RangesBaseAddress) {
1163 assert(Storage != Uniqued);
1165 ~DICompileUnit() = default;
1167 static DICompileUnit *
1168 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
1169 StringRef Producer, bool IsOptimized, StringRef Flags,
1170 unsigned RuntimeVersion, StringRef SplitDebugFilename,
1171 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
1172 DIScopeArray RetainedTypes,
1173 DIGlobalVariableExpressionArray GlobalVariables,
1174 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1175 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1176 unsigned NameTableKind, bool RangesBaseAddress, StorageType Storage,
1177 bool ShouldCreate = true) {
1178 return getImpl(Context, SourceLanguage, File,
1179 getCanonicalMDString(Context, Producer), IsOptimized,
1180 getCanonicalMDString(Context, Flags), RuntimeVersion,
1181 getCanonicalMDString(Context, SplitDebugFilename),
1182 EmissionKind, EnumTypes.get(), RetainedTypes.get(),
1183 GlobalVariables.get(), ImportedEntities.get(), Macros.get(),
1184 DWOId, SplitDebugInlining, DebugInfoForProfiling,
1185 NameTableKind, RangesBaseAddress, Storage, ShouldCreate);
1187 static DICompileUnit *
1188 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1189 MDString *Producer, bool IsOptimized, MDString *Flags,
1190 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1191 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1192 Metadata *GlobalVariables, Metadata *ImportedEntities,
1193 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
1194 bool DebugInfoForProfiling, unsigned NameTableKind,
1195 bool RangesBaseAddress, StorageType Storage, bool ShouldCreate = true);
1197 TempDICompileUnit cloneImpl() const {
1198 return getTemporary(
1199 getContext(), getSourceLanguage(), getFile(), getProducer(),
1200 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1201 getEmissionKind(), getEnumTypes(), getRetainedTypes(),
1202 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId,
1203 getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(),
1204 getRangesBaseAddress());
1207 public:
1208 static void get() = delete;
1209 static void getIfExists() = delete;
1211 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1212 DICompileUnit,
1213 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1214 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1215 StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
1216 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
1217 DIGlobalVariableExpressionArray GlobalVariables,
1218 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1219 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1220 DebugNameTableKind NameTableKind, bool RangesBaseAddress),
1221 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1222 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1223 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1224 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress))
1225 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1226 DICompileUnit,
1227 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1228 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1229 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1230 Metadata *RetainedTypes, Metadata *GlobalVariables,
1231 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
1232 bool SplitDebugInlining, bool DebugInfoForProfiling,
1233 unsigned NameTableKind, bool RangesBaseAddress),
1234 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1235 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1236 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1237 DebugInfoForProfiling, NameTableKind, RangesBaseAddress))
1239 TempDICompileUnit clone() const { return cloneImpl(); }
1241 unsigned getSourceLanguage() const { return SourceLanguage; }
1242 bool isOptimized() const { return IsOptimized; }
1243 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1244 DebugEmissionKind getEmissionKind() const {
1245 return (DebugEmissionKind)EmissionKind;
1247 bool isDebugDirectivesOnly() const {
1248 return EmissionKind == DebugDirectivesOnly;
1250 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
1251 DebugNameTableKind getNameTableKind() const {
1252 return (DebugNameTableKind)NameTableKind;
1254 bool getRangesBaseAddress() const {
1255 return RangesBaseAddress; }
1256 StringRef getProducer() const {
1257 return getStringOperand(1); }
1258 StringRef getFlags() const {
1259 return getStringOperand(2); }
1260 StringRef getSplitDebugFilename() const {
1261 return getStringOperand(3); }
1262 DICompositeTypeArray getEnumTypes() const {
1263 return cast_or_null<MDTuple>(getRawEnumTypes());
1265 DIScopeArray getRetainedTypes() const {
1266 return cast_or_null<MDTuple>(getRawRetainedTypes());
1268 DIGlobalVariableExpressionArray getGlobalVariables() const {
1269 return cast_or_null<MDTuple>(getRawGlobalVariables());
1271 DIImportedEntityArray getImportedEntities() const {
1272 return cast_or_null<MDTuple>(getRawImportedEntities());
1274 DIMacroNodeArray getMacros() const {
1275 return cast_or_null<MDTuple>(getRawMacros());
1277 uint64_t getDWOId() const { return DWOId; }
1278 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
1279 bool getSplitDebugInlining() const { return SplitDebugInlining; }
1280 void setSplitDebugInlining(bool SplitDebugInlining) {
1281 this->SplitDebugInlining = SplitDebugInlining;
1284 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1285 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1286 MDString *getRawSplitDebugFilename() const {
1287 return getOperandAs<MDString>(3);
1289 Metadata *getRawEnumTypes() const { return getOperand(4); }
1290 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1291 Metadata *getRawGlobalVariables() const { return getOperand(6); }
1292 Metadata *getRawImportedEntities() const { return getOperand(7); }
1293 Metadata *getRawMacros() const { return getOperand(8); }
1295 /// Replace arrays.
1297 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1298 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1299 /// DICompileUnit should be fairly rare.
1300 /// @{
1301 void replaceEnumTypes(DICompositeTypeArray N) {
1302 replaceOperandWith(4, N.get());
1304 void replaceRetainedTypes(DITypeArray N) {
1305 replaceOperandWith(5, N.get());
1307 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
1308 replaceOperandWith(6, N.get());
1310 void replaceImportedEntities(DIImportedEntityArray N) {
1311 replaceOperandWith(7, N.get());
1313 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
1314 /// @}
1316 static bool classof(const Metadata *MD) {
1317 return MD->getMetadataID() == DICompileUnitKind;
1321 /// A scope for locals.
1323 /// A legal scope for lexical blocks, local variables, and debug info
1324 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1325 /// DILexicalBlockFile.
1326 class DILocalScope : public DIScope {
1327 protected:
1328 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1329 ArrayRef<Metadata *> Ops)
1330 : DIScope(C, ID, Storage, Tag, Ops) {}
1331 ~DILocalScope() = default;
1333 public:
1334 /// Get the subprogram for this scope.
1336 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1337 /// chain.
1338 DISubprogram *getSubprogram() const;
1340 /// Get the first non DILexicalBlockFile scope of this scope.
1342 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1343 /// scope chain.
1344 DILocalScope *getNonLexicalBlockFileScope() const;
1346 static bool classof(const Metadata *MD) {
1347 return MD->getMetadataID() == DISubprogramKind ||
1348 MD->getMetadataID() == DILexicalBlockKind ||
1349 MD->getMetadataID() == DILexicalBlockFileKind;
1353 /// Debug location.
1355 /// A debug location in source code, used for debug info and otherwise.
1356 class DILocation : public MDNode {
1357 friend class LLVMContextImpl;
1358 friend class MDNode;
1360 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1361 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
1362 ~DILocation() { dropAllReferences(); }
1364 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1365 unsigned Column, Metadata *Scope,
1366 Metadata *InlinedAt, bool ImplicitCode,
1367 StorageType Storage, bool ShouldCreate = true);
1368 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1369 unsigned Column, DILocalScope *Scope,
1370 DILocation *InlinedAt, bool ImplicitCode,
1371 StorageType Storage, bool ShouldCreate = true) {
1372 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1373 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
1374 ShouldCreate);
1377 /// With a given unsigned int \p U, use up to 13 bits to represent it.
1378 /// old_bit 1~5 --> new_bit 1~5
1379 /// old_bit 6~12 --> new_bit 7~13
1380 /// new_bit_6 is 0 if higher bits (7~13) are all 0
1381 static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
1382 U &= 0xfff;
1383 return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
1386 /// Reverse transformation as getPrefixEncodingFromUnsigned.
1387 static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
1388 if (U & 1)
1389 return 0;
1390 U >>= 1;
1391 return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
1394 /// Returns the next component stored in discriminator.
1395 static unsigned getNextComponentInDiscriminator(unsigned D) {
1396 if ((D & 1) == 0)
1397 return D >> ((D & 0x40) ? 14 : 7);
1398 else
1399 return D >> 1;
1402 TempDILocation cloneImpl() const {
1403 // Get the raw scope/inlinedAt since it is possible to invoke this on
1404 // a DILocation containing temporary metadata.
1405 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1406 getRawInlinedAt(), isImplicitCode());
1409 static unsigned encodeComponent(unsigned C) {
1410 return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
1413 static unsigned encodingBits(unsigned C) {
1414 return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
1417 public:
1418 // Disallow replacing operands.
1419 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1421 DEFINE_MDNODE_GET(DILocation,
1422 (unsigned Line, unsigned Column, Metadata *Scope,
1423 Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
1424 (Line, Column, Scope, InlinedAt, ImplicitCode))
1425 DEFINE_MDNODE_GET(DILocation,
1426 (unsigned Line, unsigned Column, DILocalScope *Scope,
1427 DILocation *InlinedAt = nullptr,
1428 bool ImplicitCode = false),
1429 (Line, Column, Scope, InlinedAt, ImplicitCode))
1431 /// Return a (temporary) clone of this.
1432 TempDILocation clone() const { return cloneImpl(); }
1434 unsigned getLine() const { return SubclassData32; }
1435 unsigned getColumn() const { return SubclassData16; }
1436 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1438 DILocation *getInlinedAt() const {
1439 return cast_or_null<DILocation>(getRawInlinedAt());
1442 /// Check if the location corresponds to an implicit code.
1443 /// When the ImplicitCode flag is true, it means that the Instruction
1444 /// with this DILocation has been added by the front-end but it hasn't been
1445 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
1446 /// bracket). It's useful for code coverage to not show a counter on "empty"
1447 /// lines.
1448 bool isImplicitCode() const { return ImplicitCode; }
1449 void setImplicitCode(bool ImplicitCode) { this->ImplicitCode = ImplicitCode; }
1451 DIFile *getFile() const { return getScope()->getFile(); }
1452 StringRef getFilename() const { return getScope()->getFilename(); }
1453 StringRef getDirectory() const { return getScope()->getDirectory(); }
1454 Optional<StringRef> getSource() const { return getScope()->getSource(); }
1456 /// Get the scope where this is inlined.
1458 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1459 /// location.
1460 DILocalScope *getInlinedAtScope() const {
1461 if (auto *IA = getInlinedAt())
1462 return IA->getInlinedAtScope();
1463 return getScope();
1466 /// Get the DWARF discriminator.
1468 /// DWARF discriminators distinguish identical file locations between
1469 /// instructions that are on different basic blocks.
1471 /// There are 3 components stored in discriminator, from lower bits:
1473 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1474 /// that are defined by the same source line, but
1475 /// different basic blocks.
1476 /// Duplication factor: assigned by optimizations that will scale down
1477 /// the execution frequency of the original IR.
1478 /// Copy Identifier: assigned by optimizations that clones the IR.
1479 /// Each copy of the IR will be assigned an identifier.
1481 /// Encoding:
1483 /// The above 3 components are encoded into a 32bit unsigned integer in
1484 /// order. If the lowest bit is 1, the current component is empty, and the
1485 /// next component will start in the next bit. Otherwise, the current
1486 /// component is non-empty, and its content starts in the next bit. The
1487 /// value of each components is either 5 bit or 12 bit: if the 7th bit
1488 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1489 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1490 /// represent the component. Thus, the number of bits used for a component
1491 /// is either 0 (if it and all the next components are empty); 1 - if it is
1492 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1493 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1494 /// component is also capped at 0x1ff, even in the case when both first
1495 /// components are 0, and we'd technically have 29 bits available.
1497 /// For precise control over the data being encoded in the discriminator,
1498 /// use encodeDiscriminator/decodeDiscriminator.
1500 inline unsigned getDiscriminator() const;
1502 /// Returns a new DILocation with updated \p Discriminator.
1503 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
1505 /// Returns a new DILocation with updated base discriminator \p BD. Only the
1506 /// base discriminator is set in the new DILocation, the other encoded values
1507 /// are elided.
1508 /// If the discriminator cannot be encoded, the function returns None.
1509 inline Optional<const DILocation *> cloneWithBaseDiscriminator(unsigned BD) const;
1511 /// Returns the duplication factor stored in the discriminator, or 1 if no
1512 /// duplication factor (or 0) is encoded.
1513 inline unsigned getDuplicationFactor() const;
1515 /// Returns the copy identifier stored in the discriminator.
1516 inline unsigned getCopyIdentifier() const;
1518 /// Returns the base discriminator stored in the discriminator.
1519 inline unsigned getBaseDiscriminator() const;
1521 /// Returns a new DILocation with duplication factor \p DF * current
1522 /// duplication factor encoded in the discriminator. The current duplication
1523 /// factor is as defined by getDuplicationFactor().
1524 /// Returns None if encoding failed.
1525 inline Optional<const DILocation *> cloneByMultiplyingDuplicationFactor(unsigned DF) const;
1527 /// When two instructions are combined into a single instruction we also
1528 /// need to combine the original locations into a single location.
1530 /// When the locations are the same we can use either location. When they
1531 /// differ, we need a third location which is distinct from either. If they
1532 /// have the same file/line but have a different discriminator we could
1533 /// create a location with a new discriminator. If they are from different
1534 /// files/lines the location is ambiguous and can't be represented in a line
1535 /// entry. In this case, if \p GenerateLocation is true, we will set the
1536 /// merged debug location as line 0 of the nearest common scope where the two
1537 /// locations are inlined from.
1539 /// \p GenerateLocation: Whether the merged location can be generated when
1540 /// \p LocA and \p LocB differ.
1541 static const DILocation *getMergedLocation(const DILocation *LocA,
1542 const DILocation *LocB);
1544 /// Returns the base discriminator for a given encoded discriminator \p D.
1545 static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
1546 return getUnsignedFromPrefixEncoding(D);
1549 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
1550 /// have certain special case behavior (e.g. treating empty duplication factor
1551 /// as the value '1').
1552 /// This API, in conjunction with cloneWithDiscriminator, may be used to encode
1553 /// the raw values provided. \p BD: base discriminator \p DF: duplication factor
1554 /// \p CI: copy index
1555 /// The return is None if the values cannot be encoded in 32 bits - for
1556 /// example, values for BD or DF larger than 12 bits. Otherwise, the return
1557 /// is the encoded value.
1558 static Optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI);
1560 /// Raw decoder for values in an encoded discriminator D.
1561 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
1562 unsigned &CI);
1564 /// Returns the duplication factor for a given encoded discriminator \p D, or
1565 /// 1 if no value or 0 is encoded.
1566 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
1567 D = getNextComponentInDiscriminator(D);
1568 unsigned Ret = getUnsignedFromPrefixEncoding(D);
1569 if (Ret == 0)
1570 return 1;
1571 return Ret;
1574 /// Returns the copy identifier for a given encoded discriminator \p D.
1575 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
1576 return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
1577 getNextComponentInDiscriminator(D)));
1581 Metadata *getRawScope() const { return getOperand(0); }
1582 Metadata *getRawInlinedAt() const {
1583 if (getNumOperands() == 2)
1584 return getOperand(1);
1585 return nullptr;
1588 static bool classof(const Metadata *MD) {
1589 return MD->getMetadataID() == DILocationKind;
1593 /// Subprogram description.
1594 class DISubprogram : public DILocalScope {
1595 friend class LLVMContextImpl;
1596 friend class MDNode;
1598 unsigned Line;
1599 unsigned ScopeLine;
1600 unsigned VirtualIndex;
1602 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1603 /// of method overrides from secondary bases by this amount. It may be
1604 /// negative.
1605 int ThisAdjustment;
1607 public:
1608 /// Debug info subprogram flags.
1609 enum DISPFlags : uint32_t {
1610 #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
1611 #define DISP_FLAG_LARGEST_NEEDED
1612 #include "llvm/IR/DebugInfoFlags.def"
1613 SPFlagNonvirtual = SPFlagZero,
1614 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
1615 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
1618 static DISPFlags getFlag(StringRef Flag);
1619 static StringRef getFlagString(DISPFlags Flag);
1621 /// Split up a flags bitfield for easier printing.
1623 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
1624 /// any remaining (unrecognized) bits.
1625 static DISPFlags splitFlags(DISPFlags Flags,
1626 SmallVectorImpl<DISPFlags> &SplitFlags);
1628 // Helper for converting old bitfields to new flags word.
1629 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
1630 bool IsOptimized,
1631 unsigned Virtuality = SPFlagNonvirtual,
1632 bool IsMainSubprogram = false) {
1633 // We're assuming virtuality is the low-order field.
1634 static_assert(
1635 int(SPFlagVirtual) == int(dwarf::DW_VIRTUALITY_virtual) &&
1636 int(SPFlagPureVirtual) == int(dwarf::DW_VIRTUALITY_pure_virtual),
1637 "Virtuality constant mismatch");
1638 return static_cast<DISPFlags>(
1639 (Virtuality & SPFlagVirtuality) |
1640 (IsLocalToUnit ? SPFlagLocalToUnit : SPFlagZero) |
1641 (IsDefinition ? SPFlagDefinition : SPFlagZero) |
1642 (IsOptimized ? SPFlagOptimized : SPFlagZero) |
1643 (IsMainSubprogram ? SPFlagMainSubprogram : SPFlagZero));
1646 private:
1647 DIFlags Flags;
1648 DISPFlags SPFlags;
1650 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1651 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
1652 DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops)
1653 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1654 Ops),
1655 Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
1656 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) {
1657 static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
1659 ~DISubprogram() = default;
1661 static DISubprogram *
1662 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1663 StringRef LinkageName, DIFile *File, unsigned Line,
1664 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
1665 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1666 DISPFlags SPFlags, DICompileUnit *Unit,
1667 DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1668 DINodeArray RetainedNodes, DITypeArray ThrownTypes,
1669 StorageType Storage, bool ShouldCreate = true) {
1670 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1671 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1672 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1673 Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
1674 RetainedNodes.get(), ThrownTypes.get(), Storage,
1675 ShouldCreate);
1677 static DISubprogram *getImpl(LLVMContext &Context, Metadata *Scope,
1678 MDString *Name, MDString *LinkageName,
1679 Metadata *File, unsigned Line, Metadata *Type,
1680 unsigned ScopeLine, Metadata *ContainingType,
1681 unsigned VirtualIndex, int ThisAdjustment,
1682 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1683 Metadata *TemplateParams, Metadata *Declaration,
1684 Metadata *RetainedNodes, Metadata *ThrownTypes,
1685 StorageType Storage, bool ShouldCreate = true);
1687 TempDISubprogram cloneImpl() const {
1688 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1689 getFile(), getLine(), getType(), getScopeLine(),
1690 getContainingType(), getVirtualIndex(),
1691 getThisAdjustment(), getFlags(), getSPFlags(),
1692 getUnit(), getTemplateParams(), getDeclaration(),
1693 getRetainedNodes(), getThrownTypes());
1696 public:
1697 DEFINE_MDNODE_GET(
1698 DISubprogram,
1699 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File,
1700 unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
1701 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1702 DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
1703 DITemplateParameterArray TemplateParams = nullptr,
1704 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
1705 DITypeArray ThrownTypes = nullptr),
1706 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1707 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1708 Declaration, RetainedNodes, ThrownTypes))
1710 DEFINE_MDNODE_GET(
1711 DISubprogram,
1712 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1713 unsigned Line, Metadata *Type, unsigned ScopeLine,
1714 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1715 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1716 Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr,
1717 Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr),
1718 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1719 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1720 Declaration, RetainedNodes, ThrownTypes))
1722 TempDISubprogram clone() const { return cloneImpl(); }
1724 /// Returns a new temporary DISubprogram with updated Flags
1725 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
1726 auto NewSP = clone();
1727 NewSP->Flags = NewFlags;
1728 return NewSP;
1731 public:
1732 unsigned getLine() const { return Line; }
1733 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
1734 unsigned getVirtualIndex() const { return VirtualIndex; }
1735 int getThisAdjustment() const { return ThisAdjustment; }
1736 unsigned getScopeLine() const { return ScopeLine; }
1737 DIFlags getFlags() const { return Flags; }
1738 DISPFlags getSPFlags() const { return SPFlags; }
1739 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
1740 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
1741 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
1742 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
1744 bool isArtificial() const { return getFlags() & FlagArtificial; }
1745 bool isPrivate() const {
1746 return (getFlags() & FlagAccessibility) == FlagPrivate;
1748 bool isProtected() const {
1749 return (getFlags() & FlagAccessibility) == FlagProtected;
1751 bool isPublic() const {
1752 return (getFlags() & FlagAccessibility) == FlagPublic;
1754 bool isExplicit() const { return getFlags() & FlagExplicit; }
1755 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1756 bool areAllCallsDescribed() const {
1757 return getFlags() & FlagAllCallsDescribed;
1759 bool isPure() const { return getSPFlags() & SPFlagPure; }
1760 bool isElemental() const { return getSPFlags() & SPFlagElemental; }
1761 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
1763 /// Check if this is reference-qualified.
1765 /// Return true if this subprogram is a C++11 reference-qualified non-static
1766 /// member function (void foo() &).
1767 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
1769 /// Check if this is rvalue-reference-qualified.
1771 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1772 /// non-static member function (void foo() &&).
1773 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
1775 /// Check if this is marked as noreturn.
1777 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1778 bool isNoReturn() const { return getFlags() & FlagNoReturn; }
1780 // Check if this routine is a compiler-generated thunk.
1782 // Returns true if this subprogram is a thunk generated by the compiler.
1783 bool isThunk() const { return getFlags() & FlagThunk; }
1785 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1787 StringRef getName() const { return getStringOperand(2); }
1788 StringRef getLinkageName() const { return getStringOperand(3); }
1790 DISubroutineType *getType() const {
1791 return cast_or_null<DISubroutineType>(getRawType());
1793 DIType *getContainingType() const {
1794 return cast_or_null<DIType>(getRawContainingType());
1797 DICompileUnit *getUnit() const {
1798 return cast_or_null<DICompileUnit>(getRawUnit());
1800 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
1801 DITemplateParameterArray getTemplateParams() const {
1802 return cast_or_null<MDTuple>(getRawTemplateParams());
1804 DISubprogram *getDeclaration() const {
1805 return cast_or_null<DISubprogram>(getRawDeclaration());
1807 DINodeArray getRetainedNodes() const {
1808 return cast_or_null<MDTuple>(getRawRetainedNodes());
1810 DITypeArray getThrownTypes() const {
1811 return cast_or_null<MDTuple>(getRawThrownTypes());
1814 Metadata *getRawScope() const { return getOperand(1); }
1815 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1816 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
1817 Metadata *getRawType() const { return getOperand(4); }
1818 Metadata *getRawUnit() const { return getOperand(5); }
1819 Metadata *getRawDeclaration() const { return getOperand(6); }
1820 Metadata *getRawRetainedNodes() const { return getOperand(7); }
1821 Metadata *getRawContainingType() const {
1822 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
1824 Metadata *getRawTemplateParams() const {
1825 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
1827 Metadata *getRawThrownTypes() const {
1828 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
1831 /// Check if this subprogram describes the given function.
1833 /// FIXME: Should this be looking through bitcasts?
1834 bool describes(const Function *F) const;
1836 static bool classof(const Metadata *MD) {
1837 return MD->getMetadataID() == DISubprogramKind;
1841 class DILexicalBlockBase : public DILocalScope {
1842 protected:
1843 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1844 ArrayRef<Metadata *> Ops)
1845 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1846 ~DILexicalBlockBase() = default;
1848 public:
1849 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1851 Metadata *getRawScope() const { return getOperand(1); }
1853 static bool classof(const Metadata *MD) {
1854 return MD->getMetadataID() == DILexicalBlockKind ||
1855 MD->getMetadataID() == DILexicalBlockFileKind;
1859 class DILexicalBlock : public DILexicalBlockBase {
1860 friend class LLVMContextImpl;
1861 friend class MDNode;
1863 unsigned Line;
1864 uint16_t Column;
1866 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1867 unsigned Column, ArrayRef<Metadata *> Ops)
1868 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1869 Column(Column) {
1870 assert(Column < (1u << 16) && "Expected 16-bit column");
1872 ~DILexicalBlock() = default;
1874 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1875 DIFile *File, unsigned Line, unsigned Column,
1876 StorageType Storage,
1877 bool ShouldCreate = true) {
1878 return getImpl(Context, static_cast<Metadata *>(Scope),
1879 static_cast<Metadata *>(File), Line, Column, Storage,
1880 ShouldCreate);
1883 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1884 Metadata *File, unsigned Line, unsigned Column,
1885 StorageType Storage, bool ShouldCreate = true);
1887 TempDILexicalBlock cloneImpl() const {
1888 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1889 getColumn());
1892 public:
1893 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1894 unsigned Line, unsigned Column),
1895 (Scope, File, Line, Column))
1896 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1897 unsigned Line, unsigned Column),
1898 (Scope, File, Line, Column))
1900 TempDILexicalBlock clone() const { return cloneImpl(); }
1902 unsigned getLine() const { return Line; }
1903 unsigned getColumn() const { return Column; }
1905 static bool classof(const Metadata *MD) {
1906 return MD->getMetadataID() == DILexicalBlockKind;
1910 class DILexicalBlockFile : public DILexicalBlockBase {
1911 friend class LLVMContextImpl;
1912 friend class MDNode;
1914 unsigned Discriminator;
1916 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1917 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1918 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1919 Discriminator(Discriminator) {}
1920 ~DILexicalBlockFile() = default;
1922 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1923 DIFile *File, unsigned Discriminator,
1924 StorageType Storage,
1925 bool ShouldCreate = true) {
1926 return getImpl(Context, static_cast<Metadata *>(Scope),
1927 static_cast<Metadata *>(File), Discriminator, Storage,
1928 ShouldCreate);
1931 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1932 Metadata *File, unsigned Discriminator,
1933 StorageType Storage,
1934 bool ShouldCreate = true);
1936 TempDILexicalBlockFile cloneImpl() const {
1937 return getTemporary(getContext(), getScope(), getFile(),
1938 getDiscriminator());
1941 public:
1942 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1943 unsigned Discriminator),
1944 (Scope, File, Discriminator))
1945 DEFINE_MDNODE_GET(DILexicalBlockFile,
1946 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1947 (Scope, File, Discriminator))
1949 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1951 // TODO: Remove these once they're gone from DILexicalBlockBase.
1952 unsigned getLine() const = delete;
1953 unsigned getColumn() const = delete;
1955 unsigned getDiscriminator() const { return Discriminator; }
1957 static bool classof(const Metadata *MD) {
1958 return MD->getMetadataID() == DILexicalBlockFileKind;
1962 unsigned DILocation::getDiscriminator() const {
1963 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1964 return F->getDiscriminator();
1965 return 0;
1968 const DILocation *
1969 DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
1970 DIScope *Scope = getScope();
1971 // Skip all parent DILexicalBlockFile that already have a discriminator
1972 // assigned. We do not want to have nested DILexicalBlockFiles that have
1973 // mutliple discriminators because only the leaf DILexicalBlockFile's
1974 // dominator will be used.
1975 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
1976 LBF && LBF->getDiscriminator() != 0;
1977 LBF = dyn_cast<DILexicalBlockFile>(Scope))
1978 Scope = LBF->getScope();
1979 DILexicalBlockFile *NewScope =
1980 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
1981 return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
1982 getInlinedAt());
1985 unsigned DILocation::getBaseDiscriminator() const {
1986 return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
1989 unsigned DILocation::getDuplicationFactor() const {
1990 return getDuplicationFactorFromDiscriminator(getDiscriminator());
1993 unsigned DILocation::getCopyIdentifier() const {
1994 return getCopyIdentifierFromDiscriminator(getDiscriminator());
1997 Optional<const DILocation *> DILocation::cloneWithBaseDiscriminator(unsigned D) const {
1998 unsigned BD, DF, CI;
1999 decodeDiscriminator(getDiscriminator(), BD, DF, CI);
2000 if (D == BD)
2001 return this;
2002 if (Optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
2003 return cloneWithDiscriminator(*Encoded);
2004 return None;
2007 Optional<const DILocation *> DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const {
2008 DF *= getDuplicationFactor();
2009 if (DF <= 1)
2010 return this;
2012 unsigned BD = getBaseDiscriminator();
2013 unsigned CI = getCopyIdentifier();
2014 if (Optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
2015 return cloneWithDiscriminator(*D);
2016 return None;
2019 class DINamespace : public DIScope {
2020 friend class LLVMContextImpl;
2021 friend class MDNode;
2023 unsigned ExportSymbols : 1;
2025 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
2026 ArrayRef<Metadata *> Ops)
2027 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
2028 Ops),
2029 ExportSymbols(ExportSymbols) {}
2030 ~DINamespace() = default;
2032 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
2033 StringRef Name, bool ExportSymbols,
2034 StorageType Storage, bool ShouldCreate = true) {
2035 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2036 ExportSymbols, Storage, ShouldCreate);
2038 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
2039 MDString *Name, bool ExportSymbols,
2040 StorageType Storage, bool ShouldCreate = true);
2042 TempDINamespace cloneImpl() const {
2043 return getTemporary(getContext(), getScope(), getName(),
2044 getExportSymbols());
2047 public:
2048 DEFINE_MDNODE_GET(DINamespace,
2049 (DIScope *Scope, StringRef Name, bool ExportSymbols),
2050 (Scope, Name, ExportSymbols))
2051 DEFINE_MDNODE_GET(DINamespace,
2052 (Metadata *Scope, MDString *Name, bool ExportSymbols),
2053 (Scope, Name, ExportSymbols))
2055 TempDINamespace clone() const { return cloneImpl(); }
2057 bool getExportSymbols() const { return ExportSymbols; }
2058 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2059 StringRef getName() const { return getStringOperand(2); }
2061 Metadata *getRawScope() const { return getOperand(1); }
2062 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2064 static bool classof(const Metadata *MD) {
2065 return MD->getMetadataID() == DINamespaceKind;
2069 /// A (clang) module that has been imported by the compile unit.
2071 class DIModule : public DIScope {
2072 friend class LLVMContextImpl;
2073 friend class MDNode;
2075 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
2076 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
2077 ~DIModule() = default;
2079 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
2080 StringRef Name, StringRef ConfigurationMacros,
2081 StringRef IncludePath, StringRef ISysRoot,
2082 StorageType Storage, bool ShouldCreate = true) {
2083 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2084 getCanonicalMDString(Context, ConfigurationMacros),
2085 getCanonicalMDString(Context, IncludePath),
2086 getCanonicalMDString(Context, ISysRoot),
2087 Storage, ShouldCreate);
2089 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
2090 MDString *Name, MDString *ConfigurationMacros,
2091 MDString *IncludePath, MDString *ISysRoot,
2092 StorageType Storage, bool ShouldCreate = true);
2094 TempDIModule cloneImpl() const {
2095 return getTemporary(getContext(), getScope(), getName(),
2096 getConfigurationMacros(), getIncludePath(),
2097 getISysRoot());
2100 public:
2101 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
2102 StringRef ConfigurationMacros, StringRef IncludePath,
2103 StringRef ISysRoot),
2104 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2105 DEFINE_MDNODE_GET(DIModule,
2106 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
2107 MDString *IncludePath, MDString *ISysRoot),
2108 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2110 TempDIModule clone() const { return cloneImpl(); }
2112 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2113 StringRef getName() const { return getStringOperand(1); }
2114 StringRef getConfigurationMacros() const { return getStringOperand(2); }
2115 StringRef getIncludePath() const { return getStringOperand(3); }
2116 StringRef getISysRoot() const { return getStringOperand(4); }
2118 Metadata *getRawScope() const { return getOperand(0); }
2119 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2120 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
2121 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
2122 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
2124 static bool classof(const Metadata *MD) {
2125 return MD->getMetadataID() == DIModuleKind;
2129 /// Base class for template parameters.
2130 class DITemplateParameter : public DINode {
2131 protected:
2132 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
2133 unsigned Tag, ArrayRef<Metadata *> Ops)
2134 : DINode(Context, ID, Storage, Tag, Ops) {}
2135 ~DITemplateParameter() = default;
2137 public:
2138 StringRef getName() const { return getStringOperand(0); }
2139 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2141 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2142 Metadata *getRawType() const { return getOperand(1); }
2144 static bool classof(const Metadata *MD) {
2145 return MD->getMetadataID() == DITemplateTypeParameterKind ||
2146 MD->getMetadataID() == DITemplateValueParameterKind;
2150 class DITemplateTypeParameter : public DITemplateParameter {
2151 friend class LLVMContextImpl;
2152 friend class MDNode;
2154 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
2155 ArrayRef<Metadata *> Ops)
2156 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
2157 dwarf::DW_TAG_template_type_parameter, Ops) {}
2158 ~DITemplateTypeParameter() = default;
2160 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
2161 DIType *Type, StorageType Storage,
2162 bool ShouldCreate = true) {
2163 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
2164 ShouldCreate);
2166 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
2167 Metadata *Type, StorageType Storage,
2168 bool ShouldCreate = true);
2170 TempDITemplateTypeParameter cloneImpl() const {
2171 return getTemporary(getContext(), getName(), getType());
2174 public:
2175 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DIType *Type),
2176 (Name, Type))
2177 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
2178 (Name, Type))
2180 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2182 static bool classof(const Metadata *MD) {
2183 return MD->getMetadataID() == DITemplateTypeParameterKind;
2187 class DITemplateValueParameter : public DITemplateParameter {
2188 friend class LLVMContextImpl;
2189 friend class MDNode;
2191 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
2192 unsigned Tag, ArrayRef<Metadata *> Ops)
2193 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2194 Ops) {}
2195 ~DITemplateValueParameter() = default;
2197 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2198 StringRef Name, DIType *Type,
2199 Metadata *Value, StorageType Storage,
2200 bool ShouldCreate = true) {
2201 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2202 Value, Storage, ShouldCreate);
2204 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2205 MDString *Name, Metadata *Type,
2206 Metadata *Value, StorageType Storage,
2207 bool ShouldCreate = true);
2209 TempDITemplateValueParameter cloneImpl() const {
2210 return getTemporary(getContext(), getTag(), getName(), getType(),
2211 getValue());
2214 public:
2215 DEFINE_MDNODE_GET(DITemplateValueParameter,
2216 (unsigned Tag, StringRef Name, DIType *Type,
2217 Metadata *Value),
2218 (Tag, Name, Type, Value))
2219 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
2220 Metadata *Type, Metadata *Value),
2221 (Tag, Name, Type, Value))
2223 TempDITemplateValueParameter clone() const { return cloneImpl(); }
2225 Metadata *getValue() const { return getOperand(2); }
2227 static bool classof(const Metadata *MD) {
2228 return MD->getMetadataID() == DITemplateValueParameterKind;
2232 /// Base class for variables.
2233 class DIVariable : public DINode {
2234 unsigned Line;
2235 uint32_t AlignInBits;
2237 protected:
2238 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
2239 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
2240 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
2241 AlignInBits(AlignInBits) {}
2242 ~DIVariable() = default;
2244 public:
2245 unsigned getLine() const { return Line; }
2246 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2247 StringRef getName() const { return getStringOperand(1); }
2248 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2249 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2250 uint32_t getAlignInBits() const { return AlignInBits; }
2251 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2252 /// Determines the size of the variable's type.
2253 Optional<uint64_t> getSizeInBits() const;
2255 /// Return the signedness of this variable's type, or None if this type is
2256 /// neither signed nor unsigned.
2257 Optional<DIBasicType::Signedness> getSignedness() const {
2258 if (auto *BT = dyn_cast<DIBasicType>(getType()))
2259 return BT->getSignedness();
2260 return None;
2263 StringRef getFilename() const {
2264 if (auto *F = getFile())
2265 return F->getFilename();
2266 return "";
2269 StringRef getDirectory() const {
2270 if (auto *F = getFile())
2271 return F->getDirectory();
2272 return "";
2275 Optional<StringRef> getSource() const {
2276 if (auto *F = getFile())
2277 return F->getSource();
2278 return None;
2281 Metadata *getRawScope() const { return getOperand(0); }
2282 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2283 Metadata *getRawFile() const { return getOperand(2); }
2284 Metadata *getRawType() const { return getOperand(3); }
2286 static bool classof(const Metadata *MD) {
2287 return MD->getMetadataID() == DILocalVariableKind ||
2288 MD->getMetadataID() == DIGlobalVariableKind;
2292 /// DWARF expression.
2294 /// This is (almost) a DWARF expression that modifies the location of a
2295 /// variable, or the location of a single piece of a variable, or (when using
2296 /// DW_OP_stack_value) is the constant variable value.
2298 /// TODO: Co-allocate the expression elements.
2299 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2300 /// storage types.
2301 class DIExpression : public MDNode {
2302 friend class LLVMContextImpl;
2303 friend class MDNode;
2305 std::vector<uint64_t> Elements;
2307 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2308 : MDNode(C, DIExpressionKind, Storage, None),
2309 Elements(Elements.begin(), Elements.end()) {}
2310 ~DIExpression() = default;
2312 static DIExpression *getImpl(LLVMContext &Context,
2313 ArrayRef<uint64_t> Elements, StorageType Storage,
2314 bool ShouldCreate = true);
2316 TempDIExpression cloneImpl() const {
2317 return getTemporary(getContext(), getElements());
2320 public:
2321 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2323 TempDIExpression clone() const { return cloneImpl(); }
2325 ArrayRef<uint64_t> getElements() const { return Elements; }
2327 unsigned getNumElements() const { return Elements.size(); }
2329 uint64_t getElement(unsigned I) const {
2330 assert(I < Elements.size() && "Index out of range");
2331 return Elements[I];
2334 /// Determine whether this represents a standalone constant value.
2335 bool isConstant() const;
2337 using element_iterator = ArrayRef<uint64_t>::iterator;
2339 element_iterator elements_begin() const { return getElements().begin(); }
2340 element_iterator elements_end() const { return getElements().end(); }
2342 /// A lightweight wrapper around an expression operand.
2344 /// TODO: Store arguments directly and change \a DIExpression to store a
2345 /// range of these.
2346 class ExprOperand {
2347 const uint64_t *Op = nullptr;
2349 public:
2350 ExprOperand() = default;
2351 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2353 const uint64_t *get() const { return Op; }
2355 /// Get the operand code.
2356 uint64_t getOp() const { return *Op; }
2358 /// Get an argument to the operand.
2360 /// Never returns the operand itself.
2361 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2363 unsigned getNumArgs() const { return getSize() - 1; }
2365 /// Return the size of the operand.
2367 /// Return the number of elements in the operand (1 + args).
2368 unsigned getSize() const;
2370 /// Append the elements of this operand to \p V.
2371 void appendToVector(SmallVectorImpl<uint64_t> &V) const {
2372 V.append(get(), get() + getSize());
2376 /// An iterator for expression operands.
2377 class expr_op_iterator
2378 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2379 ExprOperand Op;
2381 public:
2382 expr_op_iterator() = default;
2383 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2385 element_iterator getBase() const { return Op.get(); }
2386 const ExprOperand &operator*() const { return Op; }
2387 const ExprOperand *operator->() const { return &Op; }
2389 expr_op_iterator &operator++() {
2390 increment();
2391 return *this;
2393 expr_op_iterator operator++(int) {
2394 expr_op_iterator T(*this);
2395 increment();
2396 return T;
2399 /// Get the next iterator.
2401 /// \a std::next() doesn't work because this is technically an
2402 /// input_iterator, but it's a perfectly valid operation. This is an
2403 /// accessor to provide the same functionality.
2404 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2406 bool operator==(const expr_op_iterator &X) const {
2407 return getBase() == X.getBase();
2409 bool operator!=(const expr_op_iterator &X) const {
2410 return getBase() != X.getBase();
2413 private:
2414 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2417 /// Visit the elements via ExprOperand wrappers.
2419 /// These range iterators visit elements through \a ExprOperand wrappers.
2420 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2421 /// true.
2423 /// \pre \a isValid() gives \c true.
2424 /// @{
2425 expr_op_iterator expr_op_begin() const {
2426 return expr_op_iterator(elements_begin());
2428 expr_op_iterator expr_op_end() const {
2429 return expr_op_iterator(elements_end());
2431 iterator_range<expr_op_iterator> expr_ops() const {
2432 return {expr_op_begin(), expr_op_end()};
2434 /// @}
2436 bool isValid() const;
2438 static bool classof(const Metadata *MD) {
2439 return MD->getMetadataID() == DIExpressionKind;
2442 /// Return whether the first element a DW_OP_deref.
2443 bool startsWithDeref() const {
2444 return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
2447 /// Holds the characteristics of one fragment of a larger variable.
2448 struct FragmentInfo {
2449 uint64_t SizeInBits;
2450 uint64_t OffsetInBits;
2453 /// Retrieve the details of this fragment expression.
2454 static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2455 expr_op_iterator End);
2457 /// Retrieve the details of this fragment expression.
2458 Optional<FragmentInfo> getFragmentInfo() const {
2459 return getFragmentInfo(expr_op_begin(), expr_op_end());
2462 /// Return whether this is a piece of an aggregate variable.
2463 bool isFragment() const { return getFragmentInfo().hasValue(); }
2465 /// Return whether this is an implicit location description.
2466 bool isImplicit() const;
2468 /// Return whether the location is computed on the expression stack, meaning
2469 /// it cannot be a simple register location.
2470 bool isComplex() const;
2472 /// Append \p Ops with operations to apply the \p Offset.
2473 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2475 /// If this is a constant offset, extract it. If there is no expression,
2476 /// return true with an offset of zero.
2477 bool extractIfOffset(int64_t &Offset) const;
2479 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
2480 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
2481 /// Space>.
2482 static const DIExpression *extractAddressClass(const DIExpression *Expr,
2483 unsigned &AddrClass);
2485 /// Used for DIExpression::prepend.
2486 enum PrependOps : uint8_t {
2487 ApplyOffset = 0,
2488 DerefBefore = 1 << 0,
2489 DerefAfter = 1 << 1,
2490 StackValue = 1 << 2,
2491 EntryValue = 1 << 3
2494 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2495 /// into a stack value or/and an entry value.
2496 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2497 int64_t Offset = 0);
2499 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2500 /// stack value.
2501 static DIExpression *prependOpcodes(const DIExpression *Expr,
2502 SmallVectorImpl<uint64_t> &Ops,
2503 bool StackValue = false,
2504 bool EntryValue = false);
2506 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
2507 /// returned expression is a stack value only if \p DIExpr is a stack value.
2508 /// If \p DIExpr describes a fragment, the returned expression will describe
2509 /// the same fragment.
2510 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
2512 /// Convert \p DIExpr into a stack value if it isn't one already by appending
2513 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
2514 /// If \p DIExpr describes a fragment, the returned expression will describe
2515 /// the same fragment.
2516 static DIExpression *appendToStack(const DIExpression *Expr,
2517 ArrayRef<uint64_t> Ops);
2519 /// Create a DIExpression to describe one part of an aggregate variable that
2520 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2521 /// will be appended to the elements of \c Expr. If \c Expr already contains
2522 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2523 /// into the existing fragment.
2525 /// \param OffsetInBits Offset of the piece in bits.
2526 /// \param SizeInBits Size of the piece in bits.
2527 /// \return Creating a fragment expression may fail if \c Expr
2528 /// contains arithmetic operations that would be truncated.
2529 static Optional<DIExpression *>
2530 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2531 unsigned SizeInBits);
2533 /// Determine the relative position of the fragments passed in.
2534 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2535 /// 1 if this is entirely after Other.
2536 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
2537 uint64_t l1 = A.OffsetInBits;
2538 uint64_t l2 = B.OffsetInBits;
2539 uint64_t r1 = l1 + A.SizeInBits;
2540 uint64_t r2 = l2 + B.SizeInBits;
2541 if (r1 <= l2)
2542 return -1;
2543 else if (r2 <= l1)
2544 return 1;
2545 else
2546 return 0;
2549 /// Check if fragments overlap between a pair of FragmentInfos.
2550 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
2551 return fragmentCmp(A, B) == 0;
2554 /// Determine the relative position of the fragments described by this
2555 /// DIExpression and \p Other. Calls static fragmentCmp implementation.
2556 int fragmentCmp(const DIExpression *Other) const {
2557 auto Fragment1 = *getFragmentInfo();
2558 auto Fragment2 = *Other->getFragmentInfo();
2559 return fragmentCmp(Fragment1, Fragment2);
2562 /// Check if fragments overlap between this DIExpression and \p Other.
2563 bool fragmentsOverlap(const DIExpression *Other) const {
2564 if (!isFragment() || !Other->isFragment())
2565 return true;
2566 return fragmentCmp(Other) == 0;
2569 /// Check if the expression consists of exactly one entry value operand.
2570 /// (This is the only configuration of entry values that is supported.)
2571 bool isEntryValue() const {
2572 return getNumElements() > 0 &&
2573 getElement(0) == dwarf::DW_OP_entry_value;
2577 inline bool operator==(const DIExpression::FragmentInfo &A,
2578 const DIExpression::FragmentInfo &B) {
2579 return std::tie(A.SizeInBits, A.OffsetInBits) ==
2580 std::tie(B.SizeInBits, B.OffsetInBits);
2583 inline bool operator<(const DIExpression::FragmentInfo &A,
2584 const DIExpression::FragmentInfo &B) {
2585 return std::tie(A.SizeInBits, A.OffsetInBits) <
2586 std::tie(B.SizeInBits, B.OffsetInBits);
2589 template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
2590 using FragInfo = DIExpression::FragmentInfo;
2591 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
2593 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
2595 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
2597 static unsigned getHashValue(const FragInfo &Frag) {
2598 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
2601 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
2604 /// Global variables.
2606 /// TODO: Remove DisplayName. It's always equal to Name.
2607 class DIGlobalVariable : public DIVariable {
2608 friend class LLVMContextImpl;
2609 friend class MDNode;
2611 bool IsLocalToUnit;
2612 bool IsDefinition;
2614 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2615 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
2616 ArrayRef<Metadata *> Ops)
2617 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
2618 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
2619 ~DIGlobalVariable() = default;
2621 static DIGlobalVariable *
2622 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
2623 StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type,
2624 bool IsLocalToUnit, bool IsDefinition,
2625 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
2626 uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true) {
2627 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2628 getCanonicalMDString(Context, LinkageName), File, Line, Type,
2629 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
2630 cast_or_null<Metadata>(TemplateParams), AlignInBits, Storage,
2631 ShouldCreate);
2633 static DIGlobalVariable *
2634 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
2635 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
2636 bool IsLocalToUnit, bool IsDefinition,
2637 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
2638 uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true);
2640 TempDIGlobalVariable cloneImpl() const {
2641 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
2642 getFile(), getLine(), getType(), isLocalToUnit(),
2643 isDefinition(), getStaticDataMemberDeclaration(),
2644 getTemplateParams(), getAlignInBits());
2647 public:
2648 DEFINE_MDNODE_GET(DIGlobalVariable,
2649 (DIScope * Scope, StringRef Name, StringRef LinkageName,
2650 DIFile *File, unsigned Line, DIType *Type,
2651 bool IsLocalToUnit, bool IsDefinition,
2652 DIDerivedType *StaticDataMemberDeclaration,
2653 MDTuple *TemplateParams, uint32_t AlignInBits),
2654 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2655 IsDefinition, StaticDataMemberDeclaration, TemplateParams,
2656 AlignInBits))
2657 DEFINE_MDNODE_GET(DIGlobalVariable,
2658 (Metadata * Scope, MDString *Name, MDString *LinkageName,
2659 Metadata *File, unsigned Line, Metadata *Type,
2660 bool IsLocalToUnit, bool IsDefinition,
2661 Metadata *StaticDataMemberDeclaration,
2662 Metadata *TemplateParams, uint32_t AlignInBits),
2663 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2664 IsDefinition, StaticDataMemberDeclaration, TemplateParams,
2665 AlignInBits))
2667 TempDIGlobalVariable clone() const { return cloneImpl(); }
2669 bool isLocalToUnit() const { return IsLocalToUnit; }
2670 bool isDefinition() const { return IsDefinition; }
2671 StringRef getDisplayName() const { return getStringOperand(4); }
2672 StringRef getLinkageName() const { return getStringOperand(5); }
2673 DIDerivedType *getStaticDataMemberDeclaration() const {
2674 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
2677 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
2678 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
2679 Metadata *getRawTemplateParams() const { return getOperand(7); }
2680 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
2682 static bool classof(const Metadata *MD) {
2683 return MD->getMetadataID() == DIGlobalVariableKind;
2687 class DICommonBlock : public DIScope {
2688 unsigned LineNo;
2690 friend class LLVMContextImpl;
2691 friend class MDNode;
2693 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
2694 ArrayRef<Metadata *> Ops)
2695 : DIScope(Context, DICommonBlockKind, Storage, dwarf::DW_TAG_common_block,
2696 Ops), LineNo(LineNo) {}
2698 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
2699 DIGlobalVariable *Decl, StringRef Name,
2700 DIFile *File, unsigned LineNo,
2701 StorageType Storage,
2702 bool ShouldCreate = true) {
2703 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
2704 File, LineNo, Storage, ShouldCreate);
2706 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
2707 Metadata *Decl, MDString *Name, Metadata *File,
2708 unsigned LineNo,
2709 StorageType Storage, bool ShouldCreate = true);
2711 TempDICommonBlock cloneImpl() const {
2712 return getTemporary(getContext(), getScope(), getDecl(), getName(),
2713 getFile(), getLineNo());
2716 public:
2717 DEFINE_MDNODE_GET(DICommonBlock,
2718 (DIScope *Scope, DIGlobalVariable *Decl, StringRef Name,
2719 DIFile *File, unsigned LineNo),
2720 (Scope, Decl, Name, File, LineNo))
2721 DEFINE_MDNODE_GET(DICommonBlock,
2722 (Metadata *Scope, Metadata *Decl, MDString *Name,
2723 Metadata *File, unsigned LineNo),
2724 (Scope, Decl, Name, File, LineNo))
2726 TempDICommonBlock clone() const { return cloneImpl(); }
2728 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2729 DIGlobalVariable *getDecl() const {
2730 return cast_or_null<DIGlobalVariable>(getRawDecl());
2732 StringRef getName() const { return getStringOperand(2); }
2733 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2734 unsigned getLineNo() const { return LineNo; }
2736 Metadata *getRawScope() const { return getOperand(0); }
2737 Metadata *getRawDecl() const { return getOperand(1); }
2738 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2739 Metadata *getRawFile() const { return getOperand(3); }
2741 static bool classof(const Metadata *MD) {
2742 return MD->getMetadataID() == DICommonBlockKind;
2746 /// Local variable.
2748 /// TODO: Split up flags.
2749 class DILocalVariable : public DIVariable {
2750 friend class LLVMContextImpl;
2751 friend class MDNode;
2753 unsigned Arg : 16;
2754 DIFlags Flags;
2756 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2757 unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
2758 ArrayRef<Metadata *> Ops)
2759 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
2760 Arg(Arg), Flags(Flags) {
2761 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
2763 ~DILocalVariable() = default;
2765 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2766 StringRef Name, DIFile *File, unsigned Line,
2767 DIType *Type, unsigned Arg, DIFlags Flags,
2768 uint32_t AlignInBits, StorageType Storage,
2769 bool ShouldCreate = true) {
2770 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2771 Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
2773 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
2774 MDString *Name, Metadata *File, unsigned Line,
2775 Metadata *Type, unsigned Arg, DIFlags Flags,
2776 uint32_t AlignInBits, StorageType Storage,
2777 bool ShouldCreate = true);
2779 TempDILocalVariable cloneImpl() const {
2780 return getTemporary(getContext(), getScope(), getName(), getFile(),
2781 getLine(), getType(), getArg(), getFlags(),
2782 getAlignInBits());
2785 public:
2786 DEFINE_MDNODE_GET(DILocalVariable,
2787 (DILocalScope * Scope, StringRef Name, DIFile *File,
2788 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
2789 uint32_t AlignInBits),
2790 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2791 DEFINE_MDNODE_GET(DILocalVariable,
2792 (Metadata * Scope, MDString *Name, Metadata *File,
2793 unsigned Line, Metadata *Type, unsigned Arg,
2794 DIFlags Flags, uint32_t AlignInBits),
2795 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2797 TempDILocalVariable clone() const { return cloneImpl(); }
2799 /// Get the local scope for this variable.
2801 /// Variables must be defined in a local scope.
2802 DILocalScope *getScope() const {
2803 return cast<DILocalScope>(DIVariable::getScope());
2806 bool isParameter() const { return Arg; }
2807 unsigned getArg() const { return Arg; }
2808 DIFlags getFlags() const { return Flags; }
2810 bool isArtificial() const { return getFlags() & FlagArtificial; }
2811 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
2813 /// Check that an argument is unmodified.
2814 bool isNotModified() const { return getFlags() & FlagArgumentNotModified; }
2815 /// Set the flag if an argument is unmodified.
2816 void setIsNotModified() { Flags |= FlagArgumentNotModified; }
2818 /// Check that a location is valid for this variable.
2820 /// Check that \c DL exists, is in the same subprogram, and has the same
2821 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2822 /// to a \a DbgInfoIntrinsic.)
2823 bool isValidLocationForIntrinsic(const DILocation *DL) const {
2824 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2827 static bool classof(const Metadata *MD) {
2828 return MD->getMetadataID() == DILocalVariableKind;
2832 /// Label.
2834 class DILabel : public DINode {
2835 friend class LLVMContextImpl;
2836 friend class MDNode;
2838 unsigned Line;
2840 DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
2841 ArrayRef<Metadata *> Ops)
2842 : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops), Line(Line) {}
2843 ~DILabel() = default;
2845 static DILabel *getImpl(LLVMContext &Context, DIScope *Scope,
2846 StringRef Name, DIFile *File, unsigned Line,
2847 StorageType Storage,
2848 bool ShouldCreate = true) {
2849 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2850 Line, Storage, ShouldCreate);
2852 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope,
2853 MDString *Name, Metadata *File, unsigned Line,
2854 StorageType Storage,
2855 bool ShouldCreate = true);
2857 TempDILabel cloneImpl() const {
2858 return getTemporary(getContext(), getScope(), getName(), getFile(),
2859 getLine());
2862 public:
2863 DEFINE_MDNODE_GET(DILabel,
2864 (DILocalScope * Scope, StringRef Name, DIFile *File,
2865 unsigned Line),
2866 (Scope, Name, File, Line))
2867 DEFINE_MDNODE_GET(DILabel,
2868 (Metadata * Scope, MDString *Name, Metadata *File,
2869 unsigned Line),
2870 (Scope, Name, File, Line))
2872 TempDILabel clone() const { return cloneImpl(); }
2874 /// Get the local scope for this label.
2876 /// Labels must be defined in a local scope.
2877 DILocalScope *getScope() const {
2878 return cast_or_null<DILocalScope>(getRawScope());
2880 unsigned getLine() const { return Line; }
2881 StringRef getName() const { return getStringOperand(1); }
2882 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2884 Metadata *getRawScope() const { return getOperand(0); }
2885 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2886 Metadata *getRawFile() const { return getOperand(2); }
2888 /// Check that a location is valid for this label.
2890 /// Check that \c DL exists, is in the same subprogram, and has the same
2891 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2892 /// to a \a DbgInfoIntrinsic.)
2893 bool isValidLocationForIntrinsic(const DILocation *DL) const {
2894 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2897 static bool classof(const Metadata *MD) {
2898 return MD->getMetadataID() == DILabelKind;
2902 class DIObjCProperty : public DINode {
2903 friend class LLVMContextImpl;
2904 friend class MDNode;
2906 unsigned Line;
2907 unsigned Attributes;
2909 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2910 unsigned Attributes, ArrayRef<Metadata *> Ops)
2911 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2912 Ops),
2913 Line(Line), Attributes(Attributes) {}
2914 ~DIObjCProperty() = default;
2916 static DIObjCProperty *
2917 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2918 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2919 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2920 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2921 getCanonicalMDString(Context, GetterName),
2922 getCanonicalMDString(Context, SetterName), Attributes, Type,
2923 Storage, ShouldCreate);
2925 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2926 Metadata *File, unsigned Line,
2927 MDString *GetterName, MDString *SetterName,
2928 unsigned Attributes, Metadata *Type,
2929 StorageType Storage, bool ShouldCreate = true);
2931 TempDIObjCProperty cloneImpl() const {
2932 return getTemporary(getContext(), getName(), getFile(), getLine(),
2933 getGetterName(), getSetterName(), getAttributes(),
2934 getType());
2937 public:
2938 DEFINE_MDNODE_GET(DIObjCProperty,
2939 (StringRef Name, DIFile *File, unsigned Line,
2940 StringRef GetterName, StringRef SetterName,
2941 unsigned Attributes, DIType *Type),
2942 (Name, File, Line, GetterName, SetterName, Attributes,
2943 Type))
2944 DEFINE_MDNODE_GET(DIObjCProperty,
2945 (MDString * Name, Metadata *File, unsigned Line,
2946 MDString *GetterName, MDString *SetterName,
2947 unsigned Attributes, Metadata *Type),
2948 (Name, File, Line, GetterName, SetterName, Attributes,
2949 Type))
2951 TempDIObjCProperty clone() const { return cloneImpl(); }
2953 unsigned getLine() const { return Line; }
2954 unsigned getAttributes() const { return Attributes; }
2955 StringRef getName() const { return getStringOperand(0); }
2956 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2957 StringRef getGetterName() const { return getStringOperand(2); }
2958 StringRef getSetterName() const { return getStringOperand(3); }
2959 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2961 StringRef getFilename() const {
2962 if (auto *F = getFile())
2963 return F->getFilename();
2964 return "";
2967 StringRef getDirectory() const {
2968 if (auto *F = getFile())
2969 return F->getDirectory();
2970 return "";
2973 Optional<StringRef> getSource() const {
2974 if (auto *F = getFile())
2975 return F->getSource();
2976 return None;
2979 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2980 Metadata *getRawFile() const { return getOperand(1); }
2981 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2982 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2983 Metadata *getRawType() const { return getOperand(4); }
2985 static bool classof(const Metadata *MD) {
2986 return MD->getMetadataID() == DIObjCPropertyKind;
2990 /// An imported module (C++ using directive or similar).
2991 class DIImportedEntity : public DINode {
2992 friend class LLVMContextImpl;
2993 friend class MDNode;
2995 unsigned Line;
2997 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2998 unsigned Line, ArrayRef<Metadata *> Ops)
2999 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
3000 ~DIImportedEntity() = default;
3002 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3003 DIScope *Scope, DINode *Entity, DIFile *File,
3004 unsigned Line, StringRef Name,
3005 StorageType Storage,
3006 bool ShouldCreate = true) {
3007 return getImpl(Context, Tag, Scope, Entity, File, Line,
3008 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
3010 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3011 Metadata *Scope, Metadata *Entity,
3012 Metadata *File, unsigned Line,
3013 MDString *Name, StorageType Storage,
3014 bool ShouldCreate = true);
3016 TempDIImportedEntity cloneImpl() const {
3017 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3018 getFile(), getLine(), getName());
3021 public:
3022 DEFINE_MDNODE_GET(DIImportedEntity,
3023 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3024 unsigned Line, StringRef Name = ""),
3025 (Tag, Scope, Entity, File, Line, Name))
3026 DEFINE_MDNODE_GET(DIImportedEntity,
3027 (unsigned Tag, Metadata *Scope, Metadata *Entity,
3028 Metadata *File, unsigned Line, MDString *Name),
3029 (Tag, Scope, Entity, File, Line, Name))
3031 TempDIImportedEntity clone() const { return cloneImpl(); }
3033 unsigned getLine() const { return Line; }
3034 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3035 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
3036 StringRef getName() const { return getStringOperand(2); }
3037 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3039 Metadata *getRawScope() const { return getOperand(0); }
3040 Metadata *getRawEntity() const { return getOperand(1); }
3041 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3042 Metadata *getRawFile() const { return getOperand(3); }
3044 static bool classof(const Metadata *MD) {
3045 return MD->getMetadataID() == DIImportedEntityKind;
3049 /// A pair of DIGlobalVariable and DIExpression.
3050 class DIGlobalVariableExpression : public MDNode {
3051 friend class LLVMContextImpl;
3052 friend class MDNode;
3054 DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
3055 ArrayRef<Metadata *> Ops)
3056 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
3057 ~DIGlobalVariableExpression() = default;
3059 static DIGlobalVariableExpression *
3060 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
3061 StorageType Storage, bool ShouldCreate = true);
3063 TempDIGlobalVariableExpression cloneImpl() const {
3064 return getTemporary(getContext(), getVariable(), getExpression());
3067 public:
3068 DEFINE_MDNODE_GET(DIGlobalVariableExpression,
3069 (Metadata * Variable, Metadata *Expression),
3070 (Variable, Expression))
3072 TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
3074 Metadata *getRawVariable() const { return getOperand(0); }
3076 DIGlobalVariable *getVariable() const {
3077 return cast_or_null<DIGlobalVariable>(getRawVariable());
3080 Metadata *getRawExpression() const { return getOperand(1); }
3082 DIExpression *getExpression() const {
3083 return cast<DIExpression>(getRawExpression());
3086 static bool classof(const Metadata *MD) {
3087 return MD->getMetadataID() == DIGlobalVariableExpressionKind;
3091 /// Macro Info DWARF-like metadata node.
3093 /// A metadata node with a DWARF macro info (i.e., a constant named
3094 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
3095 /// DIMacroNode
3096 /// because it's potentially used for non-DWARF output.
3097 class DIMacroNode : public MDNode {
3098 friend class LLVMContextImpl;
3099 friend class MDNode;
3101 protected:
3102 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
3103 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
3104 : MDNode(C, ID, Storage, Ops1, Ops2) {
3105 assert(MIType < 1u << 16);
3106 SubclassData16 = MIType;
3108 ~DIMacroNode() = default;
3110 template <class Ty> Ty *getOperandAs(unsigned I) const {
3111 return cast_or_null<Ty>(getOperand(I));
3114 StringRef getStringOperand(unsigned I) const {
3115 if (auto *S = getOperandAs<MDString>(I))
3116 return S->getString();
3117 return StringRef();
3120 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
3121 if (S.empty())
3122 return nullptr;
3123 return MDString::get(Context, S);
3126 public:
3127 unsigned getMacinfoType() const { return SubclassData16; }
3129 static bool classof(const Metadata *MD) {
3130 switch (MD->getMetadataID()) {
3131 default:
3132 return false;
3133 case DIMacroKind:
3134 case DIMacroFileKind:
3135 return true;
3140 class DIMacro : public DIMacroNode {
3141 friend class LLVMContextImpl;
3142 friend class MDNode;
3144 unsigned Line;
3146 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
3147 ArrayRef<Metadata *> Ops)
3148 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
3149 ~DIMacro() = default;
3151 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3152 StringRef Name, StringRef Value, StorageType Storage,
3153 bool ShouldCreate = true) {
3154 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
3155 getCanonicalMDString(Context, Value), Storage, ShouldCreate);
3157 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3158 MDString *Name, MDString *Value, StorageType Storage,
3159 bool ShouldCreate = true);
3161 TempDIMacro cloneImpl() const {
3162 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
3163 getValue());
3166 public:
3167 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
3168 StringRef Value = ""),
3169 (MIType, Line, Name, Value))
3170 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
3171 MDString *Value),
3172 (MIType, Line, Name, Value))
3174 TempDIMacro clone() const { return cloneImpl(); }
3176 unsigned getLine() const { return Line; }
3178 StringRef getName() const { return getStringOperand(0); }
3179 StringRef getValue() const { return getStringOperand(1); }
3181 MDString *getRawName() const { return getOperandAs<MDString>(0); }
3182 MDString *getRawValue() const { return getOperandAs<MDString>(1); }
3184 static bool classof(const Metadata *MD) {
3185 return MD->getMetadataID() == DIMacroKind;
3189 class DIMacroFile : public DIMacroNode {
3190 friend class LLVMContextImpl;
3191 friend class MDNode;
3193 unsigned Line;
3195 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
3196 unsigned Line, ArrayRef<Metadata *> Ops)
3197 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
3198 ~DIMacroFile() = default;
3200 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
3201 unsigned Line, DIFile *File,
3202 DIMacroNodeArray Elements, StorageType Storage,
3203 bool ShouldCreate = true) {
3204 return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
3205 Elements.get(), Storage, ShouldCreate);
3208 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
3209 unsigned Line, Metadata *File, Metadata *Elements,
3210 StorageType Storage, bool ShouldCreate = true);
3212 TempDIMacroFile cloneImpl() const {
3213 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
3214 getElements());
3217 public:
3218 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
3219 DIMacroNodeArray Elements),
3220 (MIType, Line, File, Elements))
3221 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
3222 Metadata *File, Metadata *Elements),
3223 (MIType, Line, File, Elements))
3225 TempDIMacroFile clone() const { return cloneImpl(); }
3227 void replaceElements(DIMacroNodeArray Elements) {
3228 #ifndef NDEBUG
3229 for (DIMacroNode *Op : getElements())
3230 assert(is_contained(Elements->operands(), Op) &&
3231 "Lost a macro node during macro node list replacement");
3232 #endif
3233 replaceOperandWith(1, Elements.get());
3236 unsigned getLine() const { return Line; }
3237 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3239 DIMacroNodeArray getElements() const {
3240 return cast_or_null<MDTuple>(getRawElements());
3243 Metadata *getRawFile() const { return getOperand(0); }
3244 Metadata *getRawElements() const { return getOperand(1); }
3246 static bool classof(const Metadata *MD) {
3247 return MD->getMetadataID() == DIMacroFileKind;
3251 } // end namespace llvm
3253 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
3254 #undef DEFINE_MDNODE_GET_UNPACK
3255 #undef DEFINE_MDNODE_GET
3257 #endif // LLVM_IR_DEBUGINFOMETADATA_H