LLVM  7.0.0svn
DebugInfoMetadata.h
Go to the documentation of this file.
1 //===- 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"
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 
62 namespace 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.
71 template <class T> class TypedDINodeRef {
72  const Metadata *MD = nullptr;
73 
74 public:
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>
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 
101 
103  const MDTuple *N = nullptr;
104 
105 public:
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 
131  ++I;
132  return *this;
133  }
134 
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.
155 class DINode : public MDNode {
156  friend class LLVMContextImpl;
157  friend class MDNode;
158 
159 protected:
160  DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
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 
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 
187 public:
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 
243 template <class T> struct simplify_type<const TypedDINodeRef<T>> {
244  using SimpleType = Metadata *;
245 
247  return MD;
248  }
249 };
250 
251 template <class 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.
261 class 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,
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 
293 public:
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(); }
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.
333 class 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 
358 public:
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.
394 class 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 
421 public:
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.
446 class DIScope : public DINode {
447 protected:
448  DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
450  : DINode(C, ID, Storage, Tag, Ops) {}
451  ~DIScope() = default;
452 
453 public:
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.
498 class DIFile : public DIScope {
499  friend class LLVMContextImpl;
500  friend class MDNode;
501 
502 public:
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.
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.
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 
534 private:
537 
538  DIFile(LLVMContext &C, StorageType Storage,
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,
548  Optional<StringRef> Source,
549  StorageType Storage, bool ShouldCreate = true) {
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,
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 
569 public:
570  DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
572  Optional<StringRef> Source = None),
573  (Filename, Directory, CS, Source))
574  DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
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 
607  if (auto *F = getFile())
608  return F->getFilename();
609  return "";
610 }
611 
613  if (auto *F = getFile())
614  return F->getDirectory();
615  return "";
616 }
617 
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.
629 class DIType : public DIScope {
630  unsigned Line;
631  DIFlags Flags;
632  uint64_t SizeInBits;
633  uint64_t OffsetInBits;
634  uint32_t AlignInBits;
635 
636 protected:
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 
662 public:
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.
731 class 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,
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 
762 public:
763  DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
764  (Tag, Name, 0, 0, 0))
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.).
789 class 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,
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 
834 public:
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))
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.
916 class 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,
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 
972 public:
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))
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.
1084 class 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 
1114 public:
1116  (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
1117  (Flags, CC, TypeArray))
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 
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.
1138 class DICompileUnit : public DIScope {
1139  friend class LLVMContextImpl;
1140  friend class MDNode;
1141 
1142 public:
1143  enum DebugEmissionKind : unsigned {
1144  NoDebug = 0,
1147  LastEmissionKind = LineTablesOnly
1148  };
1149 
1150  static Optional<DebugEmissionKind> getEmissionKind(StringRef Str);
1151  static const char *EmissionKindString(DebugEmissionKind EK);
1152 
1153 private:
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 
1214 public:
1215  static void get() = delete;
1216  static void getIfExists() = delete;
1217 
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))
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; }
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); }
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.
1322 class DILocalScope : public DIScope {
1323 protected:
1324  DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1326  : DIScope(C, ID, Storage, Tag, Ops) {}
1327  ~DILocalScope() = default;
1328 
1329 public:
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.
1352 class 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 
1401 public:
1402  // Disallow replacing operands.
1403  void replaceOperandWith(unsigned I, Metadata *New) = delete;
1404 
1406  (unsigned Line, unsigned Column, Metadata *Scope,
1407  Metadata *InlinedAt = nullptr),
1408  (Line, Column, Scope, InlinedAt))
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  enum { NoGeneratedLocation = false, WithGeneratedLocation = true };
1501 
1502  /// When two instructions are combined into a single instruction we also
1503  /// need to combine the original locations into a single location.
1504  ///
1505  /// When the locations are the same we can use either location. When they
1506  /// differ, we need a third location which is distinct from either. If they
1507  /// have the same file/line but have a different discriminator we could
1508  /// create a location with a new discriminator. If they are from different
1509  /// files/lines the location is ambiguous and can't be represented in a line
1510  /// entry. In this case, if \p GenerateLocation is true, we will set the
1511  /// merged debug location as line 0 of the nearest common scope where the two
1512  /// locations are inlined from.
1513  ///
1514  /// \p GenerateLocation: Whether the merged location can be generated when
1515  /// \p LocA and \p LocB differ.
1516  static const DILocation *
1517  getMergedLocation(const DILocation *LocA, const DILocation *LocB,
1518  bool GenerateLocation = NoGeneratedLocation);
1519 
1520  /// Returns the base discriminator for a given encoded discriminator \p D.
1521  static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
1522  if ((D & 1) == 0)
1523  return getUnsignedFromPrefixEncoding(D >> 1);
1524  else
1525  return 0;
1526  }
1527 
1528  /// Returns the duplication factor for a given encoded discriminator \p D.
1529  static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
1530  D = getNextComponentInDiscriminator(D);
1531  if (D == 0 || (D & 1))
1532  return 1;
1533  else
1534  return getUnsignedFromPrefixEncoding(D >> 1);
1535  }
1536 
1537  /// Returns the copy identifier for a given encoded discriminator \p D.
1538  static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
1539  return getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(
1540  getNextComponentInDiscriminator(D)));
1541  }
1542 
1543 
1544  Metadata *getRawScope() const { return getOperand(0); }
1546  if (getNumOperands() == 2)
1547  return getOperand(1);
1548  return nullptr;
1549  }
1550 
1551  static bool classof(const Metadata *MD) {
1552  return MD->getMetadataID() == DILocationKind;
1553  }
1554 };
1555 
1556 /// Subprogram description.
1557 ///
1558 /// TODO: Remove DisplayName. It's always equal to Name.
1559 /// TODO: Split up flags.
1560 class DISubprogram : public DILocalScope {
1561  friend class LLVMContextImpl;
1562  friend class MDNode;
1563 
1564  unsigned Line;
1565  unsigned ScopeLine;
1566  unsigned VirtualIndex;
1567 
1568  /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
1569  /// of method overrides from secondary bases by this amount. It may be
1570  /// negative.
1571  int ThisAdjustment;
1572 
1573  // Virtuality can only assume three values, so we can pack
1574  // in 2 bits (none/pure/pure_virtual).
1575  unsigned Virtuality : 2;
1576 
1577  // These are boolean flags so one bit is enough.
1578  // MSVC starts a new container field every time the base
1579  // type changes so we can't use 'bool' to ensure these bits
1580  // are packed.
1581  unsigned IsLocalToUnit : 1;
1582  unsigned IsDefinition : 1;
1583  unsigned IsOptimized : 1;
1584 
1585  unsigned Padding : 3;
1586 
1587  DIFlags Flags;
1588 
1589  DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1590  unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1591  int ThisAdjustment, DIFlags Flags, bool IsLocalToUnit,
1592  bool IsDefinition, bool IsOptimized, ArrayRef<Metadata *> Ops)
1593  : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1594  Ops),
1595  Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
1596  ThisAdjustment(ThisAdjustment), Virtuality(Virtuality),
1597  IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition),
1598  IsOptimized(IsOptimized), Flags(Flags) {
1599  static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
1600  assert(Virtuality < 4 && "Virtuality out of range");
1601  }
1602  ~DISubprogram() = default;
1603 
1604  static DISubprogram *
1605  getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1606  StringRef LinkageName, DIFile *File, unsigned Line,
1607  DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1608  unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1609  unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1610  bool IsOptimized, DICompileUnit *Unit,
1611  DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
1612  DILocalVariableArray Variables, DITypeArray ThrownTypes,
1613  StorageType Storage, bool ShouldCreate = true) {
1614  return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1615  getCanonicalMDString(Context, LinkageName), File, Line, Type,
1616  IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1617  Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1618  Unit, TemplateParams.get(), Declaration, Variables.get(),
1619  ThrownTypes.get(), Storage, ShouldCreate);
1620  }
1621  static DISubprogram *
1622  getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1623  MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1624  bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1625  Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1626  int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
1627  Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1628  Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate = true);
1629 
1630  TempDISubprogram cloneImpl() const {
1631  return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1632  getFile(), getLine(), getType(), isLocalToUnit(),
1633  isDefinition(), getScopeLine(), getContainingType(),
1634  getVirtuality(), getVirtualIndex(), getThisAdjustment(),
1635  getFlags(), isOptimized(), getUnit(),
1636  getTemplateParams(), getDeclaration(), getVariables(),
1637  getThrownTypes());
1638  }
1639 
1640 public:
1642  (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1643  DIFile *File, unsigned Line, DISubroutineType *Type,
1644  bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1645  DITypeRef ContainingType, unsigned Virtuality,
1646  unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1647  bool IsOptimized, DICompileUnit *Unit,
1648  DITemplateParameterArray TemplateParams = nullptr,
1649  DISubprogram *Declaration = nullptr,
1650  DILocalVariableArray Variables = nullptr,
1651  DITypeArray ThrownTypes = nullptr),
1652  (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1653  IsDefinition, ScopeLine, ContainingType, Virtuality,
1654  VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
1655  TemplateParams, Declaration, Variables, ThrownTypes))
1657  DISubprogram,
1658  (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1659  unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1660  unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1661  unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
1662  bool IsOptimized, Metadata *Unit, Metadata *TemplateParams = nullptr,
1663  Metadata *Declaration = nullptr, Metadata *Variables = nullptr,
1664  Metadata *ThrownTypes = nullptr),
1665  (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1666  ScopeLine, ContainingType, Virtuality, VirtualIndex, ThisAdjustment,
1667  Flags, IsOptimized, Unit, TemplateParams, Declaration, Variables,
1668  ThrownTypes))
1669 
1670  TempDISubprogram clone() const { return cloneImpl(); }
1671 
1672 public:
1673  unsigned getLine() const { return Line; }
1674  unsigned getVirtuality() const { return Virtuality; }
1675  unsigned getVirtualIndex() const { return VirtualIndex; }
1676  int getThisAdjustment() const { return ThisAdjustment; }
1677  unsigned getScopeLine() const { return ScopeLine; }
1678  DIFlags getFlags() const { return Flags; }
1679  bool isLocalToUnit() const { return IsLocalToUnit; }
1680  bool isDefinition() const { return IsDefinition; }
1681  bool isOptimized() const { return IsOptimized; }
1682 
1683  bool isArtificial() const { return getFlags() & FlagArtificial; }
1684  bool isPrivate() const {
1685  return (getFlags() & FlagAccessibility) == FlagPrivate;
1686  }
1687  bool isProtected() const {
1688  return (getFlags() & FlagAccessibility) == FlagProtected;
1689  }
1690  bool isPublic() const {
1691  return (getFlags() & FlagAccessibility) == FlagPublic;
1692  }
1693  bool isExplicit() const { return getFlags() & FlagExplicit; }
1694  bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1695  bool isMainSubprogram() const { return getFlags() & FlagMainSubprogram; }
1696 
1697  /// Check if this is reference-qualified.
1698  ///
1699  /// Return true if this subprogram is a C++11 reference-qualified non-static
1700  /// member function (void foo() &).
1701  bool isLValueReference() const { return getFlags() & FlagLValueReference; }
1702 
1703  /// Check if this is rvalue-reference-qualified.
1704  ///
1705  /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1706  /// non-static member function (void foo() &&).
1707  bool isRValueReference() const { return getFlags() & FlagRValueReference; }
1708 
1709  /// Check if this is marked as noreturn.
1710  ///
1711  /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
1712  bool isNoReturn() const { return getFlags() & FlagNoReturn; }
1713 
1714  // Check if this routine is a compiler-generated thunk.
1715  //
1716  // Returns true if this subprogram is a thunk generated by the compiler.
1717  bool isThunk() const { return getFlags() & FlagThunk; }
1718 
1719  DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1720 
1721  StringRef getName() const { return getStringOperand(2); }
1722  StringRef getLinkageName() const { return getStringOperand(3); }
1723 
1724  DISubroutineType *getType() const {
1725  return cast_or_null<DISubroutineType>(getRawType());
1726  }
1727  DITypeRef getContainingType() const {
1728  return DITypeRef(getRawContainingType());
1729  }
1730 
1731  DICompileUnit *getUnit() const {
1732  return cast_or_null<DICompileUnit>(getRawUnit());
1733  }
1734  void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
1735  DITemplateParameterArray getTemplateParams() const {
1736  return cast_or_null<MDTuple>(getRawTemplateParams());
1737  }
1738  DISubprogram *getDeclaration() const {
1739  return cast_or_null<DISubprogram>(getRawDeclaration());
1740  }
1741  DILocalVariableArray getVariables() const {
1742  return cast_or_null<MDTuple>(getRawVariables());
1743  }
1744  DITypeArray getThrownTypes() const {
1745  return cast_or_null<MDTuple>(getRawThrownTypes());
1746  }
1747 
1748  Metadata *getRawScope() const { return getOperand(1); }
1749  MDString *getRawName() const { return getOperandAs<MDString>(2); }
1750  MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
1751  Metadata *getRawType() const { return getOperand(4); }
1752  Metadata *getRawUnit() const { return getOperand(5); }
1753  Metadata *getRawDeclaration() const { return getOperand(6); }
1754  Metadata *getRawVariables() const { return getOperand(7); }
1755  Metadata *getRawContainingType() const {
1756  return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
1757  }
1758  Metadata *getRawTemplateParams() const {
1759  return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
1760  }
1761  Metadata *getRawThrownTypes() const {
1762  return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
1763  }
1764 
1765  /// Check if this subprogram describes the given function.
1766  ///
1767  /// FIXME: Should this be looking through bitcasts?
1768  bool describes(const Function *F) const;
1769 
1770  static bool classof(const Metadata *MD) {
1771  return MD->getMetadataID() == DISubprogramKind;
1772  }
1773 };
1774 
1776 protected:
1779  : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1780  ~DILexicalBlockBase() = default;
1781 
1782 public:
1783  DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1784 
1785  Metadata *getRawScope() const { return getOperand(1); }
1786 
1787  static bool classof(const Metadata *MD) {
1788  return MD->getMetadataID() == DILexicalBlockKind ||
1789  MD->getMetadataID() == DILexicalBlockFileKind;
1790  }
1791 };
1792 
1794  friend class LLVMContextImpl;
1795  friend class MDNode;
1796 
1797  unsigned Line;
1798  uint16_t Column;
1799 
1800  DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1801  unsigned Column, ArrayRef<Metadata *> Ops)
1802  : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1803  Column(Column) {
1804  assert(Column < (1u << 16) && "Expected 16-bit column");
1805  }
1806  ~DILexicalBlock() = default;
1807 
1808  static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1809  DIFile *File, unsigned Line, unsigned Column,
1810  StorageType Storage,
1811  bool ShouldCreate = true) {
1812  return getImpl(Context, static_cast<Metadata *>(Scope),
1813  static_cast<Metadata *>(File), Line, Column, Storage,
1814  ShouldCreate);
1815  }
1816 
1817  static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1818  Metadata *File, unsigned Line, unsigned Column,
1819  StorageType Storage, bool ShouldCreate = true);
1820 
1821  TempDILexicalBlock cloneImpl() const {
1822  return getTemporary(getContext(), getScope(), getFile(), getLine(),
1823  getColumn());
1824  }
1825 
1826 public:
1828  unsigned Line, unsigned Column),
1829  (Scope, File, Line, Column))
1831  unsigned Line, unsigned Column),
1832  (Scope, File, Line, Column))
1833 
1834  TempDILexicalBlock clone() const { return cloneImpl(); }
1835 
1836  unsigned getLine() const { return Line; }
1837  unsigned getColumn() const { return Column; }
1838 
1839  static bool classof(const Metadata *MD) {
1840  return MD->getMetadataID() == DILexicalBlockKind;
1841  }
1842 };
1843 
1845  friend class LLVMContextImpl;
1846  friend class MDNode;
1847 
1848  unsigned Discriminator;
1849 
1851  unsigned Discriminator, ArrayRef<Metadata *> Ops)
1852  : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1853  Discriminator(Discriminator) {}
1854  ~DILexicalBlockFile() = default;
1855 
1856  static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1857  DIFile *File, unsigned Discriminator,
1858  StorageType Storage,
1859  bool ShouldCreate = true) {
1860  return getImpl(Context, static_cast<Metadata *>(Scope),
1861  static_cast<Metadata *>(File), Discriminator, Storage,
1862  ShouldCreate);
1863  }
1864 
1865  static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1866  Metadata *File, unsigned Discriminator,
1867  StorageType Storage,
1868  bool ShouldCreate = true);
1869 
1870  TempDILexicalBlockFile cloneImpl() const {
1871  return getTemporary(getContext(), getScope(), getFile(),
1872  getDiscriminator());
1873  }
1874 
1875 public:
1877  unsigned Discriminator),
1878  (Scope, File, Discriminator))
1880  (Metadata * Scope, Metadata *File, unsigned Discriminator),
1881  (Scope, File, Discriminator))
1882 
1883  TempDILexicalBlockFile clone() const { return cloneImpl(); }
1884 
1885  // TODO: Remove these once they're gone from DILexicalBlockBase.
1886  unsigned getLine() const = delete;
1887  unsigned getColumn() const = delete;
1888 
1889  unsigned getDiscriminator() const { return Discriminator; }
1890 
1891  static bool classof(const Metadata *MD) {
1892  return MD->getMetadataID() == DILexicalBlockFileKind;
1893  }
1894 };
1895 
1896 unsigned DILocation::getDiscriminator() const {
1897  if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1898  return F->getDiscriminator();
1899  return 0;
1900 }
1901 
1902 const DILocation *
1903 DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
1904  DIScope *Scope = getScope();
1905  // Skip all parent DILexicalBlockFile that already have a discriminator
1906  // assigned. We do not want to have nested DILexicalBlockFiles that have
1907  // mutliple discriminators because only the leaf DILexicalBlockFile's
1908  // dominator will be used.
1909  for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
1910  LBF && LBF->getDiscriminator() != 0;
1911  LBF = dyn_cast<DILexicalBlockFile>(Scope))
1912  Scope = LBF->getScope();
1913  DILexicalBlockFile *NewScope =
1914  DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
1915  return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
1916  getInlinedAt());
1917 }
1918 
1920  return getBaseDiscriminatorFromDiscriminator(getDiscriminator());
1921 }
1922 
1924  return getDuplicationFactorFromDiscriminator(getDiscriminator());
1925 }
1926 
1928  return getCopyIdentifierFromDiscriminator(getDiscriminator());
1929 }
1930 
1932  if (D == 0)
1933  return this;
1934  else
1935  return cloneWithDiscriminator(getPrefixEncodingFromUnsigned(D) << 1);
1936 }
1937 
1939  DF *= getDuplicationFactor();
1940  if (DF <= 1)
1941  return this;
1942 
1943  unsigned BD = getBaseDiscriminator();
1944  unsigned CI = getCopyIdentifier() << (DF > 0x1f ? 14 : 7);
1945  unsigned D = CI | (getPrefixEncodingFromUnsigned(DF) << 1);
1946 
1947  if (BD == 0)
1948  D = (D << 1) | 1;
1949  else
1950  D = (D << (BD > 0x1f ? 14 : 7)) | (getPrefixEncodingFromUnsigned(BD) << 1);
1951 
1952  return cloneWithDiscriminator(D);
1953 }
1954 
1955 class DINamespace : public DIScope {
1956  friend class LLVMContextImpl;
1957  friend class MDNode;
1958 
1959  unsigned ExportSymbols : 1;
1960 
1961  DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
1963  : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1964  Ops),
1965  ExportSymbols(ExportSymbols) {}
1966  ~DINamespace() = default;
1967 
1968  static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1969  StringRef Name, bool ExportSymbols,
1970  StorageType Storage, bool ShouldCreate = true) {
1971  return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1972  ExportSymbols, Storage, ShouldCreate);
1973  }
1974  static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1975  MDString *Name, bool ExportSymbols,
1976  StorageType Storage, bool ShouldCreate = true);
1977 
1978  TempDINamespace cloneImpl() const {
1979  return getTemporary(getContext(), getScope(), getName(),
1980  getExportSymbols());
1981  }
1982 
1983 public:
1985  (DIScope *Scope, StringRef Name, bool ExportSymbols),
1986  (Scope, Name, ExportSymbols))
1988  (Metadata *Scope, MDString *Name, bool ExportSymbols),
1989  (Scope, Name, ExportSymbols))
1990 
1991  TempDINamespace clone() const { return cloneImpl(); }
1992 
1993  bool getExportSymbols() const { return ExportSymbols; }
1994  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1995  StringRef getName() const { return getStringOperand(2); }
1996 
1997  Metadata *getRawScope() const { return getOperand(1); }
1998  MDString *getRawName() const { return getOperandAs<MDString>(2); }
1999 
2000  static bool classof(const Metadata *MD) {
2001  return MD->getMetadataID() == DINamespaceKind;
2002  }
2003 };
2004 
2005 /// A (clang) module that has been imported by the compile unit.
2006 ///
2007 class DIModule : public DIScope {
2008  friend class LLVMContextImpl;
2009  friend class MDNode;
2010 
2011  DIModule(LLVMContext &Context, StorageType Storage, ArrayRef<Metadata *> Ops)
2012  : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) {}
2013  ~DIModule() = default;
2014 
2015  static DIModule *getImpl(LLVMContext &Context, DIScope *Scope,
2016  StringRef Name, StringRef ConfigurationMacros,
2017  StringRef IncludePath, StringRef ISysRoot,
2018  StorageType Storage, bool ShouldCreate = true) {
2019  return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2020  getCanonicalMDString(Context, ConfigurationMacros),
2021  getCanonicalMDString(Context, IncludePath),
2022  getCanonicalMDString(Context, ISysRoot),
2023  Storage, ShouldCreate);
2024  }
2025  static DIModule *getImpl(LLVMContext &Context, Metadata *Scope,
2026  MDString *Name, MDString *ConfigurationMacros,
2027  MDString *IncludePath, MDString *ISysRoot,
2028  StorageType Storage, bool ShouldCreate = true);
2029 
2030  TempDIModule cloneImpl() const {
2031  return getTemporary(getContext(), getScope(), getName(),
2032  getConfigurationMacros(), getIncludePath(),
2033  getISysRoot());
2034  }
2035 
2036 public:
2038  StringRef ConfigurationMacros, StringRef IncludePath,
2039  StringRef ISysRoot),
2040  (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2042  (Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
2043  MDString *IncludePath, MDString *ISysRoot),
2044  (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot))
2045 
2046  TempDIModule clone() const { return cloneImpl(); }
2047 
2048  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2049  StringRef getName() const { return getStringOperand(1); }
2050  StringRef getConfigurationMacros() const { return getStringOperand(2); }
2051  StringRef getIncludePath() const { return getStringOperand(3); }
2052  StringRef getISysRoot() const { return getStringOperand(4); }
2053 
2054  Metadata *getRawScope() const { return getOperand(0); }
2055  MDString *getRawName() const { return getOperandAs<MDString>(1); }
2056  MDString *getRawConfigurationMacros() const { return getOperandAs<MDString>(2); }
2057  MDString *getRawIncludePath() const { return getOperandAs<MDString>(3); }
2058  MDString *getRawISysRoot() const { return getOperandAs<MDString>(4); }
2059 
2060  static bool classof(const Metadata *MD) {
2061  return MD->getMetadataID() == DIModuleKind;
2062  }
2063 };
2064 
2065 /// Base class for template parameters.
2066 class DITemplateParameter : public DINode {
2067 protected:
2068  DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
2069  unsigned Tag, ArrayRef<Metadata *> Ops)
2070  : DINode(Context, ID, Storage, Tag, Ops) {}
2071  ~DITemplateParameter() = default;
2072 
2073 public:
2074  StringRef getName() const { return getStringOperand(0); }
2075  DITypeRef getType() const { return DITypeRef(getRawType()); }
2076 
2077  MDString *getRawName() const { return getOperandAs<MDString>(0); }
2078  Metadata *getRawType() const { return getOperand(1); }
2079 
2080  static bool classof(const Metadata *MD) {
2081  return MD->getMetadataID() == DITemplateTypeParameterKind ||
2082  MD->getMetadataID() == DITemplateValueParameterKind;
2083  }
2084 };
2085 
2087  friend class LLVMContextImpl;
2088  friend class MDNode;
2089 
2092  : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
2093  dwarf::DW_TAG_template_type_parameter, Ops) {}
2094  ~DITemplateTypeParameter() = default;
2095 
2096  static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
2097  DITypeRef Type, StorageType Storage,
2098  bool ShouldCreate = true) {
2099  return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
2100  ShouldCreate);
2101  }
2102  static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
2103  Metadata *Type, StorageType Storage,
2104  bool ShouldCreate = true);
2105 
2106  TempDITemplateTypeParameter cloneImpl() const {
2107  return getTemporary(getContext(), getName(), getType());
2108  }
2109 
2110 public:
2112  (Name, Type))
2114  (Name, Type))
2115 
2116  TempDITemplateTypeParameter clone() const { return cloneImpl(); }
2117 
2118  static bool classof(const Metadata *MD) {
2119  return MD->getMetadataID() == DITemplateTypeParameterKind;
2120  }
2121 };
2122 
2124  friend class LLVMContextImpl;
2125  friend class MDNode;
2126 
2128  unsigned Tag, ArrayRef<Metadata *> Ops)
2129  : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
2130  Ops) {}
2131  ~DITemplateValueParameter() = default;
2132 
2133  static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2134  StringRef Name, DITypeRef Type,
2135  Metadata *Value, StorageType Storage,
2136  bool ShouldCreate = true) {
2137  return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
2138  Value, Storage, ShouldCreate);
2139  }
2140  static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
2141  MDString *Name, Metadata *Type,
2142  Metadata *Value, StorageType Storage,
2143  bool ShouldCreate = true);
2144 
2145  TempDITemplateValueParameter cloneImpl() const {
2146  return getTemporary(getContext(), getTag(), getName(), getType(),
2147  getValue());
2148  }
2149 
2150 public:
2152  DITypeRef Type, Metadata *Value),
2153  (Tag, Name, Type, Value))
2155  Metadata *Type, Metadata *Value),
2156  (Tag, Name, Type, Value))
2157 
2158  TempDITemplateValueParameter clone() const { return cloneImpl(); }
2159 
2160  Metadata *getValue() const { return getOperand(2); }
2161 
2162  static bool classof(const Metadata *MD) {
2163  return MD->getMetadataID() == DITemplateValueParameterKind;
2164  }
2165 };
2166 
2167 /// Base class for variables.
2168 class DIVariable : public DINode {
2169  unsigned Line;
2170  uint32_t AlignInBits;
2171 
2172 protected:
2173  DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line,
2174  ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0)
2175  : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line),
2176  AlignInBits(AlignInBits) {}
2177  ~DIVariable() = default;
2178 
2179 public:
2180  unsigned getLine() const { return Line; }
2181  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2182  StringRef getName() const { return getStringOperand(1); }
2183  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2184  DITypeRef getType() const { return DITypeRef(getRawType()); }
2185  uint32_t getAlignInBits() const { return AlignInBits; }
2186  uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
2187  /// Determines the size of the variable's type.
2189 
2191  if (auto *F = getFile())
2192  return F->getFilename();
2193  return "";
2194  }
2195 
2197  if (auto *F = getFile())
2198  return F->getDirectory();
2199  return "";
2200  }
2201 
2203  if (auto *F = getFile())
2204  return F->getSource();
2205  return None;
2206  }
2207 
2208  Metadata *getRawScope() const { return getOperand(0); }
2209  MDString *getRawName() const { return getOperandAs<MDString>(1); }
2210  Metadata *getRawFile() const { return getOperand(2); }
2211  Metadata *getRawType() const { return getOperand(3); }
2212 
2213  static bool classof(const Metadata *MD) {
2214  return MD->getMetadataID() == DILocalVariableKind ||
2215  MD->getMetadataID() == DIGlobalVariableKind;
2216  }
2217 };
2218 
2219 /// DWARF expression.
2220 ///
2221 /// This is (almost) a DWARF expression that modifies the location of a
2222 /// variable, or the location of a single piece of a variable, or (when using
2223 /// DW_OP_stack_value) is the constant variable value.
2224 ///
2225 /// TODO: Co-allocate the expression elements.
2226 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
2227 /// storage types.
2228 class DIExpression : public MDNode {
2229  friend class LLVMContextImpl;
2230  friend class MDNode;
2231 
2232  std::vector<uint64_t> Elements;
2233 
2235  : MDNode(C, DIExpressionKind, Storage, None),
2236  Elements(Elements.begin(), Elements.end()) {}
2237  ~DIExpression() = default;
2238 
2239  static DIExpression *getImpl(LLVMContext &Context,
2240  ArrayRef<uint64_t> Elements, StorageType Storage,
2241  bool ShouldCreate = true);
2242 
2243  TempDIExpression cloneImpl() const {
2244  return getTemporary(getContext(), getElements());
2245  }
2246 
2247 public:
2249 
2250  TempDIExpression clone() const { return cloneImpl(); }
2251 
2252  ArrayRef<uint64_t> getElements() const { return Elements; }
2253 
2254  unsigned getNumElements() const { return Elements.size(); }
2255 
2256  uint64_t getElement(unsigned I) const {
2257  assert(I < Elements.size() && "Index out of range");
2258  return Elements[I];
2259  }
2260 
2261  /// Determine whether this represents a standalone constant value.
2262  bool isConstant() const;
2263 
2265 
2266  element_iterator elements_begin() const { return getElements().begin(); }
2267  element_iterator elements_end() const { return getElements().end(); }
2268 
2269  /// A lightweight wrapper around an expression operand.
2270  ///
2271  /// TODO: Store arguments directly and change \a DIExpression to store a
2272  /// range of these.
2273  class ExprOperand {
2274  const uint64_t *Op = nullptr;
2275 
2276  public:
2277  ExprOperand() = default;
2278  explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
2279 
2280  const uint64_t *get() const { return Op; }
2281 
2282  /// Get the operand code.
2283  uint64_t getOp() const { return *Op; }
2284 
2285  /// Get an argument to the operand.
2286  ///
2287  /// Never returns the operand itself.
2288  uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2289 
2290  unsigned getNumArgs() const { return getSize() - 1; }
2291 
2292  /// Return the size of the operand.
2293  ///
2294  /// Return the number of elements in the operand (1 + args).
2295  unsigned getSize() const;
2296  };
2297 
2298  /// An iterator for expression operands.
2300  : public std::iterator<std::input_iterator_tag, ExprOperand> {
2301  ExprOperand Op;
2302 
2303  public:
2304  expr_op_iterator() = default;
2305  explicit expr_op_iterator(element_iterator I) : Op(I) {}
2306 
2307  element_iterator getBase() const { return Op.get(); }
2308  const ExprOperand &operator*() const { return Op; }
2309  const ExprOperand *operator->() const { return &Op; }
2310 
2312  increment();
2313  return *this;
2314  }
2316  expr_op_iterator T(*this);
2317  increment();
2318  return T;
2319  }
2320 
2321  /// Get the next iterator.
2322  ///
2323  /// \a std::next() doesn't work because this is technically an
2324  /// input_iterator, but it's a perfectly valid operation. This is an
2325  /// accessor to provide the same functionality.
2326  expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2327 
2328  bool operator==(const expr_op_iterator &X) const {
2329  return getBase() == X.getBase();
2330  }
2331  bool operator!=(const expr_op_iterator &X) const {
2332  return getBase() != X.getBase();
2333  }
2334 
2335  private:
2336  void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2337  };
2338 
2339  /// Visit the elements via ExprOperand wrappers.
2340  ///
2341  /// These range iterators visit elements through \a ExprOperand wrappers.
2342  /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2343  /// true.
2344  ///
2345  /// \pre \a isValid() gives \c true.
2346  /// @{
2348  return expr_op_iterator(elements_begin());
2349  }
2351  return expr_op_iterator(elements_end());
2352  }
2354  return {expr_op_begin(), expr_op_end()};
2355  }
2356  /// @}
2357 
2358  bool isValid() const;
2359 
2360  static bool classof(const Metadata *MD) {
2361  return MD->getMetadataID() == DIExpressionKind;
2362  }
2363 
2364  /// Return whether the first element a DW_OP_deref.
2365  bool startsWithDeref() const {
2366  return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
2367  }
2368 
2369  /// Holds the characteristics of one fragment of a larger variable.
2370  struct FragmentInfo {
2371  uint64_t SizeInBits;
2372  uint64_t OffsetInBits;
2373  };
2374 
2375  /// Retrieve the details of this fragment expression.
2376  static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
2378 
2379  /// Retrieve the details of this fragment expression.
2381  return getFragmentInfo(expr_op_begin(), expr_op_end());
2382  }
2383 
2384  /// Return whether this is a piece of an aggregate variable.
2385  bool isFragment() const { return getFragmentInfo().hasValue(); }
2386 
2387  /// Append \p Ops with operations to apply the \p Offset.
2388  static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
2389 
2390  /// If this is a constant offset, extract it. If there is no expression,
2391  /// return true with an offset of zero.
2392  bool extractIfOffset(int64_t &Offset) const;
2393 
2394  /// Constants for DIExpression::prepend.
2395  enum { NoDeref = false, WithDeref = true, WithStackValue = true };
2396 
2397  /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
2398  /// into a stack value.
2399  static DIExpression *prepend(const DIExpression *DIExpr, bool DerefBefore,
2400  int64_t Offset = 0, bool DerefAfter = false,
2401  bool StackValue = false);
2402 
2403  /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
2404  /// stack value.
2405  static DIExpression *doPrepend(const DIExpression *DIExpr,
2407  bool StackValue = false);
2408 
2409  /// Create a DIExpression to describe one part of an aggregate variable that
2410  /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
2411  /// will be appended to the elements of \c Expr. If \c Expr already contains
2412  /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
2413  /// into the existing fragment.
2414  ///
2415  /// \param OffsetInBits Offset of the piece in bits.
2416  /// \param SizeInBits Size of the piece in bits.
2417  /// \return Creating a fragment expression may fail if \c Expr
2418  /// contains arithmetic operations that would be truncated.
2420  createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
2421  unsigned SizeInBits);
2422 
2423  /// Determine the relative position of the fragments described by this
2424  /// DIExpression and \p Other.
2425  /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
2426  /// 1 if this is entirely after Other.
2427  int fragmentCmp(const DIExpression *Other) const {
2428  auto Fragment1 = *getFragmentInfo();
2429  auto Fragment2 = *Other->getFragmentInfo();
2430  unsigned l1 = Fragment1.OffsetInBits;
2431  unsigned l2 = Fragment2.OffsetInBits;
2432  unsigned r1 = l1 + Fragment1.SizeInBits;
2433  unsigned r2 = l2 + Fragment2.SizeInBits;
2434  if (r1 <= l2)
2435  return -1;
2436  else if (r2 <= l1)
2437  return 1;
2438  else
2439  return 0;
2440  }
2441 
2442  /// Check if fragments overlap between this DIExpression and \p Other.
2443  bool fragmentsOverlap(const DIExpression *Other) const {
2444  if (!isFragment() || !Other->isFragment())
2445  return true;
2446  return fragmentCmp(Other) == 0;
2447  }
2448 };
2449 
2450 /// Global variables.
2451 ///
2452 /// TODO: Remove DisplayName. It's always equal to Name.
2454  friend class LLVMContextImpl;
2455  friend class MDNode;
2456 
2457  bool IsLocalToUnit;
2458  bool IsDefinition;
2459 
2460  DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2461  bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
2463  : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
2464  IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
2465  ~DIGlobalVariable() = default;
2466 
2467  static DIGlobalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2468  StringRef Name, StringRef LinkageName,
2469  DIFile *File, unsigned Line, DITypeRef Type,
2470  bool IsLocalToUnit, bool IsDefinition,
2471  DIDerivedType *StaticDataMemberDeclaration,
2472  uint32_t AlignInBits, StorageType Storage,
2473  bool ShouldCreate = true) {
2474  return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
2475  getCanonicalMDString(Context, LinkageName), File, Line, Type,
2476  IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
2477  AlignInBits, Storage, ShouldCreate);
2478  }
2479  static DIGlobalVariable *
2480  getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
2481  MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
2482  bool IsLocalToUnit, bool IsDefinition,
2483  Metadata *StaticDataMemberDeclaration, uint32_t AlignInBits,
2484  StorageType Storage, bool ShouldCreate = true);
2485 
2486  TempDIGlobalVariable cloneImpl() const {
2487  return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
2488  getFile(), getLine(), getType(), isLocalToUnit(),
2489  isDefinition(), getStaticDataMemberDeclaration(),
2490  getAlignInBits());
2491  }
2492 
2493 public:
2495  (DIScope * Scope, StringRef Name, StringRef LinkageName,
2496  DIFile *File, unsigned Line, DITypeRef Type,
2497  bool IsLocalToUnit, bool IsDefinition,
2498  DIDerivedType *StaticDataMemberDeclaration,
2499  uint32_t AlignInBits),
2500  (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2501  IsDefinition, StaticDataMemberDeclaration, AlignInBits))
2503  (Metadata * Scope, MDString *Name, MDString *LinkageName,
2504  Metadata *File, unsigned Line, Metadata *Type,
2505  bool IsLocalToUnit, bool IsDefinition,
2506  Metadata *StaticDataMemberDeclaration,
2507  uint32_t AlignInBits),
2508  (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2509  IsDefinition, StaticDataMemberDeclaration, AlignInBits))
2510 
2511  TempDIGlobalVariable clone() const { return cloneImpl(); }
2512 
2513  bool isLocalToUnit() const { return IsLocalToUnit; }
2514  bool isDefinition() const { return IsDefinition; }
2515  StringRef getDisplayName() const { return getStringOperand(4); }
2516  StringRef getLinkageName() const { return getStringOperand(5); }
2518  return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
2519  }
2520 
2521  MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
2522  Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
2523 
2524  static bool classof(const Metadata *MD) {
2525  return MD->getMetadataID() == DIGlobalVariableKind;
2526  }
2527 };
2528 
2529 /// Local variable.
2530 ///
2531 /// TODO: Split up flags.
2532 class DILocalVariable : public DIVariable {
2533  friend class LLVMContextImpl;
2534  friend class MDNode;
2535 
2536  unsigned Arg : 16;
2537  DIFlags Flags;
2538 
2539  DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
2540  unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
2542  : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
2543  Arg(Arg), Flags(Flags) {
2544  assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
2545  }
2546  ~DILocalVariable() = default;
2547 
2548  static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
2549  StringRef Name, DIFile *File, unsigned Line,
2550  DITypeRef Type, unsigned Arg, DIFlags Flags,
2551  uint32_t AlignInBits, StorageType Storage,
2552  bool ShouldCreate = true) {
2553  return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
2554  Line, Type, Arg, Flags, AlignInBits, Storage, ShouldCreate);
2555  }
2556  static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
2557  MDString *Name, Metadata *File, unsigned Line,
2558  Metadata *Type, unsigned Arg, DIFlags Flags,
2559  uint32_t AlignInBits, StorageType Storage,
2560  bool ShouldCreate = true);
2561 
2562  TempDILocalVariable cloneImpl() const {
2563  return getTemporary(getContext(), getScope(), getName(), getFile(),
2564  getLine(), getType(), getArg(), getFlags(),
2565  getAlignInBits());
2566  }
2567 
2568 public:
2570  (DILocalScope * Scope, StringRef Name, DIFile *File,
2571  unsigned Line, DITypeRef Type, unsigned Arg,
2572  DIFlags Flags, uint32_t AlignInBits),
2573  (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2575  (Metadata * Scope, MDString *Name, Metadata *File,
2576  unsigned Line, Metadata *Type, unsigned Arg,
2577  DIFlags Flags, uint32_t AlignInBits),
2578  (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits))
2579 
2580  TempDILocalVariable clone() const { return cloneImpl(); }
2581 
2582  /// Get the local scope for this variable.
2583  ///
2584  /// Variables must be defined in a local scope.
2586  return cast<DILocalScope>(DIVariable::getScope());
2587  }
2588 
2589  bool isParameter() const { return Arg; }
2590  unsigned getArg() const { return Arg; }
2591  DIFlags getFlags() const { return Flags; }
2592 
2593  bool isArtificial() const { return getFlags() & FlagArtificial; }
2594  bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
2595 
2596  /// Check that a location is valid for this variable.
2597  ///
2598  /// Check that \c DL exists, is in the same subprogram, and has the same
2599  /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
2600  /// to a \a DbgInfoIntrinsic.)
2601  bool isValidLocationForIntrinsic(const DILocation *DL) const {
2602  return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
2603  }
2604 
2605  static bool classof(const Metadata *MD) {
2606  return MD->getMetadataID() == DILocalVariableKind;
2607  }
2608 };
2609 
2610 class DIObjCProperty : public DINode {
2611  friend class LLVMContextImpl;
2612  friend class MDNode;
2613 
2614  unsigned Line;
2615  unsigned Attributes;
2616 
2617  DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2618  unsigned Attributes, ArrayRef<Metadata *> Ops)
2619  : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2620  Ops),
2621  Line(Line), Attributes(Attributes) {}
2622  ~DIObjCProperty() = default;
2623 
2624  static DIObjCProperty *
2625  getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2626  StringRef GetterName, StringRef SetterName, unsigned Attributes,
2627  DITypeRef Type, StorageType Storage, bool ShouldCreate = true) {
2628  return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2629  getCanonicalMDString(Context, GetterName),
2630  getCanonicalMDString(Context, SetterName), Attributes, Type,
2631  Storage, ShouldCreate);
2632  }
2633  static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2634  Metadata *File, unsigned Line,
2635  MDString *GetterName, MDString *SetterName,
2636  unsigned Attributes, Metadata *Type,
2637  StorageType Storage, bool ShouldCreate = true);
2638 
2639  TempDIObjCProperty cloneImpl() const {
2640  return getTemporary(getContext(), getName(), getFile(), getLine(),
2641  getGetterName(), getSetterName(), getAttributes(),
2642  getType());
2643  }
2644 
2645 public:
2647  (StringRef Name, DIFile *File, unsigned Line,
2648  StringRef GetterName, StringRef SetterName,
2649  unsigned Attributes, DITypeRef Type),
2650  (Name, File, Line, GetterName, SetterName, Attributes,
2651  Type))
2653  (MDString * Name, Metadata *File, unsigned Line,
2654  MDString *GetterName, MDString *SetterName,
2655  unsigned Attributes, Metadata *Type),
2656  (Name, File, Line, GetterName, SetterName, Attributes,
2657  Type))
2658 
2659  TempDIObjCProperty clone() const { return cloneImpl(); }
2660 
2661  unsigned getLine() const { return Line; }
2662  unsigned getAttributes() const { return Attributes; }
2663  StringRef getName() const { return getStringOperand(0); }
2664  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2665  StringRef getGetterName() const { return getStringOperand(2); }
2666  StringRef getSetterName() const { return getStringOperand(3); }
2667  DITypeRef getType() const { return DITypeRef(getRawType()); }
2668 
2670  if (auto *F = getFile())
2671  return F->getFilename();
2672  return "";
2673  }
2674 
2676  if (auto *F = getFile())
2677  return F->getDirectory();
2678  return "";
2679  }
2680 
2682  if (auto *F = getFile())
2683  return F->getSource();
2684  return None;
2685  }
2686 
2687  MDString *getRawName() const { return getOperandAs<MDString>(0); }
2688  Metadata *getRawFile() const { return getOperand(1); }
2689  MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2690  MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2691  Metadata *getRawType() const { return getOperand(4); }
2692 
2693  static bool classof(const Metadata *MD) {
2694  return MD->getMetadataID() == DIObjCPropertyKind;
2695  }
2696 };
2697 
2698 /// An imported module (C++ using directive or similar).
2699 class DIImportedEntity : public DINode {
2700  friend class LLVMContextImpl;
2701  friend class MDNode;
2702 
2703  unsigned Line;
2704 
2705  DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2706  unsigned Line, ArrayRef<Metadata *> Ops)
2707  : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2708  ~DIImportedEntity() = default;
2709 
2710  static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2711  DIScope *Scope, DINodeRef Entity,
2712  DIFile *File, unsigned Line, StringRef Name,
2713  StorageType Storage,
2714  bool ShouldCreate = true) {
2715  return getImpl(Context, Tag, Scope, Entity, File, Line,
2716  getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2717  }
2718  static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2719  Metadata *Scope, Metadata *Entity,
2720  Metadata *File, unsigned Line,
2721  MDString *Name, StorageType Storage,
2722  bool ShouldCreate = true);
2723 
2724  TempDIImportedEntity cloneImpl() const {
2725  return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2726  getFile(), getLine(), getName());
2727  }
2728 
2729 public:
2731  (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2732  DIFile *File, unsigned Line, StringRef Name = ""),
2733  (Tag, Scope, Entity, File, Line, Name))
2735  (unsigned Tag, Metadata *Scope, Metadata *Entity,
2736  Metadata *File, unsigned Line, MDString *Name),
2737  (Tag, Scope, Entity, File, Line, Name))
2738 
2739  TempDIImportedEntity clone() const { return cloneImpl(); }
2740 
2741  unsigned getLine() const { return Line; }
2742  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2743  DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2744  StringRef getName() const { return getStringOperand(2); }
2745  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2746 
2747  Metadata *getRawScope() const { return getOperand(0); }
2748  Metadata *getRawEntity() const { return getOperand(1); }
2749  MDString *getRawName() const { return getOperandAs<MDString>(2); }
2750  Metadata *getRawFile() const { return getOperand(3); }
2751 
2752  static bool classof(const Metadata *MD) {
2753  return MD->getMetadataID() == DIImportedEntityKind;
2754  }
2755 };
2756 
2757 /// A pair of DIGlobalVariable and DIExpression.
2759  friend class LLVMContextImpl;
2760  friend class MDNode;
2761 
2764  : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
2765  ~DIGlobalVariableExpression() = default;
2766 
2768  getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
2769  StorageType Storage, bool ShouldCreate = true);
2770 
2771  TempDIGlobalVariableExpression cloneImpl() const {
2772  return getTemporary(getContext(), getVariable(), getExpression());
2773  }
2774 
2775 public:
2777  (Metadata * Variable, Metadata *Expression),
2778  (Variable, Expression))
2779 
2780  TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
2781 
2782  Metadata *getRawVariable() const { return getOperand(0); }
2783 
2785  return cast_or_null<DIGlobalVariable>(getRawVariable());
2786  }
2787 
2788  Metadata *getRawExpression() const { return getOperand(1); }
2789 
2791  return cast<DIExpression>(getRawExpression());
2792  }
2793 
2794  static bool classof(const Metadata *MD) {
2795  return MD->getMetadataID() == DIGlobalVariableExpressionKind;
2796  }
2797 };
2798 
2799 /// Macro Info DWARF-like metadata node.
2800 ///
2801 /// A metadata node with a DWARF macro info (i.e., a constant named
2802 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
2803 /// DIMacroNode
2804 /// because it's potentially used for non-DWARF output.
2805 class DIMacroNode : public MDNode {
2806  friend class LLVMContextImpl;
2807  friend class MDNode;
2808 
2809 protected:
2810  DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
2812  : MDNode(C, ID, Storage, Ops1, Ops2) {
2813  assert(MIType < 1u << 16);
2814  SubclassData16 = MIType;
2815  }
2816  ~DIMacroNode() = default;
2817 
2818  template <class Ty> Ty *getOperandAs(unsigned I) const {
2819  return cast_or_null<Ty>(getOperand(I));
2820  }
2821 
2822  StringRef getStringOperand(unsigned I) const {
2823  if (auto *S = getOperandAs<MDString>(I))
2824  return S->getString();
2825  return StringRef();
2826  }
2827 
2829  if (S.empty())
2830  return nullptr;
2831  return MDString::get(Context, S);
2832  }
2833 
2834 public:
2835  unsigned getMacinfoType() const { return SubclassData16; }
2836 
2837  static bool classof(const Metadata *MD) {
2838  switch (MD->getMetadataID()) {
2839  default:
2840  return false;
2841  case DIMacroKind:
2842  case DIMacroFileKind:
2843  return true;
2844  }
2845  }
2846 };
2847 
2848 class DIMacro : public DIMacroNode {
2849  friend class LLVMContextImpl;
2850  friend class MDNode;
2851 
2852  unsigned Line;
2853 
2854  DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
2856  : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
2857  ~DIMacro() = default;
2858 
2859  static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2860  StringRef Name, StringRef Value, StorageType Storage,
2861  bool ShouldCreate = true) {
2862  return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
2863  getCanonicalMDString(Context, Value), Storage, ShouldCreate);
2864  }
2865  static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
2866  MDString *Name, MDString *Value, StorageType Storage,
2867  bool ShouldCreate = true);
2868 
2869  TempDIMacro cloneImpl() const {
2870  return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
2871  getValue());
2872  }
2873 
2874 public:
2875  DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, StringRef Name,
2876  StringRef Value = ""),
2877  (MIType, Line, Name, Value))
2878  DEFINE_MDNODE_GET(DIMacro, (unsigned MIType, unsigned Line, MDString *Name,
2879  MDString *Value),
2880  (MIType, Line, Name, Value))
2881 
2882  TempDIMacro clone() const { return cloneImpl(); }
2883 
2884  unsigned getLine() const { return Line; }
2885 
2886  StringRef getName() const { return getStringOperand(0); }
2887  StringRef getValue() const { return getStringOperand(1); }
2888 
2889  MDString *getRawName() const { return getOperandAs<MDString>(0); }
2890  MDString *getRawValue() const { return getOperandAs<MDString>(1); }
2891 
2892  static bool classof(const Metadata *MD) {
2893  return MD->getMetadataID() == DIMacroKind;
2894  }
2895 };
2896 
2897 class DIMacroFile : public DIMacroNode {
2898  friend class LLVMContextImpl;
2899  friend class MDNode;
2900 
2901  unsigned Line;
2902 
2903  DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
2904  unsigned Line, ArrayRef<Metadata *> Ops)
2905  : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
2906  ~DIMacroFile() = default;
2907 
2908  static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2909  unsigned Line, DIFile *File,
2910  DIMacroNodeArray Elements, StorageType Storage,
2911  bool ShouldCreate = true) {
2912  return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
2913  Elements.get(), Storage, ShouldCreate);
2914  }
2915 
2916  static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
2917  unsigned Line, Metadata *File, Metadata *Elements,
2918  StorageType Storage, bool ShouldCreate = true);
2919 
2920  TempDIMacroFile cloneImpl() const {
2921  return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
2922  getElements());
2923  }
2924 
2925 public:
2926  DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line, DIFile *File,
2927  DIMacroNodeArray Elements),
2928  (MIType, Line, File, Elements))
2929  DEFINE_MDNODE_GET(DIMacroFile, (unsigned MIType, unsigned Line,
2930  Metadata *File, Metadata *Elements),
2931  (MIType, Line, File, Elements))
2932 
2933  TempDIMacroFile clone() const { return cloneImpl(); }
2934 
2935  void replaceElements(DIMacroNodeArray Elements) {
2936 #ifndef NDEBUG
2937  for (DIMacroNode *Op : getElements())
2938  assert(is_contained(Elements->operands(), Op) &&
2939  "Lost a macro node during macro node list replacement");
2940 #endif
2941  replaceOperandWith(1, Elements.get());
2942  }
2943 
2944  unsigned getLine() const { return Line; }
2945  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2946 
2947  DIMacroNodeArray getElements() const {
2948  return cast_or_null<MDTuple>(getRawElements());
2949  }
2950 
2951  Metadata *getRawFile() const { return getOperand(0); }
2952  Metadata *getRawElements() const { return getOperand(1); }
2953 
2954  static bool classof(const Metadata *MD) {
2955  return MD->getMetadataID() == DIMacroFileKind;
2956  }
2957 };
2958 
2959 } // end namespace llvm
2960 
2961 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
2962 #undef DEFINE_MDNODE_GET_UNPACK
2963 #undef DEFINE_MDNODE_GET
2964 
2965 #endif // LLVM_IR_DEBUGINFOMETADATA_H
DIFlags getFlags() const
T Value
The string value of the checksum.
bool operator==(const expr_op_iterator &X) const
bool isObjectPointer() const
StringRef getFilename() const
Metadata * getRawRetainedTypes() const
uint64_t CallInst * C
static bool classof(const Metadata *MD)
MDString * getRawName() const
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:245
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:709
ArrayRef< uint64_t > getElements() const
#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue)
LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can perform bitwise operatio...
Definition: BitmaskEnum.h:42
StringRef getKindAsString() const
uint64_t getOffsetInBits() const
bool isPrivate() const
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:55
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1131
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
DITypeRef operator[](unsigned I) const
static bool isConstant(const MachineInstr &MI)
bool isTypePassByValue() const
unsigned getLine() const
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:236
StringRef getName() const
ChecksumKind Kind
The kind of checksum which Value encodes.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
StringRef getName() const
unsigned getRuntimeVersion() const
Metadata * getRawGlobalVariables() const
bool isVirtual() const
Metadata * getRawScope() const
MDString * getRawName() const
StringRef getGetterName() const
Metadata * getRawVTableHolder() const
unsigned size() const
MDTuple * operator->() const
void replaceTemplateParams(DITemplateParameterArray TemplateParams)
MDString * getRawName() const
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:454
Metadata * getRawFile() const
bool operator!=(const expr_op_iterator &X) const
DIFile * getFile() const
DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, ArrayRef< Metadata *> Ops)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
DITypeRef getBaseType() const
static bool classof(const Metadata *MD)
unsigned getDiscriminator() const
MDString * getRawValue() const
Metadata * getRawFile() const
Return the raw underlying file.
TypedDINodeRef(std::nullptr_t)
static bool classof(const Metadata *MD)
bool isVector() const
This file contains the declarations for metadata subclasses.
void setSplitDebugInlining(bool SplitDebugInlining)
void emplace(ArgTypes &&... Args)
Create a new object by constructing it in place with the given arguments.
Definition: Optional.h:153
Metadata * getRawInlinedAt() const
DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, ArrayRef< Metadata *> Ops)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null, or not exclusively derived from constant.
unsigned getSize() const
Return the size of the operand.
ArrayRef< uint64_t >::iterator element_iterator
DICompositeTypeArray getEnumTypes() const
const ExprOperand & operator*() const
uint64_t getDWOId() const
static bool classof(const Metadata *MD)
static bool classof(const Metadata *MD)
Metadata node.
Definition: Metadata.h:862
F(f)
Metadata * getRawScope() const
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
static bool classof(const Metadata *MD)
StringRef getConfigurationMacros() const
static bool classof(const Metadata *MD)
static bool classof(const Metadata *MD)
StringRef getProducer() const
static bool classof(const Metadata *MD)
bool isForwardDecl() const
bool getDebugInfoForProfiling() const
unsigned getVirtuality(StringRef VirtualityString)
Definition: Dwarf.cpp:291
TempDIType clone() const
#define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
op_iterator op_end() const
Definition: Metadata.h:1061
StringRef getName() const
Tuple of metadata.
Definition: Metadata.h:1104
A scope for locals.
Tagged DWARF-like metadata node.
void replaceRetainedTypes(DITypeArray N)
const DILocation * setBaseDiscriminator(unsigned BD) const
Returns a new DILocation with updated base discriminator BD.
bool operator!=(const TypedDINodeRef< T > &X) const
DINodeArray getElements() const
TempMDNode clone() const
Create a (temporary) clone of this.
Definition: Metadata.cpp:522
const DILocation * cloneWithDuplicationFactor(unsigned DF) const
Returns a new DILocation with duplication factor DF encoded in the discriminator. ...
MDString * getRawName() const
op_range dwarf_operands() const
static bool classof(const Metadata *MD)
StringRef getStringOperand(unsigned I) const
DIScope * getScope() const
Metadata * getRawTypeArray() const
Ty * getOperandAs(unsigned I) const
StringRef getSplitDebugFilename() const
StringRef getName() const
StringRef getFlags() const
StringRef getName() const
bool isLValueReference() const
bool isStaticMember() const
unsigned getTag() const
static bool classof(const Metadata *MD)
DIGlobalVariable * getVariable() const
static bool classof(const Metadata *MD)
Array subrange.
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
MDString * getRawSetterName() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
static bool classof(const Metadata *MD)
iterator(MDNode::op_iterator I)
static StringRef getName(Value *V)
const uint64_t * get() const
uint64_t getSizeInBits() const
Holds a subclass of DINode.
StringRef getDirectory() const
Metadata * getRawType() const
DITypeRef getVTableHolder() const
StringRef getFilename() const
MDString * getRawISysRoot() const
op_iterator op_begin() const
Definition: Metadata.h:1057
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
bool getExportSymbols() const
Subprogram description.
Metadata * getRawImportedEntities() const
expr_op_iterator expr_op_end() const
TypedDINodeRef(const T *MD)
StringRef getIncludePath() const
Holds the characteristics of one fragment of a larger variable.
iterator end() const
unsigned getRuntimeLang() const
unsigned getDuplicationFactor() const
Returns the duplication factor stored in the discriminator.
DITypeRef getType() const
MDString * getRawName() const
#define T
Enumeration value.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
DIMacroNodeArray getElements() const
uint32_t getAlignInBytes() const
unsigned getMetadataID() const
Definition: Metadata.h:98
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const MDOperand & getDwarfOperand(unsigned I) const
uint32_t getAlignInBits() const
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, ArrayRef< Metadata *> Ops1, ArrayRef< Metadata *> Ops2=None)
Metadata * getRawEntity() const
Debug location.
StringRef getFilename() const
DIScope * getScope() const
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:993
uint64_t getArg(unsigned I) const
Get an argument to the operand.
StringRef getDirectory() const
Metadata * getRawFile() const
unsigned getLine() const
StringRef getStringOperand(unsigned I) const
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
DIScope * getScope() const
static bool classof(const Metadata *MD)
uint64_t getOp() const
Get the operand code.
DIMacroNodeArray getMacros() const
op_iterator dwarf_op_begin() const
void replaceVTableHolder(DITypeRef VTableHolder)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
DITemplateParameterArray getTemplateParams() const
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
unsigned getColumn() const
TypedDINodeRef()=default
TypedDINodeRef< DINode > DINodeRef
Metadata * getRawDiscriminator() const
bool operator==(const ChecksumInfo< T > &X) const
static bool classof(const Metadata *MD)
A single checksum, represented by a Kind and a Value (a string).
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static bool classof(const Metadata *MD)
void replaceDwarfOperandWith(unsigned I, Metadata *New)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
This is an important base class in LLVM.
Definition: Constant.h:42
CountType getCount() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
SourceLanguage
Definition: Dwarf.h:186
Base class for template parameters.
unsigned getAttributes() const
ChecksumInfo(ChecksumKind Kind, T Value)
Ty * getOperandAs(unsigned I) const
StorageType
Active type of storage.
Definition: Metadata.h:66
DIExpression * getExpression() const
StringRef getName() const
A lightweight wrapper around an expression operand.
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
A pair of DIGlobalVariable and DIExpression.
iterator begin() const
DIImportedEntityArray getImportedEntities() const
static MDString * getCanonicalMDString(LLVMContext &Context, StringRef S)
void replaceEnumTypes(DICompositeTypeArray N)
Replace arrays.
void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags)
Change fields in place.
DIDerivedType * getStaticDataMemberDeclaration() const
static const unsigned End
Optional< FragmentInfo > getFragmentInfo() const
Retrieve the details of this fragment expression.
DEFINE_MDNODE_GET(DIGlobalVariableExpression,(Metadata *Variable, Metadata *Expression),(Variable, Expression)) TempDIGlobalVariableExpression clone() const
Base class for variables.
TypedDINodeRef(const TypedDINodeRef< U > &X, typename std::enable_if< std::is_convertible< U *, T *>::value >::type *=nullptr)
ChecksumKind
Which algorithm (e.g.
DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Line, ArrayRef< Metadata *> Ops, uint32_t AlignInBits=0)
void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags)
bool isObjcClassComplete() const
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:49
StringRef getHeader() const
static bool classof(const Metadata *MD)
MDString * getRawConfigurationMacros() const
StringRef getDirectory() const
unsigned getLine() const
static DISubprogram * getSubprogram(bool IsDistinct, Ts &&... Args)
Definition: DIBuilder.cpp:713
element_iterator elements_end() const
static wasm::ValType getType(const TargetRegisterClass *RC)
uint64_t getElement(unsigned I) const
static void get()=delete
Metadata * getRawType() const
static bool classof(const Metadata *MD)
MDString * getRawName() const
MDTuple * get() const
MDString * getRawName() const
static bool classof(const Metadata *MD)
bool isRValueReference() const
static bool classof(const Metadata *MD)
Optional< StringRef > getSource() const
An imported module (C++ using directive or similar).
StringRef getSetterName() const
Base class for scope-like contexts.
iterator_range< expr_op_iterator > expr_ops() const
op_iterator dwarf_op_end() const
void setTag(unsigned Tag)
Allow subclasses to mutate the tag.
Metadata * getRawElements() const
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
bool isPublic() const
int64_t getValue() const
static SimpleType getSimplifiedValue(const TypedDINodeRef< T > &MD)
static bool classof(const Metadata *MD)
Metadata * getRawScope() const
static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D)
Returns the base discriminator for a given encoded discriminator D.
void setDWOId(uint64_t DwoId)
static MDString * getCanonicalMDString(LLVMContext &Context, StringRef S)
DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage, unsigned Tag, ArrayRef< Metadata *> Ops)
Base class for types.
Metadata * getRawTemplateParams() const
static bool classof(const Metadata *MD)
static unsigned getDuplicationFactorFromDiscriminator(unsigned D)
Returns the duplication factor for a given encoded discriminator D.
unsigned getLine() const
DIFile * getFile() const
unsigned getCopyIdentifier() const
Returns the copy identifier stored in the discriminator.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
static bool classof(const Metadata *MD)
static bool classof(const Metadata *MD)
TypedDINodeRef(const Metadata *MD)
MDString * getRawHeader() const
const ExprOperand * operator->() const
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
DIFile * getFile() const
MDTuple & operator*() const
static unsigned getCopyIdentifierFromDiscriminator(unsigned D)
Returns the copy identifier for a given encoded discriminator D.
bool isProtected() const
static Optional< unsigned > getTag(const TargetRegisterInfo *TRI, const MachineInstr &MI, const LoadInfo &LI)
void setFlags(DIFlags NewFlags)
Metadata * getRawElements() const
Metadata * getRawFile() const
StringRef getDisplayName() const
DWARF expression.
DebugEmissionKind getEmissionKind() const
unsigned getSourceLanguage() const
StringRef getLinkageName() const
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
Metadata * getRawFile() const
bool fragmentsOverlap(const DIExpression *Other) const
Check if fragments overlap between this DIExpression and Other.
A (clang) module that has been imported by the compile unit.
DIGlobalVariableExpressionArray getGlobalVariables() const
bool isBlockByrefStruct() const
Generic tagged DWARF-like metadata node.
unsigned getNumElements() const
amdgpu Simplify well known AMD library false Value Value * Arg
Metadata * getRawBaseType() const
StringRef getIdentifier() const
Type array for a subprogram.
static bool classof(const Metadata *MD)
bool getSplitDebugInlining() const
DIFlags
Debug info flags.
MDString * getRawIdentifier() const
Macro Info DWARF-like metadata node.
unsigned getBaseDiscriminator() const
Returns the base discriminator stored in the discriminator.
static bool classof(const Metadata *MD)
bool isArtificial() const
Metadata * getRawEnumTypes() const
unsigned getEncoding() const
Metadata * getRawScope() const
MDString * getRawSplitDebugFilename() const
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType, ArrayRef< Metadata *> Ops1, ArrayRef< Metadata *> Ops2=None)
static bool classof(const Metadata *MD)
MDString * getRawProducer() const
Optional< StringRef > getSource() const
MDString * getRawName() const
element_iterator elements_begin() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
unsigned getMacinfoType() const
StringRef getValue() const
DITypeRefArray(const MDTuple *N)
MDString * getRawLinkageName() const
unsigned getNumDwarfOperands() const
Optional< StringRef > getSource() const
Metadata * getRawStaticDataMemberDeclaration() const
DIScopeArray getRetainedTypes() const
static bool classof(const Metadata *MD)
bool operator!=(const iterator &X) const
DITypeRef getType() const
unsigned getTag() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint32_t getAlignInBits() const
aarch64 promote const
uint32_t getAlignInBytes() const
LLVM Value Representation.
Definition: Value.h:73
DIScopeRef getScope() const
static bool classof(const Metadata *MD)
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
void replaceGlobalVariables(DIGlobalVariableExpressionArray N)
Metadata * getRawMacros() const
DINodeRef getEntity() const
int fragmentCmp(const DIExpression *Other) const
Determine the relative position of the fragments described by this DIExpression and Other...
DIScope * getScope() const
bool operator==(const TypedDINodeRef< T > &X) const
bool operator!=(const ChecksumInfo< T > &X) const
expr_op_iterator getNext() const
Get the next iterator.
DITypeRefArray getTypeArray() const
bool isBitField() const
bool isAppleBlockExtension() const
DIDerivedType * getDiscriminator() const
Metadata * getRawScope() const
An iterator for expression operands.
void replaceMacros(DIMacroNodeArray N)
TypedDINodeRef< DIScope > DIScopeRef
#define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
DILocalScope * getScope() const
A single uniqued string.
Definition: Metadata.h:602
MDString * getRawIncludePath() const
bool operator==(const iterator &X) const
bool startsWithDeref() const
Return whether the first element a DW_OP_deref.
unsigned getLine() const
Metadata * getRawType() const
Metadata * getRawScope() const
TypedDINodeRef< DIType > DITypeRef
DIFile * getFile() const
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
DILocalScope * getScope() const
Get the local scope for this variable.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage, ArrayRef< Metadata *> Ops)
DEFINE_MDNODE_GET(DIExpression,(ArrayRef< uint64_t > Elements),(Elements)) TempDIExpression clone() const
void replaceElements(DINodeArray Elements)
Replace operands.
bool isFragment() const
Return whether this is a piece of an aggregate variable.
MDString * getRawName() const
Root of the metadata hierarchy.
Definition: Metadata.h:58
unsigned getLine() const
MDString * getRawGetterName() const
StringRef getISysRoot() const
unsigned getHash() const
DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, ArrayRef< Metadata *> Ops)
StringRef getName() const
static bool classof(const Metadata *MD)
Metadata * getRawScope() const
void replaceElements(DIMacroNodeArray Elements)
static bool classof(const Metadata *MD)
bool isTypePassByReference() const
static bool classof(const Metadata *MD)
MDString * getRawFlags() const
void replaceImportedEntities(DIImportedEntityArray N)
StringRef getName() const
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87
Basic type, like &#39;int&#39; or &#39;float&#39;.
DIScopeRef getScope() const
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:905