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