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