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/DebugInfo.h"
21#include "llvm/DIBuilder.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/Support/ValueHandle.h"
24#include "llvm/Support/Allocator.h"
25
26#include "CGBuilder.h"
27
28namespace llvm {
29  class MDNode;
30}
31
32namespace clang {
33  class CXXMethodDecl;
34  class VarDecl;
35  class ObjCInterfaceDecl;
36  class ClassTemplateSpecializationDecl;
37  class GlobalDecl;
38
39namespace CodeGen {
40  class CodeGenModule;
41  class CodeGenFunction;
42  class CGBlockInfo;
43
44/// CGDebugInfo - This class gathers all debug information during compilation
45/// and is responsible for emitting to llvm globals or pass directly to
46/// the backend.
47class CGDebugInfo {
48  CodeGenModule &CGM;
49  llvm::DIBuilder DBuilder;
50  llvm::DICompileUnit TheCU;
51  SourceLocation CurLoc, PrevLoc;
52  llvm::DIType VTablePtrType;
53  llvm::DIType ClassTy;
54  llvm::DIType ObjTy;
55  llvm::DIType SelTy;
56
57  /// TypeCache - Cache of previously constructed Types.
58  llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
59
60  /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
61  llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
62
63  /// ReplaceMap - Cache of forward declared types to RAUW at the end of
64  /// compilation.
65  std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
66
67  bool BlockLiteralGenericSet;
68  llvm::DIType BlockLiteralGeneric;
69
70  // LexicalBlockStack - Keep track of our current nested lexical block.
71  std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
72  llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
73  // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
74  // beginning of a function. This is used to pop unbalanced regions at
75  // the end of a function.
76  std::vector<unsigned> FnBeginRegionCount;
77
78  /// DebugInfoNames - This is a storage for names that are
79  /// constructed on demand. For example, C++ destructors, C++ operators etc..
80  llvm::BumpPtrAllocator DebugInfoNames;
81  StringRef CWDName;
82
83  llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
84  llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
85  llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
86
87  /// Helper functions for getOrCreateType.
88  llvm::DIType CreateType(const BuiltinType *Ty);
89  llvm::DIType CreateType(const ComplexType *Ty);
90  llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
91  llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
92  llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
93                          llvm::DIFile F);
94  llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
95  llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
96  llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
97  llvm::DIType CreateType(const RecordType *Ty);
98  llvm::DIType CreateLimitedType(const RecordType *Ty);
99  llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
100  llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
101  llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
102  llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
103  llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
104  llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
105  llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
106  llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
107  llvm::DIType CreateEnumType(const EnumDecl *ED);
108  llvm::DIType getTypeOrNull(const QualType);
109  llvm::DIType getCompletedTypeOrNull(const QualType);
110  llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
111                                     llvm::DIFile F);
112  llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
113                                       llvm::DIFile F);
114  llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
115  llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
116  llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
117  llvm::DIType CreatePointerLikeType(unsigned Tag,
118                                     const Type *Ty, QualType PointeeTy,
119                                     llvm::DIFile F);
120
121  llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
122                                             llvm::DIFile F,
123                                             llvm::DIType RecordTy);
124
125  void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
126                                 llvm::DIFile F,
127                                 SmallVectorImpl<llvm::Value *> &E,
128                                 llvm::DIType T);
129
130  void CollectCXXFriends(const CXXRecordDecl *Decl,
131                       llvm::DIFile F,
132                       SmallVectorImpl<llvm::Value *> &EltTys,
133                       llvm::DIType RecordTy);
134
135  void CollectCXXBases(const CXXRecordDecl *Decl,
136                       llvm::DIFile F,
137                       SmallVectorImpl<llvm::Value *> &EltTys,
138                       llvm::DIType RecordTy);
139
140  llvm::DIArray
141  CollectTemplateParams(const TemplateParameterList *TPList,
142                        const TemplateArgumentList &TAList,
143                        llvm::DIFile Unit);
144  llvm::DIArray
145  CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
146  llvm::DIArray
147  CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
148                           llvm::DIFile F);
149
150  llvm::DIType createFieldType(StringRef name, QualType type,
151                               uint64_t sizeInBitsOverride, SourceLocation loc,
152                               AccessSpecifier AS, uint64_t offsetInBits,
153                               llvm::DIFile tunit,
154                               llvm::DIDescriptor scope);
155  void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
156  void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
157                           SmallVectorImpl<llvm::Value *> &E,
158                           llvm::DIType RecordTy);
159
160  void CollectVTableInfo(const CXXRecordDecl *Decl,
161                         llvm::DIFile F,
162                         SmallVectorImpl<llvm::Value *> &EltTys);
163
164  // CreateLexicalBlock - Create a new lexical block node and push it on
165  // the stack.
166  void CreateLexicalBlock(SourceLocation Loc);
167
168public:
169  CGDebugInfo(CodeGenModule &CGM);
170  ~CGDebugInfo();
171
172  void finalize(void);
173
174  /// setLocation - Update the current source location. If \arg loc is
175  /// invalid it is ignored.
176  void setLocation(SourceLocation Loc);
177
178  /// EmitLocation - Emit metadata to indicate a change in line/column
179  /// information in the source file.
180  void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
181
182  /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
183  /// start of a new function.
184  void EmitFunctionStart(GlobalDecl GD, QualType FnType,
185                         llvm::Function *Fn, CGBuilderTy &Builder);
186
187  /// EmitFunctionEnd - Constructs the debug code for exiting a function.
188  void EmitFunctionEnd(CGBuilderTy &Builder);
189
190  /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
191  /// new lexical block and push the block onto the stack.
192  void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
193
194  /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
195  /// block and pop the current block.
196  void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
197
198  /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
199  /// variable declaration.
200  void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
201                                 CGBuilderTy &Builder);
202
203  /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
204  /// imported variable declaration in a block.
205  void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
206                                         llvm::Value *storage,
207                                         CGBuilderTy &Builder,
208                                         const CGBlockInfo &blockInfo);
209
210  /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
211  /// variable declaration.
212  void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
213                                unsigned ArgNo, CGBuilderTy &Builder);
214
215  /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
216  /// llvm.dbg.declare for the block-literal argument to a block
217  /// invocation function.
218  void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
219                                            llvm::Value *addr,
220                                            CGBuilderTy &Builder);
221
222  /// EmitGlobalVariable - Emit information about a global variable.
223  void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
224
225  /// EmitGlobalVariable - Emit information about an objective-c interface.
226  void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
227
228  /// EmitGlobalVariable - Emit global variable's debug info.
229  void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
230
231  /// getOrCreateRecordType - Emit record type's standalone debug info.
232  llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
233
234  /// getOrCreateInterfaceType - Emit an objective c interface type standalone
235  /// debug info.
236  llvm::DIType getOrCreateInterfaceType(QualType Ty,
237					SourceLocation Loc);
238
239private:
240  /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
241  void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
242                   unsigned ArgNo, CGBuilderTy &Builder);
243
244  // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
245  // See BuildByRefType.
246  llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
247                                            uint64_t *OffSet);
248
249  /// getContextDescriptor - Get context info for the decl.
250  llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
251
252  /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
253  /// context.
254  llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
255
256  /// createContextChain - Create a set of decls for the context chain.
257  llvm::DIDescriptor createContextChain(const Decl *Decl);
258
259  /// getCurrentDirname - Return current directory name.
260  StringRef getCurrentDirname();
261
262  /// CreateCompileUnit - Create new compile unit.
263  void CreateCompileUnit();
264
265  /// getOrCreateFile - Get the file debug info descriptor for the input
266  /// location.
267  llvm::DIFile getOrCreateFile(SourceLocation Loc);
268
269  /// getOrCreateMainFile - Get the file info for main compile unit.
270  llvm::DIFile getOrCreateMainFile();
271
272  /// getOrCreateType - Get the type from the cache or create a new type if
273  /// necessary.
274  llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
275
276  /// getOrCreateLimitedType - Get the type from the cache or create a new
277  /// partial type if necessary.
278  llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
279
280  /// CreateTypeNode - Create type metadata for a source language type.
281  llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
282
283  /// CreateLimitedTypeNode - Create type metadata for a source language
284  /// type, but only partial types for records.
285  llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
286
287  /// CreateMemberType - Create new member and increase Offset by FType's size.
288  llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
289                                StringRef Name, uint64_t *Offset);
290
291  /// getFunctionDeclaration - Return debug info descriptor to describe method
292  /// declaration for the given method definition.
293  llvm::DISubprogram getFunctionDeclaration(const Decl *D);
294
295  /// getFunctionName - Get function name for the given FunctionDecl. If the
296  /// name is constructred on demand (e.g. C++ destructor) then the name
297  /// is stored on the side.
298  StringRef getFunctionName(const FunctionDecl *FD);
299
300  /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
301  /// This is the display name for the debugging info.
302  StringRef getObjCMethodName(const ObjCMethodDecl *FD);
303
304  /// getSelectorName - Return selector name. This is used for debugging
305  /// info.
306  StringRef getSelectorName(Selector S);
307
308  /// getClassName - Get class name including template argument list.
309  StringRef getClassName(const RecordDecl *RD);
310
311  /// getVTableName - Get vtable name for the given Class.
312  StringRef getVTableName(const CXXRecordDecl *Decl);
313
314  /// getLineNumber - Get line number for the location. If location is invalid
315  /// then use current location.
316  unsigned getLineNumber(SourceLocation Loc);
317
318  /// getColumnNumber - Get column number for the location. If location is
319  /// invalid then use current location.
320  unsigned getColumnNumber(SourceLocation Loc);
321};
322} // namespace CodeGen
323} // namespace clang
324
325
326#endif
327