Home | History | Annotate | Download | only in IR
      1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Declarations for metadata specific to debug info.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_IR_DEBUGINFOMETADATA_H
     15 #define LLVM_IR_DEBUGINFOMETADATA_H
     16 
     17 #include "llvm/IR/Metadata.h"
     18 #include "llvm/Support/Dwarf.h"
     19 
     20 // Helper macros for defining get() overrides.
     21 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
     22 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
     23 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS)                                 \
     24   static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) {  \
     25     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued);          \
     26   }                                                                            \
     27   static CLASS *getIfExists(LLVMContext &Context,                              \
     28                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
     29     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued,           \
     30                    /* ShouldCreate */ false);                                  \
     31   }                                                                            \
     32   static CLASS *getDistinct(LLVMContext &Context,                              \
     33                             DEFINE_MDNODE_GET_UNPACK(FORMAL)) {                \
     34     return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct);         \
     35   }                                                                            \
     36   static Temp##CLASS getTemporary(LLVMContext &Context,                        \
     37                                   DEFINE_MDNODE_GET_UNPACK(FORMAL)) {          \
     38     return Temp##CLASS(                                                        \
     39         getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary));          \
     40   }
     41 
     42 namespace llvm {
     43 
     44 /// \brief Pointer union between a subclass of DebugNode and MDString.
     45 ///
     46 /// \a MDCompositeType can be referenced via an \a MDString unique identifier.
     47 /// This class allows some type safety in the face of that, requiring either a
     48 /// node of a particular type or an \a MDString.
     49 template <class T> class TypedDebugNodeRef {
     50   const Metadata *MD = nullptr;
     51 
     52 public:
     53   TypedDebugNodeRef() = default;
     54   TypedDebugNodeRef(std::nullptr_t) {}
     55 
     56   /// \brief Construct from a raw pointer.
     57   explicit TypedDebugNodeRef(const Metadata *MD) : MD(MD) {
     58     assert((!MD || isa<MDString>(MD) || isa<T>(MD)) && "Expected valid ref");
     59   }
     60 
     61   template <class U>
     62   TypedDebugNodeRef(
     63       const TypedDebugNodeRef<U> &X,
     64       typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
     65           nullptr)
     66       : MD(X) {}
     67 
     68   operator Metadata *() const { return const_cast<Metadata *>(MD); }
     69 
     70   bool operator==(const TypedDebugNodeRef<T> &X) const { return MD == X.MD; };
     71   bool operator!=(const TypedDebugNodeRef<T> &X) const { return MD != X.MD; };
     72 
     73   /// \brief Create a reference.
     74   ///
     75   /// Get a reference to \c N, using an \a MDString reference if available.
     76   static TypedDebugNodeRef get(const T *N);
     77 
     78   template <class MapTy> T *resolve(const MapTy &Map) const {
     79     if (!MD)
     80       return nullptr;
     81 
     82     if (auto *Typed = dyn_cast<T>(MD))
     83       return const_cast<T *>(Typed);
     84 
     85     auto *S = cast<MDString>(MD);
     86     auto I = Map.find(S);
     87     assert(I != Map.end() && "Missing identifier in type map");
     88     return cast<T>(I->second);
     89   }
     90 };
     91 
     92 typedef TypedDebugNodeRef<DebugNode> DebugNodeRef;
     93 typedef TypedDebugNodeRef<MDScope> MDScopeRef;
     94 typedef TypedDebugNodeRef<MDType> MDTypeRef;
     95 
     96 class MDTypeRefArray {
     97   const MDTuple *N = nullptr;
     98 
     99 public:
    100   MDTypeRefArray(const MDTuple *N) : N(N) {}
    101 
    102   explicit operator bool() const { return get(); }
    103   explicit operator MDTuple *() const { return get(); }
    104 
    105   MDTuple *get() const { return const_cast<MDTuple *>(N); }
    106   MDTuple *operator->() const { return get(); }
    107   MDTuple &operator*() const { return *get(); }
    108 
    109   // FIXME: Fix callers and remove condition on N.
    110   unsigned size() const { return N ? N->getNumOperands() : 0u; }
    111   MDTypeRef operator[](unsigned I) const { return MDTypeRef(N->getOperand(I)); }
    112 
    113   class iterator : std::iterator<std::input_iterator_tag, MDTypeRef,
    114                                  std::ptrdiff_t, void, MDTypeRef> {
    115     MDNode::op_iterator I = nullptr;
    116 
    117   public:
    118     iterator() = default;
    119     explicit iterator(MDNode::op_iterator I) : I(I) {}
    120     MDTypeRef operator*() const { return MDTypeRef(*I); }
    121     iterator &operator++() {
    122       ++I;
    123       return *this;
    124     }
    125     iterator operator++(int) {
    126       iterator Temp(*this);
    127       ++I;
    128       return Temp;
    129     }
    130     bool operator==(const iterator &X) const { return I == X.I; }
    131     bool operator!=(const iterator &X) const { return I != X.I; }
    132   };
    133 
    134   // FIXME: Fix callers and remove condition on N.
    135   iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
    136   iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
    137 };
    138 
    139 /// \brief Tagged DWARF-like metadata node.
    140 ///
    141 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
    142 /// defined in llvm/Support/Dwarf.h).  Called \a DebugNode because it's
    143 /// potentially used for non-DWARF output.
    144 class DebugNode : public MDNode {
    145   friend class LLVMContextImpl;
    146   friend class MDNode;
    147 
    148 protected:
    149   DebugNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
    150             ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
    151       : MDNode(C, ID, Storage, Ops1, Ops2) {
    152     assert(Tag < 1u << 16);
    153     SubclassData16 = Tag;
    154   }
    155   ~DebugNode() = default;
    156 
    157   template <class Ty> Ty *getOperandAs(unsigned I) const {
    158     return cast_or_null<Ty>(getOperand(I));
    159   }
    160 
    161   StringRef getStringOperand(unsigned I) const {
    162     if (auto *S = getOperandAs<MDString>(I))
    163       return S->getString();
    164     return StringRef();
    165   }
    166 
    167   static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
    168     if (S.empty())
    169       return nullptr;
    170     return MDString::get(Context, S);
    171   }
    172 
    173 public:
    174   unsigned getTag() const { return SubclassData16; }
    175 
    176   /// \brief Debug info flags.
    177   ///
    178   /// The three accessibility flags are mutually exclusive and rolled together
    179   /// in the first two bits.
    180   enum DIFlags {
    181 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
    182 #include "llvm/IR/DebugInfoFlags.def"
    183     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
    184   };
    185 
    186   static unsigned getFlag(StringRef Flag);
    187   static const char *getFlagString(unsigned Flag);
    188 
    189   /// \brief Split up a flags bitfield.
    190   ///
    191   /// Split \c Flags into \c SplitFlags, a vector of its components.  Returns
    192   /// any remaining (unrecognized) bits.
    193   static unsigned splitFlags(unsigned Flags,
    194                              SmallVectorImpl<unsigned> &SplitFlags);
    195 
    196   DebugNodeRef getRef() const { return DebugNodeRef::get(this); }
    197 
    198   static bool classof(const Metadata *MD) {
    199     switch (MD->getMetadataID()) {
    200     default:
    201       return false;
    202     case GenericDebugNodeKind:
    203     case MDSubrangeKind:
    204     case MDEnumeratorKind:
    205     case MDBasicTypeKind:
    206     case MDDerivedTypeKind:
    207     case MDCompositeTypeKind:
    208     case MDSubroutineTypeKind:
    209     case MDFileKind:
    210     case MDCompileUnitKind:
    211     case MDSubprogramKind:
    212     case MDLexicalBlockKind:
    213     case MDLexicalBlockFileKind:
    214     case MDNamespaceKind:
    215     case MDTemplateTypeParameterKind:
    216     case MDTemplateValueParameterKind:
    217     case MDGlobalVariableKind:
    218     case MDLocalVariableKind:
    219     case MDObjCPropertyKind:
    220     case MDImportedEntityKind:
    221       return true;
    222     }
    223   }
    224 };
    225 
    226 template <class T>
    227 struct simplify_type<const TypedDebugNodeRef<T>> {
    228   typedef Metadata *SimpleType;
    229   static SimpleType getSimplifiedValue(const TypedDebugNodeRef<T> &MD) {
    230     return MD;
    231   }
    232 };
    233 
    234 template <class T>
    235 struct simplify_type<TypedDebugNodeRef<T>>
    236     : simplify_type<const TypedDebugNodeRef<T>> {};
    237 
    238 /// \brief Generic tagged DWARF-like metadata node.
    239 ///
    240 /// An un-specialized DWARF-like metadata node.  The first operand is a
    241 /// (possibly empty) null-separated \a MDString header that contains arbitrary
    242 /// fields.  The remaining operands are \a dwarf_operands(), and are pointers
    243 /// to other metadata.
    244 class GenericDebugNode : public DebugNode {
    245   friend class LLVMContextImpl;
    246   friend class MDNode;
    247 
    248   GenericDebugNode(LLVMContext &C, StorageType Storage, unsigned Hash,
    249                    unsigned Tag, ArrayRef<Metadata *> Ops1,
    250                    ArrayRef<Metadata *> Ops2)
    251       : DebugNode(C, GenericDebugNodeKind, Storage, Tag, Ops1, Ops2) {
    252     setHash(Hash);
    253   }
    254   ~GenericDebugNode() { dropAllReferences(); }
    255 
    256   void setHash(unsigned Hash) { SubclassData32 = Hash; }
    257   void recalculateHash();
    258 
    259   static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
    260                                    StringRef Header,
    261                                    ArrayRef<Metadata *> DwarfOps,
    262                                    StorageType Storage,
    263                                    bool ShouldCreate = true) {
    264     return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
    265                    DwarfOps, Storage, ShouldCreate);
    266   }
    267 
    268   static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
    269                                    MDString *Header,
    270                                    ArrayRef<Metadata *> DwarfOps,
    271                                    StorageType Storage,
    272                                    bool ShouldCreate = true);
    273 
    274   TempGenericDebugNode cloneImpl() const {
    275     return getTemporary(
    276         getContext(), getTag(), getHeader(),
    277         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
    278   }
    279 
    280 public:
    281   unsigned getHash() const { return SubclassData32; }
    282 
    283   DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, StringRef Header,
    284                                        ArrayRef<Metadata *> DwarfOps),
    285                     (Tag, Header, DwarfOps))
    286   DEFINE_MDNODE_GET(GenericDebugNode, (unsigned Tag, MDString *Header,
    287                                        ArrayRef<Metadata *> DwarfOps),
    288                     (Tag, Header, DwarfOps))
    289 
    290   /// \brief Return a (temporary) clone of this.
    291   TempGenericDebugNode clone() const { return cloneImpl(); }
    292 
    293   unsigned getTag() const { return SubclassData16; }
    294   StringRef getHeader() const { return getStringOperand(0); }
    295 
    296   op_iterator dwarf_op_begin() const { return op_begin() + 1; }
    297   op_iterator dwarf_op_end() const { return op_end(); }
    298   op_range dwarf_operands() const {
    299     return op_range(dwarf_op_begin(), dwarf_op_end());
    300   }
    301 
    302   unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
    303   const MDOperand &getDwarfOperand(unsigned I) const {
    304     return getOperand(I + 1);
    305   }
    306   void replaceDwarfOperandWith(unsigned I, Metadata *New) {
    307     replaceOperandWith(I + 1, New);
    308   }
    309 
    310   static bool classof(const Metadata *MD) {
    311     return MD->getMetadataID() == GenericDebugNodeKind;
    312   }
    313 };
    314 
    315 /// \brief Array subrange.
    316 ///
    317 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
    318 /// type.
    319 class MDSubrange : public DebugNode {
    320   friend class LLVMContextImpl;
    321   friend class MDNode;
    322 
    323   int64_t Count;
    324   int64_t LowerBound;
    325 
    326   MDSubrange(LLVMContext &C, StorageType Storage, int64_t Count,
    327              int64_t LowerBound)
    328       : DebugNode(C, MDSubrangeKind, Storage, dwarf::DW_TAG_subrange_type,
    329                   None),
    330         Count(Count), LowerBound(LowerBound) {}
    331   ~MDSubrange() = default;
    332 
    333   static MDSubrange *getImpl(LLVMContext &Context, int64_t Count,
    334                              int64_t LowerBound, StorageType Storage,
    335                              bool ShouldCreate = true);
    336 
    337   TempMDSubrange cloneImpl() const {
    338     return getTemporary(getContext(), getCount(), getLowerBound());
    339   }
    340 
    341 public:
    342   DEFINE_MDNODE_GET(MDSubrange, (int64_t Count, int64_t LowerBound = 0),
    343                     (Count, LowerBound))
    344 
    345   TempMDSubrange clone() const { return cloneImpl(); }
    346 
    347   int64_t getLowerBound() const { return LowerBound; }
    348   int64_t getCount() const { return Count; }
    349 
    350   static bool classof(const Metadata *MD) {
    351     return MD->getMetadataID() == MDSubrangeKind;
    352   }
    353 };
    354 
    355 /// \brief Enumeration value.
    356 ///
    357 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
    358 /// longer creates a type cycle.
    359 class MDEnumerator : public DebugNode {
    360   friend class LLVMContextImpl;
    361   friend class MDNode;
    362 
    363   int64_t Value;
    364 
    365   MDEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
    366                ArrayRef<Metadata *> Ops)
    367       : DebugNode(C, MDEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops),
    368         Value(Value) {}
    369   ~MDEnumerator() = default;
    370 
    371   static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
    372                                StringRef Name, StorageType Storage,
    373                                bool ShouldCreate = true) {
    374     return getImpl(Context, Value, getCanonicalMDString(Context, Name), Storage,
    375                    ShouldCreate);
    376   }
    377   static MDEnumerator *getImpl(LLVMContext &Context, int64_t Value,
    378                                MDString *Name, StorageType Storage,
    379                                bool ShouldCreate = true);
    380 
    381   TempMDEnumerator cloneImpl() const {
    382     return getTemporary(getContext(), getValue(), getName());
    383   }
    384 
    385 public:
    386   DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, StringRef Name),
    387                     (Value, Name))
    388   DEFINE_MDNODE_GET(MDEnumerator, (int64_t Value, MDString *Name),
    389                     (Value, Name))
    390 
    391   TempMDEnumerator clone() const { return cloneImpl(); }
    392 
    393   int64_t getValue() const { return Value; }
    394   StringRef getName() const { return getStringOperand(0); }
    395 
    396   MDString *getRawName() const { return getOperandAs<MDString>(0); }
    397 
    398   static bool classof(const Metadata *MD) {
    399     return MD->getMetadataID() == MDEnumeratorKind;
    400   }
    401 };
    402 
    403 /// \brief Base class for scope-like contexts.
    404 ///
    405 /// Base class for lexical scopes and types (which are also declaration
    406 /// contexts).
    407 ///
    408 /// TODO: Separate the concepts of declaration contexts and lexical scopes.
    409 class MDScope : public DebugNode {
    410 protected:
    411   MDScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
    412           ArrayRef<Metadata *> Ops)
    413       : DebugNode(C, ID, Storage, Tag, Ops) {}
    414   ~MDScope() = default;
    415 
    416 public:
    417   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
    418 
    419   inline StringRef getFilename() const;
    420   inline StringRef getDirectory() const;
    421 
    422   StringRef getName() const;
    423   MDScopeRef getScope() const;
    424 
    425   /// \brief Return the raw underlying file.
    426   ///
    427   /// An \a MDFile is an \a MDScope, but it doesn't point at a separate file
    428   /// (it\em is the file).  If \c this is an \a MDFile, we need to return \c
    429   /// this.  Otherwise, return the first operand, which is where all other
    430   /// subclasses store their file pointer.
    431   Metadata *getRawFile() const {
    432     return isa<MDFile>(this) ? const_cast<MDScope *>(this)
    433                              : static_cast<Metadata *>(getOperand(0));
    434   }
    435 
    436   MDScopeRef getRef() const { return MDScopeRef::get(this); }
    437 
    438   static bool classof(const Metadata *MD) {
    439     switch (MD->getMetadataID()) {
    440     default:
    441       return false;
    442     case MDBasicTypeKind:
    443     case MDDerivedTypeKind:
    444     case MDCompositeTypeKind:
    445     case MDSubroutineTypeKind:
    446     case MDFileKind:
    447     case MDCompileUnitKind:
    448     case MDSubprogramKind:
    449     case MDLexicalBlockKind:
    450     case MDLexicalBlockFileKind:
    451     case MDNamespaceKind:
    452       return true;
    453     }
    454   }
    455 };
    456 
    457 /// \brief File.
    458 ///
    459 /// TODO: Merge with directory/file node (including users).
    460 /// TODO: Canonicalize paths on creation.
    461 class MDFile : public MDScope {
    462   friend class LLVMContextImpl;
    463   friend class MDNode;
    464 
    465   MDFile(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops)
    466       : MDScope(C, MDFileKind, Storage, dwarf::DW_TAG_file_type, Ops) {}
    467   ~MDFile() = default;
    468 
    469   static MDFile *getImpl(LLVMContext &Context, StringRef Filename,
    470                          StringRef Directory, StorageType Storage,
    471                          bool ShouldCreate = true) {
    472     return getImpl(Context, getCanonicalMDString(Context, Filename),
    473                    getCanonicalMDString(Context, Directory), Storage,
    474                    ShouldCreate);
    475   }
    476   static MDFile *getImpl(LLVMContext &Context, MDString *Filename,
    477                          MDString *Directory, StorageType Storage,
    478                          bool ShouldCreate = true);
    479 
    480   TempMDFile cloneImpl() const {
    481     return getTemporary(getContext(), getFilename(), getDirectory());
    482   }
    483 
    484 public:
    485   DEFINE_MDNODE_GET(MDFile, (StringRef Filename, StringRef Directory),
    486                     (Filename, Directory))
    487   DEFINE_MDNODE_GET(MDFile, (MDString * Filename, MDString *Directory),
    488                     (Filename, Directory))
    489 
    490   TempMDFile clone() const { return cloneImpl(); }
    491 
    492   StringRef getFilename() const { return getStringOperand(0); }
    493   StringRef getDirectory() const { return getStringOperand(1); }
    494 
    495   MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
    496   MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
    497 
    498   static bool classof(const Metadata *MD) {
    499     return MD->getMetadataID() == MDFileKind;
    500   }
    501 };
    502 
    503 StringRef MDScope::getFilename() const {
    504   if (auto *F = getFile())
    505     return F->getFilename();
    506   return "";
    507 }
    508 
    509 StringRef MDScope::getDirectory() const {
    510   if (auto *F = getFile())
    511     return F->getDirectory();
    512   return "";
    513 }
    514 
    515 /// \brief Base class for types.
    516 ///
    517 /// TODO: Remove the hardcoded name and context, since many types don't use
    518 /// them.
    519 /// TODO: Split up flags.
    520 class MDType : public MDScope {
    521   unsigned Line;
    522   unsigned Flags;
    523   uint64_t SizeInBits;
    524   uint64_t AlignInBits;
    525   uint64_t OffsetInBits;
    526 
    527 protected:
    528   MDType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
    529          unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
    530          uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
    531       : MDScope(C, ID, Storage, Tag, Ops), Line(Line), Flags(Flags),
    532         SizeInBits(SizeInBits), AlignInBits(AlignInBits),
    533         OffsetInBits(OffsetInBits) {}
    534   ~MDType() = default;
    535 
    536 public:
    537   TempMDType clone() const {
    538     return TempMDType(cast<MDType>(MDNode::clone().release()));
    539   }
    540 
    541   unsigned getLine() const { return Line; }
    542   uint64_t getSizeInBits() const { return SizeInBits; }
    543   uint64_t getAlignInBits() const { return AlignInBits; }
    544   uint64_t getOffsetInBits() const { return OffsetInBits; }
    545   unsigned getFlags() const { return Flags; }
    546 
    547   MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
    548   StringRef getName() const { return getStringOperand(2); }
    549 
    550 
    551   Metadata *getRawScope() const { return getOperand(1); }
    552   MDString *getRawName() const { return getOperandAs<MDString>(2); }
    553 
    554   void setFlags(unsigned NewFlags) {
    555     assert(!isUniqued() && "Cannot set flags on uniqued nodes");
    556     Flags = NewFlags;
    557   }
    558 
    559   bool isPrivate() const {
    560     return (getFlags() & FlagAccessibility) == FlagPrivate;
    561   }
    562   bool isProtected() const {
    563     return (getFlags() & FlagAccessibility) == FlagProtected;
    564   }
    565   bool isPublic() const {
    566     return (getFlags() & FlagAccessibility) == FlagPublic;
    567   }
    568   bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
    569   bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
    570   bool isBlockByrefStruct() const { return getFlags() & FlagBlockByrefStruct; }
    571   bool isVirtual() const { return getFlags() & FlagVirtual; }
    572   bool isArtificial() const { return getFlags() & FlagArtificial; }
    573   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
    574   bool isObjcClassComplete() const {
    575     return getFlags() & FlagObjcClassComplete;
    576   }
    577   bool isVector() const { return getFlags() & FlagVector; }
    578   bool isStaticMember() const { return getFlags() & FlagStaticMember; }
    579   bool isLValueReference() const { return getFlags() & FlagLValueReference; }
    580   bool isRValueReference() const { return getFlags() & FlagRValueReference; }
    581 
    582   MDTypeRef getRef() const { return MDTypeRef::get(this); }
    583 
    584   static bool classof(const Metadata *MD) {
    585     switch (MD->getMetadataID()) {
    586     default:
    587       return false;
    588     case MDBasicTypeKind:
    589     case MDDerivedTypeKind:
    590     case MDCompositeTypeKind:
    591     case MDSubroutineTypeKind:
    592       return true;
    593     }
    594   }
    595 };
    596 
    597 /// \brief Basic type, like 'int' or 'float'.
    598 ///
    599 /// TODO: Split out DW_TAG_unspecified_type.
    600 /// TODO: Drop unused accessors.
    601 class MDBasicType : public MDType {
    602   friend class LLVMContextImpl;
    603   friend class MDNode;
    604 
    605   unsigned Encoding;
    606 
    607   MDBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
    608               uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding,
    609               ArrayRef<Metadata *> Ops)
    610       : MDType(C, MDBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
    611                0, Ops),
    612         Encoding(Encoding) {}
    613   ~MDBasicType() = default;
    614 
    615   static MDBasicType *getImpl(LLVMContext &Context, unsigned Tag,
    616                               StringRef Name, uint64_t SizeInBits,
    617                               uint64_t AlignInBits, unsigned Encoding,
    618                               StorageType Storage, bool ShouldCreate = true) {
    619     return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
    620                    SizeInBits, AlignInBits, Encoding, Storage, ShouldCreate);
    621   }
    622   static MDBasicType *getImpl(LLVMContext &Context, unsigned Tag,
    623                               MDString *Name, uint64_t SizeInBits,
    624                               uint64_t AlignInBits, unsigned Encoding,
    625                               StorageType Storage, bool ShouldCreate = true);
    626 
    627   TempMDBasicType cloneImpl() const {
    628     return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
    629                         getAlignInBits(), getEncoding());
    630   }
    631 
    632 public:
    633   DEFINE_MDNODE_GET(MDBasicType, (unsigned Tag, StringRef Name),
    634                     (Tag, Name, 0, 0, 0))
    635   DEFINE_MDNODE_GET(MDBasicType,
    636                     (unsigned Tag, StringRef Name, uint64_t SizeInBits,
    637                      uint64_t AlignInBits, unsigned Encoding),
    638                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
    639   DEFINE_MDNODE_GET(MDBasicType,
    640                     (unsigned Tag, MDString *Name, uint64_t SizeInBits,
    641                      uint64_t AlignInBits, unsigned Encoding),
    642                     (Tag, Name, SizeInBits, AlignInBits, Encoding))
    643 
    644   TempMDBasicType clone() const { return cloneImpl(); }
    645 
    646   unsigned getEncoding() const { return Encoding; }
    647 
    648   static bool classof(const Metadata *MD) {
    649     return MD->getMetadataID() == MDBasicTypeKind;
    650   }
    651 };
    652 
    653 /// \brief Base class for MDDerivedType and MDCompositeType.
    654 ///
    655 /// TODO: Delete; they're not really related.
    656 class MDDerivedTypeBase : public MDType {
    657 protected:
    658   MDDerivedTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
    659                     unsigned Tag, unsigned Line, uint64_t SizeInBits,
    660                     uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
    661                     ArrayRef<Metadata *> Ops)
    662       : MDType(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
    663                Flags, Ops) {}
    664   ~MDDerivedTypeBase() = default;
    665 
    666 public:
    667   MDTypeRef getBaseType() const { return MDTypeRef(getRawBaseType()); }
    668   Metadata *getRawBaseType() const { return getOperand(3); }
    669 
    670   static bool classof(const Metadata *MD) {
    671     return MD->getMetadataID() == MDDerivedTypeKind ||
    672            MD->getMetadataID() == MDCompositeTypeKind ||
    673            MD->getMetadataID() == MDSubroutineTypeKind;
    674   }
    675 };
    676 
    677 /// \brief Derived types.
    678 ///
    679 /// This includes qualified types, pointers, references, friends, typedefs, and
    680 /// class members.
    681 ///
    682 /// TODO: Split out members (inheritance, fields, methods, etc.).
    683 class MDDerivedType : public MDDerivedTypeBase {
    684   friend class LLVMContextImpl;
    685   friend class MDNode;
    686 
    687   MDDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
    688                 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
    689                 uint64_t OffsetInBits, unsigned Flags, ArrayRef<Metadata *> Ops)
    690       : MDDerivedTypeBase(C, MDDerivedTypeKind, Storage, Tag, Line, SizeInBits,
    691                           AlignInBits, OffsetInBits, Flags, Ops) {}
    692   ~MDDerivedType() = default;
    693 
    694   static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
    695                                 StringRef Name, MDFile *File, unsigned Line,
    696                                 MDScopeRef Scope, MDTypeRef BaseType,
    697                                 uint64_t SizeInBits, uint64_t AlignInBits,
    698                                 uint64_t OffsetInBits, unsigned Flags,
    699                                 Metadata *ExtraData, StorageType Storage,
    700                                 bool ShouldCreate = true) {
    701     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
    702                    Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
    703                    Flags, ExtraData, Storage, ShouldCreate);
    704   }
    705   static MDDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
    706                                 MDString *Name, Metadata *File, unsigned Line,
    707                                 Metadata *Scope, Metadata *BaseType,
    708                                 uint64_t SizeInBits, uint64_t AlignInBits,
    709                                 uint64_t OffsetInBits, unsigned Flags,
    710                                 Metadata *ExtraData, StorageType Storage,
    711                                 bool ShouldCreate = true);
    712 
    713   TempMDDerivedType cloneImpl() const {
    714     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
    715                         getScope(), getBaseType(), getSizeInBits(),
    716                         getAlignInBits(), getOffsetInBits(), getFlags(),
    717                         getExtraData());
    718   }
    719 
    720 public:
    721   DEFINE_MDNODE_GET(MDDerivedType,
    722                     (unsigned Tag, MDString *Name, Metadata *File,
    723                      unsigned Line, Metadata *Scope, Metadata *BaseType,
    724                      uint64_t SizeInBits, uint64_t AlignInBits,
    725                      uint64_t OffsetInBits, unsigned Flags,
    726                      Metadata *ExtraData = nullptr),
    727                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    728                      AlignInBits, OffsetInBits, Flags, ExtraData))
    729   DEFINE_MDNODE_GET(MDDerivedType,
    730                     (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
    731                      MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
    732                      uint64_t AlignInBits, uint64_t OffsetInBits,
    733                      unsigned Flags, Metadata *ExtraData = nullptr),
    734                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    735                      AlignInBits, OffsetInBits, Flags, ExtraData))
    736 
    737   TempMDDerivedType clone() const { return cloneImpl(); }
    738 
    739   /// \brief Get extra data associated with this derived type.
    740   ///
    741   /// Class type for pointer-to-members, objective-c property node for ivars,
    742   /// or global constant wrapper for static members.
    743   ///
    744   /// TODO: Separate out types that need this extra operand: pointer-to-member
    745   /// types and member fields (static members and ivars).
    746   Metadata *getExtraData() const { return getRawExtraData(); }
    747   Metadata *getRawExtraData() const { return getOperand(4); }
    748 
    749   /// \brief Get casted version of extra data.
    750   /// @{
    751   MDTypeRef getClassType() const {
    752     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
    753     return MDTypeRef(getExtraData());
    754   }
    755   MDObjCProperty *getObjCProperty() const {
    756     return dyn_cast_or_null<MDObjCProperty>(getExtraData());
    757   }
    758   Constant *getConstant() const {
    759     assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
    760     if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
    761       return C->getValue();
    762     return nullptr;
    763   }
    764   /// @}
    765 
    766   static bool classof(const Metadata *MD) {
    767     return MD->getMetadataID() == MDDerivedTypeKind;
    768   }
    769 };
    770 
    771 /// \brief Base class for MDCompositeType and MDSubroutineType.
    772 ///
    773 /// TODO: Delete; they're not really related.
    774 class MDCompositeTypeBase : public MDDerivedTypeBase {
    775   unsigned RuntimeLang;
    776 
    777 protected:
    778   MDCompositeTypeBase(LLVMContext &C, unsigned ID, StorageType Storage,
    779                       unsigned Tag, unsigned Line, unsigned RuntimeLang,
    780                       uint64_t SizeInBits, uint64_t AlignInBits,
    781                       uint64_t OffsetInBits, unsigned Flags,
    782                       ArrayRef<Metadata *> Ops)
    783       : MDDerivedTypeBase(C, ID, Storage, Tag, Line, SizeInBits, AlignInBits,
    784                           OffsetInBits, Flags, Ops),
    785         RuntimeLang(RuntimeLang) {}
    786   ~MDCompositeTypeBase() = default;
    787 
    788 public:
    789   /// \brief Get the elements of the composite type.
    790   ///
    791   /// \note Calling this is only valid for \a MDCompositeType.  This assertion
    792   /// can be removed once \a MDSubroutineType has been separated from
    793   /// "composite types".
    794   DebugNodeArray getElements() const {
    795     assert(!isa<MDSubroutineType>(this) && "no elements for DISubroutineType");
    796     return cast_or_null<MDTuple>(getRawElements());
    797   }
    798   MDTypeRef getVTableHolder() const { return MDTypeRef(getRawVTableHolder()); }
    799   MDTemplateParameterArray getTemplateParams() const {
    800     return cast_or_null<MDTuple>(getRawTemplateParams());
    801   }
    802   StringRef getIdentifier() const { return getStringOperand(7); }
    803   unsigned getRuntimeLang() const { return RuntimeLang; }
    804 
    805   Metadata *getRawElements() const { return getOperand(4); }
    806   Metadata *getRawVTableHolder() const { return getOperand(5); }
    807   Metadata *getRawTemplateParams() const { return getOperand(6); }
    808   MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
    809 
    810   /// \brief Replace operands.
    811   ///
    812   /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
    813   /// this will be RAUW'ed and deleted.  Use a \a TrackingMDRef to keep track
    814   /// of its movement if necessary.
    815   /// @{
    816   void replaceElements(DebugNodeArray Elements) {
    817 #ifndef NDEBUG
    818     for (DebugNode *Op : getElements())
    819       assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
    820              "Lost a member during member list replacement");
    821 #endif
    822     replaceOperandWith(4, Elements.get());
    823   }
    824   void replaceVTableHolder(MDTypeRef VTableHolder) {
    825     replaceOperandWith(5, VTableHolder);
    826   }
    827   void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
    828     replaceOperandWith(6, TemplateParams.get());
    829   }
    830   /// @}
    831 
    832   static bool classof(const Metadata *MD) {
    833     return MD->getMetadataID() == MDCompositeTypeKind ||
    834            MD->getMetadataID() == MDSubroutineTypeKind;
    835   }
    836 };
    837 
    838 /// \brief Composite types.
    839 ///
    840 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
    841 /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
    842 class MDCompositeType : public MDCompositeTypeBase {
    843   friend class LLVMContextImpl;
    844   friend class MDNode;
    845 
    846   MDCompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
    847                   unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
    848                   uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
    849                   ArrayRef<Metadata *> Ops)
    850       : MDCompositeTypeBase(C, MDCompositeTypeKind, Storage, Tag, Line,
    851                             RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
    852                             Flags, Ops) {}
    853   ~MDCompositeType() = default;
    854 
    855   static MDCompositeType *
    856   getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
    857           unsigned Line, MDScopeRef Scope, MDTypeRef BaseType,
    858           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
    859           uint64_t Flags, DebugNodeArray Elements, unsigned RuntimeLang,
    860           MDTypeRef VTableHolder, MDTemplateParameterArray TemplateParams,
    861           StringRef Identifier, StorageType Storage, bool ShouldCreate = true) {
    862     return getImpl(
    863         Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
    864         BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
    865         RuntimeLang, VTableHolder, TemplateParams.get(),
    866         getCanonicalMDString(Context, Identifier), Storage, ShouldCreate);
    867   }
    868   static MDCompositeType *
    869   getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
    870           unsigned Line, Metadata *Scope, Metadata *BaseType,
    871           uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
    872           unsigned Flags, Metadata *Elements, unsigned RuntimeLang,
    873           Metadata *VTableHolder, Metadata *TemplateParams,
    874           MDString *Identifier, StorageType Storage, bool ShouldCreate = true);
    875 
    876   TempMDCompositeType cloneImpl() const {
    877     return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
    878                         getScope(), getBaseType(), getSizeInBits(),
    879                         getAlignInBits(), getOffsetInBits(), getFlags(),
    880                         getElements(), getRuntimeLang(), getVTableHolder(),
    881                         getTemplateParams(), getIdentifier());
    882   }
    883 
    884 public:
    885   DEFINE_MDNODE_GET(MDCompositeType,
    886                     (unsigned Tag, StringRef Name, MDFile *File, unsigned Line,
    887                      MDScopeRef Scope, MDTypeRef BaseType, uint64_t SizeInBits,
    888                      uint64_t AlignInBits, uint64_t OffsetInBits,
    889                      unsigned Flags, DebugNodeArray Elements,
    890                      unsigned RuntimeLang, MDTypeRef VTableHolder,
    891                      MDTemplateParameterArray TemplateParams = nullptr,
    892                      StringRef Identifier = ""),
    893                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    894                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
    895                      VTableHolder, TemplateParams, Identifier))
    896   DEFINE_MDNODE_GET(MDCompositeType,
    897                     (unsigned Tag, MDString *Name, Metadata *File,
    898                      unsigned Line, Metadata *Scope, Metadata *BaseType,
    899                      uint64_t SizeInBits, uint64_t AlignInBits,
    900                      uint64_t OffsetInBits, unsigned Flags, Metadata *Elements,
    901                      unsigned RuntimeLang, Metadata *VTableHolder,
    902                      Metadata *TemplateParams = nullptr,
    903                      MDString *Identifier = nullptr),
    904                     (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
    905                      AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
    906                      VTableHolder, TemplateParams, Identifier))
    907 
    908   TempMDCompositeType clone() const { return cloneImpl(); }
    909 
    910   static bool classof(const Metadata *MD) {
    911     return MD->getMetadataID() == MDCompositeTypeKind;
    912   }
    913 };
    914 
    915 template <class T> TypedDebugNodeRef<T> TypedDebugNodeRef<T>::get(const T *N) {
    916   if (N)
    917     if (auto *Composite = dyn_cast<MDCompositeType>(N))
    918       if (auto *S = Composite->getRawIdentifier())
    919         return TypedDebugNodeRef<T>(S);
    920   return TypedDebugNodeRef<T>(N);
    921 }
    922 
    923 /// \brief Type array for a subprogram.
    924 ///
    925 /// TODO: Detach from CompositeType, and fold the array of types in directly
    926 /// as operands.
    927 class MDSubroutineType : public MDCompositeTypeBase {
    928   friend class LLVMContextImpl;
    929   friend class MDNode;
    930 
    931   MDSubroutineType(LLVMContext &C, StorageType Storage, unsigned Flags,
    932                    ArrayRef<Metadata *> Ops)
    933       : MDCompositeTypeBase(C, MDSubroutineTypeKind, Storage,
    934                             dwarf::DW_TAG_subroutine_type, 0, 0, 0, 0, 0, Flags,
    935                             Ops) {}
    936   ~MDSubroutineType() = default;
    937 
    938   static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
    939                                    MDTypeRefArray TypeArray,
    940                                    StorageType Storage,
    941                                    bool ShouldCreate = true) {
    942     return getImpl(Context, Flags, TypeArray.get(), Storage, ShouldCreate);
    943   }
    944   static MDSubroutineType *getImpl(LLVMContext &Context, unsigned Flags,
    945                                    Metadata *TypeArray, StorageType Storage,
    946                                    bool ShouldCreate = true);
    947 
    948   TempMDSubroutineType cloneImpl() const {
    949     return getTemporary(getContext(), getFlags(), getTypeArray());
    950   }
    951 
    952 public:
    953   DEFINE_MDNODE_GET(MDSubroutineType,
    954                     (unsigned Flags, MDTypeRefArray TypeArray),
    955                     (Flags, TypeArray))
    956   DEFINE_MDNODE_GET(MDSubroutineType, (unsigned Flags, Metadata *TypeArray),
    957                     (Flags, TypeArray))
    958 
    959   TempMDSubroutineType clone() const { return cloneImpl(); }
    960 
    961   MDTypeRefArray getTypeArray() const {
    962     return cast_or_null<MDTuple>(getRawTypeArray());
    963   }
    964   Metadata *getRawTypeArray() const { return getRawElements(); }
    965 
    966   static bool classof(const Metadata *MD) {
    967     return MD->getMetadataID() == MDSubroutineTypeKind;
    968   }
    969 };
    970 
    971 /// \brief Compile unit.
    972 class MDCompileUnit : public MDScope {
    973   friend class LLVMContextImpl;
    974   friend class MDNode;
    975 
    976   unsigned SourceLanguage;
    977   bool IsOptimized;
    978   unsigned RuntimeVersion;
    979   unsigned EmissionKind;
    980 
    981   MDCompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
    982                 bool IsOptimized, unsigned RuntimeVersion,
    983                 unsigned EmissionKind, ArrayRef<Metadata *> Ops)
    984       : MDScope(C, MDCompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops),
    985         SourceLanguage(SourceLanguage), IsOptimized(IsOptimized),
    986         RuntimeVersion(RuntimeVersion), EmissionKind(EmissionKind) {}
    987   ~MDCompileUnit() = default;
    988 
    989   static MDCompileUnit *
    990   getImpl(LLVMContext &Context, unsigned SourceLanguage, MDFile *File,
    991           StringRef Producer, bool IsOptimized, StringRef Flags,
    992           unsigned RuntimeVersion, StringRef SplitDebugFilename,
    993           unsigned EmissionKind, MDCompositeTypeArray EnumTypes,
    994           MDTypeArray RetainedTypes, MDSubprogramArray Subprograms,
    995           MDGlobalVariableArray GlobalVariables,
    996           MDImportedEntityArray ImportedEntities, StorageType Storage,
    997           bool ShouldCreate = true) {
    998     return getImpl(
    999         Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
   1000         IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
   1001         getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
   1002         EnumTypes.get(), RetainedTypes.get(), Subprograms.get(),
   1003         GlobalVariables.get(), ImportedEntities.get(), Storage, ShouldCreate);
   1004   }
   1005   static MDCompileUnit *
   1006   getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
   1007           MDString *Producer, bool IsOptimized, MDString *Flags,
   1008           unsigned RuntimeVersion, MDString *SplitDebugFilename,
   1009           unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
   1010           Metadata *Subprograms, Metadata *GlobalVariables,
   1011           Metadata *ImportedEntities, StorageType Storage,
   1012           bool ShouldCreate = true);
   1013 
   1014   TempMDCompileUnit cloneImpl() const {
   1015     return getTemporary(
   1016         getContext(), getSourceLanguage(), getFile(), getProducer(),
   1017         isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
   1018         getEmissionKind(), getEnumTypes(), getRetainedTypes(), getSubprograms(),
   1019         getGlobalVariables(), getImportedEntities());
   1020   }
   1021 
   1022 public:
   1023   DEFINE_MDNODE_GET(MDCompileUnit,
   1024                     (unsigned SourceLanguage, MDFile *File, StringRef Producer,
   1025                      bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
   1026                      StringRef SplitDebugFilename, unsigned EmissionKind,
   1027                      MDCompositeTypeArray EnumTypes, MDTypeArray RetainedTypes,
   1028                      MDSubprogramArray Subprograms,
   1029                      MDGlobalVariableArray GlobalVariables,
   1030                      MDImportedEntityArray ImportedEntities),
   1031                     (SourceLanguage, File, Producer, IsOptimized, Flags,
   1032                      RuntimeVersion, SplitDebugFilename, EmissionKind,
   1033                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
   1034                      ImportedEntities))
   1035   DEFINE_MDNODE_GET(MDCompileUnit,
   1036                     (unsigned SourceLanguage, Metadata *File,
   1037                      MDString *Producer, bool IsOptimized, MDString *Flags,
   1038                      unsigned RuntimeVersion, MDString *SplitDebugFilename,
   1039                      unsigned EmissionKind, Metadata *EnumTypes,
   1040                      Metadata *RetainedTypes, Metadata *Subprograms,
   1041                      Metadata *GlobalVariables, Metadata *ImportedEntities),
   1042                     (SourceLanguage, File, Producer, IsOptimized, Flags,
   1043                      RuntimeVersion, SplitDebugFilename, EmissionKind,
   1044                      EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
   1045                      ImportedEntities))
   1046 
   1047   TempMDCompileUnit clone() const { return cloneImpl(); }
   1048 
   1049   unsigned getSourceLanguage() const { return SourceLanguage; }
   1050   bool isOptimized() const { return IsOptimized; }
   1051   unsigned getRuntimeVersion() const { return RuntimeVersion; }
   1052   unsigned getEmissionKind() const { return EmissionKind; }
   1053   StringRef getProducer() const { return getStringOperand(1); }
   1054   StringRef getFlags() const { return getStringOperand(2); }
   1055   StringRef getSplitDebugFilename() const { return getStringOperand(3); }
   1056   MDCompositeTypeArray getEnumTypes() const {
   1057     return cast_or_null<MDTuple>(getRawEnumTypes());
   1058   }
   1059   MDTypeArray getRetainedTypes() const {
   1060     return cast_or_null<MDTuple>(getRawRetainedTypes());
   1061   }
   1062   MDSubprogramArray getSubprograms() const {
   1063     return cast_or_null<MDTuple>(getRawSubprograms());
   1064   }
   1065   MDGlobalVariableArray getGlobalVariables() const {
   1066     return cast_or_null<MDTuple>(getRawGlobalVariables());
   1067   }
   1068   MDImportedEntityArray getImportedEntities() const {
   1069     return cast_or_null<MDTuple>(getRawImportedEntities());
   1070   }
   1071 
   1072   MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
   1073   MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
   1074   MDString *getRawSplitDebugFilename() const {
   1075     return getOperandAs<MDString>(3);
   1076   }
   1077   Metadata *getRawEnumTypes() const { return getOperand(4); }
   1078   Metadata *getRawRetainedTypes() const { return getOperand(5); }
   1079   Metadata *getRawSubprograms() const { return getOperand(6); }
   1080   Metadata *getRawGlobalVariables() const { return getOperand(7); }
   1081   Metadata *getRawImportedEntities() const { return getOperand(8); }
   1082 
   1083   /// \brief Replace arrays.
   1084   ///
   1085   /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
   1086   /// deleted on a uniquing collision.  In practice, uniquing collisions on \a
   1087   /// MDCompileUnit should be fairly rare.
   1088   /// @{
   1089   void replaceSubprograms(MDSubprogramArray N) {
   1090     replaceOperandWith(6, N.get());
   1091   }
   1092   void replaceGlobalVariables(MDGlobalVariableArray N) {
   1093     replaceOperandWith(7, N.get());
   1094   }
   1095   /// @}
   1096 
   1097   static bool classof(const Metadata *MD) {
   1098     return MD->getMetadataID() == MDCompileUnitKind;
   1099   }
   1100 };
   1101 
   1102 /// \brief A scope for locals.
   1103 ///
   1104 /// A legal scope for lexical blocks, local variables, and debug info
   1105 /// locations.  Subclasses are \a MDSubprogram, \a MDLexicalBlock, and \a
   1106 /// MDLexicalBlockFile.
   1107 class MDLocalScope : public MDScope {
   1108 protected:
   1109   MDLocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
   1110                ArrayRef<Metadata *> Ops)
   1111       : MDScope(C, ID, Storage, Tag, Ops) {}
   1112   ~MDLocalScope() = default;
   1113 
   1114 public:
   1115   /// \brief Get the subprogram for this scope.
   1116   ///
   1117   /// Return this if it's an \a MDSubprogram; otherwise, look up the scope
   1118   /// chain.
   1119   MDSubprogram *getSubprogram() const;
   1120 
   1121   static bool classof(const Metadata *MD) {
   1122     return MD->getMetadataID() == MDSubprogramKind ||
   1123            MD->getMetadataID() == MDLexicalBlockKind ||
   1124            MD->getMetadataID() == MDLexicalBlockFileKind;
   1125   }
   1126 };
   1127 
   1128 /// \brief Debug location.
   1129 ///
   1130 /// A debug location in source code, used for debug info and otherwise.
   1131 class MDLocation : public MDNode {
   1132   friend class LLVMContextImpl;
   1133   friend class MDNode;
   1134 
   1135   MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
   1136              unsigned Column, ArrayRef<Metadata *> MDs);
   1137   ~MDLocation() { dropAllReferences(); }
   1138 
   1139   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
   1140                              unsigned Column, Metadata *Scope,
   1141                              Metadata *InlinedAt, StorageType Storage,
   1142                              bool ShouldCreate = true);
   1143   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
   1144                              unsigned Column, MDLocalScope *Scope,
   1145                              MDLocation *InlinedAt, StorageType Storage,
   1146                              bool ShouldCreate = true) {
   1147     return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
   1148                    static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
   1149   }
   1150 
   1151   TempMDLocation cloneImpl() const {
   1152     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
   1153                         getInlinedAt());
   1154   }
   1155 
   1156   // Disallow replacing operands.
   1157   void replaceOperandWith(unsigned I, Metadata *New) = delete;
   1158 
   1159 public:
   1160   DEFINE_MDNODE_GET(MDLocation,
   1161                     (unsigned Line, unsigned Column, Metadata *Scope,
   1162                      Metadata *InlinedAt = nullptr),
   1163                     (Line, Column, Scope, InlinedAt))
   1164   DEFINE_MDNODE_GET(MDLocation,
   1165                     (unsigned Line, unsigned Column, MDLocalScope *Scope,
   1166                      MDLocation *InlinedAt = nullptr),
   1167                     (Line, Column, Scope, InlinedAt))
   1168 
   1169   /// \brief Return a (temporary) clone of this.
   1170   TempMDLocation clone() const { return cloneImpl(); }
   1171 
   1172   unsigned getLine() const { return SubclassData32; }
   1173   unsigned getColumn() const { return SubclassData16; }
   1174   MDLocalScope *getScope() const {
   1175     return cast<MDLocalScope>(getRawScope());
   1176   }
   1177   MDLocation *getInlinedAt() const {
   1178     return cast_or_null<MDLocation>(getRawInlinedAt());
   1179   }
   1180 
   1181   MDFile *getFile() const { return getScope()->getFile(); }
   1182   StringRef getFilename() const { return getScope()->getFilename(); }
   1183   StringRef getDirectory() const { return getScope()->getDirectory(); }
   1184 
   1185   /// \brief Get the scope where this is inlined.
   1186   ///
   1187   /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
   1188   /// location.
   1189   MDLocalScope *getInlinedAtScope() const {
   1190     if (auto *IA = getInlinedAt())
   1191       return IA->getInlinedAtScope();
   1192     return getScope();
   1193   }
   1194 
   1195   /// \brief Check whether this can be discriminated from another location.
   1196   ///
   1197   /// Check \c this can be discriminated from \c RHS in a linetable entry.
   1198   /// Scope and inlined-at chains are not recorded in the linetable, so they
   1199   /// cannot be used to distinguish basic blocks.
   1200   ///
   1201   /// The current implementation is weaker than it should be, since it just
   1202   /// checks filename and line.
   1203   ///
   1204   /// FIXME: Add a check for getDiscriminator().
   1205   /// FIXME: Add a check for getColumn().
   1206   /// FIXME: Change the getFilename() check to getFile() (or add one for
   1207   /// getDirectory()).
   1208   bool canDiscriminate(const MDLocation &RHS) const {
   1209     return getFilename() != RHS.getFilename() || getLine() != RHS.getLine();
   1210   }
   1211 
   1212   /// \brief Get the DWARF discriminator.
   1213   ///
   1214   /// DWARF discriminators distinguish identical file locations between
   1215   /// instructions that are on different basic blocks.
   1216   inline unsigned getDiscriminator() const;
   1217 
   1218   /// \brief Compute new discriminator in the given context.
   1219   ///
   1220   /// This modifies the \a LLVMContext that \c this is in to increment the next
   1221   /// discriminator for \c this's line/filename combination.
   1222   ///
   1223   /// FIXME: Delete this.  See comments in implementation and at the only call
   1224   /// site in \a AddDiscriminators::runOnFunction().
   1225   unsigned computeNewDiscriminator() const;
   1226 
   1227   Metadata *getRawScope() const { return getOperand(0); }
   1228   Metadata *getRawInlinedAt() const {
   1229     if (getNumOperands() == 2)
   1230       return getOperand(1);
   1231     return nullptr;
   1232   }
   1233 
   1234   static bool classof(const Metadata *MD) {
   1235     return MD->getMetadataID() == MDLocationKind;
   1236   }
   1237 };
   1238 
   1239 /// \brief Subprogram description.
   1240 ///
   1241 /// TODO: Remove DisplayName.  It's always equal to Name.
   1242 /// TODO: Split up flags.
   1243 class MDSubprogram : public MDLocalScope {
   1244   friend class LLVMContextImpl;
   1245   friend class MDNode;
   1246 
   1247   unsigned Line;
   1248   unsigned ScopeLine;
   1249   unsigned Virtuality;
   1250   unsigned VirtualIndex;
   1251   unsigned Flags;
   1252   bool IsLocalToUnit;
   1253   bool IsDefinition;
   1254   bool IsOptimized;
   1255 
   1256   MDSubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
   1257                unsigned ScopeLine, unsigned Virtuality, unsigned VirtualIndex,
   1258                unsigned Flags, bool IsLocalToUnit, bool IsDefinition,
   1259                bool IsOptimized, ArrayRef<Metadata *> Ops)
   1260       : MDLocalScope(C, MDSubprogramKind, Storage, dwarf::DW_TAG_subprogram,
   1261                      Ops),
   1262         Line(Line), ScopeLine(ScopeLine), Virtuality(Virtuality),
   1263         VirtualIndex(VirtualIndex), Flags(Flags), IsLocalToUnit(IsLocalToUnit),
   1264         IsDefinition(IsDefinition), IsOptimized(IsOptimized) {}
   1265   ~MDSubprogram() = default;
   1266 
   1267   static MDSubprogram *
   1268   getImpl(LLVMContext &Context, MDScopeRef Scope, StringRef Name,
   1269           StringRef LinkageName, MDFile *File, unsigned Line,
   1270           MDSubroutineType *Type, bool IsLocalToUnit, bool IsDefinition,
   1271           unsigned ScopeLine, MDTypeRef ContainingType, unsigned Virtuality,
   1272           unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
   1273           Constant *Function, MDTemplateParameterArray TemplateParams,
   1274           MDSubprogram *Declaration, MDLocalVariableArray Variables,
   1275           StorageType Storage, bool ShouldCreate = true) {
   1276     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
   1277                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
   1278                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
   1279                    Virtuality, VirtualIndex, Flags, IsOptimized,
   1280                    Function ? ConstantAsMetadata::get(Function) : nullptr,
   1281                    TemplateParams.get(), Declaration, Variables.get(), Storage,
   1282                    ShouldCreate);
   1283   }
   1284   static MDSubprogram *
   1285   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
   1286           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
   1287           bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
   1288           Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
   1289           unsigned Flags, bool IsOptimized, Metadata *Function,
   1290           Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
   1291           StorageType Storage, bool ShouldCreate = true);
   1292 
   1293   TempMDSubprogram cloneImpl() const {
   1294     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
   1295                         getFile(), getLine(), getType(), isLocalToUnit(),
   1296                         isDefinition(), getScopeLine(), getContainingType(),
   1297                         getVirtuality(), getVirtualIndex(), getFlags(),
   1298                         isOptimized(), getFunctionConstant(),
   1299                         getTemplateParams(), getDeclaration(), getVariables());
   1300   }
   1301 
   1302 public:
   1303   DEFINE_MDNODE_GET(MDSubprogram,
   1304                     (MDScopeRef Scope, StringRef Name, StringRef LinkageName,
   1305                      MDFile *File, unsigned Line, MDSubroutineType *Type,
   1306                      bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
   1307                      MDTypeRef ContainingType, unsigned Virtuality,
   1308                      unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
   1309                      Constant *Function = nullptr,
   1310                      MDTemplateParameterArray TemplateParams = nullptr,
   1311                      MDSubprogram *Declaration = nullptr,
   1312                      MDLocalVariableArray Variables = nullptr),
   1313                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
   1314                      IsDefinition, ScopeLine, ContainingType, Virtuality,
   1315                      VirtualIndex, Flags, IsOptimized, Function, TemplateParams,
   1316                      Declaration, Variables))
   1317   DEFINE_MDNODE_GET(
   1318       MDSubprogram,
   1319       (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
   1320        unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
   1321        unsigned ScopeLine, Metadata *ContainingType, unsigned Virtuality,
   1322        unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
   1323        Metadata *Function = nullptr, Metadata *TemplateParams = nullptr,
   1324        Metadata *Declaration = nullptr, Metadata *Variables = nullptr),
   1325       (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
   1326        ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
   1327        Function, TemplateParams, Declaration, Variables))
   1328 
   1329   TempMDSubprogram clone() const { return cloneImpl(); }
   1330 
   1331 public:
   1332   unsigned getLine() const { return Line; }
   1333   unsigned getVirtuality() const { return Virtuality; }
   1334   unsigned getVirtualIndex() const { return VirtualIndex; }
   1335   unsigned getScopeLine() const { return ScopeLine; }
   1336   unsigned getFlags() const { return Flags; }
   1337   bool isLocalToUnit() const { return IsLocalToUnit; }
   1338   bool isDefinition() const { return IsDefinition; }
   1339   bool isOptimized() const { return IsOptimized; }
   1340 
   1341   unsigned isArtificial() const { return getFlags() & FlagArtificial; }
   1342   bool isPrivate() const {
   1343     return (getFlags() & FlagAccessibility) == FlagPrivate;
   1344   }
   1345   bool isProtected() const {
   1346     return (getFlags() & FlagAccessibility) == FlagProtected;
   1347   }
   1348   bool isPublic() const {
   1349     return (getFlags() & FlagAccessibility) == FlagPublic;
   1350   }
   1351   bool isExplicit() const { return getFlags() & FlagExplicit; }
   1352   bool isPrototyped() const { return getFlags() & FlagPrototyped; }
   1353 
   1354   /// \brief Check if this is reference-qualified.
   1355   ///
   1356   /// Return true if this subprogram is a C++11 reference-qualified non-static
   1357   /// member function (void foo() &).
   1358   unsigned isLValueReference() const {
   1359     return getFlags() & FlagLValueReference;
   1360   }
   1361 
   1362   /// \brief Check if this is rvalue-reference-qualified.
   1363   ///
   1364   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
   1365   /// non-static member function (void foo() &&).
   1366   unsigned isRValueReference() const {
   1367     return getFlags() & FlagRValueReference;
   1368   }
   1369 
   1370   MDScopeRef getScope() const { return MDScopeRef(getRawScope()); }
   1371 
   1372   StringRef getName() const { return getStringOperand(2); }
   1373   StringRef getDisplayName() const { return getStringOperand(3); }
   1374   StringRef getLinkageName() const { return getStringOperand(4); }
   1375 
   1376   MDString *getRawName() const { return getOperandAs<MDString>(2); }
   1377   MDString *getRawLinkageName() const { return getOperandAs<MDString>(4); }
   1378 
   1379   MDSubroutineType *getType() const {
   1380     return cast_or_null<MDSubroutineType>(getRawType());
   1381   }
   1382   MDTypeRef getContainingType() const {
   1383     return MDTypeRef(getRawContainingType());
   1384   }
   1385 
   1386   Constant *getFunctionConstant() const {
   1387     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawFunction()))
   1388       return C->getValue();
   1389     return nullptr;
   1390   }
   1391   MDTemplateParameterArray getTemplateParams() const {
   1392     return cast_or_null<MDTuple>(getRawTemplateParams());
   1393   }
   1394   MDSubprogram *getDeclaration() const {
   1395     return cast_or_null<MDSubprogram>(getRawDeclaration());
   1396   }
   1397   MDLocalVariableArray getVariables() const {
   1398     return cast_or_null<MDTuple>(getRawVariables());
   1399   }
   1400 
   1401   Metadata *getRawScope() const { return getOperand(1); }
   1402   Metadata *getRawType() const { return getOperand(5); }
   1403   Metadata *getRawContainingType() const { return getOperand(6); }
   1404   Metadata *getRawFunction() const { return getOperand(7); }
   1405   Metadata *getRawTemplateParams() const { return getOperand(8); }
   1406   Metadata *getRawDeclaration() const { return getOperand(9); }
   1407   Metadata *getRawVariables() const { return getOperand(10); }
   1408 
   1409   /// \brief Get a pointer to the function this subprogram describes.
   1410   ///
   1411   /// This dyn_casts \a getFunctionConstant() to \a Function.
   1412   ///
   1413   /// FIXME: Should this be looking through bitcasts?
   1414   Function *getFunction() const;
   1415 
   1416   /// \brief Replace the function.
   1417   ///
   1418   /// If \a isUniqued() and not \a isResolved(), this could node will be
   1419   /// RAUW'ed and deleted out from under the caller.  Use a \a TrackingMDRef if
   1420   /// that's a problem.
   1421   /// @{
   1422   void replaceFunction(Function *F);
   1423   void replaceFunction(ConstantAsMetadata *MD) { replaceOperandWith(7, MD); }
   1424   void replaceFunction(std::nullptr_t) { replaceOperandWith(7, nullptr); }
   1425   /// @}
   1426 
   1427   /// \brief Check if this subprogram decribes the given function.
   1428   ///
   1429   /// FIXME: Should this be looking through bitcasts?
   1430   bool describes(const Function *F) const;
   1431 
   1432   static bool classof(const Metadata *MD) {
   1433     return MD->getMetadataID() == MDSubprogramKind;
   1434   }
   1435 };
   1436 
   1437 class MDLexicalBlockBase : public MDLocalScope {
   1438 protected:
   1439   MDLexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
   1440                      ArrayRef<Metadata *> Ops)
   1441       : MDLocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {}
   1442   ~MDLexicalBlockBase() = default;
   1443 
   1444 public:
   1445   MDLocalScope *getScope() const { return cast<MDLocalScope>(getRawScope()); }
   1446 
   1447   Metadata *getRawScope() const { return getOperand(1); }
   1448 
   1449   /// \brief Forwarding accessors to LexicalBlock.
   1450   ///
   1451   /// TODO: Remove these and update code to use \a MDLexicalBlock directly.
   1452   /// @{
   1453   inline unsigned getLine() const;
   1454   inline unsigned getColumn() const;
   1455   /// @}
   1456   static bool classof(const Metadata *MD) {
   1457     return MD->getMetadataID() == MDLexicalBlockKind ||
   1458            MD->getMetadataID() == MDLexicalBlockFileKind;
   1459   }
   1460 };
   1461 
   1462 class MDLexicalBlock : public MDLexicalBlockBase {
   1463   friend class LLVMContextImpl;
   1464   friend class MDNode;
   1465 
   1466   unsigned Line;
   1467   unsigned Column;
   1468 
   1469   MDLexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
   1470                  unsigned Column, ArrayRef<Metadata *> Ops)
   1471       : MDLexicalBlockBase(C, MDLexicalBlockKind, Storage, Ops), Line(Line),
   1472         Column(Column) {}
   1473   ~MDLexicalBlock() = default;
   1474 
   1475   static MDLexicalBlock *getImpl(LLVMContext &Context, MDLocalScope *Scope,
   1476                                  MDFile *File, unsigned Line, unsigned Column,
   1477                                  StorageType Storage,
   1478                                  bool ShouldCreate = true) {
   1479     return getImpl(Context, static_cast<Metadata *>(Scope),
   1480                    static_cast<Metadata *>(File), Line, Column, Storage,
   1481                    ShouldCreate);
   1482   }
   1483 
   1484   static MDLexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
   1485                                  Metadata *File, unsigned Line, unsigned Column,
   1486                                  StorageType Storage, bool ShouldCreate = true);
   1487 
   1488   TempMDLexicalBlock cloneImpl() const {
   1489     return getTemporary(getContext(), getScope(), getFile(), getLine(),
   1490                         getColumn());
   1491   }
   1492 
   1493 public:
   1494   DEFINE_MDNODE_GET(MDLexicalBlock, (MDLocalScope * Scope, MDFile *File,
   1495                                      unsigned Line, unsigned Column),
   1496                     (Scope, File, Line, Column))
   1497   DEFINE_MDNODE_GET(MDLexicalBlock, (Metadata * Scope, Metadata *File,
   1498                                      unsigned Line, unsigned Column),
   1499                     (Scope, File, Line, Column))
   1500 
   1501   TempMDLexicalBlock clone() const { return cloneImpl(); }
   1502 
   1503   unsigned getLine() const { return Line; }
   1504   unsigned getColumn() const { return Column; }
   1505 
   1506   static bool classof(const Metadata *MD) {
   1507     return MD->getMetadataID() == MDLexicalBlockKind;
   1508   }
   1509 };
   1510 
   1511 unsigned MDLexicalBlockBase::getLine() const {
   1512   if (auto *N = dyn_cast<MDLexicalBlock>(this))
   1513     return N->getLine();
   1514   return 0;
   1515 }
   1516 
   1517 unsigned MDLexicalBlockBase::getColumn() const {
   1518   if (auto *N = dyn_cast<MDLexicalBlock>(this))
   1519     return N->getColumn();
   1520   return 0;
   1521 }
   1522 
   1523 class MDLexicalBlockFile : public MDLexicalBlockBase {
   1524   friend class LLVMContextImpl;
   1525   friend class MDNode;
   1526 
   1527   unsigned Discriminator;
   1528 
   1529   MDLexicalBlockFile(LLVMContext &C, StorageType Storage,
   1530                      unsigned Discriminator, ArrayRef<Metadata *> Ops)
   1531       : MDLexicalBlockBase(C, MDLexicalBlockFileKind, Storage, Ops),
   1532         Discriminator(Discriminator) {}
   1533   ~MDLexicalBlockFile() = default;
   1534 
   1535   static MDLexicalBlockFile *getImpl(LLVMContext &Context, MDLocalScope *Scope,
   1536                                      MDFile *File, unsigned Discriminator,
   1537                                      StorageType Storage,
   1538                                      bool ShouldCreate = true) {
   1539     return getImpl(Context, static_cast<Metadata *>(Scope),
   1540                    static_cast<Metadata *>(File), Discriminator, Storage,
   1541                    ShouldCreate);
   1542   }
   1543 
   1544   static MDLexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
   1545                                      Metadata *File, unsigned Discriminator,
   1546                                      StorageType Storage,
   1547                                      bool ShouldCreate = true);
   1548 
   1549   TempMDLexicalBlockFile cloneImpl() const {
   1550     return getTemporary(getContext(), getScope(), getFile(),
   1551                         getDiscriminator());
   1552   }
   1553 
   1554 public:
   1555   DEFINE_MDNODE_GET(MDLexicalBlockFile, (MDLocalScope * Scope, MDFile *File,
   1556                                          unsigned Discriminator),
   1557                     (Scope, File, Discriminator))
   1558   DEFINE_MDNODE_GET(MDLexicalBlockFile,
   1559                     (Metadata * Scope, Metadata *File, unsigned Discriminator),
   1560                     (Scope, File, Discriminator))
   1561 
   1562   TempMDLexicalBlockFile clone() const { return cloneImpl(); }
   1563 
   1564   // TODO: Remove these once they're gone from MDLexicalBlockBase.
   1565   unsigned getLine() const = delete;
   1566   unsigned getColumn() const = delete;
   1567 
   1568   unsigned getDiscriminator() const { return Discriminator; }
   1569 
   1570   static bool classof(const Metadata *MD) {
   1571     return MD->getMetadataID() == MDLexicalBlockFileKind;
   1572   }
   1573 };
   1574 
   1575 unsigned MDLocation::getDiscriminator() const {
   1576   if (auto *F = dyn_cast<MDLexicalBlockFile>(getScope()))
   1577     return F->getDiscriminator();
   1578   return 0;
   1579 }
   1580 
   1581 class MDNamespace : public MDScope {
   1582   friend class LLVMContextImpl;
   1583   friend class MDNode;
   1584 
   1585   unsigned Line;
   1586 
   1587   MDNamespace(LLVMContext &Context, StorageType Storage, unsigned Line,
   1588               ArrayRef<Metadata *> Ops)
   1589       : MDScope(Context, MDNamespaceKind, Storage, dwarf::DW_TAG_namespace,
   1590                 Ops),
   1591         Line(Line) {}
   1592   ~MDNamespace() = default;
   1593 
   1594   static MDNamespace *getImpl(LLVMContext &Context, MDScope *Scope,
   1595                               MDFile *File, StringRef Name, unsigned Line,
   1596                               StorageType Storage, bool ShouldCreate = true) {
   1597     return getImpl(Context, Scope, File, getCanonicalMDString(Context, Name),
   1598                    Line, Storage, ShouldCreate);
   1599   }
   1600   static MDNamespace *getImpl(LLVMContext &Context, Metadata *Scope,
   1601                               Metadata *File, MDString *Name, unsigned Line,
   1602                               StorageType Storage, bool ShouldCreate = true);
   1603 
   1604   TempMDNamespace cloneImpl() const {
   1605     return getTemporary(getContext(), getScope(), getFile(), getName(),
   1606                         getLine());
   1607   }
   1608 
   1609 public:
   1610   DEFINE_MDNODE_GET(MDNamespace, (MDScope * Scope, MDFile *File, StringRef Name,
   1611                                   unsigned Line),
   1612                     (Scope, File, Name, Line))
   1613   DEFINE_MDNODE_GET(MDNamespace, (Metadata * Scope, Metadata *File,
   1614                                   MDString *Name, unsigned Line),
   1615                     (Scope, File, Name, Line))
   1616 
   1617   TempMDNamespace clone() const { return cloneImpl(); }
   1618 
   1619   unsigned getLine() const { return Line; }
   1620   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
   1621   StringRef getName() const { return getStringOperand(2); }
   1622 
   1623   Metadata *getRawScope() const { return getOperand(1); }
   1624   MDString *getRawName() const { return getOperandAs<MDString>(2); }
   1625 
   1626   static bool classof(const Metadata *MD) {
   1627     return MD->getMetadataID() == MDNamespaceKind;
   1628   }
   1629 };
   1630 
   1631 /// \brief Base class for template parameters.
   1632 class MDTemplateParameter : public DebugNode {
   1633 protected:
   1634   MDTemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
   1635                       unsigned Tag, ArrayRef<Metadata *> Ops)
   1636       : DebugNode(Context, ID, Storage, Tag, Ops) {}
   1637   ~MDTemplateParameter() = default;
   1638 
   1639 public:
   1640   StringRef getName() const { return getStringOperand(0); }
   1641   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
   1642 
   1643   MDString *getRawName() const { return getOperandAs<MDString>(0); }
   1644   Metadata *getRawType() const { return getOperand(1); }
   1645 
   1646   static bool classof(const Metadata *MD) {
   1647     return MD->getMetadataID() == MDTemplateTypeParameterKind ||
   1648            MD->getMetadataID() == MDTemplateValueParameterKind;
   1649   }
   1650 };
   1651 
   1652 class MDTemplateTypeParameter : public MDTemplateParameter {
   1653   friend class LLVMContextImpl;
   1654   friend class MDNode;
   1655 
   1656   MDTemplateTypeParameter(LLVMContext &Context, StorageType Storage,
   1657                           ArrayRef<Metadata *> Ops)
   1658       : MDTemplateParameter(Context, MDTemplateTypeParameterKind, Storage,
   1659                             dwarf::DW_TAG_template_type_parameter, Ops) {}
   1660   ~MDTemplateTypeParameter() = default;
   1661 
   1662   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
   1663                                           MDTypeRef Type, StorageType Storage,
   1664                                           bool ShouldCreate = true) {
   1665     return getImpl(Context, getCanonicalMDString(Context, Name), Type, Storage,
   1666                    ShouldCreate);
   1667   }
   1668   static MDTemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
   1669                                           Metadata *Type, StorageType Storage,
   1670                                           bool ShouldCreate = true);
   1671 
   1672   TempMDTemplateTypeParameter cloneImpl() const {
   1673     return getTemporary(getContext(), getName(), getType());
   1674   }
   1675 
   1676 public:
   1677   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (StringRef Name, MDTypeRef Type),
   1678                     (Name, Type))
   1679   DEFINE_MDNODE_GET(MDTemplateTypeParameter, (MDString * Name, Metadata *Type),
   1680                     (Name, Type))
   1681 
   1682   TempMDTemplateTypeParameter clone() const { return cloneImpl(); }
   1683 
   1684   static bool classof(const Metadata *MD) {
   1685     return MD->getMetadataID() == MDTemplateTypeParameterKind;
   1686   }
   1687 };
   1688 
   1689 class MDTemplateValueParameter : public MDTemplateParameter {
   1690   friend class LLVMContextImpl;
   1691   friend class MDNode;
   1692 
   1693   MDTemplateValueParameter(LLVMContext &Context, StorageType Storage,
   1694                            unsigned Tag, ArrayRef<Metadata *> Ops)
   1695       : MDTemplateParameter(Context, MDTemplateValueParameterKind, Storage, Tag,
   1696                             Ops) {}
   1697   ~MDTemplateValueParameter() = default;
   1698 
   1699   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
   1700                                            StringRef Name, MDTypeRef Type,
   1701                                            Metadata *Value, StorageType Storage,
   1702                                            bool ShouldCreate = true) {
   1703     return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
   1704                    Value, Storage, ShouldCreate);
   1705   }
   1706   static MDTemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
   1707                                            MDString *Name, Metadata *Type,
   1708                                            Metadata *Value, StorageType Storage,
   1709                                            bool ShouldCreate = true);
   1710 
   1711   TempMDTemplateValueParameter cloneImpl() const {
   1712     return getTemporary(getContext(), getTag(), getName(), getType(),
   1713                         getValue());
   1714   }
   1715 
   1716 public:
   1717   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, StringRef Name,
   1718                                                MDTypeRef Type, Metadata *Value),
   1719                     (Tag, Name, Type, Value))
   1720   DEFINE_MDNODE_GET(MDTemplateValueParameter, (unsigned Tag, MDString *Name,
   1721                                                Metadata *Type, Metadata *Value),
   1722                     (Tag, Name, Type, Value))
   1723 
   1724   TempMDTemplateValueParameter clone() const { return cloneImpl(); }
   1725 
   1726   Metadata *getValue() const { return getOperand(2); }
   1727 
   1728   static bool classof(const Metadata *MD) {
   1729     return MD->getMetadataID() == MDTemplateValueParameterKind;
   1730   }
   1731 };
   1732 
   1733 /// \brief Base class for variables.
   1734 ///
   1735 /// TODO: Hardcode to DW_TAG_variable.
   1736 class MDVariable : public DebugNode {
   1737   unsigned Line;
   1738 
   1739 protected:
   1740   MDVariable(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
   1741              unsigned Line, ArrayRef<Metadata *> Ops)
   1742       : DebugNode(C, ID, Storage, Tag, Ops), Line(Line) {}
   1743   ~MDVariable() = default;
   1744 
   1745 public:
   1746   unsigned getLine() const { return Line; }
   1747   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
   1748   StringRef getName() const { return getStringOperand(1); }
   1749   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
   1750   MDTypeRef getType() const { return MDTypeRef(getRawType()); }
   1751 
   1752   StringRef getFilename() const {
   1753     if (auto *F = getFile())
   1754       return F->getFilename();
   1755     return "";
   1756   }
   1757   StringRef getDirectory() const {
   1758     if (auto *F = getFile())
   1759       return F->getDirectory();
   1760     return "";
   1761   }
   1762 
   1763   Metadata *getRawScope() const { return getOperand(0); }
   1764   MDString *getRawName() const { return getOperandAs<MDString>(1); }
   1765   Metadata *getRawFile() const { return getOperand(2); }
   1766   Metadata *getRawType() const { return getOperand(3); }
   1767 
   1768   static bool classof(const Metadata *MD) {
   1769     return MD->getMetadataID() == MDLocalVariableKind ||
   1770            MD->getMetadataID() == MDGlobalVariableKind;
   1771   }
   1772 };
   1773 
   1774 /// \brief Global variables.
   1775 ///
   1776 /// TODO: Remove DisplayName.  It's always equal to Name.
   1777 class MDGlobalVariable : public MDVariable {
   1778   friend class LLVMContextImpl;
   1779   friend class MDNode;
   1780 
   1781   bool IsLocalToUnit;
   1782   bool IsDefinition;
   1783 
   1784   MDGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
   1785                    bool IsLocalToUnit, bool IsDefinition,
   1786                    ArrayRef<Metadata *> Ops)
   1787       : MDVariable(C, MDGlobalVariableKind, Storage, dwarf::DW_TAG_variable,
   1788                    Line, Ops),
   1789         IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
   1790   ~MDGlobalVariable() = default;
   1791 
   1792   static MDGlobalVariable *
   1793   getImpl(LLVMContext &Context, MDScope *Scope, StringRef Name,
   1794           StringRef LinkageName, MDFile *File, unsigned Line, MDTypeRef Type,
   1795           bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
   1796           MDDerivedType *StaticDataMemberDeclaration, StorageType Storage,
   1797           bool ShouldCreate = true) {
   1798     return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
   1799                    getCanonicalMDString(Context, LinkageName), File, Line, Type,
   1800                    IsLocalToUnit, IsDefinition,
   1801                    Variable ? ConstantAsMetadata::get(Variable) : nullptr,
   1802                    StaticDataMemberDeclaration, Storage, ShouldCreate);
   1803   }
   1804   static MDGlobalVariable *
   1805   getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
   1806           MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
   1807           bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
   1808           Metadata *StaticDataMemberDeclaration, StorageType Storage,
   1809           bool ShouldCreate = true);
   1810 
   1811   TempMDGlobalVariable cloneImpl() const {
   1812     return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
   1813                         getFile(), getLine(), getType(), isLocalToUnit(),
   1814                         isDefinition(), getVariable(),
   1815                         getStaticDataMemberDeclaration());
   1816   }
   1817 
   1818 public:
   1819   DEFINE_MDNODE_GET(MDGlobalVariable,
   1820                     (MDScope * Scope, StringRef Name, StringRef LinkageName,
   1821                      MDFile *File, unsigned Line, MDTypeRef Type,
   1822                      bool IsLocalToUnit, bool IsDefinition, Constant *Variable,
   1823                      MDDerivedType *StaticDataMemberDeclaration),
   1824                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
   1825                      IsDefinition, Variable, StaticDataMemberDeclaration))
   1826   DEFINE_MDNODE_GET(MDGlobalVariable,
   1827                     (Metadata * Scope, MDString *Name, MDString *LinkageName,
   1828                      Metadata *File, unsigned Line, Metadata *Type,
   1829                      bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
   1830                      Metadata *StaticDataMemberDeclaration),
   1831                     (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
   1832                      IsDefinition, Variable, StaticDataMemberDeclaration))
   1833 
   1834   TempMDGlobalVariable clone() const { return cloneImpl(); }
   1835 
   1836   bool isLocalToUnit() const { return IsLocalToUnit; }
   1837   bool isDefinition() const { return IsDefinition; }
   1838   StringRef getDisplayName() const { return getStringOperand(4); }
   1839   StringRef getLinkageName() const { return getStringOperand(5); }
   1840   Constant *getVariable() const {
   1841     if (auto *C = cast_or_null<ConstantAsMetadata>(getRawVariable()))
   1842       return dyn_cast<Constant>(C->getValue());
   1843     return nullptr;
   1844   }
   1845   MDDerivedType *getStaticDataMemberDeclaration() const {
   1846     return cast_or_null<MDDerivedType>(getRawStaticDataMemberDeclaration());
   1847   }
   1848 
   1849   MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
   1850   Metadata *getRawVariable() const { return getOperand(6); }
   1851   Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(7); }
   1852 
   1853   static bool classof(const Metadata *MD) {
   1854     return MD->getMetadataID() == MDGlobalVariableKind;
   1855   }
   1856 };
   1857 
   1858 /// \brief Local variable.
   1859 ///
   1860 /// TODO: Split between arguments and otherwise.
   1861 /// TODO: Use \c DW_TAG_variable instead of fake tags.
   1862 /// TODO: Split up flags.
   1863 class MDLocalVariable : public MDVariable {
   1864   friend class LLVMContextImpl;
   1865   friend class MDNode;
   1866 
   1867   unsigned Arg;
   1868   unsigned Flags;
   1869 
   1870   MDLocalVariable(LLVMContext &C, StorageType Storage, unsigned Tag,
   1871                   unsigned Line, unsigned Arg, unsigned Flags,
   1872                   ArrayRef<Metadata *> Ops)
   1873       : MDVariable(C, MDLocalVariableKind, Storage, Tag, Line, Ops), Arg(Arg),
   1874         Flags(Flags) {}
   1875   ~MDLocalVariable() = default;
   1876 
   1877   static MDLocalVariable *getImpl(LLVMContext &Context, unsigned Tag,
   1878                                   MDScope *Scope, StringRef Name, MDFile *File,
   1879                                   unsigned Line, MDTypeRef Type, unsigned Arg,
   1880                                   unsigned Flags, StorageType Storage,
   1881                                   bool ShouldCreate = true) {
   1882     return getImpl(Context, Tag, Scope, getCanonicalMDString(Context, Name),
   1883                    File, Line, Type, Arg, Flags, Storage, ShouldCreate);
   1884   }
   1885   static MDLocalVariable *
   1886   getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
   1887           Metadata *File, unsigned Line, Metadata *Type, unsigned Arg,
   1888           unsigned Flags, StorageType Storage, bool ShouldCreate = true);
   1889 
   1890   TempMDLocalVariable cloneImpl() const {
   1891     return getTemporary(getContext(), getTag(), getScope(), getName(),
   1892                         getFile(), getLine(), getType(), getArg(), getFlags());
   1893   }
   1894 
   1895 public:
   1896   DEFINE_MDNODE_GET(MDLocalVariable,
   1897                     (unsigned Tag, MDLocalScope *Scope, StringRef Name,
   1898                      MDFile *File, unsigned Line, MDTypeRef Type, unsigned Arg,
   1899                      unsigned Flags),
   1900                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
   1901   DEFINE_MDNODE_GET(MDLocalVariable,
   1902                     (unsigned Tag, Metadata *Scope, MDString *Name,
   1903                      Metadata *File, unsigned Line, Metadata *Type,
   1904                      unsigned Arg, unsigned Flags),
   1905                     (Tag, Scope, Name, File, Line, Type, Arg, Flags))
   1906 
   1907   TempMDLocalVariable clone() const { return cloneImpl(); }
   1908 
   1909   /// \brief Get the local scope for this variable.
   1910   ///
   1911   /// Variables must be defined in a local scope.
   1912   MDLocalScope *getScope() const {
   1913     return cast<MDLocalScope>(MDVariable::getScope());
   1914   }
   1915 
   1916   unsigned getArg() const { return Arg; }
   1917   unsigned getFlags() const { return Flags; }
   1918 
   1919   bool isArtificial() const { return getFlags() & FlagArtificial; }
   1920   bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
   1921 
   1922   /// \brief Check that a location is valid for this variable.
   1923   ///
   1924   /// Check that \c DL exists, is in the same subprogram, and has the same
   1925   /// inlined-at location as \c this.  (Otherwise, it's not a valid attachemnt
   1926   /// to a \a DbgInfoIntrinsic.)
   1927   bool isValidLocationForIntrinsic(const MDLocation *DL) const {
   1928     return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
   1929   }
   1930 
   1931   static bool classof(const Metadata *MD) {
   1932     return MD->getMetadataID() == MDLocalVariableKind;
   1933   }
   1934 };
   1935 
   1936 /// \brief DWARF expression.
   1937 ///
   1938 /// This is (almost) a DWARF expression that modifies the location of a
   1939 /// variable or (or the location of a single piece of a variable).
   1940 ///
   1941 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
   1942 /// and have DW_OP_plus consume the topmost elements on the stack.
   1943 ///
   1944 /// TODO: Co-allocate the expression elements.
   1945 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
   1946 /// storage types.
   1947 class MDExpression : public MDNode {
   1948   friend class LLVMContextImpl;
   1949   friend class MDNode;
   1950 
   1951   std::vector<uint64_t> Elements;
   1952 
   1953   MDExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
   1954       : MDNode(C, MDExpressionKind, Storage, None),
   1955         Elements(Elements.begin(), Elements.end()) {}
   1956   ~MDExpression() = default;
   1957 
   1958   static MDExpression *getImpl(LLVMContext &Context,
   1959                                ArrayRef<uint64_t> Elements, StorageType Storage,
   1960                                bool ShouldCreate = true);
   1961 
   1962   TempMDExpression cloneImpl() const {
   1963     return getTemporary(getContext(), getElements());
   1964   }
   1965 
   1966 public:
   1967   DEFINE_MDNODE_GET(MDExpression, (ArrayRef<uint64_t> Elements), (Elements))
   1968 
   1969   TempMDExpression clone() const { return cloneImpl(); }
   1970 
   1971   ArrayRef<uint64_t> getElements() const { return Elements; }
   1972 
   1973   unsigned getNumElements() const { return Elements.size(); }
   1974   uint64_t getElement(unsigned I) const {
   1975     assert(I < Elements.size() && "Index out of range");
   1976     return Elements[I];
   1977   }
   1978 
   1979   /// \brief Return whether this is a piece of an aggregate variable.
   1980   bool isBitPiece() const;
   1981 
   1982   /// \brief Return the offset of this piece in bits.
   1983   uint64_t getBitPieceOffset() const;
   1984 
   1985   /// \brief Return the size of this piece in bits.
   1986   uint64_t getBitPieceSize() const;
   1987 
   1988   typedef ArrayRef<uint64_t>::iterator element_iterator;
   1989   element_iterator elements_begin() const { return getElements().begin(); }
   1990   element_iterator elements_end() const { return getElements().end(); }
   1991 
   1992   /// \brief A lightweight wrapper around an expression operand.
   1993   ///
   1994   /// TODO: Store arguments directly and change \a MDExpression to store a
   1995   /// range of these.
   1996   class ExprOperand {
   1997     const uint64_t *Op;
   1998 
   1999   public:
   2000     explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
   2001 
   2002     const uint64_t *get() const { return Op; }
   2003 
   2004     /// \brief Get the operand code.
   2005     uint64_t getOp() const { return *Op; }
   2006 
   2007     /// \brief Get an argument to the operand.
   2008     ///
   2009     /// Never returns the operand itself.
   2010     uint64_t getArg(unsigned I) const { return Op[I + 1]; }
   2011 
   2012     unsigned getNumArgs() const { return getSize() - 1; }
   2013 
   2014     /// \brief Return the size of the operand.
   2015     ///
   2016     /// Return the number of elements in the operand (1 + args).
   2017     unsigned getSize() const;
   2018   };
   2019 
   2020   /// \brief An iterator for expression operands.
   2021   class expr_op_iterator
   2022       : public std::iterator<std::input_iterator_tag, ExprOperand> {
   2023     ExprOperand Op;
   2024 
   2025   public:
   2026     explicit expr_op_iterator(element_iterator I) : Op(I) {}
   2027 
   2028     element_iterator getBase() const { return Op.get(); }
   2029     const ExprOperand &operator*() const { return Op; }
   2030     const ExprOperand *operator->() const { return &Op; }
   2031 
   2032     expr_op_iterator &operator++() {
   2033       increment();
   2034       return *this;
   2035     }
   2036     expr_op_iterator operator++(int) {
   2037       expr_op_iterator T(*this);
   2038       increment();
   2039       return T;
   2040     }
   2041 
   2042     /// \brief Get the next iterator.
   2043     ///
   2044     /// \a std::next() doesn't work because this is technically an
   2045     /// input_iterator, but it's a perfectly valid operation.  This is an
   2046     /// accessor to provide the same functionality.
   2047     expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
   2048 
   2049     bool operator==(const expr_op_iterator &X) const {
   2050       return getBase() == X.getBase();
   2051     }
   2052     bool operator!=(const expr_op_iterator &X) const {
   2053       return getBase() != X.getBase();
   2054     }
   2055 
   2056   private:
   2057     void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
   2058   };
   2059 
   2060   /// \brief Visit the elements via ExprOperand wrappers.
   2061   ///
   2062   /// These range iterators visit elements through \a ExprOperand wrappers.
   2063   /// This is not guaranteed to be a valid range unless \a isValid() gives \c
   2064   /// true.
   2065   ///
   2066   /// \pre \a isValid() gives \c true.
   2067   /// @{
   2068   expr_op_iterator expr_op_begin() const {
   2069     return expr_op_iterator(elements_begin());
   2070   }
   2071   expr_op_iterator expr_op_end() const {
   2072     return expr_op_iterator(elements_end());
   2073   }
   2074   /// @}
   2075 
   2076   bool isValid() const;
   2077 
   2078   static bool classof(const Metadata *MD) {
   2079     return MD->getMetadataID() == MDExpressionKind;
   2080   }
   2081 };
   2082 
   2083 class MDObjCProperty : public DebugNode {
   2084   friend class LLVMContextImpl;
   2085   friend class MDNode;
   2086 
   2087   unsigned Line;
   2088   unsigned Attributes;
   2089 
   2090   MDObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
   2091                  unsigned Attributes, ArrayRef<Metadata *> Ops)
   2092       : DebugNode(C, MDObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property,
   2093                   Ops),
   2094         Line(Line), Attributes(Attributes) {}
   2095   ~MDObjCProperty() = default;
   2096 
   2097   static MDObjCProperty *
   2098   getImpl(LLVMContext &Context, StringRef Name, MDFile *File, unsigned Line,
   2099           StringRef GetterName, StringRef SetterName, unsigned Attributes,
   2100           MDType *Type, StorageType Storage, bool ShouldCreate = true) {
   2101     return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
   2102                    getCanonicalMDString(Context, GetterName),
   2103                    getCanonicalMDString(Context, SetterName), Attributes, Type,
   2104                    Storage, ShouldCreate);
   2105   }
   2106   static MDObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
   2107                                  Metadata *File, unsigned Line,
   2108                                  MDString *GetterName, MDString *SetterName,
   2109                                  unsigned Attributes, Metadata *Type,
   2110                                  StorageType Storage, bool ShouldCreate = true);
   2111 
   2112   TempMDObjCProperty cloneImpl() const {
   2113     return getTemporary(getContext(), getName(), getFile(), getLine(),
   2114                         getGetterName(), getSetterName(), getAttributes(),
   2115                         getType());
   2116   }
   2117 
   2118 public:
   2119   DEFINE_MDNODE_GET(MDObjCProperty,
   2120                     (StringRef Name, MDFile *File, unsigned Line,
   2121                      StringRef GetterName, StringRef SetterName,
   2122                      unsigned Attributes, MDType *Type),
   2123                     (Name, File, Line, GetterName, SetterName, Attributes,
   2124                      Type))
   2125   DEFINE_MDNODE_GET(MDObjCProperty,
   2126                     (MDString * Name, Metadata *File, unsigned Line,
   2127                      MDString *GetterName, MDString *SetterName,
   2128                      unsigned Attributes, Metadata *Type),
   2129                     (Name, File, Line, GetterName, SetterName, Attributes,
   2130                      Type))
   2131 
   2132   TempMDObjCProperty clone() const { return cloneImpl(); }
   2133 
   2134   unsigned getLine() const { return Line; }
   2135   unsigned getAttributes() const { return Attributes; }
   2136   StringRef getName() const { return getStringOperand(0); }
   2137   MDFile *getFile() const { return cast_or_null<MDFile>(getRawFile()); }
   2138   StringRef getGetterName() const { return getStringOperand(2); }
   2139   StringRef getSetterName() const { return getStringOperand(3); }
   2140 
   2141   /// \brief Get the type.
   2142   ///
   2143   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
   2144   /// the type as a DITypeRef here.
   2145   MDType *getType() const { return cast_or_null<MDType>(getRawType()); }
   2146 
   2147   StringRef getFilename() const {
   2148     if (auto *F = getFile())
   2149       return F->getFilename();
   2150     return "";
   2151   }
   2152   StringRef getDirectory() const {
   2153     if (auto *F = getFile())
   2154       return F->getDirectory();
   2155     return "";
   2156   }
   2157 
   2158   MDString *getRawName() const { return getOperandAs<MDString>(0); }
   2159   Metadata *getRawFile() const { return getOperand(1); }
   2160   MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
   2161   MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
   2162   Metadata *getRawType() const { return getOperand(4); }
   2163 
   2164   static bool classof(const Metadata *MD) {
   2165     return MD->getMetadataID() == MDObjCPropertyKind;
   2166   }
   2167 };
   2168 
   2169 /// \brief An imported module (C++ using directive or similar).
   2170 class MDImportedEntity : public DebugNode {
   2171   friend class LLVMContextImpl;
   2172   friend class MDNode;
   2173 
   2174   unsigned Line;
   2175 
   2176   MDImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
   2177                    unsigned Line, ArrayRef<Metadata *> Ops)
   2178       : DebugNode(C, MDImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
   2179   ~MDImportedEntity() = default;
   2180 
   2181   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
   2182                                    MDScope *Scope, DebugNodeRef Entity,
   2183                                    unsigned Line, StringRef Name,
   2184                                    StorageType Storage,
   2185                                    bool ShouldCreate = true) {
   2186     return getImpl(Context, Tag, Scope, Entity, Line,
   2187                    getCanonicalMDString(Context, Name), Storage, ShouldCreate);
   2188   }
   2189   static MDImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
   2190                                    Metadata *Scope, Metadata *Entity,
   2191                                    unsigned Line, MDString *Name,
   2192                                    StorageType Storage,
   2193                                    bool ShouldCreate = true);
   2194 
   2195   TempMDImportedEntity cloneImpl() const {
   2196     return getTemporary(getContext(), getTag(), getScope(), getEntity(),
   2197                         getLine(), getName());
   2198   }
   2199 
   2200 public:
   2201   DEFINE_MDNODE_GET(MDImportedEntity,
   2202                     (unsigned Tag, MDScope *Scope, DebugNodeRef Entity,
   2203                      unsigned Line, StringRef Name = ""),
   2204                     (Tag, Scope, Entity, Line, Name))
   2205   DEFINE_MDNODE_GET(MDImportedEntity,
   2206                     (unsigned Tag, Metadata *Scope, Metadata *Entity,
   2207                      unsigned Line, MDString *Name),
   2208                     (Tag, Scope, Entity, Line, Name))
   2209 
   2210   TempMDImportedEntity clone() const { return cloneImpl(); }
   2211 
   2212   unsigned getLine() const { return Line; }
   2213   MDScope *getScope() const { return cast_or_null<MDScope>(getRawScope()); }
   2214   DebugNodeRef getEntity() const { return DebugNodeRef(getRawEntity()); }
   2215   StringRef getName() const { return getStringOperand(2); }
   2216 
   2217   Metadata *getRawScope() const { return getOperand(0); }
   2218   Metadata *getRawEntity() const { return getOperand(1); }
   2219   MDString *getRawName() const { return getOperandAs<MDString>(2); }
   2220 
   2221   static bool classof(const Metadata *MD) {
   2222     return MD->getMetadataID() == MDImportedEntityKind;
   2223   }
   2224 };
   2225 
   2226 } // end namespace llvm
   2227 
   2228 #undef DEFINE_MDNODE_GET_UNPACK_IMPL
   2229 #undef DEFINE_MDNODE_GET_UNPACK
   2230 #undef DEFINE_MDNODE_GET
   2231 
   2232 #endif
   2233