DebugInfoMetadata.h revision 283625
1156230Smux//===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
2156230Smux//
3156230Smux//                     The LLVM Compiler Infrastructure
4156230Smux//
5156230Smux// This file is distributed under the University of Illinois Open Source
6156230Smux// License. See LICENSE.TXT for details.
7156230Smux//
8156230Smux//===----------------------------------------------------------------------===//
9156230Smux//
10156230Smux// Declarations for metadata specific to debug info.
11156230Smux//
12156230Smux//===----------------------------------------------------------------------===//
13156230Smux
14156230Smux#ifndef LLVM_IR_DEBUGINFOMETADATA_H
15156230Smux#define LLVM_IR_DEBUGINFOMETADATA_H
16156230Smux
17156230Smux#include "llvm/IR/Metadata.h"
18156230Smux#include "llvm/Support/Dwarf.h"
19156230Smux
20156230Smux// Helper macros for defining get() overrides.
21156230Smux#define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
22156230Smux#define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
23156230Smux#define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)                                 \
24156230Smux  static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) {  \
25156230Smux    return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued);          \
26156230Smux  }                                                                            \
27156230Smux  static CLASS *getIfExists(LLVMContext &Context,                              \
28156230Smux                            DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
29156230Smux    return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued,           \
30156230Smux                   /* ShouldCreate */ false);                                  \
31156230Smux  }                                                                            \
32156230Smux  static CLASS *getDistinct(LLVMContext &Context,                              \
33156230Smux                            DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
34156230Smux    return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct);         \
35156230Smux  }                                                                            \
36156230Smux  static Temp##CLASS getTemporary(LLVMContext &Context,                        \
37156230Smux                                  DEFINE_MDNODE_GET_UNPACK(FORMAL)) {          \
38156230Smux    return Temp##CLASS(                                                        \
39156230Smux        getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary));          \
40156230Smux  }
41156230Smux
42156230Smuxnamespace llvm {
43156230Smux
44156230Smux/// \brief Pointer union between a subclass of DINode and MDString.
45156230Smux///
46156230Smux/// \a DICompositeType can be referenced via an \a MDString unique identifier.
47156230Smux/// This class allows some type safety in the face of that, requiring either a
48156230Smux/// node of a particular type or an \a MDString.
49156230Smuxtemplate <class T> class TypedDINodeRef {
50156230Smux  const Metadata *MD = nullptr;
51156230Smux
52156230Smuxpublic:
53156230Smux  TypedDINodeRef() = default;
54156230Smux  TypedDINodeRef(std::nullptr_t) {}
55156230Smux
56156701Smux  /// \brief Construct from a raw pointer.
57156701Smux  explicit TypedDINodeRef(const Metadata *MD) : MD(MD) {
58156230Smux    assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
59156230Smux  }
60156230Smux
61156230Smux  template <class U>
62156230Smux  TypedDINodeRef(
63156230Smux      const TypedDINodeRef<U> &X,
64156230Smux      typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
65156230Smux          nullptr)
66156230Smux      : MD(X) {}
67156230Smux
68156230Smux  operator Metadata *() const { return const_cast<Metadata *>(MD); }
69156230Smux
70156230Smux  bool operator==(const TypedDINodeRef<T> &X) const { return MD == X.MD; };
71156230Smux  bool operator!=(const TypedDINodeRef<T> &X) const { return MD != X.MD; };
72156230Smux
73156230Smux  /// \brief Create a reference.
74156230Smux  ///
75156230Smux  /// Get a reference to \c N, using an \a MDString reference if available.
76156230Smux  static TypedDINodeRef get(const T *N);
77156230Smux
78156230Smux  template <class MapTy> T *resolve(const MapTy &Map) const {
79156230Smux    if (!MD)
80156230Smux      return nullptr;
81156230Smux
82156230Smux    if (auto *Typed = dyn_cast<T>(MD))
83156701Smux      return const_cast<T *>(Typed);
84156701Smux
85156230Smux    auto *S = cast<MDString>(MD);
86156230Smux    auto I = Map.find(S);
87156230Smux    assert(I != Map.end() && "Missing identifier in type map");
88156230Smux    return cast<T>(I->second);
89156230Smux  }
90156701Smux};
91156230Smux
92156230Smuxtypedef TypedDINodeRef<DINode> DINodeRef;
93156230Smuxtypedef TypedDINodeRef<DIScope> DIScopeRef;
94156230Smuxtypedef TypedDINodeRef<DIType> DITypeRef;
95156230Smux
96156230Smuxclass DITypeRefArray {
97156230Smux  const MDTuple *N = nullptr;
98156230Smux
99156230Smuxpublic:
100156230Smux  DITypeRefArray(const MDTuple *N) : N(N) {}
101156230Smux
102156230Smux  explicit operator bool() const { return get(); }
103156230Smux  explicit operator MDTuple *() const { return get(); }
104156230Smux
105156230Smux  MDTuple *get() const { return const_cast<MDTuple *>(N); }
106156230Smux  MDTuple *operator->() const { return get(); }
107156230Smux  MDTuple &operator*() const { return *get(); }
108156230Smux
109156230Smux  // FIXME: Fix callers and remove condition on N.
110156230Smux  unsigned size() const { return N ? N->getNumOperands() : 0u; }
111156230Smux  DITypeRef operator[](unsigned I) const { return DITypeRef(N->getOperand(I)); }
112156230Smux
113156230Smux  class iterator : std::iterator<std::input_iterator_tag, DITypeRef,
114156230Smux                                 std::ptrdiff_t, void, DITypeRef> {
115156230Smux    MDNode::op_iterator I = nullptr;
116156230Smux
117156230Smux  public:
118156230Smux    iterator() = default;
119156230Smux    explicit iterator(MDNode::op_iterator I) : I(I) {}
120156230Smux    DITypeRef operator*() const { return DITypeRef(*I); }
121156230Smux    iterator &operator++() {
122156230Smux      ++I;
123156230Smux      return *this;
124156230Smux    }
125156230Smux    iterator operator++(int) {
126156230Smux      iterator Temp(*this);
127156230Smux      ++I;
128156230Smux      return Temp;
129156230Smux    }
130156230Smux    bool operator==(const iterator &X) const { return I == X.I; }
131156230Smux    bool operator!=(const iterator &X) const { return I != X.I; }
132156230Smux  };
133156230Smux
134156230Smux  // FIXME: Fix callers and remove condition on N.
135156230Smux  iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
136156230Smux  iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
137156230Smux};
138156230Smux
139156230Smux/// \brief Tagged DWARF-like metadata node.
140156230Smux///
141156230Smux/// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
142156230Smux/// defined in llvm/Support/Dwarf.h).  Called \a DINode because it's
143156230Smux/// potentially used for non-DWARF output.
144156230Smuxclass DINode : public MDNode {
145156230Smux  friend class LLVMContextImpl;
146156230Smux  friend class MDNode;
147156230Smux
148156230Smuxprotected:
149156230Smux  DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
150156230Smux         ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
151156230Smux      : MDNode(C, ID, Storage, Ops1, Ops2) {
152156230Smux    assert(Tag < 1u << 16);
153156230Smux    SubclassData16 = Tag;
154156230Smux  }
155156230Smux  ~DINode() = default;
156156230Smux
157156230Smux  template <class Ty> Ty *getOperandAs(unsigned I) const {
158156230Smux    return cast_or_null<Ty>(getOperand(I));
159156230Smux  }
160156230Smux
161156230Smux  StringRef getStringOperand(unsigned I) const {
162156230Smux    if (auto *S = getOperandAs<MDString>(I))
163156230Smux      return S->getString();
164156230Smux    return StringRef();
165156230Smux  }
166156230Smux
167156230Smux  static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
168156230Smux    if (S.empty())
169156230Smux      return nullptr;
170156230Smux    return MDString::get(Context, S);
171156230Smux  }
172156230Smux
173156230Smuxpublic:
174156230Smux  unsigned getTag() const { return SubclassData16; }
175156230Smux
176156230Smux  /// \brief Debug info flags.
177156230Smux  ///
178156230Smux  /// The three accessibility flags are mutually exclusive and rolled together
179156230Smux  /// in the first two bits.
180156230Smux  enum DIFlags {
181156230Smux#define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
182156230Smux#include "llvm/IR/DebugInfoFlags.def"
183156230Smux    FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
184156230Smux  };
185156230Smux
186156230Smux  static unsigned getFlag(StringRef Flag);
187156230Smux  static const char *getFlagString(unsigned Flag);
188156230Smux
189156230Smux  /// \brief Split up a flags bitfield.
190156230Smux  ///
191156230Smux  /// Split \c Flags into \c SplitFlags, a vector of its components.  Returns
192156230Smux  /// any remaining (unrecognized) bits.
193156230Smux  static unsigned splitFlags(unsigned Flags,
194156230Smux                             SmallVectorImpl<unsigned> &SplitFlags);
195156230Smux
196156230Smux  DINodeRef getRef() const { return DINodeRef::get(this); }
197156230Smux
198156230Smux  static bool classof(const Metadata *MD) {
199156230Smux    switch (MD->getMetadataID()) {
200156230Smux    default:
201156230Smux      return false;
202156230Smux    case GenericDINodeKind:
203156230Smux    case DISubrangeKind:
204156230Smux    case DIEnumeratorKind:
205156230Smux    case DIBasicTypeKind:
206156230Smux    case DIDerivedTypeKind:
207156230Smux    case DICompositeTypeKind:
208156230Smux    case DISubroutineTypeKind:
209156230Smux    case DIFileKind:
210156230Smux    case DICompileUnitKind:
211156230Smux    case DISubprogramKind:
212156230Smux    case DILexicalBlockKind:
213156230Smux    case DILexicalBlockFileKind:
214156230Smux    case DINamespaceKind:
215156230Smux    case DITemplateTypeParameterKind:
216156230Smux    case DITemplateValueParameterKind:
217156230Smux    case DIGlobalVariableKind:
218156230Smux    case DILocalVariableKind:
219156230Smux    case DIObjCPropertyKind:
220156230Smux    case DIImportedEntityKind:
221156230Smux      return true;
222156230Smux    }
223156230Smux  }
224156230Smux};
225156230Smux
226156230Smuxtemplate <class T> struct simplify_type<const TypedDINodeRef<T>> {
227156230Smux  typedef Metadata *SimpleType;
228156230Smux  static SimpleType getSimplifiedValue(const TypedDINodeRef<T> &MD) {
229156230Smux    return MD;
230156230Smux  }
231156230Smux};
232156230Smux
233156701Smuxtemplate <class T>
234156230Smuxstruct simplify_type<TypedDINodeRef<T>>
235156230Smux    : simplify_type<const TypedDINodeRef<T>> {};
236156230Smux
237156230Smux/// \brief Generic tagged DWARF-like metadata node.
238156701Smux///
239156701Smux/// An un-specialized DWARF-like metadata node.  The first operand is a
240156701Smux/// (possibly empty) null-separated \a MDString header that contains arbitrary
241156701Smux/// fields.  The remaining operands are \a dwarf_operands(), and are pointers
242156701Smux/// to other metadata.
243156701Smuxclass GenericDINode : public DINode {
244156701Smux  friend class LLVMContextImpl;
245156701Smux  friend class MDNode;
246156701Smux
247156701Smux  GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
248156701Smux                unsigned Tag, ArrayRef<Metadata *> Ops1,
249156701Smux                ArrayRef<Metadata *> Ops2)
250156701Smux      : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
251156701Smux    setHash(Hash);
252156701Smux  }
253156701Smux  ~GenericDINode() { dropAllReferences(); }
254156230Smux
255156230Smux  void setHash(unsigned Hash) { SubclassData32 = Hash; }
256156230Smux  void recalculateHash();
257156230Smux
258156230Smux  static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
259156230Smux                                StringRef Header, ArrayRef<Metadata *> DwarfOps,
260156230Smux                                StorageType Storage, bool ShouldCreate = true) {
261156230Smux    return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
262156230Smux                   DwarfOps, Storage, ShouldCreate);
263156230Smux  }
264156230Smux
265156230Smux  static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
266156230Smux                                MDString *Header, ArrayRef<Metadata *> DwarfOps,
267156230Smux                                StorageType Storage, bool ShouldCreate = true);
268156230Smux
269156230Smux  TempGenericDINode cloneImpl() const {
270156230Smux    return getTemporary(
271156230Smux        getContext(), getTag(), getHeader(),
272156230Smux        SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
273156230Smux  }
274156230Smux
275156230Smuxpublic:
276156230Smux  unsigned getHash() const { return SubclassData32; }
277156230Smux
278156230Smux  DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, StringRef Header,
279156230Smux                                    ArrayRef<Metadata *> DwarfOps),
280156230Smux                    (Tag, Header, DwarfOps))
281156230Smux  DEFINE_MDNODE_GET(GenericDINode, (unsigned Tag, MDString *Header,
282156230Smux                                    ArrayRef<Metadata *> DwarfOps),
283156230Smux                    (Tag, Header, DwarfOps))
284156230Smux
285156230Smux  /// \brief Return a (temporary) clone of this.
286156230Smux  TempGenericDINode clone() const { return cloneImpl(); }
287156230Smux
288156230Smux  unsigned getTag() const { return SubclassData16; }
289156230Smux  StringRef getHeader() const { return getStringOperand(0); }
290156230Smux
291156230Smux  op_iterator dwarf_op_begin() const { return op_begin() + 1; }
292156230Smux  op_iterator dwarf_op_end() const { return op_end(); }
293156230Smux  op_range dwarf_operands() const {
294156230Smux    return op_range(dwarf_op_begin(), dwarf_op_end());
295156230Smux  }
296156230Smux
297156230Smux  unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
298156230Smux  const MDOperand &getDwarfOperand(unsigned I) const {
299156230Smux    return getOperand(I + 1);
300156230Smux  }
301156230Smux  void replaceDwarfOperandWith(unsigned I, Metadata *New) {
302156230Smux    replaceOperandWith(I + 1, New);
303156230Smux  }
304156230Smux
305156230Smux  static bool classof(const Metadata *MD) {
306156230Smux    return MD->getMetadataID() == GenericDINodeKind;
307156230Smux  }
308156230Smux};
309156230Smux
310156230Smux/// \brief Array subrange.
311156230Smux///
312156230Smux/// TODO: Merge into node for DW_TAG_array_type, which should have a custom
313156230Smux/// type.
314156230Smuxclass DISubrange : public DINode {
315156230Smux  friend class LLVMContextImpl;
316156230Smux  friend class MDNode;
317156230Smux
318156230Smux  int64_t Count;
319156230Smux  int64_t LowerBound;
320156230Smux
321156230Smux  DISubrange(LLVMContext &C, StorageType Storage, int64_t Count,
322156230Smux             int64_t LowerBound)
323156230Smux      : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, None),
324156230Smux        Count(Count), LowerBound(LowerBound) {}
325156230Smux  ~DISubrange() = default;
326156230Smux
327156230Smux  static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
328156230Smux                             int64_t LowerBound, StorageType Storage,
329156230Smux                             bool ShouldCreate = true);
330156230Smux
331156230Smux  TempDISubrange cloneImpl() const {
332156230Smux    return getTemporary(getContext(), getCount(), getLowerBound());
333156230Smux  }
334156230Smux
335156230Smuxpublic:
336156230Smux  DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
337156230Smux                    (Count, LowerBound))
338156230Smux
339156230Smux  TempDISubrange clone() const { return cloneImpl(); }
340156230Smux
341156230Smux  int64_t getLowerBound() const { return LowerBound; }
342156230Smux  int64_t getCount() const { return Count; }
343156230Smux
344156230Smux  static bool classof(const Metadata *MD) {
345156230Smux    return MD->getMetadataID() == DISubrangeKind;
346156230Smux  }
347156230Smux};
348156230Smux
349156230Smux/// \brief Enumeration value.
350156230Smux///
351156230Smux/// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
352156230Smux/// longer creates a type cycle.
353156230Smuxclass DIEnumerator : public DINode {
354156230Smux  friend class LLVMContextImpl;
355156230Smux  friend class MDNode;
356156230Smux
357156230Smux  int64_t Value;
358156230Smux
359156230Smux  DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
360156230Smux               ArrayRef<Metadata *> Ops)
361156230Smux      : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
362156230Smux        Value(Value) {}
363156230Smux  ~DIEnumerator() = default;
364156230Smux
365156230Smux  static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
366156230Smux                               StringRef Name, StorageType Storage,
367156230Smux                               bool ShouldCreate = true) {
368156230Smux    return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
369156230Smux                   ShouldCreate);
370156230Smux  }
371156230Smux  static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value,
372156230Smux                               MDString *Name, StorageType Storage,
373156230Smux                               bool ShouldCreate = true);
374156230Smux
375156230Smux  TempDIEnumerator cloneImpl() const {
376156230Smux    return getTemporary(getContext(), getValue(), getName());
377156230Smux  }
378156230Smux
379156230Smuxpublic:
380156230Smux  DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, StringRef Name),
381156230Smux                    (Value, Name))
382156230Smux  DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, MDString *Name),
383156230Smux                    (Value, Name))
384156230Smux
385156230Smux  TempDIEnumerator clone() const { return cloneImpl(); }
386156230Smux
387156230Smux  int64_t getValue() const { return Value; }
388156230Smux  StringRef getName() const { return getStringOperand(0); }
389156230Smux
390156230Smux  MDString *getRawName() const { return getOperandAs<MDString>(0); }
391156230Smux
392156230Smux  static bool classof(const Metadata *MD) {
393156230Smux    return MD->getMetadataID() == DIEnumeratorKind;
394156230Smux  }
395156230Smux};
396156230Smux
397156230Smux/// \brief Base class for scope-like contexts.
398156230Smux///
399156230Smux/// Base class for lexical scopes and types (which are also declaration
400156230Smux/// contexts).
401156230Smux///
402156230Smux/// TODO: Separate the concepts of declaration contexts and lexical scopes.
403156230Smuxclass DIScope : public DINode {
404156230Smuxprotected:
405156230Smux  DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
406156230Smux          ArrayRef<Metadata *> Ops)
407156230Smux      : DINode(C, ID, Storage, Tag, Ops) {}
408156230Smux  ~DIScope() = default;
409156230Smux
410156230Smuxpublic:
411156230Smux  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
412156230Smux
413156230Smux  inline StringRef getFilename() const;
414156230Smux  inline StringRef getDirectory() const;
415156230Smux
416156230Smux  StringRef getName() const;
417156230Smux  DIScopeRef getScope() const;
418156230Smux
419156230Smux  /// \brief Return the raw underlying file.
420156230Smux  ///
421156230Smux  /// An \a DIFile is an \a DIScope, but it doesn't point at a separate file
422156230Smux  /// (it\em is the file).  If \c this is an \a DIFile, we need to return \c
423156230Smux  /// this.  Otherwise, return the first operand, which is where all other
424156230Smux  /// subclasses store their file pointer.
425156230Smux  Metadata *getRawFile() const {
426156230Smux    return isa<DIFile>(this) ? const_cast<DIScope *>(this)
427156230Smux                             : static_cast<Metadata *>(getOperand(0));
428156230Smux  }
429156230Smux
430156230Smux  DIScopeRef getRef() const { return DIScopeRef::get(this); }
431156230Smux
432156230Smux  static bool classof(const Metadata *MD) {
433156230Smux    switch (MD->getMetadataID()) {
434156230Smux    default:
435156701Smux      return false;
436156230Smux    case DIBasicTypeKind:
437156230Smux    case DIDerivedTypeKind:
438156230Smux    case DICompositeTypeKind:
439156230Smux    case DISubroutineTypeKind:
440156230Smux    case DIFileKind:
441156230Smux    case DICompileUnitKind:
442156230Smux    case DISubprogramKind:
443156230Smux    case DILexicalBlockKind:
444156230Smux    case DILexicalBlockFileKind:
445156230Smux    case DINamespaceKind:
446156230Smux      return true;
447156230Smux    }
448156230Smux  }
449156230Smux};
450156230Smux
451156230Smux/// \brief File.
452156230Smux///
453156230Smux/// TODO: Merge with directory/file node (including users).
454156230Smux/// TODO: Canonicalize paths on creation.
455156230Smuxclass DIFile : public DIScope {
456156230Smux  friend class LLVMContextImpl;
457156230Smux  friend class MDNode;
458156230Smux
459156230Smux  DIFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
460156230Smux      : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
461156230Smux  ~DIFile() = default;
462156230Smux
463156230Smux  static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
464156230Smux                         StringRef Directory, StorageType Storage,
465156230Smux                         bool ShouldCreate = true) {
466156230Smux    return getImpl(Context, getCanonicalMDString(Context, Filename),
467156230Smux                   getCanonicalMDString(Context, Directory), Storage,
468156230Smux                   ShouldCreate);
469156230Smux  }
470156230Smux  static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
471156230Smux                         MDString *Directory, StorageType Storage,
472156230Smux                         bool ShouldCreate = true);
473156230Smux
474156230Smux  TempDIFile cloneImpl() const {
475156230Smux    return getTemporary(getContext(), getFilename(), getDirectory());
476156230Smux  }
477156230Smux
478156230Smuxpublic:
479156230Smux  DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory),
480156230Smux                    (Filename, Directory))
481156230Smux  DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory),
482156230Smux                    (Filename, Directory))
483156230Smux
484156230Smux  TempDIFile clone() const { return cloneImpl(); }
485156230Smux
486156230Smux  StringRef getFilename() const { return getStringOperand(0); }
487156701Smux  StringRef getDirectory() const { return getStringOperand(1); }
488156701Smux
489156230Smux  MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
490156230Smux  MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
491156230Smux
492156230Smux  static bool classof(const Metadata *MD) {
493156230Smux    return MD->getMetadataID() == DIFileKind;
494156230Smux  }
495156230Smux};
496156230Smux
497156230SmuxStringRef DIScope::getFilename() const {
498156230Smux  if (auto *F = getFile())
499156230Smux    return F->getFilename();
500156701Smux  return "";
501156230Smux}
502156230Smux
503156230SmuxStringRef DIScope::getDirectory() const {
504156230Smux  if (auto *F = getFile())
505156230Smux    return F->getDirectory();
506156230Smux  return "";
507156230Smux}
508156230Smux
509156230Smux/// \brief Base class for types.
510156230Smux///
511156230Smux/// TODO: Remove the hardcoded name and context, since many types don't use
512156230Smux/// them.
513156230Smux/// TODO: Split up flags.
514156230Smuxclass DIType : public DIScope {
515156230Smux  unsigned Line;
516156230Smux  unsigned Flags;
517156230Smux  uint64_t SizeInBits;
518156230Smux  uint64_t AlignInBits;
519156230Smux  uint64_t OffsetInBits;
520156230Smux
521156230Smuxprotected:
522156230Smux  DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
523156230Smux         unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
524156230Smux         uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
525156230Smux      : DIScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
526156230Smux        SizeInBits(SizeInBits), AlignInBits(AlignInBits),
527156230Smux        OffsetInBits(OffsetInBits) {}
528156230Smux  ~DIType() = default;
529156230Smux
530156230Smuxpublic:
531156230Smux  TempDIType clone() const {
532156230Smux    return TempDIType(cast<DIType>(MDNode::clone().release()));
533156230Smux  }
534156230Smux
535156230Smux  unsigned getLine() const { return Line; }
536156230Smux  uint64_t getSizeInBits() const { return SizeInBits; }
537156230Smux  uint64_t getAlignInBits() const { return AlignInBits; }
538156230Smux  uint64_t getOffsetInBits() const { return OffsetInBits; }
539156230Smux  unsigned getFlags() const { return Flags; }
540156230Smux
541156230Smux  DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
542156230Smux  StringRef getName() const { return getStringOperand(2); }
543156230Smux
544156230Smux
545156230Smux  Metadata *getRawScope() const { return getOperand(1); }
546156230Smux  MDString *getRawName() const { return getOperandAs<MDString>(2); }
547156701Smux
548156230Smux  void setFlags(unsigned NewFlags) {
549156701Smux    assert(!isUniqued() && "Cannot set flags on uniqued nodes");
550156230Smux    Flags = NewFlags;
551156230Smux  }
552156230Smux
553156230Smux  bool isPrivate() const {
554156701Smux    return (getFlags() & FlagAccessibility) == FlagPrivate;
555156230Smux  }
556156230Smux  bool isProtected() const {
557156230Smux    return (getFlags() & FlagAccessibility) == FlagProtected;
558156230Smux  }
559156230Smux  bool isPublic() const {
560156230Smux    return (getFlags() & FlagAccessibility) == FlagPublic;
561156230Smux  }
562156230Smux  bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
563156230Smux  bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
564156230Smux  bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
565156701Smux  bool isVirtual() const { return getFlags() & FlagVirtual; }
566156701Smux  bool isArtificial() const { return getFlags() & FlagArtificial; }
567156701Smux  bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
568156230Smux  bool isObjcClassComplete() const {
569156230Smux    return getFlags() & FlagObjcClassComplete;
570156230Smux  }
571156230Smux  bool isVector() const { return getFlags() & FlagVector; }
572156230Smux  bool isStaticMember() const { return getFlags() & FlagStaticMember; }
573156230Smux  bool isLValueReference() const { return getFlags() & FlagLValueReference; }
574156230Smux  bool isRValueReference() const { return getFlags() & FlagRValueReference; }
575156230Smux
576156230Smux  DITypeRef getRef() const { return DITypeRef::get(this); }
577156230Smux
578156230Smux  static bool classof(const Metadata *MD) {
579156230Smux    switch (MD->getMetadataID()) {
580    default:
581      return false;
582    case DIBasicTypeKind:
583    case DIDerivedTypeKind:
584    case DICompositeTypeKind:
585    case DISubroutineTypeKind:
586      return true;
587    }
588  }
589};
590
591/// \brief Basic type, like 'int' or 'float'.
592///
593/// TODO: Split out DW_TAG_unspecified_type.
594/// TODO: Drop unused accessors.
595class DIBasicType : public DIType {
596  friend class LLVMContextImpl;
597  friend class MDNode;
598
599  unsigned Encoding;
600
601  DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
602              uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
603              ArrayRef<Metadata *> Ops)
604      : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
605               0, Ops),
606        Encoding(Encoding) {}
607  ~DIBasicType() = default;
608
609  static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
610                              StringRef Name, uint64_t SizeInBits,
611                              uint64_t AlignInBits, unsigned Encoding,
612                              StorageType Storage, bool ShouldCreate = true) {
613    return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
614                   SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
615  }
616  static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
617                              MDString *Name, uint64_t SizeInBits,
618                              uint64_t AlignInBits, unsigned Encoding,
619                              StorageType Storage, bool ShouldCreate = true);
620
621  TempDIBasicType cloneImpl() const {
622    return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
623                        getAlignInBits(), getEncoding());
624  }
625
626public:
627  DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
628                    (Tag, Name, 0, 0, 0))
629  DEFINE_MDNODE_GET(DIBasicType,
630                    (unsigned Tag, StringRef Name, uint64_t SizeInBits,
631                     uint64_t AlignInBits, unsigned Encoding),
632                    (Tag, Name, SizeInBits, AlignInBits, Encoding))
633  DEFINE_MDNODE_GET(DIBasicType,
634                    (unsigned Tag, MDString *Name, uint64_t SizeInBits,
635                     uint64_t AlignInBits, unsigned Encoding),
636                    (Tag, Name, SizeInBits, AlignInBits, Encoding))
637
638  TempDIBasicType clone() const { return cloneImpl(); }
639
640  unsigned getEncoding() const { return Encoding; }
641
642  static bool classof(const Metadata *MD) {
643    return MD->getMetadataID() == DIBasicTypeKind;
644  }
645};
646
647/// \brief Base class for DIDerivedType and DICompositeType.
648///
649/// TODO: Delete; they're not really related.
650class DIDerivedTypeBase : public DIType {
651protected:
652  DIDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
653                    unsigned Tag, unsigned Line, uint64_t SizeInBits,
654                    uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
655                    ArrayRef<Metadata *> Ops)
656      : DIType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
657               Flags, Ops) {}
658  ~DIDerivedTypeBase() = default;
659
660public:
661  DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
662  Metadata *getRawBaseType() const { return getOperand(3); }
663
664  static bool classof(const Metadata *MD) {
665    return MD->getMetadataID() == DIDerivedTypeKind ||
666           MD->getMetadataID() == DICompositeTypeKind ||
667           MD->getMetadataID() == DISubroutineTypeKind;
668  }
669};
670
671/// \brief Derived types.
672///
673/// This includes qualified types, pointers, references, friends, typedefs, and
674/// class members.
675///
676/// TODO: Split out members (inheritance, fields, methods, etc.).
677class DIDerivedType : public DIDerivedTypeBase {
678  friend class LLVMContextImpl;
679  friend class MDNode;
680
681  DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
682                unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
683                uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
684      : DIDerivedTypeBase(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
685                          AlignInBits, OffsetInBits, Flags, Ops) {}
686  ~DIDerivedType() = default;
687
688  static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
689                                StringRef Name, DIFile *File, unsigned Line,
690                                DIScopeRef Scope, DITypeRef BaseType,
691                                uint64_t SizeInBits, uint64_t AlignInBits,
692                                uint64_t OffsetInBits, unsigned Flags,
693                                Metadata *ExtraData, StorageType Storage,
694                                bool ShouldCreate = true) {
695    return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
696                   Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
697                   Flags, ExtraData, Storage, ShouldCreate);
698  }
699  static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
700                                MDString *Name, Metadata *File, unsigned Line,
701                                Metadata *Scope, Metadata *BaseType,
702                                uint64_t SizeInBits, uint64_t AlignInBits,
703                                uint64_t OffsetInBits, unsigned Flags,
704                                Metadata *ExtraData, StorageType Storage,
705                                bool ShouldCreate = true);
706
707  TempDIDerivedType cloneImpl() const {
708    return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
709                        getScope(), getBaseType(), getSizeInBits(),
710                        getAlignInBits(), getOffsetInBits(), getFlags(),
711                        getExtraData());
712  }
713
714public:
715  DEFINE_MDNODE_GET(DIDerivedType,
716                    (unsigned Tag, MDString *Name, Metadata *File,
717                     unsigned Line, Metadata *Scope, Metadata *BaseType,
718                     uint64_t SizeInBits, uint64_t AlignInBits,
719                     uint64_t OffsetInBits, unsigned Flags,
720                     Metadata *ExtraData = nullptr),
721                    (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
722                     AlignInBits, OffsetInBits, Flags, ExtraData))
723  DEFINE_MDNODE_GET(DIDerivedType,
724                    (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
725                     DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
726                     uint64_t AlignInBits, uint64_t OffsetInBits,
727                     unsigned Flags, Metadata *ExtraData = nullptr),
728                    (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
729                     AlignInBits, OffsetInBits, Flags, ExtraData))
730
731  TempDIDerivedType clone() const { return cloneImpl(); }
732
733  /// \brief Get extra data associated with this derived type.
734  ///
735  /// Class type for pointer-to-members, objective-c property node for ivars,
736  /// or global constant wrapper for static members.
737  ///
738  /// TODO: Separate out types that need this extra operand: pointer-to-member
739  /// types and member fields (static members and ivars).
740  Metadata *getExtraData() const { return getRawExtraData(); }
741  Metadata *getRawExtraData() const { return getOperand(4); }
742
743  /// \brief Get casted version of extra data.
744  /// @{
745  DITypeRef getClassType() const {
746    assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
747    return DITypeRef(getExtraData());
748  }
749  DIObjCProperty *getObjCProperty() const {
750    return dyn_cast_or_null<DIObjCProperty>(getExtraData());
751  }
752  Constant *getConstant() const {
753    assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
754    if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
755      return C->getValue();
756    return nullptr;
757  }
758  /// @}
759
760  static bool classof(const Metadata *MD) {
761    return MD->getMetadataID() == DIDerivedTypeKind;
762  }
763};
764
765/// \brief Base class for DICompositeType and DISubroutineType.
766///
767/// TODO: Delete; they're not really related.
768class DICompositeTypeBase : public DIDerivedTypeBase {
769  unsigned RuntimeLang;
770
771protected:
772  DICompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
773                      unsigned Tag, unsigned Line, unsigned RuntimeLang,
774                      uint64_t SizeInBits, uint64_t AlignInBits,
775                      uint64_t OffsetInBits, unsigned Flags,
776                      ArrayRef<Metadata *> Ops)
777      : DIDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
778                          OffsetInBits, Flags, Ops),
779        RuntimeLang(RuntimeLang) {}
780  ~DICompositeTypeBase() = default;
781
782public:
783  /// \brief Get the elements of the composite type.
784  ///
785  /// \note Calling this is only valid for \a DICompositeType.  This assertion
786  /// can be removed once \a DISubroutineType has been separated from
787  /// "composite types".
788  DINodeArray getElements() const {
789    assert(!isa<DISubroutineType>(this) && "no elements for DISubroutineType");
790    return cast_or_null<MDTuple>(getRawElements());
791  }
792  DITypeRef getVTableHolder() const { return DITypeRef(getRawVTableHolder()); }
793  DITemplateParameterArray getTemplateParams() const {
794    return cast_or_null<MDTuple>(getRawTemplateParams());
795  }
796  StringRef getIdentifier() const { return getStringOperand(7); }
797  unsigned getRuntimeLang() const { return RuntimeLang; }
798
799  Metadata *getRawElements() const { return getOperand(4); }
800  Metadata *getRawVTableHolder() const { return getOperand(5); }
801  Metadata *getRawTemplateParams() const { return getOperand(6); }
802  MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
803
804  /// \brief Replace operands.
805  ///
806  /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
807  /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
808  /// of its movement if necessary.
809  /// @{
810  void replaceElements(DINodeArray Elements) {
811#ifndef NDEBUG
812    for (DINode *Op : getElements())
813      assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
814             "Lost a member during member list replacement");
815#endif
816    replaceOperandWith(4, Elements.get());
817  }
818  void replaceVTableHolder(DITypeRef VTableHolder) {
819    replaceOperandWith(5, VTableHolder);
820  }
821  void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
822    replaceOperandWith(6, TemplateParams.get());
823  }
824  /// @}
825
826  static bool classof(const Metadata *MD) {
827    return MD->getMetadataID() == DICompositeTypeKind ||
828           MD->getMetadataID() == DISubroutineTypeKind;
829  }
830};
831
832/// \brief Composite types.
833///
834/// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
835/// TODO: Create a custom, unrelated node for DW_TAG_array_type.
836class DICompositeType : public DICompositeTypeBase {
837  friend class LLVMContextImpl;
838  friend class MDNode;
839
840  DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
841                  unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
842                  uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
843                  ArrayRef<Metadata *> Ops)
844      : DICompositeTypeBase(C, DICompositeTypeKind, Storage, Tag, Line,
845                            RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
846                            Flags, Ops) {}
847  ~DICompositeType() = default;
848
849  static DICompositeType *
850  getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
851          unsigned Line, DIScopeRef Scope, DITypeRef BaseType,
852          uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
853          uint64_t Flags, DINodeArray Elements, unsigned RuntimeLang,
854          DITypeRef VTableHolder, DITemplateParameterArray TemplateParams,
855          StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
856    return getImpl(
857        Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
858        BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
859        RuntimeLang, VTableHolder, TemplateParams.get(),
860        getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
861  }
862  static DICompositeType *
863  getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
864          unsigned Line, Metadata *Scope, Metadata *BaseType,
865          uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
866          unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
867          Metadata *VTableHolder, Metadata *TemplateParams,
868          MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
869
870  TempDICompositeType cloneImpl() const {
871    return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
872                        getScope(), getBaseType(), getSizeInBits(),
873                        getAlignInBits(), getOffsetInBits(), getFlags(),
874                        getElements(), getRuntimeLang(), getVTableHolder(),
875                        getTemplateParams(), getIdentifier());
876  }
877
878public:
879  DEFINE_MDNODE_GET(DICompositeType,
880                    (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
881                     DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
882                     uint64_t AlignInBits, uint64_t OffsetInBits,
883                     unsigned Flags, DINodeArray Elements, unsigned RuntimeLang,
884                     DITypeRef VTableHolder,
885                     DITemplateParameterArray TemplateParams = nullptr,
886                     StringRef Identifier = ""),
887                    (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
888                     AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
889                     VTableHolder, TemplateParams, Identifier))
890  DEFINE_MDNODE_GET(DICompositeType,
891                    (unsigned Tag, MDString *Name, Metadata *File,
892                     unsigned Line, Metadata *Scope, Metadata *BaseType,
893                     uint64_t SizeInBits, uint64_t AlignInBits,
894                     uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
895                     unsigned RuntimeLang, Metadata *VTableHolder,
896                     Metadata *TemplateParams = nullptr,
897                     MDString *Identifier = nullptr),
898                    (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
899                     AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
900                     VTableHolder, TemplateParams, Identifier))
901
902  TempDICompositeType clone() const { return cloneImpl(); }
903
904  static bool classof(const Metadata *MD) {
905    return MD->getMetadataID() == DICompositeTypeKind;
906  }
907};
908
909template <class T> TypedDINodeRef<T> TypedDINodeRef<T>::get(const T *N) {
910  if (N)
911    if (auto *Composite = dyn_cast<DICompositeType>(N))
912      if (auto *S = Composite->getRawIdentifier())
913        return TypedDINodeRef<T>(S);
914  return TypedDINodeRef<T>(N);
915}
916
917/// \brief Type array for a subprogram.
918///
919/// TODO: Detach from CompositeType, and fold the array of types in directly
920/// as operands.
921class DISubroutineType : public DICompositeTypeBase {
922  friend class LLVMContextImpl;
923  friend class MDNode;
924
925  DISubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
926                   ArrayRef<Metadata *> Ops)
927      : DICompositeTypeBase(C, DISubroutineTypeKind, Storage,
928                            dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
929                            Ops) {}
930  ~DISubroutineType() = default;
931
932  static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
933                                   DITypeRefArray TypeArray,
934                                   StorageType Storage,
935                                   bool ShouldCreate = true) {
936    return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
937  }
938  static DISubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
939                                   Metadata *TypeArray, StorageType Storage,
940                                   bool ShouldCreate = true);
941
942  TempDISubroutineType cloneImpl() const {
943    return getTemporary(getContext(), getFlags(), getTypeArray());
944  }
945
946public:
947  DEFINE_MDNODE_GET(DISubroutineType,
948                    (unsigned Flags, DITypeRefArray TypeArray),
949                    (Flags, TypeArray))
950  DEFINE_MDNODE_GET(DISubroutineType, (unsigned Flags, Metadata *TypeArray),
951                    (Flags, TypeArray))
952
953  TempDISubroutineType clone() const { return cloneImpl(); }
954
955  DITypeRefArray getTypeArray() const {
956    return cast_or_null<MDTuple>(getRawTypeArray());
957  }
958  Metadata *getRawTypeArray() const { return getRawElements(); }
959
960  static bool classof(const Metadata *MD) {
961    return MD->getMetadataID() == DISubroutineTypeKind;
962  }
963};
964
965/// \brief Compile unit.
966class DICompileUnit : public DIScope {
967  friend class LLVMContextImpl;
968  friend class MDNode;
969
970  unsigned SourceLanguage;
971  bool IsOptimized;
972  unsigned RuntimeVersion;
973  unsigned EmissionKind;
974  uint64_t DWOId;
975
976  DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
977                bool IsOptimized, unsigned RuntimeVersion,
978                unsigned EmissionKind, uint64_t DWOId, ArrayRef<Metadata *> Ops)
979      : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
980        SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
981        RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind),
982        DWOId(DWOId) {}
983  ~DICompileUnit() = default;
984
985  static DICompileUnit *
986  getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
987          StringRef Producer, bool IsOptimized, StringRef Flags,
988          unsigned RuntimeVersion, StringRef SplitDebugFilename,
989          unsigned EmissionKind, DICompositeTypeArray EnumTypes,
990          DITypeArray RetainedTypes, DISubprogramArray Subprograms,
991          DIGlobalVariableArray GlobalVariables,
992          DIImportedEntityArray ImportedEntities, uint64_t DWOId,
993          StorageType Storage, bool ShouldCreate = true) {
994    return getImpl(Context, SourceLanguage, File,
995                   getCanonicalMDString(Context, Producer), IsOptimized,
996                   getCanonicalMDString(Context, Flags), RuntimeVersion,
997                   getCanonicalMDString(Context, SplitDebugFilename),
998                   EmissionKind, EnumTypes.get(), RetainedTypes.get(),
999                   Subprograms.get(), GlobalVariables.get(),
1000                   ImportedEntities.get(), DWOId, Storage, ShouldCreate);
1001  }
1002  static DICompileUnit *
1003  getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
1004          MDString *Producer, bool IsOptimized, MDString *Flags,
1005          unsigned RuntimeVersion, MDString *SplitDebugFilename,
1006          unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
1007          Metadata *Subprograms, Metadata *GlobalVariables,
1008          Metadata *ImportedEntities, uint64_t DWOId, StorageType Storage,
1009          bool ShouldCreate = true);
1010
1011  TempDICompileUnit cloneImpl() const {
1012    return getTemporary(
1013        getContext(), getSourceLanguage(), getFile(), getProducer(),
1014        isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
1015        getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
1016        getGlobalVariables(), getImportedEntities(), DWOId);
1017  }
1018
1019public:
1020  DEFINE_MDNODE_GET(DICompileUnit,
1021                    (unsigned SourceLanguage, DIFile *File, StringRef Producer,
1022                     bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
1023                     StringRef SplitDebugFilename, unsigned EmissionKind,
1024                     DICompositeTypeArray EnumTypes, DITypeArray RetainedTypes,
1025                     DISubprogramArray Subprograms,
1026                     DIGlobalVariableArray GlobalVariables,
1027                     DIImportedEntityArray ImportedEntities, uint64_t DWOId),
1028                    (SourceLanguage, File, Producer, IsOptimized, Flags,
1029                     RuntimeVersion, SplitDebugFilename, EmissionKind,
1030                     EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
1031                     ImportedEntities, DWOId))
1032  DEFINE_MDNODE_GET(
1033      DICompileUnit,
1034      (unsigned SourceLanguage, Metadata *File, MDString *Producer,
1035       bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
1036       MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
1037       Metadata *RetainedTypes, Metadata *Subprograms,
1038       Metadata *GlobalVariables, Metadata *ImportedEntities, uint64_t DWOId),
1039      (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
1040       SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, Subprograms,
1041       GlobalVariables, ImportedEntities, DWOId))
1042
1043  TempDICompileUnit clone() const { return cloneImpl(); }
1044
1045  unsigned getSourceLanguage() const { return SourceLanguage; }
1046  bool isOptimized() const { return IsOptimized; }
1047  unsigned getRuntimeVersion() const { return RuntimeVersion; }
1048  unsigned getEmissionKind() const { return EmissionKind; }
1049  StringRef getProducer() const { return getStringOperand(1); }
1050  StringRef getFlags() const { return getStringOperand(2); }
1051  StringRef getSplitDebugFilename() const { return getStringOperand(3); }
1052  DICompositeTypeArray getEnumTypes() const {
1053    return cast_or_null<MDTuple>(getRawEnumTypes());
1054  }
1055  DITypeArray getRetainedTypes() const {
1056    return cast_or_null<MDTuple>(getRawRetainedTypes());
1057  }
1058  DISubprogramArray getSubprograms() const {
1059    return cast_or_null<MDTuple>(getRawSubprograms());
1060  }
1061  DIGlobalVariableArray getGlobalVariables() const {
1062    return cast_or_null<MDTuple>(getRawGlobalVariables());
1063  }
1064  DIImportedEntityArray getImportedEntities() const {
1065    return cast_or_null<MDTuple>(getRawImportedEntities());
1066  }
1067  unsigned getDWOId() const { return DWOId; }
1068
1069  MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
1070  MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
1071  MDString *getRawSplitDebugFilename() const {
1072    return getOperandAs<MDString>(3);
1073  }
1074  Metadata *getRawEnumTypes() const { return getOperand(4); }
1075  Metadata *getRawRetainedTypes() const { return getOperand(5); }
1076  Metadata *getRawSubprograms() const { return getOperand(6); }
1077  Metadata *getRawGlobalVariables() const { return getOperand(7); }
1078  Metadata *getRawImportedEntities() const { return getOperand(8); }
1079
1080  /// \brief Replace arrays.
1081  ///
1082  /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
1083  /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
1084  /// DICompileUnit should be fairly rare.
1085  /// @{
1086  void replaceSubprograms(DISubprogramArray N) {
1087    replaceOperandWith(6, N.get());
1088  }
1089  void replaceGlobalVariables(DIGlobalVariableArray N) {
1090    replaceOperandWith(7, N.get());
1091  }
1092  /// @}
1093
1094  static bool classof(const Metadata *MD) {
1095    return MD->getMetadataID() == DICompileUnitKind;
1096  }
1097};
1098
1099/// \brief A scope for locals.
1100///
1101/// A legal scope for lexical blocks, local variables, and debug info
1102/// locations.  Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
1103/// DILexicalBlockFile.
1104class DILocalScope : public DIScope {
1105protected:
1106  DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1107               ArrayRef<Metadata *> Ops)
1108      : DIScope(C, ID, Storage, Tag, Ops) {}
1109  ~DILocalScope() = default;
1110
1111public:
1112  /// \brief Get the subprogram for this scope.
1113  ///
1114  /// Return this if it's an \a DISubprogram; otherwise, look up the scope
1115  /// chain.
1116  DISubprogram *getSubprogram() const;
1117
1118  static bool classof(const Metadata *MD) {
1119    return MD->getMetadataID() == DISubprogramKind ||
1120           MD->getMetadataID() == DILexicalBlockKind ||
1121           MD->getMetadataID() == DILexicalBlockFileKind;
1122  }
1123};
1124
1125/// \brief Debug location.
1126///
1127/// A debug location in source code, used for debug info and otherwise.
1128class DILocation : public MDNode {
1129  friend class LLVMContextImpl;
1130  friend class MDNode;
1131
1132  DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
1133             unsigned Column, ArrayRef<Metadata *> MDs);
1134  ~DILocation() { dropAllReferences(); }
1135
1136  static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1137                             unsigned Column, Metadata *Scope,
1138                             Metadata *InlinedAt, StorageType Storage,
1139                             bool ShouldCreate = true);
1140  static DILocation *getImpl(LLVMContext &Context, unsigned Line,
1141                             unsigned Column, DILocalScope *Scope,
1142                             DILocation *InlinedAt, StorageType Storage,
1143                             bool ShouldCreate = true) {
1144    return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
1145                   static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
1146  }
1147
1148  TempDILocation cloneImpl() const {
1149    return getTemporary(getContext(), getLine(), getColumn(), getScope(),
1150                        getInlinedAt());
1151  }
1152
1153  // Disallow replacing operands.
1154  void replaceOperandWith(unsigned I, Metadata *New) = delete;
1155
1156public:
1157  DEFINE_MDNODE_GET(DILocation,
1158                    (unsigned Line, unsigned Column, Metadata *Scope,
1159                     Metadata *InlinedAt = nullptr),
1160                    (Line, Column, Scope, InlinedAt))
1161  DEFINE_MDNODE_GET(DILocation,
1162                    (unsigned Line, unsigned Column, DILocalScope *Scope,
1163                     DILocation *InlinedAt = nullptr),
1164                    (Line, Column, Scope, InlinedAt))
1165
1166  /// \brief Return a (temporary) clone of this.
1167  TempDILocation clone() const { return cloneImpl(); }
1168
1169  unsigned getLine() const { return SubclassData32; }
1170  unsigned getColumn() const { return SubclassData16; }
1171  DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1172  DILocation *getInlinedAt() const {
1173    return cast_or_null<DILocation>(getRawInlinedAt());
1174  }
1175
1176  DIFile *getFile() const { return getScope()->getFile(); }
1177  StringRef getFilename() const { return getScope()->getFilename(); }
1178  StringRef getDirectory() const { return getScope()->getDirectory(); }
1179
1180  /// \brief Get the scope where this is inlined.
1181  ///
1182  /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
1183  /// location.
1184  DILocalScope *getInlinedAtScope() const {
1185    if (auto *IA = getInlinedAt())
1186      return IA->getInlinedAtScope();
1187    return getScope();
1188  }
1189
1190  /// \brief Check whether this can be discriminated from another location.
1191  ///
1192  /// Check \c this can be discriminated from \c RHS in a linetable entry.
1193  /// Scope and inlined-at chains are not recorded in the linetable, so they
1194  /// cannot be used to distinguish basic blocks.
1195  ///
1196  /// The current implementation is weaker than it should be, since it just
1197  /// checks filename and line.
1198  ///
1199  /// FIXME: Add a check for getDiscriminator().
1200  /// FIXME: Add a check for getColumn().
1201  /// FIXME: Change the getFilename() check to getFile() (or add one for
1202  /// getDirectory()).
1203  bool canDiscriminate(const DILocation &RHS) const {
1204    return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
1205  }
1206
1207  /// \brief Get the DWARF discriminator.
1208  ///
1209  /// DWARF discriminators distinguish identical file locations between
1210  /// instructions that are on different basic blocks.
1211  inline unsigned getDiscriminator() const;
1212
1213  /// \brief Compute new discriminator in the given context.
1214  ///
1215  /// This modifies the \a LLVMContext that \c this is in to increment the next
1216  /// discriminator for \c this's line/filename combination.
1217  ///
1218  /// FIXME: Delete this.  See comments in implementation and at the only call
1219  /// site in \a AddDiscriminators::runOnFunction().
1220  unsigned computeNewDiscriminator() const;
1221
1222  Metadata *getRawScope() const { return getOperand(0); }
1223  Metadata *getRawInlinedAt() const {
1224    if (getNumOperands() == 2)
1225      return getOperand(1);
1226    return nullptr;
1227  }
1228
1229  static bool classof(const Metadata *MD) {
1230    return MD->getMetadataID() == DILocationKind;
1231  }
1232};
1233
1234/// \brief Subprogram description.
1235///
1236/// TODO: Remove DisplayName.  It's always equal to Name.
1237/// TODO: Split up flags.
1238class DISubprogram : public DILocalScope {
1239  friend class LLVMContextImpl;
1240  friend class MDNode;
1241
1242  unsigned Line;
1243  unsigned ScopeLine;
1244  unsigned Virtuality;
1245  unsigned VirtualIndex;
1246  unsigned Flags;
1247  bool IsLocalToUnit;
1248  bool IsDefinition;
1249  bool IsOptimized;
1250
1251  DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
1252               unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
1253               unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
1254               bool IsOptimized, ArrayRef<Metadata *> Ops)
1255      : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram,
1256                     Ops),
1257        Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
1258        VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
1259        IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
1260  ~DISubprogram() = default;
1261
1262  static DISubprogram *
1263  getImpl(LLVMContext &Context, DIScopeRef Scope, StringRef Name,
1264          StringRef LinkageName, DIFile *File, unsigned Line,
1265          DISubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
1266          unsigned ScopeLine, DITypeRef ContainingType, unsigned Virtuality,
1267          unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1268          Constant *Function, DITemplateParameterArray TemplateParams,
1269          DISubprogram *Declaration, DILocalVariableArray Variables,
1270          StorageType Storage, bool ShouldCreate = true) {
1271    return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1272                   getCanonicalMDString(Context, LinkageName), File, Line, Type,
1273                   IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1274                   Virtuality, VirtualIndex, Flags, IsOptimized,
1275                   Function ? ConstantAsMetadata::get(Function) : nullptr,
1276                   TemplateParams.get(), Declaration, Variables.get(), Storage,
1277                   ShouldCreate);
1278  }
1279  static DISubprogram *
1280  getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1281          MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1282          bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1283          Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
1284          unsigned Flags, bool IsOptimized, Metadata *Function,
1285          Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
1286          StorageType Storage, bool ShouldCreate = true);
1287
1288  TempDISubprogram cloneImpl() const {
1289    return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1290                        getFile(), getLine(), getType(), isLocalToUnit(),
1291                        isDefinition(), getScopeLine(), getContainingType(),
1292                        getVirtuality(), getVirtualIndex(), getFlags(),
1293                        isOptimized(), getFunctionConstant(),
1294                        getTemplateParams(), getDeclaration(), getVariables());
1295  }
1296
1297public:
1298  DEFINE_MDNODE_GET(DISubprogram,
1299                    (DIScopeRef Scope, StringRef Name, StringRef LinkageName,
1300                     DIFile *File, unsigned Line, DISubroutineType *Type,
1301                     bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
1302                     DITypeRef ContainingType, unsigned Virtuality,
1303                     unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1304                     Constant *Function = nullptr,
1305                     DITemplateParameterArray TemplateParams = nullptr,
1306                     DISubprogram *Declaration = nullptr,
1307                     DILocalVariableArray Variables = nullptr),
1308                    (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1309                     IsDefinition, ScopeLine, ContainingType, Virtuality,
1310                     VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
1311                     Declaration, Variables))
1312  DEFINE_MDNODE_GET(
1313      DISubprogram,
1314      (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
1315       unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
1316       unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
1317       unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
1318       Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
1319       Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
1320      (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
1321       ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1322       Function, TemplateParams, Declaration, Variables))
1323
1324  TempDISubprogram clone() const { return cloneImpl(); }
1325
1326public:
1327  unsigned getLine() const { return Line; }
1328  unsigned getVirtuality() const { return Virtuality; }
1329  unsigned getVirtualIndex() const { return VirtualIndex; }
1330  unsigned getScopeLine() const { return ScopeLine; }
1331  unsigned getFlags() const { return Flags; }
1332  bool isLocalToUnit() const { return IsLocalToUnit; }
1333  bool isDefinition() const { return IsDefinition; }
1334  bool isOptimized() const { return IsOptimized; }
1335
1336  unsigned isArtificial() const { return getFlags() & FlagArtificial; }
1337  bool isPrivate() const {
1338    return (getFlags() & FlagAccessibility) == FlagPrivate;
1339  }
1340  bool isProtected() const {
1341    return (getFlags() & FlagAccessibility) == FlagProtected;
1342  }
1343  bool isPublic() const {
1344    return (getFlags() & FlagAccessibility) == FlagPublic;
1345  }
1346  bool isExplicit() const { return getFlags() & FlagExplicit; }
1347  bool isPrototyped() const { return getFlags() & FlagPrototyped; }
1348
1349  /// \brief Check if this is reference-qualified.
1350  ///
1351  /// Return true if this subprogram is a C++11 reference-qualified non-static
1352  /// member function (void foo() &).
1353  unsigned isLValueReference() const {
1354    return getFlags() & FlagLValueReference;
1355  }
1356
1357  /// \brief Check if this is rvalue-reference-qualified.
1358  ///
1359  /// Return true if this subprogram is a C++11 rvalue-reference-qualified
1360  /// non-static member function (void foo() &&).
1361  unsigned isRValueReference() const {
1362    return getFlags() & FlagRValueReference;
1363  }
1364
1365  DIScopeRef getScope() const { return DIScopeRef(getRawScope()); }
1366
1367  StringRef getName() const { return getStringOperand(2); }
1368  StringRef getDisplayName() const { return getStringOperand(3); }
1369  StringRef getLinkageName() const { return getStringOperand(4); }
1370
1371  MDString *getRawName() const { return getOperandAs<MDString>(2); }
1372  MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
1373
1374  DISubroutineType *getType() const {
1375    return cast_or_null<DISubroutineType>(getRawType());
1376  }
1377  DITypeRef getContainingType() const {
1378    return DITypeRef(getRawContainingType());
1379  }
1380
1381  Constant *getFunctionConstant() const {
1382    if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
1383      return C->getValue();
1384    return nullptr;
1385  }
1386  DITemplateParameterArray getTemplateParams() const {
1387    return cast_or_null<MDTuple>(getRawTemplateParams());
1388  }
1389  DISubprogram *getDeclaration() const {
1390    return cast_or_null<DISubprogram>(getRawDeclaration());
1391  }
1392  DILocalVariableArray getVariables() const {
1393    return cast_or_null<MDTuple>(getRawVariables());
1394  }
1395
1396  Metadata *getRawScope() const { return getOperand(1); }
1397  Metadata *getRawType() const { return getOperand(5); }
1398  Metadata *getRawContainingType() const { return getOperand(6); }
1399  Metadata *getRawFunction() const { return getOperand(7); }
1400  Metadata *getRawTemplateParams() const { return getOperand(8); }
1401  Metadata *getRawDeclaration() const { return getOperand(9); }
1402  Metadata *getRawVariables() const { return getOperand(10); }
1403
1404  /// \brief Get a pointer to the function this subprogram describes.
1405  ///
1406  /// This dyn_casts \a getFunctionConstant() to \a Function.
1407  ///
1408  /// FIXME: Should this be looking through bitcasts?
1409  Function *getFunction() const;
1410
1411  /// \brief Replace the function.
1412  ///
1413  /// If \a isUniqued() and not \a isResolved(), this could node will be
1414  /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
1415  /// that's a problem.
1416  /// @{
1417  void replaceFunction(Function *F);
1418  void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
1419  void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
1420  /// @}
1421
1422  /// \brief Check if this subprogram decribes the given function.
1423  ///
1424  /// FIXME: Should this be looking through bitcasts?
1425  bool describes(const Function *F) const;
1426
1427  static bool classof(const Metadata *MD) {
1428    return MD->getMetadataID() == DISubprogramKind;
1429  }
1430};
1431
1432class DILexicalBlockBase : public DILocalScope {
1433protected:
1434  DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
1435                     ArrayRef<Metadata *> Ops)
1436      : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
1437  ~DILexicalBlockBase() = default;
1438
1439public:
1440  DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
1441
1442  Metadata *getRawScope() const { return getOperand(1); }
1443
1444  /// \brief Forwarding accessors to LexicalBlock.
1445  ///
1446  /// TODO: Remove these and update code to use \a DILexicalBlock directly.
1447  /// @{
1448  inline unsigned getLine() const;
1449  inline unsigned getColumn() const;
1450  /// @}
1451  static bool classof(const Metadata *MD) {
1452    return MD->getMetadataID() == DILexicalBlockKind ||
1453           MD->getMetadataID() == DILexicalBlockFileKind;
1454  }
1455};
1456
1457class DILexicalBlock : public DILexicalBlockBase {
1458  friend class LLVMContextImpl;
1459  friend class MDNode;
1460
1461  unsigned Line;
1462  unsigned Column;
1463
1464  DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
1465                 unsigned Column, ArrayRef<Metadata *> Ops)
1466      : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
1467        Column(Column) {}
1468  ~DILexicalBlock() = default;
1469
1470  static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
1471                                 DIFile *File, unsigned Line, unsigned Column,
1472                                 StorageType Storage,
1473                                 bool ShouldCreate = true) {
1474    return getImpl(Context, static_cast<Metadata *>(Scope),
1475                   static_cast<Metadata *>(File), Line, Column, Storage,
1476                   ShouldCreate);
1477  }
1478
1479  static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
1480                                 Metadata *File, unsigned Line, unsigned Column,
1481                                 StorageType Storage, bool ShouldCreate = true);
1482
1483  TempDILexicalBlock cloneImpl() const {
1484    return getTemporary(getContext(), getScope(), getFile(), getLine(),
1485                        getColumn());
1486  }
1487
1488public:
1489  DEFINE_MDNODE_GET(DILexicalBlock, (DILocalScope * Scope, DIFile *File,
1490                                     unsigned Line, unsigned Column),
1491                    (Scope, File, Line, Column))
1492  DEFINE_MDNODE_GET(DILexicalBlock, (Metadata * Scope, Metadata *File,
1493                                     unsigned Line, unsigned Column),
1494                    (Scope, File, Line, Column))
1495
1496  TempDILexicalBlock clone() const { return cloneImpl(); }
1497
1498  unsigned getLine() const { return Line; }
1499  unsigned getColumn() const { return Column; }
1500
1501  static bool classof(const Metadata *MD) {
1502    return MD->getMetadataID() == DILexicalBlockKind;
1503  }
1504};
1505
1506unsigned DILexicalBlockBase::getLine() const {
1507  if (auto *N = dyn_cast<DILexicalBlock>(this))
1508    return N->getLine();
1509  return 0;
1510}
1511
1512unsigned DILexicalBlockBase::getColumn() const {
1513  if (auto *N = dyn_cast<DILexicalBlock>(this))
1514    return N->getColumn();
1515  return 0;
1516}
1517
1518class DILexicalBlockFile : public DILexicalBlockBase {
1519  friend class LLVMContextImpl;
1520  friend class MDNode;
1521
1522  unsigned Discriminator;
1523
1524  DILexicalBlockFile(LLVMContext &C, StorageType Storage,
1525                     unsigned Discriminator, ArrayRef<Metadata *> Ops)
1526      : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
1527        Discriminator(Discriminator) {}
1528  ~DILexicalBlockFile() = default;
1529
1530  static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
1531                                     DIFile *File, unsigned Discriminator,
1532                                     StorageType Storage,
1533                                     bool ShouldCreate = true) {
1534    return getImpl(Context, static_cast<Metadata *>(Scope),
1535                   static_cast<Metadata *>(File), Discriminator, Storage,
1536                   ShouldCreate);
1537  }
1538
1539  static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
1540                                     Metadata *File, unsigned Discriminator,
1541                                     StorageType Storage,
1542                                     bool ShouldCreate = true);
1543
1544  TempDILexicalBlockFile cloneImpl() const {
1545    return getTemporary(getContext(), getScope(), getFile(),
1546                        getDiscriminator());
1547  }
1548
1549public:
1550  DEFINE_MDNODE_GET(DILexicalBlockFile, (DILocalScope * Scope, DIFile *File,
1551                                         unsigned Discriminator),
1552                    (Scope, File, Discriminator))
1553  DEFINE_MDNODE_GET(DILexicalBlockFile,
1554                    (Metadata * Scope, Metadata *File, unsigned Discriminator),
1555                    (Scope, File, Discriminator))
1556
1557  TempDILexicalBlockFile clone() const { return cloneImpl(); }
1558
1559  // TODO: Remove these once they're gone from DILexicalBlockBase.
1560  unsigned getLine() const = delete;
1561  unsigned getColumn() const = delete;
1562
1563  unsigned getDiscriminator() const { return Discriminator; }
1564
1565  static bool classof(const Metadata *MD) {
1566    return MD->getMetadataID() == DILexicalBlockFileKind;
1567  }
1568};
1569
1570unsigned DILocation::getDiscriminator() const {
1571  if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
1572    return F->getDiscriminator();
1573  return 0;
1574}
1575
1576class DINamespace : public DIScope {
1577  friend class LLVMContextImpl;
1578  friend class MDNode;
1579
1580  unsigned Line;
1581
1582  DINamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
1583              ArrayRef<Metadata *> Ops)
1584      : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace,
1585                Ops),
1586        Line(Line) {}
1587  ~DINamespace() = default;
1588
1589  static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
1590                              DIFile *File, StringRef Name, unsigned Line,
1591                              StorageType Storage, bool ShouldCreate = true) {
1592    return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
1593                   Line, Storage, ShouldCreate);
1594  }
1595  static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
1596                              Metadata *File, MDString *Name, unsigned Line,
1597                              StorageType Storage, bool ShouldCreate = true);
1598
1599  TempDINamespace cloneImpl() const {
1600    return getTemporary(getContext(), getScope(), getFile(), getName(),
1601                        getLine());
1602  }
1603
1604public:
1605  DEFINE_MDNODE_GET(DINamespace, (DIScope * Scope, DIFile *File, StringRef Name,
1606                                  unsigned Line),
1607                    (Scope, File, Name, Line))
1608  DEFINE_MDNODE_GET(DINamespace, (Metadata * Scope, Metadata *File,
1609                                  MDString *Name, unsigned Line),
1610                    (Scope, File, Name, Line))
1611
1612  TempDINamespace clone() const { return cloneImpl(); }
1613
1614  unsigned getLine() const { return Line; }
1615  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1616  StringRef getName() const { return getStringOperand(2); }
1617
1618  Metadata *getRawScope() const { return getOperand(1); }
1619  MDString *getRawName() const { return getOperandAs<MDString>(2); }
1620
1621  static bool classof(const Metadata *MD) {
1622    return MD->getMetadataID() == DINamespaceKind;
1623  }
1624};
1625
1626/// \brief Base class for template parameters.
1627class DITemplateParameter : public DINode {
1628protected:
1629  DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
1630                      unsigned Tag, ArrayRef<Metadata *> Ops)
1631      : DINode(Context, ID, Storage, Tag, Ops) {}
1632  ~DITemplateParameter() = default;
1633
1634public:
1635  StringRef getName() const { return getStringOperand(0); }
1636  DITypeRef getType() const { return DITypeRef(getRawType()); }
1637
1638  MDString *getRawName() const { return getOperandAs<MDString>(0); }
1639  Metadata *getRawType() const { return getOperand(1); }
1640
1641  static bool classof(const Metadata *MD) {
1642    return MD->getMetadataID() == DITemplateTypeParameterKind ||
1643           MD->getMetadataID() == DITemplateValueParameterKind;
1644  }
1645};
1646
1647class DITemplateTypeParameter : public DITemplateParameter {
1648  friend class LLVMContextImpl;
1649  friend class MDNode;
1650
1651  DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
1652                          ArrayRef<Metadata *> Ops)
1653      : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage,
1654                            dwarf::DW_TAG_template_type_parameter, Ops) {}
1655  ~DITemplateTypeParameter() = default;
1656
1657  static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
1658                                          DITypeRef Type, StorageType Storage,
1659                                          bool ShouldCreate = true) {
1660    return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
1661                   ShouldCreate);
1662  }
1663  static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
1664                                          Metadata *Type, StorageType Storage,
1665                                          bool ShouldCreate = true);
1666
1667  TempDITemplateTypeParameter cloneImpl() const {
1668    return getTemporary(getContext(), getName(), getType());
1669  }
1670
1671public:
1672  DEFINE_MDNODE_GET(DITemplateTypeParameter, (StringRef Name, DITypeRef Type),
1673                    (Name, Type))
1674  DEFINE_MDNODE_GET(DITemplateTypeParameter, (MDString * Name, Metadata *Type),
1675                    (Name, Type))
1676
1677  TempDITemplateTypeParameter clone() const { return cloneImpl(); }
1678
1679  static bool classof(const Metadata *MD) {
1680    return MD->getMetadataID() == DITemplateTypeParameterKind;
1681  }
1682};
1683
1684class DITemplateValueParameter : public DITemplateParameter {
1685  friend class LLVMContextImpl;
1686  friend class MDNode;
1687
1688  DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
1689                           unsigned Tag, ArrayRef<Metadata *> Ops)
1690      : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
1691                            Ops) {}
1692  ~DITemplateValueParameter() = default;
1693
1694  static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1695                                           StringRef Name, DITypeRef Type,
1696                                           Metadata *Value, StorageType Storage,
1697                                           bool ShouldCreate = true) {
1698    return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
1699                   Value, Storage, ShouldCreate);
1700  }
1701  static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
1702                                           MDString *Name, Metadata *Type,
1703                                           Metadata *Value, StorageType Storage,
1704                                           bool ShouldCreate = true);
1705
1706  TempDITemplateValueParameter cloneImpl() const {
1707    return getTemporary(getContext(), getTag(), getName(), getType(),
1708                        getValue());
1709  }
1710
1711public:
1712  DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, StringRef Name,
1713                                               DITypeRef Type, Metadata *Value),
1714                    (Tag, Name, Type, Value))
1715  DEFINE_MDNODE_GET(DITemplateValueParameter, (unsigned Tag, MDString *Name,
1716                                               Metadata *Type, Metadata *Value),
1717                    (Tag, Name, Type, Value))
1718
1719  TempDITemplateValueParameter clone() const { return cloneImpl(); }
1720
1721  Metadata *getValue() const { return getOperand(2); }
1722
1723  static bool classof(const Metadata *MD) {
1724    return MD->getMetadataID() == DITemplateValueParameterKind;
1725  }
1726};
1727
1728/// \brief Base class for variables.
1729///
1730/// TODO: Hardcode to DW_TAG_variable.
1731class DIVariable : public DINode {
1732  unsigned Line;
1733
1734protected:
1735  DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
1736             unsigned Line, ArrayRef<Metadata *> Ops)
1737      : DINode(C, ID, Storage, Tag, Ops), Line(Line) {}
1738  ~DIVariable() = default;
1739
1740public:
1741  unsigned getLine() const { return Line; }
1742  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
1743  StringRef getName() const { return getStringOperand(1); }
1744  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
1745  DITypeRef getType() const { return DITypeRef(getRawType()); }
1746
1747  StringRef getFilename() const {
1748    if (auto *F = getFile())
1749      return F->getFilename();
1750    return "";
1751  }
1752  StringRef getDirectory() const {
1753    if (auto *F = getFile())
1754      return F->getDirectory();
1755    return "";
1756  }
1757
1758  Metadata *getRawScope() const { return getOperand(0); }
1759  MDString *getRawName() const { return getOperandAs<MDString>(1); }
1760  Metadata *getRawFile() const { return getOperand(2); }
1761  Metadata *getRawType() const { return getOperand(3); }
1762
1763  static bool classof(const Metadata *MD) {
1764    return MD->getMetadataID() == DILocalVariableKind ||
1765           MD->getMetadataID() == DIGlobalVariableKind;
1766  }
1767};
1768
1769/// \brief Global variables.
1770///
1771/// TODO: Remove DisplayName.  It's always equal to Name.
1772class DIGlobalVariable : public DIVariable {
1773  friend class LLVMContextImpl;
1774  friend class MDNode;
1775
1776  bool IsLocalToUnit;
1777  bool IsDefinition;
1778
1779  DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
1780                   bool IsLocalToUnit, bool IsDefinition,
1781                   ArrayRef<Metadata *> Ops)
1782      : DIVariable(C, DIGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
1783                   Line, Ops),
1784        IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
1785  ~DIGlobalVariable() = default;
1786
1787  static DIGlobalVariable *
1788  getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
1789          StringRef LinkageName, DIFile *File, unsigned Line, DITypeRef Type,
1790          bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1791          DIDerivedType *StaticDataMemberDeclaration, StorageType Storage,
1792          bool ShouldCreate = true) {
1793    return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
1794                   getCanonicalMDString(Context, LinkageName), File, Line, Type,
1795                   IsLocalToUnit, IsDefinition,
1796                   Variable ? ConstantAsMetadata::get(Variable) : nullptr,
1797                   StaticDataMemberDeclaration, Storage, ShouldCreate);
1798  }
1799  static DIGlobalVariable *
1800  getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
1801          MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
1802          bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1803          Metadata *StaticDataMemberDeclaration, StorageType Storage,
1804          bool ShouldCreate = true);
1805
1806  TempDIGlobalVariable cloneImpl() const {
1807    return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
1808                        getFile(), getLine(), getType(), isLocalToUnit(),
1809                        isDefinition(), getVariable(),
1810                        getStaticDataMemberDeclaration());
1811  }
1812
1813public:
1814  DEFINE_MDNODE_GET(DIGlobalVariable,
1815                    (DIScope * Scope, StringRef Name, StringRef LinkageName,
1816                     DIFile *File, unsigned Line, DITypeRef Type,
1817                     bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
1818                     DIDerivedType *StaticDataMemberDeclaration),
1819                    (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1820                     IsDefinition, Variable, StaticDataMemberDeclaration))
1821  DEFINE_MDNODE_GET(DIGlobalVariable,
1822                    (Metadata * Scope, MDString *Name, MDString *LinkageName,
1823                     Metadata *File, unsigned Line, Metadata *Type,
1824                     bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
1825                     Metadata *StaticDataMemberDeclaration),
1826                    (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1827                     IsDefinition, Variable, StaticDataMemberDeclaration))
1828
1829  TempDIGlobalVariable clone() const { return cloneImpl(); }
1830
1831  bool isLocalToUnit() const { return IsLocalToUnit; }
1832  bool isDefinition() const { return IsDefinition; }
1833  StringRef getDisplayName() const { return getStringOperand(4); }
1834  StringRef getLinkageName() const { return getStringOperand(5); }
1835  Constant *getVariable() const {
1836    if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
1837      return dyn_cast<Constant>(C->getValue());
1838    return nullptr;
1839  }
1840  DIDerivedType *getStaticDataMemberDeclaration() const {
1841    return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
1842  }
1843
1844  MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
1845  Metadata *getRawVariable() const { return getOperand(6); }
1846  Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
1847
1848  static bool classof(const Metadata *MD) {
1849    return MD->getMetadataID() == DIGlobalVariableKind;
1850  }
1851};
1852
1853/// \brief Local variable.
1854///
1855/// TODO: Split between arguments and otherwise.
1856/// TODO: Use \c DW_TAG_variable instead of fake tags.
1857/// TODO: Split up flags.
1858class DILocalVariable : public DIVariable {
1859  friend class LLVMContextImpl;
1860  friend class MDNode;
1861
1862  unsigned Arg;
1863  unsigned Flags;
1864
1865  DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
1866                  unsigned Line, unsigned Arg, unsigned Flags,
1867                  ArrayRef<Metadata *> Ops)
1868      : DIVariable(C, DILocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
1869        Flags(Flags) {}
1870  ~DILocalVariable() = default;
1871
1872  static DILocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
1873                                  DIScope *Scope, StringRef Name, DIFile *File,
1874                                  unsigned Line, DITypeRef Type, unsigned Arg,
1875                                  unsigned Flags, StorageType Storage,
1876                                  bool ShouldCreate = true) {
1877    return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
1878                   File, Line, Type, Arg, Flags, Storage, ShouldCreate);
1879  }
1880  static DILocalVariable *
1881  getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
1882          Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
1883          unsigned Flags, StorageType Storage, bool ShouldCreate = true);
1884
1885  TempDILocalVariable cloneImpl() const {
1886    return getTemporary(getContext(), getTag(), getScope(), getName(),
1887                        getFile(), getLine(), getType(), getArg(), getFlags());
1888  }
1889
1890public:
1891  DEFINE_MDNODE_GET(DILocalVariable,
1892                    (unsigned Tag, DILocalScope *Scope, StringRef Name,
1893                     DIFile *File, unsigned Line, DITypeRef Type, unsigned Arg,
1894                     unsigned Flags),
1895                    (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1896  DEFINE_MDNODE_GET(DILocalVariable,
1897                    (unsigned Tag, Metadata *Scope, MDString *Name,
1898                     Metadata *File, unsigned Line, Metadata *Type,
1899                     unsigned Arg, unsigned Flags),
1900                    (Tag, Scope, Name, File, Line, Type, Arg, Flags))
1901
1902  TempDILocalVariable clone() const { return cloneImpl(); }
1903
1904  /// \brief Get the local scope for this variable.
1905  ///
1906  /// Variables must be defined in a local scope.
1907  DILocalScope *getScope() const {
1908    return cast<DILocalScope>(DIVariable::getScope());
1909  }
1910
1911  unsigned getArg() const { return Arg; }
1912  unsigned getFlags() const { return Flags; }
1913
1914  bool isArtificial() const { return getFlags() & FlagArtificial; }
1915  bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
1916
1917  /// \brief Check that a location is valid for this variable.
1918  ///
1919  /// Check that \c DL exists, is in the same subprogram, and has the same
1920  /// inlined-at location as \c this.  (Otherwise, it's not a valid attachemnt
1921  /// to a \a DbgInfoIntrinsic.)
1922  bool isValidLocationForIntrinsic(const DILocation *DL) const {
1923    return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
1924  }
1925
1926  static bool classof(const Metadata *MD) {
1927    return MD->getMetadataID() == DILocalVariableKind;
1928  }
1929};
1930
1931/// \brief DWARF expression.
1932///
1933/// This is (almost) a DWARF expression that modifies the location of a
1934/// variable or (or the location of a single piece of a variable).
1935///
1936/// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
1937/// and have DW_OP_plus consume the topmost elements on the stack.
1938///
1939/// TODO: Co-allocate the expression elements.
1940/// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
1941/// storage types.
1942class DIExpression : public MDNode {
1943  friend class LLVMContextImpl;
1944  friend class MDNode;
1945
1946  std::vector<uint64_t> Elements;
1947
1948  DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
1949      : MDNode(C, DIExpressionKind, Storage, None),
1950        Elements(Elements.begin(), Elements.end()) {}
1951  ~DIExpression() = default;
1952
1953  static DIExpression *getImpl(LLVMContext &Context,
1954                               ArrayRef<uint64_t> Elements, StorageType Storage,
1955                               bool ShouldCreate = true);
1956
1957  TempDIExpression cloneImpl() const {
1958    return getTemporary(getContext(), getElements());
1959  }
1960
1961public:
1962  DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
1963
1964  TempDIExpression clone() const { return cloneImpl(); }
1965
1966  ArrayRef<uint64_t> getElements() const { return Elements; }
1967
1968  unsigned getNumElements() const { return Elements.size(); }
1969  uint64_t getElement(unsigned I) const {
1970    assert(I < Elements.size() && "Index out of range");
1971    return Elements[I];
1972  }
1973
1974  /// \brief Return whether this is a piece of an aggregate variable.
1975  bool isBitPiece() const;
1976
1977  /// \brief Return the offset of this piece in bits.
1978  uint64_t getBitPieceOffset() const;
1979
1980  /// \brief Return the size of this piece in bits.
1981  uint64_t getBitPieceSize() const;
1982
1983  typedef ArrayRef<uint64_t>::iterator element_iterator;
1984  element_iterator elements_begin() const { return getElements().begin(); }
1985  element_iterator elements_end() const { return getElements().end(); }
1986
1987  /// \brief A lightweight wrapper around an expression operand.
1988  ///
1989  /// TODO: Store arguments directly and change \a DIExpression to store a
1990  /// range of these.
1991  class ExprOperand {
1992    const uint64_t *Op;
1993
1994  public:
1995    explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
1996
1997    const uint64_t *get() const { return Op; }
1998
1999    /// \brief Get the operand code.
2000    uint64_t getOp() const { return *Op; }
2001
2002    /// \brief Get an argument to the operand.
2003    ///
2004    /// Never returns the operand itself.
2005    uint64_t getArg(unsigned I) const { return Op[I + 1]; }
2006
2007    unsigned getNumArgs() const { return getSize() - 1; }
2008
2009    /// \brief Return the size of the operand.
2010    ///
2011    /// Return the number of elements in the operand (1 + args).
2012    unsigned getSize() const;
2013  };
2014
2015  /// \brief An iterator for expression operands.
2016  class expr_op_iterator
2017      : public std::iterator<std::input_iterator_tag, ExprOperand> {
2018    ExprOperand Op;
2019
2020  public:
2021    explicit expr_op_iterator(element_iterator I) : Op(I) {}
2022
2023    element_iterator getBase() const { return Op.get(); }
2024    const ExprOperand &operator*() const { return Op; }
2025    const ExprOperand *operator->() const { return &Op; }
2026
2027    expr_op_iterator &operator++() {
2028      increment();
2029      return *this;
2030    }
2031    expr_op_iterator operator++(int) {
2032      expr_op_iterator T(*this);
2033      increment();
2034      return T;
2035    }
2036
2037    /// \brief Get the next iterator.
2038    ///
2039    /// \a std::next() doesn't work because this is technically an
2040    /// input_iterator, but it's a perfectly valid operation.  This is an
2041    /// accessor to provide the same functionality.
2042    expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
2043
2044    bool operator==(const expr_op_iterator &X) const {
2045      return getBase() == X.getBase();
2046    }
2047    bool operator!=(const expr_op_iterator &X) const {
2048      return getBase() != X.getBase();
2049    }
2050
2051  private:
2052    void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
2053  };
2054
2055  /// \brief Visit the elements via ExprOperand wrappers.
2056  ///
2057  /// These range iterators visit elements through \a ExprOperand wrappers.
2058  /// This is not guaranteed to be a valid range unless \a isValid() gives \c
2059  /// true.
2060  ///
2061  /// \pre \a isValid() gives \c true.
2062  /// @{
2063  expr_op_iterator expr_op_begin() const {
2064    return expr_op_iterator(elements_begin());
2065  }
2066  expr_op_iterator expr_op_end() const {
2067    return expr_op_iterator(elements_end());
2068  }
2069  /// @}
2070
2071  bool isValid() const;
2072
2073  static bool classof(const Metadata *MD) {
2074    return MD->getMetadataID() == DIExpressionKind;
2075  }
2076};
2077
2078class DIObjCProperty : public DINode {
2079  friend class LLVMContextImpl;
2080  friend class MDNode;
2081
2082  unsigned Line;
2083  unsigned Attributes;
2084
2085  DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
2086                 unsigned Attributes, ArrayRef<Metadata *> Ops)
2087      : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
2088               Ops),
2089        Line(Line), Attributes(Attributes) {}
2090  ~DIObjCProperty() = default;
2091
2092  static DIObjCProperty *
2093  getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
2094          StringRef GetterName, StringRef SetterName, unsigned Attributes,
2095          DIType *Type, StorageType Storage, bool ShouldCreate = true) {
2096    return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
2097                   getCanonicalMDString(Context, GetterName),
2098                   getCanonicalMDString(Context, SetterName), Attributes, Type,
2099                   Storage, ShouldCreate);
2100  }
2101  static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
2102                                 Metadata *File, unsigned Line,
2103                                 MDString *GetterName, MDString *SetterName,
2104                                 unsigned Attributes, Metadata *Type,
2105                                 StorageType Storage, bool ShouldCreate = true);
2106
2107  TempDIObjCProperty cloneImpl() const {
2108    return getTemporary(getContext(), getName(), getFile(), getLine(),
2109                        getGetterName(), getSetterName(), getAttributes(),
2110                        getType());
2111  }
2112
2113public:
2114  DEFINE_MDNODE_GET(DIObjCProperty,
2115                    (StringRef Name, DIFile *File, unsigned Line,
2116                     StringRef GetterName, StringRef SetterName,
2117                     unsigned Attributes, DIType *Type),
2118                    (Name, File, Line, GetterName, SetterName, Attributes,
2119                     Type))
2120  DEFINE_MDNODE_GET(DIObjCProperty,
2121                    (MDString * Name, Metadata *File, unsigned Line,
2122                     MDString *GetterName, MDString *SetterName,
2123                     unsigned Attributes, Metadata *Type),
2124                    (Name, File, Line, GetterName, SetterName, Attributes,
2125                     Type))
2126
2127  TempDIObjCProperty clone() const { return cloneImpl(); }
2128
2129  unsigned getLine() const { return Line; }
2130  unsigned getAttributes() const { return Attributes; }
2131  StringRef getName() const { return getStringOperand(0); }
2132  DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
2133  StringRef getGetterName() const { return getStringOperand(2); }
2134  StringRef getSetterName() const { return getStringOperand(3); }
2135
2136  /// \brief Get the type.
2137  ///
2138  /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
2139  /// a type ref here.
2140  DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
2141
2142  StringRef getFilename() const {
2143    if (auto *F = getFile())
2144      return F->getFilename();
2145    return "";
2146  }
2147  StringRef getDirectory() const {
2148    if (auto *F = getFile())
2149      return F->getDirectory();
2150    return "";
2151  }
2152
2153  MDString *getRawName() const { return getOperandAs<MDString>(0); }
2154  Metadata *getRawFile() const { return getOperand(1); }
2155  MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
2156  MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
2157  Metadata *getRawType() const { return getOperand(4); }
2158
2159  static bool classof(const Metadata *MD) {
2160    return MD->getMetadataID() == DIObjCPropertyKind;
2161  }
2162};
2163
2164/// \brief An imported module (C++ using directive or similar).
2165class DIImportedEntity : public DINode {
2166  friend class LLVMContextImpl;
2167  friend class MDNode;
2168
2169  unsigned Line;
2170
2171  DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
2172                   unsigned Line, ArrayRef<Metadata *> Ops)
2173      : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
2174  ~DIImportedEntity() = default;
2175
2176  static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2177                                   DIScope *Scope, DINodeRef Entity,
2178                                   unsigned Line, StringRef Name,
2179                                   StorageType Storage,
2180                                   bool ShouldCreate = true) {
2181    return getImpl(Context, Tag, Scope, Entity, Line,
2182                   getCanonicalMDString(Context, Name), Storage, ShouldCreate);
2183  }
2184  static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
2185                                   Metadata *Scope, Metadata *Entity,
2186                                   unsigned Line, MDString *Name,
2187                                   StorageType Storage,
2188                                   bool ShouldCreate = true);
2189
2190  TempDIImportedEntity cloneImpl() const {
2191    return getTemporary(getContext(), getTag(), getScope(), getEntity(),
2192                        getLine(), getName());
2193  }
2194
2195public:
2196  DEFINE_MDNODE_GET(DIImportedEntity,
2197                    (unsigned Tag, DIScope *Scope, DINodeRef Entity,
2198                     unsigned Line, StringRef Name = ""),
2199                    (Tag, Scope, Entity, Line, Name))
2200  DEFINE_MDNODE_GET(DIImportedEntity,
2201                    (unsigned Tag, Metadata *Scope, Metadata *Entity,
2202                     unsigned Line, MDString *Name),
2203                    (Tag, Scope, Entity, Line, Name))
2204
2205  TempDIImportedEntity clone() const { return cloneImpl(); }
2206
2207  unsigned getLine() const { return Line; }
2208  DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
2209  DINodeRef getEntity() const { return DINodeRef(getRawEntity()); }
2210  StringRef getName() const { return getStringOperand(2); }
2211
2212  Metadata *getRawScope() const { return getOperand(0); }
2213  Metadata *getRawEntity() const { return getOperand(1); }
2214  MDString *getRawName() const { return getOperandAs<MDString>(2); }
2215
2216  static bool classof(const Metadata *MD) {
2217    return MD->getMetadataID() == DIImportedEntityKind;
2218  }
2219};
2220
2221} // end namespace llvm
2222
2223#undef DEFINE_MDNODE_GET_UNPACK_IMPL
2224#undef DEFINE_MDNODE_GET_UNPACK
2225#undef DEFINE_MDNODE_GET
2226
2227#endif
2228