CGDebugInfo.h revision 219077
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
36namespace CodeGen {
37  class CodeGenModule;
38  class CodeGenFunction;
39  class GlobalDecl;
40  class CGBlockInfo;
41
42/// CGDebugInfo - This class gathers all debug information during compilation
43/// and is responsible for emitting to llvm globals or pass directly to
44/// the backend.
45class CGDebugInfo {
46  CodeGenModule &CGM;
47  llvm::DIBuilder DBuilder;
48  llvm::DICompileUnit TheCU;
49  SourceLocation CurLoc, PrevLoc;
50  llvm::DIType VTablePtrType;
51
52  /// TypeCache - Cache of previously constructed Types.
53  llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
54
55  bool BlockLiteralGenericSet;
56  llvm::DIType BlockLiteralGeneric;
57
58  std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
59  llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
60  // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
61  // of a function. This is used to pop unbalanced regions at the end of a
62  // function.
63  std::vector<unsigned> FnBeginRegionCount;
64
65  /// LineDirectiveFiles - This stack is used to keep track of
66  /// scopes introduced by #line directives.
67  std::vector<const char *> LineDirectiveFiles;
68
69  /// DebugInfoNames - This is a storage for names that are
70  /// constructed on demand. For example, C++ destructors, C++ operators etc..
71  llvm::BumpPtrAllocator DebugInfoNames;
72  llvm::StringRef CWDName;
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);
80  llvm::DIType CreateType(const ComplexType *Ty);
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);
89  llvm::DIType CreateType(const RecordType *Ty);
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 VectorType *Ty, llvm::DIFile F);
93  llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
94  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
95  llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
96  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
97  llvm::DIType CreateEnumType(const EnumDecl *ED);
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::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
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::DIType RecordTy);
110
111  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
112                                 llvm::DIFile F,
113                                 llvm::SmallVectorImpl<llvm::Value *> &E,
114                                 llvm::DIType T);
115
116  void CollectCXXFriends(const CXXRecordDecl *Decl,
117                       llvm::DIFile F,
118                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
119                       llvm::DIType RecordTy);
120
121  void CollectCXXBases(const CXXRecordDecl *Decl,
122                       llvm::DIFile F,
123                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
124                       llvm::DIType RecordTy);
125
126  llvm::DIType createFieldType(llvm::StringRef name, QualType type,
127                               Expr *bitWidth, SourceLocation loc,
128                               AccessSpecifier AS, uint64_t offsetInBits,
129                               llvm::DIFile tunit);
130  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
131                           llvm::SmallVectorImpl<llvm::Value *> &E);
132
133  void CollectVTableInfo(const CXXRecordDecl *Decl,
134                         llvm::DIFile F,
135                         llvm::SmallVectorImpl<llvm::Value *> &EltTys);
136
137public:
138  CGDebugInfo(CodeGenModule &CGM);
139  ~CGDebugInfo();
140
141  /// setLocation - Update the current source location. If \arg loc is
142  /// invalid it is ignored.
143  void setLocation(SourceLocation Loc);
144
145  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
146  /// source line.
147  void EmitStopPoint(CGBuilderTy &Builder);
148
149  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
150  /// start of a new function.
151  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
152                         llvm::Function *Fn, CGBuilderTy &Builder);
153
154  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
155  void EmitFunctionEnd(CGBuilderTy &Builder);
156
157  /// UpdateLineDirectiveRegion - Update region stack only if #line directive
158  /// has introduced scope change.
159  void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
160
161  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
162  /// of a new block.
163  void EmitRegionStart(CGBuilderTy &Builder);
164
165  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
166  /// block.
167  void EmitRegionEnd(CGBuilderTy &Builder);
168
169  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
170  /// variable declaration.
171  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
172                                 CGBuilderTy &Builder);
173
174  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
175  /// imported variable declaration in a block.
176  void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
177                                         llvm::Value *storage,
178                                         CGBuilderTy &Builder,
179                                         const CGBlockInfo &blockInfo);
180
181  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
182  /// variable declaration.
183  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
184                                CGBuilderTy &Builder);
185
186  /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
187  /// llvm.dbg.declare for the block-literal argument to a block
188  /// invocation function.
189  void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
190                                            llvm::Value *addr,
191                                            CGBuilderTy &Builder);
192
193  /// EmitGlobalVariable - Emit information about a global variable.
194  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
195
196  /// EmitGlobalVariable - Emit information about an objective-c interface.
197  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
198
199  /// EmitGlobalVariable - Emit global variable's debug info.
200  void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
201
202  /// getOrCreateRecordType - Emit record type's standalone debug info.
203  llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
204private:
205  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
206  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
207                   CGBuilderTy &Builder);
208
209  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable
210  /// declaration from an enclosing block.
211  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
212                   CGBuilderTy &Builder, const CGBlockInfo &blockInfo);
213
214  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
215  // See BuildByRefType.
216  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
217                                            uint64_t *OffSet);
218
219  /// getContextDescriptor - Get context info for the decl.
220  llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
221
222  /// getCurrentDirname - Return current directory name.
223  llvm::StringRef getCurrentDirname();
224
225  /// CreateCompileUnit - Create new compile unit.
226  void CreateCompileUnit();
227
228  /// getOrCreateFile - Get the file debug info descriptor for the input
229  /// location.
230  llvm::DIFile getOrCreateFile(SourceLocation Loc);
231
232  /// getOrCreateMainFile - Get the file info for main compile unit.
233  llvm::DIFile getOrCreateMainFile();
234
235  /// getOrCreateType - Get the type from the cache or create a new type if
236  /// necessary.
237  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
238
239  /// CreateTypeNode - Create type metadata for a source language type.
240  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
241
242  /// CreateMemberType - Create new member and increase Offset by FType's size.
243  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
244                                llvm::StringRef Name, uint64_t *Offset);
245
246  /// getFunctionName - Get function name for the given FunctionDecl. If the
247  /// name is constructred on demand (e.g. C++ destructor) then the name
248  /// is stored on the side.
249  llvm::StringRef getFunctionName(const FunctionDecl *FD);
250
251  /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
252  /// This is the display name for the debugging info.
253  llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
254
255  /// getClassName - Get class name including template argument list.
256  llvm::StringRef getClassName(RecordDecl *RD);
257
258  /// getVTableName - Get vtable name for the given Class.
259  llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
260
261  /// getLineNumber - Get line number for the location. If location is invalid
262  /// then use current location.
263  unsigned getLineNumber(SourceLocation Loc);
264
265  /// getColumnNumber - Get column number for the location. If location is
266  /// invalid then use current location.
267  unsigned getColumnNumber(SourceLocation Loc);
268};
269} // namespace CodeGen
270} // namespace clang
271
272
273#endif
274