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