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/DebugInfo.h"
21 #include "llvm/DIBuilder.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/ValueHandle.h"
24 #include "llvm/Support/Allocator.h"
26 #include "CGBuilder.h"
35 class ObjCInterfaceDecl;
36 class ClassTemplateSpecializationDecl;
41 class CodeGenFunction;
44 /// CGDebugInfo - This class gathers all debug information during compilation
45 /// and is responsible for emitting to llvm globals or pass directly to
49 llvm::DIBuilder DBuilder;
50 llvm::DICompileUnit TheCU;
51 SourceLocation CurLoc, PrevLoc;
52 llvm::DIType VTablePtrType;
57 /// TypeCache - Cache of previously constructed Types.
58 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
60 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
61 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
63 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
65 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
67 bool BlockLiteralGenericSet;
68 llvm::DIType BlockLiteralGeneric;
70 // LexicalBlockStack - Keep track of our current nested lexical block.
71 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
72 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
73 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
74 // beginning of a function. This is used to pop unbalanced regions at
75 // the end of a function.
76 std::vector<unsigned> FnBeginRegionCount;
78 /// DebugInfoNames - This is a storage for names that are
79 /// constructed on demand. For example, C++ destructors, C++ operators etc..
80 llvm::BumpPtrAllocator DebugInfoNames;
83 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
84 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
85 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
87 /// Helper functions for getOrCreateType.
88 llvm::DIType CreateType(const BuiltinType *Ty);
89 llvm::DIType CreateType(const ComplexType *Ty);
90 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
91 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
92 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
94 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
95 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
96 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
97 llvm::DIType CreateType(const RecordType *Ty);
98 llvm::DIType CreateLimitedType(const RecordType *Ty);
99 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
100 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
101 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
102 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
103 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
104 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
105 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
106 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
107 llvm::DIType CreateEnumType(const EnumDecl *ED);
108 llvm::DIType getTypeOrNull(const QualType);
109 llvm::DIType getCompletedTypeOrNull(const QualType);
110 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
112 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
114 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
115 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
116 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
117 llvm::DIType CreatePointerLikeType(unsigned Tag,
118 const Type *Ty, QualType PointeeTy,
121 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
123 llvm::DIType RecordTy);
125 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
127 SmallVectorImpl<llvm::Value *> &E,
130 void CollectCXXFriends(const CXXRecordDecl *Decl,
132 SmallVectorImpl<llvm::Value *> &EltTys,
133 llvm::DIType RecordTy);
135 void CollectCXXBases(const CXXRecordDecl *Decl,
137 SmallVectorImpl<llvm::Value *> &EltTys,
138 llvm::DIType RecordTy);
141 CollectTemplateParams(const TemplateParameterList *TPList,
142 const TemplateArgumentList &TAList,
145 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
147 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
150 llvm::DIType createFieldType(StringRef name, QualType type,
151 uint64_t sizeInBitsOverride, SourceLocation loc,
152 AccessSpecifier AS, uint64_t offsetInBits,
154 llvm::DIDescriptor scope);
155 void CollectRecordStaticVars(const RecordDecl *, llvm::DIType);
156 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
157 SmallVectorImpl<llvm::Value *> &E,
158 llvm::DIType RecordTy);
160 void CollectVTableInfo(const CXXRecordDecl *Decl,
162 SmallVectorImpl<llvm::Value *> &EltTys);
164 // CreateLexicalBlock - Create a new lexical block node and push it on
166 void CreateLexicalBlock(SourceLocation Loc);
169 CGDebugInfo(CodeGenModule &CGM);
174 /// setLocation - Update the current source location. If \arg loc is
175 /// invalid it is ignored.
176 void setLocation(SourceLocation Loc);
178 /// EmitLocation - Emit metadata to indicate a change in line/column
179 /// information in the source file.
180 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
182 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
183 /// start of a new function.
184 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
185 llvm::Function *Fn, CGBuilderTy &Builder);
187 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
188 void EmitFunctionEnd(CGBuilderTy &Builder);
190 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
191 /// new lexical block and push the block onto the stack.
192 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
194 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
195 /// block and pop the current block.
196 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
198 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
199 /// variable declaration.
200 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
201 CGBuilderTy &Builder);
203 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
204 /// imported variable declaration in a block.
205 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
206 llvm::Value *storage,
207 CGBuilderTy &Builder,
208 const CGBlockInfo &blockInfo);
210 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
211 /// variable declaration.
212 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
213 unsigned ArgNo, CGBuilderTy &Builder);
215 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
216 /// llvm.dbg.declare for the block-literal argument to a block
217 /// invocation function.
218 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
220 CGBuilderTy &Builder);
222 /// EmitGlobalVariable - Emit information about a global variable.
223 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
225 /// EmitGlobalVariable - Emit information about an objective-c interface.
226 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
228 /// EmitGlobalVariable - Emit global variable's debug info.
229 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
231 /// getOrCreateRecordType - Emit record type's standalone debug info.
232 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
234 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
236 llvm::DIType getOrCreateInterfaceType(QualType Ty,
240 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
241 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
242 unsigned ArgNo, CGBuilderTy &Builder);
244 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
245 // See BuildByRefType.
246 llvm::DIType EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
249 /// getContextDescriptor - Get context info for the decl.
250 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
252 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
254 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
256 /// createContextChain - Create a set of decls for the context chain.
257 llvm::DIDescriptor createContextChain(const Decl *Decl);
259 /// getCurrentDirname - Return current directory name.
260 StringRef getCurrentDirname();
262 /// CreateCompileUnit - Create new compile unit.
263 void CreateCompileUnit();
265 /// getOrCreateFile - Get the file debug info descriptor for the input
267 llvm::DIFile getOrCreateFile(SourceLocation Loc);
269 /// getOrCreateMainFile - Get the file info for main compile unit.
270 llvm::DIFile getOrCreateMainFile();
272 /// getOrCreateType - Get the type from the cache or create a new type if
274 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
276 /// getOrCreateLimitedType - Get the type from the cache or create a new
277 /// partial type if necessary.
278 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
280 /// CreateTypeNode - Create type metadata for a source language type.
281 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
283 /// CreateLimitedTypeNode - Create type metadata for a source language
284 /// type, but only partial types for records.
285 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
287 /// CreateMemberType - Create new member and increase Offset by FType's size.
288 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
289 StringRef Name, uint64_t *Offset);
291 /// getFunctionDeclaration - Return debug info descriptor to describe method
292 /// declaration for the given method definition.
293 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
295 /// getFunctionName - Get function name for the given FunctionDecl. If the
296 /// name is constructred on demand (e.g. C++ destructor) then the name
297 /// is stored on the side.
298 StringRef getFunctionName(const FunctionDecl *FD);
300 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
301 /// This is the display name for the debugging info.
302 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
304 /// getSelectorName - Return selector name. This is used for debugging
306 StringRef getSelectorName(Selector S);
308 /// getClassName - Get class name including template argument list.
309 StringRef getClassName(const RecordDecl *RD);
311 /// getVTableName - Get vtable name for the given Class.
312 StringRef getVTableName(const CXXRecordDecl *Decl);
314 /// getLineNumber - Get line number for the location. If location is invalid
315 /// then use current location.
316 unsigned getLineNumber(SourceLocation Loc);
318 /// getColumnNumber - Get column number for the location. If location is
319 /// invalid then use current location.
320 unsigned getColumnNumber(SourceLocation Loc);
322 } // namespace CodeGen