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