CGDebugInfo.h revision 25c2c8fb9309050612009a6571e2660e75531348
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/Support/ValueHandle.h"
23#include "llvm/Support/Allocator.h"
24
25#include "CGBuilder.h"
26
27namespace llvm {
28  class MDNode;
29}
30
31namespace clang {
32  class VarDecl;
33  class ObjCInterfaceDecl;
34
35namespace CodeGen {
36  class CodeGenModule;
37  class CodeGenFunction;
38  class GlobalDecl;
39
40/// CGDebugInfo - This class gathers all debug information during compilation
41/// and is responsible for emitting to llvm globals or pass directly to
42/// the backend.
43class CGDebugInfo {
44  CodeGenModule &CGM;
45  llvm::DIFactory DebugFactory;
46  llvm::DICompileUnit TheCU;
47  SourceLocation CurLoc, PrevLoc;
48  llvm::DIType VTablePtrType;
49  /// FwdDeclCount - This counter is used to ensure unique names for forward
50  /// record decls.
51  unsigned FwdDeclCount;
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, llvm::DIFile F);
81  llvm::DIType CreateType(const ComplexType *Ty, llvm::DIFile F);
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, llvm::DIFile F);
90  llvm::DIType CreateType(const RecordType *Ty, llvm::DIFile F);
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 EnumType *Ty, llvm::DIFile F);
94  llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
95  llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
96  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
97  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
98  llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
99                                     llvm::DIFile F);
100  llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
101  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
102                                         llvm::DIDescriptor Unit);
103
104  llvm::DIType CreatePointerLikeType(unsigned Tag,
105                                     const Type *Ty, QualType PointeeTy,
106                                     llvm::DIFile F);
107
108  llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
109                                             llvm::DIFile F,
110                                             llvm::DICompositeType &RecordTy);
111
112  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
113                                 llvm::DIFile F,
114                                 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
115                                 llvm::DICompositeType &T);
116  void CollectCXXBases(const CXXRecordDecl *Decl,
117                       llvm::DIFile F,
118                       llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
119                       llvm::DICompositeType &RecordTy);
120
121
122  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
123                           llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
124
125  void CollectVTableInfo(const CXXRecordDecl *Decl,
126                         llvm::DIFile F,
127                         llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
128
129public:
130  CGDebugInfo(CodeGenModule &CGM);
131  ~CGDebugInfo();
132
133  /// setLocation - Update the current source location. If \arg loc is
134  /// invalid it is ignored.
135  void setLocation(SourceLocation Loc);
136
137  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
138  /// source line.
139  void EmitStopPoint(CGBuilderTy &Builder);
140
141  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
142  /// start of a new function.
143  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
144                         llvm::Function *Fn, CGBuilderTy &Builder);
145
146  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
147  void EmitFunctionEnd(CGBuilderTy &Builder);
148
149  /// UpdateLineDirectiveRegion - Update region stack only if #line directive
150  /// has introduced scope change.
151  void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
152
153  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
154  /// of a new block.
155  void EmitRegionStart(CGBuilderTy &Builder);
156
157  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
158  /// block.
159  void EmitRegionEnd(CGBuilderTy &Builder);
160
161  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
162  /// variable declaration.
163  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
164                                 CGBuilderTy &Builder);
165
166  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
167  /// imported variable declaration in a block.
168  void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
169                                         llvm::Value *AI,
170                                         CGBuilderTy &Builder,
171                                         CodeGenFunction *CGF);
172
173  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
174  /// variable declaration.
175  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
176                                CGBuilderTy &Builder);
177
178  /// EmitGlobalVariable - Emit information about a global variable.
179  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
180
181  /// EmitGlobalVariable - Emit information about an objective-c interface.
182  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
183
184  /// EmitGlobalVariable - Emit global variable's debug info.
185  void EmitGlobalVariable(const ValueDecl *VD, llvm::ConstantInt *Init,
186                          CGBuilderTy &Builder);
187
188private:
189  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
190  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
191                   CGBuilderTy &Builder);
192
193  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
194  void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
195                   CGBuilderTy &Builder, CodeGenFunction *CGF);
196
197  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
198  // See BuildByRefType.
199  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
200                                            uint64_t *OffSet);
201
202  /// getContextDescriptor - Get context info for the decl.
203  llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
204                                          llvm::DIDescriptor &CU);
205
206  /// getCurrentDirname - Return current directory name.
207  llvm::StringRef getCurrentDirname();
208
209  /// CreateCompileUnit - Create new compile unit.
210  void CreateCompileUnit();
211
212  /// getOrCreateFile - Get the file debug info descriptor for the input
213  /// location.
214  llvm::DIFile getOrCreateFile(SourceLocation Loc);
215
216  /// getOrCreateType - Get the type from the cache or create a new type if
217  /// necessary.
218  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
219
220  /// CreateTypeNode - Create type metadata for a source language type.
221  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
222
223  /// CreateMemberType - Create new member and increase Offset by FType's size.
224  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
225                                llvm::StringRef Name, uint64_t *Offset);
226
227  /// getFunctionName - Get function name for the given FunctionDecl. If the
228  /// name is constructred on demand (e.g. C++ destructor) then the name
229  /// is stored on the side.
230  llvm::StringRef getFunctionName(const FunctionDecl *FD);
231
232  /// getClassName - Get class name including template argument list.
233  llvm::StringRef getClassName(RecordDecl *RD);
234
235  /// getVTableName - Get vtable name for the given Class.
236  llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
237
238  /// getLineNumber - Get line number for the location. If location is invalid
239  /// then use current location.
240  unsigned getLineNumber(SourceLocation Loc);
241
242  /// getColumnNumber - Get column number for the location. If location is
243  /// invalid then use current location.
244  unsigned getColumnNumber(SourceLocation Loc);
245};
246} // namespace CodeGen
247} // namespace clang
248
249
250#endif
251