CGDebugInfo.h revision 202879
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  /// CompileUnitCache - Cache of previously constructed CompileUnits.
52  llvm::DenseMap<unsigned, llvm::DICompileUnit> CompileUnitCache;
53
54  /// TypeCache - Cache of previously constructed Types.
55  // FIXME: Eliminate this map.  Be careful of iterator invalidation.
56  std::map<void *, llvm::WeakVH> TypeCache;
57
58  bool BlockLiteralGenericSet;
59  llvm::DIType BlockLiteralGeneric;
60
61  std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
62
63  /// FunctionNames - This is a storage for function names that are
64  /// constructed on demand. For example, C++ destructors, C++ operators etc..
65  llvm::BumpPtrAllocator FunctionNames;
66
67  llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
68
69  /// Helper functions for getOrCreateType.
70  llvm::DIType CreateType(const BuiltinType *Ty, llvm::DICompileUnit U);
71  llvm::DIType CreateType(const ComplexType *Ty, llvm::DICompileUnit U);
72  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DICompileUnit U);
73  llvm::DIType CreateType(const TypedefType *Ty, llvm::DICompileUnit U);
74  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
75                          llvm::DICompileUnit Unit);
76  llvm::DIType CreateType(const PointerType *Ty, llvm::DICompileUnit U);
77  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DICompileUnit U);
78  llvm::DIType CreateType(const FunctionType *Ty, llvm::DICompileUnit U);
79  llvm::DIType CreateType(const TagType *Ty, llvm::DICompileUnit U);
80  llvm::DIType CreateType(const RecordType *Ty, llvm::DICompileUnit U);
81  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DICompileUnit U);
82  llvm::DIType CreateType(const EnumType *Ty, llvm::DICompileUnit U);
83  llvm::DIType CreateType(const ArrayType *Ty, llvm::DICompileUnit U);
84  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DICompileUnit U);
85  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DICompileUnit U);
86
87  llvm::DIType CreatePointerLikeType(unsigned Tag,
88                                     const Type *Ty, QualType PointeeTy,
89                                     llvm::DICompileUnit U);
90  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
91                                 llvm::DICompileUnit U,
92                                 llvm::SmallVectorImpl<llvm::DIDescriptor> &E,
93                                 llvm::DICompositeType &T);
94  void CollectRecordFields(const RecordDecl *Decl, llvm::DICompileUnit U,
95                           llvm::SmallVectorImpl<llvm::DIDescriptor> &E);
96public:
97  CGDebugInfo(CodeGenModule &CGM);
98  ~CGDebugInfo();
99
100  /// setLocation - Update the current source location. If \arg loc is
101  /// invalid it is ignored.
102  void setLocation(SourceLocation Loc);
103
104  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
105  /// source line.
106  void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
107
108  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
109  /// start of a new function.
110  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
111                         llvm::Function *Fn, CGBuilderTy &Builder);
112
113  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
114  /// of a new block.
115  void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
116
117  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
118  /// block.
119  void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
120
121  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
122  /// variable declaration.
123  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
124                                 CGBuilderTy &Builder);
125
126  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
127  /// imported variable declaration in a block.
128  void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
129                                         llvm::Value *AI,
130                                         CGBuilderTy &Builder,
131                                         CodeGenFunction *CGF);
132
133  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
134  /// variable declaration.
135  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
136                                CGBuilderTy &Builder);
137
138  /// EmitGlobalVariable - Emit information about a global variable.
139  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
140
141  /// EmitGlobalVariable - Emit information about an objective-c interface.
142  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
143
144private:
145  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
146  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
147                   CGBuilderTy &Builder);
148
149  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
150  void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
151                   CGBuilderTy &Builder, CodeGenFunction *CGF);
152
153  /// getContext - Get context info for the decl.
154  llvm::DIDescriptor getContext(const VarDecl *Decl,llvm::DIDescriptor &CU);
155
156  /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
157  /// new one if necessary.
158  llvm::DICompileUnit getOrCreateCompileUnit(SourceLocation Loc);
159
160  /// getOrCreateType - Get the type from the cache or create a new type if
161  /// necessary.
162  llvm::DIType getOrCreateType(QualType Ty, llvm::DICompileUnit Unit);
163
164  /// CreateTypeNode - Create type metadata for a source language type.
165  llvm::DIType CreateTypeNode(QualType Ty, llvm::DICompileUnit Unit);
166
167  /// getFunctionName - Get function name for the given FunctionDecl. If the
168  /// name is constructred on demand (e.g. C++ destructor) then the name
169  /// is stored on the side.
170  llvm::StringRef getFunctionName(const FunctionDecl *FD);
171};
172} // namespace CodeGen
173} // namespace clang
174
175
176#endif
177