CGDebugInfo.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the source level debug info generator for llvm translation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef CLANG_CODEGEN_CGDEBUGINFO_H
  14. #define CLANG_CODEGEN_CGDEBUGINFO_H
  15. #include "clang/AST/Type.h"
  16. #include "clang/AST/Expr.h"
  17. #include "clang/Basic/SourceLocation.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/Analysis/DebugInfo.h"
  20. #include "llvm/Analysis/DIBuilder.h"
  21. #include "llvm/Support/ValueHandle.h"
  22. #include "llvm/Support/Allocator.h"
  23. #include "CGBuilder.h"
  24. namespace llvm {
  25. class MDNode;
  26. }
  27. namespace clang {
  28. class VarDecl;
  29. class ObjCInterfaceDecl;
  30. class ClassTemplateSpecializationDecl;
  31. class GlobalDecl;
  32. namespace CodeGen {
  33. class CodeGenModule;
  34. class CodeGenFunction;
  35. class CGBlockInfo;
  36. /// CGDebugInfo - This class gathers all debug information during compilation
  37. /// and is responsible for emitting to llvm globals or pass directly to
  38. /// the backend.
  39. class CGDebugInfo {
  40. CodeGenModule &CGM;
  41. llvm::DIBuilder DBuilder;
  42. llvm::DICompileUnit TheCU;
  43. SourceLocation CurLoc, PrevLoc;
  44. llvm::DIType VTablePtrType;
  45. /// TypeCache - Cache of previously constructed Types.
  46. llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
  47. /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
  48. llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
  49. bool BlockLiteralGenericSet;
  50. llvm::DIType BlockLiteralGeneric;
  51. // LexicalBlockStack - Keep track of our current nested lexical block.
  52. std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
  53. llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
  54. // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
  55. // beginning of a function. This is used to pop unbalanced regions at
  56. // the end of a function.
  57. std::vector<unsigned> FnBeginRegionCount;
  58. /// DebugInfoNames - This is a storage for names that are
  59. /// constructed on demand. For example, C++ destructors, C++ operators etc..
  60. llvm::BumpPtrAllocator DebugInfoNames;
  61. StringRef CWDName;
  62. llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
  63. llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
  64. llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
  65. /// Helper functions for getOrCreateType.
  66. llvm::DIType CreateType(const BuiltinType *Ty);
  67. llvm::DIType CreateType(const ComplexType *Ty);
  68. llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
  69. llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
  70. llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
  71. llvm::DIFile F);
  72. llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
  73. llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
  74. llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
  75. llvm::DIType CreateType(const RecordType *Ty);
  76. llvm::DIType CreateLimitedType(const RecordType *Ty);
  77. llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
  78. llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
  79. llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
  80. llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
  81. llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
  82. llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
  83. llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
  84. llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
  85. llvm::DIType CreateEnumType(const EnumDecl *ED);
  86. llvm::DIType getTypeOrNull(const QualType);
  87. llvm::DIType getCompletedTypeOrNull(const QualType);
  88. llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
  89. llvm::DIFile F);
  90. llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
  91. llvm::DIFile F);
  92. llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
  93. llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
  94. llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
  95. llvm::DIType CreatePointerLikeType(unsigned Tag,
  96. const Type *Ty, QualType PointeeTy,
  97. llvm::DIFile F);
  98. llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
  99. llvm::DIFile F,
  100. llvm::DIType RecordTy);
  101. void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
  102. llvm::DIFile F,
  103. SmallVectorImpl<llvm::Value *> &E,
  104. llvm::DIType T);
  105. void CollectCXXFriends(const CXXRecordDecl *Decl,
  106. llvm::DIFile F,
  107. SmallVectorImpl<llvm::Value *> &EltTys,
  108. llvm::DIType RecordTy);
  109. void CollectCXXBases(const CXXRecordDecl *Decl,
  110. llvm::DIFile F,
  111. SmallVectorImpl<llvm::Value *> &EltTys,
  112. llvm::DIType RecordTy);
  113. llvm::DIArray
  114. CollectTemplateParams(const TemplateParameterList *TPList,
  115. const TemplateArgumentList &TAList,
  116. llvm::DIFile Unit);
  117. llvm::DIArray
  118. CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
  119. llvm::DIArray
  120. CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
  121. llvm::DIFile F);
  122. llvm::DIType createFieldType(StringRef name, QualType type,
  123. uint64_t sizeInBitsOverride, SourceLocation loc,
  124. AccessSpecifier AS, uint64_t offsetInBits,
  125. llvm::DIFile tunit,
  126. llvm::DIDescriptor scope);
  127. void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
  128. void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
  129. SmallVectorImpl<llvm::Value *> &E,
  130. llvm::DIType RecordTy);
  131. void CollectVTableInfo(const CXXRecordDecl *Decl,
  132. llvm::DIFile F,
  133. SmallVectorImpl<llvm::Value *> &EltTys);
  134. // CreateLexicalBlock - Create a new lexical block node and push it on
  135. // the stack.
  136. void CreateLexicalBlock(SourceLocation Loc);
  137. public:
  138. CGDebugInfo(CodeGenModule &CGM);
  139. ~CGDebugInfo();
  140. void finalize() { DBuilder.finalize(); }
  141. /// setLocation - Update the current source location. If \arg loc is
  142. /// invalid it is ignored.
  143. void setLocation(SourceLocation Loc);
  144. /// EmitLocation - Emit metadata to indicate a change in line/column
  145. /// information in the source file.
  146. void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
  147. /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
  148. /// start of a new function.
  149. void EmitFunctionStart(GlobalDecl GD, QualType FnType,
  150. llvm::Function *Fn, CGBuilderTy &Builder);
  151. /// EmitFunctionEnd - Constructs the debug code for exiting a function.
  152. void EmitFunctionEnd(CGBuilderTy &Builder);
  153. /// UpdateCompletedType - Update type cache because the type is now
  154. /// translated.
  155. void UpdateCompletedType(const TagDecl *TD);
  156. /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
  157. /// new lexical block and push the block onto the stack.
  158. void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
  159. /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
  160. /// block and pop the current block.
  161. void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
  162. /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
  163. /// variable declaration.
  164. void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
  165. CGBuilderTy &Builder);
  166. /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
  167. /// imported variable declaration in a block.
  168. void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
  169. llvm::Value *storage,
  170. CGBuilderTy &Builder,
  171. const CGBlockInfo &blockInfo);
  172. /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
  173. /// variable declaration.
  174. void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
  175. unsigned ArgNo, CGBuilderTy &Builder);
  176. /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
  177. /// llvm.dbg.declare for the block-literal argument to a block
  178. /// invocation function.
  179. void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
  180. llvm::Value *addr,
  181. CGBuilderTy &Builder);
  182. /// EmitGlobalVariable - Emit information about a global variable.
  183. void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
  184. /// EmitGlobalVariable - Emit information about an objective-c interface.
  185. void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
  186. /// EmitGlobalVariable - Emit global variable's debug info.
  187. void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
  188. /// getOrCreateRecordType - Emit record type's standalone debug info.
  189. llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
  190. private:
  191. /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
  192. void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
  193. unsigned ArgNo, CGBuilderTy &Builder);
  194. // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
  195. // See BuildByRefType.
  196. llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
  197. uint64_t *OffSet);
  198. /// getContextDescriptor - Get context info for the decl.
  199. llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
  200. /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
  201. /// context.
  202. llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
  203. /// createContextChain - Create a set of decls for the context chain.
  204. llvm::DIDescriptor createContextChain(const Decl *Decl);
  205. /// getCurrentDirname - Return current directory name.
  206. StringRef getCurrentDirname();
  207. /// CreateCompileUnit - Create new compile unit.
  208. void CreateCompileUnit();
  209. /// getOrCreateFile - Get the file debug info descriptor for the input
  210. /// location.
  211. llvm::DIFile getOrCreateFile(SourceLocation Loc);
  212. /// getOrCreateMainFile - Get the file info for main compile unit.
  213. llvm::DIFile getOrCreateMainFile();
  214. /// getOrCreateType - Get the type from the cache or create a new type if
  215. /// necessary.
  216. llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
  217. /// getOrCreateLimitedType - Get the type from the cache or create a new
  218. /// partial type if necessary.
  219. llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
  220. /// CreateTypeNode - Create type metadata for a source language type.
  221. llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
  222. /// CreateLimitedTypeNode - Create type metadata for a source language
  223. /// type, but only partial types for records.
  224. llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
  225. /// CreateMemberType - Create new member and increase Offset by FType's size.
  226. llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
  227. StringRef Name, uint64_t *Offset);
  228. /// getFunctionDeclaration - Return debug info descriptor to describe method
  229. /// declaration for the given method definition.
  230. llvm::DISubprogram getFunctionDeclaration(const Decl *D);
  231. /// getFunctionName - Get function name for the given FunctionDecl. If the
  232. /// name is constructred on demand (e.g. C++ destructor) then the name
  233. /// is stored on the side.
  234. StringRef getFunctionName(const FunctionDecl *FD);
  235. /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
  236. /// This is the display name for the debugging info.
  237. StringRef getObjCMethodName(const ObjCMethodDecl *FD);
  238. /// getSelectorName - Return selector name. This is used for debugging
  239. /// info.
  240. StringRef getSelectorName(Selector S);
  241. /// getClassName - Get class name including template argument list.
  242. StringRef getClassName(const RecordDecl *RD);
  243. /// getVTableName - Get vtable name for the given Class.
  244. StringRef getVTableName(const CXXRecordDecl *Decl);
  245. /// getLineNumber - Get line number for the location. If location is invalid
  246. /// then use current location.
  247. unsigned getLineNumber(SourceLocation Loc);
  248. /// getColumnNumber - Get column number for the location. If location is
  249. /// invalid then use current location.
  250. unsigned getColumnNumber(SourceLocation Loc);
  251. };
  252. } // namespace CodeGen
  253. } // namespace clang
  254. #endif