blob: ac75ac02319ee05fb5bce587c245ac8d8eb784e3 [file] [log] [blame]
Daniel Dunbara290ded2008-10-31 03:54:29 +00001//===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
Sanjiv Gupta15cb6692008-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 Stump11289f42009-09-09 15:08:12 +000010// This is the source level debug info generator for llvm translation.
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGDEBUGINFO_H
15#define CLANG_CODEGEN_CGDEBUGINFO_H
16
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/Type.h"
Mike Stump2e722b92009-09-30 02:43:10 +000018#include "clang/AST/Expr.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000019#include "clang/Basic/SourceLocation.h"
Chris Lattneraffb3732008-11-10 06:08:34 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/Analysis/DebugInfo.h"
Daniel Dunbar1cbaae52009-09-19 19:27:24 +000022#include "llvm/Support/ValueHandle.h"
Devang Patel934661e2010-01-14 00:36:21 +000023#include "llvm/Support/Allocator.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000024#include <map>
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000025
Daniel Dunbarcb463852008-11-01 01:53:16 +000026#include "CGBuilder.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000027
Daniel Dunbar1cbaae52009-09-19 19:27:24 +000028namespace llvm {
29 class MDNode;
30}
31
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000032namespace clang {
Sanjiv Gupta18de6242008-05-30 10:30:31 +000033 class VarDecl;
Devang Patelf4c205b2009-02-26 21:10:26 +000034 class ObjCInterfaceDecl;
Daniel Dunbarcb463852008-11-01 01:53:16 +000035
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000036namespace CodeGen {
37 class CodeGenModule;
Mike Stump2e722b92009-09-30 02:43:10 +000038 class CodeGenFunction;
Devang Patel934661e2010-01-14 00:36:21 +000039 class GlobalDecl;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000040
Mike Stump11289f42009-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 Gupta98070572008-05-25 05:15:42 +000043/// the backend.
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000044class CGDebugInfo {
Anders Carlsson3efc6e62009-12-06 18:00:51 +000045 CodeGenModule &CGM;
Devang Patela6acb392009-04-23 18:09:16 +000046 bool isMainCompileUnitCreated;
Chris Lattneraffb3732008-11-10 06:08:34 +000047 llvm::DIFactory DebugFactory;
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattneraffb3732008-11-10 06:08:34 +000049 SourceLocation CurLoc, PrevLoc;
Devang Patel84033fb2010-01-28 18:11:52 +000050
51 llvm::DIType VTablePtrType;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000052
53 /// CompileUnitCache - Cache of previously constructed CompileUnits.
Devang Patel75009452009-04-17 21:06:59 +000054 llvm::DenseMap<unsigned, llvm::DICompileUnit> CompileUnitCache;
Sanjiv Gupta98070572008-05-25 05:15:42 +000055
56 /// TypeCache - Cache of previously constructed Types.
Chris Lattneraffb3732008-11-10 06:08:34 +000057 // FIXME: Eliminate this map. Be careful of iterator invalidation.
Daniel Dunbar99961382009-09-19 20:17:48 +000058 std::map<void *, llvm::WeakVH> TypeCache;
Mike Stump11289f42009-09-09 15:08:12 +000059
Mike Stump31f099c2009-05-14 02:03:51 +000060 bool BlockLiteralGenericSet;
61 llvm::DIType BlockLiteralGeneric;
62
Devang Patelb40f2952009-11-13 19:10:24 +000063 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
Eli Friedman17630752008-05-22 01:40:10 +000064
Devang Patel934661e2010-01-14 00:36:21 +000065 /// FunctionNames - This is a storage for function names that are
66 /// constructed on demand. For example, C++ destructors, C++ operators etc..
67 llvm::BumpPtrAllocator FunctionNames;
68
Devang Patel7a12ad02010-01-19 01:54:44 +000069 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
70
Sanjiv Gupta98070572008-05-25 05:15:42 +000071 /// Helper functions for getOrCreateType.
Chris Lattneraffb3732008-11-10 06:08:34 +000072 llvm::DIType CreateType(const BuiltinType *Ty, llvm::DICompileUnit U);
Chris Lattner7b0344f2009-04-23 06:13:01 +000073 llvm::DIType CreateType(const ComplexType *Ty, llvm::DICompileUnit U);
John McCall0cf15512009-09-25 01:40:47 +000074 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DICompileUnit U);
Chris Lattneraffb3732008-11-10 06:08:34 +000075 llvm::DIType CreateType(const TypedefType *Ty, llvm::DICompileUnit U);
Mike Stump11289f42009-09-09 15:08:12 +000076 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
Daniel Dunbarf5c79702009-07-14 01:20:56 +000077 llvm::DICompileUnit Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +000078 llvm::DIType CreateType(const PointerType *Ty, llvm::DICompileUnit U);
Mike Stump31f099c2009-05-14 02:03:51 +000079 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DICompileUnit U);
Chris Lattneraffb3732008-11-10 06:08:34 +000080 llvm::DIType CreateType(const FunctionType *Ty, llvm::DICompileUnit U);
81 llvm::DIType CreateType(const TagType *Ty, llvm::DICompileUnit U);
82 llvm::DIType CreateType(const RecordType *Ty, llvm::DICompileUnit U);
Devang Patelf4c205b2009-02-26 21:10:26 +000083 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DICompileUnit U);
Chris Lattneraffb3732008-11-10 06:08:34 +000084 llvm::DIType CreateType(const EnumType *Ty, llvm::DICompileUnit U);
85 llvm::DIType CreateType(const ArrayType *Ty, llvm::DICompileUnit U);
Anders Carlsson443f6772009-11-06 19:19:55 +000086 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DICompileUnit U);
Anders Carlsson3efc6e62009-12-06 18:00:51 +000087 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DICompileUnit U);
Devang Patel3d4e6d92010-01-28 00:28:01 +000088 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
89 llvm::DICompileUnit Unit);
Devang Patel84033fb2010-01-28 18:11:52 +000090 llvm::DIType getOrCreateVTablePtrType(llvm::DICompileUnit Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +000091
Anders Carlsson443f6772009-11-06 19:19:55 +000092 llvm::DIType CreatePointerLikeType(unsigned Tag,
93 const Type *Ty, QualType PointeeTy,
94 llvm::DICompileUnit U);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +000095
Anders Carlsson17ed0492010-01-26 05:19:50 +000096 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +000097 llvm::DICompileUnit Unit,
98 llvm::DICompositeType &RecordTy);
99
Devang Patel7a12ad02010-01-19 01:54:44 +0000100 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
101 llvm::DICompileUnit U,
102 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
103 llvm::DICompositeType &T);
Devang Patelc54353d2010-01-25 23:32:18 +0000104 void CollectCXXBases(const CXXRecordDecl *Decl,
105 llvm::DICompileUnit Unit,
106 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
107 llvm::DICompositeType &RecordTy);
108
109
Devang Patel889ce762010-01-19 00:00:59 +0000110 void CollectRecordFields(const RecordDecl *Decl, llvm::DICompileUnit U,
111 llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
Devang Patel84033fb2010-01-28 18:11:52 +0000112
113 void CollectVtableInfo(const CXXRecordDecl *Decl,
114 llvm::DICompileUnit Unit,
115 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
116
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000117public:
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000118 CGDebugInfo(CodeGenModule &CGM);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000119 ~CGDebugInfo();
120
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000121 /// setLocation - Update the current source location. If \arg loc is
122 /// invalid it is ignored.
Chris Lattneraffb3732008-11-10 06:08:34 +0000123 void setLocation(SourceLocation Loc);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000124
125 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
126 /// source line.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000127 void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000128
129 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
Daniel Dunbar354d2782008-10-18 18:22:23 +0000130 /// start of a new function.
Devang Patel934661e2010-01-14 00:36:21 +0000131 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
Daniel Dunbarcb463852008-11-01 01:53:16 +0000132 llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000133
Sanjiv Gupta98070572008-05-25 05:15:42 +0000134 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
Mike Stump11289f42009-09-09 15:08:12 +0000135 /// of a new block.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000136 void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000137
138 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000139 /// block.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000140 void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000141
Chris Lattneraffb3732008-11-10 06:08:34 +0000142 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
143 /// variable declaration.
144 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
145 CGBuilderTy &Builder);
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000146
Mike Stump2e722b92009-09-30 02:43:10 +0000147 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
148 /// imported variable declaration in a block.
149 void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
150 llvm::Value *AI,
151 CGBuilderTy &Builder,
152 CodeGenFunction *CGF);
153
Chris Lattneraffb3732008-11-10 06:08:34 +0000154 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
155 /// variable declaration.
156 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
157 CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000158
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000159 /// EmitGlobalVariable - Emit information about a global variable.
Chris Lattneraffb3732008-11-10 06:08:34 +0000160 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
Devang Patelf4c205b2009-02-26 21:10:26 +0000161
162 /// EmitGlobalVariable - Emit information about an objective-c interface.
163 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner7d7fff22008-11-03 09:11:11 +0000165private:
Chris Lattneraffb3732008-11-10 06:08:34 +0000166 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
167 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
168 CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000169
Mike Stump2e722b92009-09-30 02:43:10 +0000170 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
171 void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
172 CGBuilderTy &Builder, CodeGenFunction *CGF);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Devang Patelfaf7e9a2009-10-06 00:35:31 +0000174 /// getContext - Get context info for the decl.
175 llvm::DIDescriptor getContext(const VarDecl *Decl,llvm::DIDescriptor &CU);
176
Sanjiv Gupta98070572008-05-25 05:15:42 +0000177 /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
178 /// new one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +0000179 llvm::DICompileUnit getOrCreateCompileUnit(SourceLocation Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000180
181 /// getOrCreateType - Get the type from the cache or create a new type if
182 /// necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +0000183 llvm::DIType getOrCreateType(QualType Ty, llvm::DICompileUnit Unit);
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000184
185 /// CreateTypeNode - Create type metadata for a source language type.
186 llvm::DIType CreateTypeNode(QualType Ty, llvm::DICompileUnit Unit);
Devang Patel934661e2010-01-14 00:36:21 +0000187
188 /// getFunctionName - Get function name for the given FunctionDecl. If the
189 /// name is constructred on demand (e.g. C++ destructor) then the name
190 /// is stored on the side.
191 llvm::StringRef getFunctionName(const FunctionDecl *FD);
Devang Patel84033fb2010-01-28 18:11:52 +0000192
193 /// getVtableName - Get vtable name for the given Class.
194 llvm::StringRef getVtableName(const CXXRecordDecl *Decl);
195
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000196};
197} // namespace CodeGen
198} // namespace clang
199
Devang Patel5d90d622009-10-06 18:36:08 +0000200
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000201#endif