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