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