blob: fdfd3dca571768d3ecb0b937b96a48673fb7fc11 [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
Daniel Dunbarcb463852008-11-01 01:53:16 +000025#include "CGBuilder.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000026
Daniel Dunbar1cbaae52009-09-19 19:27:24 +000027namespace llvm {
28 class MDNode;
29}
30
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000031namespace clang {
Sanjiv Gupta18de6242008-05-30 10:30:31 +000032 class VarDecl;
Devang Patelf4c205b2009-02-26 21:10:26 +000033 class ObjCInterfaceDecl;
Daniel Dunbarcb463852008-11-01 01:53:16 +000034
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035namespace CodeGen {
36 class CodeGenModule;
Mike Stump2e722b92009-09-30 02:43:10 +000037 class CodeGenFunction;
Devang Patel934661e2010-01-14 00:36:21 +000038 class GlobalDecl;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000039
Mike Stump11289f42009-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 Gupta98070572008-05-25 05:15:42 +000042/// the backend.
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000043class CGDebugInfo {
Anders Carlsson3efc6e62009-12-06 18:00:51 +000044 CodeGenModule &CGM;
Chris Lattneraffb3732008-11-10 06:08:34 +000045 llvm::DIFactory DebugFactory;
Devang Patel408dcf62010-03-09 00:44:50 +000046 llvm::DICompileUnit TheCU;
Chris Lattneraffb3732008-11-10 06:08:34 +000047 SourceLocation CurLoc, PrevLoc;
Devang Patel84033fb2010-01-28 18:11:52 +000048 llvm::DIType VTablePtrType;
Devang Patel4f262052010-03-09 21:32:27 +000049 /// FwdDeclCount - This counter is used to ensure unique names for forward
50 /// record decls.
51 unsigned FwdDeclCount;
Devang Patel408dcf62010-03-09 00:44:50 +000052
Sanjiv Gupta98070572008-05-25 05:15:42 +000053 /// TypeCache - Cache of previously constructed Types.
Ted Kremenek23c29f02010-03-29 18:29:57 +000054 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
Mike Stump11289f42009-09-09 15:08:12 +000055
Mike Stump31f099c2009-05-14 02:03:51 +000056 bool BlockLiteralGenericSet;
57 llvm::DIType BlockLiteralGeneric;
58
Devang Patelb40f2952009-11-13 19:10:24 +000059 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
Devang Patel92e25412010-01-29 18:11:03 +000060 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
Devang Patel0884a602010-07-22 22:29:16 +000061 // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
62 // of a function. This is used to pop unbalanced regions at the end of a
63 // function.
64 std::vector<unsigned> FnBeginRegionCount;
65
66 /// LineDirectiveFiles - This stack is used to keep track of
67 /// scopes introduced by #line directives.
68 std::vector<const char *> LineDirectiveFiles;
Eli Friedman17630752008-05-22 01:40:10 +000069
Devang Patel0d61eeb2010-01-28 18:21:00 +000070 /// DebugInfoNames - This is a storage for names that are
Devang Patel934661e2010-01-14 00:36:21 +000071 /// constructed on demand. For example, C++ destructors, C++ operators etc..
Devang Patel0d61eeb2010-01-28 18:21:00 +000072 llvm::BumpPtrAllocator DebugInfoNames;
Devang Patel934661e2010-01-14 00:36:21 +000073
Ted Kremenekdbb8cd12010-03-30 00:27:51 +000074 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
Devang Patel7a12ad02010-01-19 01:54:44 +000075 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
Devang Patel973f2eb2010-02-01 19:16:32 +000076 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
Devang Patel7a12ad02010-01-19 01:54:44 +000077
Sanjiv Gupta98070572008-05-25 05:15:42 +000078 /// Helper functions for getOrCreateType.
Devang Patel408dcf62010-03-09 00:44:50 +000079 llvm::DIType CreateType(const BuiltinType *Ty, llvm::DIFile F);
80 llvm::DIType CreateType(const ComplexType *Ty, llvm::DIFile F);
81 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
82 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
Mike Stump11289f42009-09-09 15:08:12 +000083 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +000084 llvm::DIFile F);
85 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
86 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
87 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
88 llvm::DIType CreateType(const TagType *Ty, llvm::DIFile F);
89 llvm::DIType CreateType(const RecordType *Ty, llvm::DIFile F);
90 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
John McCall8b07ec22010-05-15 11:32:37 +000091 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
Devang Patel408dcf62010-03-09 00:44:50 +000092 llvm::DIType CreateType(const EnumType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
94 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
96 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
Devang Patel3d4e6d92010-01-28 00:28:01 +000097 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +000098 llvm::DIFile F);
99 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
Devang Patel973f2eb2010-02-01 19:16:32 +0000100 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
101 llvm::DIDescriptor Unit);
102
Anders Carlsson443f6772009-11-06 19:19:55 +0000103 llvm::DIType CreatePointerLikeType(unsigned Tag,
104 const Type *Ty, QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000105 llvm::DIFile F);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000106
Anders Carlsson17ed0492010-01-26 05:19:50 +0000107 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000108 llvm::DIFile F,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000109 llvm::DICompositeType &RecordTy);
110
Devang Patel7a12ad02010-01-19 01:54:44 +0000111 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
Devang Patel408dcf62010-03-09 00:44:50 +0000112 llvm::DIFile F,
Devang Patel7a12ad02010-01-19 01:54:44 +0000113 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
114 llvm::DICompositeType &T);
Devang Patelc54353d2010-01-25 23:32:18 +0000115 void CollectCXXBases(const CXXRecordDecl *Decl,
Devang Patel408dcf62010-03-09 00:44:50 +0000116 llvm::DIFile F,
Devang Patelc54353d2010-01-25 23:32:18 +0000117 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
118 llvm::DICompositeType &RecordTy);
119
120
Devang Patel408dcf62010-03-09 00:44:50 +0000121 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
Devang Patel889ce762010-01-19 00:00:59 +0000122 llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
Devang Patel84033fb2010-01-28 18:11:52 +0000123
Anders Carlsson11e51402010-04-17 20:15:18 +0000124 void CollectVTableInfo(const CXXRecordDecl *Decl,
Devang Patel408dcf62010-03-09 00:44:50 +0000125 llvm::DIFile F,
Devang Patel84033fb2010-01-28 18:11:52 +0000126 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
127
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000128public:
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000129 CGDebugInfo(CodeGenModule &CGM);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000130 ~CGDebugInfo();
131
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000132 /// setLocation - Update the current source location. If \arg loc is
133 /// invalid it is ignored.
Chris Lattneraffb3732008-11-10 06:08:34 +0000134 void setLocation(SourceLocation Loc);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000135
136 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
137 /// source line.
Devang Patel11a42a42010-07-20 22:20:10 +0000138 void EmitStopPoint(CGBuilderTy &Builder);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000139
140 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
Daniel Dunbar354d2782008-10-18 18:22:23 +0000141 /// start of a new function.
Devang Patel934661e2010-01-14 00:36:21 +0000142 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
Daniel Dunbarcb463852008-11-01 01:53:16 +0000143 llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000144
Devang Patel0884a602010-07-22 22:29:16 +0000145 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
146 void EmitFunctionEnd(CGBuilderTy &Builder);
147
148 /// UpdateLineDirectiveRegion - Update region stack only if #line directive
149 /// has introduced scope change.
150 void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
151
Sanjiv Gupta98070572008-05-25 05:15:42 +0000152 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
Mike Stump11289f42009-09-09 15:08:12 +0000153 /// of a new block.
Devang Patel11a42a42010-07-20 22:20:10 +0000154 void EmitRegionStart(CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000155
156 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000157 /// block.
Devang Patel11a42a42010-07-20 22:20:10 +0000158 void EmitRegionEnd(CGBuilderTy &Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000159
Chris Lattneraffb3732008-11-10 06:08:34 +0000160 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
161 /// variable declaration.
162 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
163 CGBuilderTy &Builder);
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000164
Mike Stump2e722b92009-09-30 02:43:10 +0000165 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
166 /// imported variable declaration in a block.
167 void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
168 llvm::Value *AI,
169 CGBuilderTy &Builder,
170 CodeGenFunction *CGF);
171
Chris Lattneraffb3732008-11-10 06:08:34 +0000172 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
173 /// variable declaration.
174 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
175 CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000176
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000177 /// EmitGlobalVariable - Emit information about a global variable.
Chris Lattneraffb3732008-11-10 06:08:34 +0000178 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
Devang Patelf4c205b2009-02-26 21:10:26 +0000179
180 /// EmitGlobalVariable - Emit information about an objective-c interface.
181 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner7d7fff22008-11-03 09:11:11 +0000183private:
Chris Lattneraffb3732008-11-10 06:08:34 +0000184 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
185 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
186 CGBuilderTy &Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000187
Mike Stump2e722b92009-09-30 02:43:10 +0000188 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
189 void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
190 CGBuilderTy &Builder, CodeGenFunction *CGF);
Mike Stump11289f42009-09-09 15:08:12 +0000191
Devang Patel535fdaf2010-02-10 18:49:08 +0000192 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
193 // See BuildByRefType.
194 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
195 uint64_t *OffSet);
196
Devang Patel7bfc5962010-01-28 23:15:27 +0000197 /// getContextDescriptor - Get context info for the decl.
Devang Patel92e25412010-01-29 18:11:03 +0000198 llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
Devang Patel7bfc5962010-01-28 23:15:27 +0000199 llvm::DIDescriptor &CU);
Devang Patelfaf7e9a2009-10-06 00:35:31 +0000200
Devang Patel408dcf62010-03-09 00:44:50 +0000201 /// CreateCompileUnit - Create new compile unit.
202 void CreateCompileUnit();
203
204 /// getOrCreateFile - Get the file debug info descriptor for the input
205 /// location.
206 llvm::DIFile getOrCreateFile(SourceLocation Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000207
208 /// getOrCreateType - Get the type from the cache or create a new type if
209 /// necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000210 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000211
212 /// CreateTypeNode - Create type metadata for a source language type.
Devang Patel408dcf62010-03-09 00:44:50 +0000213 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
Devang Patel934661e2010-01-14 00:36:21 +0000214
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000215 /// CreateMemberType - Create new member and increase Offset by FType's size.
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000216 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
217 llvm::StringRef Name, uint64_t *Offset);
218
Devang Patel934661e2010-01-14 00:36:21 +0000219 /// getFunctionName - Get function name for the given FunctionDecl. If the
220 /// name is constructred on demand (e.g. C++ destructor) then the name
221 /// is stored on the side.
222 llvm::StringRef getFunctionName(const FunctionDecl *FD);
Devang Patel84033fb2010-01-28 18:11:52 +0000223
Devang Patel6c018202010-07-20 20:24:18 +0000224 /// getClassName - Get class name including template argument list.
225 llvm::StringRef getClassName(RecordDecl *RD);
226
Anders Carlsson11e51402010-04-17 20:15:18 +0000227 /// getVTableName - Get vtable name for the given Class.
228 llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
Devang Patel84033fb2010-01-28 18:11:52 +0000229
Devang Patelc5ffabc2010-05-12 23:46:38 +0000230 /// getLineNumber - Get line number for the location. If location is invalid
231 /// then use current location.
232 unsigned getLineNumber(SourceLocation Loc);
233
234 /// getColumnNumber - Get column number for the location. If location is
235 /// invalid then use current location.
236 unsigned getColumnNumber(SourceLocation Loc);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000237};
238} // namespace CodeGen
239} // namespace clang
240
Devang Patel5d90d622009-10-06 18:36:08 +0000241
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000242#endif