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