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;
42 class CodeGenFunction;
45 /// CGDebugInfo - This class gathers all debug information during compilation
46 /// and is responsible for emitting to llvm globals or pass directly to
50 llvm::DIBuilder DBuilder;
51 llvm::DICompileUnit TheCU;
52 SourceLocation CurLoc, PrevLoc;
53 llvm::DIType VTablePtrType;
55 llvm::DICompositeType ObjTy;
57 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy;
58 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy;
59 llvm::DIType OCLImage3dDITy;
60 llvm::DIType OCLEventDITy;
62 /// TypeCache - Cache of previously constructed Types.
63 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
65 /// ObjCInterfaceCache - Cache of previously constructed interfaces
66 /// which may change. Storing a pair of DIType and checksum.
67 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
70 /// RetainedTypes - list of interfaces we want to keep even if orphaned.
71 std::vector<void *> RetainedTypes;
73 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
74 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
76 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
78 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
80 bool BlockLiteralGenericSet;
81 llvm::DIType BlockLiteralGeneric;
83 // LexicalBlockStack - Keep track of our current nested lexical block.
84 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
85 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
86 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
87 // beginning of a function. This is used to pop unbalanced regions at
88 // the end of a function.
89 std::vector<unsigned> FnBeginRegionCount;
91 /// DebugInfoNames - This is a storage for names that are
92 /// constructed on demand. For example, C++ destructors, C++ operators etc..
93 llvm::BumpPtrAllocator DebugInfoNames;
96 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
97 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
98 /// \brief Cache declarations relevant to DW_TAG_imported_declarations (C++
99 /// using declarations) that aren't covered by other more specific caches.
100 llvm::DenseMap<const Decl *, llvm::WeakVH> DeclCache;
101 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
102 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache;
104 /// Helper functions for getOrCreateType.
105 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl);
106 llvm::DIType CreateType(const BuiltinType *Ty);
107 llvm::DIType CreateType(const ComplexType *Ty);
108 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
109 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
110 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
112 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
113 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
114 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
115 llvm::DIType CreateType(const RecordType *Ty);
116 llvm::DIType CreateLimitedType(const RecordType *Ty);
117 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
118 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
119 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
120 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
121 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
122 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
123 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
124 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
125 llvm::DIType CreateEnumType(const EnumDecl *ED);
126 llvm::DIType CreateSelfType(const QualType &QualTy, llvm::DIType Ty);
127 llvm::DIType getTypeOrNull(const QualType);
128 llvm::DIType getCompletedTypeOrNull(const QualType);
129 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
131 llvm::DIType getOrCreateInstanceMethodType(
132 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
133 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
135 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
136 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
137 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
138 llvm::DIType CreatePointerLikeType(unsigned Tag,
139 const Type *Ty, QualType PointeeTy,
142 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
143 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
145 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
147 llvm::DIType RecordTy);
149 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
151 SmallVectorImpl<llvm::Value *> &E,
154 void CollectCXXFriends(const CXXRecordDecl *Decl,
156 SmallVectorImpl<llvm::Value *> &EltTys,
157 llvm::DIType RecordTy);
159 void CollectCXXBases(const CXXRecordDecl *Decl,
161 SmallVectorImpl<llvm::Value *> &EltTys,
162 llvm::DIType RecordTy);
165 CollectTemplateParams(const TemplateParameterList *TPList,
166 const TemplateArgumentList &TAList,
169 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
171 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
174 llvm::DIType createFieldType(StringRef name, QualType type,
175 uint64_t sizeInBitsOverride, SourceLocation loc,
176 AccessSpecifier AS, uint64_t offsetInBits,
178 llvm::DIDescriptor scope);
180 // Helpers for collecting fields of a record.
181 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
182 SmallVectorImpl<llvm::Value *> &E,
183 llvm::DIType RecordTy);
184 void CollectRecordStaticField(const VarDecl *Var,
185 SmallVectorImpl<llvm::Value *> &E,
186 llvm::DIType RecordTy);
187 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
189 SmallVectorImpl<llvm::Value *> &E,
190 llvm::DIType RecordTy);
191 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
192 SmallVectorImpl<llvm::Value *> &E,
193 llvm::DIType RecordTy);
195 void CollectVTableInfo(const CXXRecordDecl *Decl,
197 SmallVectorImpl<llvm::Value *> &EltTys);
199 // CreateLexicalBlock - Create a new lexical block node and push it on
201 void CreateLexicalBlock(SourceLocation Loc);
204 CGDebugInfo(CodeGenModule &CGM);
209 /// setLocation - Update the current source location. If \arg loc is
210 /// invalid it is ignored.
211 void setLocation(SourceLocation Loc);
213 /// EmitLocation - Emit metadata to indicate a change in line/column
214 /// information in the source file.
215 /// \param ForceColumnInfo Assume DebugColumnInfo option is true.
216 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
217 bool ForceColumnInfo = false);
219 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
220 /// start of a new function.
221 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
222 llvm::Function *Fn, CGBuilderTy &Builder);
224 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
225 void EmitFunctionEnd(CGBuilderTy &Builder);
227 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
228 /// new lexical block and push the block onto the stack.
229 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
231 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
232 /// block and pop the current block.
233 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
235 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
236 /// variable declaration.
237 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
238 CGBuilderTy &Builder);
240 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
241 /// imported variable declaration in a block.
242 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
243 llvm::Value *storage,
244 CGBuilderTy &Builder,
245 const CGBlockInfo &blockInfo);
247 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
248 /// variable declaration.
249 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
250 unsigned ArgNo, CGBuilderTy &Builder);
252 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
253 /// llvm.dbg.declare for the block-literal argument to a block
254 /// invocation function.
255 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
257 llvm::Value *LocalAddr,
258 CGBuilderTy &Builder);
260 /// EmitGlobalVariable - Emit information about a global variable.
261 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
263 /// EmitGlobalVariable - Emit information about an objective-c interface.
264 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
266 /// EmitGlobalVariable - Emit global variable's debug info.
267 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
269 /// \brief - Emit C++ using directive.
270 void EmitUsingDirective(const UsingDirectiveDecl &UD);
272 /// \brief - Emit C++ using declaration.
273 void EmitUsingDecl(const UsingDecl &UD);
275 /// getOrCreateRecordType - Emit record type's standalone debug info.
276 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
278 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
280 llvm::DIType getOrCreateInterfaceType(QualType Ty,
284 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
285 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
286 unsigned ArgNo, CGBuilderTy &Builder);
288 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
289 // See BuildByRefType.
290 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
293 /// getContextDescriptor - Get context info for the decl.
294 llvm::DIScope getContextDescriptor(const Decl *Decl);
296 llvm::DIScope getCurrentContextDescriptor(const Decl *Decl);
298 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
300 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
302 /// createContextChain - Create a set of decls for the context chain.
303 llvm::DIDescriptor createContextChain(const Decl *Decl);
305 /// getCurrentDirname - Return current directory name.
306 StringRef getCurrentDirname();
308 /// CreateCompileUnit - Create new compile unit.
309 void CreateCompileUnit();
311 /// getOrCreateFile - Get the file debug info descriptor for the input
313 llvm::DIFile getOrCreateFile(SourceLocation Loc);
315 /// getOrCreateMainFile - Get the file info for main compile unit.
316 llvm::DIFile getOrCreateMainFile();
318 /// getOrCreateType - Get the type from the cache or create a new type if
320 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
322 /// getOrCreateLimitedType - Get the type from the cache or create a new
323 /// partial type if necessary.
324 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
326 /// CreateTypeNode - Create type metadata for a source language type.
327 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
329 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
330 /// if Ty is an ObjCInterface or a pointer to one.
331 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
333 /// CreateLimitedTypeNode - Create type metadata for a source language
334 /// type, but only partial types for records.
335 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
337 /// CreateMemberType - Create new member and increase Offset by FType's size.
338 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
339 StringRef Name, uint64_t *Offset);
341 /// \brief Retrieve the DIDescriptor, if any, for the canonical form of this
343 llvm::DIDescriptor getDeclarationOrDefinition(const Decl *D);
345 /// getFunctionDeclaration - Return debug info descriptor to describe method
346 /// declaration for the given method definition.
347 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
349 /// getStaticDataMemberDeclaration - Return debug info descriptor to
350 /// describe in-class static data member declaration for the given
351 /// out-of-class definition.
352 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
354 /// getFunctionName - Get function name for the given FunctionDecl. If the
355 /// name is constructred on demand (e.g. C++ destructor) then the name
356 /// is stored on the side.
357 StringRef getFunctionName(const FunctionDecl *FD);
359 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
360 /// This is the display name for the debugging info.
361 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
363 /// getSelectorName - Return selector name. This is used for debugging
365 StringRef getSelectorName(Selector S);
367 /// getClassName - Get class name including template argument list.
368 StringRef getClassName(const RecordDecl *RD);
370 /// getVTableName - Get vtable name for the given Class.
371 StringRef getVTableName(const CXXRecordDecl *Decl);
373 /// getLineNumber - Get line number for the location. If location is invalid
374 /// then use current location.
375 unsigned getLineNumber(SourceLocation Loc);
377 /// getColumnNumber - Get column number for the location. If location is
378 /// invalid then use current location.
379 /// \param Force Assume DebugColumnInfo option is true.
380 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
382 } // namespace CodeGen