blob: 1d72cc18ea833c1f3cd856bd52b76f9b32eb9773 [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"
Devang Patel823d8e92010-12-08 22:42:58 +000022#include "llvm/Analysis/DIBuilder.h"
Daniel Dunbar23e81ba2009-09-19 19:27:24 +000023#include "llvm/Support/ValueHandle.h"
Devang Patel9c6c3a02010-01-14 00:36:21 +000024#include "llvm/Support/Allocator.h"
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 Patel823d8e92010-12-08 22:42:58 +000046 llvm::DIBuilder DBuilder;
Devang Patel17800552010-03-09 00:44:50 +000047 llvm::DICompileUnit TheCU;
Chris Lattner9c85ba32008-11-10 06:08:34 +000048 SourceLocation CurLoc, PrevLoc;
Devang Patel4ce3f202010-01-28 18:11:52 +000049 llvm::DIType VTablePtrType;
Devang Patel17800552010-03-09 00:44:50 +000050
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051 /// TypeCache - Cache of previously constructed Types.
Ted Kremenek590838b2010-03-29 18:29:57 +000052 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Mike Stump9bc093c2009-05-14 02:03:51 +000054 bool BlockLiteralGenericSet;
55 llvm::DIType BlockLiteralGeneric;
56
Devang Patel8fae0602009-11-13 19:10:24 +000057 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
Devang Patel3dd96a12010-01-29 18:11:03 +000058 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
Devang Patel5a6fbcf2010-07-22 22:29:16 +000059 // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
60 // of a function. This is used to pop unbalanced regions at the end of a
61 // function.
62 std::vector<unsigned> FnBeginRegionCount;
63
64 /// LineDirectiveFiles - This stack is used to keep track of
65 /// scopes introduced by #line directives.
66 std::vector<const char *> LineDirectiveFiles;
Eli Friedman3f2af102008-05-22 01:40:10 +000067
Devang Patel89f05f82010-01-28 18:21:00 +000068 /// DebugInfoNames - This is a storage for names that are
Devang Patel9c6c3a02010-01-14 00:36:21 +000069 /// constructed on demand. For example, C++ destructors, C++ operators etc..
Devang Patel89f05f82010-01-28 18:21:00 +000070 llvm::BumpPtrAllocator DebugInfoNames;
Devang Patelac4d13c2010-07-27 15:17:16 +000071 llvm::StringRef CWDName;
Devang Patel9c6c3a02010-01-14 00:36:21 +000072
Ted Kremenek9c250392010-03-30 00:27:51 +000073 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
Devang Patel4125fd22010-01-19 01:54:44 +000074 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
Devang Patelabb485f2010-02-01 19:16:32 +000075 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
Devang Patel4125fd22010-01-19 01:54:44 +000076
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000077 /// Helper functions for getOrCreateType.
Devang Patelf1d1d9a2010-11-01 16:52:40 +000078 llvm::DIType CreateType(const BuiltinType *Ty);
Devang Patel344ff5d2010-12-09 00:25:29 +000079 llvm::DIType CreateType(const ComplexType *Ty);
Devang Patel17800552010-03-09 00:44:50 +000080 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
81 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
Mike Stump1eb44332009-09-09 15:08:12 +000082 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +000083 llvm::DIFile F);
84 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
85 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
86 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
Devang Patel31f7d022011-01-17 22:23:07 +000087 llvm::DIType CreateType(const TagType *Ty);
88 llvm::DIType CreateType(const RecordType *Ty);
Devang Patel17800552010-03-09 00:44:50 +000089 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
John McCallc12c5bb2010-05-15 11:32:37 +000090 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
Devang Patel17800552010-03-09 00:44:50 +000091 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
94 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
Devang Patel31f7d022011-01-17 22:23:07 +000095 llvm::DIType CreateEnumType(const EnumDecl *ED);
Devang Patela6da1922010-01-28 00:28:01 +000096 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +000097 llvm::DIFile F);
98 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
Devang Patel170cef32010-12-09 00:33:05 +000099 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000100 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
Anders Carlssona031b352009-11-06 19:19:55 +0000101 llvm::DIType CreatePointerLikeType(unsigned Tag,
102 const Type *Ty, QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000103 llvm::DIFile F);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000104
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000105 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000106 llvm::DIFile F,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000107 llvm::DIType RecordTy);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000108
Devang Patel4125fd22010-01-19 01:54:44 +0000109 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000110 llvm::DIFile F,
Devang Patel823d8e92010-12-08 22:42:58 +0000111 llvm::SmallVectorImpl<llvm::Value *> &E,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000112 llvm::DIType T);
Devang Patel2ed8f002010-08-27 17:47:47 +0000113
114 void CollectCXXFriends(const CXXRecordDecl *Decl,
115 llvm::DIFile F,
Devang Patel823d8e92010-12-08 22:42:58 +0000116 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000117 llvm::DIType RecordTy);
118
Devang Patela245c5b2010-01-25 23:32:18 +0000119 void CollectCXXBases(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000120 llvm::DIFile F,
Devang Patel823d8e92010-12-08 22:42:58 +0000121 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000122 llvm::DIType RecordTy);
Devang Patela245c5b2010-01-25 23:32:18 +0000123
124
Devang Patel17800552010-03-09 00:44:50 +0000125 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
Devang Patel823d8e92010-12-08 22:42:58 +0000126 llvm::SmallVectorImpl<llvm::Value *> &E);
Devang Patel4ce3f202010-01-28 18:11:52 +0000127
Anders Carlsson046c2942010-04-17 20:15:18 +0000128 void CollectVTableInfo(const CXXRecordDecl *Decl,
Devang Patel17800552010-03-09 00:44:50 +0000129 llvm::DIFile F,
Devang Patel823d8e92010-12-08 22:42:58 +0000130 llvm::SmallVectorImpl<llvm::Value *> &EltTys);
Devang Patel4ce3f202010-01-28 18:11:52 +0000131
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000132public:
Anders Carlsson20f12a22009-12-06 18:00:51 +0000133 CGDebugInfo(CodeGenModule &CGM);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000134 ~CGDebugInfo();
135
Daniel Dunbar66031a52008-10-17 16:15:48 +0000136 /// setLocation - Update the current source location. If \arg loc is
137 /// invalid it is ignored.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000138 void setLocation(SourceLocation Loc);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000139
140 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
141 /// source line.
Devang Patel4d939e62010-07-20 22:20:10 +0000142 void EmitStopPoint(CGBuilderTy &Builder);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000143
144 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000145 /// start of a new function.
Devang Patel9c6c3a02010-01-14 00:36:21 +0000146 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000147 llvm::Function *Fn, CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Devang Patel5a6fbcf2010-07-22 22:29:16 +0000149 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
150 void EmitFunctionEnd(CGBuilderTy &Builder);
151
152 /// UpdateLineDirectiveRegion - Update region stack only if #line directive
153 /// has introduced scope change.
154 void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
155
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000156 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
Mike Stump1eb44332009-09-09 15:08:12 +0000157 /// of a new block.
Devang Patel4d939e62010-07-20 22:20:10 +0000158 void EmitRegionStart(CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000159
160 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000161 /// block.
Devang Patel4d939e62010-07-20 22:20:10 +0000162 void EmitRegionEnd(CGBuilderTy &Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000163
Chris Lattner9c85ba32008-11-10 06:08:34 +0000164 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
165 /// variable declaration.
166 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
167 CGBuilderTy &Builder);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000168
Mike Stumpb1a6e682009-09-30 02:43:10 +0000169 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
170 /// imported variable declaration in a block.
171 void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
172 llvm::Value *AI,
173 CGBuilderTy &Builder,
174 CodeGenFunction *CGF);
175
Chris Lattner9c85ba32008-11-10 06:08:34 +0000176 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
177 /// variable declaration.
178 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
179 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000181 /// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000183
184 /// EmitGlobalVariable - Emit information about an objective-c interface.
185 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Devang Patel25c2c8f2010-08-10 17:53:33 +0000187 /// EmitGlobalVariable - Emit global variable's debug info.
John McCall189d6ef2010-10-09 01:34:31 +0000188 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
Devang Patel8d308382010-08-10 07:24:25 +0000189
Devang Patelc69e1cf2010-09-30 19:05:55 +0000190 /// getOrCreateRecordType - Emit record type's standalone debug info.
191 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
Chris Lattner86cd8af2008-11-03 09:11:11 +0000192private:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
194 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
195 CGBuilderTy &Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Mike Stumpb1a6e682009-09-30 02:43:10 +0000197 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
198 void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
199 CGBuilderTy &Builder, CodeGenFunction *CGF);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Devang Patel809b9bb2010-02-10 18:49:08 +0000201 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
202 // See BuildByRefType.
203 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
204 uint64_t *OffSet);
205
Devang Patel33583052010-01-28 23:15:27 +0000206 /// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +0000207 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
Devang Patel979ec2e2009-10-06 00:35:31 +0000208
Devang Patelac4d13c2010-07-27 15:17:16 +0000209 /// getCurrentDirname - Return current directory name.
210 llvm::StringRef getCurrentDirname();
211
Devang Patel17800552010-03-09 00:44:50 +0000212 /// CreateCompileUnit - Create new compile unit.
213 void CreateCompileUnit();
214
215 /// getOrCreateFile - Get the file debug info descriptor for the input
216 /// location.
217 llvm::DIFile getOrCreateFile(SourceLocation Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000218
Devang Patel532105f2010-10-28 22:03:20 +0000219 /// getOrCreateMainFile - Get the file info for main compile unit.
220 llvm::DIFile getOrCreateMainFile();
221
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000222 /// getOrCreateType - Get the type from the cache or create a new type if
223 /// necessary.
Devang Patel17800552010-03-09 00:44:50 +0000224 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
Daniel Dunbar03faac32009-09-19 19:27:14 +0000225
226 /// CreateTypeNode - Create type metadata for a source language type.
Devang Patel17800552010-03-09 00:44:50 +0000227 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
Devang Patel9c6c3a02010-01-14 00:36:21 +0000228
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000229 /// CreateMemberType - Create new member and increase Offset by FType's size.
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000230 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
231 llvm::StringRef Name, uint64_t *Offset);
232
Devang Patel9c6c3a02010-01-14 00:36:21 +0000233 /// getFunctionName - Get function name for the given FunctionDecl. If the
234 /// name is constructred on demand (e.g. C++ destructor) then the name
235 /// is stored on the side.
236 llvm::StringRef getFunctionName(const FunctionDecl *FD);
Devang Patelcebbedd2010-10-12 23:24:54 +0000237
David Chisnall52044a22010-09-02 18:01:51 +0000238 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
239 /// This is the display name for the debugging info.
240 llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000241
Devang Patel700a1cb2010-07-20 20:24:18 +0000242 /// getClassName - Get class name including template argument list.
243 llvm::StringRef getClassName(RecordDecl *RD);
244
Anders Carlsson046c2942010-04-17 20:15:18 +0000245 /// getVTableName - Get vtable name for the given Class.
246 llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000247
Devang Patel8ab870d2010-05-12 23:46:38 +0000248 /// getLineNumber - Get line number for the location. If location is invalid
249 /// then use current location.
250 unsigned getLineNumber(SourceLocation Loc);
251
252 /// getColumnNumber - Get column number for the location. If location is
253 /// invalid then use current location.
254 unsigned getColumnNumber(SourceLocation Loc);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000255};
256} // namespace CodeGen
257} // namespace clang
258
Devang Patelbbd9fa42009-10-06 18:36:08 +0000259
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000260#endif