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