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