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 "CGBuilder.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/DIBuilder.h"
23 #include "llvm/DebugInfo.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/ValueHandle.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;
55 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy;
56 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy;
57 llvm::DIType OCLImage3dDITy;
58 llvm::DIType OCLEventDITy;
60 /// TypeCache - Cache of previously constructed Types.
61 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
63 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
64 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
66 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
68 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
70 bool BlockLiteralGenericSet;
71 llvm::DIType BlockLiteralGeneric;
73 // LexicalBlockStack - Keep track of our current nested lexical block.
74 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
75 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
76 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
77 // beginning of a function. This is used to pop unbalanced regions at
78 // the end of a function.
79 std::vector<unsigned> FnBeginRegionCount;
81 /// DebugInfoNames - This is a storage for names that are
82 /// constructed on demand. For example, C++ destructors, C++ operators etc..
83 llvm::BumpPtrAllocator DebugInfoNames;
86 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
87 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
88 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
89 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache;
91 /// Helper functions for getOrCreateType.
92 llvm::DIType CreateType(const BuiltinType *Ty);
93 llvm::DIType CreateType(const ComplexType *Ty);
94 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
96 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
98 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
99 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
100 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
101 llvm::DIType CreateType(const RecordType *Ty);
102 llvm::DIType CreateLimitedType(const RecordType *Ty);
103 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
104 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
105 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
106 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
107 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
108 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
109 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
110 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
111 llvm::DIType CreateEnumType(const EnumDecl *ED);
112 llvm::DIType getTypeOrNull(const QualType);
113 llvm::DIType getCompletedTypeOrNull(const QualType);
114 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
116 llvm::DIType getOrCreateInstanceMethodType(
117 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
118 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
120 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
121 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
122 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
123 llvm::DIType CreatePointerLikeType(unsigned Tag,
124 const Type *Ty, QualType PointeeTy,
127 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
129 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
131 llvm::DIType RecordTy);
133 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
135 SmallVectorImpl<llvm::Value *> &E,
138 void CollectCXXFriends(const CXXRecordDecl *Decl,
140 SmallVectorImpl<llvm::Value *> &EltTys,
141 llvm::DIType RecordTy);
143 void CollectCXXBases(const CXXRecordDecl *Decl,
145 SmallVectorImpl<llvm::Value *> &EltTys,
146 llvm::DIType RecordTy);
149 CollectTemplateParams(const TemplateParameterList *TPList,
150 const TemplateArgumentList &TAList,
153 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
155 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
158 llvm::DIType createFieldType(StringRef name, QualType type,
159 uint64_t sizeInBitsOverride, SourceLocation loc,
160 AccessSpecifier AS, uint64_t offsetInBits,
162 llvm::DIDescriptor scope);
164 // Helpers for collecting fields of a record.
165 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
166 SmallVectorImpl<llvm::Value *> &E,
167 llvm::DIType RecordTy);
168 void CollectRecordStaticField(const VarDecl *Var,
169 SmallVectorImpl<llvm::Value *> &E,
170 llvm::DIType RecordTy);
171 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
173 SmallVectorImpl<llvm::Value *> &E,
174 llvm::DIType RecordTy);
175 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
176 SmallVectorImpl<llvm::Value *> &E,
177 llvm::DIType RecordTy);
179 void CollectVTableInfo(const CXXRecordDecl *Decl,
181 SmallVectorImpl<llvm::Value *> &EltTys);
183 // CreateLexicalBlock - Create a new lexical block node and push it on
185 void CreateLexicalBlock(SourceLocation Loc);
188 CGDebugInfo(CodeGenModule &CGM);
193 /// setLocation - Update the current source location. If \arg loc is
194 /// invalid it is ignored.
195 void setLocation(SourceLocation Loc);
197 /// EmitLocation - Emit metadata to indicate a change in line/column
198 /// information in the source file.
199 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
201 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
202 /// start of a new function.
203 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
204 llvm::Function *Fn, CGBuilderTy &Builder);
206 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
207 void EmitFunctionEnd(CGBuilderTy &Builder);
209 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
210 /// new lexical block and push the block onto the stack.
211 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
213 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
214 /// block and pop the current block.
215 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
217 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
218 /// variable declaration.
219 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
220 CGBuilderTy &Builder);
222 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
223 /// imported variable declaration in a block.
224 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
225 llvm::Value *storage,
226 CGBuilderTy &Builder,
227 const CGBlockInfo &blockInfo);
229 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
230 /// variable declaration.
231 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
232 unsigned ArgNo, CGBuilderTy &Builder);
234 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
235 /// llvm.dbg.declare for the block-literal argument to a block
236 /// invocation function.
237 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
239 CGBuilderTy &Builder);
241 /// EmitGlobalVariable - Emit information about a global variable.
242 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
244 /// EmitGlobalVariable - Emit information about an objective-c interface.
245 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
247 /// EmitGlobalVariable - Emit global variable's debug info.
248 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
250 /// getOrCreateRecordType - Emit record type's standalone debug info.
251 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
253 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
255 llvm::DIType getOrCreateInterfaceType(QualType Ty,
259 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
260 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
261 unsigned ArgNo, CGBuilderTy &Builder);
263 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
264 // See BuildByRefType.
265 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
268 /// getContextDescriptor - Get context info for the decl.
269 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
271 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
273 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
275 /// createContextChain - Create a set of decls for the context chain.
276 llvm::DIDescriptor createContextChain(const Decl *Decl);
278 /// getCurrentDirname - Return current directory name.
279 StringRef getCurrentDirname();
281 /// CreateCompileUnit - Create new compile unit.
282 void CreateCompileUnit();
284 /// getOrCreateFile - Get the file debug info descriptor for the input
286 llvm::DIFile getOrCreateFile(SourceLocation Loc);
288 /// getOrCreateMainFile - Get the file info for main compile unit.
289 llvm::DIFile getOrCreateMainFile();
291 /// getOrCreateType - Get the type from the cache or create a new type if
293 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
295 /// getOrCreateLimitedType - Get the type from the cache or create a new
296 /// partial type if necessary.
297 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
299 /// CreateTypeNode - Create type metadata for a source language type.
300 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
302 /// CreateLimitedTypeNode - Create type metadata for a source language
303 /// type, but only partial types for records.
304 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
306 /// CreateMemberType - Create new member and increase Offset by FType's size.
307 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
308 StringRef Name, uint64_t *Offset);
310 /// getFunctionDeclaration - Return debug info descriptor to describe method
311 /// declaration for the given method definition.
312 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
314 /// getStaticDataMemberDeclaration - Return debug info descriptor to
315 /// describe in-class static data member declaration for the given
316 /// out-of-class definition.
317 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
319 /// getFunctionName - Get function name for the given FunctionDecl. If the
320 /// name is constructred on demand (e.g. C++ destructor) then the name
321 /// is stored on the side.
322 StringRef getFunctionName(const FunctionDecl *FD);
324 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
325 /// This is the display name for the debugging info.
326 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
328 /// getSelectorName - Return selector name. This is used for debugging
330 StringRef getSelectorName(Selector S);
332 /// getClassName - Get class name including template argument list.
333 StringRef getClassName(const RecordDecl *RD);
335 /// getVTableName - Get vtable name for the given Class.
336 StringRef getVTableName(const CXXRecordDecl *Decl);
338 /// getLineNumber - Get line number for the location. If location is invalid
339 /// then use current location.
340 unsigned getLineNumber(SourceLocation Loc);
342 /// getColumnNumber - Get column number for the location. If location is
343 /// invalid then use current location.
344 unsigned getColumnNumber(SourceLocation Loc);
346 } // namespace CodeGen