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 llvm::DenseMap<const Decl *, llvm::WeakVH> DeclCache;
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 /// EmitLocation - Emit metadata to indicate a change in line/column
212 /// information in the source file.
213 /// \param ForceColumnInfo Assume DebugColumnInfo option is true.
214 void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
215 bool ForceColumnInfo = false);
217 /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
218 /// start of a new function.
219 void EmitFunctionStart(GlobalDecl GD, QualType FnType,
220 llvm::Function *Fn, CGBuilderTy &Builder);
222 /// EmitFunctionEnd - Constructs the debug code for exiting a function.
223 void EmitFunctionEnd(CGBuilderTy &Builder);
225 /// EmitLexicalBlockStart - Emit metadata to indicate the beginning of a
226 /// new lexical block and push the block onto the stack.
227 void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
229 /// EmitLexicalBlockEnd - Emit metadata to indicate the end of a new lexical
230 /// block and pop the current block.
231 void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
233 /// EmitDeclareOfAutoVariable - Emit call to llvm.dbg.declare for an automatic
234 /// variable declaration.
235 void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
236 CGBuilderTy &Builder);
238 /// EmitDeclareOfBlockDeclRefVariable - Emit call to llvm.dbg.declare for an
239 /// imported variable declaration in a block.
240 void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
241 llvm::Value *storage,
242 CGBuilderTy &Builder,
243 const CGBlockInfo &blockInfo);
245 /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
246 /// variable declaration.
247 void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
248 unsigned ArgNo, CGBuilderTy &Builder);
250 /// EmitDeclareOfBlockLiteralArgVariable - Emit call to
251 /// llvm.dbg.declare for the block-literal argument to a block
252 /// invocation function.
253 void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
255 llvm::Value *LocalAddr,
256 CGBuilderTy &Builder);
258 /// EmitGlobalVariable - Emit information about a global variable.
259 void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
261 /// EmitGlobalVariable - Emit information about an objective-c interface.
262 void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
264 /// EmitGlobalVariable - Emit global variable's debug info.
265 void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
267 /// \brief - Emit C++ using directive.
268 void EmitUsingDirective(const UsingDirectiveDecl &UD);
270 /// \brief - Emit C++ using declaration.
271 void EmitUsingDecl(const UsingDecl &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 llvm::DIScope getCurrentContextDescriptor(const Decl *Decl);
296 /// createRecordFwdDecl - Create a forward decl for a RecordType in a given
298 llvm::DIType createRecordFwdDecl(const RecordDecl *, llvm::DIDescriptor);
300 /// createContextChain - Create a set of decls for the context chain.
301 llvm::DIDescriptor createContextChain(const Decl *Decl);
303 /// getCurrentDirname - Return current directory name.
304 StringRef getCurrentDirname();
306 /// CreateCompileUnit - Create new compile unit.
307 void CreateCompileUnit();
309 /// getOrCreateFile - Get the file debug info descriptor for the input
311 llvm::DIFile getOrCreateFile(SourceLocation Loc);
313 /// getOrCreateMainFile - Get the file info for main compile unit.
314 llvm::DIFile getOrCreateMainFile();
316 /// getOrCreateType - Get the type from the cache or create a new type if
318 llvm::DIType getOrCreateType(QualType Ty, llvm::DIFile F);
320 /// getOrCreateLimitedType - Get the type from the cache or create a new
321 /// partial type if necessary.
322 llvm::DIType getOrCreateLimitedType(QualType Ty, llvm::DIFile F);
324 /// CreateTypeNode - Create type metadata for a source language type.
325 llvm::DIType CreateTypeNode(QualType Ty, llvm::DIFile F);
327 /// getObjCInterfaceDecl - return the underlying ObjCInterfaceDecl
328 /// if Ty is an ObjCInterface or a pointer to one.
329 ObjCInterfaceDecl* getObjCInterfaceDecl(QualType Ty);
331 /// CreateLimitedTypeNode - Create type metadata for a source language
332 /// type, but only partial types for records.
333 llvm::DIType CreateLimitedTypeNode(QualType Ty, llvm::DIFile F);
335 /// CreateMemberType - Create new member and increase Offset by FType's size.
336 llvm::DIType CreateMemberType(llvm::DIFile Unit, QualType FType,
337 StringRef Name, uint64_t *Offset);
339 /// \brief Retrieve the DIDescriptor, if any, for the canonical form of this
341 llvm::DIDescriptor getDeclarationOrDefinition(const Decl *D);
343 /// getFunctionDeclaration - Return debug info descriptor to describe method
344 /// declaration for the given method definition.
345 llvm::DISubprogram getFunctionDeclaration(const Decl *D);
347 /// getStaticDataMemberDeclaration - Return debug info descriptor to
348 /// describe in-class static data member declaration for the given
349 /// out-of-class definition.
350 llvm::DIDerivedType getStaticDataMemberDeclaration(const Decl *D);
352 /// getFunctionName - Get function name for the given FunctionDecl. If the
353 /// name is constructred on demand (e.g. C++ destructor) then the name
354 /// is stored on the side.
355 StringRef getFunctionName(const FunctionDecl *FD);
357 /// getObjCMethodName - Returns the unmangled name of an Objective-C method.
358 /// This is the display name for the debugging info.
359 StringRef getObjCMethodName(const ObjCMethodDecl *FD);
361 /// getSelectorName - Return selector name. This is used for debugging
363 StringRef getSelectorName(Selector S);
365 /// getClassName - Get class name including template argument list.
366 StringRef getClassName(const RecordDecl *RD);
368 /// getVTableName - Get vtable name for the given Class.
369 StringRef getVTableName(const CXXRecordDecl *Decl);
371 /// getLineNumber - Get line number for the location. If location is invalid
372 /// then use current location.
373 unsigned getLineNumber(SourceLocation Loc);
375 /// getColumnNumber - Get column number for the location. If location is
376 /// invalid then use current location.
377 /// \param Force Assume DebugColumnInfo option is true.
378 unsigned getColumnNumber(SourceLocation Loc, bool Force=false);
380 } // namespace CodeGen