blob: af8b10783a0276c3fb2899798f335ea424f15d59 [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
Daniel Dunbar03faac32009-09-19 19:27:14 +0000844 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000845 case Type::LValueReference:
846 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847 case Type::Vector:
848 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000849 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000850 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000851 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000852 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000853 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000854 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000855 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000856 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000857 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000858 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
859 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
860 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
861 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000862 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000863 return CreateType(cast<BlockPointerType>(Ty), Unit);
864 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000865 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000866 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000867 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000868 case Type::FunctionProto:
869 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000870 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000871 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000872 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
873 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner9c85ba32008-11-10 06:08:34 +0000875 case Type::ConstantArray:
876 case Type::VariableArray:
877 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000878 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000879 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000880 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
881 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000882 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000883 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000884 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000885 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000886 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000887}
888
889/// EmitFunctionStart - Constructs the debug code for entering a function -
890/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000892 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000894 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Daniel Dunbara2893932009-05-13 23:08:57 +0000896 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000897 //
898 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000899 if (Name[0] == '\01')
900 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattner9c85ba32008-11-10 06:08:34 +0000902 // FIXME: Why is this using CurLoc???
903 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000904 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000905 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner9c85ba32008-11-10 06:08:34 +0000907 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000908 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000909 getOrCreateType(ReturnType, Unit),
910 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Devang Patel6e461bf2009-10-06 21:53:41 +0000912#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
913 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
914#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000916 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000917 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000918}
919
920
Chris Lattner9c85ba32008-11-10 06:08:34 +0000921void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000922 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000924 // Don't bother if things are the same as last time.
925 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000926 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000927 || (SM.getInstantiationLineNumber(CurLoc) ==
928 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000929 && SM.isFromSameFile(CurLoc, PrevLoc)))
930 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000931
932 // Update last state.
933 PrevLoc = CurLoc;
934
935 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000937 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000938
939#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
940 llvm::DIDescriptor DR = RegionStack.back();
941 llvm::DIScope DS = llvm::DIScope(DR.getNode());
942 llvm::DILocation DO(NULL);
943 llvm::DILocation DL =
944 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
945 DS, DO);
946 Builder.SetCurrentDebugLocation(DL.getNode());
947#else
Devang Patel0f78fea2009-04-08 19:47:04 +0000948 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000949 Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000950#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000951}
952
953/// EmitRegionStart- Constructs the debug code for entering a declarative
954/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000955void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
956 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000957 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000959 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000960 RegionStack.push_back(D);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000961#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000962 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000963#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000964}
965
966/// EmitRegionEnd - Constructs the debug code for exiting a declarative
967/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000969 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
970
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000971 // Provide an region stop point.
972 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Devang Patelbbd9fa42009-10-06 18:36:08 +0000974#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000976#endif
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000977 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000978}
979
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000980/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
982 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000983 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
984
Devang Patel07739032009-03-27 23:16:32 +0000985 // Do not emit variable debug information while generating optimized code.
986 // The llvm optimizer and code generator are not yet ready to support
987 // optimized code debugging.
988 const CompileOptions &CO = M->getCompileOpts();
989 if (CO.OptimizationLevel)
990 return;
991
Chris Lattner650cea92009-05-05 04:57:08 +0000992 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000993 QualType Type = Decl->getType();
994 llvm::DIType Ty = getOrCreateType(Type, Unit);
995 if (Decl->hasAttr<BlocksAttr>()) {
996 llvm::DICompileUnit DefUnit;
997 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
998
999 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1000
1001 llvm::DIType FieldTy;
1002
1003 QualType FType;
1004 uint64_t FieldSize, FieldOffset;
1005 unsigned FieldAlign;
1006
1007 llvm::DIArray Elements;
1008 llvm::DIType EltTy;
1009
1010 // Build up structure for the byref. See BuildByRefType.
1011 FieldOffset = 0;
1012 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1013 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1014 FieldSize = M->getContext().getTypeSize(FType);
1015 FieldAlign = M->getContext().getTypeAlign(FType);
1016 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1017 "__isa", DefUnit,
1018 0, FieldSize, FieldAlign,
1019 FieldOffset, 0, FieldTy);
1020 EltTys.push_back(FieldTy);
1021 FieldOffset += FieldSize;
1022
1023 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1024 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1025 FieldSize = M->getContext().getTypeSize(FType);
1026 FieldAlign = M->getContext().getTypeAlign(FType);
1027 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1028 "__forwarding", DefUnit,
1029 0, FieldSize, FieldAlign,
1030 FieldOffset, 0, FieldTy);
1031 EltTys.push_back(FieldTy);
1032 FieldOffset += FieldSize;
1033
1034 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1035 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1036 FieldSize = M->getContext().getTypeSize(FType);
1037 FieldAlign = M->getContext().getTypeAlign(FType);
1038 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1039 "__flags", DefUnit,
1040 0, FieldSize, FieldAlign,
1041 FieldOffset, 0, FieldTy);
1042 EltTys.push_back(FieldTy);
1043 FieldOffset += FieldSize;
1044
1045 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1046 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1047 FieldSize = M->getContext().getTypeSize(FType);
1048 FieldAlign = M->getContext().getTypeAlign(FType);
1049 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1050 "__size", DefUnit,
1051 0, FieldSize, FieldAlign,
1052 FieldOffset, 0, FieldTy);
1053 EltTys.push_back(FieldTy);
1054 FieldOffset += FieldSize;
1055
1056 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1057 if (HasCopyAndDispose) {
1058 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1059 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1060 FieldSize = M->getContext().getTypeSize(FType);
1061 FieldAlign = M->getContext().getTypeAlign(FType);
1062 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1063 "__copy_helper", DefUnit,
1064 0, FieldSize, FieldAlign,
1065 FieldOffset, 0, FieldTy);
1066 EltTys.push_back(FieldTy);
1067 FieldOffset += FieldSize;
1068
1069 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1070 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1071 FieldSize = M->getContext().getTypeSize(FType);
1072 FieldAlign = M->getContext().getTypeAlign(FType);
1073 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1074 "__destroy_helper", DefUnit,
1075 0, FieldSize, FieldAlign,
1076 FieldOffset, 0, FieldTy);
1077 EltTys.push_back(FieldTy);
1078 FieldOffset += FieldSize;
1079 }
1080
1081 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1082 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1083 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001084 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001085 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001086 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001087
1088 if (NumPaddingBytes > 0) {
1089 llvm::APInt pad(32, NumPaddingBytes);
1090 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1091 pad, ArrayType::Normal, 0);
1092 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1093 FieldSize = M->getContext().getTypeSize(FType);
1094 FieldAlign = M->getContext().getTypeAlign(FType);
1095 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1096 Unit, "", DefUnit,
1097 0, FieldSize, FieldAlign,
1098 FieldOffset, 0, FieldTy);
1099 EltTys.push_back(FieldTy);
1100 FieldOffset += FieldSize;
1101 }
1102 }
1103
1104 FType = Type;
1105 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1106 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001107 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001108 std::string Name = Decl->getNameAsString();
1109
1110 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1111 Name, DefUnit,
1112 0, FieldSize, FieldAlign,
1113 FieldOffset, 0, FieldTy);
1114 EltTys.push_back(FieldTy);
1115 FieldOffset += FieldSize;
1116
1117 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1118
1119 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1120
1121 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1122 llvm::DICompileUnit(),
1123 0, FieldOffset, 0, 0, Flags,
1124 llvm::DIType(), Elements);
1125 }
Chris Lattner650cea92009-05-05 04:57:08 +00001126
Chris Lattner9c85ba32008-11-10 06:08:34 +00001127 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001128 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001129 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001130 unsigned Line = 0;
1131 if (!PLoc.isInvalid())
1132 Line = PLoc.getLine();
1133 else
1134 Unit = llvm::DICompileUnit();
1135
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattner9c85ba32008-11-10 06:08:34 +00001137 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001138 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001139 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001140 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001141 // Insert an llvm.dbg.declare into the current block.
1142 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001143}
1144
Mike Stumpb1a6e682009-09-30 02:43:10 +00001145/// EmitDeclare - Emit local variable declaration debug info.
1146void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1147 llvm::Value *Storage, CGBuilderTy &Builder,
1148 CodeGenFunction *CGF) {
1149 const ValueDecl *Decl = BDRE->getDecl();
1150 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1151
1152 // Do not emit variable debug information while generating optimized code.
1153 // The llvm optimizer and code generator are not yet ready to support
1154 // optimized code debugging.
1155 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001156 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001157 return;
1158
1159 uint64_t XOffset = 0;
1160 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1161 QualType Type = Decl->getType();
1162 llvm::DIType Ty = getOrCreateType(Type, Unit);
1163 if (Decl->hasAttr<BlocksAttr>()) {
1164 llvm::DICompileUnit DefUnit;
1165 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1166
1167 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1168
1169 llvm::DIType FieldTy;
1170
1171 QualType FType;
1172 uint64_t FieldSize, FieldOffset;
1173 unsigned FieldAlign;
1174
1175 llvm::DIArray Elements;
1176 llvm::DIType EltTy;
1177
1178 // Build up structure for the byref. See BuildByRefType.
1179 FieldOffset = 0;
1180 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1181 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1182 FieldSize = M->getContext().getTypeSize(FType);
1183 FieldAlign = M->getContext().getTypeAlign(FType);
1184 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1185 "__isa", DefUnit,
1186 0, FieldSize, FieldAlign,
1187 FieldOffset, 0, FieldTy);
1188 EltTys.push_back(FieldTy);
1189 FieldOffset += FieldSize;
1190
1191 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1192 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1193 FieldSize = M->getContext().getTypeSize(FType);
1194 FieldAlign = M->getContext().getTypeAlign(FType);
1195 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1196 "__forwarding", DefUnit,
1197 0, FieldSize, FieldAlign,
1198 FieldOffset, 0, FieldTy);
1199 EltTys.push_back(FieldTy);
1200 FieldOffset += FieldSize;
1201
1202 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1203 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1204 FieldSize = M->getContext().getTypeSize(FType);
1205 FieldAlign = M->getContext().getTypeAlign(FType);
1206 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1207 "__flags", DefUnit,
1208 0, FieldSize, FieldAlign,
1209 FieldOffset, 0, FieldTy);
1210 EltTys.push_back(FieldTy);
1211 FieldOffset += FieldSize;
1212
1213 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1214 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1215 FieldSize = M->getContext().getTypeSize(FType);
1216 FieldAlign = M->getContext().getTypeAlign(FType);
1217 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1218 "__size", DefUnit,
1219 0, FieldSize, FieldAlign,
1220 FieldOffset, 0, FieldTy);
1221 EltTys.push_back(FieldTy);
1222 FieldOffset += FieldSize;
1223
1224 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1225 if (HasCopyAndDispose) {
1226 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1227 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1228 FieldSize = M->getContext().getTypeSize(FType);
1229 FieldAlign = M->getContext().getTypeAlign(FType);
1230 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1231 "__copy_helper", DefUnit,
1232 0, FieldSize, FieldAlign,
1233 FieldOffset, 0, FieldTy);
1234 EltTys.push_back(FieldTy);
1235 FieldOffset += FieldSize;
1236
1237 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1238 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1239 FieldSize = M->getContext().getTypeSize(FType);
1240 FieldAlign = M->getContext().getTypeAlign(FType);
1241 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1242 "__destroy_helper", DefUnit,
1243 0, FieldSize, FieldAlign,
1244 FieldOffset, 0, FieldTy);
1245 EltTys.push_back(FieldTy);
1246 FieldOffset += FieldSize;
1247 }
1248
1249 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1250 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1251 unsigned AlignedOffsetInBytes
1252 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1253 unsigned NumPaddingBytes
1254 = AlignedOffsetInBytes - FieldOffset/8;
1255
1256 if (NumPaddingBytes > 0) {
1257 llvm::APInt pad(32, NumPaddingBytes);
1258 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1259 pad, ArrayType::Normal, 0);
1260 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1261 FieldSize = M->getContext().getTypeSize(FType);
1262 FieldAlign = M->getContext().getTypeAlign(FType);
1263 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1264 Unit, "", DefUnit,
1265 0, FieldSize, FieldAlign,
1266 FieldOffset, 0, FieldTy);
1267 EltTys.push_back(FieldTy);
1268 FieldOffset += FieldSize;
1269 }
1270 }
1271
1272 FType = Type;
1273 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1274 FieldSize = M->getContext().getTypeSize(FType);
1275 FieldAlign = Align*8;
1276 std::string Name = Decl->getNameAsString();
1277
1278 XOffset = FieldOffset;
1279 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1280 Name, DefUnit,
1281 0, FieldSize, FieldAlign,
1282 FieldOffset, 0, FieldTy);
1283 EltTys.push_back(FieldTy);
1284 FieldOffset += FieldSize;
1285
1286 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1287
1288 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1289
1290 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1291 llvm::DICompileUnit(),
1292 0, FieldOffset, 0, 0, Flags,
1293 llvm::DIType(), Elements);
1294 }
1295
1296 // Get location information.
1297 SourceManager &SM = M->getContext().getSourceManager();
1298 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1299 unsigned Line = 0;
1300 if (!PLoc.isInvalid())
1301 Line = PLoc.getLine();
1302 else
1303 Unit = llvm::DICompileUnit();
1304
1305 uint64_t offset = CGF->BlockDecls[Decl];
1306 llvm::SmallVector<llvm::Value *, 9> addr;
1307 llvm::LLVMContext &VMContext = M->getLLVMContext();
1308 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1309 llvm::DIFactory::OpDeref));
1310 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1311 llvm::DIFactory::OpPlus));
1312 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1313 offset));
1314 if (BDRE->isByRef()) {
1315 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1316 llvm::DIFactory::OpDeref));
1317 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1318 llvm::DIFactory::OpPlus));
1319 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1320 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1321 offset));
1322 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1323 llvm::DIFactory::OpDeref));
1324 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1325 llvm::DIFactory::OpPlus));
1326 offset = XOffset/8; // offset of x field
1327 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1328 offset));
1329 }
1330
1331 // Create the descriptor for the variable.
1332 llvm::DIVariable D =
1333 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1334 Decl->getNameAsString(), Unit, Line, Ty,
1335 addr);
1336 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001337 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001338}
1339
Chris Lattner9c85ba32008-11-10 06:08:34 +00001340void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1341 llvm::Value *Storage,
1342 CGBuilderTy &Builder) {
1343 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1344}
1345
Mike Stumpb1a6e682009-09-30 02:43:10 +00001346void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1347 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1348 CodeGenFunction *CGF) {
1349 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1350}
1351
Chris Lattner9c85ba32008-11-10 06:08:34 +00001352/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1353/// variable declaration.
1354void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1355 CGBuilderTy &Builder) {
1356 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1357}
1358
1359
1360
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001361/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001362void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001363 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001364
1365 // Do not emit variable debug information while generating optimized code.
1366 // The llvm optimizer and code generator are not yet ready to support
1367 // optimized code debugging.
1368 const CompileOptions &CO = M->getCompileOpts();
1369 if (CO.OptimizationLevel)
1370 return;
1371
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001372 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001373 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001374 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001375 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1376 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001377
Sanjiv Gupta2d7bc3e2009-10-14 15:08:34 +00001378 std::string Name = Var->getName();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001379
1380 QualType T = Decl->getType();
1381 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001383 // CodeGen turns int[] into int[1] so we'll do the same here.
1384 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001386 ConstVal = 1;
1387 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001388
1389 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001390 ArrayType::Normal, 0);
1391 }
1392
Devang Patel979ec2e2009-10-06 00:35:31 +00001393 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
Devang Patelf6a39b72009-10-20 18:26:30 +00001394 Name, Name, Name, Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001395 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001396 Var->hasInternalLinkage(),
1397 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001398}
1399
Devang Patel9ca36b62009-02-26 21:10:26 +00001400/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001401void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001402 ObjCInterfaceDecl *Decl) {
1403 // Create global variable debug descriptor.
1404 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1405 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001406 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1407 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001408
1409 std::string Name = Decl->getNameAsString();
1410
Chris Lattner03d9f342009-04-01 06:23:52 +00001411 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001412 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Devang Patel9ca36b62009-02-26 21:10:26 +00001414 // CodeGen turns int[] into int[1] so we'll do the same here.
1415 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Devang Patel9ca36b62009-02-26 21:10:26 +00001417 ConstVal = 1;
1418 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001419
1420 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001421 ArrayType::Normal, 0);
1422 }
1423
Devang Patelf6a39b72009-10-20 18:26:30 +00001424 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001425 getOrCreateType(T, Unit),
1426 Var->hasInternalLinkage(),
1427 true/*definition*/, Var);
1428}