blob: ed5c76b676dcf76dc8a230b93785dc83e34efce7 [file] [log] [blame]
Daniel Dunbar3845f862008-10-31 03:54:29 +00001//===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00002//
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//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This is the source level debug info generator for llvm translation.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGDEBUGINFO_H
15#define CLANG_CODEGEN_CGDEBUGINFO_H
16
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/Type.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000018#include "clang/AST/Expr.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000019#include "clang/Basic/SourceLocation.h"
Chris Lattner9c85ba32008-11-10 06:08:34 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/Analysis/DebugInfo.h"
Daniel Dunbar23e81ba2009-09-19 19:27:24 +000022#include "llvm/Support/ValueHandle.h"
Devang Patel9c6c3a02010-01-14 00:36:21 +000023#include "llvm/Support/Allocator.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000024
Daniel Dunbar45d196b2008-11-01 01:53:16 +000025#include "CGBuilder.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000026
Daniel Dunbar23e81ba2009-09-19 19:27:24 +000027namespace llvm {
28 class MDNode;
29}
30
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031namespace clang {
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000032 class VarDecl;
Devang Patel9ca36b62009-02-26 21:10:26 +000033 class ObjCInterfaceDecl;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035namespace CodeGen {
36 class CodeGenModule;
Mike Stumpb1a6e682009-09-30 02:43:10 +000037 class CodeGenFunction;
Devang Patel9c6c3a02010-01-14 00:36:21 +000038 class GlobalDecl;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039
Mike Stump1eb44332009-09-09 15:08:12 +000040/// CGDebugInfo - This class gathers all debug information during compilation
41/// and is responsible for emitting to llvm globals or pass directly to
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000042/// the backend.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043class CGDebugInfo {
Anders Carlsson20f12a22009-12-06 18:00:51 +000044 CodeGenModule &CGM;
Chris Lattner9c85ba32008-11-10 06:08:34 +000045 llvm::DIFactory DebugFactory;
Devang Patel17800552010-03-09 00:44:50 +000046 llvm::DICompileUnit TheCU;
Chris Lattner9c85ba32008-11-10 06:08:34 +000047 SourceLocation CurLoc, PrevLoc;
Devang Patel4ce3f202010-01-28 18:11:52 +000048 llvm::DIType VTablePtrType;
Devang Patel7573f8b2010-03-09 21:32:27 +000049 /// FwdDeclCount - This counter is used to ensure unique names for forward
50 /// record decls.
51 unsigned FwdDeclCount;
Devang Patel17800552010-03-09 00:44:50 +000052
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053 /// TypeCache - Cache of previously constructed Types.
Ted Kremenek590838b2010-03-29 18:29:57 +000054 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
Mike Stump1eb44332009-09-09 15:08:12 +000055
Mike Stump9bc093c2009-05-14 02:03:51 +000056 bool BlockLiteralGenericSet;
57 llvm::DIType BlockLiteralGeneric;
58
Devang Patel8fae0602009-11-13 19:10:24 +000059 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
Devang Patel3dd96a12010-01-29 18:11:03 +000060 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
Eli Friedman3f2af102008-05-22 01:40:10 +000061
Devang Patel89f05f82010-01-28 18:21:00 +000062 /// DebugInfoNames - This is a storage for names that are
Devang Patel9c6c3a02010-01-14 00:36:21 +000063 /// constructed on demand. For example, C++ destructors, C++ operators etc..
Devang Patel89f05f82010-01-28 18:21:00 +000064 llvm::BumpPtrAllocator DebugInfoNames;
Devang Patel9c6c3a02010-01-14 00:36:21 +000065
Ted Kremenek9c250392010-03-30 00:27:51 +000066 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
Devang Patel4125fd22010-01-19 01:54:44 +000067 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
Devang Patelabb485f2010-02-01 19:16:32 +000068 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
Devang Patel4125fd22010-01-19 01:54:44 +000069
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000070 /// Helper functions for getOrCreateType.
Devang Patel17800552010-03-09 00:44:50 +000071 llvm::DIType CreateType(const BuiltinType *Ty, llvm::DIFile F);
72 llvm::DIType CreateType(const ComplexType *Ty, llvm::DIFile F);
73 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
74 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
Mike Stump1eb44332009-09-09 15:08:12 +000075 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +000076 llvm::DIFile F);
77 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
78 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
79 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
80 llvm::DIType CreateType(const TagType *Ty, llvm::DIFile F);
81 llvm::DIType CreateType(const RecordType *Ty, llvm::DIFile F);
82 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
83 llvm::DIType CreateType(const EnumType *Ty, llvm::DIFile F);
84 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
85 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
86 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
87 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
Devang Patela6da1922010-01-28 00:28:01 +000088 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +000089 llvm::DIFile F);
90 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
Devang Patelabb485f2010-02-01 19:16:32 +000091 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
92 llvm::DIDescriptor Unit);
93
Anders Carlssona031b352009-11-06 19:19:55 +000094 llvm::DIType CreatePointerLikeType(unsigned Tag,
95 const Type *Ty, QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +000096 llvm::DIFile F);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +000097
Anders Carlsson4433f1c2010-01-26 05:19:50 +000098 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +000099 llvm::DIFile F,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000100 llvm::DICompositeType &RecordTy);
101
Devang Patel4125fd22010-01-19 01:54:44 +0000102 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000103 llvm::DIFile F,
Devang Patel4125fd22010-01-19 01:54:44 +0000104 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
105 llvm::DICompositeType &T);
Devang Patela245c5b2010-01-25 23:32:18 +0000106 void CollectCXXBases(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000107 llvm::DIFile F,
Devang Patela245c5b2010-01-25 23:32:18 +0000108 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
109 llvm::DICompositeType &RecordTy);
110
111
Devang Patel17800552010-03-09 00:44:50 +0000112 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
Devang Patel428deb52010-01-19 00:00:59 +0000113 llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
Devang Patel4ce3f202010-01-28 18:11:52 +0000114
Anders Carlsson046c2942010-04-17 20:15:18 +0000115 void CollectVTableInfo(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000116 llvm::DIFile F,
Devang Patel4ce3f202010-01-28 18:11:52 +0000117 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
118
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000119public:
Anders Carlsson20f12a22009-12-06 18:00:51 +0000120 CGDebugInfo(CodeGenModule &CGM);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000121 ~CGDebugInfo();
122
Daniel Dunbar66031a52008-10-17 16:15:48 +0000123 /// setLocation - Update the current source location. If \arg loc is
124 /// invalid it is ignored.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000125 void setLocation(SourceLocation Loc);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000126
127 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
128 /// source line.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000129 void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000130
131 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000132 /// start of a new function.
Devang Patel9c6c3a02010-01-14 00:36:21 +0000133 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000134 llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000136 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
Mike Stump1eb44332009-09-09 15:08:12 +0000137 /// of a new block.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000138 void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
140 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000141 /// block.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000142 void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000143
Chris Lattner9c85ba32008-11-10 06:08:34 +0000144 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
145 /// variable declaration.
146 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
147 CGBuilderTy &Builder);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000148
Mike Stumpb1a6e682009-09-30 02:43:10 +0000149 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
150 /// imported variable declaration in a block.
151 void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
152 llvm::Value *AI,
153 CGBuilderTy &Builder,
154 CodeGenFunction *CGF);
155
Chris Lattner9c85ba32008-11-10 06:08:34 +0000156 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
157 /// variable declaration.
158 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
159 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000161 /// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000162 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000163
164 /// EmitGlobalVariable - Emit information about an objective-c interface.
165 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner86cd8af2008-11-03 09:11:11 +0000167private:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000168 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
169 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
170 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Mike Stumpb1a6e682009-09-30 02:43:10 +0000172 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
173 void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
174 CGBuilderTy &Builder, CodeGenFunction *CGF);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Devang Patel809b9bb2010-02-10 18:49:08 +0000176 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
177 // See BuildByRefType.
178 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
179 uint64_t *OffSet);
180
Devang Patel33583052010-01-28 23:15:27 +0000181 /// getContextDescriptor - Get context info for the decl.
Devang Patel3dd96a12010-01-29 18:11:03 +0000182 llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
Devang Patel33583052010-01-28 23:15:27 +0000183 llvm::DIDescriptor &CU);
Devang Patel979ec2e2009-10-06 00:35:31 +0000184
Devang Patel17800552010-03-09 00:44:50 +0000185 /// CreateCompileUnit - Create new compile unit.
186 void CreateCompileUnit();
187
188 /// getOrCreateFile - Get the file debug info descriptor for the input
189 /// location.
190 llvm::DIFile getOrCreateFile(SourceLocation Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000191
192 /// getOrCreateType - Get the type from the cache or create a new type if
193 /// necessary.
Devang Patel17800552010-03-09 00:44:50 +0000194 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
Daniel Dunbar03faac32009-09-19 19:27:14 +0000195
196 /// CreateTypeNode - Create type metadata for a source language type.
Devang Patel17800552010-03-09 00:44:50 +0000197 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
Devang Patel9c6c3a02010-01-14 00:36:21 +0000198
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000199 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
200 llvm::StringRef Name, uint64_t *Offset);
201
Devang Patel9c6c3a02010-01-14 00:36:21 +0000202 /// getFunctionName - Get function name for the given FunctionDecl. If the
203 /// name is constructred on demand (e.g. C++ destructor) then the name
204 /// is stored on the side.
205 llvm::StringRef getFunctionName(const FunctionDecl *FD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000206
Anders Carlsson046c2942010-04-17 20:15:18 +0000207 /// getVTableName - Get vtable name for the given Class.
208 llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000209
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000210};
211} // namespace CodeGen
212} // namespace clang
213
Devang Patelbbd9fa42009-10-06 18:36:08 +0000214
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000215#endif