blob: bad13b96ad2cbcc85aeecc3646a1b7c3bf5f3f4d [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;
Eric Christopherf068c922013-04-02 22:59:11 +000054 llvm::DICompositeType ObjTy;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055 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);
Adrian Prantle86fcc42013-03-29 19:20:29 +0000122 llvm::DIType CreateSelfType(const QualType &QualTy, llvm::DIType Ty);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000123 llvm::DIType getTypeOrNull(const QualType);
124 llvm::DIType getCompletedTypeOrNull(const QualType);
125 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
126 llvm::DIFile F);
David Blaikie9c78f9b2013-01-07 23:06:35 +0000127 llvm::DIType getOrCreateInstanceMethodType(
128 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000129 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
130 llvm::DIFile F);
131 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
132 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
133 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
134 llvm::DIType CreatePointerLikeType(unsigned Tag,
135 const Type *Ty, QualType PointeeTy,
136 llvm::DIFile F);
Guy Benyeib13621d2012-12-18 14:38:23 +0000137
Adrian Prantlebbd7e02013-03-11 18:33:46 +0000138 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
Guy Benyeib13621d2012-12-18 14:38:23 +0000139 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
140
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000141 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
142 llvm::DIFile F,
143 llvm::DIType RecordTy);
144
145 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
146 llvm::DIFile F,
147 SmallVectorImpl<llvm::Value *> &E,
148 llvm::DIType T);
149
150 void CollectCXXFriends(const CXXRecordDecl *Decl,
151 llvm::DIFile F,
152 SmallVectorImpl<llvm::Value *> &EltTys,
153 llvm::DIType RecordTy);
154
155 void CollectCXXBases(const CXXRecordDecl *Decl,
156 llvm::DIFile F,
157 SmallVectorImpl<llvm::Value *> &EltTys,
158 llvm::DIType RecordTy);
159
160 llvm::DIArray
161 CollectTemplateParams(const TemplateParameterList *TPList,
162 const TemplateArgumentList &TAList,
163 llvm::DIFile Unit);
164 llvm::DIArray
165 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
166 llvm::DIArray
167 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
168 llvm::DIFile F);
169
170 llvm::DIType createFieldType(StringRef name, QualType type,
171 uint64_t sizeInBitsOverride, SourceLocation loc,
172 AccessSpecifier AS, uint64_t offsetInBits,
173 llvm::DIFile tunit,
174 llvm::DIDescriptor scope);
Eric Christopher0395de32013-01-16 01:22:32 +0000175
176 // Helpers for collecting fields of a record.
177 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
178 SmallVectorImpl<llvm::Value *> &E,
179 llvm::DIType RecordTy);
180 void CollectRecordStaticField(const VarDecl *Var,
181 SmallVectorImpl<llvm::Value *> &E,
182 llvm::DIType RecordTy);
183 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
184 llvm::DIFile F,
185 SmallVectorImpl<llvm::Value *> &E,
186 llvm::DIType RecordTy);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000187 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
188 SmallVectorImpl<llvm::Value *> &E,
189 llvm::DIType RecordTy);
190
191 void CollectVTableInfo(const CXXRecordDecl *Decl,
192 llvm::DIFile F,
193 SmallVectorImpl<llvm::Value *> &EltTys);
194
195 // CreateLexicalBlock - Create a new lexical block node and push it on
196 // the stack.
197 void CreateLexicalBlock(SourceLocation Loc);
198
199public:
200 CGDebugInfo(CodeGenModule &CGM);
201 ~CGDebugInfo();
202
203 void finalize();
204
205 /// setLocation - Update the current source location. If \arg loc is
206 /// invalid it is ignored.
207 void setLocation(SourceLocation Loc);
208
Adrian Prantl59f0a5a2013-05-16 00:41:29 +0000209 /// getLocation - Return the current source location.
210 SourceLocation getLocation() const { return CurLoc; }
211
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000212 /// EmitLocation - Emit metadata to indicate a change in line/column
213 /// information in the source file.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000214 /// \param ForceColumnInfo Assume DebugColumnInfo option is true.
215 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
216 bool ForceColumnInfo = false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000217
218 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
219 /// start of a new function.
220 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
221 llvm::Function *Fn, CGBuilderTy &Builder);
222
223 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
224 void EmitFunctionEnd(CGBuilderTy &Builder);
225
226 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
227 /// new lexical block and push the block onto the stack.
228 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
229
230 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
231 /// block and pop the current block.
232 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
233
234 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
235 /// variable declaration.
236 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
237 CGBuilderTy &Builder);
238
239 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
240 /// imported variable declaration in a block.
241 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
242 llvm::Value *storage,
243 CGBuilderTy &Builder,
244 const CGBlockInfo &blockInfo);
245
246 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
247 /// variable declaration.
248 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
249 unsigned ArgNo, CGBuilderTy &Builder);
250
251 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
252 /// llvm.dbg.declare for the block-literal argument to a block
253 /// invocation function.
254 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
Adrian Prantl836e7c92013-03-14 17:53:33 +0000255 llvm::Value *Arg,
256 llvm::Value *LocalAddr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000257 CGBuilderTy &Builder);
258
259 /// EmitGlobalVariable - Emit information about a global variable.
260 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
261
262 /// EmitGlobalVariable - Emit information about an objective-c interface.
263 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
264
265 /// EmitGlobalVariable - Emit global variable's debug info.
266 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
267
David Blaikie957dac52013-04-22 06:13:21 +0000268 /// \brief - Emit C++ using directive.
269 void EmitUsingDirective(const UsingDirectiveDecl &UD);
270
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000271 /// getOrCreateRecordType - Emit record type's standalone debug info.
272 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
273
274 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
275 /// debug info.
276 llvm::DIType getOrCreateInterfaceType(QualType Ty,
277 SourceLocation Loc);
278
279private:
280 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
281 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
282 unsigned ArgNo, CGBuilderTy &Builder);
283
284 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
285 // See BuildByRefType.
286 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
287 uint64_t *OffSet);
288
289 /// getContextDescriptor - Get context info for the decl.
David Blaikiebb000792013-04-19 06:56:38 +0000290 llvm::DIScope getContextDescriptor(const Decl *Decl);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000291
292 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
293 /// context.
294 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
295
296 /// createContextChain - Create a set of decls for the context chain.
297 llvm::DIDescriptor createContextChain(const Decl *Decl);
298
299 /// getCurrentDirname - Return current directory name.
300 StringRef getCurrentDirname();
301
302 /// CreateCompileUnit - Create new compile unit.
303 void CreateCompileUnit();
304
305 /// getOrCreateFile - Get the file debug info descriptor for the input
306 /// location.
307 llvm::DIFile getOrCreateFile(SourceLocation Loc);
308
309 /// getOrCreateMainFile - Get the file info for main compile unit.
310 llvm::DIFile getOrCreateMainFile();
311
312 /// getOrCreateType - Get the type from the cache or create a new type if
313 /// necessary.
314 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
315
316 /// getOrCreateLimitedType - Get the type from the cache or create a new
317 /// partial type if necessary.
318 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
319
320 /// CreateTypeNode - Create type metadata for a source language type.
321 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
322
Adrian Prantl4919de62013-03-06 22:03:30 +0000323 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
324 /// if Ty is an ObjCInterface or a pointer to one.
325 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
326
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000327 /// CreateLimitedTypeNode - Create type metadata for a source language
328 /// type, but only partial types for records.
329 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
330
331 /// CreateMemberType - Create new member and increase Offset by FType's size.
332 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
333 StringRef Name, uint64_t *Offset);
334
335 /// getFunctionDeclaration - Return debug info descriptor to describe method
336 /// declaration for the given method definition.
337 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
338
Eric Christopher0395de32013-01-16 01:22:32 +0000339 /// getStaticDataMemberDeclaration - Return debug info descriptor to
340 /// describe in-class static data member declaration for the given
341 /// out-of-class definition.
342 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
343
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000344 /// getFunctionName - Get function name for the given FunctionDecl. If the
345 /// name is constructred on demand (e.g. C++ destructor) then the name
346 /// is stored on the side.
347 StringRef getFunctionName(const FunctionDecl *FD);
348
349 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
350 /// This is the display name for the debugging info.
351 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
352
353 /// getSelectorName - Return selector name. This is used for debugging
354 /// info.
355 StringRef getSelectorName(Selector S);
356
357 /// getClassName - Get class name including template argument list.
358 StringRef getClassName(const RecordDecl *RD);
359
360 /// getVTableName - Get vtable name for the given Class.
361 StringRef getVTableName(const CXXRecordDecl *Decl);
362
363 /// getLineNumber - Get line number for the location. If location is invalid
364 /// then use current location.
365 unsigned getLineNumber(SourceLocation Loc);
366
367 /// getColumnNumber - Get column number for the location. If location is
368 /// invalid then use current location.
Adrian Prantl00df5ea2013-03-12 20:43:25 +0000369 /// \param Force Assume DebugColumnInfo option is true.
370 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000371};
372} // namespace CodeGen
373} // namespace clang
374
375
376#endif