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;
38 class CodeGenFunction;
42 /// CGDebugInfo - This class gathers all debug information during compilation
43 /// and is responsible for emitting to llvm globals or pass directly to
47 llvm::DIBuilder DBuilder;
48 llvm::DICompileUnit TheCU;
49 SourceLocation CurLoc, PrevLoc;
50 llvm::DIType VTablePtrType;
52 /// TypeCache - Cache of previously constructed Types.
53 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
55 bool BlockLiteralGenericSet;
56 llvm::DIType BlockLiteralGeneric;
58 std::vector<llvm::TrackingVH<llvm::MDNode> > RegionStack;
59 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
60 // FnBeginRegionCount - Keep track of RegionStack counter at the beginning
61 // of a function. This is used to pop unbalanced regions at the end of a
63 std::vector<unsigned> FnBeginRegionCount;
65 /// LineDirectiveFiles - This stack is used to keep track of
66 /// scopes introduced by #line directives.
67 std::vector<const char *> LineDirectiveFiles;
69 /// DebugInfoNames - This is a storage for names that are
70 /// constructed on demand. For example, C++ destructors, C++ operators etc..
71 llvm::BumpPtrAllocator DebugInfoNames;
72 llvm::StringRef CWDName;
74 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
75 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
76 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
78 /// Helper functions for getOrCreateType.
79 llvm::DIType CreateType(const BuiltinType *Ty);
80 llvm::DIType CreateType(const ComplexType *Ty);
81 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
82 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
83 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
85 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
86 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
87 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
88 llvm::DIType CreateType(const TagType *Ty);
89 llvm::DIType CreateType(const RecordType *Ty);
90 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
91 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
94 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
96 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
97 llvm::DIType CreateEnumType(const EnumDecl *ED);
98 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
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,
107 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
109 llvm::DIType RecordTy);
111 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
113 llvm::SmallVectorImpl<llvm::Value *> &E,
116 void CollectCXXFriends(const CXXRecordDecl *Decl,
118 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
119 llvm::DIType RecordTy);
121 void CollectCXXBases(const CXXRecordDecl *Decl,
123 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
124 llvm::DIType RecordTy);
126 llvm::DIType createFieldType(llvm::StringRef name, QualType type,
127 Expr *bitWidth, SourceLocation loc,
128 AccessSpecifier AS, uint64_t offsetInBits,
130 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
131 llvm::SmallVectorImpl<llvm::Value *> &E);
133 void CollectVTableInfo(const CXXRecordDecl *Decl,
135 llvm::SmallVectorImpl<llvm::Value *> &EltTys);
138 CGDebugInfo(CodeGenModule &CGM);
141 /// setLocation - Update the current source location. If \arg loc is
142 /// invalid it is ignored.
143 void setLocation(SourceLocation Loc);
145 /// EmitStopPoint - Emit a call to llvm.dbg.stoppoint to indicate a change of
147 void EmitStopPoint(CGBuilderTy &Builder);
149 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
150 /// start of a new function.
151 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
152 llvm::Function *Fn, CGBuilderTy &Builder);
154 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
155 void EmitFunctionEnd(CGBuilderTy &Builder);
157 /// UpdateLineDirectiveRegion - Update region stack only if #line directive
158 /// has introduced scope change.
159 void UpdateLineDirectiveRegion(CGBuilderTy &Builder);
161 /// EmitRegionStart - Emit a call to llvm.dbg.region.start to indicate start
163 void EmitRegionStart(CGBuilderTy &Builder);
165 /// EmitRegionEnd - Emit call to llvm.dbg.region.end to indicate end of a
167 void EmitRegionEnd(CGBuilderTy &Builder);
169 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
170 /// variable declaration.
171 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
172 CGBuilderTy &Builder);
174 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
175 /// imported variable declaration in a block.
176 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
177 llvm::Value *storage,
178 CGBuilderTy &Builder,
179 const CGBlockInfo &blockInfo);
181 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
182 /// variable declaration.
183 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
184 CGBuilderTy &Builder);
186 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
187 /// llvm.dbg.declare for the block-literal argument to a block
188 /// invocation function.
189 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
191 CGBuilderTy &Builder);
193 /// EmitGlobalVariable - Emit information about a global variable.
194 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
196 /// EmitGlobalVariable - Emit information about an objective-c interface.
197 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
199 /// EmitGlobalVariable - Emit global variable's debug info.
200 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
202 /// getOrCreateRecordType - Emit record type's standalone debug info.
203 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
205 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
206 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
207 CGBuilderTy &Builder);
209 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable
210 /// declaration from an enclosing block.
211 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
212 CGBuilderTy &Builder, const CGBlockInfo &blockInfo);
214 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
215 // See BuildByRefType.
216 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
219 /// getContextDescriptor - Get context info for the decl.
220 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
222 /// getCurrentDirname - Return current directory name.
223 llvm::StringRef getCurrentDirname();
225 /// CreateCompileUnit - Create new compile unit.
226 void CreateCompileUnit();
228 /// getOrCreateFile - Get the file debug info descriptor for the input
230 llvm::DIFile getOrCreateFile(SourceLocation Loc);
232 /// getOrCreateMainFile - Get the file info for main compile unit.
233 llvm::DIFile getOrCreateMainFile();
235 /// getOrCreateType - Get the type from the cache or create a new type if
237 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
239 /// CreateTypeNode - Create type metadata for a source language type.
240 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
242 /// CreateMemberType - Create new member and increase Offset by FType's size.
243 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
244 llvm::StringRef Name, uint64_t *Offset);
246 /// getFunctionName - Get function name for the given FunctionDecl. If the
247 /// name is constructred on demand (e.g. C++ destructor) then the name
248 /// is stored on the side.
249 llvm::StringRef getFunctionName(const FunctionDecl *FD);
251 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
252 /// This is the display name for the debugging info.
253 llvm::StringRef getObjCMethodName(const ObjCMethodDecl *FD);
255 /// getClassName - Get class name including template argument list.
256 llvm::StringRef getClassName(RecordDecl *RD);
258 /// getVTableName - Get vtable name for the given Class.
259 llvm::StringRef getVTableName(const CXXRecordDecl *Decl);
261 /// getLineNumber - Get line number for the location. If location is invalid
262 /// then use current location.
263 unsigned getLineNumber(SourceLocation Loc);
265 /// getColumnNumber - Get column number for the location. If location is
266 /// invalid then use current location.
267 unsigned getColumnNumber(SourceLocation Loc);
269 } // namespace CodeGen