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;
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;
54 llvm::DICompositeType ObjTy;
56 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy;
57 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy;
58 llvm::DIType OCLImage3dDITy;
59 llvm::DIType OCLEventDITy;
61 /// TypeCache - Cache of previously constructed Types.
62 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
64 /// ObjCInterfaceCache - Cache of previously constructed interfaces
65 /// which may change. Storing a pair of DIType and checksum.
66 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
69 /// RetainedTypes - list of interfaces we want to keep even if orphaned.
70 std::vector<void *> RetainedTypes;
72 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
73 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
75 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
77 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
79 bool BlockLiteralGenericSet;
80 llvm::DIType BlockLiteralGeneric;
82 // LexicalBlockStack - Keep track of our current nested lexical block.
83 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
84 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
85 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
86 // beginning of a function. This is used to pop unbalanced regions at
87 // the end of a function.
88 std::vector<unsigned> FnBeginRegionCount;
90 /// DebugInfoNames - This is a storage for names that are
91 /// constructed on demand. For example, C++ destructors, C++ operators etc..
92 llvm::BumpPtrAllocator DebugInfoNames;
95 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
96 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
97 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
98 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache;
100 /// Helper functions for getOrCreateType.
101 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl);
102 llvm::DIType CreateType(const BuiltinType *Ty);
103 llvm::DIType CreateType(const ComplexType *Ty);
104 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
105 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
106 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
108 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
109 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
110 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
111 llvm::DIType CreateType(const RecordType *Ty);
112 llvm::DIType CreateLimitedType(const RecordType *Ty);
113 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
114 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
115 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
116 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
117 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
118 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
119 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
120 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
121 llvm::DIType CreateEnumType(const EnumDecl *ED);
122 llvm::DIType getTypeOrNull(const QualType);
123 llvm::DIType getCompletedTypeOrNull(const QualType);
124 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
126 llvm::DIType getOrCreateInstanceMethodType(
127 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
128 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
130 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
131 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
132 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
133 llvm::DIType CreatePointerLikeType(unsigned Tag,
134 const Type *Ty, QualType PointeeTy,
137 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
138 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
140 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
142 llvm::DIType RecordTy);
144 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
146 SmallVectorImpl<llvm::Value *> &E,
149 void CollectCXXFriends(const CXXRecordDecl *Decl,
151 SmallVectorImpl<llvm::Value *> &EltTys,
152 llvm::DIType RecordTy);
154 void CollectCXXBases(const CXXRecordDecl *Decl,
156 SmallVectorImpl<llvm::Value *> &EltTys,
157 llvm::DIType RecordTy);
160 CollectTemplateParams(const TemplateParameterList *TPList,
161 const TemplateArgumentList &TAList,
164 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
166 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
169 llvm::DIType createFieldType(StringRef name, QualType type,
170 uint64_t sizeInBitsOverride, SourceLocation loc,
171 AccessSpecifier AS, uint64_t offsetInBits,
173 llvm::DIDescriptor scope);
175 // Helpers for collecting fields of a record.
176 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
177 SmallVectorImpl<llvm::Value *> &E,
178 llvm::DIType RecordTy);
179 void CollectRecordStaticField(const VarDecl *Var,
180 SmallVectorImpl<llvm::Value *> &E,
181 llvm::DIType RecordTy);
182 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
184 SmallVectorImpl<llvm::Value *> &E,
185 llvm::DIType RecordTy);
186 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
187 SmallVectorImpl<llvm::Value *> &E,
188 llvm::DIType RecordTy);
190 void CollectVTableInfo(const CXXRecordDecl *Decl,
192 SmallVectorImpl<llvm::Value *> &EltTys);
194 // CreateLexicalBlock - Create a new lexical block node and push it on
196 void CreateLexicalBlock(SourceLocation Loc);
199 CGDebugInfo(CodeGenModule &CGM);
204 /// setLocation - Update the current source location. If \arg loc is
205 /// invalid it is ignored.
206 void setLocation(SourceLocation Loc);
208 /// EmitLocation - Emit metadata to indicate a change in line/column
209 /// information in the source file.
210 /// \param ForceColumnInfo Assume DebugColumnInfo option is true.
211 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
212 bool ForceColumnInfo = false);
214 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
215 /// start of a new function.
216 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
217 llvm::Function *Fn, CGBuilderTy &Builder);
219 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
220 void EmitFunctionEnd(CGBuilderTy &Builder);
222 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
223 /// new lexical block and push the block onto the stack.
224 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
226 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
227 /// block and pop the current block.
228 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
230 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
231 /// variable declaration.
232 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
233 CGBuilderTy &Builder);
235 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
236 /// imported variable declaration in a block.
237 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
238 llvm::Value *storage,
239 CGBuilderTy &Builder,
240 const CGBlockInfo &blockInfo);
242 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
243 /// variable declaration.
244 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
245 unsigned ArgNo, CGBuilderTy &Builder);
247 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
248 /// llvm.dbg.declare for the block-literal argument to a block
249 /// invocation function.
250 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
252 llvm::Value *LocalAddr,
253 CGBuilderTy &Builder);
255 /// EmitGlobalVariable - Emit information about a global variable.
256 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
258 /// EmitGlobalVariable - Emit information about an objective-c interface.
259 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
261 /// EmitGlobalVariable - Emit global variable's debug info.
262 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
264 /// getOrCreateRecordType - Emit record type's standalone debug info.
265 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
267 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
269 llvm::DIType getOrCreateInterfaceType(QualType Ty,
273 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
274 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
275 unsigned ArgNo, CGBuilderTy &Builder);
277 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
278 // See BuildByRefType.
279 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
282 /// getContextDescriptor - Get context info for the decl.
283 llvm::DIDescriptor getContextDescriptor(const Decl *Decl);
285 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
287 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
289 /// createContextChain - Create a set of decls for the context chain.
290 llvm::DIDescriptor createContextChain(const Decl *Decl);
292 /// getCurrentDirname - Return current directory name.
293 StringRef getCurrentDirname();
295 /// CreateCompileUnit - Create new compile unit.
296 void CreateCompileUnit();
298 /// getOrCreateFile - Get the file debug info descriptor for the input
300 llvm::DIFile getOrCreateFile(SourceLocation Loc);
302 /// getOrCreateMainFile - Get the file info for main compile unit.
303 llvm::DIFile getOrCreateMainFile();
305 /// getOrCreateType - Get the type from the cache or create a new type if
307 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
309 /// getOrCreateLimitedType - Get the type from the cache or create a new
310 /// partial type if necessary.
311 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
313 /// CreateTypeNode - Create type metadata for a source language type.
314 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
316 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
317 /// if Ty is an ObjCInterface or a pointer to one.
318 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
320 /// CreateLimitedTypeNode - Create type metadata for a source language
321 /// type, but only partial types for records.
322 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
324 /// CreateMemberType - Create new member and increase Offset by FType's size.
325 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
326 StringRef Name, uint64_t *Offset);
328 /// getFunctionDeclaration - Return debug info descriptor to describe method
329 /// declaration for the given method definition.
330 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
332 /// getStaticDataMemberDeclaration - Return debug info descriptor to
333 /// describe in-class static data member declaration for the given
334 /// out-of-class definition.
335 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
337 /// getFunctionName - Get function name for the given FunctionDecl. If the
338 /// name is constructred on demand (e.g. C++ destructor) then the name
339 /// is stored on the side.
340 StringRef getFunctionName(const FunctionDecl *FD);
342 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
343 /// This is the display name for the debugging info.
344 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
346 /// getSelectorName - Return selector name. This is used for debugging
348 StringRef getSelectorName(Selector S);
350 /// getClassName - Get class name including template argument list.
351 StringRef getClassName(const RecordDecl *RD);
353 /// getVTableName - Get vtable name for the given Class.
354 StringRef getVTableName(const CXXRecordDecl *Decl);
356 /// getLineNumber - Get line number for the location. If location is invalid
357 /// then use current location.
358 unsigned getLineNumber(SourceLocation Loc);
360 /// getColumnNumber - Get column number for the location. If location is
361 /// invalid then use current location.
362 /// \param Force Assume DebugColumnInfo option is true.
363 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
365 } // namespace CodeGen