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