LLVM API Documentation

DebugInfo.h
Go to the documentation of this file.
00001 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a bunch of datatypes that are useful for creating and
00011 // walking debug info in LLVM IR form. They essentially provide wrappers around
00012 // the information in the global variables that's needed when constructing the
00013 // DWARF information.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_IR_DEBUGINFO_H
00018 #define LLVM_IR_DEBUGINFO_H
00019 
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/SmallPtrSet.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/StringRef.h"
00024 #include "llvm/ADT/iterator_range.h"
00025 #include "llvm/IR/DebugInfoMetadata.h"
00026 #include "llvm/Support/Casting.h"
00027 #include "llvm/Support/Dwarf.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include <iterator>
00030 
00031 namespace llvm {
00032 class BasicBlock;
00033 class Constant;
00034 class Function;
00035 class GlobalVariable;
00036 class Module;
00037 class Type;
00038 class Value;
00039 class DbgDeclareInst;
00040 class DbgValueInst;
00041 class Instruction;
00042 class Metadata;
00043 class MDNode;
00044 class MDString;
00045 class NamedMDNode;
00046 class LLVMContext;
00047 class raw_ostream;
00048 
00049 class DIFile;
00050 class DISubprogram;
00051 class DILexicalBlock;
00052 class DILexicalBlockFile;
00053 class DIVariable;
00054 class DIType;
00055 class DIScope;
00056 class DIObjCProperty;
00057 
00058 /// \brief Maps from type identifier to the actual MDNode.
00059 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
00060 
00061 class DIHeaderFieldIterator
00062     : public std::iterator<std::input_iterator_tag, StringRef, std::ptrdiff_t,
00063                            const StringRef *, StringRef> {
00064   StringRef Header;
00065   StringRef Current;
00066 
00067 public:
00068   DIHeaderFieldIterator() {}
00069   explicit DIHeaderFieldIterator(StringRef Header)
00070       : Header(Header), Current(Header.slice(0, Header.find('\0'))) {}
00071   StringRef operator*() const { return Current; }
00072   const StringRef *operator->() const { return &Current; }
00073   DIHeaderFieldIterator &operator++() {
00074     increment();
00075     return *this;
00076   }
00077   DIHeaderFieldIterator operator++(int) {
00078     DIHeaderFieldIterator X(*this);
00079     increment();
00080     return X;
00081   }
00082   bool operator==(const DIHeaderFieldIterator &X) const {
00083     return Current.data() == X.Current.data();
00084   }
00085   bool operator!=(const DIHeaderFieldIterator &X) const {
00086     return !(*this == X);
00087   }
00088 
00089   StringRef getHeader() const { return Header; }
00090   StringRef getCurrent() const { return Current; }
00091   StringRef getPrefix() const {
00092     if (Current.begin() == Header.begin())
00093       return StringRef();
00094     return Header.slice(0, Current.begin() - Header.begin() - 1);
00095   }
00096   StringRef getSuffix() const {
00097     if (Current.end() == Header.end())
00098       return StringRef();
00099     return Header.slice(Current.end() - Header.begin() + 1, StringRef::npos);
00100   }
00101 
00102   /// \brief Get the current field as a number.
00103   ///
00104   /// Convert the current field into a number.  Return \c 0 on error.
00105   template <class T> T getNumber() const {
00106     T Int;
00107     if (getCurrent().getAsInteger(0, Int))
00108       return 0;
00109     return Int;
00110   }
00111 
00112 private:
00113   void increment() {
00114     assert(Current.data() != nullptr && "Cannot increment past the end");
00115     StringRef Suffix = getSuffix();
00116     Current = Suffix.slice(0, Suffix.find('\0'));
00117   }
00118 };
00119 
00120 /// \brief A thin wraper around MDNode to access encoded debug info.
00121 ///
00122 /// This should not be stored in a container, because the underlying MDNode may
00123 /// change in certain situations.
00124 class DIDescriptor {
00125   // Befriends DIRef so DIRef can befriend the protected member
00126   // function: getFieldAs<DIRef>.
00127   template <typename T> friend class DIRef;
00128 
00129 public:
00130   /// \brief Accessibility flags.
00131   ///
00132   /// The three accessibility flags are mutually exclusive and rolled together
00133   /// in the first two bits.
00134   enum {
00135 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
00136 #include "llvm/IR/DebugInfoFlags.def"
00137     FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic
00138   };
00139 
00140   static unsigned getFlag(StringRef Flag);
00141   static const char *getFlagString(unsigned Flag);
00142 
00143   /// \brief Split up a flags bitfield.
00144   ///
00145   /// Split \c Flags into \c SplitFlags, a vector of its components.  Returns
00146   /// any remaining (unrecognized) bits.
00147   static unsigned splitFlags(unsigned Flags,
00148                              SmallVectorImpl<unsigned> &SplitFlags);
00149 
00150 protected:
00151   const MDNode *DbgNode;
00152 
00153   StringRef getStringField(unsigned Elt) const;
00154   unsigned getUnsignedField(unsigned Elt) const {
00155     return (unsigned)getUInt64Field(Elt);
00156   }
00157   uint64_t getUInt64Field(unsigned Elt) const;
00158   int64_t getInt64Field(unsigned Elt) const;
00159   DIDescriptor getDescriptorField(unsigned Elt) const;
00160 
00161   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
00162     return DescTy(getDescriptorField(Elt));
00163   }
00164 
00165   GlobalVariable *getGlobalVariableField(unsigned Elt) const;
00166   Constant *getConstantField(unsigned Elt) const;
00167   Function *getFunctionField(unsigned Elt) const;
00168 
00169 public:
00170   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
00171 
00172   bool Verify() const;
00173 
00174   MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
00175   operator MDNode *() const { return get(); }
00176   MDNode *operator->() const { return get(); }
00177 
00178   // An explicit operator bool so that we can do testing of DI values
00179   // easily.
00180   // FIXME: This operator bool isn't actually protecting anything at the
00181   // moment due to the conversion operator above making DIDescriptor nodes
00182   // implicitly convertable to bool.
00183   explicit operator bool() const { return DbgNode != nullptr; }
00184 
00185   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
00186   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
00187 
00188   StringRef getHeader() const { return getStringField(0); }
00189 
00190   size_t getNumHeaderFields() const {
00191     return std::distance(DIHeaderFieldIterator(getHeader()),
00192                          DIHeaderFieldIterator());
00193   }
00194 
00195   DIHeaderFieldIterator header_begin() const {
00196     return DIHeaderFieldIterator(getHeader());
00197   }
00198   DIHeaderFieldIterator header_end() const { return DIHeaderFieldIterator(); }
00199 
00200   DIHeaderFieldIterator getHeaderIterator(unsigned Index) const {
00201     // Since callers expect an empty string for out-of-range accesses, we can't
00202     // use std::advance() here.
00203     for (auto I = header_begin(), E = header_end(); I != E; ++I, --Index)
00204       if (!Index)
00205         return I;
00206     return header_end();
00207   }
00208 
00209   StringRef getHeaderField(unsigned Index) const {
00210     return *getHeaderIterator(Index);
00211   }
00212 
00213   template <class T> T getHeaderFieldAs(unsigned Index) const {
00214     return getHeaderIterator(Index).getNumber<T>();
00215   }
00216 
00217   uint16_t getTag() const {
00218     if (auto *N = dyn_cast_or_null<DebugNode>(get()))
00219       return N->getTag();
00220     return 0;
00221   }
00222 
00223   bool isDerivedType() const { return get() && isa<MDDerivedTypeBase>(get()); }
00224   bool isCompositeType() const {
00225     return get() && isa<MDCompositeTypeBase>(get());
00226   }
00227   bool isSubroutineType() const {
00228     return get() && isa<MDSubroutineType>(get());
00229   }
00230   bool isBasicType() const { return get() && isa<MDBasicType>(get()); }
00231   bool isVariable() const { return get() && isa<MDLocalVariable>(get()); }
00232   bool isSubprogram() const { return get() && isa<MDSubprogram>(get()); }
00233   bool isGlobalVariable() const {
00234     return get() && isa<MDGlobalVariable>(get());
00235   }
00236   bool isScope() const { return get() && isa<MDScope>(get()); }
00237   bool isFile() const { return get() && isa<MDFile>(get()); }
00238   bool isCompileUnit() const { return get() && isa<MDCompileUnit>(get()); }
00239   bool isNameSpace() const{ return get() && isa<MDNamespace>(get()); }
00240   bool isLexicalBlockFile() const {
00241     return get() && isa<MDLexicalBlockFile>(get());
00242   }
00243   bool isLexicalBlock() const {
00244     return get() && isa<MDLexicalBlockBase>(get());
00245   }
00246   bool isSubrange() const { return get() && isa<MDSubrange>(get()); }
00247   bool isEnumerator() const { return get() && isa<MDEnumerator>(get()); }
00248   bool isType() const { return get() && isa<MDType>(get()); }
00249   bool isTemplateTypeParameter() const {
00250     return get() && isa<MDTemplateTypeParameter>(get());
00251   }
00252   bool isTemplateValueParameter() const {
00253     return get() && isa<MDTemplateValueParameter>(get());
00254   }
00255   bool isObjCProperty() const { return get() && isa<MDObjCProperty>(get()); }
00256   bool isImportedEntity() const {
00257     return get() && isa<MDImportedEntity>(get());
00258   }
00259   bool isExpression() const { return get() && isa<MDExpression>(get()); }
00260 
00261   void print(raw_ostream &OS) const;
00262   void dump() const;
00263 
00264   /// \brief Replace all uses of debug info referenced by this descriptor.
00265   void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
00266   void replaceAllUsesWith(MDNode *D);
00267 };
00268 
00269 #define RETURN_FROM_RAW(VALID, DEFAULT)                                        \
00270   do {                                                                         \
00271     if (auto *N = getRaw())                                                    \
00272       return VALID;                                                            \
00273     return DEFAULT;                                                            \
00274   } while (false)
00275 #define RETURN_DESCRIPTOR_FROM_RAW(DESC, VALID)                                \
00276   do {                                                                         \
00277     if (auto *N = getRaw())                                                    \
00278       return DESC(dyn_cast_or_null<MDNode>(VALID));                            \
00279     return DESC(static_cast<const MDNode *>(nullptr));                         \
00280   } while (false)
00281 #define RETURN_REF_FROM_RAW(REF, VALID)                                        \
00282   do {                                                                         \
00283     if (auto *N = getRaw())                                                    \
00284       return REF::get(VALID);                                                  \
00285     return REF::get(nullptr);                                                  \
00286   } while (false)
00287 
00288 /// \brief This is used to represent ranges, for array bounds.
00289 class DISubrange : public DIDescriptor {
00290   MDSubrange *getRaw() const { return dyn_cast_or_null<MDSubrange>(get()); }
00291 
00292 public:
00293   explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
00294   DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
00295 
00296   int64_t getLo() const { RETURN_FROM_RAW(N->getLo(), 0); }
00297   int64_t getCount() const { RETURN_FROM_RAW(N->getCount(), 0); }
00298   bool Verify() const;
00299 };
00300 
00301 /// \brief This descriptor holds an array of nodes with type T.
00302 template <typename T> class DITypedArray : public DIDescriptor {
00303 public:
00304   explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
00305   unsigned getNumElements() const {
00306     return DbgNode ? DbgNode->getNumOperands() : 0;
00307   }
00308   T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
00309 };
00310 
00311 typedef DITypedArray<DIDescriptor> DIArray;
00312 
00313 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
00314 ///
00315 /// FIXME: it seems strange that this doesn't have either a reference to the
00316 /// type/precision or a file/line pair for location info.
00317 class DIEnumerator : public DIDescriptor {
00318   MDEnumerator *getRaw() const { return dyn_cast_or_null<MDEnumerator>(get()); }
00319 
00320 public:
00321   explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
00322   DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
00323 
00324   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00325   int64_t getEnumValue() const { RETURN_FROM_RAW(N->getValue(), 0); }
00326   bool Verify() const;
00327 };
00328 
00329 template <typename T> class DIRef;
00330 typedef DIRef<DIDescriptor> DIDescriptorRef;
00331 typedef DIRef<DIScope> DIScopeRef;
00332 typedef DIRef<DIType> DITypeRef;
00333 typedef DITypedArray<DITypeRef> DITypeArray;
00334 
00335 /// \brief A base class for various scopes.
00336 ///
00337 /// Although, implementation-wise, DIScope is the parent class of most
00338 /// other DIxxx classes, including DIType and its descendants, most of
00339 /// DIScope's descendants are not a substitutable subtype of
00340 /// DIScope. The DIDescriptor::isScope() method only is true for
00341 /// DIScopes that are scopes in the strict lexical scope sense
00342 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
00343 class DIScope : public DIDescriptor {
00344 protected:
00345   MDScope *getRaw() const { return dyn_cast_or_null<MDScope>(get()); }
00346 
00347 public:
00348   explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
00349   DIScope(const MDScope *N) : DIDescriptor(N) {}
00350 
00351   /// \brief Get the parent scope.
00352   ///
00353   /// Gets the parent scope for this scope node or returns a default
00354   /// constructed scope.
00355   DIScopeRef getContext() const;
00356   /// \brief Get the scope name.
00357   ///
00358   /// If the scope node has a name, return that, else return an empty string.
00359   StringRef getName() const;
00360   StringRef getFilename() const;
00361   StringRef getDirectory() const;
00362 
00363   /// \brief Generate a reference to this DIScope.
00364   ///
00365   /// Uses the type identifier instead of the actual MDNode if possible, to
00366   /// help type uniquing.
00367   DIScopeRef getRef() const;
00368 };
00369 
00370 /// \brief Represents reference to a DIDescriptor.
00371 ///
00372 /// Abstracts over direct and identifier-based metadata references.
00373 template <typename T> class DIRef {
00374   template <typename DescTy>
00375   friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
00376   friend DIScopeRef DIScope::getContext() const;
00377   friend DIScopeRef DIScope::getRef() const;
00378   friend class DIType;
00379 
00380   /// \brief Val can be either a MDNode or a MDString.
00381   ///
00382   /// In the latter, MDString specifies the type identifier.
00383   const Metadata *Val;
00384   explicit DIRef(const Metadata *V);
00385 
00386 public:
00387   T resolve(const DITypeIdentifierMap &Map) const;
00388   StringRef getName() const;
00389   operator Metadata *() const { return const_cast<Metadata *>(Val); }
00390 
00391   static DIRef get(const Metadata *MD) { return DIRef(MD); }
00392 };
00393 
00394 template <typename T>
00395 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
00396   if (!Val)
00397     return T();
00398 
00399   if (const MDNode *MD = dyn_cast<MDNode>(Val))
00400     return T(MD);
00401 
00402   const MDString *MS = cast<MDString>(Val);
00403   // Find the corresponding MDNode.
00404   DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
00405   assert(Iter != Map.end() && "Identifier not in the type map?");
00406   assert(DIDescriptor(Iter->second).isType() &&
00407          "MDNode in DITypeIdentifierMap should be a DIType.");
00408   return T(Iter->second);
00409 }
00410 
00411 template <typename T> StringRef DIRef<T>::getName() const {
00412   if (!Val)
00413     return StringRef();
00414 
00415   if (const MDNode *MD = dyn_cast<MDNode>(Val))
00416     return T(MD).getName();
00417 
00418   const MDString *MS = cast<MDString>(Val);
00419   return MS->getString();
00420 }
00421 
00422 /// \brief Handle fields that are references to DIDescriptors.
00423 template <>
00424 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
00425 /// \brief Specialize DIRef constructor for DIDescriptorRef.
00426 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
00427 
00428 /// \brief Handle fields that are references to DIScopes.
00429 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
00430 /// \brief Specialize DIRef constructor for DIScopeRef.
00431 template <> DIRef<DIScope>::DIRef(const Metadata *V);
00432 
00433 /// \brief Handle fields that are references to DITypes.
00434 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
00435 /// \brief Specialize DIRef constructor for DITypeRef.
00436 template <> DIRef<DIType>::DIRef(const Metadata *V);
00437 
00438 /// \brief This is a wrapper for a type.
00439 ///
00440 /// FIXME: Types should be factored much better so that CV qualifiers and
00441 /// others do not require a huge and empty descriptor full of zeros.
00442 class DIType : public DIScope {
00443   MDType *getRaw() const { return dyn_cast_or_null<MDType>(get()); }
00444 
00445 public:
00446   explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
00447   DIType(const MDType *N) : DIScope(N) {}
00448 
00449   operator DITypeRef() const {
00450     assert(isType() &&
00451            "constructing DITypeRef from an MDNode that is not a type");
00452     return DITypeRef(&*getRef());
00453   }
00454 
00455   bool Verify() const;
00456 
00457   DIScopeRef getContext() const {
00458     RETURN_REF_FROM_RAW(DIScopeRef, N->getScope());
00459   }
00460   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00461   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
00462   uint64_t getSizeInBits() const { RETURN_FROM_RAW(N->getSizeInBits(), 0); }
00463   uint64_t getAlignInBits() const { RETURN_FROM_RAW(N->getAlignInBits(), 0); }
00464   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
00465   // carry this is just plain insane.
00466   uint64_t getOffsetInBits() const { RETURN_FROM_RAW(N->getOffsetInBits(), 0); }
00467   unsigned getFlags() const { RETURN_FROM_RAW(N->getFlags(), 0); }
00468   bool isPrivate() const {
00469     return (getFlags() & FlagAccessibility) == FlagPrivate;
00470   }
00471   bool isProtected() const {
00472     return (getFlags() & FlagAccessibility) == FlagProtected;
00473   }
00474   bool isPublic() const {
00475     return (getFlags() & FlagAccessibility) == FlagPublic;
00476   }
00477   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
00478   bool isAppleBlockExtension() const {
00479     return (getFlags() & FlagAppleBlock) != 0;
00480   }
00481   bool isBlockByrefStruct() const {
00482     return (getFlags() & FlagBlockByrefStruct) != 0;
00483   }
00484   bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
00485   bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
00486   bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
00487   bool isObjcClassComplete() const {
00488     return (getFlags() & FlagObjcClassComplete) != 0;
00489   }
00490   bool isVector() const { return (getFlags() & FlagVector) != 0; }
00491   bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
00492   bool isLValueReference() const {
00493     return (getFlags() & FlagLValueReference) != 0;
00494   }
00495   bool isRValueReference() const {
00496     return (getFlags() & FlagRValueReference) != 0;
00497   }
00498   bool isValid() const { return DbgNode && isType(); }
00499 };
00500 
00501 /// \brief A basic type, like 'int' or 'float'.
00502 class DIBasicType : public DIType {
00503   MDBasicType *getRaw() const { return dyn_cast_or_null<MDBasicType>(get()); }
00504 
00505 public:
00506   explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
00507   DIBasicType(const MDBasicType *N) : DIType(N) {}
00508 
00509   unsigned getEncoding() const { RETURN_FROM_RAW(N->getEncoding(), 0); }
00510 
00511   bool Verify() const;
00512 };
00513 
00514 /// \brief A simple derived type
00515 ///
00516 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
00517 /// Or, a data member of a class/struct/union.
00518 class DIDerivedType : public DIType {
00519   MDDerivedTypeBase *getRaw() const {
00520     return dyn_cast_or_null<MDDerivedTypeBase>(get());
00521   }
00522 
00523 public:
00524   explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
00525   DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
00526 
00527   DITypeRef getTypeDerivedFrom() const {
00528     RETURN_REF_FROM_RAW(DITypeRef, N->getBaseType());
00529   }
00530 
00531   /// \brief Return property node, if this ivar is associated with one.
00532   MDNode *getObjCProperty() const {
00533     if (auto *N = dyn_cast_or_null<MDDerivedType>(get()))
00534       return dyn_cast_or_null<MDNode>(N->getExtraData());
00535     return nullptr;
00536   }
00537 
00538   DITypeRef getClassType() const {
00539     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
00540     if (auto *N = dyn_cast_or_null<MDDerivedType>(get()))
00541       return DITypeRef::get(N->getExtraData());
00542     return DITypeRef::get(nullptr);
00543   }
00544 
00545   Constant *getConstant() const {
00546     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
00547     if (auto *N = dyn_cast_or_null<MDDerivedType>(get()))
00548       if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
00549         return C->getValue();
00550 
00551     return nullptr;
00552   }
00553 
00554   bool Verify() const;
00555 };
00556 
00557 /// \brief Types that refer to multiple other types.
00558 ///
00559 /// This descriptor holds a type that can refer to multiple other types, like a
00560 /// function or struct.
00561 ///
00562 /// DICompositeType is derived from DIDerivedType because some
00563 /// composite types (such as enums) can be derived from basic types
00564 // FIXME: Make this derive from DIType directly & just store the
00565 // base type in a single DIType field.
00566 class DICompositeType : public DIDerivedType {
00567   friend class DIBuilder;
00568 
00569   /// \brief Set the array of member DITypes.
00570   void setArraysHelper(MDNode *Elements, MDNode *TParams);
00571 
00572   MDCompositeTypeBase *getRaw() const {
00573     return dyn_cast_or_null<MDCompositeTypeBase>(get());
00574   }
00575 
00576 public:
00577   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
00578   DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
00579 
00580   DIArray getElements() const {
00581     assert(!isSubroutineType() && "no elements for DISubroutineType");
00582     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getElements());
00583   }
00584 
00585 private:
00586   template <typename T>
00587   void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
00588     assert(
00589         (!TParams || DbgNode->getNumOperands() == 8) &&
00590         "If you're setting the template parameters this should include a slot "
00591         "for that!");
00592     setArraysHelper(Elements, TParams);
00593   }
00594 
00595 public:
00596   unsigned getRunTimeLang() const { RETURN_FROM_RAW(N->getRuntimeLang(), 0); }
00597   DITypeRef getContainingType() const {
00598     RETURN_REF_FROM_RAW(DITypeRef, N->getVTableHolder());
00599   }
00600 
00601 private:
00602   /// \brief Set the containing type.
00603   void setContainingType(DICompositeType ContainingType);
00604 
00605 public:
00606   DIArray getTemplateParams() const {
00607     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getTemplateParams());
00608   }
00609   MDString *getIdentifier() const {
00610     RETURN_FROM_RAW(N->getRawIdentifier(), nullptr);
00611   }
00612 
00613   bool Verify() const;
00614 };
00615 
00616 class DISubroutineType : public DICompositeType {
00617   MDSubroutineType *getRaw() const {
00618     return dyn_cast_or_null<MDSubroutineType>(get());
00619   }
00620 
00621 public:
00622   explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
00623   DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
00624 
00625   DITypedArray<DITypeRef> getTypeArray() const {
00626     RETURN_DESCRIPTOR_FROM_RAW(DITypedArray<DITypeRef>, N->getTypeArray());
00627   }
00628 };
00629 
00630 /// \brief This is a wrapper for a file.
00631 class DIFile : public DIScope {
00632   MDFile *getRaw() const { return dyn_cast_or_null<MDFile>(get()); }
00633 
00634 public:
00635   explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
00636   DIFile(const MDFile *N) : DIScope(N) {}
00637 
00638   /// \brief Retrieve the MDNode for the directory/file pair.
00639   MDNode *getFileNode() const { return get(); }
00640   bool Verify() const;
00641 };
00642 
00643 /// \brief A wrapper for a compile unit.
00644 class DICompileUnit : public DIScope {
00645   MDCompileUnit *getRaw() const {
00646     return dyn_cast_or_null<MDCompileUnit>(get());
00647   }
00648 
00649 public:
00650   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
00651   DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
00652 
00653   dwarf::SourceLanguage getLanguage() const {
00654     RETURN_FROM_RAW(static_cast<dwarf::SourceLanguage>(N->getSourceLanguage()),
00655                     static_cast<dwarf::SourceLanguage>(0));
00656   }
00657   StringRef getProducer() const { RETURN_FROM_RAW(N->getProducer(), ""); }
00658   bool isOptimized() const { RETURN_FROM_RAW(N->isOptimized(), false); }
00659   StringRef getFlags() const { RETURN_FROM_RAW(N->getFlags(), ""); }
00660   unsigned getRunTimeVersion() const {
00661     RETURN_FROM_RAW(N->getRuntimeVersion(), 0);
00662   }
00663 
00664   DIArray getEnumTypes() const {
00665     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getEnumTypes());
00666   }
00667   DIArray getRetainedTypes() const {
00668     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getRetainedTypes());
00669   }
00670   DIArray getSubprograms() const {
00671     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getSubprograms());
00672   }
00673   DIArray getGlobalVariables() const {
00674     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getGlobalVariables());
00675   }
00676   DIArray getImportedEntities() const {
00677     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getImportedEntities());
00678   }
00679 
00680   void replaceSubprograms(DIArray Subprograms);
00681   void replaceGlobalVariables(DIArray GlobalVariables);
00682 
00683   StringRef getSplitDebugFilename() const {
00684     RETURN_FROM_RAW(N->getSplitDebugFilename(), "");
00685   }
00686   unsigned getEmissionKind() const { RETURN_FROM_RAW(N->getEmissionKind(), 0); }
00687 
00688   bool Verify() const;
00689 };
00690 
00691 /// \brief This is a wrapper for a subprogram (e.g. a function).
00692 class DISubprogram : public DIScope {
00693   MDSubprogram *getRaw() const { return dyn_cast_or_null<MDSubprogram>(get()); }
00694 
00695 public:
00696   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
00697   DISubprogram(const MDSubprogram *N) : DIScope(N) {}
00698 
00699   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00700   StringRef getDisplayName() const { RETURN_FROM_RAW(N->getDisplayName(), ""); }
00701   StringRef getLinkageName() const { RETURN_FROM_RAW(N->getLinkageName(), ""); }
00702   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
00703 
00704   /// \brief Check if this is local (like 'static' in C).
00705   unsigned isLocalToUnit() const { RETURN_FROM_RAW(N->isLocalToUnit(), 0); }
00706   unsigned isDefinition() const { RETURN_FROM_RAW(N->isDefinition(), 0); }
00707 
00708   unsigned getVirtuality() const { RETURN_FROM_RAW(N->getVirtuality(), 0); }
00709   unsigned getVirtualIndex() const { RETURN_FROM_RAW(N->getVirtualIndex(), 0); }
00710 
00711   unsigned getFlags() const { RETURN_FROM_RAW(N->getFlags(), 0); }
00712 
00713   unsigned isOptimized() const { RETURN_FROM_RAW(N->isOptimized(), 0); }
00714 
00715   /// \brief Get the beginning of the scope of the function (not the name).
00716   unsigned getScopeLineNumber() const { RETURN_FROM_RAW(N->getScopeLine(), 0); }
00717 
00718   DIScopeRef getContext() const {
00719     RETURN_REF_FROM_RAW(DIScopeRef, N->getScope());
00720   }
00721   DISubroutineType getType() const {
00722     RETURN_DESCRIPTOR_FROM_RAW(DISubroutineType, N->getType());
00723   }
00724 
00725   DITypeRef getContainingType() const {
00726     RETURN_REF_FROM_RAW(DITypeRef, N->getContainingType());
00727   }
00728 
00729   bool Verify() const;
00730 
00731   /// \brief Check if this provides debugging information for the function F.
00732   bool describes(const Function *F);
00733 
00734   Function *getFunction() const;
00735 
00736   void replaceFunction(Function *F) {
00737     if (auto *N = getRaw())
00738       N->replaceFunction(F);
00739   }
00740   DIArray getTemplateParams() const {
00741     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getTemplateParams());
00742   }
00743   DISubprogram getFunctionDeclaration() const {
00744     RETURN_DESCRIPTOR_FROM_RAW(DISubprogram, N->getDeclaration());
00745   }
00746   MDNode *getVariablesNodes() const { return getVariables(); }
00747   DIArray getVariables() const {
00748     RETURN_DESCRIPTOR_FROM_RAW(DIArray, N->getVariables());
00749   }
00750 
00751   unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
00752   /// \brief Check for the "private" access specifier.
00753   bool isPrivate() const {
00754     return (getFlags() & FlagAccessibility) == FlagPrivate;
00755   }
00756   /// \brief Check for the "protected" access specifier.
00757   bool isProtected() const {
00758     return (getFlags() & FlagAccessibility) == FlagProtected;
00759   }
00760   /// \brief Check for the "public" access specifier.
00761   bool isPublic() const {
00762     return (getFlags() & FlagAccessibility) == FlagPublic;
00763   }
00764   /// \brief Check for "explicit".
00765   bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
00766   /// \brief Check if this is prototyped.
00767   bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
00768 
00769   /// \brief Check if this is reference-qualified.
00770   ///
00771   /// Return true if this subprogram is a C++11 reference-qualified non-static
00772   /// member function (void foo() &).
00773   unsigned isLValueReference() const {
00774     return (getFlags() & FlagLValueReference) != 0;
00775   }
00776 
00777   /// \brief Check if this is rvalue-reference-qualified.
00778   ///
00779   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
00780   /// non-static member function (void foo() &&).
00781   unsigned isRValueReference() const {
00782     return (getFlags() & FlagRValueReference) != 0;
00783   }
00784 };
00785 
00786 /// \brief This is a wrapper for a lexical block.
00787 class DILexicalBlock : public DIScope {
00788   MDLexicalBlockBase *getRaw() const {
00789     return dyn_cast_or_null<MDLexicalBlockBase>(get());
00790   }
00791 
00792 public:
00793   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
00794   DILexicalBlock(const MDLexicalBlock *N) : DIScope(N) {}
00795 
00796   DIScope getContext() const {
00797     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
00798   }
00799   unsigned getLineNumber() const {
00800     if (auto *N = dyn_cast_or_null<MDLexicalBlock>(get()))
00801       return N->getLine();
00802     return 0;
00803   }
00804   unsigned getColumnNumber() const {
00805     if (auto *N = dyn_cast_or_null<MDLexicalBlock>(get()))
00806       return N->getColumn();
00807     return 0;
00808   }
00809   bool Verify() const;
00810 };
00811 
00812 /// \brief This is a wrapper for a lexical block with a filename change.
00813 class DILexicalBlockFile : public DIScope {
00814   MDLexicalBlockFile *getRaw() const {
00815     return dyn_cast_or_null<MDLexicalBlockFile>(get());
00816   }
00817 
00818 public:
00819   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
00820   DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
00821 
00822   DIScope getContext() const {
00823     // FIXME: This logic is horrible.  getScope() returns a DILexicalBlock, but
00824     // then we check if it's a subprogram?  WHAT?!?
00825     if (getScope().isSubprogram())
00826       return getScope();
00827     return getScope().getContext();
00828   }
00829   unsigned getLineNumber() const { return getScope().getLineNumber(); }
00830   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
00831   DILexicalBlock getScope() const {
00832     RETURN_DESCRIPTOR_FROM_RAW(DILexicalBlock, N->getScope());
00833   }
00834   unsigned getDiscriminator() const {
00835     RETURN_FROM_RAW(N->getDiscriminator(), 0);
00836   }
00837   bool Verify() const;
00838 };
00839 
00840 /// \brief A wrapper for a C++ style name space.
00841 class DINameSpace : public DIScope {
00842   MDNamespace *getRaw() const { return dyn_cast_or_null<MDNamespace>(get()); }
00843 
00844 public:
00845   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
00846   DINameSpace(const MDNamespace *N) : DIScope(N) {}
00847 
00848   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00849   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
00850   DIScope getContext() const {
00851     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
00852   }
00853   bool Verify() const;
00854 };
00855 
00856 /// \brief This is a wrapper for template type parameter.
00857 class DITemplateTypeParameter : public DIDescriptor {
00858   MDTemplateTypeParameter *getRaw() const {
00859     return dyn_cast_or_null<MDTemplateTypeParameter>(get());
00860   }
00861 
00862 public:
00863   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
00864       : DIDescriptor(N) {}
00865   DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
00866 
00867   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00868 
00869   DITypeRef getType() const { RETURN_REF_FROM_RAW(DITypeRef, N->getType()); }
00870   bool Verify() const;
00871 };
00872 
00873 /// \brief This is a wrapper for template value parameter.
00874 class DITemplateValueParameter : public DIDescriptor {
00875   MDTemplateValueParameter *getRaw() const {
00876     return dyn_cast_or_null<MDTemplateValueParameter>(get());
00877   }
00878 
00879 public:
00880   explicit DITemplateValueParameter(const MDNode *N = nullptr)
00881       : DIDescriptor(N) {}
00882   DITemplateValueParameter(const MDTemplateValueParameter *N)
00883       : DIDescriptor(N) {}
00884 
00885   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00886   DITypeRef getType() const { RETURN_REF_FROM_RAW(DITypeRef, N->getType()); }
00887   Metadata *getValue() const { RETURN_FROM_RAW(N->getValue(), nullptr); }
00888   bool Verify() const;
00889 };
00890 
00891 /// \brief This is a wrapper for a global variable.
00892 class DIGlobalVariable : public DIDescriptor {
00893   MDGlobalVariable *getRaw() const {
00894     return dyn_cast_or_null<MDGlobalVariable>(get());
00895   }
00896 
00897   DIFile getFile() const { RETURN_DESCRIPTOR_FROM_RAW(DIFile, N->getFile()); }
00898 
00899 public:
00900   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00901   DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
00902 
00903   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00904   StringRef getDisplayName() const { RETURN_FROM_RAW(N->getDisplayName(), ""); }
00905   StringRef getLinkageName() const { RETURN_FROM_RAW(N->getLinkageName(), ""); }
00906   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
00907   unsigned isLocalToUnit() const { RETURN_FROM_RAW(N->isLocalToUnit(), 0); }
00908   unsigned isDefinition() const { RETURN_FROM_RAW(N->isDefinition(), 0); }
00909 
00910   DIScope getContext() const {
00911     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
00912   }
00913   StringRef getFilename() const { return getFile().getFilename(); }
00914   StringRef getDirectory() const { return getFile().getDirectory(); }
00915   DITypeRef getType() const { RETURN_REF_FROM_RAW(DITypeRef, N->getType()); }
00916 
00917   GlobalVariable *getGlobal() const;
00918   Constant *getConstant() const {
00919     if (auto *N = getRaw())
00920       if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
00921         return C->getValue();
00922     return nullptr;
00923   }
00924   DIDerivedType getStaticDataMemberDeclaration() const {
00925     RETURN_DESCRIPTOR_FROM_RAW(DIDerivedType,
00926                                N->getStaticDataMemberDeclaration());
00927   }
00928 
00929   bool Verify() const;
00930 };
00931 
00932 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
00933 class DIVariable : public DIDescriptor {
00934   MDLocalVariable *getRaw() const {
00935     return dyn_cast_or_null<MDLocalVariable>(get());
00936   }
00937 
00938   unsigned getFlags() const { RETURN_FROM_RAW(N->getFlags(), 0); }
00939 
00940 public:
00941   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00942   DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
00943 
00944   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
00945   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
00946   unsigned getArgNumber() const { RETURN_FROM_RAW(N->getArg(), 0); }
00947 
00948   DIScope getContext() const {
00949     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
00950   }
00951   DIFile getFile() const { RETURN_DESCRIPTOR_FROM_RAW(DIFile, N->getFile()); }
00952   DITypeRef getType() const { RETURN_REF_FROM_RAW(DITypeRef, N->getType()); }
00953 
00954   /// \brief Return true if this variable is marked as "artificial".
00955   bool isArtificial() const {
00956     return (getFlags() & FlagArtificial) != 0;
00957   }
00958 
00959   bool isObjectPointer() const {
00960     return (getFlags() & FlagObjectPointer) != 0;
00961   }
00962 
00963   /// \brief If this variable is inlined then return inline location.
00964   MDNode *getInlinedAt() const {
00965     RETURN_DESCRIPTOR_FROM_RAW(DIDescriptor, N->getInlinedAt());
00966   }
00967 
00968   bool Verify() const;
00969 
00970   /// \brief Check if this is a "__block" variable (Apple Blocks).
00971   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
00972     return (getType().resolve(Map)).isBlockByrefStruct();
00973   }
00974 
00975   /// \brief Check if this is an inlined function argument.
00976   bool isInlinedFnArgument(const Function *CurFn);
00977 
00978   /// \brief Return the size reported by the variable's type.
00979   unsigned getSizeInBits(const DITypeIdentifierMap &Map);
00980 
00981   void printExtendedName(raw_ostream &OS) const;
00982 };
00983 
00984 /// \brief A complex location expression in postfix notation.
00985 ///
00986 /// This is (almost) a DWARF expression that modifies the location of a
00987 /// variable or (or the location of a single piece of a variable).
00988 ///
00989 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
00990 /// and have DW_OP_plus consume the topmost elements on the stack.
00991 class DIExpression : public DIDescriptor {
00992   MDExpression *getRaw() const { return dyn_cast_or_null<MDExpression>(get()); }
00993 
00994 public:
00995   explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
00996   DIExpression(const MDExpression *N) : DIDescriptor(N) {}
00997 
00998   bool Verify() const;
00999 
01000   /// \brief Return the number of elements in the complex expression.
01001   unsigned getNumElements() const { RETURN_FROM_RAW(N->getNumElements(), 0); }
01002 
01003   /// \brief return the Idx'th complex address element.
01004   uint64_t getElement(unsigned I) const {
01005     return cast<MDExpression>(get())->getElement(I);
01006   }
01007 
01008   /// \brief Return whether this is a piece of an aggregate variable.
01009   bool isBitPiece() const;
01010   /// \brief Return the offset of this piece in bits.
01011   uint64_t getBitPieceOffset() const;
01012   /// \brief Return the size of this piece in bits.
01013   uint64_t getBitPieceSize() const;
01014 
01015   class iterator;
01016   /// \brief A lightweight wrapper around an element of a DIExpression.
01017   class Operand {
01018     friend class iterator;
01019     MDExpression::element_iterator I;
01020     Operand() {}
01021     Operand(MDExpression::element_iterator I) : I(I) {}
01022   public:
01023     /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
01024     /// Argument 0 is the operand itself.
01025     uint64_t getArg(unsigned N) const {
01026       MDExpression::element_iterator In = I;
01027       std::advance(In, N);
01028       return *In;
01029     }
01030     operator uint64_t () const { return *I; }
01031     /// \brief Returns underlying MDExpression::element_iterator.
01032     const MDExpression::element_iterator &getBase() const { return I; }
01033     /// \brief Returns the next operand.
01034     iterator getNext() const;
01035   };
01036 
01037   /// \brief An iterator for DIExpression elements.
01038   class iterator : public std::iterator<std::input_iterator_tag, StringRef,
01039                                         unsigned, const Operand*, Operand> {
01040     friend class Operand;
01041     MDExpression::element_iterator I;
01042     Operand Tmp;
01043 
01044   public:
01045     iterator(MDExpression::element_iterator I) : I(I) {}
01046     const Operand &operator*() { return Tmp = Operand(I); }
01047     const Operand *operator->() { return &(Tmp = Operand(I)); }
01048     iterator &operator++() {
01049       increment();
01050       return *this;
01051     }
01052     iterator operator++(int) {
01053       iterator X(*this);
01054       increment();
01055       return X;
01056     }
01057     bool operator==(const iterator &X) const { return I == X.I; }
01058     bool operator!=(const iterator &X) const { return !(*this == X); }
01059 
01060   private:
01061     void increment() {
01062       switch (**this) {
01063       case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
01064       case dwarf::DW_OP_plus:      std::advance(I, 2); break;
01065       case dwarf::DW_OP_deref:     std::advance(I, 1); break;
01066       default:
01067         llvm_unreachable("unsupported operand");
01068       }
01069     }
01070   };
01071 
01072   iterator begin() const { return cast<MDExpression>(get())->elements_begin(); }
01073   iterator end() const { return cast<MDExpression>(get())->elements_end(); }
01074 };
01075 
01076 /// \brief This object holds location information.
01077 ///
01078 /// This object is not associated with any DWARF tag.
01079 class DILocation : public DIDescriptor {
01080   MDLocation *getRaw() const { return dyn_cast_or_null<MDLocation>(get()); }
01081 
01082 public:
01083   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
01084 
01085   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
01086   unsigned getColumnNumber() const { RETURN_FROM_RAW(N->getColumn(), 0); }
01087   DIScope getScope() const {
01088     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
01089   }
01090   DILocation getOrigLocation() const {
01091     RETURN_DESCRIPTOR_FROM_RAW(DILocation, N->getInlinedAt());
01092   }
01093   StringRef getFilename() const { return getScope().getFilename(); }
01094   StringRef getDirectory() const { return getScope().getDirectory(); }
01095   bool Verify() const;
01096   bool atSameLineAs(const DILocation &Other) const {
01097     return (getLineNumber() == Other.getLineNumber() &&
01098             getFilename() == Other.getFilename());
01099   }
01100   /// \brief Get the DWAF discriminator.
01101   ///
01102   /// DWARF discriminators are used to distinguish identical file locations for
01103   /// instructions that are on different basic blocks. If two instructions are
01104   /// inside the same lexical block and are in different basic blocks, we
01105   /// create a new lexical block with identical location as the original but
01106   /// with a different discriminator value
01107   /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
01108   unsigned getDiscriminator() const {
01109     // Since discriminators are associated with lexical blocks, make
01110     // sure this location is a lexical block before retrieving its
01111     // value.
01112     return getScope().isLexicalBlockFile()
01113                ? DILexicalBlockFile(
01114                      cast<MDNode>(cast<MDLocation>(DbgNode)->getScope()))
01115                      .getDiscriminator()
01116                : 0;
01117   }
01118 
01119   /// \brief Generate a new discriminator value for this location.
01120   unsigned computeNewDiscriminator(LLVMContext &Ctx);
01121 
01122   /// \brief Return a copy of this location with a different scope.
01123   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
01124 };
01125 
01126 class DIObjCProperty : public DIDescriptor {
01127   MDObjCProperty *getRaw() const {
01128     return dyn_cast_or_null<MDObjCProperty>(get());
01129   }
01130 
01131 public:
01132   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
01133   DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
01134 
01135   StringRef getObjCPropertyName() const { RETURN_FROM_RAW(N->getName(), ""); }
01136   DIFile getFile() const { RETURN_DESCRIPTOR_FROM_RAW(DIFile, N->getFile()); }
01137   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
01138 
01139   StringRef getObjCPropertyGetterName() const {
01140     RETURN_FROM_RAW(N->getGetterName(), "");
01141   }
01142   StringRef getObjCPropertySetterName() const {
01143     RETURN_FROM_RAW(N->getSetterName(), "");
01144   }
01145   unsigned getAttributes() const { RETURN_FROM_RAW(N->getAttributes(), 0); }
01146   bool isReadOnlyObjCProperty() const {
01147     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
01148   }
01149   bool isReadWriteObjCProperty() const {
01150     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
01151   }
01152   bool isAssignObjCProperty() const {
01153     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
01154   }
01155   bool isRetainObjCProperty() const {
01156     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
01157   }
01158   bool isCopyObjCProperty() const {
01159     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
01160   }
01161   bool isNonAtomicObjCProperty() const {
01162     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
01163   }
01164 
01165   /// \brief Get the type.
01166   ///
01167   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
01168   /// the type as a DITypeRef here.
01169   DIType getType() const { RETURN_DESCRIPTOR_FROM_RAW(DIType, N->getType()); }
01170 
01171   bool Verify() const;
01172 };
01173 
01174 /// \brief An imported module (C++ using directive or similar).
01175 class DIImportedEntity : public DIDescriptor {
01176   MDImportedEntity *getRaw() const {
01177     return dyn_cast_or_null<MDImportedEntity>(get());
01178   }
01179 
01180 public:
01181   DIImportedEntity() = default;
01182   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
01183   DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
01184 
01185   DIScope getContext() const {
01186     RETURN_DESCRIPTOR_FROM_RAW(DIScope, N->getScope());
01187   }
01188   DIDescriptorRef getEntity() const {
01189     RETURN_REF_FROM_RAW(DIDescriptorRef, N->getEntity());
01190   }
01191   unsigned getLineNumber() const { RETURN_FROM_RAW(N->getLine(), 0); }
01192   StringRef getName() const { RETURN_FROM_RAW(N->getName(), ""); }
01193   bool Verify() const;
01194 };
01195 
01196 #undef RETURN_FROM_RAW
01197 #undef RETURN_DESCRIPTOR_FROM_RAW
01198 #undef RETURN_REF_FROM_RAW
01199 
01200 /// \brief Find subprogram that is enclosing this scope.
01201 DISubprogram getDISubprogram(const MDNode *Scope);
01202 
01203 /// \brief Find debug info for a given function.
01204 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
01205 /// DISubprogram.
01206 DISubprogram getDISubprogram(const Function *F);
01207 
01208 /// \brief Find underlying composite type.
01209 DICompositeType getDICompositeType(DIType T);
01210 
01211 /// \brief Create a new inlined variable based on current variable.
01212 ///
01213 /// @param DV            Current Variable.
01214 /// @param InlinedScope  Location at current variable is inlined.
01215 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
01216                                  LLVMContext &VMContext);
01217 
01218 /// \brief Remove inlined scope from the variable.
01219 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
01220 
01221 /// \brief Generate map by visiting all retained types.
01222 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
01223 
01224 /// \brief Strip debug info in the module if it exists.
01225 ///
01226 /// To do this, we remove all calls to the debugger intrinsics and any named
01227 /// metadata for debugging. We also remove debug locations for instructions.
01228 /// Return true if module is modified.
01229 bool StripDebugInfo(Module &M);
01230 
01231 /// \brief Return Debug Info Metadata Version by checking module flags.
01232 unsigned getDebugMetadataVersionFromModule(const Module &M);
01233 
01234 /// \brief Utility to find all debug info in a module.
01235 ///
01236 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
01237 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
01238 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
01239 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
01240 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
01241 /// used by the CUs.
01242 class DebugInfoFinder {
01243 public:
01244   DebugInfoFinder() : TypeMapInitialized(false) {}
01245 
01246   /// \brief Process entire module and collect debug info anchors.
01247   void processModule(const Module &M);
01248 
01249   /// \brief Process DbgDeclareInst.
01250   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
01251   /// \brief Process DbgValueInst.
01252   void processValue(const Module &M, const DbgValueInst *DVI);
01253   /// \brief Process DILocation.
01254   void processLocation(const Module &M, DILocation Loc);
01255 
01256   /// \brief Process DIExpression.
01257   void processExpression(DIExpression Expr);
01258 
01259   /// \brief Clear all lists.
01260   void reset();
01261 
01262 private:
01263   void InitializeTypeMap(const Module &M);
01264 
01265   void processType(DIType DT);
01266   void processSubprogram(DISubprogram SP);
01267   void processScope(DIScope Scope);
01268   bool addCompileUnit(DICompileUnit CU);
01269   bool addGlobalVariable(DIGlobalVariable DIG);
01270   bool addSubprogram(DISubprogram SP);
01271   bool addType(DIType DT);
01272   bool addScope(DIScope Scope);
01273 
01274 public:
01275   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
01276   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
01277   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
01278       global_variable_iterator;
01279   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
01280   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
01281 
01282   iterator_range<compile_unit_iterator> compile_units() const {
01283     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
01284   }
01285 
01286   iterator_range<subprogram_iterator> subprograms() const {
01287     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
01288   }
01289 
01290   iterator_range<global_variable_iterator> global_variables() const {
01291     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
01292   }
01293 
01294   iterator_range<type_iterator> types() const {
01295     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
01296   }
01297 
01298   iterator_range<scope_iterator> scopes() const {
01299     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
01300   }
01301 
01302   unsigned compile_unit_count() const { return CUs.size(); }
01303   unsigned global_variable_count() const { return GVs.size(); }
01304   unsigned subprogram_count() const { return SPs.size(); }
01305   unsigned type_count() const { return TYs.size(); }
01306   unsigned scope_count() const { return Scopes.size(); }
01307 
01308 private:
01309   SmallVector<DICompileUnit, 8> CUs;
01310   SmallVector<DISubprogram, 8> SPs;
01311   SmallVector<DIGlobalVariable, 8> GVs;
01312   SmallVector<DIType, 8> TYs;
01313   SmallVector<DIScope, 8> Scopes;
01314   SmallPtrSet<MDNode *, 64> NodesSeen;
01315   DITypeIdentifierMap TypeIdentifierMap;
01316 
01317   /// \brief Specify if TypeIdentifierMap is initialized.
01318   bool TypeMapInitialized;
01319 };
01320 
01321 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
01322 
01323 } // end namespace llvm
01324 
01325 #endif