CGDebugInfo.h revision 204643
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#include <map>
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
41/// CGDebugInfo - This class gathers all debug information during compilation
42/// and is responsible for emitting to llvm globals or pass directly to
43/// the backend.
44class CGDebugInfo {
45  CodeGenModule &CGM;
46  bool isMainCompileUnitCreated;
47  llvm::DIFactory DebugFactory;
48
49  SourceLocation CurLoc, PrevLoc;
50
51  llvm::DIType VTablePtrType;
52
53  /// CompileUnitCache - Cache of previously constructed CompileUnits.
54  llvm::DenseMap<unsigned, llvm::DICompileUnit> CompileUnitCache;
55
56  /// TypeCache - Cache of previously constructed Types.
57  // FIXME: Eliminate this map.  Be careful of iterator invalidation.
58  std::map<void *, llvm::WeakVH> TypeCache;
59
60  bool BlockLiteralGenericSet;
61  llvm::DIType BlockLiteralGeneric;
62
63  std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
64  llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
65
66  /// DebugInfoNames - This is a storage for names that are
67  /// constructed on demand. For example, C++ destructors, C++ operators etc..
68  llvm::BumpPtrAllocator DebugInfoNames;
69
70  llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
71  llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
72
73  /// Helper functions for getOrCreateType.
74  llvm::DIType CreateType(const BuiltinType *Ty, llvm::DICompileUnit U);
75  llvm::DIType CreateType(const ComplexType *Ty, llvm::DICompileUnit U);
76  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DICompileUnit U);
77  llvm::DIType CreateType(const TypedefType *Ty, llvm::DICompileUnit U);
78  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
79                          llvm::DICompileUnit Unit);
80  llvm::DIType CreateType(const PointerType *Ty, llvm::DICompileUnit U);
81  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DICompileUnit U);
82  llvm::DIType CreateType(const FunctionType *Ty, llvm::DICompileUnit U);
83  llvm::DIType CreateType(const TagType *Ty, llvm::DICompileUnit U);
84  llvm::DIType CreateType(const RecordType *Ty, llvm::DICompileUnit U);
85  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DICompileUnit U);
86  llvm::DIType CreateType(const EnumType *Ty, llvm::DICompileUnit U);
87  llvm::DIType CreateType(const VectorType *Ty, llvm::DICompileUnit Unit);
88  llvm::DIType CreateType(const ArrayType *Ty, llvm::DICompileUnit U);
89  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DICompileUnit U);
90  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DICompileUnit U);
91  llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
92                                     llvm::DICompileUnit Unit);
93  llvm::DIType getOrCreateVTablePtrType(llvm::DICompileUnit Unit);
94  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N,
95                                         llvm::DIDescriptor Unit);
96
97  llvm::DIType CreatePointerLikeType(unsigned Tag,
98                                     const Type *Ty, QualType PointeeTy,
99                                     llvm::DICompileUnit U);
100
101  llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
102                                             llvm::DICompileUnit Unit,
103                                             llvm::DICompositeType &RecordTy);
104
105  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
106                                 llvm::DICompileUnit U,
107                                 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
108                                 llvm::DICompositeType &T);
109  void CollectCXXBases(const CXXRecordDecl *Decl,
110                       llvm::DICompileUnit Unit,
111                       llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
112                       llvm::DICompositeType &RecordTy);
113
114
115  void CollectRecordFields(const RecordDecl *Decl, llvm::DICompileUnit U,
116                           llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
117
118  void CollectVtableInfo(const CXXRecordDecl *Decl,
119                         llvm::DICompileUnit Unit,
120                         llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys);
121
122public:
123  CGDebugInfo(CodeGenModule &CGM);
124  ~CGDebugInfo();
125
126  /// setLocation - Update the current source location. If \arg loc is
127  /// invalid it is ignored.
128  void setLocation(SourceLocation Loc);
129
130  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
131  /// source line.
132  void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
133
134  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
135  /// start of a new function.
136  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
137                         llvm::Function *Fn, CGBuilderTy &Builder);
138
139  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
140  /// of a new block.
141  void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
142
143  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
144  /// block.
145  void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
146
147  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
148  /// variable declaration.
149  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
150                                 CGBuilderTy &Builder);
151
152  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
153  /// imported variable declaration in a block.
154  void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
155                                         llvm::Value *AI,
156                                         CGBuilderTy &Builder,
157                                         CodeGenFunction *CGF);
158
159  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
160  /// variable declaration.
161  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
162                                CGBuilderTy &Builder);
163
164  /// EmitGlobalVariable - Emit information about a global variable.
165  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
166
167  /// EmitGlobalVariable - Emit information about an objective-c interface.
168  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
169
170private:
171  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
172  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
173                   CGBuilderTy &Builder);
174
175  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
176  void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
177                   CGBuilderTy &Builder, CodeGenFunction *CGF);
178
179  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
180  // See BuildByRefType.
181  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
182                                            uint64_t *OffSet);
183
184  /// getContextDescriptor - Get context info for the decl.
185  llvm::DIDescriptor getContextDescriptor(const Decl *Decl,
186                                          llvm::DIDescriptor &CU);
187
188  /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
189  /// new one if necessary.
190  llvm::DICompileUnit getOrCreateCompileUnit(SourceLocation Loc);
191
192  /// getOrCreateType - Get the type from the cache or create a new type if
193  /// necessary.
194  llvm::DIType getOrCreateType(QualType Ty, llvm::DICompileUnit Unit);
195
196  /// CreateTypeNode - Create type metadata for a source language type.
197  llvm::DIType CreateTypeNode(QualType Ty, llvm::DICompileUnit Unit);
198
199  /// getFunctionName - Get function name for the given FunctionDecl. If the
200  /// name is constructred on demand (e.g. C++ destructor) then the name
201  /// is stored on the side.
202  llvm::StringRef getFunctionName(const FunctionDecl *FD);
203
204  /// getVtableName - Get vtable name for the given Class.
205  llvm::StringRef getVtableName(const CXXRecordDecl *Decl);
206
207};
208} // namespace CodeGen
209} // namespace clang
210
211
212#endif
213