blob: 4457e43a0a0992844b461ace1c335acd5d28c9e8 [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;
88
89 /// Helper functions for getOrCreateType.
90 llvm::DIType CreateType(const BuiltinType *Ty);
91 llvm::DIType CreateType(const ComplexType *Ty);
92 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
94 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
95 llvm::DIFile F);
96 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
97 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
98 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
99 llvm::DIType CreateType(const RecordType *Ty);
100 llvm::DIType CreateLimitedType(const RecordType *Ty);
101 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
102 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
103 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
104 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
105 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
106 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
107 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
108 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
109 llvm::DIType CreateEnumType(const EnumDecl *ED);
110 llvm::DIType getTypeOrNull(const QualType);
111 llvm::DIType getCompletedTypeOrNull(const QualType);
112 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
113 llvm::DIFile F);
David Blaikie9c78f9b2013-01-07 23:06:35 +0000114 llvm::DIType getOrCreateInstanceMethodType(
115 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000116 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
117 llvm::DIFile F);
118 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
119 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
120 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
121 llvm::DIType CreatePointerLikeType(unsigned Tag,
122 const Type *Ty, QualType PointeeTy,
123 llvm::DIFile F);
Guy Benyeib13621d2012-12-18 14:38:23 +0000124
125 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
126
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000127 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
128 llvm::DIFile F,
129 llvm::DIType RecordTy);
130
131 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
132 llvm::DIFile F,
133 SmallVectorImpl<llvm::Value *> &E,
134 llvm::DIType T);
135
136 void CollectCXXFriends(const CXXRecordDecl *Decl,
137 llvm::DIFile F,
138 SmallVectorImpl<llvm::Value *> &EltTys,
139 llvm::DIType RecordTy);
140
141 void CollectCXXBases(const CXXRecordDecl *Decl,
142 llvm::DIFile F,
143 SmallVectorImpl<llvm::Value *> &EltTys,
144 llvm::DIType RecordTy);
145
146 llvm::DIArray
147 CollectTemplateParams(const TemplateParameterList *TPList,
148 const TemplateArgumentList &TAList,
149 llvm::DIFile Unit);
150 llvm::DIArray
151 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
152 llvm::DIArray
153 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
154 llvm::DIFile F);
155
156 llvm::DIType createFieldType(StringRef name, QualType type,
157 uint64_t sizeInBitsOverride, SourceLocation loc,
158 AccessSpecifier AS, uint64_t offsetInBits,
159 llvm::DIFile tunit,
160 llvm::DIDescriptor scope);
161 void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
162 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
163 SmallVectorImpl<llvm::Value *> &E,
164 llvm::DIType RecordTy);
165
166 void CollectVTableInfo(const CXXRecordDecl *Decl,
167 llvm::DIFile F,
168 SmallVectorImpl<llvm::Value *> &EltTys);
169
170 // CreateLexicalBlock - Create a new lexical block node and push it on
171 // the stack.
172 void CreateLexicalBlock(SourceLocation Loc);
173
174public:
175 CGDebugInfo(CodeGenModule &CGM);
176 ~CGDebugInfo();
177
178 void finalize();
179
180 /// setLocation - Update the current source location. If \arg loc is
181 /// invalid it is ignored.
182 void setLocation(SourceLocation Loc);
183
184 /// EmitLocation - Emit metadata to indicate a change in line/column
185 /// information in the source file.
186 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
187
188 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
189 /// start of a new function.
190 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
191 llvm::Function *Fn, CGBuilderTy &Builder);
192
193 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
194 void EmitFunctionEnd(CGBuilderTy &Builder);
195
196 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
197 /// new lexical block and push the block onto the stack.
198 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
199
200 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
201 /// block and pop the current block.
202 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
203
204 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
205 /// variable declaration.
206 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
207 CGBuilderTy &Builder);
208
209 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
210 /// imported variable declaration in a block.
211 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
212 llvm::Value *storage,
213 CGBuilderTy &Builder,
214 const CGBlockInfo &blockInfo);
215
216 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
217 /// variable declaration.
218 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
219 unsigned ArgNo, CGBuilderTy &Builder);
220
221 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
222 /// llvm.dbg.declare for the block-literal argument to a block
223 /// invocation function.
224 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
225 llvm::Value *addr,
226 CGBuilderTy &Builder);
227
228 /// EmitGlobalVariable - Emit information about a global variable.
229 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
230
231 /// EmitGlobalVariable - Emit information about an objective-c interface.
232 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
233
234 /// EmitGlobalVariable - Emit global variable's debug info.
235 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
236
237 /// getOrCreateRecordType - Emit record type's standalone debug info.
238 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
239
240 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
241 /// debug info.
242 llvm::DIType getOrCreateInterfaceType(QualType Ty,
243 SourceLocation Loc);
244
245private:
246 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
247 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
248 unsigned ArgNo, CGBuilderTy &Builder);
249
250 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
251 // See BuildByRefType.
252 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
253 uint64_t *OffSet);
254
255 /// getContextDescriptor - Get context info for the decl.
256 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
257
258 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
259 /// context.
260 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
261
262 /// createContextChain - Create a set of decls for the context chain.
263 llvm::DIDescriptor createContextChain(const Decl *Decl);
264
265 /// getCurrentDirname - Return current directory name.
266 StringRef getCurrentDirname();
267
268 /// CreateCompileUnit - Create new compile unit.
269 void CreateCompileUnit();
270
271 /// getOrCreateFile - Get the file debug info descriptor for the input
272 /// location.
273 llvm::DIFile getOrCreateFile(SourceLocation Loc);
274
275 /// getOrCreateMainFile - Get the file info for main compile unit.
276 llvm::DIFile getOrCreateMainFile();
277
278 /// getOrCreateType - Get the type from the cache or create a new type if
279 /// necessary.
280 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
281
282 /// getOrCreateLimitedType - Get the type from the cache or create a new
283 /// partial type if necessary.
284 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
285
286 /// CreateTypeNode - Create type metadata for a source language type.
287 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
288
289 /// CreateLimitedTypeNode - Create type metadata for a source language
290 /// type, but only partial types for records.
291 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
292
293 /// CreateMemberType - Create new member and increase Offset by FType's size.
294 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
295 StringRef Name, uint64_t *Offset);
296
297 /// getFunctionDeclaration - Return debug info descriptor to describe method
298 /// declaration for the given method definition.
299 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
300
301 /// getFunctionName - Get function name for the given FunctionDecl. If the
302 /// name is constructred on demand (e.g. C++ destructor) then the name
303 /// is stored on the side.
304 StringRef getFunctionName(const FunctionDecl *FD);
305
306 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
307 /// This is the display name for the debugging info.
308 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
309
310 /// getSelectorName - Return selector name. This is used for debugging
311 /// info.
312 StringRef getSelectorName(Selector S);
313
314 /// getClassName - Get class name including template argument list.
315 StringRef getClassName(const RecordDecl *RD);
316
317 /// getVTableName - Get vtable name for the given Class.
318 StringRef getVTableName(const CXXRecordDecl *Decl);
319
320 /// getLineNumber - Get line number for the location. If location is invalid
321 /// then use current location.
322 unsigned getLineNumber(SourceLocation Loc);
323
324 /// getColumnNumber - Get column number for the location. If location is
325 /// invalid then use current location.
326 unsigned getColumnNumber(SourceLocation Loc);
327};
328} // namespace CodeGen
329} // namespace clang
330
331
332#endif