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 // 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;
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;
72 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
73 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
74 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
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,
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 TagType *Ty);
87 llvm::DIType CreateType(const RecordType *Ty);
88 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
89 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
90 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
91 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
94 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
96 llvm::DIType CreateEnumType(const EnumDecl *ED);
97 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
99 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
101 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
102 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
103 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
104 llvm::DIType CreatePointerLikeType(unsigned Tag,
105 const Type *Ty, QualType PointeeTy,
108 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
110 llvm::DIType RecordTy);
112 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
114 SmallVectorImpl<llvm::Value *> &E,
117 void CollectCXXFriends(const CXXRecordDecl *Decl,
119 SmallVectorImpl<llvm::Value *> &EltTys,
120 llvm::DIType RecordTy);
122 void CollectCXXBases(const CXXRecordDecl *Decl,
124 SmallVectorImpl<llvm::Value *> &EltTys,
125 llvm::DIType RecordTy);
128 CollectTemplateParams(const TemplateParameterList *TPList,
129 const TemplateArgumentList &TAList,
132 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
134 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
137 llvm::DIType createFieldType(StringRef name, QualType type,
138 uint64_t sizeInBitsOverride, SourceLocation loc,
139 AccessSpecifier AS, uint64_t offsetInBits,
141 llvm::DIDescriptor scope);
142 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
143 SmallVectorImpl<llvm::Value *> &E,
144 llvm::DIType RecordTy);
146 void CollectVTableInfo(const CXXRecordDecl *Decl,
148 SmallVectorImpl<llvm::Value *> &EltTys);
150 // CreateLexicalBlock - Create a new lexical block node and push it on
152 void CreateLexicalBlock(SourceLocation Loc);
155 CGDebugInfo(CodeGenModule &CGM);
157 void finalize() { DBuilder.finalize(); }
159 /// setLocation - Update the current source location. If \arg loc is
160 /// invalid it is ignored.
161 void setLocation(SourceLocation Loc);
163 /// EmitLocation - Emit metadata to indicate a change in line/column
164 /// information in the source file.
165 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
167 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
168 /// start of a new function.
169 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
170 llvm::Function *Fn, CGBuilderTy &Builder);
172 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
173 void EmitFunctionEnd(CGBuilderTy &Builder);
175 /// UpdateCompletedType - Update type cache because the type is now
177 void UpdateCompletedType(const TagDecl *TD);
179 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
180 /// new lexical block and push the block onto the stack.
181 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
183 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
184 /// block and pop the current block.
185 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
187 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
188 /// variable declaration.
189 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
190 CGBuilderTy &Builder);
192 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
193 /// imported variable declaration in a block.
194 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
195 llvm::Value *storage,
196 CGBuilderTy &Builder,
197 const CGBlockInfo &blockInfo);
199 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
200 /// variable declaration.
201 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
202 unsigned ArgNo, CGBuilderTy &Builder);
204 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
205 /// llvm.dbg.declare for the block-literal argument to a block
206 /// invocation function.
207 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
209 CGBuilderTy &Builder);
211 /// EmitGlobalVariable - Emit information about a global variable.
212 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
214 /// EmitGlobalVariable - Emit information about an objective-c interface.
215 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
217 /// EmitGlobalVariable - Emit global variable's debug info.
218 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
220 /// getOrCreateRecordType - Emit record type's standalone debug info.
221 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
223 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
224 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
225 unsigned ArgNo, CGBuilderTy &Builder);
227 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
228 // See BuildByRefType.
229 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
232 /// getContextDescriptor - Get context info for the decl.
233 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
235 /// getCurrentDirname - Return current directory name.
236 StringRef getCurrentDirname();
238 /// CreateCompileUnit - Create new compile unit.
239 void CreateCompileUnit();
241 /// getOrCreateFile - Get the file debug info descriptor for the input
243 llvm::DIFile getOrCreateFile(SourceLocation Loc);
245 /// getOrCreateMainFile - Get the file info for main compile unit.
246 llvm::DIFile getOrCreateMainFile();
248 /// getOrCreateType - Get the type from the cache or create a new type if
250 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
252 /// CreateTypeNode - Create type metadata for a source language type.
253 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
255 /// CreateMemberType - Create new member and increase Offset by FType's size.
256 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
257 StringRef Name, uint64_t *Offset);
259 /// getFunctionDeclaration - Return debug info descriptor to describe method
260 /// declaration for the given method definition.
261 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
263 /// getFunctionName - Get function name for the given FunctionDecl. If the
264 /// name is constructred on demand (e.g. C++ destructor) then the name
265 /// is stored on the side.
266 StringRef getFunctionName(const FunctionDecl *FD);
268 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
269 /// This is the display name for the debugging info.
270 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
272 /// getSelectorName - Return selector name. This is used for debugging
274 StringRef getSelectorName(Selector S);
276 /// getClassName - Get class name including template argument list.
277 StringRef getClassName(RecordDecl *RD);
279 /// getVTableName - Get vtable name for the given Class.
280 StringRef getVTableName(const CXXRecordDecl *Decl);
282 /// getLineNumber - Get line number for the location. If location is invalid
283 /// then use current location.
284 unsigned getLineNumber(SourceLocation Loc);
286 /// getColumnNumber - Get column number for the location. If location is
287 /// invalid then use current location.
288 unsigned getColumnNumber(SourceLocation Loc);
290 } // namespace CodeGen