blob: e2210bbcda0752be9ec7b5e597fb17769adb2104 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Declarations for metadata specific to debug info.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_DEBUGINFOMETADATA_H
15#define LLVM_IR_DEBUGINFOMETADATA_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/BitmaskEnum.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/iterator_range.h"
26#include "llvm/BinaryFormat/Dwarf.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/Metadata.h"
29#include "llvm/Support/Casting.h"
30#include <cassert>
31#include <climits>
32#include <cstddef>
33#include <cstdint>
34#include <iterator>
35#include <type_traits>
36#include <vector>
37
38// Helper macros for defining get() overrides.
39#define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
40#define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
41#define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
42 static CLASS *getDistinct(LLVMContext &Context, \
43 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
44 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
45 } \
46 static Temp##CLASS getTemporary(LLVMContext &Context, \
47 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
48 return Temp##CLASS( \
49 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
50 }
51#define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
52 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
53 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
54 } \
55 static CLASS *getIfExists(LLVMContext &Context, \
56 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
57 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
58 /* ShouldCreate */ false); \
59 } \
60 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
61
62namespace llvm {
63
64/// Holds a subclass of DINode.
65///
66/// FIXME: This class doesn't currently make much sense. Previously it was a
67/// union beteen MDString (for ODR-uniqued types) and things like DIType. To
68/// support CodeView work, it wasn't deleted outright when MDString-based type
69/// references were deleted; we'll soon need a similar concept for CodeView
70/// DITypeIndex.
71template <class T> class TypedDINodeRef {
72 const Metadata *MD = nullptr;
73
74public:
75 TypedDINodeRef() = default;
76 TypedDINodeRef(std::nullptr_t) {}
77 TypedDINodeRef(const T *MD) : MD(MD) {}
78
79 explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
80 assert((!MD || isa<T>(MD)) && "Expected valid type ref");
81 }
82
83 template <class U>
84 TypedDINodeRef(
85 const TypedDINodeRef<U> &X,
86 typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
87 nullptr)
88 : MD(X) {}
89
90 operator Metadata *() const { return const_cast<Metadata *>(MD); }
91
92 T *resolve() const { return const_cast<T *>(cast_or_null<T>(MD)); }
93
94 bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; }
95 bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; }
96};
97
98using DINodeRef = TypedDINodeRef<DINode>;
99using DIScopeRef = TypedDINodeRef<DIScope>;
100using DITypeRef = TypedDINodeRef<DIType>;
101
102class DITypeRefArray {
103 const MDTuple *N = nullptr;
104
105public:
106 DITypeRefArray() = default;
107 DITypeRefArray(const MDTuple *N) : N(N) {}
108
109 explicit operator bool() const { return get(); }
110 explicit operator MDTuple *() const { return get(); }
111
112 MDTuple *get() const { return const_cast<MDTuple *>(N); }
113 MDTuple *operator->() const { return get(); }
114 MDTuple &operator*() const { return *get(); }
115
116 // FIXME: Fix callers and remove condition on N.
117 unsigned size() const { return N ? N->getNumOperands() : 0u; }
118 DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
119
120 class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
121 std::ptrdiff_t, void, DITypeRef> {
122 MDNode::op_iterator I = nullptr;
123
124 public:
125 iterator() = default;
126 explicit iterator(MDNode::op_iterator I) : I(I) {}
127
128 DITypeRef operator*() const { return DITypeRef(*I); }
129
130 iterator &operator++() {
131 ++I;
132 return *this;
133 }
134
135 iterator operator++(int) {
136 iterator Temp(*this);
137 ++I;
138 return Temp;
139 }
140
141 bool operator==(const iterator &X) const { return I == X.I; }
142 bool operator!=(const iterator &X) const { return I != X.I; }
143 };
144
145 // FIXME: Fix callers and remove condition on N.
146 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
147 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
148};
149
150/// Tagged DWARF-like metadata node.
151///
152/// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
153/// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
154/// potentially used for non-DWARF output.
155class DINode : public MDNode {
156 friend class LLVMContextImpl;
157 friend class MDNode;
158
159protected:
160 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
161 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
162 : MDNode(C, ID, Storage, Ops1, Ops2) {
163 assert(Tag < 1u << 16);
164 SubclassData16 = Tag;
165 }
166 ~DINode() = default;
167
168 template <class Ty> Ty *getOperandAs(unsigned I) const {
169 return cast_or_null<Ty>(getOperand(I));
170 }
171
172 StringRef getStringOperand(unsigned I) const {
173 if (auto *S = getOperandAs<MDString>(I))
174 return S->getString();
175 return StringRef();
176 }
177
178 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
179 if (S.empty())
180 return nullptr;
181 return MDString::get(Context, S);
182 }
183
184 /// Allow subclasses to mutate the tag.
185 void setTag(unsigned Tag) { SubclassData16 = Tag; }
186
187public:
188 unsigned getTag() const { return SubclassData16; }
189
190 /// Debug info flags.
191 ///
192 /// The three accessibility flags are mutually exclusive and rolled together
193 /// in the first two bits.
194 enum DIFlags : uint32_t {
195#define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
196#define DI_FLAG_LARGEST_NEEDED
197#include "llvm/IR/DebugInfoFlags.def"
198 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
199 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
200 FlagVirtualInheritance,
201 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
202 };
203
204 static DIFlags getFlag(StringRef Flag);
205 static StringRef getFlagString(DIFlags Flag);
206
207 /// Split up a flags bitfield.
208 ///
209 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
210 /// any remaining (unrecognized) bits.
211 static DIFlags splitFlags(DIFlags Flags,
212 SmallVectorImpl<DIFlags> &SplitFlags);
213
214 static bool classof(const Metadata *MD) {
215 switch (MD->getMetadataID()) {
216 default:
217 return false;
218 case GenericDINodeKind:
219 case DISubrangeKind:
220 case DIEnumeratorKind:
221 case DIBasicTypeKind:
222 case DIDerivedTypeKind:
223 case DICompositeTypeKind:
224 case DISubroutineTypeKind:
225 case DIFileKind:
226 case DICompileUnitKind:
227 case DISubprogramKind:
228 case DILexicalBlockKind:
229 case DILexicalBlockFileKind:
230 case DINamespaceKind:
231 case DITemplateTypeParameterKind:
232 case DITemplateValueParameterKind:
233 case DIGlobalVariableKind:
234 case DILocalVariableKind:
235 case DIObjCPropertyKind:
236 case DIImportedEntityKind:
237 case DIModuleKind:
238 return true;
239 }
240 }
241};
242
243template <class T> struct simplify_type<const TypedDINodeRef<T>> {
244 using SimpleType = Metadata *;
245
246 static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
247 return MD;
248 }
249};
250
251template <class T>
252struct simplify_type<TypedDINodeRef<T>>
253 : simplify_type<const TypedDINodeRef<T>> {};
254
255/// Generic tagged DWARF-like metadata node.
256///
257/// An un-specialized DWARF-like metadata node. The first operand is a
258/// (possibly empty) null-separated \a MDString header that contains arbitrary
259/// fields. The remaining operands are \a dwarf_operands(), and are pointers
260/// to other metadata.
261class GenericDINode : public DINode {
262 friend class LLVMContextImpl;
263 friend class MDNode;
264
265 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
266 unsigned Tag, ArrayRef<Metadata *> Ops1,
267 ArrayRef<Metadata *> Ops2)
268 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
269 setHash(Hash);
270 }
271 ~GenericDINode() { dropAllReferences(); }
272
273 void setHash(unsigned Hash) { SubclassData32 = Hash; }
274 void recalculateHash();
275
276 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
277 StringRef Header, ArrayRef<Metadata *> DwarfOps,
278 StorageType Storage, bool ShouldCreate = true) {
279 return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
280 DwarfOps, Storage, ShouldCreate);
281 }
282
283 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
284 MDString *Header, ArrayRef<Metadata *> DwarfOps,
285 StorageType Storage, bool ShouldCreate = true);
286
287 TempGenericDINode cloneImpl() const {
288 return getTemporary(
289 getContext(), getTag(), getHeader(),
290 SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
291 }
292
293public:
294 unsigned getHash() const { return SubclassData32; }
295
296 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
297 ArrayRef<Metadata *> DwarfOps),
298 (Tag, Header, DwarfOps))
299 DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
300 ArrayRef<Metadata *> DwarfOps),
301 (Tag, Header, DwarfOps))
302
303 /// Return a (temporary) clone of this.
304 TempGenericDINode clone() const { return cloneImpl(); }
305
306 unsigned getTag() const { return SubclassData16; }
307 StringRef getHeader() const { return getStringOperand(0); }
308 MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
309
310 op_iterator dwarf_op_begin() const { return op_begin() + 1; }
311 op_iterator dwarf_op_end() const { return op_end(); }
312 op_range dwarf_operands() const {
313 return op_range(dwarf_op_begin(), dwarf_op_end());
314 }
315
316 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
317 const MDOperand &getDwarfOperand(unsigned I) const {
318 return getOperand(I + 1);
319 }
320 void replaceDwarfOperandWith(unsigned I, Metadata *New) {
321 replaceOperandWith(I + 1, New);
322 }
323
324 static bool classof(const Metadata *MD) {
325 return MD->getMetadataID() == GenericDINodeKind;
326 }
327};
328
329/// Array subrange.
330///
331/// TODO: Merge into node for DW_TAG_array_type, which should have a custom
332/// type.
333class DISubrange : public DINode {
334 friend class LLVMContextImpl;
335 friend class MDNode;
336
337 int64_t LowerBound;
338
339 DISubrange(LLVMContext &C, StorageType Storage, Metadata *Node,
340 int64_t LowerBound, ArrayRef<Metadata *> Ops)
341 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops),
342 LowerBound(LowerBound) {}
343
344 ~DISubrange() = default;
345
346 static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
347 int64_t LowerBound, StorageType Storage,
348 bool ShouldCreate = true);
349
350 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
351 int64_t LowerBound, StorageType Storage,
352 bool ShouldCreate = true);
353
354 TempDISubrange cloneImpl() const {
355 return getTemporary(getContext(), getRawCountNode(), getLowerBound());
356 }
357
358public:
359 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
360 (Count, LowerBound))
361
362 DEFINE_MDNODE_GET(DISubrange, (Metadata *CountNode, int64_t LowerBound = 0),
363 (CountNode, LowerBound))
364
365 TempDISubrange clone() const { return cloneImpl(); }
366
367 int64_t getLowerBound() const { return LowerBound; }
368
369 Metadata *getRawCountNode() const {
370 return getOperand(0).get();
371 }
372
373 typedef PointerUnion<ConstantInt*, DIVariable*> CountType;
374
375 CountType getCount() const {
376 if (auto *MD = dyn_cast<ConstantAsMetadata>(getRawCountNode()))
377 return CountType(cast<ConstantInt>(MD->getValue()));
378
379 if (auto *DV = dyn_cast<DIVariable>(getRawCountNode()))
380 return CountType(DV);
381
382 return CountType();
383 }
384
385 static bool classof(const Metadata *MD) {
386 return MD->getMetadataID() == DISubrangeKind;
387 }
388};
389
390/// Enumeration value.
391///
392/// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
393/// longer creates a type cycle.
394class DIEnumerator : public DINode {
395 friend class LLVMContextImpl;
396 friend class MDNode;
397
398 int64_t Value;
399 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
400 bool IsUnsigned, ArrayRef<Metadata *> Ops)
401 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
402 Value(Value) {
403 SubclassData32 = IsUnsigned;
404 }
405 ~DIEnumerator() = default;
406
407 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
408 bool IsUnsigned, StringRef Name,
409 StorageType Storage, bool ShouldCreate = true) {
410 return getImpl(Context, Value, IsUnsigned,
411 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
412 }
413 static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
414 bool IsUnsigned, MDString *Name,
415 StorageType Storage, bool ShouldCreate = true);
416
417 TempDIEnumerator cloneImpl() const {
418 return getTemporary(getContext(), getValue(), isUnsigned(), getName());
419 }
420
421public:
422 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name),
423 (Value, IsUnsigned, Name))
424 DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, MDString *Name),
425 (Value, IsUnsigned, Name))
426
427 TempDIEnumerator clone() const { return cloneImpl(); }
428
429 int64_t getValue() const { return Value; }
430 bool isUnsigned() const { return SubclassData32; }
431 StringRef getName() const { return getStringOperand(0); }
432
433 MDString *getRawName() const { return getOperandAs<MDString>(0); }
434
435 static bool classof(const Metadata *MD) {
436 return MD->getMetadataID() == DIEnumeratorKind;
437 }
438};
439
440/// Base class for scope-like contexts.
441///
442/// Base class for lexical scopes and types (which are also declaration
443/// contexts).
444///
445/// TODO: Separate the concepts of declaration contexts and lexical scopes.
446class DIScope : public DINode {
447protected:
448 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
449 ArrayRef<Metadata *> Ops)
450 : DINode(C, ID, Storage, Tag, Ops) {}
451 ~DIScope() = default;
452
453public:
454 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
455
456 inline StringRef getFilename() const;
457 inline StringRef getDirectory() const;
458 inline Optional<StringRef> getSource() const;
459
460 StringRef getName() const;
461 DIScopeRef getScope() const;
462
463 /// Return the raw underlying file.
464 ///
465 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
466 /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
467 /// Otherwise, return the first operand, which is where all other subclasses
468 /// store their file pointer.
469 Metadata *getRawFile() const {
470 return isa<DIFile>(this) ? const_cast<DIScope *>(this)
471 : static_cast<Metadata *>(getOperand(0));
472 }
473
474 static bool classof(const Metadata *MD) {
475 switch (MD->getMetadataID()) {
476 default:
477 return false;
478 case DIBasicTypeKind:
479 case DIDerivedTypeKind:
480 case DICompositeTypeKind:
481 case DISubroutineTypeKind:
482 case DIFileKind:
483 case DICompileUnitKind:
484 case DISubprogramKind:
485 case DILexicalBlockKind:
486 case DILexicalBlockFileKind:
487 case DINamespaceKind:
488 case DIModuleKind:
489 return true;
490 }
491 }
492};
493
494/// File.
495///
496/// TODO: Merge with directory/file node (including users).
497/// TODO: Canonicalize paths on creation.
498class DIFile : public DIScope {
499 friend class LLVMContextImpl;
500 friend class MDNode;
501
502public:
503 /// Which algorithm (e.g. MD5) a checksum was generated with.
504 ///
505 /// The encoding is explicit because it is used directly in Bitcode. The
506 /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
507 enum ChecksumKind {
508 // The first variant was originally CSK_None, encoded as 0. The new
509 // internal representation removes the need for this by wrapping the
510 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
511 // encoding is reserved.
512 CSK_MD5 = 1,
513 CSK_SHA1 = 2,
514 CSK_Last = CSK_SHA1 // Should be last enumeration.
515 };
516
517 /// A single checksum, represented by a \a Kind and a \a Value (a string).
518 template <typename T>
519 struct ChecksumInfo {
520 /// The kind of checksum which \a Value encodes.
521 ChecksumKind Kind;
522 /// The string value of the checksum.
523 T Value;
524
525 ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
526 ~ChecksumInfo() = default;
527 bool operator==(const ChecksumInfo<T> &X) const {
528 return Kind == X.Kind && Value == X.Value;
529 }
530 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
531 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
532 };
533
534private:
535 Optional<ChecksumInfo<MDString *>> Checksum;
536 Optional<MDString *> Source;
537
538 DIFile(LLVMContext &C, StorageType Storage,
539 Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src,
540 ArrayRef<Metadata *> Ops)
541 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
542 Checksum(CS), Source(Src) {}
543 ~DIFile() = default;
544
545 static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
546 StringRef Directory,
547 Optional<ChecksumInfo<StringRef>> CS,
548 Optional<StringRef> Source,
549 StorageType Storage, bool ShouldCreate = true) {
550 Optional<ChecksumInfo<MDString *>> MDChecksum;
551 if (CS)
552 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
553 return getImpl(Context, getCanonicalMDString(Context, Filename),
554 getCanonicalMDString(Context, Directory), MDChecksum,
555 Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) : None,
556 Storage, ShouldCreate);
557 }
558 static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
559 MDString *Directory,
560 Optional<ChecksumInfo<MDString *>> CS,
561 Optional<MDString *> Source, StorageType Storage,
562 bool ShouldCreate = true);
563
564 TempDIFile cloneImpl() const {
565 return getTemporary(getContext(), getFilename(), getDirectory(),
566 getChecksum(), getSource());
567 }
568
569public:
570 DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
571 Optional<ChecksumInfo<StringRef>> CS = None,
572 Optional<StringRef> Source = None),
573 (Filename, Directory, CS, Source))
574 DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
575 Optional<ChecksumInfo<MDString *>> CS = None,
576 Optional<MDString *> Source = None),
577 (Filename, Directory, CS, Source))
578
579 TempDIFile clone() const { return cloneImpl(); }
580
581 StringRef getFilename() const { return getStringOperand(0); }
582 StringRef getDirectory() const { return getStringOperand(1); }
583 Optional<ChecksumInfo<StringRef>> getChecksum() const {
584 Optional<ChecksumInfo<StringRef>> StringRefChecksum;
585 if (Checksum)
586 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
587 return StringRefChecksum;
588 }
589 Optional<StringRef> getSource() const {
590 return Source ? Optional<StringRef>((*Source)->getString()) : None;
591 }
592
593 MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
594 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
595 Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }
596 Optional<MDString *> getRawSource() const { return Source; }
597
598 static StringRef getChecksumKindAsString(ChecksumKind CSKind);
599 static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
600
601 static bool classof(const Metadata *MD) {
602 return MD->getMetadataID() == DIFileKind;
603 }
604};
605
606StringRef DIScope::getFilename() const {
607 if (auto *F = getFile())
608 return F->getFilename();
609 return "";
610}
611
612StringRef DIScope::getDirectory() const {
613 if (auto *F = getFile())
614 return F->getDirectory();
615 return "";
616}
617
618Optional<StringRef> DIScope::getSource() const {
619 if (auto *F = getFile())
620 return F->getSource();
621 return None;
622}
623
624/// Base class for types.
625///
626/// TODO: Remove the hardcoded name and context, since many types don't use
627/// them.
628/// TODO: Split up flags.
629class DIType : public DIScope {
630 unsigned Line;
631 DIFlags Flags;
632 uint64_t SizeInBits;
633 uint64_t OffsetInBits;
634 uint32_t AlignInBits;
635
636protected:
637 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
638 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
639 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
640 : DIScope(C, ID, Storage, Tag, Ops) {
641 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
642 }
643 ~DIType() = default;
644
645 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
646 uint64_t OffsetInBits, DIFlags Flags) {
647 this->Line = Line;
648 this->Flags = Flags;
649 this->SizeInBits = SizeInBits;
650 this->AlignInBits = AlignInBits;
651 this->OffsetInBits = OffsetInBits;
652 }
653
654 /// Change fields in place.
655 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
656 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
657 assert(isDistinct() && "Only distinct nodes can mutate");
658 setTag(Tag);
659 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
660 }
661
662public:
663 TempDIType clone() const {
664 return TempDIType(cast<DIType>(MDNode::clone().release()));
665 }
666
667 unsigned getLine() const { return Line; }
668 uint64_t getSizeInBits() const { return SizeInBits; }
669 uint32_t getAlignInBits() const { return AlignInBits; }
670 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
671 uint64_t getOffsetInBits() const { return OffsetInBits; }
672 DIFlags getFlags() const { return Flags; }
673
674 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
675 StringRef getName() const { return getStringOperand(2); }
676
677
678 Metadata *getRawScope() const { return getOperand(1); }
679 MDString *getRawName() const { return getOperandAs<MDString>(2); }
680
681 void setFlags(DIFlags NewFlags) {
682 assert(!isUniqued() && "Cannot set flags on uniqued nodes");
683 Flags = NewFlags;
684 }
685
686 bool isPrivate() const {
687 return (getFlags() & FlagAccessibility) == FlagPrivate;
688 }
689 bool isProtected() const {
690 return (getFlags() & FlagAccessibility) == FlagProtected;
691 }
692 bool isPublic() const {
693 return (getFlags() & FlagAccessibility) == FlagPublic;
694 }
695 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
696 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
697 bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
698 bool isVirtual() const { return getFlags() & FlagVirtual; }
699 bool isArtificial() const { return getFlags() & FlagArtificial; }
700 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
701 bool isObjcClassComplete() const {
702 return getFlags() & FlagObjcClassComplete;
703 }
704 bool isVector() const { return getFlags() & FlagVector; }
705 bool isBitField() const { return getFlags() & FlagBitField; }
706 bool isStaticMember() const { return getFlags() & FlagStaticMember; }
707 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
708 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
709 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
710 bool isTypePassByReference() const {
711 return getFlags() & FlagTypePassByReference;
712 }
713
714 static bool classof(const Metadata *MD) {
715 switch (MD->getMetadataID()) {
716 default:
717 return false;
718 case DIBasicTypeKind:
719 case DIDerivedTypeKind:
720 case DICompositeTypeKind:
721 case DISubroutineTypeKind:
722 return true;
723 }
724 }
725};
726
727/// Basic type, like 'int' or 'float'.
728///
729/// TODO: Split out DW_TAG_unspecified_type.
730/// TODO: Drop unused accessors.
731class DIBasicType : public DIType {
732 friend class LLVMContextImpl;
733 friend class MDNode;
734
735 unsigned Encoding;
736
737 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
738 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
739 ArrayRef<Metadata *> Ops)
740 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
741 FlagZero, Ops),
742 Encoding(Encoding) {}
743 ~DIBasicType() = default;
744
745 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
746 StringRef Name, uint64_t SizeInBits,
747 uint32_t AlignInBits, unsigned Encoding,
748 StorageType Storage, bool ShouldCreate = true) {
749 return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
750 SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
751 }
752 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
753 MDString *Name, uint64_t SizeInBits,
754 uint32_t AlignInBits, unsigned Encoding,
755 StorageType Storage, bool ShouldCreate = true);
756
757 TempDIBasicType cloneImpl() const {
758 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
759 getAlignInBits(), getEncoding());
760 }
761
762public:
763 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
764 (Tag, Name, 0, 0, 0))
765 DEFINE_MDNODE_GET(DIBasicType,
766 (unsigned Tag, StringRef Name, uint64_t SizeInBits,
767 uint32_t AlignInBits, unsigned Encoding),
768 (Tag, Name, SizeInBits, AlignInBits, Encoding))
769 DEFINE_MDNODE_GET(DIBasicType,
770 (unsigned Tag, MDString *Name, uint64_t SizeInBits,
771 uint32_t AlignInBits, unsigned Encoding),
772 (Tag, Name, SizeInBits, AlignInBits, Encoding))
773
774 TempDIBasicType clone() const { return cloneImpl(); }
775
776 unsigned getEncoding() const { return Encoding; }
777
778 static bool classof(const Metadata *MD) {
779 return MD->getMetadataID() == DIBasicTypeKind;
780 }
781};
782
783/// Derived types.
784///
785/// This includes qualified types, pointers, references, friends, typedefs, and
786/// class members.
787///
788/// TODO: Split out members (inheritance, fields, methods, etc.).
789class DIDerivedType : public DIType {
790 friend class LLVMContextImpl;
791 friend class MDNode;
792
793 /// \brief The DWARF address space of the memory pointed to or referenced by a
794 /// pointer or reference type respectively.
795 Optional<unsigned> DWARFAddressSpace;
796
797 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
798 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
799 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
800 DIFlags Flags, ArrayRef<Metadata *> Ops)
801 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
802 AlignInBits, OffsetInBits, Flags, Ops),
803 DWARFAddressSpace(DWARFAddressSpace) {}
804 ~DIDerivedType() = default;
805
806 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
807 StringRef Name, DIFile *File, unsigned Line,
808 DIScopeRef Scope, DITypeRef BaseType,
809 uint64_t SizeInBits, uint32_t AlignInBits,
810 uint64_t OffsetInBits,
811 Optional<unsigned> DWARFAddressSpace,
812 DIFlags Flags, Metadata *ExtraData,
813 StorageType Storage, bool ShouldCreate = true) {
814 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
815 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
816 DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
817 }
818 static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
819 MDString *Name, Metadata *File, unsigned Line,
820 Metadata *Scope, Metadata *BaseType,
821 uint64_t SizeInBits, uint32_t AlignInBits,
822 uint64_t OffsetInBits,
823 Optional<unsigned> DWARFAddressSpace,
824 DIFlags Flags, Metadata *ExtraData,
825 StorageType Storage, bool ShouldCreate = true);
826
827 TempDIDerivedType cloneImpl() const {
828 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
829 getScope(), getBaseType(), getSizeInBits(),
830 getAlignInBits(), getOffsetInBits(),
831 getDWARFAddressSpace(), getFlags(), getExtraData());
832 }
833
834public:
835 DEFINE_MDNODE_GET(DIDerivedType,
836 (unsigned Tag, MDString *Name, Metadata *File,
837 unsigned Line, Metadata *Scope, Metadata *BaseType,
838 uint64_t SizeInBits, uint32_t AlignInBits,
839 uint64_t OffsetInBits,
840 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
841 Metadata *ExtraData = nullptr),
842 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
843 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
844 ExtraData))
845 DEFINE_MDNODE_GET(DIDerivedType,
846 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
847 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
848 uint32_t AlignInBits, uint64_t OffsetInBits,
849 Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
850 Metadata *ExtraData = nullptr),
851 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
852 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
853 ExtraData))
854
855 TempDIDerivedType clone() const { return cloneImpl(); }
856
857 /// Get the base type this is derived from.
858 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
859 Metadata *getRawBaseType() const { return getOperand(3); }
860
861 /// \returns The DWARF address space of the memory pointed to or referenced by
862 /// a pointer or reference type respectively.
863 Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
864
865 /// Get extra data associated with this derived type.
866 ///
867 /// Class type for pointer-to-members, objective-c property node for ivars,
868 /// or global constant wrapper for static members.
869 ///
870 /// TODO: Separate out types that need this extra operand: pointer-to-member
871 /// types and member fields (static members and ivars).
872 Metadata *getExtraData() const { return getRawExtraData(); }
873 Metadata *getRawExtraData() const { return getOperand(4); }
874
875 /// Get casted version of extra data.
876 /// @{
877 DITypeRef getClassType() const {
878 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
879 return DITypeRef(getExtraData());
880 }
881
882 DIObjCProperty *getObjCProperty() const {
883 return dyn_cast_or_null<DIObjCProperty>(getExtraData());
884 }
885
886 Constant *getStorageOffsetInBits() const {
887 assert(getTag() == dwarf::DW_TAG_member && isBitField());
888 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
889 return C->getValue();
890 return nullptr;
891 }
892
893 Constant *getConstant() const {
894 assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
895 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
896 return C->getValue();
897 return nullptr;
898 }
899 Constant *getDiscriminantValue() const {
900 assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
901 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
902 return C->getValue();
903 return nullptr;
904 }
905 /// @}
906
907 static bool classof(const Metadata *MD) {
908 return MD->getMetadataID() == DIDerivedTypeKind;
909 }
910};
911
912/// Composite types.
913///
914/// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
915/// TODO: Create a custom, unrelated node for DW_TAG_array_type.
916class DICompositeType : public DIType {
917 friend class LLVMContextImpl;
918 friend class MDNode;
919
920 unsigned RuntimeLang;
921
922 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
923 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
924 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
925 ArrayRef<Metadata *> Ops)
926 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
927 AlignInBits, OffsetInBits, Flags, Ops),
928 RuntimeLang(RuntimeLang) {}
929 ~DICompositeType() = default;
930
931 /// Change fields in place.
932 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
933 uint64_t SizeInBits, uint32_t AlignInBits,
934 uint64_t OffsetInBits, DIFlags Flags) {
935 assert(isDistinct() && "Only distinct nodes can mutate");
936 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
937 this->RuntimeLang = RuntimeLang;
938 DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
939 }
940
941 static DICompositeType *
942 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
943 unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
944 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
945 DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
946 DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
947 StringRef Identifier, DIDerivedType *Discriminator,
948 StorageType Storage, bool ShouldCreate = true) {
949 return getImpl(
950 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
951 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
952 RuntimeLang, VTableHolder, TemplateParams.get(),
953 getCanonicalMDString(Context, Identifier), Discriminator, Storage, ShouldCreate);
954 }
955 static DICompositeType *
956 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
957 unsigned Line, Metadata *Scope, Metadata *BaseType,
958 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
959 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
960 Metadata *VTableHolder, Metadata *TemplateParams,
961 MDString *Identifier, Metadata *Discriminator,
962 StorageType Storage, bool ShouldCreate = true);
963
964 TempDICompositeType cloneImpl() const {
965 return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
966 getScope(), getBaseType(), getSizeInBits(),
967 getAlignInBits(), getOffsetInBits(), getFlags(),
968 getElements(), getRuntimeLang(), getVTableHolder(),
969 getTemplateParams(), getIdentifier(), getDiscriminator());
970 }
971
972public:
973 DEFINE_MDNODE_GET(DICompositeType,
974 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
975 DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
976 uint32_t AlignInBits, uint64_t OffsetInBits,
977 DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
978 DITypeRef VTableHolder,
979 DITemplateParameterArray TemplateParams = nullptr,
980 StringRef Identifier = "", DIDerivedType *Discriminator = nullptr),
981 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
982 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
983 VTableHolder, TemplateParams, Identifier, Discriminator))
984 DEFINE_MDNODE_GET(DICompositeType,
985 (unsigned Tag, MDString *Name, Metadata *File,
986 unsigned Line, Metadata *Scope, Metadata *BaseType,
987 uint64_t SizeInBits, uint32_t AlignInBits,
988 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
989 unsigned RuntimeLang, Metadata *VTableHolder,
990 Metadata *TemplateParams = nullptr,
991 MDString *Identifier = nullptr,
992 Metadata *Discriminator = nullptr),
993 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
994 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
995 VTableHolder, TemplateParams, Identifier, Discriminator))
996
997 TempDICompositeType clone() const { return cloneImpl(); }
998
999 /// Get a DICompositeType with the given ODR identifier.
1000 ///
1001 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
1002 /// DICompositeType for the given ODR \c Identifier. If none exists, creates
1003 /// a new node.
1004 ///
1005 /// Else, returns \c nullptr.
1006 static DICompositeType *
1007 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1008 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1009 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1010 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
1011 unsigned RuntimeLang, Metadata *VTableHolder,
1012 Metadata *TemplateParams, Metadata *Discriminator);
1013 static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
1014 MDString &Identifier);
1015
1016 /// Build a DICompositeType with the given ODR identifier.
1017 ///
1018 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
1019 /// it doesn't exist, creates a new one. If it does exist and \a
1020 /// isForwardDecl(), and the new arguments would be a definition, mutates the
1021 /// the type in place. In either case, returns the type.
1022 ///
1023 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
1024 /// nullptr.
1025 static DICompositeType *
1026 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1027 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1028 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1029 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
1030 unsigned RuntimeLang, Metadata *VTableHolder,
1031 Metadata *TemplateParams, Metadata *Discriminator);
1032
1033 DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
1034 DINodeArray getElements() const {
1035 return cast_or_null<MDTuple>(getRawElements());
1036 }
1037 DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
1038 DITemplateParameterArray getTemplateParams() const {
1039 return cast_or_null<MDTuple>(getRawTemplateParams());
1040 }
1041 StringRef getIdentifier() const { return getStringOperand(7); }
1042 unsigned getRuntimeLang() const { return RuntimeLang; }
1043
1044 Metadata *getRawBaseType() const { return getOperand(3); }
1045 Metadata *getRawElements() const { return getOperand(4); }
1046 Metadata *getRawVTableHolder() const { return getOperand(5); }
1047 Metadata *getRawTemplateParams() const { return getOperand(6); }
1048 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1049 Metadata *getRawDiscriminator() const { return getOperand(8); }
1050 DIDerivedType *getDiscriminator() const { return getOperandAs<DIDerivedType>(8); }
1051
1052 /// Replace operands.
1053 ///
1054 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
1055 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
1056 /// of its movement if necessary.
1057 /// @{
1058 void replaceElements(DINodeArray Elements) {
1059#ifndef NDEBUG
1060 for (DINode *Op : getElements())
1061 assert(is_contained(Elements->operands(), Op) &&
1062 "Lost a member during member list replacement");
1063#endif
1064 replaceOperandWith(4, Elements.get());
1065 }
1066
1067 void replaceVTableHolder(DITypeRef VTableHolder) {
1068 replaceOperandWith(5, VTableHolder);
1069 }
1070
1071 void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
1072 replaceOperandWith(6, TemplateParams.get());
1073 }
1074 /// @}
1075
1076 static bool classof(const Metadata *MD) {
1077 return MD->getMetadataID() == DICompositeTypeKind;
1078 }
1079};
1080
1081/// Type array for a subprogram.
1082///
1083/// TODO: Fold the array of types in directly as operands.
1084class DISubroutineType : public DIType {
1085 friend class LLVMContextImpl;
1086 friend class MDNode;
1087
1088 /// The calling convention used with DW_AT_calling_convention. Actually of
1089 /// type dwarf::CallingConvention.
1090 uint8_t CC;
1091
1092 DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
1093 uint8_t CC, ArrayRef<Metadata *> Ops)
1094 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type,
1095 0, 0, 0, 0, Flags, Ops),
1096 CC(CC) {}
1097 ~DISubroutineType() = default;
1098
1099 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1100 uint8_t CC, DITypeRefArray TypeArray,
1101 StorageType Storage,
1102 bool ShouldCreate = true) {
1103 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
1104 }
1105 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
1106 uint8_t CC, Metadata *TypeArray,
1107 StorageType Storage,
1108 bool ShouldCreate = true);
1109
1110 TempDISubroutineType cloneImpl() const {
1111 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
1112 }
1113
1114public:
1115 DEFINE_MDNODE_GET(DISubroutineType,
1116 (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
1117 (Flags, CC, TypeArray))
1118 DEFINE_MDNODE_GET(DISubroutineType,
1119 (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
1120 (Flags, CC, TypeArray))
1121
1122 TempDISubroutineType clone() const { return cloneImpl(); }
1123
1124 uint8_t getCC() const { return CC; }
1125
1126 DITypeRefArray getTypeArray() const {
1127 return cast_or_null<MDTuple>(getRawTypeArray());
1128 }
1129
1130 Metadata *getRawTypeArray() const { return getOperand(3); }
1131
1132 static bool classof(const Metadata *MD) {
1133 return MD->getMetadataID() == DISubroutineTypeKind;
1134 }
1135};
1136
1137/// Compile unit.
1138class DICompileUnit : public DIScope {
1139 friend class LLVMContextImpl;
1140 friend class MDNode;
1141
1142public:
1143 enum DebugEmissionKind : unsigned {
1144 NoDebug = 0,
1145 FullDebug,
1146 LineTablesOnly,
1147 LastEmissionKind = LineTablesOnly
1148 };
1149
1150 static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1151 static const char *EmissionKindString(DebugEmissionKind EK);
1152
1153private:
1154 unsigned SourceLanguage;
1155 bool IsOptimized;
1156 unsigned RuntimeVersion;
1157 unsigned EmissionKind;
1158 uint64_t DWOId;
1159 bool SplitDebugInlining;
1160 bool DebugInfoForProfiling;
1161 bool GnuPubnames;
1162
1163 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
1164 bool IsOptimized, unsigned RuntimeVersion,
1165 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
1166 bool DebugInfoForProfiling, bool GnuPubnames, ArrayRef<Metadata *> Ops)
1167 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
1168 SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
1169 RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
1170 DWOId(DWOId), SplitDebugInlining(SplitDebugInlining),
1171 DebugInfoForProfiling(DebugInfoForProfiling), GnuPubnames(GnuPubnames) {
1172 assert(Storage != Uniqued);
1173 }
1174 ~DICompileUnit() = default;
1175
1176 static DICompileUnit *
1177 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
1178 StringRef Producer, bool IsOptimized, StringRef Flags,
1179 unsigned RuntimeVersion, StringRef SplitDebugFilename,
1180 unsigned EmissionKind, DICompositeTypeArray EnumTypes,
1181 DIScopeArray RetainedTypes,
1182 DIGlobalVariableExpressionArray GlobalVariables,
1183 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1184 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1185 bool GnuPubnames, StorageType Storage, bool ShouldCreate = true) {
1186 return getImpl(
1187 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
1188 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
1189 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
1190 EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
1191 ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
1192 DebugInfoForProfiling, GnuPubnames, Storage, ShouldCreate);
1193 }
1194 static DICompileUnit *
1195 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1196 MDString *Producer, bool IsOptimized, MDString *Flags,
1197 unsigned RuntimeVersion, MDString *SplitDebugFilename,
1198 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1199 Metadata *GlobalVariables, Metadata *ImportedEntities,
1200 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
1201 bool DebugInfoForProfiling, bool GnuPubnames, StorageType Storage,
1202 bool ShouldCreate = true);
1203
1204 TempDICompileUnit cloneImpl() const {
1205 return getTemporary(getContext(), getSourceLanguage(), getFile(),
1206 getProducer(), isOptimized(), getFlags(),
1207 getRuntimeVersion(), getSplitDebugFilename(),
1208 getEmissionKind(), getEnumTypes(), getRetainedTypes(),
1209 getGlobalVariables(), getImportedEntities(),
1210 getMacros(), DWOId, getSplitDebugInlining(),
1211 getDebugInfoForProfiling(), getGnuPubnames());
1212 }
1213
1214public:
1215 static void get() = delete;
1216 static void getIfExists() = delete;
1217
1218 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1219 DICompileUnit,
1220 (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1221 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1222 StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
1223 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
1224 DIGlobalVariableExpressionArray GlobalVariables,
1225 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
1226 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
1227 bool GnuPubnames),
1228 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1229 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1230 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1231 DebugInfoForProfiling, GnuPubnames))
1232 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
1233 DICompileUnit,
1234 (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1235 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1236 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1237 Metadata *RetainedTypes, Metadata *GlobalVariables,
1238 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
1239 bool SplitDebugInlining, bool DebugInfoForProfiling, bool GnuPubnames),
1240 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1241 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
1242 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
1243 DebugInfoForProfiling, GnuPubnames))
1244
1245 TempDICompileUnit clone() const { return cloneImpl(); }
1246
1247 unsigned getSourceLanguage() const { return SourceLanguage; }
1248 bool isOptimized() const { return IsOptimized; }
1249 unsigned getRuntimeVersion() const { return RuntimeVersion; }
1250 DebugEmissionKind getEmissionKind() const {
1251 return (DebugEmissionKind)EmissionKind;
1252 }
1253 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
1254 bool getGnuPubnames() const { return GnuPubnames; }
1255 StringRef getProducer() const { return getStringOperand(1); }
1256 StringRef getFlags() const { return getStringOperand(2); }
1257 StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1258 DICompositeTypeArray getEnumTypes() const {
1259 return cast_or_null<MDTuple>(getRawEnumTypes());
1260 }
1261 DIScopeArray getRetainedTypes() const {
1262 return cast_or_null<MDTuple>(getRawRetainedTypes());
1263 }
1264 DIGlobalVariableExpressionArray getGlobalVariables() const {
1265 return cast_or_null<MDTuple>(getRawGlobalVariables());
1266 }
1267 DIImportedEntityArray getImportedEntities() const {
1268 return cast_or_null<MDTuple>(getRawImportedEntities());
1269 }
1270 DIMacroNodeArray getMacros() const {
1271 return cast_or_null<MDTuple>(getRawMacros());
1272 }
1273 uint64_t getDWOId() const { return DWOId; }
1274 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
1275 bool getSplitDebugInlining() const { return SplitDebugInlining; }
1276 void setSplitDebugInlining(bool SplitDebugInlining) {
1277 this->SplitDebugInlining = SplitDebugInlining;
1278 }
1279
1280 MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1281 MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1282 MDString *getRawSplitDebugFilename() const {
1283 return getOperandAs<MDString>(3);
1284 }
1285 Metadata *getRawEnumTypes() const { return getOperand(4); }
1286 Metadata *getRawRetainedTypes() const { return getOperand(5); }
1287 Metadata *getRawGlobalVariables() const { return getOperand(6); }
1288 Metadata *getRawImportedEntities() const { return getOperand(7); }
1289 Metadata *getRawMacros() const { return getOperand(8); }
1290
1291 /// Replace arrays.
1292 ///
1293 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1294 /// deleted on a uniquing collision. In practice, uniquing collisions on \a
1295 /// DICompileUnit should be fairly rare.
1296 /// @{
1297 void replaceEnumTypes(DICompositeTypeArray N) {
1298 replaceOperandWith(4, N.get());
1299 }
1300 void replaceRetainedTypes(DITypeArray N) {
1301 replaceOperandWith(5, N.get());
1302 }
1303 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
1304 replaceOperandWith(6, N.get());
1305 }
1306 void replaceImportedEntities(DIImportedEntityArray N) {
1307 replaceOperandWith(7, N.get());
1308 }
1309 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
1310 /// @}
1311
1312 static bool classof(const Metadata *MD) {
1313 return MD->getMetadataID() == DICompileUnitKind;
1314 }
1315};
1316
1317/// A scope for locals.
1318///
1319/// A legal scope for lexical blocks, local variables, and debug info
1320/// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1321/// DILexicalBlockFile.
1322class DILocalScope : public DIScope {
1323protected:
1324 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1325 ArrayRef<Metadata *> Ops)
1326 : DIScope(C, ID, Storage, Tag, Ops) {}
1327 ~DILocalScope() = default;
1328
1329public:
1330 /// Get the subprogram for this scope.
1331 ///
1332 /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1333 /// chain.
1334 DISubprogram *getSubprogram() const;
1335
1336 /// Get the first non DILexicalBlockFile scope of this scope.
1337 ///
1338 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
1339 /// scope chain.
1340 DILocalScope *getNonLexicalBlockFileScope() const;
1341
1342 static bool classof(const Metadata *MD) {
1343 return MD->getMetadataID() == DISubprogramKind ||
1344 MD->getMetadataID() == DILexicalBlockKind ||
1345 MD->getMetadataID() == DILexicalBlockFileKind;
1346 }
1347};
1348
1349/// Debug location.
1350///
1351/// A debug location in source code, used for debug info and otherwise.
1352class DILocation : public MDNode {
1353 friend class LLVMContextImpl;
1354 friend class MDNode;
1355
1356 DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1357 unsigned Column, ArrayRef<Metadata *> MDs);
1358 ~DILocation() { dropAllReferences(); }
1359
1360 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1361 unsigned Column, Metadata *Scope,
1362 Metadata *InlinedAt, StorageType Storage,
1363 bool ShouldCreate = true);
1364 static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1365 unsigned Column, DILocalScope *Scope,
1366 DILocation *InlinedAt, StorageType Storage,
1367 bool ShouldCreate = true) {
1368 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1369 static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1370 }
1371
1372 /// With a given unsigned int \p U, use up to 13 bits to represent it.
1373 /// old_bit 1~5 --> new_bit 1~5
1374 /// old_bit 6~12 --> new_bit 7~13
1375 /// new_bit_6 is 0 if higher bits (7~13) are all 0
1376 static unsigned getPrefixEncodingFromUnsigned(unsigned U) {
1377 U &= 0xfff;
1378 return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
1379 }
1380
1381 /// Reverse transformation as getPrefixEncodingFromUnsigned.
1382 static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
1383 return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
1384 }
1385
1386 /// Returns the next component stored in discriminator.
1387 static unsigned getNextComponentInDiscriminator(unsigned D) {
1388 if ((D & 1) == 0)
1389 return D >> ((D & 0x40) ? 14 : 7);
1390 else
1391 return D >> 1;
1392 }
1393
1394 TempDILocation cloneImpl() const {
1395 // Get the raw scope/inlinedAt since it is possible to invoke this on
1396 // a DILocation containing temporary metadata.
1397 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
1398 getRawInlinedAt());
1399 }
1400
1401public:
1402 // Disallow replacing operands.
1403 void replaceOperandWith(unsigned I, Metadata *New) = delete;
1404
1405 DEFINE_MDNODE_GET(DILocation,
1406 (unsigned Line, unsigned Column, Metadata *Scope,
1407 Metadata *InlinedAt = nullptr),
1408 (Line, Column, Scope, InlinedAt))
1409 DEFINE_MDNODE_GET(DILocation,
1410 (unsigned Line, unsigned Column, DILocalScope *Scope,
1411 DILocation *InlinedAt = nullptr),
1412 (Line, Column, Scope, InlinedAt))
1413
1414 /// Return a (temporary) clone of this.
1415 TempDILocation clone() const { return cloneImpl(); }
1416
1417 unsigned getLine() const { return SubclassData32; }
1418 unsigned getColumn() const { return SubclassData16; }
1419 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1420
1421 DILocation *getInlinedAt() const {
1422 return cast_or_null<DILocation>(getRawInlinedAt());
1423 }
1424
1425 DIFile *getFile() const { return getScope()->getFile(); }
1426 StringRef getFilename() const { return getScope()->getFilename(); }
1427 StringRef getDirectory() const { return getScope()->getDirectory(); }
1428 Optional<StringRef> getSource() const { return getScope()->getSource(); }
1429
1430 /// Get the scope where this is inlined.
1431 ///
1432 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1433 /// location.
1434 DILocalScope *getInlinedAtScope() const {
1435 if (auto *IA = getInlinedAt())
1436 return IA->getInlinedAtScope();
1437 return getScope();
1438 }
1439
1440 /// Check whether this can be discriminated from another location.
1441 ///
1442 /// Check \c this can be discriminated from \c RHS in a linetable entry.
1443 /// Scope and inlined-at chains are not recorded in the linetable, so they
1444 /// cannot be used to distinguish basic blocks.
1445 bool canDiscriminate(const DILocation &RHS) const {
1446 return getLine() != RHS.getLine() ||
1447 getColumn() != RHS.getColumn() ||
1448 getDiscriminator() != RHS.getDiscriminator() ||
1449 getFilename() != RHS.getFilename() ||
1450 getDirectory() != RHS.getDirectory();
1451 }
1452
1453 /// Get the DWARF discriminator.
1454 ///
1455 /// DWARF discriminators distinguish identical file locations between
1456 /// instructions that are on different basic blocks.
1457 ///
1458 /// There are 3 components stored in discriminator, from lower bits:
1459 ///
1460 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
1461 /// that are defined by the same source line, but
1462 /// different basic blocks.
1463 /// Duplication factor: assigned by optimizations that will scale down
1464 /// the execution frequency of the original IR.
1465 /// Copy Identifier: assigned by optimizations that clones the IR.
1466 /// Each copy of the IR will be assigned an identifier.
1467 ///
1468 /// Encoding:
1469 ///
1470 /// The above 3 components are encoded into a 32bit unsigned integer in
1471 /// order. If the lowest bit is 1, the current component is empty, and the
1472 /// next component will start in the next bit. Otherwise, the current
1473 /// component is non-empty, and its content starts in the next bit. The
1474 /// length of each components is either 5 bit or 12 bit: if the 7th bit
1475 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
1476 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1477 /// represent the component.
1478
1479 inline unsigned getDiscriminator() const;
1480
1481 /// Returns a new DILocation with updated \p Discriminator.
1482 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
1483
1484 /// Returns a new DILocation with updated base discriminator \p BD.
1485 inline const DILocation *setBaseDiscriminator(unsigned BD) const;
1486
1487 /// Returns the duplication factor stored in the discriminator.
1488 inline unsigned getDuplicationFactor() const;
1489
1490 /// Returns the copy identifier stored in the discriminator.
1491 inline unsigned getCopyIdentifier() const;
1492
1493 /// Returns the base discriminator stored in the discriminator.
1494 inline unsigned getBaseDiscriminator() const;
1495
1496 /// Returns a new DILocation with duplication factor \p DF encoded in the
1497 /// discriminator.
1498 inline const DILocation *cloneWithDuplicationFactor(unsigned DF) const;
1499
1500 /// When two instructions are combined into a single instruction we also
1501 /// need to combine the original locations into a single location.
1502 ///
1503 /// When the locations are the same we can use either location. When they
1504 /// differ, we need a third location which is distinct from either. If
1505 /// they have the same file/line but have a different discriminator we
1506 /// could create a location with a new discriminator. If they are from
1507 /// different files/lines the location is ambiguous and can't be
1508 /// represented in a single line entry. In this case, no location
1509 /// should be set, unless the merged instruction is a call, which we will
1510 /// set the merged debug location as line 0 of the nearest common scope
1511 /// where 2 locations are inlined from. This only applies to Instruction;
1512 /// for MachineInstruction, as it is post-inline, we will treat the call
1513 /// instruction the same way as other instructions.
1514 ///
1515 /// \p ForInst: The Instruction the merged DILocation is for. If the
1516 /// Instruction is unavailable or non-existent, use nullptr.
1517 static const DILocation *
1518 getMergedLocation(const DILocation *LocA, const DILocation *LocB,
1519 const Instruction *ForInst = nullptr);
1520
1521 /// Returns the base discriminator for a given encoded discriminator \p D.
1522 static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
1523 if ((D & 1) == 0)
1524 return getUnsignedFromPrefixEncoding(D >> 1);
1525 else
1526 return 0;
1527 }
1528
1529 /// Returns the duplication factor for a given encoded discriminator \p D.
1530 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
1531 D = getNextComponentInDiscriminator(D);
1532 if (D == 0 || (D & 1))
1533 return 1;
1534 else
1535 return getUnsignedFromPrefixEncoding(D >> 1);
1536 }
1537
1538 /// Returns the copy identifier for a given encoded discriminator \p D.
1539 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
1540 return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
1541 getNextComponentInDiscriminator(D)));
1542 }
1543
1544
1545 Metadata *getRawScope() const { return getOperand(0); }
1546 Metadata *getRawInlinedAt() const {
1547 if (getNumOperands() == 2)
1548 return getOperand(1);
1549 return nullptr;
1550 }
1551
1552 static bool classof(const Metadata *MD) {
1553 return MD->getMetadataID() == DILocationKind;
1554 }
1555};
1556
1557/// Subprogram description.
1558///
1559/// TODO: Remove DisplayName. It's always equal to Name.
1560/// TODO: Split up flags.
1561class DISubprogram : public DILocalScope {
1562 friend class LLVMContextImpl;
1563 friend class MDNode;
1564
1565 unsigned Line;
1566 unsigned ScopeLine;
1567 unsigned VirtualIndex;
1568
1569 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1570 /// of method overrides from secondary bases by this amount. It may be
1571 /// negative.
1572 int ThisAdjustment;
1573
1574 // Virtuality can only assume three values, so we can pack
1575 // in 2 bits (none/pure/pure_virtual).
1576 unsigned Virtuality : 2;
1577
1578 // These are boolean flags so one bit is enough.
1579 // MSVC starts a new container field every time the base
1580 // type changes so we can't use 'bool' to ensure these bits
1581 // are packed.
1582 unsigned IsLocalToUnit : 1;
1583 unsigned IsDefinition : 1;
1584 unsigned IsOptimized : 1;
1585
1586 unsigned Padding : 3;
1587
1588 DIFlags Flags;
1589
1590 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1591 unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1592 int ThisAdjustment, DIFlags Flags, bool IsLocalToUnit,
1593 bool IsDefinition, bool IsOptimized, ArrayRef<Metadata *> Ops)
1594 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1595 Ops),
1596 Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
1597 ThisAdjustment(ThisAdjustment), Virtuality(Virtuality),
1598 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition),
1599 IsOptimized(IsOptimized), Flags(Flags) {
1600 static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
1601 assert(Virtuality < 4 && "Virtuality out of range");
1602 }
1603 ~DISubprogram() = default;
1604
1605 static DISubprogram *
1606 getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1607 StringRef LinkageName, DIFile *File, unsigned Line,
1608 DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1609 unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1610 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1611 bool IsOptimized, DICompileUnit *Unit,
1612 DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1613 DILocalVariableArray Variables, DITypeArray ThrownTypes,
1614 StorageType Storage, bool ShouldCreate = true) {
1615 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1616 getCanonicalMDString(Context, LinkageName), File, Line, Type,
1617 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1618 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1619 Unit, TemplateParams.get(), Declaration, Variables.get(),
1620 ThrownTypes.get(), Storage, ShouldCreate);
1621 }
1622 static DISubprogram *
1623 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1624 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1625 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1626 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1627 int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
1628 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1629 Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate = true);
1630
1631 TempDISubprogram cloneImpl() const {
1632 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1633 getFile(), getLine(), getType(), isLocalToUnit(),
1634 isDefinition(), getScopeLine(), getContainingType(),
1635 getVirtuality(), getVirtualIndex(), getThisAdjustment(),
1636 getFlags(), isOptimized(), getUnit(),
1637 getTemplateParams(), getDeclaration(), getVariables(),
1638 getThrownTypes());
1639 }
1640
1641public:
1642 DEFINE_MDNODE_GET(DISubprogram,
1643 (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1644 DIFile *File, unsigned Line, DISubroutineType *Type,
1645 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1646 DITypeRef ContainingType, unsigned Virtuality,
1647 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1648 bool IsOptimized, DICompileUnit *Unit,
1649 DITemplateParameterArray TemplateParams = nullptr,
1650 DISubprogram *Declaration = nullptr,
1651 DILocalVariableArray Variables = nullptr,
1652 DITypeArray ThrownTypes = nullptr),
1653 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1654 IsDefinition, ScopeLine, ContainingType, Virtuality,
1655 VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
1656 TemplateParams, Declaration, Variables, ThrownTypes))
1657 DEFINE_MDNODE_GET(
1658 DISubprogram,
1659 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1660 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1661 unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1662 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1663 bool IsOptimized, Metadata *Unit, Metadata *TemplateParams = nullptr,
1664 Metadata *Declaration = nullptr, Metadata *Variables = nullptr,
1665 Metadata *ThrownTypes = nullptr),
1666 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1667 ScopeLine, ContainingType, Virtuality, VirtualIndex, ThisAdjustment,
1668 Flags, IsOptimized, Unit, TemplateParams, Declaration, Variables,
1669 ThrownTypes))
1670
1671 TempDISubprogram clone() const { return cloneImpl(); }
1672
1673public:
1674 unsigned getLine() const { return Line; }
1675 unsigned getVirtuality() const { return Virtuality; }
1676 unsigned getVirtualIndex() const { return VirtualIndex; }
1677 int getThisAdjustment() const { return ThisAdjustment; }
1678 unsigned getScopeLine() const { return ScopeLine; }
1679 DIFlags getFlags() const { return Flags; }
1680 bool isLocalToUnit() const { return IsLocalToUnit; }
1681 bool isDefinition() const { return IsDefinition; }
1682 bool isOptimized() const { return IsOptimized; }
1683
1684 bool isArtificial() const { return getFlags() & FlagArtificial; }
1685 bool isPrivate() const {
1686 return (getFlags() & FlagAccessibility) == FlagPrivate;
1687 }
1688 bool isProtected() const {
1689 return (getFlags() & FlagAccessibility) == FlagProtected;
1690 }
1691 bool isPublic() const {
1692 return (getFlags() & FlagAccessibility) == FlagPublic;
1693 }
1694 bool isExplicit() const { return getFlags() & FlagExplicit; }
1695 bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1696 bool isMainSubprogram() const { return getFlags() & FlagMainSubprogram; }
1697
1698 /// Check if this is reference-qualified.
1699 ///
1700 /// Return true if this subprogram is a C++11 reference-qualified non-static
1701 /// member function (void foo() &).
1702 bool isLValueReference() const { return getFlags() & FlagLValueReference; }
1703
1704 /// Check if this is rvalue-reference-qualified.
1705 ///
1706 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1707 /// non-static member function (void foo() &&).
1708 bool isRValueReference() const { return getFlags() & FlagRValueReference; }
1709
1710 /// Check if this is marked as noreturn.
1711 ///
1712 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1713 bool isNoReturn() const { return getFlags() & FlagNoReturn; }
1714
1715 DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1716
1717 StringRef getName() const { return getStringOperand(2); }
1718 StringRef getLinkageName() const { return getStringOperand(3); }
1719
1720 DISubroutineType *getType() const {
1721 return cast_or_null<DISubroutineType>(getRawType());
1722 }
1723 DITypeRef getContainingType() const {
1724 return DITypeRef(getRawContainingType());
1725 }
1726
1727 DICompileUnit *getUnit() const {
1728 return cast_or_null<DICompileUnit>(getRawUnit());
1729 }
1730 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
1731 DITemplateParameterArray getTemplateParams() const {
1732 return cast_or_null<MDTuple>(getRawTemplateParams());
1733 }
1734 DISubprogram *getDeclaration() const {
1735 return cast_or_null<DISubprogram>(getRawDeclaration());
1736 }
1737 DILocalVariableArray getVariables() const {
1738 return cast_or_null<MDTuple>(getRawVariables());
1739 }
1740 DITypeArray getThrownTypes() const {
1741 return cast_or_null<MDTuple>(getRawThrownTypes());
1742 }
1743
1744 Metadata *getRawScope() const { return getOperand(1); }
1745 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1746 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
1747 Metadata *getRawType() const { return getOperand(4); }
1748 Metadata *getRawUnit() const { return getOperand(5); }
1749 Metadata *getRawDeclaration() const { return getOperand(6); }
1750 Metadata *getRawVariables() const { return getOperand(7); }
1751 Metadata *getRawContainingType() const {
1752 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
1753 }
1754 Metadata *getRawTemplateParams() const {
1755 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
1756 }
1757 Metadata *getRawThrownTypes() const {
1758 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
1759 }
1760
1761 /// Check if this subprogram describes the given function.
1762 ///
1763 /// FIXME: Should this be looking through bitcasts?
1764 bool describes(const Function *F) const;
1765
1766 static bool classof(const Metadata *MD) {
1767 return MD->getMetadataID() == DISubprogramKind;
1768 }
1769};
1770
1771class DILexicalBlockBase : public DILocalScope {
1772protected:
1773 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1774 ArrayRef<Metadata *> Ops)
1775 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1776 ~DILexicalBlockBase() = default;
1777
1778public:
1779 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1780
1781 Metadata *getRawScope() const { return getOperand(1); }
1782
1783 static bool classof(const Metadata *MD) {
1784 return MD->getMetadataID() == DILexicalBlockKind ||
1785 MD->getMetadataID() == DILexicalBlockFileKind;
1786 }
1787};
1788
1789class DILexicalBlock : public DILexicalBlockBase {
1790 friend class LLVMContextImpl;
1791 friend class MDNode;
1792
1793 unsigned Line;
1794 uint16_t Column;
1795
1796 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1797 unsigned Column, ArrayRef<Metadata *> Ops)
1798 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1799 Column(Column) {
1800 assert(Column < (1u << 16) && "Expected 16-bit column");
1801 }
1802 ~DILexicalBlock() = default;
1803
1804 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1805 DIFile *File, unsigned Line, unsigned Column,
1806 StorageType Storage,
1807 bool ShouldCreate = true) {
1808 return getImpl(Context, static_cast<Metadata *>(Scope),
1809 static_cast<Metadata *>(File), Line, Column, Storage,
1810 ShouldCreate);
1811 }
1812
1813 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1814 Metadata *File, unsigned Line, unsigned Column,
1815 StorageType Storage, bool ShouldCreate = true);
1816
1817 TempDILexicalBlock cloneImpl() const {
1818 return getTemporary(getContext(), getScope(), getFile(), getLine(),
1819 getColumn());
1820 }
1821
1822public:
1823 DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1824 unsigned Line, unsigned Column),
1825 (Scope, File, Line, Column))
1826 DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1827 unsigned Line, unsigned Column),
1828 (Scope, File, Line, Column))
1829
1830 TempDILexicalBlock clone() const { return cloneImpl(); }
1831
1832 unsigned getLine() const { return Line; }
1833 unsigned getColumn() const { return Column; }
1834
1835 static bool classof(const Metadata *MD) {
1836 return MD->getMetadataID() == DILexicalBlockKind;
1837 }
1838};
1839
1840class DILexicalBlockFile : public DILexicalBlockBase {
1841 friend class LLVMContextImpl;
1842 friend class MDNode;
1843
1844 unsigned Discriminator;
1845
1846 DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1847 unsigned Discriminator, ArrayRef<Metadata *> Ops)
1848 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1849 Discriminator(Discriminator) {}
1850 ~DILexicalBlockFile() = default;
1851
1852 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1853 DIFile *File, unsigned Discriminator,
1854 StorageType Storage,
1855 bool ShouldCreate = true) {
1856 return getImpl(Context, static_cast<Metadata *>(Scope),
1857 static_cast<Metadata *>(File), Discriminator, Storage,
1858 ShouldCreate);
1859 }
1860
1861 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1862 Metadata *File, unsigned Discriminator,
1863 StorageType Storage,
1864 bool ShouldCreate = true);
1865
1866 TempDILexicalBlockFile cloneImpl() const {
1867 return getTemporary(getContext(), getScope(), getFile(),
1868 getDiscriminator());
1869 }
1870
1871public:
1872 DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1873 unsigned Discriminator),
1874 (Scope, File, Discriminator))
1875 DEFINE_MDNODE_GET(DILexicalBlockFile,
1876 (Metadata * Scope, Metadata *File, unsigned Discriminator),
1877 (Scope, File, Discriminator))
1878
1879 TempDILexicalBlockFile clone() const { return cloneImpl(); }
1880
1881 // TODO: Remove these once they're gone from DILexicalBlockBase.
1882 unsigned getLine() const = delete;
1883 unsigned getColumn() const = delete;
1884
1885 unsigned getDiscriminator() const { return Discriminator; }
1886
1887 static bool classof(const Metadata *MD) {
1888 return MD->getMetadataID() == DILexicalBlockFileKind;
1889 }
1890};
1891
1892unsigned DILocation::getDiscriminator() const {
1893 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1894 return F->getDiscriminator();
1895 return 0;
1896}
1897
1898const DILocation *
1899DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
1900 DIScope *Scope = getScope();
1901 // Skip all parent DILexicalBlockFile that already have a discriminator
1902 // assigned. We do not want to have nested DILexicalBlockFiles that have
1903 // mutliple discriminators because only the leaf DILexicalBlockFile's
1904 // dominator will be used.
1905 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
1906 LBF && LBF->getDiscriminator() != 0;
1907 LBF = dyn_cast<DILexicalBlockFile>(Scope))
1908 Scope = LBF->getScope();
1909 DILexicalBlockFile *NewScope =
1910 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
1911 return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
1912 getInlinedAt());
1913}
1914
1915unsigned DILocation::getBaseDiscriminator() const {
1916 return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
1917}
1918
1919unsigned DILocation::getDuplicationFactor() const {
1920 return getDuplicationFactorFromDiscriminator(getDiscriminator());
1921}
1922
1923unsigned DILocation::getCopyIdentifier() const {
1924 return getCopyIdentifierFromDiscriminator(getDiscriminator());
1925}
1926
1927const DILocation *DILocation::setBaseDiscriminator(unsigned D) const {
1928 if (D == 0)
1929 return this;
1930 else
1931 return cloneWithDiscriminator(getPrefixEncodingFromUnsigned(D) << 1);
1932}
1933
1934const DILocation *DILocation::cloneWithDuplicationFactor(unsigned DF) const {
1935 DF *= getDuplicationFactor();
1936 if (DF <= 1)
1937 return this;
1938
1939 unsigned BD = getBaseDiscriminator();
1940 unsigned CI = getCopyIdentifier() << (DF > 0x1f ? 14 : 7);
1941 unsigned D = CI | (getPrefixEncodingFromUnsigned(DF) << 1);
1942
1943 if (BD == 0)
1944 D = (D << 1) | 1;
1945 else
1946 D = (D << (BD > 0x1f ? 14 : 7)) | (getPrefixEncodingFromUnsigned(BD) << 1);
1947
1948 return cloneWithDiscriminator(D);
1949}
1950
1951class DINamespace : public DIScope {
1952 friend class LLVMContextImpl;
1953 friend class MDNode;
1954
1955 unsigned ExportSymbols : 1;
1956
1957 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
1958 ArrayRef<Metadata *> Ops)
1959 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1960 Ops),
1961 ExportSymbols(ExportSymbols) {}
1962 ~DINamespace() = default;
1963
1964 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1965 StringRef Name, bool ExportSymbols,
1966 StorageType Storage, bool ShouldCreate = true) {
1967 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1968 ExportSymbols, Storage, ShouldCreate);
1969 }
1970 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1971 MDString *Name, bool ExportSymbols,
1972 StorageType Storage, bool ShouldCreate = true);
1973
1974 TempDINamespace cloneImpl() const {
1975 return getTemporary(getContext(), getScope(), getName(),
1976 getExportSymbols());
1977 }
1978
1979public:
1980 DEFINE_MDNODE_GET(DINamespace,
1981 (DIScope *Scope, StringRef Name, bool ExportSymbols),
1982 (Scope, Name, ExportSymbols))
1983 DEFINE_MDNODE_GET(DINamespace,
1984 (Metadata *Scope, MDString *Name, bool ExportSymbols),
1985 (Scope, Name, ExportSymbols))
1986
1987 TempDINamespace clone() const { return cloneImpl(); }
1988
1989 bool getExportSymbols() const { return ExportSymbols; }
1990 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1991 StringRef getName() const { return getStringOperand(2); }
1992
1993 Metadata *getRawScope() const { return getOperand(1); }
1994 MDString *getRawName() const { return getOperandAs<MDString>(2); }
1995
1996 static bool classof(const Metadata *MD) {
1997 return MD->getMetadataID() == DINamespaceKind;
1998 }
1999};
2000
2001/// A (clang) module that has been imported by the compile unit.
2002///
2003class DIModule : public DIScope {
2004 friend class LLVMContextImpl;
2005 friend class MDNode;
2006
2007 DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
2008 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
2009 ~DIModule() = default;
2010
2011 static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
2012 StringRef Name, StringRef ConfigurationMacros,
2013 StringRef IncludePath, StringRef ISysRoot,
2014 StorageType Storage, bool ShouldCreate = true) {
2015 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2016 getCanonicalMDString(Context, ConfigurationMacros),
2017 getCanonicalMDString(Context, IncludePath),
2018 getCanonicalMDString(Context, ISysRoot),
2019 Storage, ShouldCreate);
2020 }
2021 static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
2022 MDString *Name, MDString *ConfigurationMacros,
2023 MDString *IncludePath, MDString *ISysRoot,
2024 StorageType Storage, bool ShouldCreate = true);
2025
2026 TempDIModule cloneImpl() const {
2027 return getTemporary(getContext(), getScope(), getName(),
2028 getConfigurationMacros(), getIncludePath(),
2029 getISysRoot());
2030 }
2031
2032public:
2033 DEFINE_MDNODE_GET(DIModule, (DIScope *Scope, StringRef Name,
2034 StringRef ConfigurationMacros, StringRef IncludePath,
2035 StringRef ISysRoot),
2036 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2037 DEFINE_MDNODE_GET(DIModule,
2038 (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
2039 MDString *IncludePath, MDString *ISysRoot),
2040 (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2041
2042 TempDIModule clone() const { return cloneImpl(); }
2043
2044 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2045 StringRef getName() const { return getStringOperand(1); }
2046 StringRef getConfigurationMacros() const { return getStringOperand(2); }
2047 StringRef getIncludePath() const { return getStringOperand(3); }
2048 StringRef getISysRoot() const { return getStringOperand(4); }
2049
2050 Metadata *getRawScope() const { return getOperand(0); }
2051 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2052 MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
2053 MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
2054 MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
2055
2056 static bool classof(const Metadata *MD) {
2057 return MD->getMetadataID() == DIModuleKind;
2058 }
2059};
2060
2061/// Base class for template parameters.
2062class DITemplateParameter : public DINode {
2063protected:
2064 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
2065 unsigned Tag, ArrayRef<Metadata *> Ops)
2066 : DINode(Context, ID, Storage, Tag, Ops) {}
2067 ~DITemplateParameter() = default;
2068
2069public:
2070 StringRef getName() const { return getStringOperand(0); }
2071 DITypeRef getType() const { return DITypeRef(getRawType()); }
2072
2073 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2074 Metadata *getRawType() const { return getOperand(1); }
2075
2076 static bool classof(const Metadata *MD) {
2077 return MD->getMetadataID() == DITemplateTypeParameterKind ||
2078 MD->getMetadataID() == DITemplateValueParameterKind;
2079 }
2080};
2081
2082class DITemplateTypeParameter : public DITemplateParameter {
2083 friend class LLVMContextImpl;
2084 friend class MDNode;
2085
2086 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
2087 ArrayRef<Metadata *> Ops)
2088 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
2089 dwarf::DW_TAG_template_type_parameter, Ops) {}
2090 ~DITemplateTypeParameter() = default;
2091
2092 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
2093 DITypeRef Type, StorageType Storage,
2094 bool ShouldCreate = true) {
2095 return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
2096 ShouldCreate);
2097 }
2098 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
2099 Metadata *Type, StorageType Storage,
2100 bool ShouldCreate = true);
2101
2102 TempDITemplateTypeParameter cloneImpl() const {
2103 return getTemporary(getContext(), getName(), getType());
2104 }
2105
2106public:
2107 DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
2108 (Name, Type))
2109 DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
2110 (Name, Type))
2111
2112 TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2113
2114 static bool classof(const Metadata *MD) {
2115 return MD->getMetadataID() == DITemplateTypeParameterKind;
2116 }
2117};
2118
2119class DITemplateValueParameter : public DITemplateParameter {
2120 friend class LLVMContextImpl;
2121 friend class MDNode;
2122
2123 DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
2124 unsigned Tag, ArrayRef<Metadata *> Ops)
2125 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2126 Ops) {}
2127 ~DITemplateValueParameter() = default;
2128
2129 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2130 StringRef Name, DITypeRef Type,
2131 Metadata *Value, StorageType Storage,
2132 bool ShouldCreate = true) {
2133 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2134 Value, Storage, ShouldCreate);
2135 }
2136 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2137 MDString *Name, Metadata *Type,
2138 Metadata *Value, StorageType Storage,
2139 bool ShouldCreate = true);
2140
2141 TempDITemplateValueParameter cloneImpl() const {
2142 return getTemporary(getContext(), getTag(), getName(), getType(),
2143 getValue());
2144 }
2145
2146public:
2147 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
2148 DITypeRef Type, Metadata *Value),
2149 (Tag, Name, Type, Value))
2150 DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
2151 Metadata *Type, Metadata *Value),
2152 (Tag, Name, Type, Value))
2153
2154 TempDITemplateValueParameter clone() const { return cloneImpl(); }
2155
2156 Metadata *getValue() const { return getOperand(2); }
2157
2158 static bool classof(const Metadata *MD) {
2159 return MD->getMetadataID() == DITemplateValueParameterKind;
2160 }
2161};
2162
2163/// Base class for variables.
2164class DIVariable : public DINode {
2165 unsigned Line;
2166 uint32_t AlignInBits;
2167
2168protected:
2169 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
2170 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
2171 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
2172 AlignInBits(AlignInBits) {}
2173 ~DIVariable() = default;
2174
2175public:
2176 unsigned getLine() const { return Line; }
2177 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2178 StringRef getName() const { return getStringOperand(1); }
2179 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2180 DITypeRef getType() const { return DITypeRef(getRawType()); }
2181 uint32_t getAlignInBits() const { return AlignInBits; }
2182 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2183 /// Determines the size of the variable's type.
2184 Optional<uint64_t> getSizeInBits() const;
2185
2186 StringRef getFilename() const {
2187 if (auto *F = getFile())
2188 return F->getFilename();
2189 return "";
2190 }
2191
2192 StringRef getDirectory() const {
2193 if (auto *F = getFile())
2194 return F->getDirectory();
2195 return "";
2196 }
2197
2198 Optional<StringRef> getSource() const {
2199 if (auto *F = getFile())
2200 return F->getSource();
2201 return None;
2202 }
2203
2204 Metadata *getRawScope() const { return getOperand(0); }
2205 MDString *getRawName() const { return getOperandAs<MDString>(1); }
2206 Metadata *getRawFile() const { return getOperand(2); }
2207 Metadata *getRawType() const { return getOperand(3); }
2208
2209 static bool classof(const Metadata *MD) {
2210 return MD->getMetadataID() == DILocalVariableKind ||
2211 MD->getMetadataID() == DIGlobalVariableKind;
2212 }
2213};
2214
2215/// DWARF expression.
2216///
2217/// This is (almost) a DWARF expression that modifies the location of a
2218/// variable, or the location of a single piece of a variable, or (when using
2219/// DW_OP_stack_value) is the constant variable value.
2220///
2221/// TODO: Co-allocate the expression elements.
2222/// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2223/// storage types.
2224class DIExpression : public MDNode {
2225 friend class LLVMContextImpl;
2226 friend class MDNode;
2227
2228 std::vector<uint64_t> Elements;
2229
2230 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
2231 : MDNode(C, DIExpressionKind, Storage, None),
2232 Elements(Elements.begin(), Elements.end()) {}
2233 ~DIExpression() = default;
2234
2235 static DIExpression *getImpl(LLVMContext &Context,
2236 ArrayRef<uint64_t> Elements, StorageType Storage,
2237 bool ShouldCreate = true);
2238
2239 TempDIExpression cloneImpl() const {
2240 return getTemporary(getContext(), getElements());
2241 }
2242
2243public:
2244 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
2245
2246 TempDIExpression clone() const { return cloneImpl(); }
2247
2248 ArrayRef<uint64_t> getElements() const { return Elements; }
2249
2250 unsigned getNumElements() const { return Elements.size(); }
2251
2252 uint64_t getElement(unsigned I) const {
2253 assert(I < Elements.size() && "Index out of range");
2254 return Elements[I];
2255 }
2256
2257 /// Determine whether this represents a standalone constant value.
2258 bool isConstant() const;
2259
2260 using element_iterator = ArrayRef<uint64_t>::iterator;
2261
2262 element_iterator elements_begin() const { return getElements().begin(); }
2263 element_iterator elements_end() const { return getElements().end(); }
2264
2265 /// A lightweight wrapper around an expression operand.
2266 ///
2267 /// TODO: Store arguments directly and change \a DIExpression to store a
2268 /// range of these.
2269 class ExprOperand {
2270 const uint64_t *Op = nullptr;
2271
2272 public:
2273 ExprOperand() = default;
2274 explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2275
2276 const uint64_t *get() const { return Op; }
2277
2278 /// Get the operand code.
2279 uint64_t getOp() const { return *Op; }
2280
2281 /// Get an argument to the operand.
2282 ///
2283 /// Never returns the operand itself.
2284 uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2285
2286 unsigned getNumArgs() const { return getSize() - 1; }
2287
2288 /// Return the size of the operand.
2289 ///
2290 /// Return the number of elements in the operand (1 + args).
2291 unsigned getSize() const;
2292 };
2293
2294 /// An iterator for expression operands.
2295 class expr_op_iterator
2296 : public std::iterator<std::input_iterator_tag, ExprOperand> {
2297 ExprOperand Op;
2298
2299 public:
2300 expr_op_iterator() = default;
2301 explicit expr_op_iterator(element_iterator I) : Op(I) {}
2302
2303 element_iterator getBase() const { return Op.get(); }
2304 const ExprOperand &operator*() const { return Op; }
2305 const ExprOperand *operator->() const { return &Op; }
2306
2307 expr_op_iterator &operator++() {
2308 increment();
2309 return *this;
2310 }
2311 expr_op_iterator operator++(int) {
2312 expr_op_iterator T(*this);
2313 increment();
2314 return T;
2315 }
2316
2317 /// Get the next iterator.
2318 ///
2319 /// \a std::next() doesn't work because this is technically an
2320 /// input_iterator, but it's a perfectly valid operation. This is an
2321 /// accessor to provide the same functionality.
2322 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2323
2324 bool operator==(const expr_op_iterator &X) const {
2325 return getBase() == X.getBase();
2326 }
2327 bool operator!=(const expr_op_iterator &X) const {
2328 return getBase() != X.getBase();
2329 }
2330
2331 private:
2332 void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2333 };
2334
2335 /// Visit the elements via ExprOperand wrappers.
2336 ///
2337 /// These range iterators visit elements through \a ExprOperand wrappers.
2338 /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2339 /// true.
2340 ///
2341 /// \pre \a isValid() gives \c true.
2342 /// @{
2343 expr_op_iterator expr_op_begin() const {
2344 return expr_op_iterator(elements_begin());
2345 }
2346 expr_op_iterator expr_op_end() const {
2347 return expr_op_iterator(elements_end());
2348 }
2349 iterator_range<expr_op_iterator> expr_ops() const {
2350 return {expr_op_begin(), expr_op_end()};
2351 }
2352 /// @}
2353
2354 bool isValid() const;
2355
2356 static bool classof(const Metadata *MD) {
2357 return MD->getMetadataID() == DIExpressionKind;
2358 }
2359
2360 /// Return whether the first element a DW_OP_deref.
2361 bool startsWithDeref() const {
2362 return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
2363 }
2364
2365 /// Holds the characteristics of one fragment of a larger variable.
2366 struct FragmentInfo {
2367 uint64_t SizeInBits;
2368 uint64_t OffsetInBits;
2369 };
2370
2371 /// Retrieve the details of this fragment expression.
2372 static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2373 expr_op_iterator End);
2374
2375 /// Retrieve the details of this fragment expression.
2376 Optional<FragmentInfo> getFragmentInfo() const {
2377 return getFragmentInfo(expr_op_begin(), expr_op_end());
2378 }
2379
2380 /// Return whether this is a piece of an aggregate variable.
2381 bool isFragment() const { return getFragmentInfo().hasValue(); }
2382
2383 /// Append \p Ops with operations to apply the \p Offset.
2384 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2385
2386 /// If this is a constant offset, extract it. If there is no expression,
2387 /// return true with an offset of zero.
2388 bool extractIfOffset(int64_t &Offset) const;
2389
2390 /// Constants for DIExpression::prepend.
2391 enum { NoDeref = false, WithDeref = true, WithStackValue = true };
2392
2393 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2394 /// into a stack value.
2395 static DIExpression *prepend(const DIExpression *DIExpr, bool DerefBefore,
2396 int64_t Offset = 0, bool DerefAfter = false,
2397 bool StackValue = false);
2398
2399 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2400 /// stack value.
2401 static DIExpression *doPrepend(const DIExpression *DIExpr,
2402 SmallVectorImpl<uint64_t> &Ops,
2403 bool StackValue = false);
2404
2405 /// Create a DIExpression to describe one part of an aggregate variable that
2406 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2407 /// will be appended to the elements of \c Expr. If \c Expr already contains
2408 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2409 /// into the existing fragment.
2410 ///
2411 /// \param OffsetInBits Offset of the piece in bits.
2412 /// \param SizeInBits Size of the piece in bits.
2413 /// \return Creating a fragment expression may fail if \c Expr
2414 /// contains arithmetic operations that would be truncated.
2415 static Optional<DIExpression *>
2416 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2417 unsigned SizeInBits);
2418
2419 /// Determine the relative position of the fragments described by this
2420 /// DIExpression and \p Other.
2421 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2422 /// 1 if this is entirely after Other.
2423 int fragmentCmp(const DIExpression *Other) const {
2424 auto Fragment1 = *getFragmentInfo();
2425 auto Fragment2 = *Other->getFragmentInfo();
2426 unsigned l1 = Fragment1.OffsetInBits;
2427 unsigned l2 = Fragment2.OffsetInBits;
2428 unsigned r1 = l1 + Fragment1.SizeInBits;
2429 unsigned r2 = l2 + Fragment2.SizeInBits;
2430 if (r1 <= l2)
2431 return -1;
2432 else if (r2 <= l1)
2433 return 1;
2434 else
2435 return 0;
2436 }
2437
2438 /// Check if fragments overlap between this DIExpression and \p Other.
2439 bool fragmentsOverlap(const DIExpression *Other) const {
2440 if (!isFragment() || !Other->isFragment())
2441 return true;
2442 return fragmentCmp(Other) == 0;
2443 }
2444};
2445
2446/// Global variables.
2447///
2448/// TODO: Remove DisplayName. It's always equal to Name.
2449class DIGlobalVariable : public DIVariable {
2450 friend class LLVMContextImpl;
2451 friend class MDNode;
2452
2453 bool IsLocalToUnit;
2454 bool IsDefinition;
2455
2456 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2457 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
2458 ArrayRef<Metadata *> Ops)
2459 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
2460 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
2461 ~DIGlobalVariable() = default;
2462
2463 static DIGlobalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2464 StringRef Name, StringRef LinkageName,
2465 DIFile *File, unsigned Line, DITypeRef Type,
2466 bool IsLocalToUnit, bool IsDefinition,
2467 DIDerivedType *StaticDataMemberDeclaration,
2468 uint32_t AlignInBits, StorageType Storage,
2469 bool ShouldCreate = true) {
2470 return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2471 getCanonicalMDString(Context, LinkageName), File, Line, Type,
2472 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
2473 AlignInBits, Storage, ShouldCreate);
2474 }
2475 static DIGlobalVariable *
2476 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
2477 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
2478 bool IsLocalToUnit, bool IsDefinition,
2479 Metadata *StaticDataMemberDeclaration, uint32_t AlignInBits,
2480 StorageType Storage, bool ShouldCreate = true);
2481
2482 TempDIGlobalVariable cloneImpl() const {
2483 return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
2484 getFile(), getLine(), getType(), isLocalToUnit(),
2485 isDefinition(), getStaticDataMemberDeclaration(),
2486 getAlignInBits());
2487 }
2488
2489public:
2490 DEFINE_MDNODE_GET(DIGlobalVariable,
2491 (DIScope * Scope, StringRef Name, StringRef LinkageName,
2492 DIFile *File, unsigned Line, DITypeRef Type,
2493 bool IsLocalToUnit, bool IsDefinition,
2494 DIDerivedType *StaticDataMemberDeclaration,
2495 uint32_t AlignInBits),
2496 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2497 IsDefinition, StaticDataMemberDeclaration, AlignInBits))
2498 DEFINE_MDNODE_GET(DIGlobalVariable,
2499 (Metadata * Scope, MDString *Name, MDString *LinkageName,
2500 Metadata *File, unsigned Line, Metadata *Type,
2501 bool IsLocalToUnit, bool IsDefinition,
2502 Metadata *StaticDataMemberDeclaration,
2503 uint32_t AlignInBits),
2504 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2505 IsDefinition, StaticDataMemberDeclaration, AlignInBits))
2506
2507 TempDIGlobalVariable clone() const { return cloneImpl(); }
2508
2509 bool isLocalToUnit() const { return IsLocalToUnit; }
2510 bool isDefinition() const { return IsDefinition; }
2511 StringRef getDisplayName() const { return getStringOperand(4); }
2512 StringRef getLinkageName() const { return getStringOperand(5); }
2513 DIDerivedType *getStaticDataMemberDeclaration() const {
2514 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
2515 }
2516
2517 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
2518 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
2519
2520 static bool classof(const Metadata *MD) {
2521 return MD->getMetadataID() == DIGlobalVariableKind;
2522 }
2523};
2524
2525/// Local variable.
2526///
2527/// TODO: Split up flags.
2528class DILocalVariable : public DIVariable {
2529 friend class LLVMContextImpl;
2530 friend class MDNode;
2531
2532 unsigned Arg : 16;
2533 DIFlags Flags;
2534
2535 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2536 unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
2537 ArrayRef<Metadata *> Ops)
2538 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
2539 Arg(Arg), Flags(Flags) {
2540 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
2541 }
2542 ~DILocalVariable() = default;
2543
2544 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2545 StringRef Name, DIFile *File, unsigned Line,
2546 DITypeRef Type, unsigned Arg, DIFlags Flags,
2547 uint32_t AlignInBits, StorageType Storage,
2548 bool ShouldCreate = true) {
2549 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2550 Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
2551 }
2552 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
2553 MDString *Name, Metadata *File, unsigned Line,
2554 Metadata *Type, unsigned Arg, DIFlags Flags,
2555 uint32_t AlignInBits, StorageType Storage,
2556 bool ShouldCreate = true);
2557
2558 TempDILocalVariable cloneImpl() const {
2559 return getTemporary(getContext(), getScope(), getName(), getFile(),
2560 getLine(), getType(), getArg(), getFlags(),
2561 getAlignInBits());
2562 }
2563
2564public:
2565 DEFINE_MDNODE_GET(DILocalVariable,
2566 (DILocalScope * Scope, StringRef Name, DIFile *File,
2567 unsigned Line, DITypeRef Type, unsigned Arg,
2568 DIFlags Flags, uint32_t AlignInBits),
2569 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2570 DEFINE_MDNODE_GET(DILocalVariable,
2571 (Metadata * Scope, MDString *Name, Metadata *File,
2572 unsigned Line, Metadata *Type, unsigned Arg,
2573 DIFlags Flags, uint32_t AlignInBits),
2574 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2575
2576 TempDILocalVariable clone() const { return cloneImpl(); }
2577
2578 /// Get the local scope for this variable.
2579 ///
2580 /// Variables must be defined in a local scope.
2581 DILocalScope *getScope() const {
2582 return cast<DILocalScope>(DIVariable::getScope());
2583 }
2584
2585 bool isParameter() const { return Arg; }
2586 unsigned getArg() const { return Arg; }
2587 DIFlags getFlags() const { return Flags; }
2588
2589 bool isArtificial() const { return getFlags() & FlagArtificial; }
2590 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
2591
2592 /// Check that a location is valid for this variable.
2593 ///
2594 /// Check that \c DL exists, is in the same subprogram, and has the same
2595 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2596 /// to a \a DbgInfoIntrinsic.)
2597 bool isValidLocationForIntrinsic(const DILocation *DL) const {
2598 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2599 }
2600
2601 static bool classof(const Metadata *MD) {
2602 return MD->getMetadataID() == DILocalVariableKind;
2603 }
2604};
2605
2606class DIObjCProperty : public DINode {
2607 friend class LLVMContextImpl;
2608 friend class MDNode;
2609
2610 unsigned Line;
2611 unsigned Attributes;
2612
2613 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2614 unsigned Attributes, ArrayRef<Metadata *> Ops)
2615 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2616 Ops),
2617 Line(Line), Attributes(Attributes) {}
2618 ~DIObjCProperty() = default;
2619
2620 static DIObjCProperty *
2621 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2622 StringRef GetterName, StringRef SetterName, unsigned Attributes,
2623 DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2624 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2625 getCanonicalMDString(Context, GetterName),
2626 getCanonicalMDString(Context, SetterName), Attributes, Type,
2627 Storage, ShouldCreate);
2628 }
2629 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2630 Metadata *File, unsigned Line,
2631 MDString *GetterName, MDString *SetterName,
2632 unsigned Attributes, Metadata *Type,
2633 StorageType Storage, bool ShouldCreate = true);
2634
2635 TempDIObjCProperty cloneImpl() const {
2636 return getTemporary(getContext(), getName(), getFile(), getLine(),
2637 getGetterName(), getSetterName(), getAttributes(),
2638 getType());
2639 }
2640
2641public:
2642 DEFINE_MDNODE_GET(DIObjCProperty,
2643 (StringRef Name, DIFile *File, unsigned Line,
2644 StringRef GetterName, StringRef SetterName,
2645 unsigned Attributes, DITypeRef Type),
2646 (Name, File, Line, GetterName, SetterName, Attributes,
2647 Type))
2648 DEFINE_MDNODE_GET(DIObjCProperty,
2649 (MDString * Name, Metadata *File, unsigned Line,
2650 MDString *GetterName, MDString *SetterName,
2651 unsigned Attributes, Metadata *Type),
2652 (Name, File, Line, GetterName, SetterName, Attributes,
2653 Type))
2654
2655 TempDIObjCProperty clone() const { return cloneImpl(); }
2656
2657 unsigned getLine() const { return Line; }
2658 unsigned getAttributes() const { return Attributes; }
2659 StringRef getName() const { return getStringOperand(0); }
2660 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2661 StringRef getGetterName() const { return getStringOperand(2); }
2662 StringRef getSetterName() const { return getStringOperand(3); }
2663 DITypeRef getType() const { return DITypeRef(getRawType()); }
2664
2665 StringRef getFilename() const {
2666 if (auto *F = getFile())
2667 return F->getFilename();
2668 return "";
2669 }
2670
2671 StringRef getDirectory() const {
2672 if (auto *F = getFile())
2673 return F->getDirectory();
2674 return "";
2675 }
2676
2677 Optional<StringRef> getSource() const {
2678 if (auto *F = getFile())
2679 return F->getSource();
2680 return None;
2681 }
2682
2683 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2684 Metadata *getRawFile() const { return getOperand(1); }
2685 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2686 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2687 Metadata *getRawType() const { return getOperand(4); }
2688
2689 static bool classof(const Metadata *MD) {
2690 return MD->getMetadataID() == DIObjCPropertyKind;
2691 }
2692};
2693
2694/// An imported module (C++ using directive or similar).
2695class DIImportedEntity : public DINode {
2696 friend class LLVMContextImpl;
2697 friend class MDNode;
2698
2699 unsigned Line;
2700
2701 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2702 unsigned Line, ArrayRef<Metadata *> Ops)
2703 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2704 ~DIImportedEntity() = default;
2705
2706 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2707 DIScope *Scope, DINodeRef Entity,
2708 DIFile *File, unsigned Line, StringRef Name,
2709 StorageType Storage,
2710 bool ShouldCreate = true) {
2711 return getImpl(Context, Tag, Scope, Entity, File, Line,
2712 getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2713 }
2714 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2715 Metadata *Scope, Metadata *Entity,
2716 Metadata *File, unsigned Line,
2717 MDString *Name, StorageType Storage,
2718 bool ShouldCreate = true);
2719
2720 TempDIImportedEntity cloneImpl() const {
2721 return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2722 getFile(), getLine(), getName());
2723 }
2724
2725public:
2726 DEFINE_MDNODE_GET(DIImportedEntity,
2727 (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2728 DIFile *File, unsigned Line, StringRef Name = ""),
2729 (Tag, Scope, Entity, File, Line, Name))
2730 DEFINE_MDNODE_GET(DIImportedEntity,
2731 (unsigned Tag, Metadata *Scope, Metadata *Entity,
2732 Metadata *File, unsigned Line, MDString *Name),
2733 (Tag, Scope, Entity, File, Line, Name))
2734
2735 TempDIImportedEntity clone() const { return cloneImpl(); }
2736
2737 unsigned getLine() const { return Line; }
2738 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2739 DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2740 StringRef getName() const { return getStringOperand(2); }
2741 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2742
2743 Metadata *getRawScope() const { return getOperand(0); }
2744 Metadata *getRawEntity() const { return getOperand(1); }
2745 MDString *getRawName() const { return getOperandAs<MDString>(2); }
2746 Metadata *getRawFile() const { return getOperand(3); }
2747
2748 static bool classof(const Metadata *MD) {
2749 return MD->getMetadataID() == DIImportedEntityKind;
2750 }
2751};
2752
2753/// A pair of DIGlobalVariable and DIExpression.
2754class DIGlobalVariableExpression : public MDNode {
2755 friend class LLVMContextImpl;
2756 friend class MDNode;
2757
2758 DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
2759 ArrayRef<Metadata *> Ops)
2760 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
2761 ~DIGlobalVariableExpression() = default;
2762
2763 static DIGlobalVariableExpression *
2764 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
2765 StorageType Storage, bool ShouldCreate = true);
2766
2767 TempDIGlobalVariableExpression cloneImpl() const {
2768 return getTemporary(getContext(), getVariable(), getExpression());
2769 }
2770
2771public:
2772 DEFINE_MDNODE_GET(DIGlobalVariableExpression,
2773 (Metadata * Variable, Metadata *Expression),
2774 (Variable, Expression))
2775
2776 TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
2777
2778 Metadata *getRawVariable() const { return getOperand(0); }
2779
2780 DIGlobalVariable *getVariable() const {
2781 return cast_or_null<DIGlobalVariable>(getRawVariable());
2782 }
2783
2784 Metadata *getRawExpression() const { return getOperand(1); }
2785
2786 DIExpression *getExpression() const {
2787 return cast<DIExpression>(getRawExpression());
2788 }
2789
2790 static bool classof(const Metadata *MD) {
2791 return MD->getMetadataID() == DIGlobalVariableExpressionKind;
2792 }
2793};
2794
2795/// Macro Info DWARF-like metadata node.
2796///
2797/// A metadata node with a DWARF macro info (i.e., a constant named
2798/// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
2799/// DIMacroNode
2800/// because it's potentially used for non-DWARF output.
2801class DIMacroNode : public MDNode {
2802 friend class LLVMContextImpl;
2803 friend class MDNode;
2804
2805protected:
2806 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
2807 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
2808 : MDNode(C, ID, Storage, Ops1, Ops2) {
2809 assert(MIType < 1u << 16);
2810 SubclassData16 = MIType;
2811 }
2812 ~DIMacroNode() = default;
2813
2814 template <class Ty> Ty *getOperandAs(unsigned I) const {
2815 return cast_or_null<Ty>(getOperand(I));
2816 }
2817
2818 StringRef getStringOperand(unsigned I) const {
2819 if (auto *S = getOperandAs<MDString>(I))
2820 return S->getString();
2821 return StringRef();
2822 }
2823
2824 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
2825 if (S.empty())
2826 return nullptr;
2827 return MDString::get(Context, S);
2828 }
2829
2830public:
2831 unsigned getMacinfoType() const { return SubclassData16; }
2832
2833 static bool classof(const Metadata *MD) {
2834 switch (MD->getMetadataID()) {
2835 default:
2836 return false;
2837 case DIMacroKind:
2838 case DIMacroFileKind:
2839 return true;
2840 }
2841 }
2842};
2843
2844class DIMacro : public DIMacroNode {
2845 friend class LLVMContextImpl;
2846 friend class MDNode;
2847
2848 unsigned Line;
2849
2850 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
2851 ArrayRef<Metadata *> Ops)
2852 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
2853 ~DIMacro() = default;
2854
2855 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2856 StringRef Name, StringRef Value, StorageType Storage,
2857 bool ShouldCreate = true) {
2858 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
2859 getCanonicalMDString(Context, Value), Storage, ShouldCreate);
2860 }
2861 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2862 MDString *Name, MDString *Value, StorageType Storage,
2863 bool ShouldCreate = true);
2864
2865 TempDIMacro cloneImpl() const {
2866 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
2867 getValue());
2868 }
2869
2870public:
2871 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
2872 StringRef Value = ""),
2873 (MIType, Line, Name, Value))
2874 DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
2875 MDString *Value),
2876 (MIType, Line, Name, Value))
2877
2878 TempDIMacro clone() const { return cloneImpl(); }
2879
2880 unsigned getLine() const { return Line; }
2881
2882 StringRef getName() const { return getStringOperand(0); }
2883 StringRef getValue() const { return getStringOperand(1); }
2884
2885 MDString *getRawName() const { return getOperandAs<MDString>(0); }
2886 MDString *getRawValue() const { return getOperandAs<MDString>(1); }
2887
2888 static bool classof(const Metadata *MD) {
2889 return MD->getMetadataID() == DIMacroKind;
2890 }
2891};
2892
2893class DIMacroFile : public DIMacroNode {
2894 friend class LLVMContextImpl;
2895 friend class MDNode;
2896
2897 unsigned Line;
2898
2899 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
2900 unsigned Line, ArrayRef<Metadata *> Ops)
2901 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
2902 ~DIMacroFile() = default;
2903
2904 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2905 unsigned Line, DIFile *File,
2906 DIMacroNodeArray Elements, StorageType Storage,
2907 bool ShouldCreate = true) {
2908 return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
2909 Elements.get(), Storage, ShouldCreate);
2910 }
2911
2912 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2913 unsigned Line, Metadata *File, Metadata *Elements,
2914 StorageType Storage, bool ShouldCreate = true);
2915
2916 TempDIMacroFile cloneImpl() const {
2917 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
2918 getElements());
2919 }
2920
2921public:
2922 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
2923 DIMacroNodeArray Elements),
2924 (MIType, Line, File, Elements))
2925 DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
2926 Metadata *File, Metadata *Elements),
2927 (MIType, Line, File, Elements))
2928
2929 TempDIMacroFile clone() const { return cloneImpl(); }
2930
2931 void replaceElements(DIMacroNodeArray Elements) {
2932#ifndef NDEBUG
2933 for (DIMacroNode *Op : getElements())
2934 assert(is_contained(Elements->operands(), Op) &&
2935 "Lost a macro node during macro node list replacement");
2936#endif
2937 replaceOperandWith(1, Elements.get());
2938 }
2939
2940 unsigned getLine() const { return Line; }
2941 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2942
2943 DIMacroNodeArray getElements() const {
2944 return cast_or_null<MDTuple>(getRawElements());
2945 }
2946
2947 Metadata *getRawFile() const { return getOperand(0); }
2948 Metadata *getRawElements() const { return getOperand(1); }
2949
2950 static bool classof(const Metadata *MD) {
2951 return MD->getMetadataID() == DIMacroFileKind;
2952 }
2953};
2954
2955} // end namespace llvm
2956
2957#undef DEFINE_MDNODE_GET_UNPACK_IMPL
2958#undef DEFINE_MDNODE_GET_UNPACK
2959#undef DEFINE_MDNODE_GET
2960
2961#endif // LLVM_IR_DEBUGINFOMETADATA_H