blob: c772a006f5d301aa453ab0e3aa0d33b3b0e84dc3 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-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"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Devang Patel07739032009-03-27 23:16:32 +000024#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Devang Patel6dba4322009-07-14 21:31:22 +000038CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Mike Stump9bc093c2009-05-14 02:03:51 +000039 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
40 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000049 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel979ec2e2009-10-06 00:35:31 +000052/// getContext - Get context info for the decl.
53llvm::DIDescriptor CGDebugInfo::getContext(const VarDecl *Decl,
54 llvm::DIDescriptor &CompileUnit) {
55 if (Decl->isFileVarDecl())
56 return CompileUnit;
57 if (Decl->getDeclContext()->isFunctionOrMethod()) {
58 // Find the last subprogram in region stack.
59 for (unsigned RI = RegionStack.size(), RE = 0; RI != RE; --RI) {
60 llvm::DIDescriptor R = RegionStack[RI - 1];
61 if (R.isSubprogram())
62 return R;
63 }
64 }
65 return CompileUnit;
66}
67
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000068/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000069/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000070llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000071 // Get source file information.
72 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000073 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000074 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000075 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000076 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
77 FileName = PLoc.getFilename();
78 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000079 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000082 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000083 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000084
Devang Patel446c6192009-04-17 21:06:59 +000085 // Get absolute path name.
86 llvm::sys::Path AbsFileName(FileName);
87 if (!AbsFileName.isAbsolute()) {
88 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
89 tmp.appendComponent(FileName);
90 AbsFileName = tmp;
91 }
92
Devang Patel72240d72009-06-26 18:32:22 +000093 // See if thie compile unit is representing main source file. Each source
94 // file has corresponding compile unit. There is only one main source
95 // file at a time.
96 bool isMain = false;
97 const LangOptions &LO = M->getLangOptions();
98 const char *MainFileName = LO.getMainFileName();
99 if (isMainCompileUnitCreated == false) {
100 if (MainFileName) {
101 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
102 isMain = true;
103 } else {
104 if (Loc.isValid() && SM.isFromMainFile(Loc))
105 isMain = true;
106 }
107 if (isMain)
108 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000109 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000110
Chris Lattner515455a2009-03-25 03:28:08 +0000111 unsigned LangTag;
112 if (LO.CPlusPlus) {
113 if (LO.ObjC1)
114 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
115 else
116 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
117 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000118 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000119 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000120 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000121 } else {
122 LangTag = llvm::dwarf::DW_LANG_C89;
123 }
Devang Patel446c6192009-04-17 21:06:59 +0000124
Mike Stumpd8945d62009-10-09 18:38:12 +0000125 std::string Producer =
126#ifdef CLANG_VENDOR
127 CLANG_VENDOR
128#endif
129 "clang " CLANG_VERSION_STRING;
Chris Lattnerb95ee582009-05-02 01:04:13 +0000130 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000131 const char *Flags = ""; // FIXME: Encode command line options.
132
133 // Figure out which version of the ObjC runtime we have.
134 unsigned RuntimeVers = 0;
135 if (LO.ObjC1)
136 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000138 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000139 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
Mike Stump1eb44332009-09-09 15:08:12 +0000140 AbsFileName.getDirname(),
Devang Patel72240d72009-06-26 18:32:22 +0000141 Producer, isMain, isOptimized,
142 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000143}
144
Devang Patel65e99f22009-02-25 01:36:11 +0000145/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146/// one if necessary.
147llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000148 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000149 unsigned Encoding = 0;
150 switch (BT->getKind()) {
151 default:
152 case BuiltinType::Void:
153 return llvm::DIType();
154 case BuiltinType::UChar:
155 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
156 case BuiltinType::Char_S:
157 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
158 case BuiltinType::UShort:
159 case BuiltinType::UInt:
160 case BuiltinType::ULong:
161 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
162 case BuiltinType::Short:
163 case BuiltinType::Int:
164 case BuiltinType::Long:
165 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
166 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
167 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000168 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000170 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171 // Bit size, align and offset of the type.
172 uint64_t Size = M->getContext().getTypeSize(BT);
173 uint64_t Align = M->getContext().getTypeAlign(BT);
174 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Devang Patelca80a5f2009-10-20 19:55:01 +0000176 llvm::DIType DbgTy =
177 DebugFactory.CreateBasicType(Unit,
178 BT->getName(M->getContext().getLangOptions()),
179 Unit, 0, Size, Align,
180 Offset, /*flags*/ 0, Encoding);
181
182 TypeCache[QualType(BT, 0).getAsOpaquePtr()] = DbgTy.getNode();
183 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000184}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000185
Chris Lattnerb7003772009-04-23 06:13:01 +0000186llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
187 llvm::DICompileUnit Unit) {
188 // Bit size, align and offset of the type.
189 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
190 if (Ty->isComplexIntegerType())
191 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnerb7003772009-04-23 06:13:01 +0000193 uint64_t Size = M->getContext().getTypeSize(Ty);
194 uint64_t Align = M->getContext().getTypeAlign(Ty);
195 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Devang Patelca80a5f2009-10-20 19:55:01 +0000197 llvm::DIType DbgTy =
198 DebugFactory.CreateBasicType(Unit, "complex",
199 Unit, 0, Size, Align,
200 Offset, /*flags*/ 0, Encoding);
201 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
202 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000203}
204
John McCalla1805292009-09-25 01:40:47 +0000205/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000206/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000207llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
208 QualifierCollector Qc;
209 const Type *T = Qc.strip(Ty);
210
211 // Ignore these qualifiers for now.
212 Qc.removeObjCGCAttr();
213 Qc.removeAddressSpace();
214
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 // We will create one Derived type for one qualifier and recurse to handle any
216 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000217 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000218 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000220 Qc.removeConst();
221 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000223 Qc.removeVolatile();
224 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000226 Qc.removeRestrict();
227 } else {
228 assert(Qc.empty() && "Unknown type qualifier for debug info");
229 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
John McCalla1805292009-09-25 01:40:47 +0000232 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
233
Daniel Dunbar3845f862008-10-31 03:54:29 +0000234 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
235 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000236 llvm::DIType DbgTy =
237 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
238 0, 0, 0, 0, 0, FromTy);
239 TypeCache[Ty.getAsOpaquePtr()] = DbgTy.getNode();
240 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000241}
242
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000243llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
244 llvm::DICompileUnit Unit) {
245 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000247 // Bit size, align and offset of the type.
248 uint64_t Size = M->getContext().getTypeSize(Ty);
249 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Devang Patelca80a5f2009-10-20 19:55:01 +0000251 llvm::DIType DbgTy =
252 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
253 "", llvm::DICompileUnit(),
254 0, Size, Align, 0, 0, EltTy);
255 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
256 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000257}
258
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
260 llvm::DICompileUnit Unit) {
261 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000263 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000264 uint64_t Size = M->getContext().getTypeSize(Ty);
265 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Devang Patelca80a5f2009-10-20 19:55:01 +0000267 return
268 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
269 "", llvm::DICompileUnit(),
270 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000271}
272
Mike Stump9bc093c2009-05-14 02:03:51 +0000273llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
274 llvm::DICompileUnit Unit) {
275 if (BlockLiteralGenericSet)
276 return BlockLiteralGeneric;
277
278 llvm::DICompileUnit DefUnit;
279 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
280
281 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
282
283 llvm::DIType FieldTy;
284
285 QualType FType;
286 uint64_t FieldSize, FieldOffset;
287 unsigned FieldAlign;
288
289 llvm::DIArray Elements;
290 llvm::DIType EltTy, DescTy;
291
292 FieldOffset = 0;
293 FType = M->getContext().UnsignedLongTy;
294 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
295 FieldSize = M->getContext().getTypeSize(FType);
296 FieldAlign = M->getContext().getTypeAlign(FType);
297 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
298 "reserved", DefUnit,
299 0, FieldSize, FieldAlign,
300 FieldOffset, 0, FieldTy);
301 EltTys.push_back(FieldTy);
302
303 FieldOffset += FieldSize;
304 FType = M->getContext().UnsignedLongTy;
305 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
306 FieldSize = M->getContext().getTypeSize(FType);
307 FieldAlign = M->getContext().getTypeAlign(FType);
308 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
309 "Size", DefUnit,
310 0, FieldSize, FieldAlign,
311 FieldOffset, 0, FieldTy);
312 EltTys.push_back(FieldTy);
313
314 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000315 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000316 EltTys.clear();
317
Mike Stump3d363c52009-10-02 02:30:50 +0000318 unsigned Flags = llvm::DIType::FlagAppleBlock;
319
Mike Stump9bc093c2009-05-14 02:03:51 +0000320 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000321 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000322 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Mike Stump9bc093c2009-05-14 02:03:51 +0000324 // Bit size, align and offset of the type.
325 uint64_t Size = M->getContext().getTypeSize(Ty);
326 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Mike Stump9bc093c2009-05-14 02:03:51 +0000328 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
329 Unit, "", llvm::DICompileUnit(),
330 0, Size, Align, 0, 0, EltTy);
331
332 FieldOffset = 0;
333 FType = M->getContext().getPointerType(M->getContext().VoidTy);
334 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
335 FieldSize = M->getContext().getTypeSize(FType);
336 FieldAlign = M->getContext().getTypeAlign(FType);
337 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
338 "__isa", DefUnit,
339 0, FieldSize, FieldAlign,
340 FieldOffset, 0, FieldTy);
341 EltTys.push_back(FieldTy);
342
343 FieldOffset += FieldSize;
344 FType = M->getContext().IntTy;
345 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
346 FieldSize = M->getContext().getTypeSize(FType);
347 FieldAlign = M->getContext().getTypeAlign(FType);
348 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
349 "__flags", DefUnit,
350 0, FieldSize, FieldAlign,
351 FieldOffset, 0, FieldTy);
352 EltTys.push_back(FieldTy);
353
354 FieldOffset += FieldSize;
355 FType = M->getContext().IntTy;
356 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
357 FieldSize = M->getContext().getTypeSize(FType);
358 FieldAlign = M->getContext().getTypeAlign(FType);
359 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
360 "__reserved", DefUnit,
361 0, FieldSize, FieldAlign,
362 FieldOffset, 0, FieldTy);
363 EltTys.push_back(FieldTy);
364
365 FieldOffset += FieldSize;
366 FType = M->getContext().getPointerType(M->getContext().VoidTy);
367 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
368 FieldSize = M->getContext().getTypeSize(FType);
369 FieldAlign = M->getContext().getTypeAlign(FType);
370 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
371 "__FuncPtr", DefUnit,
372 0, FieldSize, FieldAlign,
373 FieldOffset, 0, FieldTy);
374 EltTys.push_back(FieldTy);
375
376 FieldOffset += FieldSize;
377 FType = M->getContext().getPointerType(M->getContext().VoidTy);
378 FieldTy = DescTy;
379 FieldSize = M->getContext().getTypeSize(Ty);
380 FieldAlign = M->getContext().getTypeAlign(Ty);
381 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
382 "__descriptor", DefUnit,
383 0, FieldSize, FieldAlign,
384 FieldOffset, 0, FieldTy);
385 EltTys.push_back(FieldTy);
386
387 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000388 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000389
390 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000391 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000392 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 BlockLiteralGenericSet = true;
395 BlockLiteralGeneric
396 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
397 "", llvm::DICompileUnit(),
398 0, Size, Align, 0, 0, EltTy);
399 return BlockLiteralGeneric;
400}
401
Chris Lattner9c85ba32008-11-10 06:08:34 +0000402llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
403 llvm::DICompileUnit Unit) {
404 // Typedefs are derived from some other type. If we have a typedef of a
405 // typedef, make sure to emit the whole chain.
406 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner9c85ba32008-11-10 06:08:34 +0000408 // We don't set size information, but do specify where the typedef was
409 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000410 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411 SourceLocation DefLoc = Ty->getDecl()->getLocation();
412 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000413
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000414 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000415 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
416 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000417
Devang Patelca80a5f2009-10-20 19:55:01 +0000418 llvm::DIType DbgTy =
419 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
420 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
421 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
422 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000423}
424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
426 llvm::DICompileUnit Unit) {
427 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Add the result type at least.
430 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432 // Set up remainder of arguments if there is a prototype.
433 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000434 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
436 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
437 } else {
438 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000439 }
440
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000442 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Devang Patelca80a5f2009-10-20 19:55:01 +0000444 llvm::DIType DbgTy =
445 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
446 Unit, "", llvm::DICompileUnit(),
447 0, 0, 0, 0, 0,
448 llvm::DIType(), EltTypeArray);
449 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
450 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000451}
452
Devang Patel65e99f22009-02-25 01:36:11 +0000453/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
455 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000456 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner9c85ba32008-11-10 06:08:34 +0000458 unsigned Tag;
459 if (Decl->isStruct())
460 Tag = llvm::dwarf::DW_TAG_structure_type;
461 else if (Decl->isUnion())
462 Tag = llvm::dwarf::DW_TAG_union_type;
463 else {
464 assert(Decl->isClass() && "Unknown RecordType!");
465 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000466 }
467
Sanjiv Gupta507de852008-06-09 10:47:41 +0000468 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000469
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000471 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000472
Devang Patel4f6fa232009-04-17 21:35:15 +0000473 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000474 llvm::DICompileUnit DefUnit;
475 unsigned Line = 0;
476 if (!PLoc.isInvalid()) {
477 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
478 Line = PLoc.getLine();
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner9c85ba32008-11-10 06:08:34 +0000481 // Records and classes and unions can all be recursive. To handle them, we
482 // first generate a debug descriptor for the struct as a forward declaration.
483 // Then (if it is a definition) we go through and get debug info for all of
484 // its members. Finally, we create a descriptor for the complete type (which
485 // may refer to the forward decl if the struct is recursive) and replace all
486 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000487 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000488 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
489 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner9c85ba32008-11-10 06:08:34 +0000491 // If this is just a forward declaration, return it.
492 if (!Decl->getDefinition(M->getContext()))
493 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000494
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495 // Otherwise, insert it into the TypeCache so that recursive uses will find
496 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000497 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498
499 // Convert all the elements.
500 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
501
502 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
503
504 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000505 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000506 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000507 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 FieldDecl *Field = *I;
509 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000510
511 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000512
Devang Patelde135022009-04-27 22:40:36 +0000513 // Ignore unnamed fields.
514 if (FieldName.empty())
515 continue;
516
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517 // Get the location for the field.
518 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000519 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000520 llvm::DICompileUnit FieldDefUnit;
521 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000523 if (!PLoc.isInvalid()) {
524 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
525 FieldLine = PLoc.getLine();
526 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000527
528 QualType FType = Field->getType();
529 uint64_t FieldSize = 0;
530 unsigned FieldAlign = 0;
531 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Devang Patelec9b5d52009-03-16 23:47:53 +0000533 // Bit size, align and offset of the type.
534 FieldSize = M->getContext().getTypeSize(FType);
535 Expr *BitWidth = Field->getBitWidth();
536 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000537 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Devang Patelec9b5d52009-03-16 23:47:53 +0000539 FieldAlign = M->getContext().getTypeAlign(FType);
540 }
541
Mike Stump1eb44332009-09-09 15:08:12 +0000542 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
543
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544 // Create a DW_TAG_member node to remember the offset of this field in the
545 // struct. FIXME: This is an absolutely insane way to capture this
546 // information. When we gut debug info, this should be fixed.
547 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
548 FieldName, FieldDefUnit,
549 FieldLine, FieldSize, FieldAlign,
550 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551 EltTys.push_back(FieldTy);
552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattner9c85ba32008-11-10 06:08:34 +0000554 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000555 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556
557 // Bit size, align and offset of the type.
558 uint64_t Size = M->getContext().getTypeSize(Ty);
559 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Devang Patel0ce73f62009-07-22 18:57:00 +0000561 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000562 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
563 Align, 0, 0, llvm::DIType(), Elements);
564
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000565 // Update TypeCache.
566 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
567
Chris Lattner9c85ba32008-11-10 06:08:34 +0000568 // Now that we have a real decl for the struct, replace anything using the
569 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000570 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572 return RealDecl;
573}
574
Devang Patel9ca36b62009-02-26 21:10:26 +0000575/// CreateType - get objective-c interface type.
576llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
577 llvm::DICompileUnit Unit) {
578 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Devang Patel9ca36b62009-02-26 21:10:26 +0000580 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
581 SourceManager &SM = M->getContext().getSourceManager();
582
583 // Get overall information about the record type for the debug info.
584 std::string Name = Decl->getNameAsString();
585
586 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000587 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
588 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
589
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Daniel Dunbard86d3362009-05-18 20:51:58 +0000591 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000592
Devang Patel9ca36b62009-02-26 21:10:26 +0000593 // To handle recursive interface, we
594 // first generate a debug descriptor for the struct as a forward declaration.
595 // Then (if it is a definition) we go through and get debug info for all of
596 // its members. Finally, we create a descriptor for the complete type (which
597 // may refer to the forward decl if the struct is recursive) and replace all
598 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000599 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000600 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000601 llvm::DIType(), llvm::DIArray(),
602 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Devang Patel9ca36b62009-02-26 21:10:26 +0000604 // If this is just a forward declaration, return it.
605 if (Decl->isForwardDecl())
606 return FwdDecl;
607
608 // Otherwise, insert it into the TypeCache so that recursive uses will find
609 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000610 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000611
612 // Convert all the elements.
613 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
614
Devang Patelfbe899f2009-03-10 21:30:26 +0000615 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
616 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000617 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000618 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000619 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000620 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000621 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000622 0 /* offset */, 0, SClassTy);
623 EltTys.push_back(InhTag);
624 }
625
Devang Patel9ca36b62009-02-26 21:10:26 +0000626 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
627
628 unsigned FieldNo = 0;
629 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
630 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
631 ObjCIvarDecl *Field = *I;
632 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
633
634 std::string FieldName = Field->getNameAsString();
635
Devang Patelde135022009-04-27 22:40:36 +0000636 // Ignore unnamed fields.
637 if (FieldName.empty())
638 continue;
639
Devang Patel9ca36b62009-02-26 21:10:26 +0000640 // Get the location for the field.
641 SourceLocation FieldDefLoc = Field->getLocation();
642 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000643 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
644 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
645
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Devang Patel99c20eb2009-03-20 18:24:39 +0000647 QualType FType = Field->getType();
648 uint64_t FieldSize = 0;
649 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000650
Devang Patel99c20eb2009-03-20 18:24:39 +0000651 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Devang Patel99c20eb2009-03-20 18:24:39 +0000653 // Bit size, align and offset of the type.
654 FieldSize = M->getContext().getTypeSize(FType);
655 Expr *BitWidth = Field->getBitWidth();
656 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000657 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
658
Devang Patel99c20eb2009-03-20 18:24:39 +0000659 FieldAlign = M->getContext().getTypeAlign(FType);
660 }
661
Mike Stump1eb44332009-09-09 15:08:12 +0000662 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
663
Devang Patelc20482b2009-03-19 00:23:53 +0000664 unsigned Flags = 0;
665 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
666 Flags = llvm::DIType::FlagProtected;
667 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
668 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Devang Patel9ca36b62009-02-26 21:10:26 +0000670 // Create a DW_TAG_member node to remember the offset of this field in the
671 // struct. FIXME: This is an absolutely insane way to capture this
672 // information. When we gut debug info, this should be fixed.
673 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
674 FieldName, FieldDefUnit,
675 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000676 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000677 EltTys.push_back(FieldTy);
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Devang Patel9ca36b62009-02-26 21:10:26 +0000680 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000681 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000682
683 // Bit size, align and offset of the type.
684 uint64_t Size = M->getContext().getTypeSize(Ty);
685 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Devang Patel6c1fddf2009-07-27 18:42:03 +0000687 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000688 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000689 Align, 0, 0, llvm::DIType(), Elements,
690 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000691
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000692 // Update TypeCache.
693 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
694
Devang Patel9ca36b62009-02-26 21:10:26 +0000695 // Now that we have a real decl for the struct, replace anything using the
696 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000697 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000698
Devang Patel9ca36b62009-02-26 21:10:26 +0000699 return RealDecl;
700}
701
Chris Lattner9c85ba32008-11-10 06:08:34 +0000702llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
703 llvm::DICompileUnit Unit) {
704 EnumDecl *Decl = Ty->getDecl();
705
706 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
707
708 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000709 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000710 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000711 Enum != EnumEnd; ++Enum) {
712 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
713 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattner9c85ba32008-11-10 06:08:34 +0000716 // Return a CompositeType for the enum itself.
717 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000718 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000719
Chris Lattner8ec03f52008-11-24 03:54:41 +0000720 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000721 SourceLocation DefLoc = Decl->getLocation();
722 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
723 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000724 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
725 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
726
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattner9c85ba32008-11-10 06:08:34 +0000728 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000729 uint64_t Size = 0;
730 unsigned Align = 0;
731 if (!Ty->isIncompleteType()) {
732 Size = M->getContext().getTypeSize(Ty);
733 Align = M->getContext().getTypeAlign(Ty);
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Devang Patelca80a5f2009-10-20 19:55:01 +0000736 llvm::DIType DbgTy =
737 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
738 Unit, EnumName, DefUnit, Line,
739 Size, Align, 0, 0,
740 llvm::DIType(), EltArray);
741
742 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
743 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000744}
745
746llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
747 llvm::DICompileUnit Unit) {
748 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
749 return CreateType(RT, Unit);
750 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
751 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Chris Lattner9c85ba32008-11-10 06:08:34 +0000753 return llvm::DIType();
754}
755
756llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
757 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000758 uint64_t Size;
759 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000760
761
Nuno Lopes010d5142009-01-28 00:35:17 +0000762 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000763 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000764 Size = 0;
765 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000766 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
767 } else if (Ty->isIncompleteArrayType()) {
768 Size = 0;
769 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000770 } else {
771 // Size and align of the whole array, not the element type.
772 Size = M->getContext().getTypeSize(Ty);
773 Align = M->getContext().getTypeAlign(Ty);
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattner9c85ba32008-11-10 06:08:34 +0000776 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
777 // interior arrays, do we care? Why aren't nested arrays represented the
778 // obvious/recursive way?
779 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
780 QualType EltTy(Ty, 0);
781 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000782 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000783 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000784 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000785 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000786 // FIXME: Verify this is right for VLAs.
787 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
788 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000792 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000793
Devang Patelca80a5f2009-10-20 19:55:01 +0000794 llvm::DIType DbgTy =
795 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
796 Unit, "", llvm::DICompileUnit(),
797 0, Size, Align, 0, 0,
798 getOrCreateType(EltTy, Unit),
799 SubscriptArray);
800
801 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = DbgTy.getNode();
802 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803}
804
805
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000806/// getOrCreateType - Get the type from the cache or create a new
807/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
809 llvm::DICompileUnit Unit) {
810 if (Ty.isNull())
811 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000813 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000814 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000815 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000816 if (it != TypeCache.end()) {
817 // Verify that the debug info still exists.
818 if (&*it->second)
819 return llvm::DIType(cast<llvm::MDNode>(it->second));
820 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000821
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000822 // Otherwise create the type.
823 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000824 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000825}
826
827/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
828/// new one if necessary.
829llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
830 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000831 // Handle qualifiers, which recursively handles what they refer to.
832 if (Ty.hasQualifiers())
833 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000834
835 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000837#define TYPE(Class, Base)
838#define ABSTRACT_TYPE(Class, Base)
839#define NON_CANONICAL_TYPE(Class, Base)
840#define DEPENDENT_TYPE(Class, Base) case Type::Class:
841#include "clang/AST/TypeNodes.def"
842 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000843
Anders Carlssonbfe69952009-11-06 18:24:04 +0000844 // FIXME: Handle these.
845 case Type::ExtVector:
846 case Type::Vector:
847 return llvm::DIType();
Daniel Dunbar03faac32009-09-19 19:27:14 +0000848 default:
Anders Carlssonb9381182009-11-06 17:01:39 +0000849 assert(false && "Unhandled type class!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000850 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000851 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000852 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000853 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000854 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
855 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
856 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
857 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000858 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000859 return CreateType(cast<BlockPointerType>(Ty), Unit);
860 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000861 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000862 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000863 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000864 case Type::FunctionProto:
865 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000866 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000867 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000868 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
869 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattner9c85ba32008-11-10 06:08:34 +0000871 case Type::ConstantArray:
872 case Type::VariableArray:
873 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000874 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000875 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000876 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
877 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000878 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000879 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000880 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000881 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000882 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000883}
884
885/// EmitFunctionStart - Constructs the debug code for entering a function -
886/// "llvm.dbg.func.start.".
Mike Stump91cc8152009-10-23 01:52:13 +0000887void CGDebugInfo::EmitFunctionStart(const char *Name, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000888 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000889 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000890 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Daniel Dunbara2893932009-05-13 23:08:57 +0000892 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000893 //
894 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000895 if (Name[0] == '\01')
896 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattner9c85ba32008-11-10 06:08:34 +0000898 // FIXME: Why is this using CurLoc???
899 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000900 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000901 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattner9c85ba32008-11-10 06:08:34 +0000903 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000904 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +0000905 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000906 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Devang Patel6e461bf2009-10-06 21:53:41 +0000908#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
909 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
910#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000912 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000913 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000914}
915
916
Chris Lattner9c85ba32008-11-10 06:08:34 +0000917void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000918 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000920 // Don't bother if things are the same as last time.
921 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000922 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000923 || (SM.getInstantiationLineNumber(CurLoc) ==
924 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000925 && SM.isFromSameFile(CurLoc, PrevLoc)))
926 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000927
928 // Update last state.
929 PrevLoc = CurLoc;
930
931 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000932 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000933 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000934
935#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
936 llvm::DIDescriptor DR = RegionStack.back();
937 llvm::DIScope DS = llvm::DIScope(DR.getNode());
938 llvm::DILocation DO(NULL);
939 llvm::DILocation DL =
940 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
941 DS, DO);
942 Builder.SetCurrentDebugLocation(DL.getNode());
943#else
Devang Patel0f78fea2009-04-08 19:47:04 +0000944 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000945 Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000946#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000947}
948
949/// EmitRegionStart- Constructs the debug code for entering a declarative
950/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000951void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
952 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000953 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000955 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956 RegionStack.push_back(D);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000957#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000959#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000960}
961
962/// EmitRegionEnd - Constructs the debug code for exiting a declarative
963/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000964void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000965 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
966
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000967 // Provide an region stop point.
968 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Devang Patelbbd9fa42009-10-06 18:36:08 +0000970#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000971 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000972#endif
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000973 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000974}
975
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000976/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
978 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000979 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
980
Devang Patel07739032009-03-27 23:16:32 +0000981 // Do not emit variable debug information while generating optimized code.
982 // The llvm optimizer and code generator are not yet ready to support
983 // optimized code debugging.
984 const CompileOptions &CO = M->getCompileOpts();
985 if (CO.OptimizationLevel)
986 return;
987
Chris Lattner650cea92009-05-05 04:57:08 +0000988 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000989 QualType Type = Decl->getType();
990 llvm::DIType Ty = getOrCreateType(Type, Unit);
991 if (Decl->hasAttr<BlocksAttr>()) {
992 llvm::DICompileUnit DefUnit;
993 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
994
995 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
996
997 llvm::DIType FieldTy;
998
999 QualType FType;
1000 uint64_t FieldSize, FieldOffset;
1001 unsigned FieldAlign;
1002
1003 llvm::DIArray Elements;
1004 llvm::DIType EltTy;
1005
1006 // Build up structure for the byref. See BuildByRefType.
1007 FieldOffset = 0;
1008 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1009 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1010 FieldSize = M->getContext().getTypeSize(FType);
1011 FieldAlign = M->getContext().getTypeAlign(FType);
1012 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1013 "__isa", DefUnit,
1014 0, FieldSize, FieldAlign,
1015 FieldOffset, 0, FieldTy);
1016 EltTys.push_back(FieldTy);
1017 FieldOffset += FieldSize;
1018
1019 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1020 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1021 FieldSize = M->getContext().getTypeSize(FType);
1022 FieldAlign = M->getContext().getTypeAlign(FType);
1023 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1024 "__forwarding", DefUnit,
1025 0, FieldSize, FieldAlign,
1026 FieldOffset, 0, FieldTy);
1027 EltTys.push_back(FieldTy);
1028 FieldOffset += FieldSize;
1029
1030 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1031 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1032 FieldSize = M->getContext().getTypeSize(FType);
1033 FieldAlign = M->getContext().getTypeAlign(FType);
1034 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1035 "__flags", DefUnit,
1036 0, FieldSize, FieldAlign,
1037 FieldOffset, 0, FieldTy);
1038 EltTys.push_back(FieldTy);
1039 FieldOffset += FieldSize;
1040
1041 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1042 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1043 FieldSize = M->getContext().getTypeSize(FType);
1044 FieldAlign = M->getContext().getTypeAlign(FType);
1045 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1046 "__size", DefUnit,
1047 0, FieldSize, FieldAlign,
1048 FieldOffset, 0, FieldTy);
1049 EltTys.push_back(FieldTy);
1050 FieldOffset += FieldSize;
1051
1052 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1053 if (HasCopyAndDispose) {
1054 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1055 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1056 FieldSize = M->getContext().getTypeSize(FType);
1057 FieldAlign = M->getContext().getTypeAlign(FType);
1058 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1059 "__copy_helper", DefUnit,
1060 0, FieldSize, FieldAlign,
1061 FieldOffset, 0, FieldTy);
1062 EltTys.push_back(FieldTy);
1063 FieldOffset += FieldSize;
1064
1065 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1066 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1067 FieldSize = M->getContext().getTypeSize(FType);
1068 FieldAlign = M->getContext().getTypeAlign(FType);
1069 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1070 "__destroy_helper", DefUnit,
1071 0, FieldSize, FieldAlign,
1072 FieldOffset, 0, FieldTy);
1073 EltTys.push_back(FieldTy);
1074 FieldOffset += FieldSize;
1075 }
1076
1077 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1078 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1079 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001080 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001081 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001082 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001083
1084 if (NumPaddingBytes > 0) {
1085 llvm::APInt pad(32, NumPaddingBytes);
1086 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1087 pad, ArrayType::Normal, 0);
1088 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1089 FieldSize = M->getContext().getTypeSize(FType);
1090 FieldAlign = M->getContext().getTypeAlign(FType);
1091 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1092 Unit, "", DefUnit,
1093 0, FieldSize, FieldAlign,
1094 FieldOffset, 0, FieldTy);
1095 EltTys.push_back(FieldTy);
1096 FieldOffset += FieldSize;
1097 }
1098 }
1099
1100 FType = Type;
1101 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1102 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001103 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001104 std::string Name = Decl->getNameAsString();
1105
1106 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1107 Name, DefUnit,
1108 0, FieldSize, FieldAlign,
1109 FieldOffset, 0, FieldTy);
1110 EltTys.push_back(FieldTy);
1111 FieldOffset += FieldSize;
1112
1113 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1114
1115 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1116
1117 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1118 llvm::DICompileUnit(),
1119 0, FieldOffset, 0, 0, Flags,
1120 llvm::DIType(), Elements);
1121 }
Chris Lattner650cea92009-05-05 04:57:08 +00001122
Chris Lattner9c85ba32008-11-10 06:08:34 +00001123 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001124 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001125 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001126 unsigned Line = 0;
1127 if (!PLoc.isInvalid())
1128 Line = PLoc.getLine();
1129 else
1130 Unit = llvm::DICompileUnit();
1131
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner9c85ba32008-11-10 06:08:34 +00001133 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001134 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001135 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001136 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001137 // Insert an llvm.dbg.declare into the current block.
1138 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001139}
1140
Mike Stumpb1a6e682009-09-30 02:43:10 +00001141/// EmitDeclare - Emit local variable declaration debug info.
1142void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1143 llvm::Value *Storage, CGBuilderTy &Builder,
1144 CodeGenFunction *CGF) {
1145 const ValueDecl *Decl = BDRE->getDecl();
1146 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1147
1148 // Do not emit variable debug information while generating optimized code.
1149 // The llvm optimizer and code generator are not yet ready to support
1150 // optimized code debugging.
1151 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001152 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001153 return;
1154
1155 uint64_t XOffset = 0;
1156 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1157 QualType Type = Decl->getType();
1158 llvm::DIType Ty = getOrCreateType(Type, Unit);
1159 if (Decl->hasAttr<BlocksAttr>()) {
1160 llvm::DICompileUnit DefUnit;
1161 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1162
1163 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1164
1165 llvm::DIType FieldTy;
1166
1167 QualType FType;
1168 uint64_t FieldSize, FieldOffset;
1169 unsigned FieldAlign;
1170
1171 llvm::DIArray Elements;
1172 llvm::DIType EltTy;
1173
1174 // Build up structure for the byref. See BuildByRefType.
1175 FieldOffset = 0;
1176 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1177 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1178 FieldSize = M->getContext().getTypeSize(FType);
1179 FieldAlign = M->getContext().getTypeAlign(FType);
1180 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1181 "__isa", DefUnit,
1182 0, FieldSize, FieldAlign,
1183 FieldOffset, 0, FieldTy);
1184 EltTys.push_back(FieldTy);
1185 FieldOffset += FieldSize;
1186
1187 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1188 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1189 FieldSize = M->getContext().getTypeSize(FType);
1190 FieldAlign = M->getContext().getTypeAlign(FType);
1191 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1192 "__forwarding", DefUnit,
1193 0, FieldSize, FieldAlign,
1194 FieldOffset, 0, FieldTy);
1195 EltTys.push_back(FieldTy);
1196 FieldOffset += FieldSize;
1197
1198 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1199 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1200 FieldSize = M->getContext().getTypeSize(FType);
1201 FieldAlign = M->getContext().getTypeAlign(FType);
1202 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1203 "__flags", DefUnit,
1204 0, FieldSize, FieldAlign,
1205 FieldOffset, 0, FieldTy);
1206 EltTys.push_back(FieldTy);
1207 FieldOffset += FieldSize;
1208
1209 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1210 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1211 FieldSize = M->getContext().getTypeSize(FType);
1212 FieldAlign = M->getContext().getTypeAlign(FType);
1213 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1214 "__size", DefUnit,
1215 0, FieldSize, FieldAlign,
1216 FieldOffset, 0, FieldTy);
1217 EltTys.push_back(FieldTy);
1218 FieldOffset += FieldSize;
1219
1220 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1221 if (HasCopyAndDispose) {
1222 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1223 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1224 FieldSize = M->getContext().getTypeSize(FType);
1225 FieldAlign = M->getContext().getTypeAlign(FType);
1226 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1227 "__copy_helper", DefUnit,
1228 0, FieldSize, FieldAlign,
1229 FieldOffset, 0, FieldTy);
1230 EltTys.push_back(FieldTy);
1231 FieldOffset += FieldSize;
1232
1233 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1234 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1235 FieldSize = M->getContext().getTypeSize(FType);
1236 FieldAlign = M->getContext().getTypeAlign(FType);
1237 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1238 "__destroy_helper", DefUnit,
1239 0, FieldSize, FieldAlign,
1240 FieldOffset, 0, FieldTy);
1241 EltTys.push_back(FieldTy);
1242 FieldOffset += FieldSize;
1243 }
1244
1245 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1246 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1247 unsigned AlignedOffsetInBytes
1248 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1249 unsigned NumPaddingBytes
1250 = AlignedOffsetInBytes - FieldOffset/8;
1251
1252 if (NumPaddingBytes > 0) {
1253 llvm::APInt pad(32, NumPaddingBytes);
1254 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1255 pad, ArrayType::Normal, 0);
1256 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1257 FieldSize = M->getContext().getTypeSize(FType);
1258 FieldAlign = M->getContext().getTypeAlign(FType);
1259 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1260 Unit, "", DefUnit,
1261 0, FieldSize, FieldAlign,
1262 FieldOffset, 0, FieldTy);
1263 EltTys.push_back(FieldTy);
1264 FieldOffset += FieldSize;
1265 }
1266 }
1267
1268 FType = Type;
1269 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1270 FieldSize = M->getContext().getTypeSize(FType);
1271 FieldAlign = Align*8;
1272 std::string Name = Decl->getNameAsString();
1273
1274 XOffset = FieldOffset;
1275 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1276 Name, DefUnit,
1277 0, FieldSize, FieldAlign,
1278 FieldOffset, 0, FieldTy);
1279 EltTys.push_back(FieldTy);
1280 FieldOffset += FieldSize;
1281
1282 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1283
1284 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1285
1286 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1287 llvm::DICompileUnit(),
1288 0, FieldOffset, 0, 0, Flags,
1289 llvm::DIType(), Elements);
1290 }
1291
1292 // Get location information.
1293 SourceManager &SM = M->getContext().getSourceManager();
1294 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1295 unsigned Line = 0;
1296 if (!PLoc.isInvalid())
1297 Line = PLoc.getLine();
1298 else
1299 Unit = llvm::DICompileUnit();
1300
1301 uint64_t offset = CGF->BlockDecls[Decl];
1302 llvm::SmallVector<llvm::Value *, 9> addr;
1303 llvm::LLVMContext &VMContext = M->getLLVMContext();
1304 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1305 llvm::DIFactory::OpDeref));
1306 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1307 llvm::DIFactory::OpPlus));
1308 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1309 offset));
1310 if (BDRE->isByRef()) {
1311 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1312 llvm::DIFactory::OpDeref));
1313 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1314 llvm::DIFactory::OpPlus));
1315 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1316 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1317 offset));
1318 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1319 llvm::DIFactory::OpDeref));
1320 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1321 llvm::DIFactory::OpPlus));
1322 offset = XOffset/8; // offset of x field
1323 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1324 offset));
1325 }
1326
1327 // Create the descriptor for the variable.
1328 llvm::DIVariable D =
1329 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1330 Decl->getNameAsString(), Unit, Line, Ty,
1331 addr);
1332 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001333 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001334}
1335
Chris Lattner9c85ba32008-11-10 06:08:34 +00001336void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1337 llvm::Value *Storage,
1338 CGBuilderTy &Builder) {
1339 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1340}
1341
Mike Stumpb1a6e682009-09-30 02:43:10 +00001342void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1343 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1344 CodeGenFunction *CGF) {
1345 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1346}
1347
Chris Lattner9c85ba32008-11-10 06:08:34 +00001348/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1349/// variable declaration.
1350void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1351 CGBuilderTy &Builder) {
1352 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1353}
1354
1355
1356
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001357/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001358void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001359 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001360
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001361 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001362 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001363 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001364 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1365 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001366
Sanjiv Gupta2d7bc3e2009-10-14 15:08:34 +00001367 std::string Name = Var->getName();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001368
1369 QualType T = Decl->getType();
1370 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001372 // CodeGen turns int[] into int[1] so we'll do the same here.
1373 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001375 ConstVal = 1;
1376 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001377
1378 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001379 ArrayType::Normal, 0);
1380 }
1381
Devang Patel979ec2e2009-10-06 00:35:31 +00001382 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
Devang Patelf6a39b72009-10-20 18:26:30 +00001383 Name, Name, Name, Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001384 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001385 Var->hasInternalLinkage(),
1386 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001387}
1388
Devang Patel9ca36b62009-02-26 21:10:26 +00001389/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001390void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001391 ObjCInterfaceDecl *Decl) {
1392 // Create global variable debug descriptor.
1393 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1394 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001395 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1396 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001397
1398 std::string Name = Decl->getNameAsString();
1399
Chris Lattner03d9f342009-04-01 06:23:52 +00001400 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001401 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Devang Patel9ca36b62009-02-26 21:10:26 +00001403 // CodeGen turns int[] into int[1] so we'll do the same here.
1404 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Devang Patel9ca36b62009-02-26 21:10:26 +00001406 ConstVal = 1;
1407 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
1409 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001410 ArrayType::Normal, 0);
1411 }
1412
Devang Patelf6a39b72009-10-20 18:26:30 +00001413 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001414 getOrCreateType(T, Unit),
1415 Var->hasInternalLinkage(),
1416 true/*definition*/, Var);
1417}