CGDebugInfo.h revision 2284ac9ec80299fcdefae9a2787cf85105a0f203
1//===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -----------------------===//
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/Basic/SourceLocation.h"
19#include "llvm/Support/IRBuilder.h"
20#include <map>
21#include <vector>
22
23
24namespace llvm {
25  class Function;
26  class DISerializer;
27  class CompileUnitDesc;
28  class BasicBlock;
29  class AnchorDesc;
30  class DebugInfoDesc;
31  class Value;
32  class TypeDesc;
33  class VariableDesc;
34  class SubprogramDesc;
35  class GlobalVariable;
36  class GlobalVariableDesc;
37  class EnumeratorDesc;
38  class SubrangeDesc;
39}
40
41namespace clang {
42  class FunctionDecl;
43  class VarDecl;
44namespace CodeGen {
45  class CodeGenModule;
46
47/// CGDebugInfo - This class gathers all debug information during compilation
48/// and is responsible for emitting to llvm globals or pass directly to
49/// the backend.
50class CGDebugInfo {
51private:
52  CodeGenModule *M;
53  llvm::DISerializer *SR;
54  SourceLocation CurLoc;
55  SourceLocation PrevLoc;
56
57  typedef llvm::IRBuilder<> BuilderType;
58
59  /// CompileUnitCache - Cache of previously constructed CompileUnits.
60  std::map<unsigned, llvm::CompileUnitDesc *> CompileUnitCache;
61
62  /// TypeCache - Cache of previously constructed Types.
63  std::map<void *, llvm::TypeDesc *> TypeCache;
64
65  llvm::Function *StopPointFn;
66  llvm::Function *FuncStartFn;
67  llvm::Function *DeclareFn;
68  llvm::Function *RegionStartFn;
69  llvm::Function *RegionEndFn;
70  llvm::AnchorDesc *CompileUnitAnchor;
71  llvm::AnchorDesc *SubprogramAnchor;
72  llvm::AnchorDesc *GlobalVariableAnchor;
73  std::vector<llvm::DebugInfoDesc *> RegionStack;
74  std::vector<llvm::VariableDesc *> VariableDescList;
75  std::vector<llvm::GlobalVariableDesc *> GlobalVarDescList;
76  std::vector<llvm::EnumeratorDesc *> EnumDescList;
77  std::vector<llvm::SubrangeDesc *> SubrangeDescList;
78  llvm::SubprogramDesc *Subprogram;
79
80  /// Helper functions for getOrCreateType.
81  llvm::TypeDesc *getOrCreateCVRType(QualType type,
82                                     llvm::CompileUnitDesc *unit);
83  llvm::TypeDesc *getOrCreateBuiltinType(QualType type,
84                                     llvm::CompileUnitDesc *unit);
85  llvm::TypeDesc *getOrCreateTypedefType(QualType type,
86                                     llvm::CompileUnitDesc *unit);
87  llvm::TypeDesc *getOrCreatePointerType(QualType type,
88                                     llvm::CompileUnitDesc *unit);
89  llvm::TypeDesc *getOrCreateFunctionType(QualType type,
90                                     llvm::CompileUnitDesc *unit);
91  llvm::TypeDesc *getOrCreateRecordType(QualType type,
92                                     llvm::CompileUnitDesc *unit);
93  llvm::TypeDesc *getOrCreateEnumType(QualType type,
94                                     llvm::CompileUnitDesc *unit);
95  llvm::TypeDesc *getOrCreateTaggedType(QualType type,
96                                     llvm::CompileUnitDesc *unit);
97  llvm::TypeDesc *getOrCreateArrayType(QualType type,
98                                     llvm::CompileUnitDesc *unit);
99
100public:
101  CGDebugInfo(CodeGenModule *m);
102  ~CGDebugInfo();
103
104  /// setLocation - Update the current source location. If \arg loc is
105  /// invalid it is ignored.
106  void setLocation(SourceLocation loc);
107
108  /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
109  /// source line.
110  void EmitStopPoint(llvm::Function *Fn, BuilderType &Builder);
111
112  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
113  /// start of a new function.
114  void EmitFunctionStart(const char *Name, QualType ReturnType,
115                         llvm::Function *Fn, BuilderType &Builder);
116
117  /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
118  /// of a new block.
119  void EmitRegionStart(llvm::Function *Fn, BuilderType &Builder);
120
121  /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
122  /// block.
123  void EmitRegionEnd(llvm::Function *Fn, BuilderType &Builder);
124
125  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
126  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
127                   BuilderType &Builder);
128
129  /// EmitGlobalVariable - Emit information about a global variable.
130  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *decl);
131
132  /// getOrCreateCompileUnit - Get the compile unit from the cache or create a
133  /// new one if necessary.
134  llvm::CompileUnitDesc *getOrCreateCompileUnit(SourceLocation loc);
135
136  /// getOrCreateType - Get the type from the cache or create a new type if
137  /// necessary.
138  llvm::TypeDesc *getOrCreateType(QualType type, llvm::CompileUnitDesc *unit);
139
140  /// getCastValueFor - Return a llvm representation for a given debug
141  /// information descriptor cast to an empty struct pointer.
142  llvm::Value *getCastValueFor(llvm::DebugInfoDesc *DD);
143
144  /// getValueFor - Return a llvm representation for a given debug information
145  /// descriptor.
146  llvm::Value *getValueFor(llvm::DebugInfoDesc *DD);
147};
148} // namespace CodeGen
149} // namespace clang
150
151#endif
152