CGDebugInfo.h revision e9b711c39827d7dcba0fc4cb787a4a7395285634
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
74  llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
75  llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
76  llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
77
78  /// Helper functions for getOrCreateType.
79  llvm::DIType CreateType(const BuiltinType *Ty, llvm::DIFile F);
80  llvm::DIType CreateType(const ComplexType *Ty, llvm::DIFile F);
81  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
82  llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
83  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
84                          llvm::DIFile F);
85  llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
86  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
87  llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
88  llvm::DIType CreateType(const TagType *Ty, llvm::DIFile F);
89  llvm::DIType CreateType(const RecordType *Ty, llvm::DIFile F);
90  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
91  llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
92  llvm::DIType CreateType(const EnumType *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 MemberPointerType *Ty, llvm::DIFile F);
97  llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
98                                     llvm::DIFile F);
99  llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
100  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
101                                         llvm::DIDescriptor Unit);
102
103  llvm::DIType CreatePointerLikeType(unsigned Tag,
104                                     const Type *Ty, QualType PointeeTy,
105                                     llvm::DIFile F);
106
107  llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
108                                             llvm::DIFile F,
109                                             llvm::DICompositeType &RecordTy);
110
111  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
112                                 llvm::DIFile F,
113                                 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
114                                 llvm::DICompositeType &T);
115  void CollectCXXBases(const CXXRecordDecl *Decl,
116                       llvm::DIFile F,
117                       llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
118                       llvm::DICompositeType &RecordTy);
119
120
121  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
122                           llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
123
124  void CollectVTableInfo(const CXXRecordDecl *Decl,
125                         llvm::DIFile F,
126                         llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
127
128public:
129  CGDebugInfo(CodeGenModule &CGM);
130  ~CGDebugInfo();
131
132  /// setLocation - Update the current source location. If \arg loc is
133  /// invalid it is ignored.
134  void setLocation(SourceLocation Loc);
135
136  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
137  /// source line.
138  void EmitStopPoint(CGBuilderTy &Builder);
139
140  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
141  /// start of a new function.
142  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
143                         llvm::Function *Fn, CGBuilderTy &Builder);
144
145  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
146  void EmitFunctionEnd(CGBuilderTy &Builder);
147
148  /// UpdateLineDirectiveRegion - Update region stack only if #line directive
149  /// has introduced scope change.
150  void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
151
152  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
153  /// of a new block.
154  void EmitRegionStart(CGBuilderTy &Builder);
155
156  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
157  /// block.
158  void EmitRegionEnd(CGBuilderTy &Builder);
159
160  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
161  /// variable declaration.
162  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
163                                 CGBuilderTy &Builder);
164
165  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
166  /// imported variable declaration in a block.
167  void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
168                                         llvm::Value *AI,
169                                         CGBuilderTy &Builder,
170                                         CodeGenFunction *CGF);
171
172  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
173  /// variable declaration.
174  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
175                                CGBuilderTy &Builder);
176
177  /// EmitGlobalVariable - Emit information about a global variable.
178  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
179
180  /// EmitGlobalVariable - Emit information about an objective-c interface.
181  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
182
183private:
184  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
185  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
186                   CGBuilderTy &Builder);
187
188  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
189  void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
190                   CGBuilderTy &Builder, CodeGenFunction *CGF);
191
192  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
193  // See BuildByRefType.
194  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
195                                            uint64_t *OffSet);
196
197  /// getContextDescriptor - Get context info for the decl.
198  llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
199                                          llvm::DIDescriptor &CU);
200
201  /// CreateCompileUnit - Create new compile unit.
202  void CreateCompileUnit();
203
204  /// getOrCreateFile - Get the file debug info descriptor for the input
205  /// location.
206  llvm::DIFile getOrCreateFile(SourceLocation Loc);
207
208  /// getOrCreateType - Get the type from the cache or create a new type if
209  /// necessary.
210  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
211
212  /// CreateTypeNode - Create type metadata for a source language type.
213  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
214
215  /// CreateMemberType - Create new member and increase Offset by FType's size.
216  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
217                                llvm::StringRef Name, uint64_t *Offset);
218
219  /// getFunctionName - Get function name for the given FunctionDecl. If the
220  /// name is constructred on demand (e.g. C++ destructor) then the name
221  /// is stored on the side.
222  llvm::StringRef getFunctionName(const FunctionDecl *FD);
223
224  /// getClassName - Get class name including template argument list.
225  llvm::StringRef getClassName(RecordDecl *RD);
226
227  /// getVTableName - Get vtable name for the given Class.
228  llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
229
230  /// getLineNumber - Get line number for the location. If location is invalid
231  /// then use current location.
232  unsigned getLineNumber(SourceLocation Loc);
233
234  /// getColumnNumber - Get column number for the location. If location is
235  /// invalid then use current location.
236  unsigned getColumnNumber(SourceLocation Loc);
237};
238} // namespace CodeGen
239} // namespace clang
240
241
242#endif
243