blob: 2e896cfe2251f943accf0480c8347f5f67132b25 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
2//
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//
10// This is the source level debug info generator for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGDEBUGINFO_H
15#define CLANG_CODEGEN_CGDEBUGINFO_H
16
17#include "CGBuilder.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/Type.h"
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/DIBuilder.h"
23#include "llvm/DebugInfo.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/Support/ValueHandle.h"
26
27namespace llvm {
28 class MDNode;
29}
30
31namespace clang {
32 class CXXMethodDecl;
33 class VarDecl;
34 class ObjCInterfaceDecl;
Adrian Prantl4919de62013-03-06 22:03:30 +000035 class ObjCIvarDecl;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000036 class ClassTemplateSpecializationDecl;
37 class GlobalDecl;
38
39namespace CodeGen {
40 class CodeGenModule;
41 class CodeGenFunction;
42 class CGBlockInfo;
43
44/// CGDebugInfo - This class gathers all debug information during compilation
45/// and is responsible for emitting to llvm globals or pass directly to
46/// the backend.
47class CGDebugInfo {
48 CodeGenModule &CGM;
49 llvm::DIBuilder DBuilder;
50 llvm::DICompileUnit TheCU;
51 SourceLocation CurLoc, PrevLoc;
52 llvm::DIType VTablePtrType;
53 llvm::DIType ClassTy;
54 llvm::DIType ObjTy;
55 llvm::DIType SelTy;
Guy Benyeib13621d2012-12-18 14:38:23 +000056 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy;
57 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy;
58 llvm::DIType OCLImage3dDITy;
Guy Benyeie6b9d802013-01-20 12:31:11 +000059 llvm::DIType OCLEventDITy;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000060
61 /// TypeCache - Cache of previously constructed Types.
62 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
63
Adrian Prantl4919de62013-03-06 22:03:30 +000064 /// ObjCInterfaceCache - Cache of previously constructed interfaces
65 /// which may change. Storing a pair of DIType and checksum.
66 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
67 ObjCInterfaceCache;
68
Adrian Prantlebbd7e02013-03-11 18:33:46 +000069 /// RetainedTypes - list of interfaces we want to keep even if orphaned.
70 std::vector<void *> RetainedTypes;
71
Guy Benyei7f92f2d2012-12-18 14:30:41 +000072 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
73 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
74
75 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
76 /// compilation.
77 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
78
79 bool BlockLiteralGenericSet;
80 llvm::DIType BlockLiteralGeneric;
81
82 // LexicalBlockStack - Keep track of our current nested lexical block.
83 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
84 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
85 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
86 // beginning of a function. This is used to pop unbalanced regions at
87 // the end of a function.
88 std::vector<unsigned> FnBeginRegionCount;
89
90 /// DebugInfoNames - This is a storage for names that are
91 /// constructed on demand. For example, C++ destructors, C++ operators etc..
92 llvm::BumpPtrAllocator DebugInfoNames;
93 StringRef CWDName;
94
95 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
96 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
97 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
Eric Christopher0395de32013-01-16 01:22:32 +000098 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000099
100 /// Helper functions for getOrCreateType.
Adrian Prantl4919de62013-03-06 22:03:30 +0000101 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000102 llvm::DIType CreateType(const BuiltinType *Ty);
103 llvm::DIType CreateType(const ComplexType *Ty);
104 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
105 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
106 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
107 llvm::DIFile F);
108 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
109 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
110 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
111 llvm::DIType CreateType(const RecordType *Ty);
112 llvm::DIType CreateLimitedType(const RecordType *Ty);
113 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
114 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
115 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
116 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
117 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
118 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
119 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
120 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
121 llvm::DIType CreateEnumType(const EnumDecl *ED);
122 llvm::DIType getTypeOrNull(const QualType);
123 llvm::DIType getCompletedTypeOrNull(const QualType);
124 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
125 llvm::DIFile F);
David Blaikie9c78f9b2013-01-07 23:06:35 +0000126 llvm::DIType getOrCreateInstanceMethodType(
127 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000128 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
129 llvm::DIFile F);
130 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
131 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
132 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
133 llvm::DIType CreatePointerLikeType(unsigned Tag,
134 const Type *Ty, QualType PointeeTy,
135 llvm::DIFile F);
Guy Benyeib13621d2012-12-18 14:38:23 +0000136
Adrian Prantlebbd7e02013-03-11 18:33:46 +0000137 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
Guy Benyeib13621d2012-12-18 14:38:23 +0000138 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
139
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000140 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
141 llvm::DIFile F,
142 llvm::DIType RecordTy);
143
144 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
145 llvm::DIFile F,
146 SmallVectorImpl<llvm::Value *> &E,
147 llvm::DIType T);
148
149 void CollectCXXFriends(const CXXRecordDecl *Decl,
150 llvm::DIFile F,
151 SmallVectorImpl<llvm::Value *> &EltTys,
152 llvm::DIType RecordTy);
153
154 void CollectCXXBases(const CXXRecordDecl *Decl,
155 llvm::DIFile F,
156 SmallVectorImpl<llvm::Value *> &EltTys,
157 llvm::DIType RecordTy);
158
159 llvm::DIArray
160 CollectTemplateParams(const TemplateParameterList *TPList,
161 const TemplateArgumentList &TAList,
162 llvm::DIFile Unit);
163 llvm::DIArray
164 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
165 llvm::DIArray
166 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
167 llvm::DIFile F);
168
169 llvm::DIType createFieldType(StringRef name, QualType type,
170 uint64_t sizeInBitsOverride, SourceLocation loc,
171 AccessSpecifier AS, uint64_t offsetInBits,
172 llvm::DIFile tunit,
173 llvm::DIDescriptor scope);
Eric Christopher0395de32013-01-16 01:22:32 +0000174
175 // Helpers for collecting fields of a record.
176 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
177 SmallVectorImpl<llvm::Value *> &E,
178 llvm::DIType RecordTy);
179 void CollectRecordStaticField(const VarDecl *Var,
180 SmallVectorImpl<llvm::Value *> &E,
181 llvm::DIType RecordTy);
182 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
183 llvm::DIFile F,
184 SmallVectorImpl<llvm::Value *> &E,
185 llvm::DIType RecordTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000186 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
187 SmallVectorImpl<llvm::Value *> &E,
188 llvm::DIType RecordTy);
189
190 void CollectVTableInfo(const CXXRecordDecl *Decl,
191 llvm::DIFile F,
192 SmallVectorImpl<llvm::Value *> &EltTys);
193
194 // CreateLexicalBlock - Create a new lexical block node and push it on
195 // the stack.
196 void CreateLexicalBlock(SourceLocation Loc);
197
198public:
199 CGDebugInfo(CodeGenModule &CGM);
200 ~CGDebugInfo();
201
202 void finalize();
203
204 /// setLocation - Update the current source location. If \arg loc is
205 /// invalid it is ignored.
206 void setLocation(SourceLocation Loc);
207
208 /// EmitLocation - Emit metadata to indicate a change in line/column
209 /// information in the source file.
210 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
211
212 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
213 /// start of a new function.
214 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
215 llvm::Function *Fn, CGBuilderTy &Builder);
216
217 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
218 void EmitFunctionEnd(CGBuilderTy &Builder);
219
220 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
221 /// new lexical block and push the block onto the stack.
222 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
223
224 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
225 /// block and pop the current block.
226 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
227
228 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
229 /// variable declaration.
230 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
231 CGBuilderTy &Builder);
232
233 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
234 /// imported variable declaration in a block.
235 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
236 llvm::Value *storage,
237 CGBuilderTy &Builder,
238 const CGBlockInfo &blockInfo);
239
240 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
241 /// variable declaration.
242 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
243 unsigned ArgNo, CGBuilderTy &Builder);
244
245 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
246 /// llvm.dbg.declare for the block-literal argument to a block
247 /// invocation function.
248 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
249 llvm::Value *addr,
250 CGBuilderTy &Builder);
251
252 /// EmitGlobalVariable - Emit information about a global variable.
253 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
254
255 /// EmitGlobalVariable - Emit information about an objective-c interface.
256 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
257
258 /// EmitGlobalVariable - Emit global variable's debug info.
259 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
260
261 /// getOrCreateRecordType - Emit record type's standalone debug info.
262 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
263
264 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
265 /// debug info.
266 llvm::DIType getOrCreateInterfaceType(QualType Ty,
267 SourceLocation Loc);
268
269private:
270 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
271 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
272 unsigned ArgNo, CGBuilderTy &Builder);
273
274 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
275 // See BuildByRefType.
276 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
277 uint64_t *OffSet);
278
279 /// getContextDescriptor - Get context info for the decl.
280 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
281
282 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
283 /// context.
284 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
285
286 /// createContextChain - Create a set of decls for the context chain.
287 llvm::DIDescriptor createContextChain(const Decl *Decl);
288
289 /// getCurrentDirname - Return current directory name.
290 StringRef getCurrentDirname();
291
292 /// CreateCompileUnit - Create new compile unit.
293 void CreateCompileUnit();
294
295 /// getOrCreateFile - Get the file debug info descriptor for the input
296 /// location.
297 llvm::DIFile getOrCreateFile(SourceLocation Loc);
298
299 /// getOrCreateMainFile - Get the file info for main compile unit.
300 llvm::DIFile getOrCreateMainFile();
301
302 /// getOrCreateType - Get the type from the cache or create a new type if
303 /// necessary.
304 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
305
306 /// getOrCreateLimitedType - Get the type from the cache or create a new
307 /// partial type if necessary.
308 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
309
310 /// CreateTypeNode - Create type metadata for a source language type.
311 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
312
Adrian Prantl4919de62013-03-06 22:03:30 +0000313 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
314 /// if Ty is an ObjCInterface or a pointer to one.
315 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
316
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000317 /// CreateLimitedTypeNode - Create type metadata for a source language
318 /// type, but only partial types for records.
319 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
320
321 /// CreateMemberType - Create new member and increase Offset by FType's size.
322 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
323 StringRef Name, uint64_t *Offset);
324
325 /// getFunctionDeclaration - Return debug info descriptor to describe method
326 /// declaration for the given method definition.
327 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
328
Eric Christopher0395de32013-01-16 01:22:32 +0000329 /// getStaticDataMemberDeclaration - Return debug info descriptor to
330 /// describe in-class static data member declaration for the given
331 /// out-of-class definition.
332 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
333
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000334 /// getFunctionName - Get function name for the given FunctionDecl. If the
335 /// name is constructred on demand (e.g. C++ destructor) then the name
336 /// is stored on the side.
337 StringRef getFunctionName(const FunctionDecl *FD);
338
339 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
340 /// This is the display name for the debugging info.
341 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
342
343 /// getSelectorName - Return selector name. This is used for debugging
344 /// info.
345 StringRef getSelectorName(Selector S);
346
347 /// getClassName - Get class name including template argument list.
348 StringRef getClassName(const RecordDecl *RD);
349
350 /// getVTableName - Get vtable name for the given Class.
351 StringRef getVTableName(const CXXRecordDecl *Decl);
352
353 /// getLineNumber - Get line number for the location. If location is invalid
354 /// then use current location.
355 unsigned getLineNumber(SourceLocation Loc);
356
357 /// getColumnNumber - Get column number for the location. If location is
358 /// invalid then use current location.
359 unsigned getColumnNumber(SourceLocation Loc);
360};
361} // namespace CodeGen
362} // namespace clang
363
364
365#endif