blob: cec39fd71a22524f56ef88c7aeae096312146bcb [file] [log] [blame]
Sanjiv Gupta40e56a12008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel3fc13e32009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattnere3afa462008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Module.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000027#include "llvm/ADT/StringExtras.h"
28#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000029#include "llvm/Support/Dwarf.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000031using namespace clang;
32using namespace clang::CodeGen;
33
34CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000035 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000036}
37
Chris Lattner562ce0a2008-11-10 06:08:34 +000038CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000039 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000040}
41
Chris Lattner562ce0a2008-11-10 06:08:34 +000042void CGDebugInfo::setLocation(SourceLocation Loc) {
43 if (Loc.isValid())
Chris Lattner18c8dc02009-01-16 07:36:28 +000044 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000045}
46
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000047/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000048/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000049llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000050 // FIXME: Until we do a complete job of emitting debug information,
51 // we need to support making dummy compile units so that we generate
52 // "well formed" debug info.
53 const FileEntry *FE = 0;
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000054
Devang Patel61910072009-02-24 23:16:03 +000055 SourceManager &SM = M->getContext().getSourceManager();
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000056 if (Loc.isValid()) {
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000057 Loc = SM.getInstantiationLoc(Loc);
58 FE = SM.getFileEntryForID(SM.getFileID(Loc));
Devang Patel61910072009-02-24 23:16:03 +000059 } else {
60 // If Loc is not valid then use main file id.
61 FE = SM.getFileEntryForID(SM.getMainFileID());
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000062 }
63
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000064 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000065 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
66 if (!Unit.isNull()) return Unit;
67
68 // Get source file information.
Daniel Dunbar67bb1fa2009-01-22 00:09:25 +000069 const char *FileName = FE ? FE->getName() : "<unknown>";
Daniel Dunbar2863cae2009-02-07 00:40:41 +000070 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>";
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000071
Devang Patel2b34d8f2009-03-05 01:55:07 +000072 bool isMain = (FE == SM.getFileEntryForID(SM.getMainFileID()));
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000073 // Create new compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +000074 // FIXME: Handle other language IDs as well.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000075 // FIXME: Do not know how to get clang version yet.
Chris Lattner562ce0a2008-11-10 06:08:34 +000076 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
Devang Patel2b34d8f2009-03-05 01:55:07 +000077 FileName, DirName, "clang",
78 isMain);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000079}
80
Devang Patela4cc0c42009-02-25 01:36:11 +000081/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner562ce0a2008-11-10 06:08:34 +000082/// one if necessary.
83llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patela4cc0c42009-02-25 01:36:11 +000084 llvm::DICompileUnit Unit) {
Chris Lattner562ce0a2008-11-10 06:08:34 +000085 unsigned Encoding = 0;
86 switch (BT->getKind()) {
87 default:
88 case BuiltinType::Void:
89 return llvm::DIType();
90 case BuiltinType::UChar:
91 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
92 case BuiltinType::Char_S:
93 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
94 case BuiltinType::UShort:
95 case BuiltinType::UInt:
96 case BuiltinType::ULong:
97 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
98 case BuiltinType::Short:
99 case BuiltinType::Int:
100 case BuiltinType::Long:
101 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
102 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
103 case BuiltinType::Float:
104 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
105 }
106 // Bit size, align and offset of the type.
107 uint64_t Size = M->getContext().getTypeSize(BT);
108 uint64_t Align = M->getContext().getTypeAlign(BT);
109 uint64_t Offset = 0;
110
111 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
112 Offset, /*flags*/ 0, Encoding);
113}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000114
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000115/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
116/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000117llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
118 // We will create one Derived type for one qualifier and recurse to handle any
119 // additional ones.
120 llvm::DIType FromTy;
121 unsigned Tag;
122 if (Ty.isConstQualified()) {
123 Tag = llvm::dwarf::DW_TAG_const_type;
124 Ty.removeConst();
125 FromTy = getOrCreateType(Ty, Unit);
126 } else if (Ty.isVolatileQualified()) {
127 Tag = llvm::dwarf::DW_TAG_volatile_type;
128 Ty.removeVolatile();
129 FromTy = getOrCreateType(Ty, Unit);
130 } else {
131 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
132 Tag = llvm::dwarf::DW_TAG_restrict_type;
133 Ty.removeRestrict();
134 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000135 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000136
Daniel Dunbar44252b42008-10-31 03:54:29 +0000137 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
138 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000139 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
140 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000141}
142
Chris Lattner562ce0a2008-11-10 06:08:34 +0000143llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
144 llvm::DICompileUnit Unit) {
145 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000146
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000147 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000148 uint64_t Size = M->getContext().getTypeSize(Ty);
149 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000150
Chris Lattner562ce0a2008-11-10 06:08:34 +0000151 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
152 "", llvm::DICompileUnit(),
153 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000154}
155
Chris Lattner562ce0a2008-11-10 06:08:34 +0000156llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
157 llvm::DICompileUnit Unit) {
158 // Typedefs are derived from some other type. If we have a typedef of a
159 // typedef, make sure to emit the whole chain.
160 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
161
162 // We don't set size information, but do specify where the typedef was
163 // declared.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000164 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000165 SourceLocation DefLoc = Ty->getDecl()->getLocation();
166 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000167
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000168 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000169 uint64_t Line = SM.getInstantiationLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000170
Chris Lattner562ce0a2008-11-10 06:08:34 +0000171 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
172 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000173}
174
Chris Lattner562ce0a2008-11-10 06:08:34 +0000175llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
176 llvm::DICompileUnit Unit) {
177 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000178
Chris Lattner562ce0a2008-11-10 06:08:34 +0000179 // Add the result type at least.
180 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
181
182 // Set up remainder of arguments if there is a prototype.
183 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor4fa58902009-02-26 23:50:07 +0000184 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000185 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
186 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
187 } else {
188 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000189 }
190
Chris Lattner562ce0a2008-11-10 06:08:34 +0000191 llvm::DIArray EltTypeArray =
192 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
193
194 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
195 Unit, "", llvm::DICompileUnit(),
196 0, 0, 0, 0, 0,
197 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000198}
199
Devang Patela4cc0c42009-02-25 01:36:11 +0000200/// CreateType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000201llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
202 llvm::DICompileUnit Unit) {
Douglas Gregor640a04b2008-12-11 17:59:21 +0000203 RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000204
Chris Lattner562ce0a2008-11-10 06:08:34 +0000205 unsigned Tag;
206 if (Decl->isStruct())
207 Tag = llvm::dwarf::DW_TAG_structure_type;
208 else if (Decl->isUnion())
209 Tag = llvm::dwarf::DW_TAG_union_type;
210 else {
211 assert(Decl->isClass() && "Unknown RecordType!");
212 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000213 }
214
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000215 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000216
Chris Lattner562ce0a2008-11-10 06:08:34 +0000217 // Get overall information about the record type for the debug info.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000218 std::string Name = Decl->getNameAsString();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000219
Chris Lattner562ce0a2008-11-10 06:08:34 +0000220 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Chris Lattner18c8dc02009-01-16 07:36:28 +0000221 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000222
223
224 // Records and classes and unions can all be recursive. To handle them, we
225 // first generate a debug descriptor for the struct as a forward declaration.
226 // Then (if it is a definition) we go through and get debug info for all of
227 // its members. Finally, we create a descriptor for the complete type (which
228 // may refer to the forward decl if the struct is recursive) and replace all
229 // uses of the forward declaration with the final definition.
230 llvm::DIType FwdDecl =
231 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
232 llvm::DIType(), llvm::DIArray());
233
234 // If this is just a forward declaration, return it.
235 if (!Decl->getDefinition(M->getContext()))
236 return FwdDecl;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000237
Chris Lattner562ce0a2008-11-10 06:08:34 +0000238 // Otherwise, insert it into the TypeCache so that recursive uses will find
239 // it.
240 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
241
242 // Convert all the elements.
243 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
244
245 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
246
247 unsigned FieldNo = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000248 for (RecordDecl::field_iterator I = Decl->field_begin(),
249 E = Decl->field_end();
250 I != E; ++I, ++FieldNo) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000251 FieldDecl *Field = *I;
252 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000253
254 std::string FieldName = Field->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000255
256 // Get the location for the field.
257 SourceLocation FieldDefLoc = Field->getLocation();
258 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000259 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
Devang Patelb3a08ba2009-03-16 23:47:53 +0000260
261 QualType FType = Field->getType();
262 uint64_t FieldSize = 0;
263 unsigned FieldAlign = 0;
264 if (!FType->isIncompleteArrayType()) {
Chris Lattner562ce0a2008-11-10 06:08:34 +0000265
Devang Patelb3a08ba2009-03-16 23:47:53 +0000266 // Bit size, align and offset of the type.
267 FieldSize = M->getContext().getTypeSize(FType);
268 Expr *BitWidth = Field->getBitWidth();
269 if (BitWidth)
270 FieldSize =
271 BitWidth->getIntegerConstantExprValue(M->getContext()).getZExtValue();
272
273 FieldAlign = M->getContext().getTypeAlign(FType);
274 }
275
Chris Lattner562ce0a2008-11-10 06:08:34 +0000276 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
277
278 // Create a DW_TAG_member node to remember the offset of this field in the
279 // struct. FIXME: This is an absolutely insane way to capture this
280 // information. When we gut debug info, this should be fixed.
281 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
282 FieldName, FieldDefUnit,
283 FieldLine, FieldSize, FieldAlign,
284 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000285 EltTys.push_back(FieldTy);
286 }
287
288 llvm::DIArray Elements =
289 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
290
291 // Bit size, align and offset of the type.
292 uint64_t Size = M->getContext().getTypeSize(Ty);
293 uint64_t Align = M->getContext().getTypeAlign(Ty);
294
295 llvm::DIType RealDecl =
296 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
297 Align, 0, 0, llvm::DIType(), Elements);
298
299 // Now that we have a real decl for the struct, replace anything using the
300 // old decl with the new one. This will recursively update the debug info.
301 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
302 FwdDecl.getGV()->eraseFromParent();
303
304 return RealDecl;
305}
306
Devang Patel3fc13e32009-02-26 21:10:26 +0000307/// CreateType - get objective-c interface type.
308llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
309 llvm::DICompileUnit Unit) {
310 ObjCInterfaceDecl *Decl = Ty->getDecl();
311
312 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
313 SourceManager &SM = M->getContext().getSourceManager();
314
315 // Get overall information about the record type for the debug info.
316 std::string Name = Decl->getNameAsString();
317
318 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
319 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
320
321
322 // To handle recursive interface, we
323 // first generate a debug descriptor for the struct as a forward declaration.
324 // Then (if it is a definition) we go through and get debug info for all of
325 // its members. Finally, we create a descriptor for the complete type (which
326 // may refer to the forward decl if the struct is recursive) and replace all
327 // uses of the forward declaration with the final definition.
328 llvm::DIType FwdDecl =
329 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
330 llvm::DIType(), llvm::DIArray());
331
332 // If this is just a forward declaration, return it.
333 if (Decl->isForwardDecl())
334 return FwdDecl;
335
336 // Otherwise, insert it into the TypeCache so that recursive uses will find
337 // it.
338 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
339
340 // Convert all the elements.
341 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
342
Devang Patele6178df2009-03-10 21:30:26 +0000343 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
344 if (SClass) {
345 llvm::DIType SClassTy =
346 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
347 llvm::DIType InhTag =
348 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
349 Unit, "", Unit, 0, 0, 0,
350 0 /* offset */, 0, SClassTy);
351 EltTys.push_back(InhTag);
352 }
353
Devang Patel3fc13e32009-02-26 21:10:26 +0000354 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
355
356 unsigned FieldNo = 0;
357 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
358 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
359 ObjCIvarDecl *Field = *I;
360 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
361
362 std::string FieldName = Field->getNameAsString();
363
364 // Get the location for the field.
365 SourceLocation FieldDefLoc = Field->getLocation();
366 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
367 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc);
368
369 // Bit size, align and offset of the type.
370 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
371 unsigned FieldAlign = M->getContext().getTypeAlign(Ty);
372 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
373
374 // Create a DW_TAG_member node to remember the offset of this field in the
375 // struct. FIXME: This is an absolutely insane way to capture this
376 // information. When we gut debug info, this should be fixed.
377 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
378 FieldName, FieldDefUnit,
379 FieldLine, FieldSize, FieldAlign,
380 FieldOffset, 0, FieldTy);
381 EltTys.push_back(FieldTy);
382 }
383
384 llvm::DIArray Elements =
385 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
386
387 // Bit size, align and offset of the type.
388 uint64_t Size = M->getContext().getTypeSize(Ty);
389 uint64_t Align = M->getContext().getTypeAlign(Ty);
390
391 llvm::DIType RealDecl =
392 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
393 Align, 0, 0, llvm::DIType(), Elements);
394
395 // Now that we have a real decl for the struct, replace anything using the
396 // old decl with the new one. This will recursively update the debug info.
397 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
398 FwdDecl.getGV()->eraseFromParent();
399
400 return RealDecl;
401}
402
Chris Lattner562ce0a2008-11-10 06:08:34 +0000403llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
404 llvm::DICompileUnit Unit) {
405 EnumDecl *Decl = Ty->getDecl();
406
407 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
408
409 // Create DIEnumerator elements for each enumerator.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000410 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
411 EnumEnd = Decl->enumerator_end();
412 Enum != EnumEnd; ++Enum) {
413 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
414 Enum->getInitVal().getZExtValue()));
Chris Lattner562ce0a2008-11-10 06:08:34 +0000415 }
416
417 // Return a CompositeType for the enum itself.
418 llvm::DIArray EltArray =
419 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
420
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000421 std::string EnumName = Decl->getNameAsString();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000422 SourceLocation DefLoc = Decl->getLocation();
423 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
424 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000425 unsigned Line = SM.getInstantiationLineNumber(DefLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000426
427 // Size and align of the type.
428 uint64_t Size = M->getContext().getTypeSize(Ty);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000429 unsigned Align = M->getContext().getTypeAlign(Ty);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000430
431 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
432 Unit, EnumName, DefUnit, Line,
433 Size, Align, 0, 0,
434 llvm::DIType(), EltArray);
435}
436
437llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
438 llvm::DICompileUnit Unit) {
439 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
440 return CreateType(RT, Unit);
441 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
442 return CreateType(ET, Unit);
443
444 return llvm::DIType();
445}
446
447llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
448 llvm::DICompileUnit Unit) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000449 uint64_t Size;
450 uint64_t Align;
451
452
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000453 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson86b360e2009-01-05 01:23:29 +0000454 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson86b360e2009-01-05 01:23:29 +0000455 Size = 0;
456 Align =
Nuno Lopes15bc9c32009-01-28 00:35:17 +0000457 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
458 } else if (Ty->isIncompleteArrayType()) {
459 Size = 0;
460 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson86b360e2009-01-05 01:23:29 +0000461 } else {
462 // Size and align of the whole array, not the element type.
463 Size = M->getContext().getTypeSize(Ty);
464 Align = M->getContext().getTypeAlign(Ty);
465 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000466
467 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
468 // interior arrays, do we care? Why aren't nested arrays represented the
469 // obvious/recursive way?
470 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
471 QualType EltTy(Ty, 0);
472 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000473 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000474 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
475 Upper = CAT->getSize().getZExtValue() - 1;
476 // FIXME: Verify this is right for VLAs.
477 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
478 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000479 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000480
Chris Lattner562ce0a2008-11-10 06:08:34 +0000481 llvm::DIArray SubscriptArray =
482 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
483
484 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
485 Unit, "", llvm::DICompileUnit(),
486 0, Size, Align, 0, 0,
487 getOrCreateType(EltTy, Unit),
488 SubscriptArray);
489}
490
491
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000492/// getOrCreateType - Get the type from the cache or create a new
493/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000494llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
495 llvm::DICompileUnit Unit) {
496 if (Ty.isNull())
497 return llvm::DIType();
498
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000499 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000500 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
501 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000502
Chris Lattner562ce0a2008-11-10 06:08:34 +0000503 // Handle CVR qualifiers, which recursively handles what they refer to.
504 if (Ty.getCVRQualifiers())
505 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000506
507 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000508 switch (Ty->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000509#define TYPE(Class, Base)
510#define ABSTRACT_TYPE(Class, Base)
511#define NON_CANONICAL_TYPE(Class, Base)
512#define DEPENDENT_TYPE(Class, Base) case Type::Class:
513#include "clang/AST/TypeNodes.def"
514 assert(false && "Dependent types cannot show up in debug information");
515
Chris Lattner562ce0a2008-11-10 06:08:34 +0000516 case Type::Complex:
Sebastian Redlce6fff02009-03-16 23:22:08 +0000517 case Type::LValueReference:
518 case Type::RValueReference:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000519 case Type::Vector:
520 case Type::ExtVector:
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000521 case Type::ExtQual:
Chris Lattner562ce0a2008-11-10 06:08:34 +0000522 case Type::ObjCQualifiedInterface:
523 case Type::ObjCQualifiedId:
Eli Friedman673e91b2009-02-27 23:15:07 +0000524 case Type::FixedWidthInt:
525 case Type::BlockPointer:
526 case Type::MemberPointer:
527 case Type::ClassTemplateSpecialization:
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000528 case Type::QualifiedName:
Eli Friedman673e91b2009-02-27 23:15:07 +0000529 case Type::ObjCQualifiedClass:
530 // Unsupported types
Chris Lattner562ce0a2008-11-10 06:08:34 +0000531 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000532
Devang Patel5a1ff3b2009-03-02 17:58:28 +0000533 case Type::ObjCInterface:
534 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000535 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
536 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000537 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
538 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000539 case Type::Enum:
540 Slot = CreateType(cast<TagType>(Ty), Unit);
541 break;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000542 case Type::FunctionProto:
543 case Type::FunctionNoProto:
Chris Lattnere3afa462008-11-11 07:01:36 +0000544 return Slot = CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000545
546 case Type::ConstantArray:
547 case Type::VariableArray:
548 case Type::IncompleteArray:
Chris Lattnere3afa462008-11-11 07:01:36 +0000549 return Slot = CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000550 case Type::TypeOfExpr:
551 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
Chris Lattnere3afa462008-11-11 07:01:36 +0000552 ->getType(), Unit);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000553 case Type::TypeOf:
Chris Lattnere3afa462008-11-11 07:01:36 +0000554 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(),
555 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000556 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000557
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000558 return Slot;
559}
560
561/// EmitFunctionStart - Constructs the debug code for entering a function -
562/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000563void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000564 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000565 CGBuilderTy &Builder) {
566 // FIXME: Why is this using CurLoc???
567 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000568 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000569 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000570
571 llvm::DISubprogram SP =
572 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
573 getOrCreateType(ReturnType, Unit),
574 Fn->hasInternalLinkage(), true/*definition*/);
575
576 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
577
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000578 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000579 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000580}
581
582
Chris Lattner562ce0a2008-11-10 06:08:34 +0000583void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000584 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
585
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000586 // Don't bother if things are the same as last time.
587 SourceManager &SM = M->getContext().getSourceManager();
588 if (CurLoc == PrevLoc
Chris Lattner2d89c562009-02-04 01:06:56 +0000589 || (SM.getInstantiationLineNumber(CurLoc) ==
590 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000591 && SM.isFromSameFile(CurLoc, PrevLoc)))
592 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000593
594 // Update last state.
595 PrevLoc = CurLoc;
596
597 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000598 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000599 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc),
600 SM.getInstantiationColumnNumber(CurLoc),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000601 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000602}
603
604/// EmitRegionStart- Constructs the debug code for entering a declarative
605/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000606void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
607 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000608 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000609 D = RegionStack.back();
610 D = DebugFactory.CreateBlock(D);
611 RegionStack.push_back(D);
612 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000613}
614
615/// EmitRegionEnd - Constructs the debug code for exiting a declarative
616/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000617void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000618 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
619
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000620 // Provide an region stop point.
621 EmitStopPoint(Fn, Builder);
622
Chris Lattner562ce0a2008-11-10 06:08:34 +0000623 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000624 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000625}
626
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000627/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000628void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
629 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000630 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
631
Chris Lattner562ce0a2008-11-10 06:08:34 +0000632 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000633 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000634 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000635 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
636
637 // Create the descriptor for the variable.
638 llvm::DIVariable D =
Chris Lattner271d4c22008-11-24 05:29:24 +0000639 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000640 Unit, Line,
641 getOrCreateType(Decl->getType(), Unit));
642 // Insert an llvm.dbg.declare into the current block.
643 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000644}
645
Chris Lattner562ce0a2008-11-10 06:08:34 +0000646void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
647 llvm::Value *Storage,
648 CGBuilderTy &Builder) {
649 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
650}
651
652/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
653/// variable declaration.
654void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
655 CGBuilderTy &Builder) {
656 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
657}
658
659
660
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000661/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000662void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
663 const VarDecl *Decl) {
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000664 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000665 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000666 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000667 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000668
669 std::string Name = Decl->getNameAsString();
Anders Carlsson123485c2008-11-26 17:40:42 +0000670
671 QualType T = Decl->getType();
672 if (T->isIncompleteArrayType()) {
673
674 // CodeGen turns int[] into int[1] so we'll do the same here.
675 llvm::APSInt ConstVal(32);
676
677 ConstVal = 1;
678 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
679
680 T = M->getContext().getConstantArrayType(ET, ConstVal,
681 ArrayType::Normal, 0);
682 }
683
Chris Lattner562ce0a2008-11-10 06:08:34 +0000684 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson123485c2008-11-26 17:40:42 +0000685 getOrCreateType(T, Unit),
Chris Lattner562ce0a2008-11-10 06:08:34 +0000686 Var->hasInternalLinkage(),
687 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000688}
689
Devang Patel3fc13e32009-02-26 21:10:26 +0000690/// EmitGlobalVariable - Emit information about an objective-c interface.
691void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
692 ObjCInterfaceDecl *Decl) {
693 // Create global variable debug descriptor.
694 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
695 SourceManager &SM = M->getContext().getSourceManager();
696 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation());
697
698 std::string Name = Decl->getNameAsString();
699
700 QualType T = M->getContext().buildObjCInterfaceType(Decl);
701 if (T->isIncompleteArrayType()) {
702
703 // CodeGen turns int[] into int[1] so we'll do the same here.
704 llvm::APSInt ConstVal(32);
705
706 ConstVal = 1;
707 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
708
709 T = M->getContext().getConstantArrayType(ET, ConstVal,
710 ArrayType::Normal, 0);
711 }
712
713 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
714 getOrCreateType(T, Unit),
715 Var->hasInternalLinkage(),
716 true/*definition*/, Var);
717}
718