CGDebugInfo.h revision 221345
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 getOrCreateVTablePtrType(llvm::DIFile F);
102  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
103  llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
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::DIType RecordTy);
111
112  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
113                                 llvm::DIFile F,
114                                 llvm::SmallVectorImpl<llvm::Value *> &E,
115                                 llvm::DIType T);
116
117  void CollectCXXFriends(const CXXRecordDecl *Decl,
118                       llvm::DIFile F,
119                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
120                       llvm::DIType RecordTy);
121
122  void CollectCXXBases(const CXXRecordDecl *Decl,
123                       llvm::DIFile F,
124                       llvm::SmallVectorImpl<llvm::Value *> &EltTys,
125                       llvm::DIType RecordTy);
126
127  llvm::DIArray
128  CollectTemplateParams(const TemplateParameterList *TPList,
129                        const TemplateArgumentList &TAList,
130                        llvm::DIFile Unit);
131  llvm::DIArray
132  CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
133  llvm::DIArray
134  CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
135                           llvm::DIFile F);
136
137  llvm::DIType createFieldType(llvm::StringRef name, QualType type,
138                               Expr *bitWidth, SourceLocation loc,
139                               AccessSpecifier AS, uint64_t offsetInBits,
140                               llvm::DIFile tunit);
141  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
142                           llvm::SmallVectorImpl<llvm::Value *> &E);
143
144  void CollectVTableInfo(const CXXRecordDecl *Decl,
145                         llvm::DIFile F,
146                         llvm::SmallVectorImpl<llvm::Value *> &EltTys);
147
148public:
149  CGDebugInfo(CodeGenModule &CGM);
150  ~CGDebugInfo();
151
152  /// setLocation - Update the current source location. If \arg loc is
153  /// invalid it is ignored.
154  void setLocation(SourceLocation Loc);
155
156  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
157  /// source line.
158  void EmitStopPoint(CGBuilderTy &Builder);
159
160  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
161  /// start of a new function.
162  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
163                         llvm::Function *Fn, CGBuilderTy &Builder);
164
165  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
166  void EmitFunctionEnd(CGBuilderTy &Builder);
167
168  /// UpdateLineDirectiveRegion - Update region stack only if #line directive
169  /// has introduced scope change.
170  void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
171
172  /// UpdateCompletedType - Update type cache because the type is now
173  /// translated.
174  void UpdateCompletedType(const TagDecl *TD);
175
176  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
177  /// of a new block.
178  void EmitRegionStart(CGBuilderTy &Builder);
179
180  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
181  /// block.
182  void EmitRegionEnd(CGBuilderTy &Builder);
183
184  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
185  /// variable declaration.
186  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
187                                 CGBuilderTy &Builder);
188
189  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
190  /// imported variable declaration in a block.
191  void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
192                                         llvm::Value *storage,
193                                         CGBuilderTy &Builder,
194                                         const CGBlockInfo &blockInfo);
195
196  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
197  /// variable declaration.
198  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
199                                unsigned ArgNo, CGBuilderTy &Builder);
200
201  /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
202  /// llvm.dbg.declare for the block-literal argument to a block
203  /// invocation function.
204  void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
205                                            llvm::Value *addr,
206                                            CGBuilderTy &Builder);
207
208  /// EmitGlobalVariable - Emit information about a global variable.
209  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
210
211  /// EmitGlobalVariable - Emit information about an objective-c interface.
212  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
213
214  /// EmitGlobalVariable - Emit global variable's debug info.
215  void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
216
217  /// getOrCreateRecordType - Emit record type's standalone debug info.
218  llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
219private:
220  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
221  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
222                   unsigned ArgNo, CGBuilderTy &Builder);
223
224  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
225  // See BuildByRefType.
226  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
227                                            uint64_t *OffSet);
228
229  /// getContextDescriptor - Get context info for the decl.
230  llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
231
232  /// getCurrentDirname - Return current directory name.
233  llvm::StringRef getCurrentDirname();
234
235  /// CreateCompileUnit - Create new compile unit.
236  void CreateCompileUnit();
237
238  /// getOrCreateFile - Get the file debug info descriptor for the input
239  /// location.
240  llvm::DIFile getOrCreateFile(SourceLocation Loc);
241
242  /// getOrCreateMainFile - Get the file info for main compile unit.
243  llvm::DIFile getOrCreateMainFile();
244
245  /// getOrCreateType - Get the type from the cache or create a new type if
246  /// necessary.
247  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
248
249  /// CreateTypeNode - Create type metadata for a source language type.
250  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
251
252  /// CreateMemberType - Create new member and increase Offset by FType's size.
253  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
254                                llvm::StringRef Name, uint64_t *Offset);
255
256  /// getFunctionDeclaration - Return debug info descriptor to describe method
257  /// declaration for the given method definition.
258  llvm::DISubprogram getFunctionDeclaration(const Decl *D);
259
260  /// getFunctionName - Get function name for the given FunctionDecl. If the
261  /// name is constructred on demand (e.g. C++ destructor) then the name
262  /// is stored on the side.
263  llvm::StringRef getFunctionName(const FunctionDecl *FD);
264
265  /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
266  /// This is the display name for the debugging info.
267  llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
268
269  /// getSelectorName - Return selector name. This is used for debugging
270  /// info.
271  llvm::StringRef getSelectorName(Selector S);
272
273  /// getClassName - Get class name including template argument list.
274  llvm::StringRef getClassName(RecordDecl *RD);
275
276  /// getVTableName - Get vtable name for the given Class.
277  llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
278
279  /// getLineNumber - Get line number for the location. If location is invalid
280  /// then use current location.
281  unsigned getLineNumber(SourceLocation Loc);
282
283  /// getColumnNumber - Get column number for the location. If location is
284  /// invalid then use current location.
285  unsigned getColumnNumber(SourceLocation Loc);
286};
287} // namespace CodeGen
288} // namespace clang
289
290
291#endif
292