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 "clang/Frontend/CodeGenOptions.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/DIBuilder.h"
24 #include "llvm/DebugInfo.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/ValueHandle.h"
35 class ObjCInterfaceDecl;
37 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 CodeGenOptions::DebugInfoKind DebugKind;
51 llvm::DIBuilder DBuilder;
52 llvm::DICompileUnit TheCU;
53 SourceLocation CurLoc, PrevLoc;
54 llvm::DIType VTablePtrType;
56 llvm::DICompositeType ObjTy;
58 llvm::DIType OCLImage1dDITy, OCLImage1dArrayDITy, OCLImage1dBufferDITy;
59 llvm::DIType OCLImage2dDITy, OCLImage2dArrayDITy;
60 llvm::DIType OCLImage3dDITy;
61 llvm::DIType OCLEventDITy;
63 /// TypeCache - Cache of previously constructed Types.
64 llvm::DenseMap<void *, llvm::WeakVH> TypeCache;
66 /// ObjCInterfaceCache - Cache of previously constructed interfaces
67 /// which may change. Storing a pair of DIType and checksum.
68 llvm::DenseMap<void *, std::pair<llvm::WeakVH, unsigned > >
71 /// RetainedTypes - list of interfaces we want to keep even if orphaned.
72 std::vector<void *> RetainedTypes;
74 /// CompleteTypeCache - Cache of previously constructed complete RecordTypes.
75 llvm::DenseMap<void *, llvm::WeakVH> CompletedTypeCache;
77 /// ReplaceMap - Cache of forward declared types to RAUW at the end of
79 std::vector<std::pair<void *, llvm::WeakVH> >ReplaceMap;
81 bool BlockLiteralGenericSet;
82 llvm::DIType BlockLiteralGeneric;
84 // LexicalBlockStack - Keep track of our current nested lexical block.
85 std::vector<llvm::TrackingVH<llvm::MDNode> > LexicalBlockStack;
86 llvm::DenseMap<const Decl *, llvm::WeakVH> RegionMap;
87 // FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
88 // beginning of a function. This is used to pop unbalanced regions at
89 // the end of a function.
90 std::vector<unsigned> FnBeginRegionCount;
92 /// DebugInfoNames - This is a storage for names that are
93 /// constructed on demand. For example, C++ destructors, C++ operators etc..
94 llvm::BumpPtrAllocator DebugInfoNames;
97 llvm::DenseMap<const char *, llvm::WeakVH> DIFileCache;
98 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH> SPCache;
99 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH> NameSpaceCache;
100 llvm::DenseMap<const Decl *, llvm::WeakVH> StaticDataMemberCache;
102 /// Helper functions for getOrCreateType.
103 unsigned Checksum(const ObjCInterfaceDecl *InterfaceDecl);
104 llvm::DIType CreateType(const BuiltinType *Ty);
105 llvm::DIType CreateType(const ComplexType *Ty);
106 llvm::DIType CreateQualifiedType(QualType Ty, llvm::DIFile F);
107 llvm::DIType CreateType(const TypedefType *Ty, llvm::DIFile F);
108 llvm::DIType CreateType(const ObjCObjectPointerType *Ty,
110 llvm::DIType CreateType(const PointerType *Ty, llvm::DIFile F);
111 llvm::DIType CreateType(const BlockPointerType *Ty, llvm::DIFile F);
112 llvm::DIType CreateType(const FunctionType *Ty, llvm::DIFile F);
113 llvm::DIType CreateType(const RecordType *Ty);
114 llvm::DIType CreateLimitedType(const RecordType *Ty);
115 llvm::DIType CreateType(const ObjCInterfaceType *Ty, llvm::DIFile F);
116 llvm::DIType CreateType(const ObjCObjectType *Ty, llvm::DIFile F);
117 llvm::DIType CreateType(const VectorType *Ty, llvm::DIFile F);
118 llvm::DIType CreateType(const ArrayType *Ty, llvm::DIFile F);
119 llvm::DIType CreateType(const LValueReferenceType *Ty, llvm::DIFile F);
120 llvm::DIType CreateType(const RValueReferenceType *Ty, llvm::DIFile Unit);
121 llvm::DIType CreateType(const MemberPointerType *Ty, llvm::DIFile F);
122 llvm::DIType CreateType(const AtomicType *Ty, llvm::DIFile F);
123 llvm::DIType CreateEnumType(const EnumDecl *ED);
124 llvm::DIType CreateSelfType(const QualType &QualTy, llvm::DIType Ty);
125 llvm::DIType getTypeOrNull(const QualType);
126 llvm::DIType getCompletedTypeOrNull(const QualType);
127 llvm::DIType getOrCreateMethodType(const CXXMethodDecl *Method,
129 llvm::DIType getOrCreateInstanceMethodType(
130 QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile Unit);
131 llvm::DIType getOrCreateFunctionType(const Decl *D, QualType FnType,
133 llvm::DIType getOrCreateVTablePtrType(llvm::DIFile F);
134 llvm::DINameSpace getOrCreateNameSpace(const NamespaceDecl *N);
135 llvm::DIType CreatePointeeType(QualType PointeeTy, llvm::DIFile F);
136 llvm::DIType CreatePointerLikeType(unsigned Tag,
137 const Type *Ty, QualType PointeeTy,
140 llvm::Value *getCachedInterfaceTypeOrNull(const QualType Ty);
141 llvm::DIType getOrCreateStructPtrType(StringRef Name, llvm::DIType &Cache);
143 llvm::DISubprogram CreateCXXMemberFunction(const CXXMethodDecl *Method,
145 llvm::DIType RecordTy);
147 void CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
149 SmallVectorImpl<llvm::Value *> &E,
152 void CollectCXXFriends(const CXXRecordDecl *Decl,
154 SmallVectorImpl<llvm::Value *> &EltTys,
155 llvm::DIType RecordTy);
157 void CollectCXXBases(const CXXRecordDecl *Decl,
159 SmallVectorImpl<llvm::Value *> &EltTys,
160 llvm::DIType RecordTy);
163 CollectTemplateParams(const TemplateParameterList *TPList,
164 const TemplateArgumentList &TAList,
167 CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit);
169 CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
172 llvm::DIType createFieldType(StringRef name, QualType type,
173 uint64_t sizeInBitsOverride, SourceLocation loc,
174 AccessSpecifier AS, uint64_t offsetInBits,
176 llvm::DIDescriptor scope);
178 // Helpers for collecting fields of a record.
179 void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
180 SmallVectorImpl<llvm::Value *> &E,
181 llvm::DIType RecordTy);
182 void CollectRecordStaticField(const VarDecl *Var,
183 SmallVectorImpl<llvm::Value *> &E,
184 llvm::DIType RecordTy);
185 void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
187 SmallVectorImpl<llvm::Value *> &E,
188 llvm::DIType RecordTy);
189 void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile F,
190 SmallVectorImpl<llvm::Value *> &E,
191 llvm::DIType RecordTy);
193 void CollectVTableInfo(const CXXRecordDecl *Decl,
195 SmallVectorImpl<llvm::Value *> &EltTys);
197 // CreateLexicalBlock - Create a new lexical block node and push it on
199 void CreateLexicalBlock(SourceLocation Loc);
202 CGDebugInfo(CodeGenModule &CGM);
207 /// setLocation - Update the current source location. If \arg loc is
208 /// invalid it is ignored.
209 void setLocation(SourceLocation Loc);
211 /// getLocation - Return the current source location.
212 SourceLocation getLocation() const { return CurLoc; }
214 /// EmitLocation - Emit metadata to indicate a change in line/column
215 /// information in the source file.
216 /// \param ForceColumnInfo Assume DebugColumnInfo option is true.
217 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
218 bool ForceColumnInfo = false);
220 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
221 /// start of a new function.
222 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
223 llvm::Function *Fn, CGBuilderTy &Builder);
225 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
226 void EmitFunctionEnd(CGBuilderTy &Builder);
228 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
229 /// new lexical block and push the block onto the stack.
230 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
232 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
233 /// block and pop the current block.
234 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
236 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
237 /// variable declaration.
238 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
239 CGBuilderTy &Builder);
241 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
242 /// imported variable declaration in a block.
243 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
244 llvm::Value *storage,
245 CGBuilderTy &Builder,
246 const CGBlockInfo &blockInfo);
248 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
249 /// variable declaration.
250 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
251 unsigned ArgNo, CGBuilderTy &Builder);
253 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
254 /// llvm.dbg.declare for the block-literal argument to a block
255 /// invocation function.
256 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
258 llvm::Value *LocalAddr,
259 CGBuilderTy &Builder);
261 /// EmitGlobalVariable - Emit information about a global variable.
262 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
264 /// EmitGlobalVariable - Emit information about an objective-c interface.
265 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
267 /// EmitGlobalVariable - Emit global variable's debug info.
268 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
270 /// \brief - Emit C++ using directive.
271 void EmitUsingDirective(const UsingDirectiveDecl &UD);
273 /// getOrCreateRecordType - Emit record type's standalone debug info.
274 llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
276 /// getOrCreateInterfaceType - Emit an objective c interface type standalone
278 llvm::DIType getOrCreateInterfaceType(QualType Ty,
282 /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration.
283 void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI,
284 unsigned ArgNo, CGBuilderTy &Builder);
286 // EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
287 // See BuildByRefType.
288 llvm::DIType EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
291 /// getContextDescriptor - Get context info for the decl.
292 llvm::DIScope getContextDescriptor(const Decl *Decl);
294 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
296 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
298 /// createContextChain - Create a set of decls for the context chain.
299 llvm::DIDescriptor createContextChain(const Decl *Decl);
301 /// getCurrentDirname - Return current directory name.
302 StringRef getCurrentDirname();
304 /// CreateCompileUnit - Create new compile unit.
305 void CreateCompileUnit();
307 /// getOrCreateFile - Get the file debug info descriptor for the input
309 llvm::DIFile getOrCreateFile(SourceLocation Loc);
311 /// getOrCreateMainFile - Get the file info for main compile unit.
312 llvm::DIFile getOrCreateMainFile();
314 /// getOrCreateType - Get the type from the cache or create a new type if
316 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
318 /// getOrCreateLimitedType - Get the type from the cache or create a new
319 /// partial type if necessary.
320 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
322 /// CreateTypeNode - Create type metadata for a source language type.
323 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
325 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
326 /// if Ty is an ObjCInterface or a pointer to one.
327 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
329 /// CreateLimitedTypeNode - Create type metadata for a source language
330 /// type, but only partial types for records.
331 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
333 /// CreateMemberType - Create new member and increase Offset by FType's size.
334 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
335 StringRef Name, uint64_t *Offset);
337 /// getFunctionDeclaration - Return debug info descriptor to describe method
338 /// declaration for the given method definition.
339 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
341 /// getStaticDataMemberDeclaration - Return debug info descriptor to
342 /// describe in-class static data member declaration for the given
343 /// out-of-class definition.
344 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
346 /// getFunctionName - Get function name for the given FunctionDecl. If the
347 /// name is constructred on demand (e.g. C++ destructor) then the name
348 /// is stored on the side.
349 StringRef getFunctionName(const FunctionDecl *FD);
351 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
352 /// This is the display name for the debugging info.
353 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
355 /// getSelectorName - Return selector name. This is used for debugging
357 StringRef getSelectorName(Selector S);
359 /// getClassName - Get class name including template argument list.
360 StringRef getClassName(const RecordDecl *RD);
362 /// getVTableName - Get vtable name for the given Class.
363 StringRef getVTableName(const CXXRecordDecl *Decl);
365 /// getLineNumber - Get line number for the location. If location is invalid
366 /// then use current location.
367 unsigned getLineNumber(SourceLocation Loc);
369 /// getColumnNumber - Get column number for the location. If location is
370 /// invalid then use current location.
371 /// \param Force Assume DebugColumnInfo option is true.
372 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
374 } // namespace CodeGen