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/iterator_range.h"
00022 #include "llvm/ADT/SmallPtrSet.h"
00023 #include "llvm/ADT/SmallVector.h"
00024 #include "llvm/ADT/StringRef.h"
00025 #include "llvm/IR/Metadata.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   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 private:
00103   void increment() {
00104     assert(Current.data() != nullptr && "Cannot increment past the end");
00105     StringRef Suffix = getSuffix();
00106     Current = Suffix.slice(0, Suffix.find('\0'));
00107   }
00108 };
00109 
00110 /// \brief A thin wraper around MDNode to access encoded debug info.
00111 ///
00112 /// This should not be stored in a container, because the underlying MDNode may
00113 /// change in certain situations.
00114 class DIDescriptor {
00115   // Befriends DIRef so DIRef can befriend the protected member
00116   // function: getFieldAs<DIRef>.
00117   template <typename T> friend class DIRef;
00118 
00119 public:
00120   /// \brief Accessibility flags.
00121   ///
00122   /// The three accessibility flags are mutually exclusive and rolled together
00123   /// in the first two bits.
00124   enum {
00125     FlagAccessibility     = 1 << 0 | 1 << 1,
00126     FlagPrivate           = 1,
00127     FlagProtected         = 2,
00128     FlagPublic            = 3,
00129 
00130     FlagFwdDecl           = 1 << 2,
00131     FlagAppleBlock        = 1 << 3,
00132     FlagBlockByrefStruct  = 1 << 4,
00133     FlagVirtual           = 1 << 5,
00134     FlagArtificial        = 1 << 6,
00135     FlagExplicit          = 1 << 7,
00136     FlagPrototyped        = 1 << 8,
00137     FlagObjcClassComplete = 1 << 9,
00138     FlagObjectPointer     = 1 << 10,
00139     FlagVector            = 1 << 11,
00140     FlagStaticMember      = 1 << 12,
00141     FlagIndirectVariable  = 1 << 13,
00142     FlagLValueReference   = 1 << 14,
00143     FlagRValueReference   = 1 << 15
00144   };
00145 
00146 protected:
00147   const MDNode *DbgNode;
00148 
00149   StringRef getStringField(unsigned Elt) const;
00150   unsigned getUnsignedField(unsigned Elt) const {
00151     return (unsigned)getUInt64Field(Elt);
00152   }
00153   uint64_t getUInt64Field(unsigned Elt) const;
00154   int64_t getInt64Field(unsigned Elt) const;
00155   DIDescriptor getDescriptorField(unsigned Elt) const;
00156 
00157   template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
00158     return DescTy(getDescriptorField(Elt));
00159   }
00160 
00161   GlobalVariable *getGlobalVariableField(unsigned Elt) const;
00162   Constant *getConstantField(unsigned Elt) const;
00163   Function *getFunctionField(unsigned Elt) const;
00164   void replaceFunctionField(unsigned Elt, Function *F);
00165 
00166 public:
00167   explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
00168 
00169   bool Verify() const;
00170 
00171   MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
00172   operator MDNode *() const { return get(); }
00173   MDNode *operator->() const { return get(); }
00174 
00175   // An explicit operator bool so that we can do testing of DI values
00176   // easily.
00177   // FIXME: This operator bool isn't actually protecting anything at the
00178   // moment due to the conversion operator above making DIDescriptor nodes
00179   // implicitly convertable to bool.
00180   LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
00181 
00182   bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
00183   bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
00184 
00185   StringRef getHeader() const {
00186     return getStringField(0);
00187   }
00188 
00189   size_t getNumHeaderFields() const {
00190     return std::distance(DIHeaderFieldIterator(getHeader()),
00191                          DIHeaderFieldIterator());
00192   }
00193 
00194   StringRef getHeaderField(unsigned Index) const {
00195     // Since callers expect an empty string for out-of-range accesses, we can't
00196     // use std::advance() here.
00197     for (DIHeaderFieldIterator I(getHeader()), E; I != E; ++I, --Index)
00198       if (!Index)
00199         return *I;
00200     return StringRef();
00201   }
00202 
00203   template <class T> T getHeaderFieldAs(unsigned Index) const {
00204     T Int;
00205     if (getHeaderField(Index).getAsInteger(0, Int))
00206       return 0;
00207     return Int;
00208   }
00209 
00210   uint16_t getTag() const { return getHeaderFieldAs<uint16_t>(0); }
00211 
00212   bool isDerivedType() const;
00213   bool isCompositeType() const;
00214   bool isSubroutineType() const;
00215   bool isBasicType() const;
00216   bool isVariable() const;
00217   bool isSubprogram() const;
00218   bool isGlobalVariable() const;
00219   bool isScope() const;
00220   bool isFile() const;
00221   bool isCompileUnit() const;
00222   bool isNameSpace() const;
00223   bool isLexicalBlockFile() const;
00224   bool isLexicalBlock() const;
00225   bool isSubrange() const;
00226   bool isEnumerator() const;
00227   bool isType() const;
00228   bool isTemplateTypeParameter() const;
00229   bool isTemplateValueParameter() const;
00230   bool isObjCProperty() const;
00231   bool isImportedEntity() const;
00232   bool isExpression() const;
00233 
00234   void print(raw_ostream &OS) const;
00235   void dump() const;
00236 
00237   /// \brief Replace all uses of debug info referenced by this descriptor.
00238   void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
00239   void replaceAllUsesWith(MDNode *D);
00240 };
00241 
00242 /// \brief This is used to represent ranges, for array bounds.
00243 class DISubrange : public DIDescriptor {
00244   friend class DIDescriptor;
00245   void printInternal(raw_ostream &OS) const;
00246 
00247 public:
00248   explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
00249 
00250   int64_t getLo() const { return getHeaderFieldAs<int64_t>(1); }
00251   int64_t getCount() const { return getHeaderFieldAs<int64_t>(2); }
00252   bool Verify() const;
00253 };
00254 
00255 /// \brief This descriptor holds an array of nodes with type T.
00256 template <typename T> class DITypedArray : public DIDescriptor {
00257 public:
00258   explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
00259   unsigned getNumElements() const {
00260     return DbgNode ? DbgNode->getNumOperands() : 0;
00261   }
00262   T getElement(unsigned Idx) const {
00263     return getFieldAs<T>(Idx);
00264   }
00265 };
00266 
00267 typedef DITypedArray<DIDescriptor> DIArray;
00268 
00269 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
00270 ///
00271 /// FIXME: it seems strange that this doesn't have either a reference to the
00272 /// type/precision or a file/line pair for location info.
00273 class DIEnumerator : public DIDescriptor {
00274   friend class DIDescriptor;
00275   void printInternal(raw_ostream &OS) const;
00276 
00277 public:
00278   explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
00279 
00280   StringRef getName() const { return getHeaderField(1); }
00281   int64_t getEnumValue() const { return getHeaderFieldAs<int64_t>(2); }
00282   bool Verify() const;
00283 };
00284 
00285 template <typename T> class DIRef;
00286 typedef DIRef<DIScope> DIScopeRef;
00287 typedef DIRef<DIType> DITypeRef;
00288 typedef DITypedArray<DITypeRef> DITypeArray;
00289 
00290 /// \brief A base class for various scopes.
00291 ///
00292 /// Although, implementation-wise, DIScope is the parent class of most
00293 /// other DIxxx classes, including DIType and its descendants, most of
00294 /// DIScope's descendants are not a substitutable subtype of
00295 /// DIScope. The DIDescriptor::isScope() method only is true for
00296 /// DIScopes that are scopes in the strict lexical scope sense
00297 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
00298 class DIScope : public DIDescriptor {
00299 protected:
00300   friend class DIDescriptor;
00301   void printInternal(raw_ostream &OS) const;
00302 
00303 public:
00304   explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
00305 
00306   /// \brief Get the parent scope.
00307   ///
00308   /// Gets the parent scope for this scope node or returns a default
00309   /// constructed scope.
00310   DIScopeRef getContext() const;
00311   /// \brief Get the scope name.
00312   ///
00313   /// If the scope node has a name, return that, else return an empty string.
00314   StringRef getName() const;
00315   StringRef getFilename() const;
00316   StringRef getDirectory() const;
00317 
00318   /// \brief Generate a reference to this DIScope.
00319   ///
00320   /// Uses the type identifier instead of the actual MDNode if possible, to
00321   /// help type uniquing.
00322   DIScopeRef getRef() const;
00323 };
00324 
00325 /// \brief Represents reference to a DIDescriptor.
00326 ///
00327 /// Abstracts over direct and identifier-based metadata references.
00328 template <typename T> class DIRef {
00329   template <typename DescTy>
00330   friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
00331   friend DIScopeRef DIScope::getContext() const;
00332   friend DIScopeRef DIScope::getRef() const;
00333   friend class DIType;
00334 
00335   /// \brief Val can be either a MDNode or a MDString.
00336   ///
00337   /// In the latter, MDString specifies the type identifier.
00338   const Metadata *Val;
00339   explicit DIRef(const Metadata *V);
00340 
00341 public:
00342   T resolve(const DITypeIdentifierMap &Map) const;
00343   StringRef getName() const;
00344   operator Metadata *() const { return const_cast<Metadata *>(Val); }
00345 };
00346 
00347 template <typename T>
00348 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
00349   if (!Val)
00350     return T();
00351 
00352   if (const MDNode *MD = dyn_cast<MDNode>(Val))
00353     return T(MD);
00354 
00355   const MDString *MS = cast<MDString>(Val);
00356   // Find the corresponding MDNode.
00357   DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
00358   assert(Iter != Map.end() && "Identifier not in the type map?");
00359   assert(DIDescriptor(Iter->second).isType() &&
00360          "MDNode in DITypeIdentifierMap should be a DIType.");
00361   return T(Iter->second);
00362 }
00363 
00364 template <typename T> StringRef DIRef<T>::getName() const {
00365   if (!Val)
00366     return StringRef();
00367 
00368   if (const MDNode *MD = dyn_cast<MDNode>(Val))
00369     return T(MD).getName();
00370 
00371   const MDString *MS = cast<MDString>(Val);
00372   return MS->getString();
00373 }
00374 
00375 /// \brief Handle fields that are references to DIScopes.
00376 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
00377 /// \brief Specialize DIRef constructor for DIScopeRef.
00378 template <> DIRef<DIScope>::DIRef(const Metadata *V);
00379 
00380 /// \brief Handle fields that are references to DITypes.
00381 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
00382 /// \brief Specialize DIRef constructor for DITypeRef.
00383 template <> DIRef<DIType>::DIRef(const Metadata *V);
00384 
00385 /// \briefThis is a wrapper for a type.
00386 ///
00387 /// FIXME: Types should be factored much better so that CV qualifiers and
00388 /// others do not require a huge and empty descriptor full of zeros.
00389 class DIType : public DIScope {
00390 protected:
00391   friend class DIDescriptor;
00392   void printInternal(raw_ostream &OS) const;
00393 
00394 public:
00395   explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
00396   operator DITypeRef () const {
00397     assert(isType() &&
00398            "constructing DITypeRef from an MDNode that is not a type");
00399     return DITypeRef(&*getRef());
00400   }
00401 
00402   bool Verify() const;
00403 
00404   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
00405   StringRef getName() const { return getHeaderField(1); }
00406   unsigned getLineNumber() const {
00407     return getHeaderFieldAs<unsigned>(2);
00408   }
00409   uint64_t getSizeInBits() const {
00410     return getHeaderFieldAs<unsigned>(3);
00411   }
00412   uint64_t getAlignInBits() const {
00413     return getHeaderFieldAs<unsigned>(4);
00414   }
00415   // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
00416   // carry this is just plain insane.
00417   uint64_t getOffsetInBits() const {
00418     return getHeaderFieldAs<unsigned>(5);
00419   }
00420   unsigned getFlags() const { return getHeaderFieldAs<unsigned>(6); }
00421   bool isPrivate() const {
00422     return (getFlags() & FlagAccessibility) == FlagPrivate;
00423   }
00424   bool isProtected() const {
00425     return (getFlags() & FlagAccessibility) == FlagProtected;
00426   }
00427   bool isPublic() const {
00428     return (getFlags() & FlagAccessibility) == FlagPublic;
00429   }
00430   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
00431   bool isAppleBlockExtension() const {
00432     return (getFlags() & FlagAppleBlock) != 0;
00433   }
00434   bool isBlockByrefStruct() const {
00435     return (getFlags() & FlagBlockByrefStruct) != 0;
00436   }
00437   bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
00438   bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
00439   bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
00440   bool isObjcClassComplete() const {
00441     return (getFlags() & FlagObjcClassComplete) != 0;
00442   }
00443   bool isVector() const { return (getFlags() & FlagVector) != 0; }
00444   bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
00445   bool isLValueReference() const {
00446     return (getFlags() & FlagLValueReference) != 0;
00447   }
00448   bool isRValueReference() const {
00449     return (getFlags() & FlagRValueReference) != 0;
00450   }
00451   bool isValid() const { return DbgNode && isType(); }
00452 };
00453 
00454 /// \brief A basic type, like 'int' or 'float'.
00455 class DIBasicType : public DIType {
00456 public:
00457   explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
00458 
00459   unsigned getEncoding() const { return getHeaderFieldAs<unsigned>(7); }
00460 
00461   bool Verify() const;
00462 };
00463 
00464 /// \brief A simple derived type
00465 ///
00466 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
00467 /// Or, a data member of a class/struct/union.
00468 class DIDerivedType : public DIType {
00469   friend class DIDescriptor;
00470   void printInternal(raw_ostream &OS) const;
00471 
00472 public:
00473   explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
00474 
00475   DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(3); }
00476 
00477   /// \brief Return property node, if this ivar is associated with one.
00478   MDNode *getObjCProperty() const;
00479 
00480   DITypeRef getClassType() const {
00481     assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
00482     return getFieldAs<DITypeRef>(4);
00483   }
00484 
00485   Constant *getConstant() const {
00486     assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
00487     return getConstantField(4);
00488   }
00489 
00490   bool Verify() const;
00491 };
00492 
00493 /// \brief Types that refer to multiple other types.
00494 ///
00495 /// This descriptor holds a type that can refer to multiple other types, like a
00496 /// function or struct.
00497 ///
00498 /// DICompositeType is derived from DIDerivedType because some
00499 /// composite types (such as enums) can be derived from basic types
00500 // FIXME: Make this derive from DIType directly & just store the
00501 // base type in a single DIType field.
00502 class DICompositeType : public DIDerivedType {
00503   friend class DIBuilder;
00504   friend class DIDescriptor;
00505   void printInternal(raw_ostream &OS) const;
00506 
00507   /// \brief Set the array of member DITypes.
00508   void setArraysHelper(MDNode *Elements, MDNode *TParams);
00509 
00510 public:
00511   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
00512 
00513   DIArray getElements() const {
00514     assert(!isSubroutineType() && "no elements for DISubroutineType");
00515     return getFieldAs<DIArray>(4);
00516   }
00517 
00518 private:
00519   template <typename T>
00520   void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
00521     assert((!TParams || DbgNode->getNumOperands() == 8) &&
00522            "If you're setting the template parameters this should include a slot "
00523            "for that!");
00524     setArraysHelper(Elements, TParams);
00525   }
00526 
00527 public:
00528   unsigned getRunTimeLang() const {
00529     return getHeaderFieldAs<unsigned>(7);
00530   }
00531   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(5); }
00532 
00533 private:
00534   /// \brief Set the containing type.
00535   void setContainingType(DICompositeType ContainingType);
00536 
00537 public:
00538   DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
00539   MDString *getIdentifier() const;
00540 
00541   bool Verify() const;
00542 };
00543 
00544 class DISubroutineType : public DICompositeType {
00545 public:
00546   explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
00547   DITypedArray<DITypeRef> getTypeArray() const {
00548     return getFieldAs<DITypedArray<DITypeRef>>(4);
00549   }
00550 };
00551 
00552 /// \brief This is a wrapper for a file.
00553 class DIFile : public DIScope {
00554   friend class DIDescriptor;
00555 
00556 public:
00557   explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
00558 
00559   /// \brief Retrieve the MDNode for the directory/file pair.
00560   MDNode *getFileNode() const;
00561   bool Verify() const;
00562 };
00563 
00564 /// \brief A wrapper for a compile unit.
00565 class DICompileUnit : public DIScope {
00566   friend class DIDescriptor;
00567   void printInternal(raw_ostream &OS) const;
00568 
00569 public:
00570   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
00571 
00572   dwarf::SourceLanguage getLanguage() const {
00573     return static_cast<dwarf::SourceLanguage>(getHeaderFieldAs<unsigned>(1));
00574   }
00575   StringRef getProducer() const { return getHeaderField(2); }
00576 
00577   bool isOptimized() const { return getHeaderFieldAs<bool>(3) != 0; }
00578   StringRef getFlags() const { return getHeaderField(4); }
00579   unsigned getRunTimeVersion() const { return getHeaderFieldAs<unsigned>(5); }
00580 
00581   DIArray getEnumTypes() const;
00582   DIArray getRetainedTypes() const;
00583   DIArray getSubprograms() const;
00584   DIArray getGlobalVariables() const;
00585   DIArray getImportedEntities() const;
00586 
00587   void replaceSubprograms(DIArray Subprograms);
00588   void replaceGlobalVariables(DIArray GlobalVariables);
00589 
00590   StringRef getSplitDebugFilename() const { return getHeaderField(6); }
00591   unsigned getEmissionKind() const { return getHeaderFieldAs<unsigned>(7); }
00592 
00593   bool Verify() const;
00594 };
00595 
00596 /// \brief This is a wrapper for a subprogram (e.g. a function).
00597 class DISubprogram : public DIScope {
00598   friend class DIDescriptor;
00599   void printInternal(raw_ostream &OS) const;
00600 
00601 public:
00602   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
00603 
00604   StringRef getName() const { return getHeaderField(1); }
00605   StringRef getDisplayName() const { return getHeaderField(2); }
00606   StringRef getLinkageName() const { return getHeaderField(3); }
00607   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
00608 
00609   /// \brief Check if this is local (like 'static' in C).
00610   unsigned isLocalToUnit() const { return getHeaderFieldAs<unsigned>(5); }
00611   unsigned isDefinition() const { return getHeaderFieldAs<unsigned>(6); }
00612 
00613   unsigned getVirtuality() const { return getHeaderFieldAs<unsigned>(7); }
00614   unsigned getVirtualIndex() const { return getHeaderFieldAs<unsigned>(8); }
00615 
00616   unsigned getFlags() const { return getHeaderFieldAs<unsigned>(9); }
00617 
00618   unsigned isOptimized() const { return getHeaderFieldAs<bool>(10); }
00619 
00620   /// \brief Get the beginning of the scope of the function (not the name).
00621   unsigned getScopeLineNumber() const { return getHeaderFieldAs<unsigned>(11); }
00622 
00623   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
00624   DISubroutineType getType() const { return getFieldAs<DISubroutineType>(3); }
00625 
00626   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(4); }
00627 
00628   bool Verify() const;
00629 
00630   /// \brief Check if this provides debugging information for the function F.
00631   bool describes(const Function *F);
00632 
00633   Function *getFunction() const { return getFunctionField(5); }
00634   void replaceFunction(Function *F) { replaceFunctionField(5, F); }
00635   DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
00636   DISubprogram getFunctionDeclaration() const {
00637     return getFieldAs<DISubprogram>(7);
00638   }
00639   MDNode *getVariablesNodes() const;
00640   DIArray getVariables() const;
00641 
00642   unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
00643   /// \brief Check for the "private" access specifier.
00644   bool isPrivate() const {
00645     return (getFlags() & FlagAccessibility) == FlagPrivate;
00646   }
00647   /// \brief Check for the "protected" access specifier.
00648   bool isProtected() const {
00649     return (getFlags() & FlagAccessibility) == FlagProtected;
00650   }
00651   /// \brief Check for the "public" access specifier.
00652   bool isPublic() const {
00653     return (getFlags() & FlagAccessibility) == FlagPublic;
00654   }
00655   /// \brief Check for "explicit".
00656   bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
00657   /// \brief Check if this is prototyped.
00658   bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
00659 
00660   /// \brief Check if this is reference-qualified.
00661   ///
00662   /// Return true if this subprogram is a C++11 reference-qualified non-static
00663   /// member function (void foo() &).
00664   unsigned isLValueReference() const {
00665     return (getFlags() & FlagLValueReference) != 0;
00666   }
00667 
00668   /// \brief Check if this is rvalue-reference-qualified.
00669   ///
00670   /// Return true if this subprogram is a C++11 rvalue-reference-qualified
00671   /// non-static member function (void foo() &&).
00672   unsigned isRValueReference() const {
00673     return (getFlags() & FlagRValueReference) != 0;
00674   }
00675 
00676 };
00677 
00678 /// \brief This is a wrapper for a lexical block.
00679 class DILexicalBlock : public DIScope {
00680 public:
00681   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
00682   DIScope getContext() const { return getFieldAs<DIScope>(2); }
00683   unsigned getLineNumber() const {
00684     return getHeaderFieldAs<unsigned>(1);
00685   }
00686   unsigned getColumnNumber() const {
00687     return getHeaderFieldAs<unsigned>(2);
00688   }
00689   bool Verify() const;
00690 };
00691 
00692 /// \brief This is a wrapper for a lexical block with a filename change.
00693 class DILexicalBlockFile : public DIScope {
00694 public:
00695   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
00696   DIScope getContext() const {
00697     if (getScope().isSubprogram())
00698       return getScope();
00699     return getScope().getContext();
00700   }
00701   unsigned getLineNumber() const { return getScope().getLineNumber(); }
00702   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
00703   DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
00704   unsigned getDiscriminator() const { return getHeaderFieldAs<unsigned>(1); }
00705   bool Verify() const;
00706 };
00707 
00708 /// \brief A wrapper for a C++ style name space.
00709 class DINameSpace : public DIScope {
00710   friend class DIDescriptor;
00711   void printInternal(raw_ostream &OS) const;
00712 
00713 public:
00714   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
00715   StringRef getName() const { return getHeaderField(1); }
00716   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
00717   DIScope getContext() const { return getFieldAs<DIScope>(2); }
00718   bool Verify() const;
00719 };
00720 
00721 /// \brief This is a wrapper for template type parameter.
00722 class DITemplateTypeParameter : public DIDescriptor {
00723 public:
00724   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
00725     : DIDescriptor(N) {}
00726 
00727   StringRef getName() const { return getHeaderField(1); }
00728   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
00729   unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
00730 
00731   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
00732   DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
00733   StringRef getFilename() const { return getFieldAs<DIFile>(3).getFilename(); }
00734   StringRef getDirectory() const {
00735     return getFieldAs<DIFile>(3).getDirectory();
00736   }
00737   bool Verify() const;
00738 };
00739 
00740 /// \brief This is a wrapper for template value parameter.
00741 class DITemplateValueParameter : public DIDescriptor {
00742 public:
00743   explicit DITemplateValueParameter(const MDNode *N = nullptr)
00744     : DIDescriptor(N) {}
00745 
00746   StringRef getName() const { return getHeaderField(1); }
00747   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
00748   unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
00749 
00750   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
00751   DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
00752   Metadata *getValue() const;
00753   StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
00754   StringRef getDirectory() const {
00755     return getFieldAs<DIFile>(4).getDirectory();
00756   }
00757   bool Verify() const;
00758 };
00759 
00760 /// \brief This is a wrapper for a global variable.
00761 class DIGlobalVariable : public DIDescriptor {
00762   friend class DIDescriptor;
00763   void printInternal(raw_ostream &OS) const;
00764 
00765 public:
00766   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00767 
00768   StringRef getName() const { return getHeaderField(1); }
00769   StringRef getDisplayName() const { return getHeaderField(2); }
00770   StringRef getLinkageName() const { return getHeaderField(3); }
00771   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
00772   unsigned isLocalToUnit() const { return getHeaderFieldAs<bool>(5); }
00773   unsigned isDefinition() const { return getHeaderFieldAs<bool>(6); }
00774 
00775   DIScope getContext() const { return getFieldAs<DIScope>(1); }
00776   StringRef getFilename() const { return getFieldAs<DIFile>(2).getFilename(); }
00777   StringRef getDirectory() const {
00778     return getFieldAs<DIFile>(2).getDirectory();
00779   }
00780   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
00781 
00782   GlobalVariable *getGlobal() const { return getGlobalVariableField(4); }
00783   Constant *getConstant() const { return getConstantField(4); }
00784   DIDerivedType getStaticDataMemberDeclaration() const {
00785     return getFieldAs<DIDerivedType>(5);
00786   }
00787 
00788   bool Verify() const;
00789 };
00790 
00791 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
00792 class DIVariable : public DIDescriptor {
00793   friend class DIDescriptor;
00794   void printInternal(raw_ostream &OS) const;
00795 
00796 public:
00797   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00798 
00799   StringRef getName() const { return getHeaderField(1); }
00800   unsigned getLineNumber() const {
00801     // FIXME: Line number and arg number shouldn't be merged together like this.
00802     return (getHeaderFieldAs<unsigned>(2) << 8) >> 8;
00803   }
00804   unsigned getArgNumber() const { return getHeaderFieldAs<unsigned>(2) >> 24; }
00805 
00806   DIScope getContext() const { return getFieldAs<DIScope>(1); }
00807   DIFile getFile() const { return getFieldAs<DIFile>(2); }
00808   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
00809 
00810   /// \brief Return true if this variable is marked as "artificial".
00811   bool isArtificial() const {
00812     return (getHeaderFieldAs<unsigned>(3) & FlagArtificial) != 0;
00813   }
00814 
00815   bool isObjectPointer() const {
00816     return (getHeaderFieldAs<unsigned>(3) & FlagObjectPointer) != 0;
00817   }
00818 
00819   /// \brief Return true if this variable is represented as a pointer.
00820   bool isIndirect() const {
00821     return (getHeaderFieldAs<unsigned>(3) & FlagIndirectVariable) != 0;
00822   }
00823 
00824   /// \brief If this variable is inlined then return inline location.
00825   MDNode *getInlinedAt() const;
00826 
00827   bool Verify() const;
00828 
00829   /// \brief Check if this is a "__block" variable (Apple Blocks).
00830   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
00831     return (getType().resolve(Map)).isBlockByrefStruct();
00832   }
00833 
00834   /// \brief Check if this is an inlined function argument.
00835   bool isInlinedFnArgument(const Function *CurFn);
00836 
00837   /// \brief Return the size reported by the variable's type.
00838   unsigned getSizeInBits(const DITypeIdentifierMap &Map);
00839 
00840   void printExtendedName(raw_ostream &OS) const;
00841 };
00842 
00843 /// \brief A complex location expression.
00844 class DIExpression : public DIDescriptor {
00845   friend class DIDescriptor;
00846   void printInternal(raw_ostream &OS) const;
00847 
00848 public:
00849   explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
00850 
00851   bool Verify() const;
00852 
00853   /// \brief Return the number of elements in the complex expression.
00854   unsigned getNumElements() const {
00855     if (!DbgNode)
00856       return 0;
00857     unsigned N = getNumHeaderFields();
00858     assert(N > 0 && "missing tag");
00859     return N - 1;
00860   }
00861 
00862   /// \brief return the Idx'th complex address element.
00863   uint64_t getElement(unsigned Idx) const;
00864 
00865   /// \brief Return whether this is a piece of an aggregate variable.
00866   bool isVariablePiece() const;
00867   /// \brief Return the offset of this piece in bytes.
00868   uint64_t getPieceOffset() const;
00869   /// \brief Return the size of this piece in bytes.
00870   uint64_t getPieceSize() const;
00871 };
00872 
00873 /// \brief This object holds location information.
00874 ///
00875 /// This object is not associated with any DWARF tag.
00876 class DILocation : public DIDescriptor {
00877 public:
00878   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
00879 
00880   unsigned getLineNumber() const { return getUnsignedField(0); }
00881   unsigned getColumnNumber() const { return getUnsignedField(1); }
00882   DIScope getScope() const { return getFieldAs<DIScope>(2); }
00883   DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
00884   StringRef getFilename() const { return getScope().getFilename(); }
00885   StringRef getDirectory() const { return getScope().getDirectory(); }
00886   bool Verify() const;
00887   bool atSameLineAs(const DILocation &Other) const {
00888     return (getLineNumber() == Other.getLineNumber() &&
00889             getFilename() == Other.getFilename());
00890   }
00891   /// \brief Get the DWAF discriminator.
00892   ///
00893   /// DWARF discriminators are used to distinguish identical file locations for
00894   /// instructions that are on different basic blocks. If two instructions are
00895   /// inside the same lexical block and are in different basic blocks, we
00896   /// create a new lexical block with identical location as the original but
00897   /// with a different discriminator value
00898   /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
00899   unsigned getDiscriminator() const {
00900     // Since discriminators are associated with lexical blocks, make
00901     // sure this location is a lexical block before retrieving its
00902     // value.
00903     return getScope().isLexicalBlockFile()
00904                ? getFieldAs<DILexicalBlockFile>(2).getDiscriminator()
00905                : 0;
00906   }
00907 
00908   /// \brief Generate a new discriminator value for this location.
00909   unsigned computeNewDiscriminator(LLVMContext &Ctx);
00910 
00911   /// \brief Return a copy of this location with a different scope.
00912   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
00913 };
00914 
00915 class DIObjCProperty : public DIDescriptor {
00916   friend class DIDescriptor;
00917   void printInternal(raw_ostream &OS) const;
00918 
00919 public:
00920   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
00921 
00922   StringRef getObjCPropertyName() const { return getHeaderField(1); }
00923   DIFile getFile() const { return getFieldAs<DIFile>(1); }
00924   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
00925 
00926   StringRef getObjCPropertyGetterName() const { return getHeaderField(3); }
00927   StringRef getObjCPropertySetterName() const { return getHeaderField(4); }
00928   unsigned getAttributes() const { return getHeaderFieldAs<unsigned>(5); }
00929   bool isReadOnlyObjCProperty() const {
00930     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
00931   }
00932   bool isReadWriteObjCProperty() const {
00933     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
00934   }
00935   bool isAssignObjCProperty() const {
00936     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
00937   }
00938   bool isRetainObjCProperty() const {
00939     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
00940   }
00941   bool isCopyObjCProperty() const {
00942     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
00943   }
00944   bool isNonAtomicObjCProperty() const {
00945     return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
00946   }
00947 
00948   /// \brief Get the type.
00949   ///
00950   /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
00951   /// the type as a DITypeRef here.
00952   DIType getType() const { return getFieldAs<DIType>(2); }
00953 
00954   bool Verify() const;
00955 };
00956 
00957 /// \brief An imported module (C++ using directive or similar).
00958 class DIImportedEntity : public DIDescriptor {
00959   friend class DIDescriptor;
00960   void printInternal(raw_ostream &OS) const;
00961 
00962 public:
00963   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
00964   DIScope getContext() const { return getFieldAs<DIScope>(1); }
00965   DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
00966   unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(1); }
00967   StringRef getName() const { return getHeaderField(2); }
00968   bool Verify() const;
00969 };
00970 
00971 /// \brief Find subprogram that is enclosing this scope.
00972 DISubprogram getDISubprogram(const MDNode *Scope);
00973 
00974 /// \brief Find debug info for a given function.
00975 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
00976 /// DISubprogram.
00977 DISubprogram getDISubprogram(const Function *F);
00978 
00979 /// \brief Find underlying composite type.
00980 DICompositeType getDICompositeType(DIType T);
00981 
00982 /// \brief Create a new inlined variable based on current variable.
00983 ///
00984 /// @param DV            Current Variable.
00985 /// @param InlinedScope  Location at current variable is inlined.
00986 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
00987                                  LLVMContext &VMContext);
00988 
00989 /// \brief Remove inlined scope from the variable.
00990 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
00991 
00992 /// \brief Generate map by visiting all retained types.
00993 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
00994 
00995 /// \brief Strip debug info in the module if it exists.
00996 ///
00997 /// To do this, we remove all calls to the debugger intrinsics and any named
00998 /// metadata for debugging. We also remove debug locations for instructions.
00999 /// Return true if module is modified.
01000 bool StripDebugInfo(Module &M);
01001 
01002 /// \brief Return Debug Info Metadata Version by checking module flags.
01003 unsigned getDebugMetadataVersionFromModule(const Module &M);
01004 
01005 /// \brief Utility to find all debug info in a module.
01006 ///
01007 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
01008 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
01009 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
01010 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
01011 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
01012 /// used by the CUs.
01013 class DebugInfoFinder {
01014 public:
01015   DebugInfoFinder() : TypeMapInitialized(false) {}
01016 
01017   /// \brief Process entire module and collect debug info anchors.
01018   void processModule(const Module &M);
01019 
01020   /// \brief Process DbgDeclareInst.
01021   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
01022   /// \brief Process DbgValueInst.
01023   void processValue(const Module &M, const DbgValueInst *DVI);
01024   /// \brief Process DILocation.
01025   void processLocation(const Module &M, DILocation Loc);
01026 
01027   /// \brief Clear all lists.
01028   void reset();
01029 
01030 private:
01031   void InitializeTypeMap(const Module &M);
01032 
01033   void processType(DIType DT);
01034   void processSubprogram(DISubprogram SP);
01035   void processScope(DIScope Scope);
01036   bool addCompileUnit(DICompileUnit CU);
01037   bool addGlobalVariable(DIGlobalVariable DIG);
01038   bool addSubprogram(DISubprogram SP);
01039   bool addType(DIType DT);
01040   bool addScope(DIScope Scope);
01041 
01042 public:
01043   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
01044   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
01045   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
01046   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
01047   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
01048 
01049   iterator_range<compile_unit_iterator> compile_units() const {
01050     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
01051   }
01052 
01053   iterator_range<subprogram_iterator> subprograms() const {
01054     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
01055   }
01056 
01057   iterator_range<global_variable_iterator> global_variables() const {
01058     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
01059   }
01060 
01061   iterator_range<type_iterator> types() const {
01062     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
01063   }
01064 
01065   iterator_range<scope_iterator> scopes() const {
01066     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
01067   }
01068 
01069   unsigned compile_unit_count() const { return CUs.size(); }
01070   unsigned global_variable_count() const { return GVs.size(); }
01071   unsigned subprogram_count() const { return SPs.size(); }
01072   unsigned type_count() const { return TYs.size(); }
01073   unsigned scope_count() const { return Scopes.size(); }
01074 
01075 private:
01076   SmallVector<DICompileUnit, 8> CUs;
01077   SmallVector<DISubprogram, 8> SPs;
01078   SmallVector<DIGlobalVariable, 8> GVs;
01079   SmallVector<DIType, 8> TYs;
01080   SmallVector<DIScope, 8> Scopes;
01081   SmallPtrSet<MDNode *, 64> NodesSeen;
01082   DITypeIdentifierMap TypeIdentifierMap;
01083 
01084   /// \brief Specify if TypeIdentifierMap is initialized.
01085   bool TypeMapInitialized;
01086 };
01087 
01088 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
01089 
01090 } // end namespace llvm
01091 
01092 #endif