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