1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the source level debug info generator for llvm translation.
12 //===----------------------------------------------------------------------===//
14 #ifndef CLANG_CODEGEN_CGDEBUGINFO_H
15 #define CLANG_CODEGEN_CGDEBUGINFO_H
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"
26 #include "CGBuilder.h"
34 class ObjCInterfaceDecl;
35 class ClassTemplateSpecializationDecl;
40 class CodeGenFunction;
43 /// CGDebugInfo - This class gathers all debug information during compilation
44 /// and is responsible for emitting to llvm globals or pass directly to
48 llvm::DIBuilder DBuilder;
49 llvm::DICompileUnit TheCU;
50 SourceLocation CurLoc, PrevLoc;
51 llvm::DIType VTablePtrType;
53 /// TypeCache - Cache of previously constructed Types.
54 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
56 bool BlockLiteralGenericSet;
57 llvm::DIType BlockLiteralGeneric;
59 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
60 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
61 // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
62 // of a function. This is used to pop unbalanced regions at the end of a
64 std::vector<unsigned> FnBeginRegionCount;
66 /// LineDirectiveFiles - This stack is used to keep track of
67 /// scopes introduced by #line directives.
68 std::vector<const char *> LineDirectiveFiles;
70 /// DebugInfoNames - This is a storage for names that are
71 /// constructed on demand. For example, C++ destructors, C++ operators etc..
72 llvm::BumpPtrAllocator DebugInfoNames;
73 llvm::StringRef CWDName;
75 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
76 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
77 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
79 /// Helper functions for getOrCreateType.
80 llvm::DIType CreateType(const BuiltinType *Ty);
81 llvm::DIType CreateType(const ComplexType *Ty);
82 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
83 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
84 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
86 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
87 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
88 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
89 llvm::DIType CreateType(const TagType *Ty);
90 llvm::DIType CreateType(const RecordType *Ty);
91 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
94 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
96 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
97 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
98 llvm::DIType CreateEnumType(const EnumDecl *ED);
99 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
101 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
103 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
104 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
105 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
106 llvm::DIType CreatePointerLikeType(unsigned Tag,
107 const Type *Ty, QualType PointeeTy,
110 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
112 llvm::DIType RecordTy);
114 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
116 llvm::SmallVectorImpl<llvm::Value *> &E,
119 void CollectCXXFriends(const CXXRecordDecl *Decl,
121 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
122 llvm::DIType RecordTy);
124 void CollectCXXBases(const CXXRecordDecl *Decl,
126 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
127 llvm::DIType RecordTy);
130 CollectTemplateParams(const TemplateParameterList *TPList,
131 const TemplateArgumentList &TAList,
134 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
136 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
139 llvm::DIType createFieldType(llvm::StringRef name, QualType type,
140 Expr *bitWidth, SourceLocation loc,
141 AccessSpecifier AS, uint64_t offsetInBits,
143 llvm::DIDescriptor scope);
144 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
145 llvm::SmallVectorImpl<llvm::Value *> &E,
146 llvm::DIType RecordTy);
148 void CollectVTableInfo(const CXXRecordDecl *Decl,
150 llvm::SmallVectorImpl<llvm::Value *> &EltTys);
153 CGDebugInfo(CodeGenModule &CGM);
156 /// setLocation - Update the current source location. If \arg loc is
157 /// invalid it is ignored.
158 void setLocation(SourceLocation Loc);
160 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
162 void EmitStopPoint(CGBuilderTy &Builder);
164 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
165 /// start of a new function.
166 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
167 llvm::Function *Fn, CGBuilderTy &Builder);
169 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
170 void EmitFunctionEnd(CGBuilderTy &Builder);
172 /// UpdateLineDirectiveRegion - Update region stack only if #line directive
173 /// has introduced scope change.
174 void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
176 /// UpdateCompletedType - Update type cache because the type is now
178 void UpdateCompletedType(const TagDecl *TD);
180 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
182 void EmitRegionStart(CGBuilderTy &Builder);
184 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
186 void EmitRegionEnd(CGBuilderTy &Builder);
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);
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);
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);
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,
210 CGBuilderTy &Builder);
212 /// EmitGlobalVariable - Emit information about a global variable.
213 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
215 /// EmitGlobalVariable - Emit information about an objective-c interface.
216 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
218 /// EmitGlobalVariable - Emit global variable's debug info.
219 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
221 /// getOrCreateRecordType - Emit record type's standalone debug info.
222 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
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);
228 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
229 // See BuildByRefType.
230 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
233 /// getContextDescriptor - Get context info for the decl.
234 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
236 /// getCurrentDirname - Return current directory name.
237 llvm::StringRef getCurrentDirname();
239 /// CreateCompileUnit - Create new compile unit.
240 void CreateCompileUnit();
242 /// getOrCreateFile - Get the file debug info descriptor for the input
244 llvm::DIFile getOrCreateFile(SourceLocation Loc);
246 /// getOrCreateMainFile - Get the file info for main compile unit.
247 llvm::DIFile getOrCreateMainFile();
249 /// getOrCreateType - Get the type from the cache or create a new type if
251 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
253 /// CreateTypeNode - Create type metadata for a source language type.
254 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
256 /// CreateMemberType - Create new member and increase Offset by FType's size.
257 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
258 llvm::StringRef Name, uint64_t *Offset);
260 /// getFunctionDeclaration - Return debug info descriptor to describe method
261 /// declaration for the given method definition.
262 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
264 /// getFunctionName - Get function name for the given FunctionDecl. If the
265 /// name is constructred on demand (e.g. C++ destructor) then the name
266 /// is stored on the side.
267 llvm::StringRef getFunctionName(const FunctionDecl *FD);
269 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
270 /// This is the display name for the debugging info.
271 llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
273 /// getSelectorName - Return selector name. This is used for debugging
275 llvm::StringRef getSelectorName(Selector S);
277 /// getClassName - Get class name including template argument list.
278 llvm::StringRef getClassName(RecordDecl *RD);
280 /// getVTableName - Get vtable name for the given Class.
281 llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
283 /// getLineNumber - Get line number for the location. If location is invalid
284 /// then use current location.
285 unsigned getLineNumber(SourceLocation Loc);
287 /// getColumnNumber - Get column number for the location. If location is
288 /// invalid then use current location.
289 unsigned getColumnNumber(SourceLocation Loc);
291 } // namespace CodeGen