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