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