CGDebugInfo.h revision 202379
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  /// Helper functions for getOrCreateType.
68  llvm::DIType CreateType(const BuiltinType *Ty, llvm::DICompileUnit U);
69  llvm::DIType CreateType(const ComplexType *Ty, llvm::DICompileUnit U);
70  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DICompileUnit U);
71  llvm::DIType CreateType(const TypedefType *Ty, llvm::DICompileUnit U);
72  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
73                          llvm::DICompileUnit Unit);
74  llvm::DIType CreateType(const PointerType *Ty, llvm::DICompileUnit U);
75  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DICompileUnit U);
76  llvm::DIType CreateType(const FunctionType *Ty, llvm::DICompileUnit U);
77  llvm::DIType CreateType(const TagType *Ty, llvm::DICompileUnit U);
78  llvm::DIType CreateType(const RecordType *Ty, llvm::DICompileUnit U);
79  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DICompileUnit U);
80  llvm::DIType CreateType(const EnumType *Ty, llvm::DICompileUnit U);
81  llvm::DIType CreateType(const ArrayType *Ty, llvm::DICompileUnit U);
82  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DICompileUnit U);
83  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DICompileUnit U);
84
85  llvm::DIType CreatePointerLikeType(unsigned Tag,
86                                     const Type *Ty, QualType PointeeTy,
87                                     llvm::DICompileUnit U);
88public:
89  CGDebugInfo(CodeGenModule &CGM);
90  ~CGDebugInfo();
91
92  /// setLocation - Update the current source location. If \arg loc is
93  /// invalid it is ignored.
94  void setLocation(SourceLocation Loc);
95
96  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
97  /// source line.
98  void EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder);
99
100  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
101  /// start of a new function.
102  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
103                         llvm::Function *Fn, CGBuilderTy &Builder);
104
105  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
106  /// of a new block.
107  void EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder);
108
109  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
110  /// block.
111  void EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder);
112
113  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
114  /// variable declaration.
115  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
116                                 CGBuilderTy &Builder);
117
118  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
119  /// imported variable declaration in a block.
120  void EmitDeclareOfBlockDeclRefVariable(const BlockDeclRefExpr *BDRE,
121                                         llvm::Value *AI,
122                                         CGBuilderTy &Builder,
123                                         CodeGenFunction *CGF);
124
125  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
126  /// variable declaration.
127  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
128                                CGBuilderTy &Builder);
129
130  /// EmitGlobalVariable - Emit information about a global variable.
131  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
132
133  /// EmitGlobalVariable - Emit information about an objective-c interface.
134  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
135
136private:
137  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
138  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
139                   CGBuilderTy &Builder);
140
141  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
142  void EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, llvm::Value *AI,
143                   CGBuilderTy &Builder, CodeGenFunction *CGF);
144
145  /// getContext - Get context info for the decl.
146  llvm::DIDescriptor getContext(const VarDecl *Decl,llvm::DIDescriptor &CU);
147
148  /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
149  /// new one if necessary.
150  llvm::DICompileUnit getOrCreateCompileUnit(SourceLocation Loc);
151
152  /// getOrCreateType - Get the type from the cache or create a new type if
153  /// necessary.
154  llvm::DIType getOrCreateType(QualType Ty, llvm::DICompileUnit Unit);
155
156  /// CreateTypeNode - Create type metadata for a source language type.
157  llvm::DIType CreateTypeNode(QualType Ty, llvm::DICompileUnit Unit);
158
159  /// getFunctionName - Get function name for the given FunctionDecl. If the
160  /// name is constructred on demand (e.g. C++ destructor) then the name
161  /// is stored on the side.
162  llvm::StringRef getFunctionName(const FunctionDecl *FD);
163};
164} // namespace CodeGen
165} // namespace clang
166
167
168#endif
169