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(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   unsigned getLanguage() const { return getUnsignedField(2); }
00431   StringRef getProducer() const { return getStringField(3); }
00432 
00433   bool isOptimized() const { return getUnsignedField(4) != 0; }
00434   StringRef getFlags() const { return getStringField(5); }
00435   unsigned getRunTimeVersion() const { return getUnsignedField(6); }
00436 
00437   DIArray getEnumTypes() const;
00438   DIArray getRetainedTypes() const;
00439   DIArray getSubprograms() const;
00440   DIArray getGlobalVariables() const;
00441   DIArray getImportedEntities() const;
00442 
00443   StringRef getSplitDebugFilename() const { return getStringField(12); }
00444   unsigned getEmissionKind() const { return getUnsignedField(13); }
00445 
00446   /// Verify - Verify that a compile unit is well formed.
00447   bool Verify() const;
00448 };
00449 
00450 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
00451 class DISubprogram : public DIScope {
00452   friend class DIDescriptor;
00453   void printInternal(raw_ostream &OS) const;
00454 
00455 public:
00456   explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
00457 
00458   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
00459   StringRef getName() const { return getStringField(3); }
00460   StringRef getDisplayName() const { return getStringField(4); }
00461   StringRef getLinkageName() const { return getStringField(5); }
00462   unsigned getLineNumber() const { return getUnsignedField(6); }
00463   DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
00464 
00465   /// isLocalToUnit - Return true if this subprogram is local to the current
00466   /// compile unit, like 'static' in C.
00467   unsigned isLocalToUnit() const { return getUnsignedField(8); }
00468   unsigned isDefinition() const { return getUnsignedField(9); }
00469 
00470   unsigned getVirtuality() const { return getUnsignedField(10); }
00471   unsigned getVirtualIndex() const { return getUnsignedField(11); }
00472 
00473   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
00474 
00475   unsigned getFlags() const { return getUnsignedField(13); }
00476 
00477   unsigned isArtificial() const {
00478     return (getUnsignedField(13) & FlagArtificial) != 0;
00479   }
00480   /// isPrivate - Return true if this subprogram has "private"
00481   /// access specifier.
00482   bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
00483   /// isProtected - Return true if this subprogram has "protected"
00484   /// access specifier.
00485   bool isProtected() const {
00486     return (getUnsignedField(13) & FlagProtected) != 0;
00487   }
00488   /// isExplicit - Return true if this subprogram is marked as explicit.
00489   bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
00490   /// isPrototyped - Return true if this subprogram is prototyped.
00491   bool isPrototyped() const {
00492     return (getUnsignedField(13) & FlagPrototyped) != 0;
00493   }
00494 
00495   /// Return true if this subprogram is a C++11 reference-qualified
00496   /// non-static member function (void foo() &).
00497   unsigned isLValueReference() const {
00498     return (getUnsignedField(13) & FlagLValueReference) != 0;
00499   }
00500 
00501   /// Return true if this subprogram is a C++11
00502   /// rvalue-reference-qualified non-static member function
00503   /// (void foo() &&).
00504   unsigned isRValueReference() const {
00505     return (getUnsignedField(13) & FlagRValueReference) != 0;
00506   }
00507 
00508   unsigned isOptimized() const;
00509 
00510   /// Verify - Verify that a subprogram descriptor is well formed.
00511   bool Verify() const;
00512 
00513   /// describes - Return true if this subprogram provides debugging
00514   /// information for the function F.
00515   bool describes(const Function *F);
00516 
00517   Function *getFunction() const { return getFunctionField(15); }
00518   void replaceFunction(Function *F) { replaceFunctionField(15, F); }
00519   DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
00520   DISubprogram getFunctionDeclaration() const {
00521     return getFieldAs<DISubprogram>(17);
00522   }
00523   MDNode *getVariablesNodes() const;
00524   DIArray getVariables() const;
00525 
00526   /// getScopeLineNumber - Get the beginning of the scope of the
00527   /// function, not necessarily where the name of the program
00528   /// starts.
00529   unsigned getScopeLineNumber() const { return getUnsignedField(19); }
00530 };
00531 
00532 /// DILexicalBlock - This is a wrapper for a lexical block.
00533 class DILexicalBlock : public DIScope {
00534 public:
00535   explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
00536   DIScope getContext() const { return getFieldAs<DIScope>(2); }
00537   unsigned getLineNumber() const { return getUnsignedField(3); }
00538   unsigned getColumnNumber() const { return getUnsignedField(4); }
00539   unsigned getDiscriminator() const { return getUnsignedField(5); }
00540   bool Verify() const;
00541 };
00542 
00543 /// DILexicalBlockFile - This is a wrapper for a lexical block with
00544 /// a filename change.
00545 class DILexicalBlockFile : public DIScope {
00546 public:
00547   explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
00548   DIScope getContext() const {
00549     if (getScope().isSubprogram())
00550       return getScope();
00551     return getScope().getContext();
00552   }
00553   unsigned getLineNumber() const { return getScope().getLineNumber(); }
00554   unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
00555   DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
00556   bool Verify() const;
00557 };
00558 
00559 /// DINameSpace - A wrapper for a C++ style name space.
00560 class DINameSpace : public DIScope {
00561   friend class DIDescriptor;
00562   void printInternal(raw_ostream &OS) const;
00563 
00564 public:
00565   explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
00566   DIScope getContext() const { return getFieldAs<DIScope>(2); }
00567   StringRef getName() const { return getStringField(3); }
00568   unsigned getLineNumber() const { return getUnsignedField(4); }
00569   bool Verify() const;
00570 };
00571 
00572 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
00573 class DIUnspecifiedParameter : public DIDescriptor {
00574 public:
00575   explicit DIUnspecifiedParameter(const MDNode *N = nullptr)
00576     : DIDescriptor(N) {}
00577   bool Verify() const;
00578 };
00579 
00580 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
00581 class DITemplateTypeParameter : public DIDescriptor {
00582 public:
00583   explicit DITemplateTypeParameter(const MDNode *N = nullptr)
00584     : DIDescriptor(N) {}
00585 
00586   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
00587   StringRef getName() const { return getStringField(2); }
00588   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
00589   StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
00590   StringRef getDirectory() const {
00591     return getFieldAs<DIFile>(4).getDirectory();
00592   }
00593   unsigned getLineNumber() const { return getUnsignedField(5); }
00594   unsigned getColumnNumber() const { return getUnsignedField(6); }
00595   bool Verify() const;
00596 };
00597 
00598 /// DITemplateValueParameter - This is a wrapper for template value parameter.
00599 class DITemplateValueParameter : public DIDescriptor {
00600 public:
00601   explicit DITemplateValueParameter(const MDNode *N = nullptr)
00602     : DIDescriptor(N) {}
00603 
00604   DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
00605   StringRef getName() const { return getStringField(2); }
00606   DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
00607   Value *getValue() const;
00608   StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
00609   StringRef getDirectory() const {
00610     return getFieldAs<DIFile>(5).getDirectory();
00611   }
00612   unsigned getLineNumber() const { return getUnsignedField(6); }
00613   unsigned getColumnNumber() const { return getUnsignedField(7); }
00614   bool Verify() const;
00615 };
00616 
00617 /// DIGlobalVariable - This is a wrapper for a global variable.
00618 class DIGlobalVariable : public DIDescriptor {
00619   friend class DIDescriptor;
00620   void printInternal(raw_ostream &OS) const;
00621 
00622 public:
00623   explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00624 
00625   DIScope getContext() const { return getFieldAs<DIScope>(2); }
00626   StringRef getName() const { return getStringField(3); }
00627   StringRef getDisplayName() const { return getStringField(4); }
00628   StringRef getLinkageName() const { return getStringField(5); }
00629   StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
00630   StringRef getDirectory() const {
00631     return getFieldAs<DIFile>(6).getDirectory();
00632   }
00633 
00634   unsigned getLineNumber() const { return getUnsignedField(7); }
00635   DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
00636   unsigned isLocalToUnit() const { return getUnsignedField(9); }
00637   unsigned isDefinition() const { return getUnsignedField(10); }
00638 
00639   GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
00640   Constant *getConstant() const { return getConstantField(11); }
00641   DIDerivedType getStaticDataMemberDeclaration() const {
00642     return getFieldAs<DIDerivedType>(12);
00643   }
00644 
00645   /// Verify - Verify that a global variable descriptor is well formed.
00646   bool Verify() const;
00647 };
00648 
00649 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
00650 /// global etc).
00651 class DIVariable : public DIDescriptor {
00652   friend class DIDescriptor;
00653   void printInternal(raw_ostream &OS) const;
00654 
00655 public:
00656   explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
00657 
00658   DIScope getContext() const { return getFieldAs<DIScope>(1); }
00659   StringRef getName() const { return getStringField(2); }
00660   DIFile getFile() const { return getFieldAs<DIFile>(3); }
00661   unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
00662   unsigned getArgNumber() const {
00663     unsigned L = getUnsignedField(4);
00664     return L >> 24;
00665   }
00666   DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
00667 
00668   /// isArtificial - Return true if this variable is marked as "artificial".
00669   bool isArtificial() const {
00670     return (getUnsignedField(6) & FlagArtificial) != 0;
00671   }
00672 
00673   bool isObjectPointer() const {
00674     return (getUnsignedField(6) & FlagObjectPointer) != 0;
00675   }
00676 
00677   /// \brief Return true if this variable is represented as a pointer.
00678   bool isIndirect() const {
00679     return (getUnsignedField(6) & FlagIndirectVariable) != 0;
00680   }
00681 
00682   /// getInlinedAt - If this variable is inlined then return inline location.
00683   MDNode *getInlinedAt() const;
00684 
00685   /// Verify - Verify that a variable descriptor is well formed.
00686   bool Verify() const;
00687 
00688   /// HasComplexAddr - Return true if the variable has a complex address.
00689   bool hasComplexAddress() const { return getNumAddrElements() > 0; }
00690 
00691   unsigned getNumAddrElements() const;
00692 
00693   uint64_t getAddrElement(unsigned Idx) const {
00694     return getUInt64Field(Idx + 8);
00695   }
00696 
00697   /// isBlockByrefVariable - Return true if the variable was declared as
00698   /// a "__block" variable (Apple Blocks).
00699   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
00700     return (getType().resolve(Map)).isBlockByrefStruct();
00701   }
00702 
00703   /// isInlinedFnArgument - Return true if this variable provides debugging
00704   /// information for an inlined function arguments.
00705   bool isInlinedFnArgument(const Function *CurFn);
00706 
00707   void printExtendedName(raw_ostream &OS) const;
00708 };
00709 
00710 /// DILocation - This object holds location information. This object
00711 /// is not associated with any DWARF tag.
00712 class DILocation : public DIDescriptor {
00713 public:
00714   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
00715 
00716   unsigned getLineNumber() const { return getUnsignedField(0); }
00717   unsigned getColumnNumber() const { return getUnsignedField(1); }
00718   DIScope getScope() const { return getFieldAs<DIScope>(2); }
00719   DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
00720   StringRef getFilename() const { return getScope().getFilename(); }
00721   StringRef getDirectory() const { return getScope().getDirectory(); }
00722   bool Verify() const;
00723   bool atSameLineAs(const DILocation &Other) const {
00724     return (getLineNumber() == Other.getLineNumber() &&
00725             getFilename() == Other.getFilename());
00726   }
00727   /// getDiscriminator - DWARF discriminators are used to distinguish
00728   /// identical file locations for instructions that are on different
00729   /// basic blocks. If two instructions are inside the same lexical block
00730   /// and are in different basic blocks, we create a new lexical block
00731   /// with identical location as the original but with a different
00732   /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
00733   /// for details).
00734   unsigned getDiscriminator() const {
00735     // Since discriminators are associated with lexical blocks, make
00736     // sure this location is a lexical block before retrieving its
00737     // value.
00738     return getScope().isLexicalBlock()
00739                ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
00740                : 0;
00741   }
00742   unsigned computeNewDiscriminator(LLVMContext &Ctx);
00743   DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
00744 };
00745 
00746 class DIObjCProperty : public DIDescriptor {
00747   friend class DIDescriptor;
00748   void printInternal(raw_ostream &OS) const;
00749 
00750 public:
00751   explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
00752 
00753   StringRef getObjCPropertyName() const { return getStringField(1); }
00754   DIFile getFile() const { return getFieldAs<DIFile>(2); }
00755   unsigned getLineNumber() const { return getUnsignedField(3); }
00756 
00757   StringRef getObjCPropertyGetterName() const { return getStringField(4); }
00758   StringRef getObjCPropertySetterName() const { return getStringField(5); }
00759   bool isReadOnlyObjCProperty() const {
00760     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
00761   }
00762   bool isReadWriteObjCProperty() const {
00763     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
00764   }
00765   bool isAssignObjCProperty() const {
00766     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
00767   }
00768   bool isRetainObjCProperty() const {
00769     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
00770   }
00771   bool isCopyObjCProperty() const {
00772     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
00773   }
00774   bool isNonAtomicObjCProperty() const {
00775     return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
00776   }
00777 
00778   /// Objective-C doesn't have an ODR, so there is no benefit in storing
00779   /// the type as a DITypeRef here.
00780   DIType getType() const { return getFieldAs<DIType>(7); }
00781 
00782   /// Verify - Verify that a derived type descriptor is well formed.
00783   bool Verify() const;
00784 };
00785 
00786 /// \brief An imported module (C++ using directive or similar).
00787 class DIImportedEntity : public DIDescriptor {
00788   friend class DIDescriptor;
00789   void printInternal(raw_ostream &OS) const;
00790 
00791 public:
00792   explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
00793   DIScope getContext() const { return getFieldAs<DIScope>(1); }
00794   DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
00795   unsigned getLineNumber() const { return getUnsignedField(3); }
00796   StringRef getName() const { return getStringField(4); }
00797   bool Verify() const;
00798 };
00799 
00800 /// getDISubprogram - Find subprogram that is enclosing this scope.
00801 DISubprogram getDISubprogram(const MDNode *Scope);
00802 
00803 /// getDICompositeType - Find underlying composite type.
00804 DICompositeType getDICompositeType(DIType T);
00805 
00806 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
00807 /// to hold function specific information.
00808 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
00809 
00810 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
00811 /// suitable to hold function specific information.
00812 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
00813 
00814 /// createInlinedVariable - Create a new inlined variable based on current
00815 /// variable.
00816 /// @param DV            Current Variable.
00817 /// @param InlinedScope  Location at current variable is inlined.
00818 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
00819                                  LLVMContext &VMContext);
00820 
00821 /// cleanseInlinedVariable - Remove inlined scope from the variable.
00822 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
00823 
00824 /// Construct DITypeIdentifierMap by going through retained types of each CU.
00825 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
00826 
00827 /// Strip debug info in the module if it exists.
00828 /// To do this, we remove all calls to the debugger intrinsics and any named
00829 /// metadata for debugging. We also remove debug locations for instructions.
00830 /// Return true if module is modified.
00831 bool StripDebugInfo(Module &M);
00832 
00833 /// Return Debug Info Metadata Version by checking module flags.
00834 unsigned getDebugMetadataVersionFromModule(const Module &M);
00835 
00836 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
00837 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
00838 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
00839 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
00840 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
00841 /// used by the CUs.
00842 class DebugInfoFinder {
00843 public:
00844   DebugInfoFinder() : TypeMapInitialized(false) {}
00845 
00846   /// processModule - Process entire module and collect debug info
00847   /// anchors.
00848   void processModule(const Module &M);
00849 
00850   /// processDeclare - Process DbgDeclareInst.
00851   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
00852   /// Process DbgValueInst.
00853   void processValue(const Module &M, const DbgValueInst *DVI);
00854   /// processLocation - Process DILocation.
00855   void processLocation(const Module &M, DILocation Loc);
00856 
00857   /// Clear all lists.
00858   void reset();
00859 
00860 private:
00861   /// Initialize TypeIdentifierMap.
00862   void InitializeTypeMap(const Module &M);
00863 
00864   /// processType - Process DIType.
00865   void processType(DIType DT);
00866 
00867   /// processSubprogram - Process DISubprogram.
00868   void processSubprogram(DISubprogram SP);
00869 
00870   void processScope(DIScope Scope);
00871 
00872   /// addCompileUnit - Add compile unit into CUs.
00873   bool addCompileUnit(DICompileUnit CU);
00874 
00875   /// addGlobalVariable - Add global variable into GVs.
00876   bool addGlobalVariable(DIGlobalVariable DIG);
00877 
00878   // addSubprogram - Add subprogram into SPs.
00879   bool addSubprogram(DISubprogram SP);
00880 
00881   /// addType - Add type into Tys.
00882   bool addType(DIType DT);
00883 
00884   bool addScope(DIScope Scope);
00885 
00886 public:
00887   typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
00888   typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
00889   typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
00890   typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
00891   typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
00892 
00893   iterator_range<compile_unit_iterator> compile_units() const {
00894     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
00895   }
00896 
00897   iterator_range<subprogram_iterator> subprograms() const {
00898     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
00899   }
00900 
00901   iterator_range<global_variable_iterator> global_variables() const {
00902     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
00903   }
00904 
00905   iterator_range<type_iterator> types() const {
00906     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
00907   }
00908 
00909   iterator_range<scope_iterator> scopes() const {
00910     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
00911   }
00912 
00913   unsigned compile_unit_count() const { return CUs.size(); }
00914   unsigned global_variable_count() const { return GVs.size(); }
00915   unsigned subprogram_count() const { return SPs.size(); }
00916   unsigned type_count() const { return TYs.size(); }
00917   unsigned scope_count() const { return Scopes.size(); }
00918 
00919 private:
00920   SmallVector<DICompileUnit, 8> CUs;    // Compile Units
00921   SmallVector<DISubprogram, 8> SPs;    // Subprograms
00922   SmallVector<DIGlobalVariable, 8> GVs;    // Global Variables;
00923   SmallVector<DIType, 8> TYs;    // Types
00924   SmallVector<DIScope, 8> Scopes; // Scopes
00925   SmallPtrSet<MDNode *, 64> NodesSeen;
00926   DITypeIdentifierMap TypeIdentifierMap;
00927   /// Specify if TypeIdentifierMap is initialized.
00928   bool TypeMapInitialized;
00929 };
00930 } // end namespace llvm
00931 
00932 #endif