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