blob: 50f575940886f2599accd047d00e37e70a20f675 [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#include <map>
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025
Daniel Dunbar45d196b2008-11-01 01:53:16 +000026#include "CGBuilder.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027
Daniel Dunbar23e81ba2009-09-19 19:27:24 +000028namespace llvm {
29 class MDNode;
30}
31
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032namespace clang {
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000033 class VarDecl;
Devang Patel9ca36b62009-02-26 21:10:26 +000034 class ObjCInterfaceDecl;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000035
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036namespace CodeGen {
37 class CodeGenModule;
Mike Stumpb1a6e682009-09-30 02:43:10 +000038 class CodeGenFunction;
Devang Patel9c6c3a02010-01-14 00:36:21 +000039 class GlobalDecl;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000040
Mike Stump1eb44332009-09-09 15:08:12 +000041/// CGDebugInfo - This class gathers all debug information during compilation
42/// and is responsible for emitting to llvm globals or pass directly to
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000043/// the backend.
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044class CGDebugInfo {
Anders Carlsson20f12a22009-12-06 18:00:51 +000045 CodeGenModule &CGM;
Devang Patel87de6492009-04-23 18:09:16 +000046 bool isMainCompileUnitCreated;
Chris Lattner9c85ba32008-11-10 06:08:34 +000047 llvm::DIFactory DebugFactory;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner9c85ba32008-11-10 06:08:34 +000049 SourceLocation CurLoc, PrevLoc;
Devang Patel4ce3f202010-01-28 18:11:52 +000050
51 llvm::DIType VTablePtrType;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000052
53 /// CompileUnitCache - Cache of previously constructed CompileUnits.
Devang Patel446c6192009-04-17 21:06:59 +000054 llvm::DenseMap<unsigned, llvm::DICompileUnit> CompileUnitCache;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000055
56 /// TypeCache - Cache of previously constructed Types.
Chris Lattner9c85ba32008-11-10 06:08:34 +000057 // FIXME: Eliminate this map. Be careful of iterator invalidation.
Daniel Dunbar65f13c32009-09-19 20:17:48 +000058 std::map<void *, llvm::WeakVH> TypeCache;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Mike Stump9bc093c2009-05-14 02:03:51 +000060 bool BlockLiteralGenericSet;
61 llvm::DIType BlockLiteralGeneric;
62
Devang Patel8fae0602009-11-13 19:10:24 +000063 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
Devang Patel3dd96a12010-01-29 18:11:03 +000064 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
Eli Friedman3f2af102008-05-22 01:40:10 +000065
Devang Patel89f05f82010-01-28 18:21:00 +000066 /// DebugInfoNames - This is a storage for names that are
Devang Patel9c6c3a02010-01-14 00:36:21 +000067 /// constructed on demand. For example, C++ destructors, C++ operators etc..
Devang Patel89f05f82010-01-28 18:21:00 +000068 llvm::BumpPtrAllocator DebugInfoNames;
Devang Patel9c6c3a02010-01-14 00:36:21 +000069
Devang Patel4125fd22010-01-19 01:54:44 +000070 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
Devang Patelabb485f2010-02-01 19:16:32 +000071 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
Devang Patel4125fd22010-01-19 01:54:44 +000072
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000073 /// Helper functions for getOrCreateType.
Chris Lattner9c85ba32008-11-10 06:08:34 +000074 llvm::DIType CreateType(const BuiltinType *Ty, llvm::DICompileUnit U);
Chris Lattnerb7003772009-04-23 06:13:01 +000075 llvm::DIType CreateType(const ComplexType *Ty, llvm::DICompileUnit U);
John McCalla1805292009-09-25 01:40:47 +000076 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DICompileUnit U);
Chris Lattner9c85ba32008-11-10 06:08:34 +000077 llvm::DIType CreateType(const TypedefType *Ty, llvm::DICompileUnit U);
Mike Stump1eb44332009-09-09 15:08:12 +000078 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
Daniel Dunbar9df4bb32009-07-14 01:20:56 +000079 llvm::DICompileUnit Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +000080 llvm::DIType CreateType(const PointerType *Ty, llvm::DICompileUnit U);
Mike Stump9bc093c2009-05-14 02:03:51 +000081 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DICompileUnit U);
Chris Lattner9c85ba32008-11-10 06:08:34 +000082 llvm::DIType CreateType(const FunctionType *Ty, llvm::DICompileUnit U);
83 llvm::DIType CreateType(const TagType *Ty, llvm::DICompileUnit U);
84 llvm::DIType CreateType(const RecordType *Ty, llvm::DICompileUnit U);
Devang Patel9ca36b62009-02-26 21:10:26 +000085 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DICompileUnit U);
Chris Lattner9c85ba32008-11-10 06:08:34 +000086 llvm::DIType CreateType(const EnumType *Ty, llvm::DICompileUnit U);
Devang Patel70c23cd2010-02-23 22:59:39 +000087 llvm::DIType CreateType(const VectorType *Ty, llvm::DICompileUnit Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +000088 llvm::DIType CreateType(const ArrayType *Ty, llvm::DICompileUnit U);
Anders Carlssona031b352009-11-06 19:19:55 +000089 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DICompileUnit U);
Anders Carlsson20f12a22009-12-06 18:00:51 +000090 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DICompileUnit U);
Devang Patela6da1922010-01-28 00:28:01 +000091 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
92 llvm::DICompileUnit Unit);
Devang Patel4ce3f202010-01-28 18:11:52 +000093 llvm::DIType getOrCreateVTablePtrType(llvm::DICompileUnit Unit);
Devang Patelabb485f2010-02-01 19:16:32 +000094 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
95 llvm::DIDescriptor Unit);
96
Anders Carlssona031b352009-11-06 19:19:55 +000097 llvm::DIType CreatePointerLikeType(unsigned Tag,
98 const Type *Ty, QualType PointeeTy,
99 llvm::DICompileUnit U);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000100
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000101 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000102 llvm::DICompileUnit Unit,
103 llvm::DICompositeType &RecordTy);
104
Devang Patel4125fd22010-01-19 01:54:44 +0000105 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
106 llvm::DICompileUnit U,
107 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
108 llvm::DICompositeType &T);
Devang Patela245c5b2010-01-25 23:32:18 +0000109 void CollectCXXBases(const CXXRecordDecl *Decl,
110 llvm::DICompileUnit Unit,
111 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
112 llvm::DICompositeType &RecordTy);
113
114
Devang Patel428deb52010-01-19 00:00:59 +0000115 void CollectRecordFields(const RecordDecl *Decl, llvm::DICompileUnit U,
116 llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
Devang Patel4ce3f202010-01-28 18:11:52 +0000117
118 void CollectVtableInfo(const CXXRecordDecl *Decl,
119 llvm::DICompileUnit Unit,
120 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
121
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000122public:
Anders Carlsson20f12a22009-12-06 18:00:51 +0000123 CGDebugInfo(CodeGenModule &CGM);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000124 ~CGDebugInfo();
125
Daniel Dunbar66031a52008-10-17 16:15:48 +0000126 /// setLocation - Update the current source location. If \arg loc is
127 /// invalid it is ignored.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000128 void setLocation(SourceLocation Loc);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000129
130 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
131 /// source line.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000132 void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000133
134 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000135 /// start of a new function.
Devang Patel9c6c3a02010-01-14 00:36:21 +0000136 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000137 llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000139 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
Mike Stump1eb44332009-09-09 15:08:12 +0000140 /// of a new block.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000141 void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
143 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000144 /// block.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000145 void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000146
Chris Lattner9c85ba32008-11-10 06:08:34 +0000147 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
148 /// variable declaration.
149 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
150 CGBuilderTy &Builder);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000151
Mike Stumpb1a6e682009-09-30 02:43:10 +0000152 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
153 /// imported variable declaration in a block.
154 void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
155 llvm::Value *AI,
156 CGBuilderTy &Builder,
157 CodeGenFunction *CGF);
158
Chris Lattner9c85ba32008-11-10 06:08:34 +0000159 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
160 /// variable declaration.
161 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
162 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000164 /// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000165 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000166
167 /// EmitGlobalVariable - Emit information about an objective-c interface.
168 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattner86cd8af2008-11-03 09:11:11 +0000170private:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
172 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
173 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Mike Stumpb1a6e682009-09-30 02:43:10 +0000175 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
176 void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
177 CGBuilderTy &Builder, CodeGenFunction *CGF);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Devang Patel809b9bb2010-02-10 18:49:08 +0000179 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
180 // See BuildByRefType.
181 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
182 uint64_t *OffSet);
183
Devang Patel33583052010-01-28 23:15:27 +0000184 /// getContextDescriptor - Get context info for the decl.
Devang Patel3dd96a12010-01-29 18:11:03 +0000185 llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
Devang Patel33583052010-01-28 23:15:27 +0000186 llvm::DIDescriptor &CU);
Devang Patel979ec2e2009-10-06 00:35:31 +0000187
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000188 /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
189 /// new one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000190 llvm::DICompileUnit getOrCreateCompileUnit(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.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194 llvm::DIType getOrCreateType(QualType Ty, llvm::DICompileUnit Unit);
Daniel Dunbar03faac32009-09-19 19:27:14 +0000195
196 /// CreateTypeNode - Create type metadata for a source language type.
197 llvm::DIType CreateTypeNode(QualType Ty, llvm::DICompileUnit Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +0000198
199 /// getFunctionName - Get function name for the given FunctionDecl. If the
200 /// name is constructred on demand (e.g. C++ destructor) then the name
201 /// is stored on the side.
202 llvm::StringRef getFunctionName(const FunctionDecl *FD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000203
204 /// getVtableName - Get vtable name for the given Class.
205 llvm::StringRef getVtableName(const CXXRecordDecl *Decl);
206
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000207};
208} // namespace CodeGen
209} // namespace clang
210
Devang Patelbbd9fa42009-10-06 18:36:08 +0000211
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000212#endif