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 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
57 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
59 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
61 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
63 bool BlockLiteralGenericSet;
64 llvm::DIType BlockLiteralGeneric;
66 // LexicalBlockStack - Keep track of our current nested lexical block.
67 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
68 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
69 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
70 // beginning of a function. This is used to pop unbalanced regions at
71 // the end of a function.
72 std::vector<unsigned> FnBeginRegionCount;
74 /// DebugInfoNames - This is a storage for names that are
75 /// constructed on demand. For example, C++ destructors, C++ operators etc..
76 llvm::BumpPtrAllocator DebugInfoNames;
79 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
80 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
81 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
83 /// Helper functions for getOrCreateType.
84 llvm::DIType CreateType(const BuiltinType *Ty);
85 llvm::DIType CreateType(const ComplexType *Ty);
86 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
87 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
88 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
90 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
91 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
93 llvm::DIType CreateType(const RecordType *Ty);
94 llvm::DIType CreateLimitedType(const RecordType *Ty);
95 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
96 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
97 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
98 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
99 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
100 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
101 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
102 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
103 llvm::DIType CreateEnumType(const EnumDecl *ED);
104 llvm::DIType getTypeOrNull(const QualType);
105 llvm::DIType getCompletedTypeOrNull(const QualType);
106 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
108 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
110 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
111 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
112 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
113 llvm::DIType CreatePointerLikeType(unsigned Tag,
114 const Type *Ty, QualType PointeeTy,
117 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
119 llvm::DIType RecordTy);
121 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
123 SmallVectorImpl<llvm::Value *> &E,
126 void CollectCXXFriends(const CXXRecordDecl *Decl,
128 SmallVectorImpl<llvm::Value *> &EltTys,
129 llvm::DIType RecordTy);
131 void CollectCXXBases(const CXXRecordDecl *Decl,
133 SmallVectorImpl<llvm::Value *> &EltTys,
134 llvm::DIType RecordTy);
137 CollectTemplateParams(const TemplateParameterList *TPList,
138 const TemplateArgumentList &TAList,
141 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
143 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
146 llvm::DIType createFieldType(StringRef name, QualType type,
147 uint64_t sizeInBitsOverride, SourceLocation loc,
148 AccessSpecifier AS, uint64_t offsetInBits,
150 llvm::DIDescriptor scope);
151 void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
152 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
153 SmallVectorImpl<llvm::Value *> &E,
154 llvm::DIType RecordTy);
156 void CollectVTableInfo(const CXXRecordDecl *Decl,
158 SmallVectorImpl<llvm::Value *> &EltTys);
160 // CreateLexicalBlock - Create a new lexical block node and push it on
162 void CreateLexicalBlock(SourceLocation Loc);
165 CGDebugInfo(CodeGenModule &CGM);
170 /// setLocation - Update the current source location. If \arg loc is
171 /// invalid it is ignored.
172 void setLocation(SourceLocation Loc);
174 /// EmitLocation - Emit metadata to indicate a change in line/column
175 /// information in the source file.
176 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
178 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
179 /// start of a new function.
180 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
181 llvm::Function *Fn, CGBuilderTy &Builder);
183 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
184 void EmitFunctionEnd(CGBuilderTy &Builder);
186 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
187 /// new lexical block and push the block onto the stack.
188 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
190 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
191 /// block and pop the current block.
192 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
194 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
195 /// variable declaration.
196 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
197 CGBuilderTy &Builder);
199 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
200 /// imported variable declaration in a block.
201 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
202 llvm::Value *storage,
203 CGBuilderTy &Builder,
204 const CGBlockInfo &blockInfo);
206 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
207 /// variable declaration.
208 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
209 unsigned ArgNo, CGBuilderTy &Builder);
211 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
212 /// llvm.dbg.declare for the block-literal argument to a block
213 /// invocation function.
214 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
216 CGBuilderTy &Builder);
218 /// EmitGlobalVariable - Emit information about a global variable.
219 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
221 /// EmitGlobalVariable - Emit information about an objective-c interface.
222 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
224 /// EmitGlobalVariable - Emit global variable's debug info.
225 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
227 /// getOrCreateRecordType - Emit record type's standalone debug info.
228 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
230 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
232 llvm::DIType getOrCreateInterfaceType(QualType Ty,
236 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
237 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
238 unsigned ArgNo, CGBuilderTy &Builder);
240 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
241 // See BuildByRefType.
242 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
245 /// getContextDescriptor - Get context info for the decl.
246 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
248 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
250 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
252 /// createContextChain - Create a set of decls for the context chain.
253 llvm::DIDescriptor createContextChain(const Decl *Decl);
255 /// getCurrentDirname - Return current directory name.
256 StringRef getCurrentDirname();
258 /// CreateCompileUnit - Create new compile unit.
259 void CreateCompileUnit();
261 /// getOrCreateFile - Get the file debug info descriptor for the input
263 llvm::DIFile getOrCreateFile(SourceLocation Loc);
265 /// getOrCreateMainFile - Get the file info for main compile unit.
266 llvm::DIFile getOrCreateMainFile();
268 /// getOrCreateType - Get the type from the cache or create a new type if
270 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
272 /// getOrCreateLimitedType - Get the type from the cache or create a new
273 /// partial type if necessary.
274 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
276 /// CreateTypeNode - Create type metadata for a source language type.
277 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
279 /// CreateLimitedTypeNode - Create type metadata for a source language
280 /// type, but only partial types for records.
281 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
283 /// CreateMemberType - Create new member and increase Offset by FType's size.
284 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
285 StringRef Name, uint64_t *Offset);
287 /// getFunctionDeclaration - Return debug info descriptor to describe method
288 /// declaration for the given method definition.
289 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
291 /// getFunctionName - Get function name for the given FunctionDecl. If the
292 /// name is constructred on demand (e.g. C++ destructor) then the name
293 /// is stored on the side.
294 StringRef getFunctionName(const FunctionDecl *FD);
296 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
297 /// This is the display name for the debugging info.
298 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
300 /// getSelectorName - Return selector name. This is used for debugging
302 StringRef getSelectorName(Selector S);
304 /// getClassName - Get class name including template argument list.
305 StringRef getClassName(const RecordDecl *RD);
307 /// getVTableName - Get vtable name for the given Class.
308 StringRef getVTableName(const CXXRecordDecl *Decl);
310 /// getLineNumber - Get line number for the location. If location is invalid
311 /// then use current location.
312 unsigned getLineNumber(SourceLocation Loc);
314 /// getColumnNumber - Get column number for the location. If location is
315 /// invalid then use current location.
316 unsigned getColumnNumber(SourceLocation Loc);
318 } // namespace CodeGen