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