CGDebugInfo.h revision 1c29652fbcd097aa6c21511c9f4f886bb3f3a8af
1//===--- 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 "clang/AST/Type.h"
18#include "clang/AST/Expr.h"
19#include "clang/Basic/SourceLocation.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/Analysis/DebugInfo.h"
22#include "llvm/Analysis/DIBuilder.h"
23#include "llvm/Support/ValueHandle.h"
24#include "llvm/Support/Allocator.h"
25
26#include "CGBuilder.h"
27
28namespace llvm {
29  class MDNode;
30}
31
32namespace clang {
33  class VarDecl;
34  class ObjCInterfaceDecl;
35  class ClassTemplateSpecializationDecl;
36
37namespace CodeGen {
38  class CodeGenModule;
39  class CodeGenFunction;
40  class GlobalDecl;
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
53  /// TypeCache - Cache of previously constructed Types.
54  llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
55
56  bool BlockLiteralGenericSet;
57  llvm::DIType BlockLiteralGeneric;
58
59  std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
60  llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
61  // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
62  // of a function. This is used to pop unbalanced regions at the end of a
63  // function.
64  std::vector<unsigned> FnBeginRegionCount;
65
66  /// LineDirectiveFiles - This stack is used to keep track of
67  /// scopes introduced by #line directives.
68  std::vector<const char *> LineDirectiveFiles;
69
70  /// DebugInfoNames - This is a storage for names that are
71  /// constructed on demand. For example, C++ destructors, C++ operators etc..
72  llvm::BumpPtrAllocator DebugInfoNames;
73  llvm::StringRef CWDName;
74
75  llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
76  llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
77  llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
78
79  /// Helper functions for getOrCreateType.
80  llvm::DIType CreateType(const BuiltinType *Ty);
81  llvm::DIType CreateType(const ComplexType *Ty);
82  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
83  llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
84  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
85                          llvm::DIFile F);
86  llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
87  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
88  llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
89  llvm::DIType CreateType(const TagType *Ty);
90  llvm::DIType CreateType(const RecordType *Ty);
91  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
92  llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
93  llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
94  llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
95  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
96  llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
97  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
98  llvm::DIType CreateEnumType(const EnumDecl *ED);
99  llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
100                                     llvm::DIFile F);
101  llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
102                                       llvm::DIFile F);
103  llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
104  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
105  llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
106  llvm::DIType CreatePointerLikeType(unsigned Tag,
107                                     const Type *Ty, QualType PointeeTy,
108                                     llvm::DIFile F);
109
110  llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
111                                             llvm::DIFile F,
112                                             llvm::DIType RecordTy);
113
114  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
115                                 llvm::DIFile F,
116                                 llvm::SmallVectorImpl<llvm::Value *> &E,
117                                 llvm::DIType T);
118
119  void CollectCXXFriends(const CXXRecordDecl *Decl,
120                       llvm::DIFile F,
121                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
122                       llvm::DIType RecordTy);
123
124  void CollectCXXBases(const CXXRecordDecl *Decl,
125                       llvm::DIFile F,
126                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
127                       llvm::DIType RecordTy);
128
129  llvm::DIArray
130  CollectTemplateParams(const TemplateParameterList *TPList,
131                        const TemplateArgumentList &TAList,
132                        llvm::DIFile Unit);
133  llvm::DIArray
134  CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
135  llvm::DIArray
136  CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
137                           llvm::DIFile F);
138
139  llvm::DIType createFieldType(llvm::StringRef name, QualType type,
140                               Expr *bitWidth, SourceLocation loc,
141                               AccessSpecifier AS, uint64_t offsetInBits,
142                               llvm::DIFile tunit);
143  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
144                           llvm::SmallVectorImpl<llvm::Value *> &E);
145
146  void CollectVTableInfo(const CXXRecordDecl *Decl,
147                         llvm::DIFile F,
148                         llvm::SmallVectorImpl<llvm::Value *> &EltTys);
149
150public:
151  CGDebugInfo(CodeGenModule &CGM);
152  ~CGDebugInfo();
153
154  /// setLocation - Update the current source location. If \arg loc is
155  /// invalid it is ignored.
156  void setLocation(SourceLocation Loc);
157
158  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
159  /// source line.
160  void EmitStopPoint(CGBuilderTy &Builder);
161
162  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
163  /// start of a new function.
164  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
165                         llvm::Function *Fn, CGBuilderTy &Builder);
166
167  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
168  void EmitFunctionEnd(CGBuilderTy &Builder);
169
170  /// UpdateLineDirectiveRegion - Update region stack only if #line directive
171  /// has introduced scope change.
172  void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
173
174  /// UpdateCompletedType - Update type cache because the type is now
175  /// translated.
176  void UpdateCompletedType(const TagDecl *TD);
177
178  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
179  /// of a new block.
180  void EmitRegionStart(CGBuilderTy &Builder);
181
182  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
183  /// block.
184  void EmitRegionEnd(CGBuilderTy &Builder);
185
186  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
187  /// variable declaration.
188  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
189                                 CGBuilderTy &Builder);
190
191  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
192  /// imported variable declaration in a block.
193  void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
194                                         llvm::Value *storage,
195                                         CGBuilderTy &Builder,
196                                         const CGBlockInfo &blockInfo);
197
198  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
199  /// variable declaration.
200  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
201                                unsigned ArgNo, CGBuilderTy &Builder);
202
203  /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
204  /// llvm.dbg.declare for the block-literal argument to a block
205  /// invocation function.
206  void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
207                                            llvm::Value *addr,
208                                            CGBuilderTy &Builder);
209
210  /// EmitGlobalVariable - Emit information about a global variable.
211  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
212
213  /// EmitGlobalVariable - Emit information about an objective-c interface.
214  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
215
216  /// EmitGlobalVariable - Emit global variable's debug info.
217  void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
218
219  /// getOrCreateRecordType - Emit record type's standalone debug info.
220  llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
221private:
222  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
223  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
224                   unsigned ArgNo, CGBuilderTy &Builder);
225
226  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
227  // See BuildByRefType.
228  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
229                                            uint64_t *OffSet);
230
231  /// getContextDescriptor - Get context info for the decl.
232  llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
233
234  /// getCurrentDirname - Return current directory name.
235  llvm::StringRef getCurrentDirname();
236
237  /// CreateCompileUnit - Create new compile unit.
238  void CreateCompileUnit();
239
240  /// getOrCreateFile - Get the file debug info descriptor for the input
241  /// location.
242  llvm::DIFile getOrCreateFile(SourceLocation Loc);
243
244  /// getOrCreateMainFile - Get the file info for main compile unit.
245  llvm::DIFile getOrCreateMainFile();
246
247  /// getOrCreateType - Get the type from the cache or create a new type if
248  /// necessary.
249  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
250
251  /// CreateTypeNode - Create type metadata for a source language type.
252  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
253
254  /// CreateMemberType - Create new member and increase Offset by FType's size.
255  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
256                                llvm::StringRef Name, uint64_t *Offset);
257
258  /// getFunctionDeclaration - Return debug info descriptor to describe method
259  /// declaration for the given method definition.
260  llvm::DISubprogram getFunctionDeclaration(const Decl *D);
261
262  /// getFunctionName - Get function name for the given FunctionDecl. If the
263  /// name is constructred on demand (e.g. C++ destructor) then the name
264  /// is stored on the side.
265  llvm::StringRef getFunctionName(const FunctionDecl *FD);
266
267  /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
268  /// This is the display name for the debugging info.
269  llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
270
271  /// getSelectorName - Return selector name. This is used for debugging
272  /// info.
273  llvm::StringRef getSelectorName(Selector S);
274
275  /// getClassName - Get class name including template argument list.
276  llvm::StringRef getClassName(RecordDecl *RD);
277
278  /// getVTableName - Get vtable name for the given Class.
279  llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
280
281  /// getLineNumber - Get line number for the location. If location is invalid
282  /// then use current location.
283  unsigned getLineNumber(SourceLocation Loc);
284
285  /// getColumnNumber - Get column number for the location. If location is
286  /// invalid then use current location.
287  unsigned getColumnNumber(SourceLocation Loc);
288};
289} // namespace CodeGen
290} // namespace clang
291
292
293#endif
294