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