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