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