LLVM 17.0.0git
DebugInfoMetadata.h
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_IR_DEBUGINFOMETADATA_H
14#define LLVM_IR_DEBUGINFOMETADATA_H
15
16#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/Metadata.h"
28#include <cassert>
29#include <climits>
30#include <cstddef>
31#include <cstdint>
32#include <iterator>
33#include <optional>
34#include <vector>
35
36// Helper macros for defining get() overrides.
37#define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
38#define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
39#define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
40 static CLASS *getDistinct(LLVMContext &Context, \
41 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
42 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
43 } \
44 static Temp##CLASS getTemporary(LLVMContext &Context, \
45 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
46 return Temp##CLASS( \
47 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
48 }
49#define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
50 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
51 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
52 } \
53 static CLASS *getIfExists(LLVMContext &Context, \
54 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
55 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
56 /* ShouldCreate */ false); \
57 } \
58 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
59
60namespace llvm {
61
62namespace dwarf {
63enum Tag : uint16_t;
64}
65
66class DbgVariableIntrinsic;
67
68extern cl::opt<bool> EnableFSDiscriminator;
69
71 const MDTuple *N = nullptr;
72
73public:
74 DITypeRefArray() = default;
75 DITypeRefArray(const MDTuple *N) : N(N) {}
76
77 explicit operator bool() const { return get(); }
78 explicit operator MDTuple *() const { return get(); }
79
80 MDTuple *get() const { return const_cast<MDTuple *>(N); }
81 MDTuple *operator->() const { return get(); }
82 MDTuple &operator*() const { return *get(); }
83
84 // FIXME: Fix callers and remove condition on N.
85 unsigned size() const { return N ? N->getNumOperands() : 0u; }
86 DIType *operator[](unsigned I) const {
87 return cast_or_null<DIType>(N->getOperand(I));
88 }
89
90 class iterator {
91 MDNode::op_iterator I = nullptr;
92
93 public:
94 using iterator_category = std::input_iterator_tag;
95 using value_type = DIType *;
96 using difference_type = std::ptrdiff_t;
97 using pointer = void;
98 using reference = DIType *;
99
100 iterator() = default;
101 explicit iterator(MDNode::op_iterator I) : I(I) {}
102
103 DIType *operator*() const { return cast_or_null<DIType>(*I); }
104
106 ++I;
107 return *this;
108 }
109
111 iterator Temp(*this);
112 ++I;
113 return Temp;
114 }
115
116 bool operator==(const iterator &X) const { return I == X.I; }
117 bool operator!=(const iterator &X) const { return I != X.I; }
118 };
119
120 // FIXME: Fix callers and remove condition on N.
121 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
122 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
123};
124
125/// Tagged DWARF-like metadata node.
126///
127/// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
128/// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
129/// potentially used for non-DWARF output.
130class DINode : public MDNode {
131 friend class LLVMContextImpl;
132 friend class MDNode;
133
134protected:
135 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
136 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = std::nullopt)
137 : MDNode(C, ID, Storage, Ops1, Ops2) {
138 assert(Tag < 1u << 16);
140 }
141 ~DINode() = default;
142
143 template <class Ty> Ty *getOperandAs(unsigned I) const {
144 return cast_or_null<Ty>(getOperand(I));
145 }
146
147 StringRef getStringOperand(unsigned I) const {
148 if (auto *S = getOperandAs<MDString>(I))
149 return S->getString();
150 return StringRef();
151 }
152
154 if (S.empty())
155 return nullptr;
156 return MDString::get(Context, S);
157 }
158
159 /// Allow subclasses to mutate the tag.
160 void setTag(unsigned Tag) { SubclassData16 = Tag; }
161
162public:
163 dwarf::Tag getTag() const;
164
165 /// Debug info flags.
166 ///
167 /// The three accessibility flags are mutually exclusive and rolled together
168 /// in the first two bits.
170#define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
171#define DI_FLAG_LARGEST_NEEDED
172#include "llvm/IR/DebugInfoFlags.def"
173 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
174 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
175 FlagVirtualInheritance,
176 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
177 };
178
179 static DIFlags getFlag(StringRef Flag);
180 static StringRef getFlagString(DIFlags Flag);
181
182 /// Split up a flags bitfield.
183 ///
184 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
185 /// any remaining (unrecognized) bits.
186 static DIFlags splitFlags(DIFlags Flags,
187 SmallVectorImpl<DIFlags> &SplitFlags);
188
189 static bool classof(const Metadata *MD) {
190 switch (MD->getMetadataID()) {
191 default:
192 return false;
193 case GenericDINodeKind:
194 case DISubrangeKind:
195 case DIEnumeratorKind:
196 case DIBasicTypeKind:
197 case DIStringTypeKind:
198 case DIDerivedTypeKind:
199 case DICompositeTypeKind:
200 case DISubroutineTypeKind:
201 case DIFileKind:
202 case DICompileUnitKind:
203 case DISubprogramKind:
204 case DILexicalBlockKind:
205 case DILexicalBlockFileKind:
206 case DINamespaceKind:
207 case DICommonBlockKind:
208 case DITemplateTypeParameterKind:
209 case DITemplateValueParameterKind:
210 case DIGlobalVariableKind:
211 case DILocalVariableKind:
212 case DILabelKind:
213 case DIObjCPropertyKind:
214 case DIImportedEntityKind:
215 case DIModuleKind:
216 case DIGenericSubrangeKind:
217 case DIAssignIDKind:
218 return true;
219 }
220 }
221};
222
223/// Generic tagged DWARF-like metadata node.
224///
225/// An un-specialized DWARF-like metadata node. The first operand is a
226/// (possibly empty) null-separated \a MDString header that contains arbitrary
227/// fields. The remaining operands are \a dwarf_operands(), and are pointers
228/// to other metadata.
229class GenericDINode : public DINode {
230 friend class LLVMContextImpl;
231 friend class MDNode;
232
234 unsigned Tag, ArrayRef<Metadata *> Ops1,
236 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
237 setHash(Hash);
238 }
240
241 void setHash(unsigned Hash) { SubclassData32 = Hash; }
242 void recalculateHash();
243
244 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
245 StringRef Header, ArrayRef<Metadata *> DwarfOps,
246 StorageType Storage, bool ShouldCreate = true) {
247 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
248 DwarfOps, Storage, ShouldCreate);
249 }
250
251 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
252 MDString *Header, ArrayRef<Metadata *> DwarfOps,
253 StorageType Storage, bool ShouldCreate = true);
254
255 TempGenericDINode cloneImpl() const {
257 SmallVector<Metadata *, 4>(dwarf_operands()));
258 }
259
260public:
261 unsigned getHash() const { return SubclassData32; }
262
264 (unsigned Tag, StringRef Header,
266 (Tag, Header, DwarfOps))
268 (unsigned Tag, MDString *Header,
271
272 /// Return a (temporary) clone of this.
273 TempGenericDINode clone() const { return cloneImpl(); }
274
275 dwarf::Tag getTag() const;
276 StringRef getHeader() const { return getStringOperand(0); }
277 MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
278
279 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
280 op_iterator dwarf_op_end() const { return op_end(); }
283 }
284
285 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
286 const MDOperand &getDwarfOperand(unsigned I) const {
287 return getOperand(I + 1);
288 }
289 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
290 replaceOperandWith(I + 1, New);
291 }
292
293 static bool classof(const Metadata *MD) {
294 return MD->getMetadataID() == GenericDINodeKind;
295 }
296};
297
298/// Assignment ID.
299/// Used to link stores (as an attachment) and dbg.assigns (as an operand).
300/// DIAssignID metadata is never uniqued as we compare instances using
301/// referential equality (the instance/address is the ID).
302class DIAssignID : public MDNode {
303 friend class LLVMContextImpl;
304 friend class MDNode;
305
307 : MDNode(C, DIAssignIDKind, Storage, std::nullopt) {}
308
310
311 static DIAssignID *getImpl(LLVMContext &Context, StorageType Storage,
312 bool ShouldCreate = true);
313
314 TempDIAssignID cloneImpl() const { return getTemporary(getContext()); }
315
316public:
317 // This node has no operands to replace.
318 void replaceOperandWith(unsigned I, Metadata *New) = delete;
319
321 return getImpl(Context, Distinct);
322 }
323 static TempDIAssignID getTemporary(LLVMContext &Context) {
324 return TempDIAssignID(getImpl(Context, Temporary));
325 }
326 // NOTE: Do not define get(LLVMContext&) - see class comment.
327
328 static bool classof(const Metadata *MD) {
329 return MD->getMetadataID() == DIAssignIDKind;
330 }
331};
332
333/// Array subrange.
334///
335/// TODO: Merge into node for DW_TAG_array_type, which should have a custom
336/// type.
337class DISubrange : public DINode {
338 friend class LLVMContextImpl;
339 friend class MDNode;
340
342
343 ~DISubrange() = default;
344
345 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
347 bool ShouldCreate = true);
348
351 bool ShouldCreate = true);
352
354 Metadata *LowerBound, Metadata *UpperBound,
356 bool ShouldCreate = true);
357
358 TempDISubrange cloneImpl() const {
359 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
360 getRawUpperBound(), getRawStride());
361 }
362
363public:
364 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
365 (Count, LowerBound))
366
369
372 Metadata *UpperBound, Metadata *Stride),
373 (CountNode, LowerBound, UpperBound, Stride))
374
375 TempDISubrange clone() const { return cloneImpl(); }
376
377 Metadata *getRawCountNode() const { return getOperand(0).get(); }
378
379 Metadata *getRawLowerBound() const { return getOperand(1).get(); }
380
381 Metadata *getRawUpperBound() const { return getOperand(2).get(); }
382
383 Metadata *getRawStride() const { return getOperand(3).get(); }
384
385 typedef PointerUnion<ConstantInt *, DIVariable *, DIExpression *> BoundType;
386
387 BoundType getCount() const;
388
389 BoundType getLowerBound() const;
390
391 BoundType getUpperBound() const;
392
393 BoundType getStride() const;
394
395 static bool classof(const Metadata *MD) {
396 return MD->getMetadataID() == DISubrangeKind;
397 }
398};
399
400class DIGenericSubrange : public DINode {
401 friend class LLVMContextImpl;
402 friend class MDNode;
403
406
407 ~DIGenericSubrange() = default;
408
409 static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
410 Metadata *LowerBound, Metadata *UpperBound,
412 bool ShouldCreate = true);
413
414 TempDIGenericSubrange cloneImpl() const {
417 }
418
419public:
421 (Metadata * CountNode, Metadata *LowerBound,
422 Metadata *UpperBound, Metadata *Stride),
423 (CountNode, LowerBound, UpperBound, Stride))
424
425 TempDIGenericSubrange clone() const { return cloneImpl(); }
426
427 Metadata *getRawCountNode() const { return getOperand(0).get(); }
428 Metadata *getRawLowerBound() const { return getOperand(1).get(); }
429 Metadata *getRawUpperBound() const { return getOperand(2).get(); }
430 Metadata *getRawStride() const { return getOperand(3).get(); }
431
433
434 BoundType getCount() const;
435 BoundType getLowerBound() const;
436 BoundType getUpperBound() const;
437 BoundType getStride() const;
438
439 static bool classof(const Metadata *MD) {
440 return MD->getMetadataID() == DIGenericSubrangeKind;
441 }
442};
443
444/// Enumeration value.
445///
446/// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
447/// longer creates a type cycle.
448class DIEnumerator : public DINode {
449 friend class LLVMContextImpl;
450 friend class MDNode;
451
452 APInt Value;
458 Ops) {}
459 ~DIEnumerator() = default;
460
461 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
463 StorageType Storage, bool ShouldCreate = true) {
464 return getImpl(Context, Value, IsUnsigned,
465 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
466 }
467 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
468 bool IsUnsigned, MDString *Name,
469 StorageType Storage, bool ShouldCreate = true);
470
471 TempDIEnumerator cloneImpl() const {
473 }
474
475public:
477 (int64_t Value, bool IsUnsigned, StringRef Name),
480 (int64_t Value, bool IsUnsigned, MDString *Name),
488
489 TempDIEnumerator clone() const { return cloneImpl(); }
490
491 const APInt &getValue() const { return Value; }
492 bool isUnsigned() const { return SubclassData32; }
493 StringRef getName() const { return getStringOperand(0); }
494
495 MDString *getRawName() const { return getOperandAs<MDString>(0); }
496
497 static bool classof(const Metadata *MD) {
498 return MD->getMetadataID() == DIEnumeratorKind;
499 }
500};
501
502/// Base class for scope-like contexts.
503///
504/// Base class for lexical scopes and types (which are also declaration
505/// contexts).
506///
507/// TODO: Separate the concepts of declaration contexts and lexical scopes.
508class DIScope : public DINode {
509protected:
512 : DINode(C, ID, Storage, Tag, Ops) {}
513 ~DIScope() = default;
514
515public:
516 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
517
518 inline StringRef getFilename() const;
519 inline StringRef getDirectory() const;
520 inline std::optional<StringRef> getSource() const;
521
522 StringRef getName() const;
523 DIScope *getScope() const;
524
525 /// Return the raw underlying file.
526 ///
527 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
528 /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
529 /// Otherwise, return the first operand, which is where all other subclasses
530 /// store their file pointer.
532 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
533 : static_cast<Metadata *>(getOperand(0));
534 }
535
536 static bool classof(const Metadata *MD) {
537 switch (MD->getMetadataID()) {
538 default:
539 return false;
540 case DIBasicTypeKind:
541 case DIStringTypeKind:
542 case DIDerivedTypeKind:
543 case DICompositeTypeKind:
544 case DISubroutineTypeKind:
545 case DIFileKind:
546 case DICompileUnitKind:
547 case DISubprogramKind:
548 case DILexicalBlockKind:
549 case DILexicalBlockFileKind:
550 case DINamespaceKind:
551 case DICommonBlockKind:
552 case DIModuleKind:
553 return true;
554 }
555 }
556};
557
558/// File.
559///
560/// TODO: Merge with directory/file node (including users).
561/// TODO: Canonicalize paths on creation.
562class DIFile : public DIScope {
563 friend class LLVMContextImpl;
564 friend class MDNode;
565
566public:
567 /// Which algorithm (e.g. MD5) a checksum was generated with.
568 ///
569 /// The encoding is explicit because it is used directly in Bitcode. The
570 /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
572 // The first variant was originally CSK_None, encoded as 0. The new
573 // internal representation removes the need for this by wrapping the
574 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
575 // encoding is reserved.
579 CSK_Last = CSK_SHA256 // Should be last enumeration.
580 };
581
582 /// A single checksum, represented by a \a Kind and a \a Value (a string).
583 template <typename T> struct ChecksumInfo {
584 /// The kind of checksum which \a Value encodes.
586 /// The string value of the checksum.
588
590 ~ChecksumInfo() = default;
591 bool operator==(const ChecksumInfo<T> &X) const {
592 return Kind == X.Kind && Value == X.Value;
593 }
594 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
595 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
596 };
597
598private:
599 std::optional<ChecksumInfo<MDString *>> Checksum;
600 /// An optional source. A nullptr means none.
601 MDString *Source;
602
604 std::optional<ChecksumInfo<MDString *>> CS, MDString *Src,
606 ~DIFile() = default;
607
608 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
610 std::optional<ChecksumInfo<StringRef>> CS,
611 std::optional<StringRef> Source, StorageType Storage,
612 bool ShouldCreate = true) {
613 std::optional<ChecksumInfo<MDString *>> MDChecksum;
614 if (CS)
615 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
616 return getImpl(Context, getCanonicalMDString(Context, Filename),
617 getCanonicalMDString(Context, Directory), MDChecksum,
618 Source ? MDString::get(Context, *Source) : nullptr, Storage,
619 ShouldCreate);
620 }
621 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
622 MDString *Directory,
623 std::optional<ChecksumInfo<MDString *>> CS,
624 MDString *Source, StorageType Storage,
625 bool ShouldCreate = true);
626
627 TempDIFile cloneImpl() const {
629 getChecksum(), getSource());
630 }
631
632public:
635 std::optional<ChecksumInfo<StringRef>> CS = std::nullopt,
636 std::optional<StringRef> Source = std::nullopt),
637 (Filename, Directory, CS, Source))
640 std::optional<ChecksumInfo<MDString *>> CS = std::nullopt,
641 MDString *Source = nullptr),
642 (Filename, Directory, CS, Source))
643
644 TempDIFile clone() const { return cloneImpl(); }
645
646 StringRef getFilename() const { return getStringOperand(0); }
647 StringRef getDirectory() const { return getStringOperand(1); }
648 std::optional<ChecksumInfo<StringRef>> getChecksum() const {
649 std::optional<ChecksumInfo<StringRef>> StringRefChecksum;
650 if (Checksum)
651 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
652 return StringRefChecksum;
653 }
654 std::optional<StringRef> getSource() const {
655 return Source ? std::optional<StringRef>(Source->getString())
656 : std::nullopt;
657 }
658
659 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
660 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
661 std::optional<ChecksumInfo<MDString *>> getRawChecksum() const {
662 return Checksum;
663 }
664 MDString *getRawSource() const { return Source; }
665
666 static StringRef getChecksumKindAsString(ChecksumKind CSKind);
667 static std::optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
668
669 static bool classof(const Metadata *MD) {
670 return MD->getMetadataID() == DIFileKind;
671 }
672};
673
675 if (auto *F = getFile())
676 return F->getFilename();
677 return "";
678}
679
681 if (auto *F = getFile())
682 return F->getDirectory();
683 return "";
684}
685
686std::optional<StringRef> DIScope::getSource() const {
687 if (auto *F = getFile())
688 return F->getSource();
689 return std::nullopt;
690}
691
692/// Base class for types.
693///
694/// TODO: Remove the hardcoded name and context, since many types don't use
695/// them.
696/// TODO: Split up flags.
697class DIType : public DIScope {
698 unsigned Line;
699 DIFlags Flags;
700 uint64_t SizeInBits;
701 uint64_t OffsetInBits;
702 uint32_t AlignInBits;
703
704protected:
705 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
706 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
707 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
708 : DIScope(C, ID, Storage, Tag, Ops) {
709 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
710 }
711 ~DIType() = default;
712
713 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
714 uint64_t OffsetInBits, DIFlags Flags) {
715 this->Line = Line;
716 this->Flags = Flags;
717 this->SizeInBits = SizeInBits;
718 this->AlignInBits = AlignInBits;
719 this->OffsetInBits = OffsetInBits;
720 }
721
722 /// Change fields in place.
723 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
724 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
725 assert(isDistinct() && "Only distinct nodes can mutate");
726 setTag(Tag);
727 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
728 }
729
730public:
731 TempDIType clone() const {
732 return TempDIType(cast<DIType>(MDNode::clone().release()));
733 }
734
735 unsigned getLine() const { return Line; }
736 uint64_t getSizeInBits() const { return SizeInBits; }
737 uint32_t getAlignInBits() const { return AlignInBits; }
738 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
739 uint64_t getOffsetInBits() const { return OffsetInBits; }
740 DIFlags getFlags() const { return Flags; }
741
742 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
743 StringRef getName() const { return getStringOperand(2); }
744
745 Metadata *getRawScope() const { return getOperand(1); }
746 MDString *getRawName() const { return getOperandAs<MDString>(2); }
747
748 /// Returns a new temporary DIType with updated Flags
749 TempDIType cloneWithFlags(DIFlags NewFlags) const {
750 auto NewTy = clone();
751 NewTy->Flags = NewFlags;
752 return NewTy;
753 }
754
755 bool isPrivate() const {
756 return (getFlags() & FlagAccessibility) == FlagPrivate;
757 }
758 bool isProtected() const {
759 return (getFlags() & FlagAccessibility) == FlagProtected;
760 }
761 bool isPublic() const {
762 return (getFlags() & FlagAccessibility) == FlagPublic;
763 }
764 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
765 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
766 bool isVirtual() const { return getFlags() & FlagVirtual; }
767 bool isArtificial() const { return getFlags() & FlagArtificial; }
768 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
769 bool isObjcClassComplete() const {
770 return getFlags() & FlagObjcClassComplete;
771 }
772 bool isVector() const { return getFlags() & FlagVector; }
773 bool isBitField() const { return getFlags() & FlagBitField; }
774 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
775 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
776 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
777 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
779 return getFlags() & FlagTypePassByReference;
780 }
781 bool isBigEndian() const { return getFlags() & FlagBigEndian; }
782 bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
783 bool getExportSymbols() const { return getFlags() & FlagExportSymbols; }
784
785 static bool classof(const Metadata *MD) {
786 switch (MD->getMetadataID()) {
787 default:
788 return false;
789 case DIBasicTypeKind:
790 case DIStringTypeKind:
791 case DIDerivedTypeKind:
792 case DICompositeTypeKind:
793 case DISubroutineTypeKind:
794 return true;
795 }
796 }
797};
798
799/// Basic type, like 'int' or 'float'.
800///
801/// TODO: Split out DW_TAG_unspecified_type.
802/// TODO: Drop unused accessors.
803class DIBasicType : public DIType {
804 friend class LLVMContextImpl;
805 friend class MDNode;
806
807 unsigned Encoding;
808
810 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
812 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
813 Flags, Ops),
814 Encoding(Encoding) {}
815 ~DIBasicType() = default;
816
817 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
819 uint32_t AlignInBits, unsigned Encoding,
821 bool ShouldCreate = true) {
822 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
823 SizeInBits, AlignInBits, Encoding, Flags, Storage,
824 ShouldCreate);
825 }
826 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
827 MDString *Name, uint64_t SizeInBits,
828 uint32_t AlignInBits, unsigned Encoding,
830 bool ShouldCreate = true);
831
832 TempDIBasicType cloneImpl() const {
835 }
836
837public:
839 (Tag, Name, 0, 0, 0, FlagZero))
842 (Tag, Name, SizeInBits, 0, 0, FlagZero))
844 (unsigned Tag, MDString *Name, uint64_t SizeInBits),
845 (Tag, Name, SizeInBits, 0, 0, FlagZero))
848 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
851 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
852 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
853 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
854
855 TempDIBasicType clone() const { return cloneImpl(); }
856
857 unsigned getEncoding() const { return Encoding; }
858
859 enum class Signedness { Signed, Unsigned };
860
861 /// Return the signedness of this type, or std::nullopt if this type is
862 /// neither signed nor unsigned.
863 std::optional<Signedness> getSignedness() const;
864
865 static bool classof(const Metadata *MD) {
866 return MD->getMetadataID() == DIBasicTypeKind;
867 }
868};
869
870/// String type, Fortran CHARACTER(n)
871class DIStringType : public DIType {
872 friend class LLVMContextImpl;
873 friend class MDNode;
874
875 unsigned Encoding;
876
878 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
880 : DIType(C, DIStringTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
881 FlagZero, Ops),
882 Encoding(Encoding) {}
883 ~DIStringType() = default;
884
885 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
887 Metadata *StrLenExp, Metadata *StrLocationExp,
889 unsigned Encoding, StorageType Storage,
890 bool ShouldCreate = true) {
891 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
892 StringLength, StrLenExp, StrLocationExp, SizeInBits,
893 AlignInBits, Encoding, Storage, ShouldCreate);
894 }
895 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
896 MDString *Name, Metadata *StringLength,
897 Metadata *StrLenExp, Metadata *StrLocationExp,
899 unsigned Encoding, StorageType Storage,
900 bool ShouldCreate = true);
901
902 TempDIStringType cloneImpl() const {
907 }
908
909public:
913 (Tag, Name, nullptr, nullptr, nullptr, SizeInBits,
914 AlignInBits, 0))
919 unsigned Encoding),
926 unsigned Encoding),
929
930 TempDIStringType clone() const { return cloneImpl(); }
931
932 static bool classof(const Metadata *MD) {
933 return MD->getMetadataID() == DIStringTypeKind;
934 }
935
937 return cast_or_null<DIVariable>(getRawStringLength());
938 }
939
941 return cast_or_null<DIExpression>(getRawStringLengthExp());
942 }
943
945 return cast_or_null<DIExpression>(getRawStringLocationExp());
946 }
947
948 unsigned getEncoding() const { return Encoding; }
949
950 Metadata *getRawStringLength() const { return getOperand(3); }
951
953
955};
956
957/// Derived types.
958///
959/// This includes qualified types, pointers, references, friends, typedefs, and
960/// class members.
961///
962/// TODO: Split out members (inheritance, fields, methods, etc.).
963class DIDerivedType : public DIType {
964 friend class LLVMContextImpl;
965 friend class MDNode;
966
967 /// The DWARF address space of the memory pointed to or referenced by a
968 /// pointer or reference type respectively.
969 std::optional<unsigned> DWARFAddressSpace;
970
974 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
976 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
978 DWARFAddressSpace(DWARFAddressSpace) {}
979 ~DIDerivedType() = default;
980 static DIDerivedType *
981 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
984 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
986 bool ShouldCreate = true) {
987 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
989 DWARFAddressSpace, Flags, ExtraData, Annotations.get(),
990 Storage, ShouldCreate);
991 }
992 static DIDerivedType *
993 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
994 unsigned Line, Metadata *Scope, Metadata *BaseType,
996 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
998 bool ShouldCreate = true);
999
1000 TempDIDerivedType cloneImpl() const {
1001 return getTemporary(
1004 getDWARFAddressSpace(), getFlags(), getExtraData(), getAnnotations());
1005 }
1006
1007public:
1010 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
1013 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
1014 Metadata *ExtraData = nullptr, Metadata *Annotations = nullptr),
1016 OffsetInBits, DWARFAddressSpace, Flags, ExtraData, Annotations))
1018 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
1021 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
1023 DINodeArray Annotations = nullptr),
1025 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
1027
1028 TempDIDerivedType clone() const { return cloneImpl(); }
1029
1030 /// Get the base type this is derived from.
1031 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1032 Metadata *getRawBaseType() const { return getOperand(3); }
1033
1034 /// \returns The DWARF address space of the memory pointed to or referenced by
1035 /// a pointer or reference type respectively.
1036 std::optional<unsigned> getDWARFAddressSpace() const {
1037 return DWARFAddressSpace;
1038 }
1039
1040 /// Get extra data associated with this derived type.
1041 ///
1042 /// Class type for pointer-to-members, objective-c property node for ivars,
1043 /// global constant wrapper for static members, or virtual base pointer offset
1044 /// for inheritance.
1045 ///
1046 /// TODO: Separate out types that need this extra operand: pointer-to-member
1047 /// types and member fields (static members and ivars).
1048 Metadata *getExtraData() const { return getRawExtraData(); }
1049 Metadata *getRawExtraData() const { return getOperand(4); }
1050
1051 /// Get annotations associated with this derived type.
1052 DINodeArray getAnnotations() const {
1053 return cast_or_null<MDTuple>(getRawAnnotations());
1054 }
1055 Metadata *getRawAnnotations() const { return getOperand(5); }
1056
1057 /// Get casted version of extra data.
1058 /// @{
1059 DIType *getClassType() const;
1060
1062 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
1063 }
1064
1065 uint32_t getVBPtrOffset() const;
1066
1068
1069 Constant *getConstant() const;
1070
1072 /// @}
1073
1074 static bool classof(const Metadata *MD) {
1075 return MD->getMetadataID() == DIDerivedTypeKind;
1076 }
1077};
1078
1079/// Composite types.
1080///
1081/// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
1082/// TODO: Create a custom, unrelated node for DW_TAG_array_type.
1083class DICompositeType : public DIType {
1084 friend class LLVMContextImpl;
1085 friend class MDNode;
1086
1087 unsigned RuntimeLang;
1088
1090 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
1093 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
1095 RuntimeLang(RuntimeLang) {}
1096 ~DICompositeType() = default;
1097
1098 /// Change fields in place.
1099 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
1101 DIFlags Flags) {
1102 assert(isDistinct() && "Only distinct nodes can mutate");
1103 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
1104 this->RuntimeLang = RuntimeLang;
1106 }
1107
1108 static DICompositeType *
1109 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
1110 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
1112 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
1113 DITemplateParameterArray TemplateParams, StringRef Identifier,
1114 DIDerivedType *Discriminator, Metadata *DataLocation,
1116 DINodeArray Annotations, StorageType Storage,
1117 bool ShouldCreate = true) {
1118 return getImpl(
1119 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
1121 RuntimeLang, VTableHolder, TemplateParams.get(),
1123 Associated, Allocated, Rank, Annotations.get(), Storage, ShouldCreate);
1124 }
1125 static DICompositeType *
1126 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
1127 unsigned Line, Metadata *Scope, Metadata *BaseType,
1129 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
1133 Metadata *Annotations, StorageType Storage, bool ShouldCreate = true);
1134
1135 TempDICompositeType cloneImpl() const {
1136 return getTemporary(
1143 }
1144
1145public:
1148 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
1151 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
1152 DITemplateParameterArray TemplateParams = nullptr,
1154 Metadata *DataLocation = nullptr, Metadata *Associated = nullptr,
1155 Metadata *Allocated = nullptr, Metadata *Rank = nullptr,
1156 DINodeArray Annotations = nullptr),
1160 Annotations))
1163 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
1166 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
1169 Metadata *Associated = nullptr, Metadata *Allocated = nullptr,
1170 Metadata *Rank = nullptr, Metadata *Annotations = nullptr),
1174 Annotations))
1175
1176 TempDICompositeType clone() const { return cloneImpl(); }
1177
1178 /// Get a DICompositeType with the given ODR identifier.
1179 ///
1180 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
1181 /// DICompositeType for the given ODR \c Identifier. If none exists, creates
1182 /// a new node.
1183 ///
1184 /// Else, returns \c nullptr.
1185 static DICompositeType *
1186 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1187 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1190 unsigned RuntimeLang, Metadata *VTableHolder,
1196
1197 /// Build a DICompositeType with the given ODR identifier.
1198 ///
1199 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
1200 /// it doesn't exist, creates a new one. If it does exist and \a
1201 /// isForwardDecl(), and the new arguments would be a definition, mutates the
1202 /// the type in place. In either case, returns the type.
1203 ///
1204 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
1205 /// nullptr.
1206 static DICompositeType *
1207 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1208 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1211 unsigned RuntimeLang, Metadata *VTableHolder,
1215
1216 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1217 DINodeArray getElements() const {
1218 return cast_or_null<MDTuple>(getRawElements());
1219 }
1221 return cast_or_null<DIType>(getRawVTableHolder());
1222 }
1223 DITemplateParameterArray getTemplateParams() const {
1224 return cast_or_null<MDTuple>(getRawTemplateParams());
1225 }
1227 unsigned getRuntimeLang() const { return RuntimeLang; }
1228
1229 Metadata *getRawBaseType() const { return getOperand(3); }
1230 Metadata *getRawElements() const { return getOperand(4); }
1233 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1236 return getOperandAs<DIDerivedType>(8);
1237 }
1240 return dyn_cast_or_null<DIVariable>(getRawDataLocation());
1241 }
1243 return dyn_cast_or_null<DIExpression>(getRawDataLocation());
1244 }
1245 Metadata *getRawAssociated() const { return getOperand(10); }
1247 return dyn_cast_or_null<DIVariable>(getRawAssociated());
1248 }
1250 return dyn_cast_or_null<DIExpression>(getRawAssociated());
1251 }
1252 Metadata *getRawAllocated() const { return getOperand(11); }
1254 return dyn_cast_or_null<DIVariable>(getRawAllocated());
1255 }
1257 return dyn_cast_or_null<DIExpression>(getRawAllocated());
1258 }
1259 Metadata *getRawRank() const { return getOperand(12); }
1261 if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank()))
1262 return dyn_cast_or_null<ConstantInt>(MD->getValue());
1263 return nullptr;
1264 }
1266 return dyn_cast_or_null<DIExpression>(getRawRank());
1267 }
1268
1269 Metadata *getRawAnnotations() const { return getOperand(13); }
1270 DINodeArray getAnnotations() const {
1271 return cast_or_null<MDTuple>(getRawAnnotations());
1272 }
1273
1274 /// Replace operands.
1275 ///
1276 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
1277 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
1278 /// of its movement if necessary.
1279 /// @{
1280 void replaceElements(DINodeArray Elements) {
1281#ifndef NDEBUG
1282 for (DINode *Op : getElements())
1283 assert(is_contained(Elements->operands(), Op) &&
1284 "Lost a member during member list replacement");
1285#endif
1286 replaceOperandWith(4, Elements.get());
1287 }
1288
1291 }
1292
1293 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
1295 }
1296 /// @}
1297
1298 static bool classof(const Metadata *MD) {
1299 return MD->getMetadataID() == DICompositeTypeKind;
1300 }
1301};
1302
1303/// Type array for a subprogram.
1304///
1305/// TODO: Fold the array of types in directly as operands.
1306class DISubroutineType : public DIType {
1307 friend class LLVMContextImpl;
1308 friend class MDNode;
1309
1310 /// The calling convention used with DW_AT_calling_convention. Actually of
1311 /// type dwarf::CallingConvention.
1312 uint8_t CC;
1313
1315 uint8_t CC, ArrayRef<Metadata *> Ops);
1316 ~DISubroutineType() = default;
1317
1319 uint8_t CC, DITypeRefArray TypeArray,
1321 bool ShouldCreate = true) {
1322 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
1323 }
1325 uint8_t CC, Metadata *TypeArray,
1327 bool ShouldCreate = true);
1328
1329 TempDISubroutineType cloneImpl() const {
1331 }
1332
1333public:
1336 (Flags, CC, TypeArray))
1340
1341 TempDISubroutineType clone() const { return cloneImpl(); }
1342 // Returns a new temporary DISubroutineType with updated CC
1343 TempDISubroutineType cloneWithCC(uint8_t CC) const {
1344 auto NewTy = clone();
1345 NewTy->CC = CC;
1346 return NewTy;
1347 }
1348
1349 uint8_t getCC() const { return CC; }
1350
1352 return cast_or_null<MDTuple>(getRawTypeArray());
1353 }
1354
1355 Metadata *getRawTypeArray() const { return getOperand(3); }
1356
1357 static bool classof(const Metadata *MD) {
1358 return MD->getMetadataID() == DISubroutineTypeKind;
1359 }
1360};
1361
1362/// Compile unit.
1363class DICompileUnit : public DIScope {
1364 friend class LLVMContextImpl;
1365 friend class MDNode;
1366
1367public:
1368 enum DebugEmissionKind : unsigned {
1375
1376 enum class DebugNameTableKind : unsigned {
1377 Default = 0,
1378 GNU = 1,
1379 None = 2,
1381 };
1382
1383 static std::optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1384 static const char *emissionKindString(DebugEmissionKind EK);
1385 static std::optional<DebugNameTableKind> getNameTableKind(StringRef Str);
1386 static const char *nameTableKindString(DebugNameTableKind PK);
1387
1388private:
1389 unsigned SourceLanguage;
1390 bool IsOptimized;
1391 unsigned RuntimeVersion;
1392 unsigned EmissionKind;
1393 uint64_t DWOId;
1394 bool SplitDebugInlining;
1395 bool DebugInfoForProfiling;
1396 unsigned NameTableKind;
1397 bool RangesBaseAddress;
1398
1400 bool IsOptimized, unsigned RuntimeVersion,
1401 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
1402 bool DebugInfoForProfiling, unsigned NameTableKind,
1403 bool RangesBaseAddress, ArrayRef<Metadata *> Ops);
1404 ~DICompileUnit() = default;
1405
1406 static DICompileUnit *
1407 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
1408 StringRef Producer, bool IsOptimized, StringRef Flags,
1409 unsigned RuntimeVersion, StringRef SplitDebugFilename,
1410 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
1411 DIScopeArray RetainedTypes,
1412 DIGlobalVariableExpressionArray GlobalVariables,
1413 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1414 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1415 unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot,
1416 StringRef SDK, StorageType Storage, bool ShouldCreate = true) {
1417 return getImpl(
1418 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
1419 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
1420 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
1421 EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
1422 ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
1423 DebugInfoForProfiling, NameTableKind, RangesBaseAddress,
1424 getCanonicalMDString(Context, SysRoot),
1425 getCanonicalMDString(Context, SDK), Storage, ShouldCreate);
1426 }
1427 static DICompileUnit *
1428 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1429 MDString *Producer, bool IsOptimized, MDString *Flags,
1430 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1431 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1433 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
1434 bool DebugInfoForProfiling, unsigned NameTableKind,
1435 bool RangesBaseAddress, MDString *SysRoot, MDString *SDK,
1436 StorageType Storage, bool ShouldCreate = true);
1437
1438 TempDICompileUnit cloneImpl() const {
1439 return getTemporary(
1446 }
1447
1448public:
1449 static void get() = delete;
1450 static void getIfExists() = delete;
1451
1455 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1457 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
1458 DIGlobalVariableExpressionArray GlobalVariables,
1459 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1460 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1461 DebugNameTableKind NameTableKind, bool RangesBaseAddress,
1463 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1465 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1466 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress,
1467 SysRoot, SDK))
1471 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1475 bool SplitDebugInlining, bool DebugInfoForProfiling,
1476 unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot,
1478 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1480 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1481 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK))
1482
1483 TempDICompileUnit clone() const { return cloneImpl(); }
1484
1485 unsigned getSourceLanguage() const { return SourceLanguage; }
1486 bool isOptimized() const { return IsOptimized; }
1487 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1489 return (DebugEmissionKind)EmissionKind;
1490 }
1492 return EmissionKind == DebugDirectivesOnly;
1493 }
1494 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
1496 return (DebugNameTableKind)NameTableKind;
1497 }
1498 bool getRangesBaseAddress() const { return RangesBaseAddress; }
1500 StringRef getFlags() const { return getStringOperand(2); }
1502 DICompositeTypeArray getEnumTypes() const {
1503 return cast_or_null<MDTuple>(getRawEnumTypes());
1504 }
1505 DIScopeArray getRetainedTypes() const {
1506 return cast_or_null<MDTuple>(getRawRetainedTypes());
1507 }
1508 DIGlobalVariableExpressionArray getGlobalVariables() const {
1509 return cast_or_null<MDTuple>(getRawGlobalVariables());
1510 }
1511 DIImportedEntityArray getImportedEntities() const {
1512 return cast_or_null<MDTuple>(getRawImportedEntities());
1513 }
1514 DIMacroNodeArray getMacros() const {
1515 return cast_or_null<MDTuple>(getRawMacros());
1516 }
1517 uint64_t getDWOId() const { return DWOId; }
1518 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
1519 bool getSplitDebugInlining() const { return SplitDebugInlining; }
1520 void setSplitDebugInlining(bool SplitDebugInlining) {
1521 this->SplitDebugInlining = SplitDebugInlining;
1522 }
1524 StringRef getSDK() const { return getStringOperand(10); }
1525
1526 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1527 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1529 return getOperandAs<MDString>(3);
1530 }
1531 Metadata *getRawEnumTypes() const { return getOperand(4); }
1535 Metadata *getRawMacros() const { return getOperand(8); }
1536 MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); }
1537 MDString *getRawSDK() const { return getOperandAs<MDString>(10); }
1538
1539 /// Replace arrays.
1540 ///
1541 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1542 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1543 /// DICompileUnit should be fairly rare.
1544 /// @{
1545 void replaceEnumTypes(DICompositeTypeArray N) {
1546 replaceOperandWith(4, N.get());
1547 }
1548 void replaceRetainedTypes(DITypeArray N) { replaceOperandWith(5, N.get()); }
1549 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
1550 replaceOperandWith(6, N.get());
1551 }
1552 void replaceImportedEntities(DIImportedEntityArray N) {
1553 replaceOperandWith(7, N.get());
1554 }
1555 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
1556 /// @}
1557
1558 static bool classof(const Metadata *MD) {
1559 return MD->getMetadataID() == DICompileUnitKind;
1560 }
1561};
1562
1563/// A scope for locals.
1564///
1565/// A legal scope for lexical blocks, local variables, and debug info
1566/// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1567/// DILexicalBlockFile.
1568class DILocalScope : public DIScope {
1569protected:
1572 : DIScope(C, ID, Storage, Tag, Ops) {}
1573 ~DILocalScope() = default;
1574
1575public:
1576 /// Get the subprogram for this scope.
1577 ///
1578 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1579 /// chain.
1580 DISubprogram *getSubprogram() const;
1581
1582 /// Traverses the scope chain rooted at RootScope until it hits a Subprogram,
1583 /// recreating the chain with "NewSP" instead.
1584 static DILocalScope *
1586 LLVMContext &Ctx,
1588
1589 /// Get the first non DILexicalBlockFile scope of this scope.
1590 ///
1591 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1592 /// scope chain.
1594
1595 static bool classof(const Metadata *MD) {
1596 return MD->getMetadataID() == DISubprogramKind ||
1597 MD->getMetadataID() == DILexicalBlockKind ||
1598 MD->getMetadataID() == DILexicalBlockFileKind;
1599 }
1600};
1601
1602/// Debug location.
1603///
1604/// A debug location in source code, used for debug info and otherwise.
1605class DILocation : public MDNode {
1606 friend class LLVMContextImpl;
1607 friend class MDNode;
1608
1610 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
1612
1613 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1614 unsigned Column, Metadata *Scope,
1616 StorageType Storage, bool ShouldCreate = true);
1617 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1618 unsigned Column, DILocalScope *Scope,
1620 StorageType Storage, bool ShouldCreate = true) {
1621 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1622 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
1623 ShouldCreate);
1624 }
1625
1626 TempDILocation cloneImpl() const {
1627 // Get the raw scope/inlinedAt since it is possible to invoke this on
1628 // a DILocation containing temporary metadata.
1629 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1630 getRawInlinedAt(), isImplicitCode());
1631 }
1632
1633public:
1634 // Disallow replacing operands.
1635 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1636
1638 (unsigned Line, unsigned Column, Metadata *Scope,
1639 Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
1642 (unsigned Line, unsigned Column, DILocalScope *Scope,
1644 bool ImplicitCode = false),
1646
1647 /// Return a (temporary) clone of this.
1648 TempDILocation clone() const { return cloneImpl(); }
1649
1650 unsigned getLine() const { return SubclassData32; }
1651 unsigned getColumn() const { return SubclassData16; }
1652 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1653
1654 DILocation *getInlinedAt() const {
1655 return cast_or_null<DILocation>(getRawInlinedAt());
1656 }
1657
1658 /// Check if the location corresponds to an implicit code.
1659 /// When the ImplicitCode flag is true, it means that the Instruction
1660 /// with this DILocation has been added by the front-end but it hasn't been
1661 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
1662 /// bracket). It's useful for code coverage to not show a counter on "empty"
1663 /// lines.
1664 bool isImplicitCode() const { return SubclassData1; }
1665 void setImplicitCode(bool ImplicitCode) { SubclassData1 = ImplicitCode; }
1666
1667 DIFile *getFile() const { return getScope()->getFile(); }
1668 StringRef getFilename() const { return getScope()->getFilename(); }
1669 StringRef getDirectory() const { return getScope()->getDirectory(); }
1670 std::optional<StringRef> getSource() const { return getScope()->getSource(); }
1671
1672 /// Get the scope where this is inlined.
1673 ///
1674 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1675 /// location.
1676 DILocalScope *getInlinedAtScope() const {
1677 if (auto *IA = getInlinedAt())
1678 return IA->getInlinedAtScope();
1679 return getScope();
1680 }
1681
1682 /// Get the DWARF discriminator.
1683 ///
1684 /// DWARF discriminators distinguish identical file locations between
1685 /// instructions that are on different basic blocks.
1686 ///
1687 /// There are 3 components stored in discriminator, from lower bits:
1688 ///
1689 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1690 /// that are defined by the same source line, but
1691 /// different basic blocks.
1692 /// Duplication factor: assigned by optimizations that will scale down
1693 /// the execution frequency of the original IR.
1694 /// Copy Identifier: assigned by optimizations that clones the IR.
1695 /// Each copy of the IR will be assigned an identifier.
1696 ///
1697 /// Encoding:
1698 ///
1699 /// The above 3 components are encoded into a 32bit unsigned integer in
1700 /// order. If the lowest bit is 1, the current component is empty, and the
1701 /// next component will start in the next bit. Otherwise, the current
1702 /// component is non-empty, and its content starts in the next bit. The
1703 /// value of each components is either 5 bit or 12 bit: if the 7th bit
1704 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1705 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1706 /// represent the component. Thus, the number of bits used for a component
1707 /// is either 0 (if it and all the next components are empty); 1 - if it is
1708 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1709 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1710 /// component is also capped at 0x1ff, even in the case when both first
1711 /// components are 0, and we'd technically have 29 bits available.
1712 ///
1713 /// For precise control over the data being encoded in the discriminator,
1714 /// use encodeDiscriminator/decodeDiscriminator.
1715
1716 inline unsigned getDiscriminator() const;
1717
1718 // For the regular discriminator, it stands for all empty components if all
1719 // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by
1720 // default). Here we fully leverage the higher 29 bits for pseudo probe use.
1721 // This is the format:
1722 // [2:0] - 0x7
1723 // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole
1724 // So if the lower 3 bits is non-zero and the others has at least one
1725 // non-zero bit, it guarantees to be a pseudo probe discriminator
1726 inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) {
1727 return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8);
1728 }
1729
1730 /// Returns a new DILocation with updated \p Discriminator.
1731 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
1732
1733 /// Returns a new DILocation with updated base discriminator \p BD. Only the
1734 /// base discriminator is set in the new DILocation, the other encoded values
1735 /// are elided.
1736 /// If the discriminator cannot be encoded, the function returns std::nullopt.
1737 inline std::optional<const DILocation *>
1738 cloneWithBaseDiscriminator(unsigned BD) const;
1739
1740 /// Returns the duplication factor stored in the discriminator, or 1 if no
1741 /// duplication factor (or 0) is encoded.
1742 inline unsigned getDuplicationFactor() const;
1743
1744 /// Returns the copy identifier stored in the discriminator.
1745 inline unsigned getCopyIdentifier() const;
1746
1747 /// Returns the base discriminator stored in the discriminator.
1748 inline unsigned getBaseDiscriminator() const;
1749
1750 /// Returns a new DILocation with duplication factor \p DF * current
1751 /// duplication factor encoded in the discriminator. The current duplication
1752 /// factor is as defined by getDuplicationFactor().
1753 /// Returns std::nullopt if encoding failed.
1754 inline std::optional<const DILocation *>
1756
1757 /// When two instructions are combined into a single instruction we also
1758 /// need to combine the original locations into a single location.
1759 /// When the locations are the same we can use either location.
1760 /// When they differ, we need a third location which is distinct from either.
1761 /// If they share a common scope, use this scope and compare the line/column
1762 /// pair of the locations with the common scope:
1763 /// * if both match, keep the line and column;
1764 /// * if only the line number matches, keep the line and set the column as 0;
1765 /// * otherwise set line and column as 0.
1766 /// If they do not share a common scope the location is ambiguous and can't be
1767 /// represented in a line entry. In this case, set line and column as 0 and
1768 /// use the scope of any location.
1769 ///
1770 /// \p LocA \p LocB: The locations to be merged.
1771 static const DILocation *getMergedLocation(const DILocation *LocA,
1772 const DILocation *LocB);
1773
1774 /// Try to combine the vector of locations passed as input in a single one.
1775 /// This function applies getMergedLocation() repeatedly left-to-right.
1776 ///
1777 /// \p Locs: The locations to be merged.
1778 static const DILocation *
1780
1781 /// Return the masked discriminator value for an input discrimnator value D
1782 /// (i.e. zero out the (B+1)-th and above bits for D (B is 0-base).
1783 // Example: an input of (0x1FF, 7) returns 0xFF.
1784 static unsigned getMaskedDiscriminator(unsigned D, unsigned B) {
1785 return (D & getN1Bits(B));
1786 }
1787
1788 /// Return the bits used for base discriminators.
1789 static unsigned getBaseDiscriminatorBits() { return getBaseFSBitEnd(); }
1790
1791 /// Returns the base discriminator for a given encoded discriminator \p D.
1792 static unsigned
1794 bool IsFSDiscriminator = false) {
1795 if (IsFSDiscriminator)
1798 }
1799
1800 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
1801 /// have certain special case behavior (e.g. treating empty duplication factor
1802 /// as the value '1').
1803 /// This API, in conjunction with cloneWithDiscriminator, may be used to
1804 /// encode the raw values provided.
1805 ///
1806 /// \p BD: base discriminator
1807 /// \p DF: duplication factor
1808 /// \p CI: copy index
1809 ///
1810 /// The return is std::nullopt if the values cannot be encoded in 32 bits -
1811 /// for example, values for BD or DF larger than 12 bits. Otherwise, the
1812 /// return is the encoded value.
1813 static std::optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF,
1814 unsigned CI);
1815
1816 /// Raw decoder for values in an encoded discriminator D.
1817 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
1818 unsigned &CI);
1819
1820 /// Returns the duplication factor for a given encoded discriminator \p D, or
1821 /// 1 if no value or 0 is encoded.
1822 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
1824 return 1;
1826 unsigned Ret = getUnsignedFromPrefixEncoding(D);
1827 if (Ret == 0)
1828 return 1;
1829 return Ret;
1830 }
1831
1832 /// Returns the copy identifier for a given encoded discriminator \p D.
1833 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
1836 }
1837
1838 Metadata *getRawScope() const { return getOperand(0); }
1840 if (getNumOperands() == 2)
1841 return getOperand(1);
1842 return nullptr;
1843 }
1844
1845 static bool classof(const Metadata *MD) {
1846 return MD->getMetadataID() == DILocationKind;
1847 }
1848};
1849
1850/// Subprogram description.
1852 friend class LLVMContextImpl;
1853 friend class MDNode;
1854
1855 unsigned Line;
1856 unsigned ScopeLine;
1857 unsigned VirtualIndex;
1858
1859 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1860 /// of method overrides from secondary bases by this amount. It may be
1861 /// negative.
1862 int ThisAdjustment;
1863
1864public:
1865 /// Debug info subprogram flags.
1867#define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
1868#define DISP_FLAG_LARGEST_NEEDED
1869#include "llvm/IR/DebugInfoFlags.def"
1870 SPFlagNonvirtual = SPFlagZero,
1871 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
1872 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
1873 };
1874
1875 static DISPFlags getFlag(StringRef Flag);
1876 static StringRef getFlagString(DISPFlags Flag);
1877
1878 /// Split up a flags bitfield for easier printing.
1879 ///
1880 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
1881 /// any remaining (unrecognized) bits.
1882 static DISPFlags splitFlags(DISPFlags Flags,
1883 SmallVectorImpl<DISPFlags> &SplitFlags);
1884
1885 // Helper for converting old bitfields to new flags word.
1886 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
1887 bool IsOptimized,
1888 unsigned Virtuality = SPFlagNonvirtual,
1889 bool IsMainSubprogram = false);
1890
1891private:
1892 DIFlags Flags;
1893 DISPFlags SPFlags;
1894
1895 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1896 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
1897 DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops);
1898 ~DISubprogram() = default;
1899
1900 static DISubprogram *
1902 StringRef LinkageName, DIFile *File, unsigned Line,
1903 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
1904 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1905 DISPFlags SPFlags, DICompileUnit *Unit,
1906 DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1907 DINodeArray RetainedNodes, DITypeArray ThrownTypes,
1909 StorageType Storage, bool ShouldCreate = true) {
1910 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1911 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1912 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1913 Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
1914 RetainedNodes.get(), ThrownTypes.get(), Annotations.get(),
1916 Storage, ShouldCreate);
1917 }
1918 static DISubprogram *
1919 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1920 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1921 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex,
1922 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1926 bool ShouldCreate = true);
1927
1928 TempDISubprogram cloneImpl() const {
1930 getFile(), getLine(), getType(), getScopeLine(),
1931 getContainingType(), getVirtualIndex(),
1932 getThisAdjustment(), getFlags(), getSPFlags(),
1933 getUnit(), getTemplateParams(), getDeclaration(),
1934 getRetainedNodes(), getThrownTypes(), getAnnotations(),
1935 getTargetFuncName());
1936 }
1937
1938public:
1942 unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
1943 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1944 DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
1945 DITemplateParameterArray TemplateParams = nullptr,
1946 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
1947 DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr,
1949 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1950 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1952
1956 unsigned Line, Metadata *Type, unsigned ScopeLine,
1957 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
1958 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
1962 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
1963 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
1965
1966 TempDISubprogram clone() const { return cloneImpl(); }
1967
1968 /// Returns a new temporary DISubprogram with updated Flags
1969 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
1970 auto NewSP = clone();
1971 NewSP->Flags = NewFlags;
1972 return NewSP;
1973 }
1974
1975public:
1976 unsigned getLine() const { return Line; }
1977 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
1978 unsigned getVirtualIndex() const { return VirtualIndex; }
1979 int getThisAdjustment() const { return ThisAdjustment; }
1980 unsigned getScopeLine() const { return ScopeLine; }
1981 void setScopeLine(unsigned L) {
1982 assert(isDistinct());
1983 ScopeLine = L;
1984 }
1985 DIFlags getFlags() const { return Flags; }
1986 DISPFlags getSPFlags() const { return SPFlags; }
1987 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
1988 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
1989 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
1990 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
1991
1992 bool isArtificial() const { return getFlags() & FlagArtificial; }
1993 bool isPrivate() const {
1994 return (getFlags() & FlagAccessibility) == FlagPrivate;
1995 }
1996 bool isProtected() const {
1997 return (getFlags() & FlagAccessibility) == FlagProtected;
1998 }
1999 bool isPublic() const {
2000 return (getFlags() & FlagAccessibility) == FlagPublic;
2001 }
2002 bool isExplicit() const { return getFlags() & FlagExplicit; }
2003 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
2004 bool areAllCallsDescribed() const {
2005 return getFlags() & FlagAllCallsDescribed;
2006 }
2007 bool isPure() const { return getSPFlags() & SPFlagPure; }
2008 bool isElemental() const { return getSPFlags() & SPFlagElemental; }
2009 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
2010 bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; }
2011
2012 /// Check if this is deleted member function.
2013 ///
2014 /// Return true if this subprogram is a C++11 special
2015 /// member function declared deleted.
2016 bool isDeleted() const { return getSPFlags() & SPFlagDeleted; }
2017
2018 /// Check if this is reference-qualified.
2019 ///
2020 /// Return true if this subprogram is a C++11 reference-qualified non-static
2021 /// member function (void foo() &).
2022 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
2023
2024 /// Check if this is rvalue-reference-qualified.
2025 ///
2026 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
2027 /// non-static member function (void foo() &&).
2028 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
2029
2030 /// Check if this is marked as noreturn.
2031 ///
2032 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
2033 bool isNoReturn() const { return getFlags() & FlagNoReturn; }
2034
2035 // Check if this routine is a compiler-generated thunk.
2036 //
2037 // Returns true if this subprogram is a thunk generated by the compiler.
2038 bool isThunk() const { return getFlags() & FlagThunk; }
2039
2040 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2041
2042 StringRef getName() const { return getStringOperand(2); }
2043 StringRef getLinkageName() const { return getStringOperand(3); }
2044 /// Only used by clients of CloneFunction, and only right after the cloning.
2045 void replaceLinkageName(MDString *LN) { replaceOperandWith(3, LN); }
2046
2047 DISubroutineType *getType() const {
2048 return cast_or_null<DISubroutineType>(getRawType());
2049 }
2050 DIType *getContainingType() const {
2051 return cast_or_null<DIType>(getRawContainingType());
2052 }
2053 void replaceType(DISubroutineType *Ty) {
2054 assert(isDistinct() && "Only distinct nodes can mutate");
2055 replaceOperandWith(4, Ty);
2056 }
2057
2058 DICompileUnit *getUnit() const {
2059 return cast_or_null<DICompileUnit>(getRawUnit());
2060 }
2061 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
2062 DITemplateParameterArray getTemplateParams() const {
2063 return cast_or_null<MDTuple>(getRawTemplateParams());
2064 }
2065 DISubprogram *getDeclaration() const {
2066 return cast_or_null<DISubprogram>(getRawDeclaration());
2067 }
2068 DINodeArray getRetainedNodes() const {
2069 return cast_or_null<MDTuple>(getRawRetainedNodes());
2070 }
2071 DITypeArray getThrownTypes() const {
2072 return cast_or_null<MDTuple>(getRawThrownTypes());
2073 }
2074 DINodeArray getAnnotations() const {
2075 return cast_or_null<MDTuple>(getRawAnnotations());
2076 }
2077 StringRef getTargetFuncName() const {
2078 return (getRawTargetFuncName()) ? getStringOperand(12) : StringRef();
2079 }
2080
2081 Metadata *getRawScope() const { return getOperand(1); }
2082 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2083 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
2084 Metadata *getRawType() const { return getOperand(4); }
2085 Metadata *getRawUnit() const { return getOperand(5); }
2086 Metadata *getRawDeclaration() const { return getOperand(6); }
2087 Metadata *getRawRetainedNodes() const { return getOperand(7); }
2088 Metadata *getRawContainingType() const {
2089 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
2090 }
2091 Metadata *getRawTemplateParams() const {
2092 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
2093 }
2094 Metadata *getRawThrownTypes() const {
2095 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
2096 }
2097 Metadata *getRawAnnotations() const {
2098 return getNumOperands() > 11 ? getOperandAs<Metadata>(11) : nullptr;
2099 }
2100 MDString *getRawTargetFuncName() const {
2101 return getNumOperands() > 12 ? getOperandAs<MDString>(12) : nullptr;
2102 }
2103
2104 void replaceRawLinkageName(MDString *LinkageName) {
2106 }
2107
2108 /// Check if this subprogram describes the given function.
2109 ///
2110 /// FIXME: Should this be looking through bitcasts?
2111 bool describes(const Function *F) const;
2112
2113 static bool classof(const Metadata *MD) {
2114 return MD->getMetadataID() == DISubprogramKind;
2115 }
2116};
2117
2119protected:
2123
2124public:
2125 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
2126
2127 Metadata *getRawScope() const { return getOperand(1); }
2128
2129 void replaceScope(DIScope *Scope) {
2130 assert(!isUniqued());
2131 setOperand(1, Scope);
2132 }
2133
2134 static bool classof(const Metadata *MD) {
2135 return MD->getMetadataID() == DILexicalBlockKind ||
2136 MD->getMetadataID() == DILexicalBlockFileKind;
2137 }
2138};
2139
2141 friend class LLVMContextImpl;
2142 friend class MDNode;
2143
2144 unsigned Line;
2145 uint16_t Column;
2146
2148 unsigned Column, ArrayRef<Metadata *> Ops)
2149 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
2150 Column(Column) {
2151 assert(Column < (1u << 16) && "Expected 16-bit column");
2152 }
2153 ~DILexicalBlock() = default;
2154
2156 DIFile *File, unsigned Line, unsigned Column,
2158 bool ShouldCreate = true) {
2159 return getImpl(Context, static_cast<Metadata *>(Scope),
2160 static_cast<Metadata *>(File), Line, Column, Storage,
2161 ShouldCreate);
2162 }
2163
2164 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
2165 Metadata *File, unsigned Line, unsigned Column,
2166 StorageType Storage, bool ShouldCreate = true);
2167
2168 TempDILexicalBlock cloneImpl() const {
2170 getColumn());
2171 }
2172
2173public:
2175 (DILocalScope * Scope, DIFile *File, unsigned Line,
2176 unsigned Column),
2177 (Scope, File, Line, Column))
2179 (Metadata * Scope, Metadata *File, unsigned Line,
2180 unsigned Column),
2181 (Scope, File, Line, Column))
2182
2183 TempDILexicalBlock clone() const { return cloneImpl(); }
2184
2185 unsigned getLine() const { return Line; }
2186 unsigned getColumn() const { return Column; }
2187
2188 static bool classof(const Metadata *MD) {
2189 return MD->getMetadataID() == DILexicalBlockKind;
2190 }
2191};
2192
2194 friend class LLVMContextImpl;
2195 friend class MDNode;
2196
2197 unsigned Discriminator;
2198
2200 unsigned Discriminator, ArrayRef<Metadata *> Ops)
2201 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
2202 Discriminator(Discriminator) {}
2203 ~DILexicalBlockFile() = default;
2204
2206 DIFile *File, unsigned Discriminator,
2208 bool ShouldCreate = true) {
2209 return getImpl(Context, static_cast<Metadata *>(Scope),
2210 static_cast<Metadata *>(File), Discriminator, Storage,
2211 ShouldCreate);
2212 }
2213
2214 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
2215 Metadata *File, unsigned Discriminator,
2217 bool ShouldCreate = true);
2218
2219 TempDILexicalBlockFile cloneImpl() const {
2220 return getTemporary(getContext(), getScope(), getFile(),
2222 }
2223
2224public:
2227 unsigned Discriminator),
2228 (Scope, File, Discriminator))
2230 (Metadata * Scope, Metadata *File, unsigned Discriminator),
2231 (Scope, File, Discriminator))
2232
2233 TempDILexicalBlockFile clone() const { return cloneImpl(); }
2234 unsigned getDiscriminator() const { return Discriminator; }
2235
2236 static bool classof(const Metadata *MD) {
2237 return MD->getMetadataID() == DILexicalBlockFileKind;
2238 }
2239};
2240
2241unsigned DILocation::getDiscriminator() const {
2242 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
2243 return F->getDiscriminator();
2244 return 0;
2245}
2246
2247const DILocation *
2248DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
2249 DIScope *Scope = getScope();
2250 // Skip all parent DILexicalBlockFile that already have a discriminator
2251 // assigned. We do not want to have nested DILexicalBlockFiles that have
2252 // mutliple discriminators because only the leaf DILexicalBlockFile's
2253 // dominator will be used.
2254 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
2255 LBF && LBF->getDiscriminator() != 0;
2256 LBF = dyn_cast<DILexicalBlockFile>(Scope))
2257 Scope = LBF->getScope();
2258 DILexicalBlockFile *NewScope =
2259 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
2260 return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
2261 getInlinedAt());
2262}
2263
2265 return getBaseDiscriminatorFromDiscriminator(getDiscriminator(),
2267}
2268
2270 return getDuplicationFactorFromDiscriminator(getDiscriminator());
2271}
2272
2274 return getCopyIdentifierFromDiscriminator(getDiscriminator());
2275}
2276
2277std::optional<const DILocation *>
2279 unsigned BD, DF, CI;
2280
2282 BD = getBaseDiscriminator();
2283 if (D == BD)
2284 return this;
2285 return cloneWithDiscriminator(D);
2286 }
2287
2288 decodeDiscriminator(getDiscriminator(), BD, DF, CI);
2289 if (D == BD)
2290 return this;
2291 if (std::optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
2292 return cloneWithDiscriminator(*Encoded);
2293 return std::nullopt;
2294}
2295
2296std::optional<const DILocation *>
2298 assert(!EnableFSDiscriminator && "FSDiscriminator should not call this.");
2299
2301 if (DF <= 1)
2302 return this;
2303
2304 unsigned BD = getBaseDiscriminator();
2305 unsigned CI = getCopyIdentifier();
2306 if (std::optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
2307 return cloneWithDiscriminator(*D);
2308 return std::nullopt;
2309}
2310
2311class DINamespace : public DIScope {
2312 friend class LLVMContextImpl;
2313 friend class MDNode;
2314
2315 unsigned ExportSymbols : 1;
2316
2317 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
2319 ~DINamespace() = default;
2320
2321 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
2322 StringRef Name, bool ExportSymbols,
2323 StorageType Storage, bool ShouldCreate = true) {
2324 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2325 ExportSymbols, Storage, ShouldCreate);
2326 }
2327 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
2328 MDString *Name, bool ExportSymbols,
2329 StorageType Storage, bool ShouldCreate = true);
2330
2331 TempDINamespace cloneImpl() const {
2332 return getTemporary(getContext(), getScope(), getName(),
2334 }
2335
2336public:
2338 (DIScope * Scope, StringRef Name, bool ExportSymbols),
2339 (Scope, Name, ExportSymbols))
2341 (Metadata * Scope, MDString *Name, bool ExportSymbols),
2342 (Scope, Name, ExportSymbols))
2343
2344 TempDINamespace clone() const { return cloneImpl(); }
2345
2346 bool getExportSymbols() const { return ExportSymbols; }
2347 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2348 StringRef getName() const { return getStringOperand(2); }
2349
2350 Metadata *getRawScope() const { return getOperand(1); }
2351 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2352
2353 static bool classof(const Metadata *MD) {
2354 return MD->getMetadataID() == DINamespaceKind;
2355 }
2356};
2357
2358/// Represents a module in the programming language, for example, a Clang
2359/// module, or a Fortran module.
2360class DIModule : public DIScope {
2361 friend class LLVMContextImpl;
2362 friend class MDNode;
2363 unsigned LineNo;
2364 bool IsDecl;
2365
2366 DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo,
2367 bool IsDecl, ArrayRef<Metadata *> Ops);
2368 ~DIModule() = default;
2369
2370 static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope,
2373 unsigned LineNo, bool IsDecl, StorageType Storage,
2374 bool ShouldCreate = true) {
2375 return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name),
2378 getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl,
2379 Storage, ShouldCreate);
2380 }
2381 static DIModule *getImpl(LLVMContext &Context, Metadata *File,
2384 MDString *APINotesFile, unsigned LineNo, bool IsDecl,
2385 StorageType Storage, bool ShouldCreate = true);
2386
2387 TempDIModule cloneImpl() const {
2389 getConfigurationMacros(), getIncludePath(),
2390 getAPINotesFile(), getLineNo(), getIsDecl());
2391 }
2392
2393public:
2397 StringRef APINotesFile, unsigned LineNo,
2398 bool IsDecl = false),
2400 APINotesFile, LineNo, IsDecl))
2404 MDString *APINotesFile, unsigned LineNo,
2405 bool IsDecl = false),
2407 APINotesFile, LineNo, IsDecl))
2408
2409 TempDIModule clone() const { return cloneImpl(); }
2410
2411 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2412 StringRef getName() const { return getStringOperand(2); }
2413 StringRef getConfigurationMacros() const { return getStringOperand(3); }
2414 StringRef getIncludePath() const { return getStringOperand(4); }
2415 StringRef getAPINotesFile() const { return getStringOperand(5); }
2416 unsigned getLineNo() const { return LineNo; }
2417 bool getIsDecl() const { return IsDecl; }
2418
2419 Metadata *getRawScope() const { return getOperand(1); }
2420 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2421 MDString *getRawConfigurationMacros() const {
2422 return getOperandAs<MDString>(3);
2423 }
2424 MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); }
2425 MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); }
2426
2427 static bool classof(const Metadata *MD) {
2428 return MD->getMetadataID() == DIModuleKind;
2429 }
2430};
2431
2432/// Base class for template parameters.
2434protected:
2436
2438 unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops)
2441
2442public:
2443 StringRef getName() const { return getStringOperand(0); }
2444 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2445
2446 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2447 Metadata *getRawType() const { return getOperand(1); }
2448 bool isDefault() const { return IsDefault; }
2449
2450 static bool classof(const Metadata *MD) {
2451 return MD->getMetadataID() == DITemplateTypeParameterKind ||
2452 MD->getMetadataID() == DITemplateValueParameterKind;
2453 }
2454};
2455
2457 friend class LLVMContextImpl;
2458 friend class MDNode;
2459
2462 ~DITemplateTypeParameter() = default;
2463
2465 DIType *Type, bool IsDefault,
2467 bool ShouldCreate = true) {
2468 return getImpl(Context, getCanonicalMDString(Context, Name), Type,
2469 IsDefault, Storage, ShouldCreate);
2470 }
2472 Metadata *Type, bool IsDefault,
2474 bool ShouldCreate = true);
2475
2476 TempDITemplateTypeParameter cloneImpl() const {
2477 return getTemporary(getContext(), getName(), getType(), isDefault());
2478 }
2479
2480public:
2483 (Name, Type, IsDefault))
2487
2488 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2489
2490 static bool classof(const Metadata *MD) {
2491 return MD->getMetadataID() == DITemplateTypeParameterKind;
2492 }
2493};
2494
2496 friend class LLVMContextImpl;
2497 friend class MDNode;
2498
2500 unsigned Tag, bool IsDefault,
2502 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2503 IsDefault, Ops) {}
2504 ~DITemplateValueParameter() = default;
2505
2506 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2508 bool IsDefault, Metadata *Value,
2510 bool ShouldCreate = true) {
2511 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2512 IsDefault, Value, Storage, ShouldCreate);
2513 }
2514 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2515 MDString *Name, Metadata *Type,
2516 bool IsDefault, Metadata *Value,
2518 bool ShouldCreate = true);
2519
2520 TempDITemplateValueParameter cloneImpl() const {
2521 return getTemporary(getContext(), getTag(), getName(), getType(),
2522 isDefault(), getValue());
2523 }
2524
2525public:
2527 (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault,
2528 Metadata *Value),
2529 (Tag, Name, Type, IsDefault, Value))
2534
2535 TempDITemplateValueParameter clone() const { return cloneImpl(); }
2536
2537 Metadata *getValue() const { return getOperand(2); }
2538
2539 static bool classof(const Metadata *MD) {
2540 return MD->getMetadataID() == DITemplateValueParameterKind;
2541 }
2542};
2543
2544/// Base class for variables.
2545class DIVariable : public DINode {
2546 unsigned Line;
2547 uint32_t AlignInBits;
2548
2549protected:
2550 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, signed Line,
2551 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0);
2552 ~DIVariable() = default;
2553
2554public:
2555 unsigned getLine() const { return Line; }
2556 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2557 StringRef getName() const { return getStringOperand(1); }
2558 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2559 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2560 uint32_t getAlignInBits() const { return AlignInBits; }
2561 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2562 /// Determines the size of the variable's type.
2563 std::optional<uint64_t> getSizeInBits() const;
2564
2565 /// Return the signedness of this variable's type, or std::nullopt if this
2566 /// type is neither signed nor unsigned.
2567 std::optional<DIBasicType::Signedness> getSignedness() const {
2568 if (auto *BT = dyn_cast<DIBasicType>(getType()))
2569 return BT->getSignedness();
2570 return std::nullopt;
2571 }
2572
2574 if (auto *F = getFile())
2575 return F->getFilename();
2576 return "";
2577 }
2578
2580 if (auto *F = getFile())
2581 return F->getDirectory();
2582 return "";
2583 }
2584
2585 std::optional<StringRef> getSource() const {
2586 if (auto *F = getFile())
2587 return F->getSource();
2588 return std::nullopt;
2589 }
2590
2591 Metadata *getRawScope() const { return getOperand(0); }
2592 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2593 Metadata *getRawFile() const { return getOperand(2); }
2594 Metadata *getRawType() const { return getOperand(3); }
2595
2596 static bool classof(const Metadata *MD) {
2597 return MD->getMetadataID() == DILocalVariableKind ||
2598 MD->getMetadataID() == DIGlobalVariableKind;
2599 }
2600};
2601
2602/// DWARF expression.
2603///
2604/// This is (almost) a DWARF expression that modifies the location of a
2605/// variable, or the location of a single piece of a variable, or (when using
2606/// DW_OP_stack_value) is the constant variable value.
2607///
2608/// TODO: Co-allocate the expression elements.
2609/// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2610/// storage types.
2611class DIExpression : public MDNode {
2612 friend class LLVMContextImpl;
2613 friend class MDNode;
2614
2615 std::vector<uint64_t> Elements;
2616
2618 : MDNode(C, DIExpressionKind, Storage, std::nullopt),
2619 Elements(Elements.begin(), Elements.end()) {}
2620 ~DIExpression() = default;
2621
2622 static DIExpression *getImpl(LLVMContext &Context,
2624 bool ShouldCreate = true);
2625
2626 TempDIExpression cloneImpl() const {
2627 return getTemporary(getContext(), getElements());
2628 }
2629
2630public:
2632
2633 TempDIExpression clone() const { return cloneImpl(); }
2634
2635 ArrayRef<uint64_t> getElements() const { return Elements; }
2636
2637 unsigned getNumElements() const { return Elements.size(); }
2638
2639 uint64_t getElement(unsigned I) const {
2640 assert(I < Elements.size() && "Index out of range");
2641 return Elements[I];
2642 }
2643
2645 /// Determine whether this represents a constant value, if so
2646 // return it's sign information.
2647 std::optional<SignedOrUnsignedConstant> isConstant() const;
2648
2649 /// Return the number of unique location operands referred to (via
2650 /// DW_OP_LLVM_arg) in this expression; this is not necessarily the number of
2651 /// instances of DW_OP_LLVM_arg within the expression.
2652 /// For example, for the expression:
2653 /// (DW_OP_LLVM_arg 0, DW_OP_LLVM_arg 1, DW_OP_plus,
2654 /// DW_OP_LLVM_arg 0, DW_OP_mul)
2655 /// This function would return 2, as there are two unique location operands
2656 /// (0 and 1).
2658
2660
2663
2664 /// A lightweight wrapper around an expression operand.
2665 ///
2666 /// TODO: Store arguments directly and change \a DIExpression to store a
2667 /// range of these.
2669 const uint64_t *Op = nullptr;
2670
2671 public:
2672 ExprOperand() = default;
2673 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2674
2675 const uint64_t *get() const { return Op; }
2676
2677 /// Get the operand code.
2678 uint64_t getOp() const { return *Op; }
2679
2680 /// Get an argument to the operand.
2681 ///
2682 /// Never returns the operand itself.
2683 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2684
2685 unsigned getNumArgs() const { return getSize() - 1; }
2686
2687 /// Return the size of the operand.
2688 ///
2689 /// Return the number of elements in the operand (1 + args).
2690 unsigned getSize() const;
2691
2692 /// Append the elements of this operand to \p V.
2694 V.append(get(), get() + getSize());
2695 }
2696 };
2697
2698 /// An iterator for expression operands.
2700 ExprOperand Op;
2701
2702 public:
2703 using iterator_category = std::input_iterator_tag;
2705 using difference_type = std::ptrdiff_t;
2708
2709 expr_op_iterator() = default;
2711
2712 element_iterator getBase() const { return Op.get(); }
2713 const ExprOperand &operator*() const { return Op; }
2714 const ExprOperand *operator->() const { return &Op; }
2715
2717 increment();
2718 return *this;
2719 }
2721 expr_op_iterator T(*this);
2722 increment();
2723 return T;
2724 }
2725
2726 /// Get the next iterator.
2727 ///
2728 /// \a std::next() doesn't work because this is technically an
2729 /// input_iterator, but it's a perfectly valid operation. This is an
2730 /// accessor to provide the same functionality.
2731 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2732
2733 bool operator==(const expr_op_iterator &X) const {
2734 return getBase() == X.getBase();
2735 }
2736 bool operator!=(const expr_op_iterator &X) const {
2737 return getBase() != X.getBase();
2738 }
2739
2740 private:
2741 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2742 };
2743
2744 /// Visit the elements via ExprOperand wrappers.
2745 ///
2746 /// These range iterators visit elements through \a ExprOperand wrappers.
2747 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2748 /// true.
2749 ///
2750 /// \pre \a isValid() gives \c true.
2751 /// @{
2754 }
2757 }
2759 return {expr_op_begin(), expr_op_end()};
2760 }
2761 /// @}
2762
2763 bool isValid() const;
2764
2765 static bool classof(const Metadata *MD) {
2766 return MD->getMetadataID() == DIExpressionKind;
2767 }
2768
2769 /// Return whether the first element a DW_OP_deref.
2770 bool startsWithDeref() const;
2771
2772 /// Return whether there is exactly one operator and it is a DW_OP_deref;
2773 bool isDeref() const;
2774
2775 /// Holds the characteristics of one fragment of a larger variable.
2779 };
2780
2781 /// Retrieve the details of this fragment expression.
2782 static std::optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2783 expr_op_iterator End);
2784
2785 /// Retrieve the details of this fragment expression.
2786 std::optional<FragmentInfo> getFragmentInfo() const {
2788 }
2789
2790 /// Return whether this is a piece of an aggregate variable.
2791 bool isFragment() const { return getFragmentInfo().has_value(); }
2792
2793 /// Return whether this is an implicit location description.
2794 bool isImplicit() const;
2795
2796 /// Return whether the location is computed on the expression stack, meaning
2797 /// it cannot be a simple register location.
2798 bool isComplex() const;
2799
2800 /// Return whether the evaluated expression makes use of a single location at
2801 /// the start of the expression, i.e. if it contains only a single
2802 /// DW_OP_LLVM_arg op as its first operand, or if it contains none.
2803 bool isSingleLocationExpression() const;
2804
2805 /// Removes all elements from \p Expr that do not apply to an undef debug
2806 /// value, which includes every operator that computes the value/location on
2807 /// the DWARF stack, including any DW_OP_LLVM_arg elements (making the result
2808 /// of this function always a single-location expression) while leaving
2809 /// everything that defines what the computed value applies to, i.e. the
2810 /// fragment information.
2811 static const DIExpression *convertToUndefExpression(const DIExpression *Expr);
2812
2813 /// If \p Expr is a non-variadic expression (i.e. one that does not contain
2814 /// DW_OP_LLVM_arg), returns \p Expr converted to variadic form by adding a
2815 /// leading [DW_OP_LLVM_arg, 0] to the expression; otherwise returns \p Expr.
2816 static const DIExpression *
2818
2819 /// If \p Expr is a valid single-location expression, i.e. it refers to only a
2820 /// single debug operand at the start of the expression, then return that
2821 /// expression in a non-variadic form by removing DW_OP_LLVM_arg from the
2822 /// expression if it is present; otherwise returns std::nullopt.
2823 static std::optional<const DIExpression *>
2825
2826 /// Inserts the elements of \p Expr into \p Ops modified to a canonical form,
2827 /// which uses DW_OP_LLVM_arg (i.e. is a variadic expression) and folds the
2828 /// implied derefence from the \p IsIndirect flag into the expression. This
2829 /// allows us to check equivalence between expressions with differing
2830 /// directness or variadicness.
2832 const DIExpression *Expr,
2833 bool IsIndirect);
2834
2835 /// Determines whether two debug values should produce equivalent DWARF
2836 /// expressions, using their DIExpressions and directness, ignoring the
2837 /// differences between otherwise identical expressions in variadic and
2838 /// non-variadic form and not considering the debug operands.
2839 /// \p FirstExpr is the DIExpression for the first debug value.
2840 /// \p FirstIndirect should be true if the first debug value is indirect; in
2841 /// IR this should be true for dbg.declare and dbg.addr intrinsics and false
2842 /// for dbg.values, and in MIR this should be true only for DBG_VALUE
2843 /// instructions whose second operand is an immediate value.
2844 /// \p SecondExpr and \p SecondIndirect have the same meaning as the prior
2845 /// arguments, but apply to the second debug value.
2846 static bool isEqualExpression(const DIExpression *FirstExpr,
2847 bool FirstIndirect,
2848 const DIExpression *SecondExpr,
2849 bool SecondIndirect);
2850
2851 /// Append \p Ops with operations to apply the \p Offset.
2852 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2853
2854 /// If this is a constant offset, extract it. If there is no expression,
2855 /// return true with an offset of zero.
2856 bool extractIfOffset(int64_t &Offset) const;
2857
2858 /// Returns true iff this DIExpression contains at least one instance of
2859 /// `DW_OP_LLVM_arg, n` for all n in [0, N).
2860 bool hasAllLocationOps(unsigned N) const;
2861
2862 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
2863 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
2864 /// Space>.
2865 static const DIExpression *extractAddressClass(const DIExpression *Expr,
2866 unsigned &AddrClass);
2867
2868 /// Used for DIExpression::prepend.
2869 enum PrependOps : uint8_t {
2871 DerefBefore = 1 << 0,
2872 DerefAfter = 1 << 1,
2873 StackValue = 1 << 2,
2874 EntryValue = 1 << 3
2876
2877 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2878 /// into a stack value or/and an entry value.
2879 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2880 int64_t Offset = 0);
2881
2882 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2883 /// stack value.
2884 static DIExpression *prependOpcodes(const DIExpression *Expr,
2886 bool StackValue = false,
2887 bool EntryValue = false);
2888
2889 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
2890 /// returned expression is a stack value only if \p DIExpr is a stack value.
2891 /// If \p DIExpr describes a fragment, the returned expression will describe
2892 /// the same fragment.
2893 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
2894
2895 /// Convert \p DIExpr into a stack value if it isn't one already by appending
2896 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
2897 /// If \p DIExpr describes a fragment, the returned expression will describe
2898 /// the same fragment.
2899 static DIExpression *appendToStack(const DIExpression *Expr,
2900 ArrayRef<uint64_t> Ops);
2901
2902 /// Create a copy of \p Expr by appending the given list of \p Ops to each
2903 /// instance of the operand `DW_OP_LLVM_arg, \p ArgNo`. This is used to
2904 /// modify a specific location used by \p Expr, such as when salvaging that
2905 /// location.
2906 static DIExpression *appendOpsToArg(const DIExpression *Expr,
2907 ArrayRef<uint64_t> Ops, unsigned ArgNo,
2908 bool StackValue = false);
2909
2910 /// Create a copy of \p Expr with each instance of
2911 /// `DW_OP_LLVM_arg, \p OldArg` replaced with `DW_OP_LLVM_arg, \p NewArg`,
2912 /// and each instance of `DW_OP_LLVM_arg, Arg` with `DW_OP_LLVM_arg, Arg - 1`
2913 /// for all Arg > \p OldArg.
2914 /// This is used when replacing one of the operands of a debug value list
2915 /// with another operand in the same list and deleting the old operand.
2916 static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg,
2917 uint64_t NewArg);
2918
2919 /// Create a DIExpression to describe one part of an aggregate variable that
2920 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2921 /// will be appended to the elements of \c Expr. If \c Expr already contains
2922 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2923 /// into the existing fragment.
2924 ///
2925 /// \param OffsetInBits Offset of the piece in bits.
2926 /// \param SizeInBits Size of the piece in bits.
2927 /// \return Creating a fragment expression may fail if \c Expr
2928 /// contains arithmetic operations that would be
2929 /// truncated.
2930 static std::optional<DIExpression *>
2931 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2932 unsigned SizeInBits);
2933
2934 /// Determine the relative position of the fragments passed in.
2935 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2936 /// 1 if this is entirely after Other.
2937 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
2938 uint64_t l1 = A.OffsetInBits;
2939 uint64_t l2 = B.OffsetInBits;
2940 uint64_t r1 = l1 + A.SizeInBits;
2941 uint64_t r2 = l2 + B.SizeInBits;
2942 if (r1 <= l2)
2943 return -1;
2944 else if (r2 <= l1)
2945 return 1;
2946 else
2947 return 0;
2948 }
2949
2950 using ExtOps = std::array<uint64_t, 6>;
2951
2952 /// Returns the ops for a zero- or sign-extension in a DIExpression.
2953 static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed);
2954
2955 /// Append a zero- or sign-extension to \p Expr. Converts the expression to a
2956 /// stack value if it isn't one already.
2957 static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize,
2958 unsigned ToSize, bool Signed);
2959
2960 /// Check if fragments overlap between a pair of FragmentInfos.
2961 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
2962 return fragmentCmp(A, B) == 0;
2963 }
2964
2965 /// Determine the relative position of the fragments described by this
2966 /// DIExpression and \p Other. Calls static fragmentCmp implementation.
2967 int fragmentCmp(const DIExpression *Other) const {
2968 auto Fragment1 = *getFragmentInfo();
2969 auto Fragment2 = *Other->getFragmentInfo();
2970 return fragmentCmp(Fragment1, Fragment2);
2971 }
2972
2973 /// Check if fragments overlap between this DIExpression and \p Other.
2975 if (!isFragment() || !Other->isFragment())
2976 return true;
2977 return fragmentCmp(Other) == 0;
2978 }
2979
2980 /// Check if the expression consists of exactly one entry value operand.
2981 /// (This is the only configuration of entry values that is supported.)
2982 bool isEntryValue() const;
2983
2984 /// Try to shorten an expression with an initial constant operand.
2985 /// Returns a new expression and constant on success, or the original
2986 /// expression and constant on failure.
2987 std::pair<DIExpression *, const ConstantInt *>
2988 constantFold(const ConstantInt *CI);
2989};
2990
2993 return std::tie(A.SizeInBits, A.OffsetInBits) ==
2994 std::tie(B.SizeInBits, B.OffsetInBits);
2995}
2996
2999 return std::tie(A.SizeInBits, A.OffsetInBits) <
3000 std::tie(B.SizeInBits, B.OffsetInBits);
3001}
3002
3003template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
3005 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
3006
3007 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
3008
3009 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
3010
3011 static unsigned getHashValue(const FragInfo &Frag) {
3012 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
3013 }
3014
3015 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
3016};
3017
3018/// Global variables.
3019///
3020/// TODO: Remove DisplayName. It's always equal to Name.
3022 friend class LLVMContextImpl;
3023 friend class MDNode;
3024
3025 bool IsLocalToUnit;
3026 bool IsDefinition;
3027
3029 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
3031 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
3032 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
3033 ~DIGlobalVariable() = default;
3034
3035 static DIGlobalVariable *
3038 bool IsLocalToUnit, bool IsDefinition,
3041 bool ShouldCreate = true) {
3042 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
3044 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
3045 cast_or_null<Metadata>(TemplateParams), AlignInBits,
3046 Annotations.get(), Storage, ShouldCreate);
3047 }
3048 static DIGlobalVariable *
3049 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
3050 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
3051 bool IsLocalToUnit, bool IsDefinition,
3054 bool ShouldCreate = true);
3055
3056 TempDIGlobalVariable cloneImpl() const {
3061 getAnnotations());
3062 }
3063
3064public:
3068 unsigned Line, DIType *Type, bool IsLocalToUnit, bool IsDefinition,
3070 uint32_t AlignInBits, DINodeArray Annotations),
3071 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
3076 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
3079 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
3081
3082 TempDIGlobalVariable clone() const { return cloneImpl(); }
3083
3084 bool isLocalToUnit() const { return IsLocalToUnit; }
3085 bool isDefinition() const { return IsDefinition; }
3089 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
3090 }
3091 DINodeArray getAnnotations() const {
3092 return cast_or_null<MDTuple>(getRawAnnotations());
3093 }
3094
3095 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
3098 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
3099 Metadata *getRawAnnotations() const { return getOperand(8); }
3100
3101 static bool classof(const Metadata *MD) {
3102 return MD->getMetadataID() == DIGlobalVariableKind;
3103 }
3104};
3105
3106class DICommonBlock : public DIScope {
3107 unsigned LineNo;
3108
3109 friend class LLVMContextImpl;
3110 friend class MDNode;
3111
3112 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
3114
3115 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
3117 DIFile *File, unsigned LineNo,
3118 StorageType Storage, bool ShouldCreate = true) {
3119 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
3120 File, LineNo, Storage, ShouldCreate);
3121 }
3122 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
3124 unsigned LineNo, StorageType Storage,
3125 bool ShouldCreate = true);
3126
3127 TempDICommonBlock cloneImpl() const {
3129 getFile(), getLineNo());
3130 }
3131
3132public:
3135 DIFile *File, unsigned LineNo),
3136 (Scope, Decl, Name, File, LineNo))
3139 Metadata *File, unsigned LineNo),
3140 (Scope, Decl, Name, File, LineNo))
3141
3142 TempDICommonBlock clone() const { return cloneImpl(); }
3143
3144 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3146 return cast_or_null<DIGlobalVariable>(getRawDecl());
3147 }
3148 StringRef getName() const { return getStringOperand(2); }
3149 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3150 unsigned getLineNo() const { return LineNo; }
3151
3152 Metadata *getRawScope() const { return getOperand(0); }
3153 Metadata *getRawDecl() const { return getOperand(1); }
3154 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3155 Metadata *getRawFile() const { return getOperand(3); }
3156
3157 static bool classof(const Metadata *MD) {
3158 return MD->getMetadataID() == DICommonBlockKind;
3159 }
3160};
3161
3162/// Local variable.
3163///
3164/// TODO: Split up flags.
3166 friend class LLVMContextImpl;
3167 friend class MDNode;
3168
3169 unsigned Arg : 16;
3170 DIFlags Flags;
3171
3173 unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
3175 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
3176 Arg(Arg), Flags(Flags) {
3177 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
3178 }
3179 ~DILocalVariable() = default;
3180
3181 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
3182 StringRef Name, DIFile *File, unsigned Line,
3183 DIType *Type, unsigned Arg, DIFlags Flags,
3184 uint32_t AlignInBits, DINodeArray Annotations,
3186 bool ShouldCreate = true) {
3187 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
3188 Line, Type, Arg, Flags, AlignInBits, Annotations.get(),
3189 Storage, ShouldCreate);
3190 }
3191 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
3192 MDString *Name, Metadata *File, unsigned Line,
3193 Metadata *Type, unsigned Arg, DIFlags Flags,
3196 bool ShouldCreate = true);
3197
3198 TempDILocalVariable cloneImpl() const {
3200 getLine(), getType(), getArg(), getFlags(),
3202 }
3203
3204public:
3207 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
3208 uint32_t AlignInBits, DINodeArray Annotations),
3209 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits,
3210 Annotations))
3213 unsigned Line, Metadata *Type, unsigned Arg, DIFlags Flags,
3216 Annotations))
3217
3218 TempDILocalVariable clone() const { return cloneImpl(); }
3219
3220 /// Get the local scope for this variable.
3221 ///
3222 /// Variables must be defined in a local scope.
3224 return cast<DILocalScope>(DIVariable::getScope());
3225 }
3226
3227 bool isParameter() const { return Arg; }
3228 unsigned getArg() const { return Arg; }
3229 DIFlags getFlags() const { return Flags; }
3230
3231 DINodeArray getAnnotations() const {
3232 return cast_or_null<MDTuple>(getRawAnnotations());
3233 }
3234 Metadata *getRawAnnotations() const { return getOperand(4); }
3235
3236 bool isArtificial() const { return getFlags() & FlagArtificial; }
3237 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
3238
3239 /// Check that a location is valid for this variable.
3240 ///
3241 /// Check that \c DL exists, is in the same subprogram, and has the same
3242 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
3243 /// to a \a DbgInfoIntrinsic.)
3245 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
3246 }
3247
3248 static bool classof(const Metadata *MD) {
3249 return MD->getMetadataID() == DILocalVariableKind;
3250 }
3251};
3252
3253/// Label.
3254///
3255class DILabel : public DINode {
3256 friend class LLVMContextImpl;
3257 friend class MDNode;
3258
3259 unsigned Line;
3260
3261 DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
3263 ~DILabel() = default;
3264
3266 DIFile *File, unsigned Line, StorageType Storage,
3267 bool ShouldCreate = true) {
3268 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
3269 Line, Storage, ShouldCreate);
3270 }
3271 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
3272 Metadata *File, unsigned Line, StorageType Storage,
3273 bool ShouldCreate = true);
3274
3275 TempDILabel cloneImpl() const {
3277 getLine());
3278 }
3279
3280public:
3283 unsigned Line),
3284 (Scope, Name, File, Line))
3287 unsigned Line),
3288 (Scope, Name, File, Line))
3289
3290 TempDILabel clone() const { return cloneImpl(); }
3291
3292 /// Get the local scope for this label.
3293 ///
3294 /// Labels must be defined in a local scope.
3296 return cast_or_null<DILocalScope>(getRawScope());
3297 }
3298 unsigned getLine() const { return Line; }
3299 StringRef getName() const { return getStringOperand(1); }
3300 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3301
3302 Metadata *getRawScope() const { return getOperand(0); }
3303 MDString *getRawName() const { return getOperandAs<MDString>(1); }
3304 Metadata *getRawFile() const { return getOperand(2); }
3305
3306 /// Check that a location is valid for this label.
3307 ///
3308 /// Check that \c DL exists, is in the same subprogram, and has the same
3309 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
3310 /// to a \a DbgInfoIntrinsic.)
3312 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
3313 }
3314
3315 static bool classof(const Metadata *MD) {
3316 return MD->getMetadataID() == DILabelKind;
3317 }
3318};
3319
3320class DIObjCProperty : public DINode {
3321 friend class LLVMContextImpl;
3322 friend class MDNode;
3323
3324 unsigned Line;
3325 unsigned Attributes;
3326
3328 unsigned Attributes, ArrayRef<Metadata *> Ops);
3329 ~DIObjCProperty() = default;
3330
3331 static DIObjCProperty *
3332 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
3333 StringRef GetterName, StringRef SetterName, unsigned Attributes,
3334 DIType *Type, StorageType Storage, bool ShouldCreate = true) {
3335 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
3337 getCanonicalMDString(Context, SetterName), Attributes, Type,
3338 Storage, ShouldCreate);
3339 }
3340 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
3341 Metadata *File, unsigned Line,
3343 unsigned Attributes, Metadata *Type,
3344 StorageType Storage, bool ShouldCreate = true);
3345
3346 TempDIObjCProperty cloneImpl() const {
3347 return getTemporary(getContext(), getName(), getFile(), getLine(),
3349 getType());
3350 }
3351
3352public:
3354 (StringRef Name, DIFile *File, unsigned Line,
3356 unsigned Attributes, DIType *Type),
3357 (Name, File, Line, GetterName, SetterName, Attributes,
3358 Type))
3360 (MDString * Name, Metadata *File, unsigned Line,
3362 unsigned Attributes, Metadata *Type),
3363 (Name, File, Line, GetterName, SetterName, Attributes,
3364 Type))
3365
3366 TempDIObjCProperty clone() const { return cloneImpl(); }
3367
3368 unsigned getLine() const { return Line; }
3369 unsigned getAttributes() const { return Attributes; }
3370 StringRef getName() const { return getStringOperand(0); }
3371 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3374 DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
3375
3377 if (auto *F = getFile())
3378 return F->getFilename();
3379 return "";
3380 }
3381
3383 if (auto *F = getFile())
3384 return F->getDirectory();
3385 return "";
3386 }
3387
3388 MDString *getRawName() const { return getOperandAs<MDString>(0); }
3389 Metadata *getRawFile() const { return getOperand(1); }
3390 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
3391 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
3392 Metadata *getRawType() const { return getOperand(4); }
3393
3394 static bool classof(const Metadata *MD) {
3395 return MD->getMetadataID() == DIObjCPropertyKind;
3396 }
3397};
3398
3399/// An imported module (C++ using directive or similar).
3400class DIImportedEntity : public DINode {
3401 friend class LLVMContextImpl;
3402 friend class MDNode;
3403
3404 unsigned Line;
3405
3407 unsigned Line, ArrayRef<Metadata *> Ops)
3408 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
3409 ~DIImportedEntity() = default;
3410
3411 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3413 unsigned Line, StringRef Name,
3414 DINodeArray Elements, StorageType Storage,
3415 bool ShouldCreate = true) {
3416 return getImpl(Context, Tag, Scope, Entity, File, Line,
3417 getCanonicalMDString(Context, Name), Elements.get(), Storage,
3418 ShouldCreate);
3419 }
3420 static DIImportedEntity *
3421 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity,
3422 Metadata *File, unsigned Line, MDString *Name, Metadata *Elements,
3423 StorageType Storage, bool ShouldCreate = true);
3424
3425 TempDIImportedEntity cloneImpl() const {
3426 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3427 getFile(), getLine(), getName(), getElements());
3428 }
3429
3430public:
3432 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3433 unsigned Line, StringRef Name = "",
3434 DINodeArray Elements = nullptr),
3435 (Tag, Scope, Entity, File, Line, Name, Elements))
3438 Metadata *File, unsigned Line, MDString *Name,
3439 Metadata *Elements = nullptr),
3440 (Tag, Scope, Entity, File, Line, Name, Elements))
3441
3442 TempDIImportedEntity clone() const { return cloneImpl(); }
3443
3444 unsigned getLine() const { return Line; }
3445 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
3446 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
3447 StringRef getName() const { return getStringOperand(2); }
3448 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3449 DINodeArray getElements() const {
3450 return cast_or_null<MDTuple>(getRawElements());
3451 }
3452
3453 Metadata *getRawScope() const { return getOperand(0); }
3454 Metadata *getRawEntity() const { return getOperand(1); }
3455 MDString *getRawName() const { return getOperandAs<MDString>(2); }
3456 Metadata *getRawFile() const { return getOperand(3); }
3457 Metadata *getRawElements() const { return getOperand(4); }
3458
3459 static bool classof(const Metadata *MD) {
3460 return MD->getMetadataID() == DIImportedEntityKind;
3461 }
3462};
3463
3464/// A pair of DIGlobalVariable and DIExpression.
3466 friend class LLVMContextImpl;
3467 friend class MDNode;
3468
3471 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
3472 ~DIGlobalVariableExpression() = default;
3473
3475 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
3476 StorageType Storage, bool ShouldCreate = true);
3477
3478 TempDIGlobalVariableExpression cloneImpl() const {
3480 }
3481
3482public:
3484 (Metadata * Variable, Metadata *Expression),
3485 (Variable, Expression))
3486
3487 TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
3488
3489 Metadata *getRawVariable() const { return getOperand(0); }
3490
3492 return cast_or_null<DIGlobalVariable>(getRawVariable());
3493 }
3494
3495 Metadata *getRawExpression() const { return getOperand(1); }
3496
3498 return cast<DIExpression>(getRawExpression());
3499 }
3500
3501 static bool classof(const Metadata *MD) {
3502 return MD->getMetadataID() == DIGlobalVariableExpressionKind;
3503 }
3504};
3505
3506/// Macro Info DWARF-like metadata node.
3507///
3508/// A metadata node with a DWARF macro info (i.e., a constant named
3509/// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
3510/// DIMacroNode
3511/// because it's potentially used for non-DWARF output.
3512class DIMacroNode : public MDNode {
3513 friend class LLVMContextImpl;
3514 friend class MDNode;
3515
3516protected:
3517 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
3519 ArrayRef<Metadata *> Ops2 = std::nullopt)
3520 : MDNode(C, ID, Storage, Ops1, Ops2) {
3521 assert(MIType < 1u << 16);
3522 SubclassData16 = MIType;
3523 }
3524 ~DIMacroNode() = default;
3525
3526 template <class Ty> Ty *getOperandAs(unsigned I) const {
3527 return cast_or_null<Ty>(getOperand(I));
3528 }
3529
3530 StringRef getStringOperand(unsigned I) const {
3531 if (auto *S = getOperandAs<MDString>(I))
3532 return S->getString();
3533 return StringRef();
3534 }
3535
3537 if (S.empty())
3538 return nullptr;
3539 return MDString::get(Context, S);
3540 }
3541
3542public:
3543 unsigned getMacinfoType() const { return SubclassData16; }
3544
3545 static bool classof(const Metadata *MD) {
3546 switch (MD->getMetadataID()) {
3547 default:
3548 return false;
3549 case DIMacroKind:
3550 case DIMacroFileKind:
3551 return true;
3552 }
3553 }
3554};
3555
3556class DIMacro : public DIMacroNode {
3557 friend class LLVMContextImpl;
3558 friend class MDNode;
3559
3560 unsigned Line;
3561
3562 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
3564 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
3565 ~DIMacro() = default;
3566
3567 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3569 bool ShouldCreate = true) {
3570 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
3571 getCanonicalMDString(Context, Value), Storage, ShouldCreate);
3572 }
3573 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
3574 MDString *Name, MDString *Value, StorageType Storage,
3575 bool ShouldCreate = true);
3576
3577 TempDIMacro cloneImpl() const {
3579 getValue());
3580 }
3581
3582public:
3584 (unsigned MIType, unsigned Line, StringRef Name,
3585 StringRef Value = ""),
3586 (MIType, Line, Name, Value))