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   // LexicalBlockStack - Keep track of our current nested lexical block.
60   std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
61   llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
62   // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
63   // beginning of a function. This is used to pop unbalanced regions at
64   // the end of a function.
65   std::vector<unsigned> FnBeginRegionCount;
66 
67   /// DebugInfoNames - This is a storage for names that are
68   /// constructed on demand. For example, C++ destructors, C++ operators etc..
69   llvm::BumpPtrAllocator DebugInfoNames;
70   StringRef CWDName;
71 
72   llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
73   llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
74   llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
75 
76   /// Helper functions for getOrCreateType.
77   llvm::DIType CreateType(const BuiltinType *Ty);
78   llvm::DIType CreateType(const ComplexType *Ty);
79   llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
80   llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
81   llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
82                           llvm::DIFile F);
83   llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
84   llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
85   llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
86   llvm::DIType CreateType(const RecordType *Ty);
87   llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
88   llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
89   llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
90   llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
91   llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
92   llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
93   llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
94   llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
95   llvm::DIType CreateEnumType(const EnumDecl *ED);
96   llvm::DIType getTypeOrNull(const QualType);
97   llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
98                                      llvm::DIFile F);
99   llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
100                                        llvm::DIFile F);
101   llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
102   llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
103   llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
104   llvm::DIType CreatePointerLikeType(unsigned Tag,
105                                      const Type *Ty, QualType PointeeTy,
106                                      llvm::DIFile F);
107 
108   llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
109                                              llvm::DIFile F,
110                                              llvm::DIType RecordTy);
111 
112   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
113                                  llvm::DIFile F,
114                                  SmallVectorImpl<llvm::Value *> &E,
115                                  llvm::DIType T);
116 
117   void CollectCXXFriends(const CXXRecordDecl *Decl,
118                        llvm::DIFile F,
119                        SmallVectorImpl<llvm::Value *> &EltTys,
120                        llvm::DIType RecordTy);
121 
122   void CollectCXXBases(const CXXRecordDecl *Decl,
123                        llvm::DIFile F,
124                        SmallVectorImpl<llvm::Value *> &EltTys,
125                        llvm::DIType RecordTy);
126 
127   llvm::DIArray
128   CollectTemplateParams(const TemplateParameterList *TPList,
129                         const TemplateArgumentList &TAList,
130                         llvm::DIFile Unit);
131   llvm::DIArray
132   CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
133   llvm::DIArray
134   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
135                            llvm::DIFile F);
136 
137   llvm::DIType createFieldType(StringRef name, QualType type,
138                                uint64_t sizeInBitsOverride, SourceLocation loc,
139                                AccessSpecifier AS, uint64_t offsetInBits,
140                                llvm::DIFile tunit,
141                                llvm::DIDescriptor scope);
142   void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
143   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
144                            SmallVectorImpl<llvm::Value *> &E,
145                            llvm::DIType RecordTy);
146 
147   void CollectVTableInfo(const CXXRecordDecl *Decl,
148                          llvm::DIFile F,
149                          SmallVectorImpl<llvm::Value *> &EltTys);
150 
151   // CreateLexicalBlock - Create a new lexical block node and push it on
152   // the stack.
153   void CreateLexicalBlock(SourceLocation Loc);
154 
155 public:
156   CGDebugInfo(CodeGenModule &CGM);
157   ~CGDebugInfo();
158   void finalize() { DBuilder.finalize(); }
159 
160   /// setLocation - Update the current source location. If \arg loc is
161   /// invalid it is ignored.
162   void setLocation(SourceLocation Loc);
163 
164   /// EmitLocation - Emit metadata to indicate a change in line/column
165   /// information in the source file.
166   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
167 
168   /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
169   /// start of a new function.
170   void EmitFunctionStart(GlobalDecl GD, QualType FnType,
171                          llvm::Function *Fn, CGBuilderTy &Builder);
172 
173   /// EmitFunctionEnd - Constructs the debug code for exiting a function.
174   void EmitFunctionEnd(CGBuilderTy &Builder);
175 
176   /// UpdateCompletedType - Update type cache because the type is now
177   /// translated.
178   void UpdateCompletedType(const TagDecl *TD);
179 
180   /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
181   /// new lexical block and push the block onto the stack.
182   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
183 
184   /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
185   /// block and pop the current block.
186   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
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   /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
237   /// context.
238   llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
239 
240   /// createContextChain - Create a set of decls for the context chain.
241   llvm::DIDescriptor createContextChain(const Decl *Decl);
242 
243   /// getCurrentDirname - Return current directory name.
244   StringRef getCurrentDirname();
245 
246   /// CreateCompileUnit - Create new compile unit.
247   void CreateCompileUnit();
248 
249   /// getOrCreateFile - Get the file debug info descriptor for the input
250   /// location.
251   llvm::DIFile getOrCreateFile(SourceLocation Loc);
252 
253   /// getOrCreateMainFile - Get the file info for main compile unit.
254   llvm::DIFile getOrCreateMainFile();
255 
256   /// getOrCreateType - Get the type from the cache or create a new type if
257   /// necessary.
258   llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
259 
260   /// CreateTypeNode - Create type metadata for a source language type.
261   llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
262 
263   /// CreateMemberType - Create new member and increase Offset by FType's size.
264   llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
265                                 StringRef Name, uint64_t *Offset);
266 
267   /// getFunctionDeclaration - Return debug info descriptor to describe method
268   /// declaration for the given method definition.
269   llvm::DISubprogram getFunctionDeclaration(const Decl *D);
270 
271   /// getFunctionName - Get function name for the given FunctionDecl. If the
272   /// name is constructred on demand (e.g. C++ destructor) then the name
273   /// is stored on the side.
274   StringRef getFunctionName(const FunctionDecl *FD);
275 
276   /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
277   /// This is the display name for the debugging info.
278   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
279 
280   /// getSelectorName - Return selector name. This is used for debugging
281   /// info.
282   StringRef getSelectorName(Selector S);
283 
284   /// getClassName - Get class name including template argument list.
285   StringRef getClassName(RecordDecl *RD);
286 
287   /// getVTableName - Get vtable name for the given Class.
288   StringRef getVTableName(const CXXRecordDecl *Decl);
289 
290   /// getLineNumber - Get line number for the location. If location is invalid
291   /// then use current location.
292   unsigned getLineNumber(SourceLocation Loc);
293 
294   /// getColumnNumber - Get column number for the location. If location is
295   /// invalid then use current location.
296   unsigned getColumnNumber(SourceLocation Loc);
297 };
298 } // namespace CodeGen
299 } // namespace clang
300 
301 
302 #endif
303