blob: bbe113b93128e1a3ceacb8516cdbdbfd6fa46eae [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
176 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000177 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000178 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000179 Offset, /*flags*/ 0, Encoding);
180}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000181
Chris Lattnerb7003772009-04-23 06:13:01 +0000182llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
183 llvm::DICompileUnit Unit) {
184 // Bit size, align and offset of the type.
185 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
186 if (Ty->isComplexIntegerType())
187 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnerb7003772009-04-23 06:13:01 +0000189 uint64_t Size = M->getContext().getTypeSize(Ty);
190 uint64_t Align = M->getContext().getTypeAlign(Ty);
191 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnerb7003772009-04-23 06:13:01 +0000193 return DebugFactory.CreateBasicType(Unit, "complex",
194 Unit, 0, Size, Align,
195 Offset, /*flags*/ 0, Encoding);
196}
197
John McCalla1805292009-09-25 01:40:47 +0000198/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000199/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000200llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
201 QualifierCollector Qc;
202 const Type *T = Qc.strip(Ty);
203
204 // Ignore these qualifiers for now.
205 Qc.removeObjCGCAttr();
206 Qc.removeAddressSpace();
207
Chris Lattner9c85ba32008-11-10 06:08:34 +0000208 // We will create one Derived type for one qualifier and recurse to handle any
209 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000210 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000211 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000213 Qc.removeConst();
214 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000216 Qc.removeVolatile();
217 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000218 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000219 Qc.removeRestrict();
220 } else {
221 assert(Qc.empty() && "Unknown type qualifier for debug info");
222 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
John McCalla1805292009-09-25 01:40:47 +0000225 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
226
Daniel Dunbar3845f862008-10-31 03:54:29 +0000227 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
228 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
230 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000231}
232
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000233llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
234 llvm::DICompileUnit Unit) {
235 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000237 // Bit size, align and offset of the type.
238 uint64_t Size = M->getContext().getTypeSize(Ty);
239 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000241 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
242 "", llvm::DICompileUnit(),
243 0, Size, Align, 0, 0, EltTy);
244}
245
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
247 llvm::DICompileUnit Unit) {
248 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000250 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000251 uint64_t Size = M->getContext().getTypeSize(Ty);
252 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Chris Lattner9c85ba32008-11-10 06:08:34 +0000254 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
255 "", llvm::DICompileUnit(),
256 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000257}
258
Mike Stump9bc093c2009-05-14 02:03:51 +0000259llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
260 llvm::DICompileUnit Unit) {
261 if (BlockLiteralGenericSet)
262 return BlockLiteralGeneric;
263
264 llvm::DICompileUnit DefUnit;
265 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
266
267 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
268
269 llvm::DIType FieldTy;
270
271 QualType FType;
272 uint64_t FieldSize, FieldOffset;
273 unsigned FieldAlign;
274
275 llvm::DIArray Elements;
276 llvm::DIType EltTy, DescTy;
277
278 FieldOffset = 0;
279 FType = M->getContext().UnsignedLongTy;
280 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
281 FieldSize = M->getContext().getTypeSize(FType);
282 FieldAlign = M->getContext().getTypeAlign(FType);
283 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
284 "reserved", DefUnit,
285 0, FieldSize, FieldAlign,
286 FieldOffset, 0, FieldTy);
287 EltTys.push_back(FieldTy);
288
289 FieldOffset += FieldSize;
290 FType = M->getContext().UnsignedLongTy;
291 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
292 FieldSize = M->getContext().getTypeSize(FType);
293 FieldAlign = M->getContext().getTypeAlign(FType);
294 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
295 "Size", DefUnit,
296 0, FieldSize, FieldAlign,
297 FieldOffset, 0, FieldTy);
298 EltTys.push_back(FieldTy);
299
300 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000301 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000302 EltTys.clear();
303
Mike Stump3d363c52009-10-02 02:30:50 +0000304 unsigned Flags = llvm::DIType::FlagAppleBlock;
305
Mike Stump9bc093c2009-05-14 02:03:51 +0000306 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000307 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000308 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Mike Stump9bc093c2009-05-14 02:03:51 +0000310 // Bit size, align and offset of the type.
311 uint64_t Size = M->getContext().getTypeSize(Ty);
312 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Mike Stump9bc093c2009-05-14 02:03:51 +0000314 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
315 Unit, "", llvm::DICompileUnit(),
316 0, Size, Align, 0, 0, EltTy);
317
318 FieldOffset = 0;
319 FType = M->getContext().getPointerType(M->getContext().VoidTy);
320 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
321 FieldSize = M->getContext().getTypeSize(FType);
322 FieldAlign = M->getContext().getTypeAlign(FType);
323 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
324 "__isa", DefUnit,
325 0, FieldSize, FieldAlign,
326 FieldOffset, 0, FieldTy);
327 EltTys.push_back(FieldTy);
328
329 FieldOffset += FieldSize;
330 FType = M->getContext().IntTy;
331 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
332 FieldSize = M->getContext().getTypeSize(FType);
333 FieldAlign = M->getContext().getTypeAlign(FType);
334 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
335 "__flags", DefUnit,
336 0, FieldSize, FieldAlign,
337 FieldOffset, 0, FieldTy);
338 EltTys.push_back(FieldTy);
339
340 FieldOffset += FieldSize;
341 FType = M->getContext().IntTy;
342 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
343 FieldSize = M->getContext().getTypeSize(FType);
344 FieldAlign = M->getContext().getTypeAlign(FType);
345 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
346 "__reserved", DefUnit,
347 0, FieldSize, FieldAlign,
348 FieldOffset, 0, FieldTy);
349 EltTys.push_back(FieldTy);
350
351 FieldOffset += FieldSize;
352 FType = M->getContext().getPointerType(M->getContext().VoidTy);
353 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
354 FieldSize = M->getContext().getTypeSize(FType);
355 FieldAlign = M->getContext().getTypeAlign(FType);
356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
357 "__FuncPtr", DefUnit,
358 0, FieldSize, FieldAlign,
359 FieldOffset, 0, FieldTy);
360 EltTys.push_back(FieldTy);
361
362 FieldOffset += FieldSize;
363 FType = M->getContext().getPointerType(M->getContext().VoidTy);
364 FieldTy = DescTy;
365 FieldSize = M->getContext().getTypeSize(Ty);
366 FieldAlign = M->getContext().getTypeAlign(Ty);
367 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
368 "__descriptor", DefUnit,
369 0, FieldSize, FieldAlign,
370 FieldOffset, 0, FieldTy);
371 EltTys.push_back(FieldTy);
372
373 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000374 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000375
376 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000377 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 BlockLiteralGenericSet = true;
381 BlockLiteralGeneric
382 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
383 "", llvm::DICompileUnit(),
384 0, Size, Align, 0, 0, EltTy);
385 return BlockLiteralGeneric;
386}
387
Chris Lattner9c85ba32008-11-10 06:08:34 +0000388llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
389 llvm::DICompileUnit Unit) {
390 // Typedefs are derived from some other type. If we have a typedef of a
391 // typedef, make sure to emit the whole chain.
392 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394 // We don't set size information, but do specify where the typedef was
395 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000396 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397 SourceLocation DefLoc = Ty->getDecl()->getLocation();
398 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000400 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000401 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
402 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
405 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000406}
407
Chris Lattner9c85ba32008-11-10 06:08:34 +0000408llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
409 llvm::DICompileUnit Unit) {
410 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000411
Chris Lattner9c85ba32008-11-10 06:08:34 +0000412 // Add the result type at least.
413 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415 // Set up remainder of arguments if there is a prototype.
416 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000417 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
419 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
420 } else {
421 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000422 }
423
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000425 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
428 Unit, "", llvm::DICompileUnit(),
429 0, 0, 0, 0, 0,
430 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000431}
432
Devang Patel65e99f22009-02-25 01:36:11 +0000433/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000434llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
435 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000436 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438 unsigned Tag;
439 if (Decl->isStruct())
440 Tag = llvm::dwarf::DW_TAG_structure_type;
441 else if (Decl->isUnion())
442 Tag = llvm::dwarf::DW_TAG_union_type;
443 else {
444 assert(Decl->isClass() && "Unknown RecordType!");
445 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000446 }
447
Sanjiv Gupta507de852008-06-09 10:47:41 +0000448 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000449
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000451 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000452
Devang Patel4f6fa232009-04-17 21:35:15 +0000453 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000454 llvm::DICompileUnit DefUnit;
455 unsigned Line = 0;
456 if (!PLoc.isInvalid()) {
457 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
458 Line = PLoc.getLine();
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner9c85ba32008-11-10 06:08:34 +0000461 // Records and classes and unions can all be recursive. To handle them, we
462 // first generate a debug descriptor for the struct as a forward declaration.
463 // Then (if it is a definition) we go through and get debug info for all of
464 // its members. Finally, we create a descriptor for the complete type (which
465 // may refer to the forward decl if the struct is recursive) and replace all
466 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000467 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000468 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
469 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner9c85ba32008-11-10 06:08:34 +0000471 // If this is just a forward declaration, return it.
472 if (!Decl->getDefinition(M->getContext()))
473 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000474
Chris Lattner9c85ba32008-11-10 06:08:34 +0000475 // Otherwise, insert it into the TypeCache so that recursive uses will find
476 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000477 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000478
479 // Convert all the elements.
480 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
481
482 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
483
484 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000485 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000486 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000487 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000488 FieldDecl *Field = *I;
489 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000490
491 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000492
Devang Patelde135022009-04-27 22:40:36 +0000493 // Ignore unnamed fields.
494 if (FieldName.empty())
495 continue;
496
Chris Lattner9c85ba32008-11-10 06:08:34 +0000497 // Get the location for the field.
498 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000499 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000500 llvm::DICompileUnit FieldDefUnit;
501 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000503 if (!PLoc.isInvalid()) {
504 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
505 FieldLine = PLoc.getLine();
506 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000507
508 QualType FType = Field->getType();
509 uint64_t FieldSize = 0;
510 unsigned FieldAlign = 0;
511 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Devang Patelec9b5d52009-03-16 23:47:53 +0000513 // Bit size, align and offset of the type.
514 FieldSize = M->getContext().getTypeSize(FType);
515 Expr *BitWidth = Field->getBitWidth();
516 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000517 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Devang Patelec9b5d52009-03-16 23:47:53 +0000519 FieldAlign = M->getContext().getTypeAlign(FType);
520 }
521
Mike Stump1eb44332009-09-09 15:08:12 +0000522 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
523
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 // Create a DW_TAG_member node to remember the offset of this field in the
525 // struct. FIXME: This is an absolutely insane way to capture this
526 // information. When we gut debug info, this should be fixed.
527 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
528 FieldName, FieldDefUnit,
529 FieldLine, FieldSize, FieldAlign,
530 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531 EltTys.push_back(FieldTy);
532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner9c85ba32008-11-10 06:08:34 +0000534 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000535 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536
537 // Bit size, align and offset of the type.
538 uint64_t Size = M->getContext().getTypeSize(Ty);
539 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Devang Patel0ce73f62009-07-22 18:57:00 +0000541 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
543 Align, 0, 0, llvm::DIType(), Elements);
544
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000545 // Update TypeCache.
546 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // Now that we have a real decl for the struct, replace anything using the
549 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000550 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000551
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552 return RealDecl;
553}
554
Devang Patel9ca36b62009-02-26 21:10:26 +0000555/// CreateType - get objective-c interface type.
556llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
557 llvm::DICompileUnit Unit) {
558 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Devang Patel9ca36b62009-02-26 21:10:26 +0000560 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
561 SourceManager &SM = M->getContext().getSourceManager();
562
563 // Get overall information about the record type for the debug info.
564 std::string Name = Decl->getNameAsString();
565
566 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000567 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
568 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
569
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Daniel Dunbard86d3362009-05-18 20:51:58 +0000571 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000572
Devang Patel9ca36b62009-02-26 21:10:26 +0000573 // To handle recursive interface, we
574 // first generate a debug descriptor for the struct as a forward declaration.
575 // Then (if it is a definition) we go through and get debug info for all of
576 // its members. Finally, we create a descriptor for the complete type (which
577 // may refer to the forward decl if the struct is recursive) and replace all
578 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000579 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000580 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000581 llvm::DIType(), llvm::DIArray(),
582 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Devang Patel9ca36b62009-02-26 21:10:26 +0000584 // If this is just a forward declaration, return it.
585 if (Decl->isForwardDecl())
586 return FwdDecl;
587
588 // Otherwise, insert it into the TypeCache so that recursive uses will find
589 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000590 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000591
592 // Convert all the elements.
593 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
594
Devang Patelfbe899f2009-03-10 21:30:26 +0000595 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
596 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000597 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000598 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000599 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000600 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000601 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000602 0 /* offset */, 0, SClassTy);
603 EltTys.push_back(InhTag);
604 }
605
Devang Patel9ca36b62009-02-26 21:10:26 +0000606 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
607
608 unsigned FieldNo = 0;
609 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
610 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
611 ObjCIvarDecl *Field = *I;
612 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
613
614 std::string FieldName = Field->getNameAsString();
615
Devang Patelde135022009-04-27 22:40:36 +0000616 // Ignore unnamed fields.
617 if (FieldName.empty())
618 continue;
619
Devang Patel9ca36b62009-02-26 21:10:26 +0000620 // Get the location for the field.
621 SourceLocation FieldDefLoc = Field->getLocation();
622 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000623 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
624 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
625
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Devang Patel99c20eb2009-03-20 18:24:39 +0000627 QualType FType = Field->getType();
628 uint64_t FieldSize = 0;
629 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000630
Devang Patel99c20eb2009-03-20 18:24:39 +0000631 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Devang Patel99c20eb2009-03-20 18:24:39 +0000633 // Bit size, align and offset of the type.
634 FieldSize = M->getContext().getTypeSize(FType);
635 Expr *BitWidth = Field->getBitWidth();
636 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000637 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
638
Devang Patel99c20eb2009-03-20 18:24:39 +0000639 FieldAlign = M->getContext().getTypeAlign(FType);
640 }
641
Mike Stump1eb44332009-09-09 15:08:12 +0000642 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
643
Devang Patelc20482b2009-03-19 00:23:53 +0000644 unsigned Flags = 0;
645 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
646 Flags = llvm::DIType::FlagProtected;
647 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
648 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Devang Patel9ca36b62009-02-26 21:10:26 +0000650 // Create a DW_TAG_member node to remember the offset of this field in the
651 // struct. FIXME: This is an absolutely insane way to capture this
652 // information. When we gut debug info, this should be fixed.
653 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
654 FieldName, FieldDefUnit,
655 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000656 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000657 EltTys.push_back(FieldTy);
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Devang Patel9ca36b62009-02-26 21:10:26 +0000660 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000661 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000662
663 // Bit size, align and offset of the type.
664 uint64_t Size = M->getContext().getTypeSize(Ty);
665 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Devang Patel6c1fddf2009-07-27 18:42:03 +0000667 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000668 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000669 Align, 0, 0, llvm::DIType(), Elements,
670 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000671
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000672 // Update TypeCache.
673 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
674
Devang Patel9ca36b62009-02-26 21:10:26 +0000675 // Now that we have a real decl for the struct, replace anything using the
676 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000677 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000678
Devang Patel9ca36b62009-02-26 21:10:26 +0000679 return RealDecl;
680}
681
Chris Lattner9c85ba32008-11-10 06:08:34 +0000682llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
683 llvm::DICompileUnit Unit) {
684 EnumDecl *Decl = Ty->getDecl();
685
686 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
687
688 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000689 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000690 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000691 Enum != EnumEnd; ++Enum) {
692 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
693 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattner9c85ba32008-11-10 06:08:34 +0000696 // Return a CompositeType for the enum itself.
697 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000698 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000699
Chris Lattner8ec03f52008-11-24 03:54:41 +0000700 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000701 SourceLocation DefLoc = Decl->getLocation();
702 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
703 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000704 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
705 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
706
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner9c85ba32008-11-10 06:08:34 +0000708 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000709 uint64_t Size = 0;
710 unsigned Align = 0;
711 if (!Ty->isIncompleteType()) {
712 Size = M->getContext().getTypeSize(Ty);
713 Align = M->getContext().getTypeAlign(Ty);
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattner9c85ba32008-11-10 06:08:34 +0000716 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
717 Unit, EnumName, DefUnit, Line,
718 Size, Align, 0, 0,
719 llvm::DIType(), EltArray);
720}
721
722llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
723 llvm::DICompileUnit Unit) {
724 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
725 return CreateType(RT, Unit);
726 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
727 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattner9c85ba32008-11-10 06:08:34 +0000729 return llvm::DIType();
730}
731
732llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
733 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000734 uint64_t Size;
735 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
737
Nuno Lopes010d5142009-01-28 00:35:17 +0000738 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000739 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000740 Size = 0;
741 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000742 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
743 } else if (Ty->isIncompleteArrayType()) {
744 Size = 0;
745 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000746 } else {
747 // Size and align of the whole array, not the element type.
748 Size = M->getContext().getTypeSize(Ty);
749 Align = M->getContext().getTypeAlign(Ty);
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattner9c85ba32008-11-10 06:08:34 +0000752 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
753 // interior arrays, do we care? Why aren't nested arrays represented the
754 // obvious/recursive way?
755 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
756 QualType EltTy(Ty, 0);
757 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000758 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000759 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000760 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000761 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 // FIXME: Verify this is right for VLAs.
763 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
764 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattner9c85ba32008-11-10 06:08:34 +0000767 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000768 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000769
770 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
771 Unit, "", llvm::DICompileUnit(),
772 0, Size, Align, 0, 0,
773 getOrCreateType(EltTy, Unit),
774 SubscriptArray);
775}
776
777
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000778/// getOrCreateType - Get the type from the cache or create a new
779/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
781 llvm::DICompileUnit Unit) {
782 if (Ty.isNull())
783 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000785 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000786 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000787 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000788 if (it != TypeCache.end()) {
789 // Verify that the debug info still exists.
790 if (&*it->second)
791 return llvm::DIType(cast<llvm::MDNode>(it->second));
792 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000793
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000794 // Otherwise create the type.
795 llvm::DIType Res = CreateTypeNode(Ty, Unit);
796 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
797 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000798}
799
800/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
801/// new one if necessary.
802llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
803 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000804 // Handle qualifiers, which recursively handles what they refer to.
805 if (Ty.hasQualifiers())
806 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000807
808 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000809 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000810#define TYPE(Class, Base)
811#define ABSTRACT_TYPE(Class, Base)
812#define NON_CANONICAL_TYPE(Class, Base)
813#define DEPENDENT_TYPE(Class, Base) case Type::Class:
814#include "clang/AST/TypeNodes.def"
815 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000816
Daniel Dunbar03faac32009-09-19 19:27:14 +0000817 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000818 case Type::LValueReference:
819 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000820 case Type::Vector:
821 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000822 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000823 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000824 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000825 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000826 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000828 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000829 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000830 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000831 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
832 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
833 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
834 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000835 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000836 return CreateType(cast<BlockPointerType>(Ty), Unit);
837 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000838 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000839 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000840 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841 case Type::FunctionProto:
842 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000843 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000844 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000845 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
846 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattner9c85ba32008-11-10 06:08:34 +0000848 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000849 case Type::ConstantArrayWithExpr:
850 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000851 case Type::VariableArray:
852 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000853 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000854 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000855 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
856 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000857 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000858 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000859 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000860 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000861 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000862}
863
864/// EmitFunctionStart - Constructs the debug code for entering a function -
865/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000867 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000868 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000869 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Daniel Dunbara2893932009-05-13 23:08:57 +0000871 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000872 //
873 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000874 if (Name[0] == '\01')
875 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877 // FIXME: Why is this using CurLoc???
878 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000879 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000880 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner9c85ba32008-11-10 06:08:34 +0000882 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000883 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000884 getOrCreateType(ReturnType, Unit),
885 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Devang Patel6e461bf2009-10-06 21:53:41 +0000887#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
888 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
889#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000891 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000892 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000893}
894
895
Chris Lattner9c85ba32008-11-10 06:08:34 +0000896void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000897 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000899 // Don't bother if things are the same as last time.
900 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000901 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000902 || (SM.getInstantiationLineNumber(CurLoc) ==
903 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000904 && SM.isFromSameFile(CurLoc, PrevLoc)))
905 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000906
907 // Update last state.
908 PrevLoc = CurLoc;
909
910 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000911 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000912 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000913
914#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
915 llvm::DIDescriptor DR = RegionStack.back();
916 llvm::DIScope DS = llvm::DIScope(DR.getNode());
917 llvm::DILocation DO(NULL);
918 llvm::DILocation DL =
919 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
920 DS, DO);
921 Builder.SetCurrentDebugLocation(DL.getNode());
922#else
Devang Patel0f78fea2009-04-08 19:47:04 +0000923 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000924 Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000925#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000926}
927
928/// EmitRegionStart- Constructs the debug code for entering a declarative
929/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
931 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000932 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000933 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000934 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000935 RegionStack.push_back(D);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000936#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000937 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000938#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000939}
940
941/// EmitRegionEnd - Constructs the debug code for exiting a declarative
942/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000944 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
945
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000946 // Provide an region stop point.
947 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Devang Patelbbd9fa42009-10-06 18:36:08 +0000949#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000950 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000951#endif
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000952 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000953}
954
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000955/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
957 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000958 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
959
Devang Patel07739032009-03-27 23:16:32 +0000960 // Do not emit variable debug information while generating optimized code.
961 // The llvm optimizer and code generator are not yet ready to support
962 // optimized code debugging.
963 const CompileOptions &CO = M->getCompileOpts();
964 if (CO.OptimizationLevel)
965 return;
966
Chris Lattner650cea92009-05-05 04:57:08 +0000967 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000968 QualType Type = Decl->getType();
969 llvm::DIType Ty = getOrCreateType(Type, Unit);
970 if (Decl->hasAttr<BlocksAttr>()) {
971 llvm::DICompileUnit DefUnit;
972 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
973
974 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
975
976 llvm::DIType FieldTy;
977
978 QualType FType;
979 uint64_t FieldSize, FieldOffset;
980 unsigned FieldAlign;
981
982 llvm::DIArray Elements;
983 llvm::DIType EltTy;
984
985 // Build up structure for the byref. See BuildByRefType.
986 FieldOffset = 0;
987 FType = M->getContext().getPointerType(M->getContext().VoidTy);
988 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
989 FieldSize = M->getContext().getTypeSize(FType);
990 FieldAlign = M->getContext().getTypeAlign(FType);
991 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
992 "__isa", DefUnit,
993 0, FieldSize, FieldAlign,
994 FieldOffset, 0, FieldTy);
995 EltTys.push_back(FieldTy);
996 FieldOffset += FieldSize;
997
998 FType = M->getContext().getPointerType(M->getContext().VoidTy);
999 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1000 FieldSize = M->getContext().getTypeSize(FType);
1001 FieldAlign = M->getContext().getTypeAlign(FType);
1002 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1003 "__forwarding", DefUnit,
1004 0, FieldSize, FieldAlign,
1005 FieldOffset, 0, FieldTy);
1006 EltTys.push_back(FieldTy);
1007 FieldOffset += FieldSize;
1008
1009 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1010 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1011 FieldSize = M->getContext().getTypeSize(FType);
1012 FieldAlign = M->getContext().getTypeAlign(FType);
1013 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1014 "__flags", DefUnit,
1015 0, FieldSize, FieldAlign,
1016 FieldOffset, 0, FieldTy);
1017 EltTys.push_back(FieldTy);
1018 FieldOffset += FieldSize;
1019
1020 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1021 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1022 FieldSize = M->getContext().getTypeSize(FType);
1023 FieldAlign = M->getContext().getTypeAlign(FType);
1024 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1025 "__size", DefUnit,
1026 0, FieldSize, FieldAlign,
1027 FieldOffset, 0, FieldTy);
1028 EltTys.push_back(FieldTy);
1029 FieldOffset += FieldSize;
1030
1031 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1032 if (HasCopyAndDispose) {
1033 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1034 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1035 FieldSize = M->getContext().getTypeSize(FType);
1036 FieldAlign = M->getContext().getTypeAlign(FType);
1037 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1038 "__copy_helper", DefUnit,
1039 0, FieldSize, FieldAlign,
1040 FieldOffset, 0, FieldTy);
1041 EltTys.push_back(FieldTy);
1042 FieldOffset += FieldSize;
1043
1044 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1045 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1046 FieldSize = M->getContext().getTypeSize(FType);
1047 FieldAlign = M->getContext().getTypeAlign(FType);
1048 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1049 "__destroy_helper", DefUnit,
1050 0, FieldSize, FieldAlign,
1051 FieldOffset, 0, FieldTy);
1052 EltTys.push_back(FieldTy);
1053 FieldOffset += FieldSize;
1054 }
1055
1056 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1057 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1058 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001059 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001060 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001061 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001062
1063 if (NumPaddingBytes > 0) {
1064 llvm::APInt pad(32, NumPaddingBytes);
1065 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1066 pad, ArrayType::Normal, 0);
1067 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1068 FieldSize = M->getContext().getTypeSize(FType);
1069 FieldAlign = M->getContext().getTypeAlign(FType);
1070 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1071 Unit, "", DefUnit,
1072 0, FieldSize, FieldAlign,
1073 FieldOffset, 0, FieldTy);
1074 EltTys.push_back(FieldTy);
1075 FieldOffset += FieldSize;
1076 }
1077 }
1078
1079 FType = Type;
1080 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1081 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001082 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001083 std::string Name = Decl->getNameAsString();
1084
1085 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1086 Name, DefUnit,
1087 0, FieldSize, FieldAlign,
1088 FieldOffset, 0, FieldTy);
1089 EltTys.push_back(FieldTy);
1090 FieldOffset += FieldSize;
1091
1092 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1093
1094 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1095
1096 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1097 llvm::DICompileUnit(),
1098 0, FieldOffset, 0, 0, Flags,
1099 llvm::DIType(), Elements);
1100 }
Chris Lattner650cea92009-05-05 04:57:08 +00001101
Chris Lattner9c85ba32008-11-10 06:08:34 +00001102 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001103 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001104 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001105 unsigned Line = 0;
1106 if (!PLoc.isInvalid())
1107 Line = PLoc.getLine();
1108 else
1109 Unit = llvm::DICompileUnit();
1110
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner9c85ba32008-11-10 06:08:34 +00001112 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001113 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001114 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001115 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001116 // Insert an llvm.dbg.declare into the current block.
1117 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001118}
1119
Mike Stumpb1a6e682009-09-30 02:43:10 +00001120/// EmitDeclare - Emit local variable declaration debug info.
1121void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1122 llvm::Value *Storage, CGBuilderTy &Builder,
1123 CodeGenFunction *CGF) {
1124 const ValueDecl *Decl = BDRE->getDecl();
1125 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1126
1127 // Do not emit variable debug information while generating optimized code.
1128 // The llvm optimizer and code generator are not yet ready to support
1129 // optimized code debugging.
1130 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001131 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001132 return;
1133
1134 uint64_t XOffset = 0;
1135 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1136 QualType Type = Decl->getType();
1137 llvm::DIType Ty = getOrCreateType(Type, Unit);
1138 if (Decl->hasAttr<BlocksAttr>()) {
1139 llvm::DICompileUnit DefUnit;
1140 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1141
1142 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1143
1144 llvm::DIType FieldTy;
1145
1146 QualType FType;
1147 uint64_t FieldSize, FieldOffset;
1148 unsigned FieldAlign;
1149
1150 llvm::DIArray Elements;
1151 llvm::DIType EltTy;
1152
1153 // Build up structure for the byref. See BuildByRefType.
1154 FieldOffset = 0;
1155 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1156 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1157 FieldSize = M->getContext().getTypeSize(FType);
1158 FieldAlign = M->getContext().getTypeAlign(FType);
1159 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1160 "__isa", DefUnit,
1161 0, FieldSize, FieldAlign,
1162 FieldOffset, 0, FieldTy);
1163 EltTys.push_back(FieldTy);
1164 FieldOffset += FieldSize;
1165
1166 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1167 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1168 FieldSize = M->getContext().getTypeSize(FType);
1169 FieldAlign = M->getContext().getTypeAlign(FType);
1170 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1171 "__forwarding", DefUnit,
1172 0, FieldSize, FieldAlign,
1173 FieldOffset, 0, FieldTy);
1174 EltTys.push_back(FieldTy);
1175 FieldOffset += FieldSize;
1176
1177 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1178 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1179 FieldSize = M->getContext().getTypeSize(FType);
1180 FieldAlign = M->getContext().getTypeAlign(FType);
1181 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1182 "__flags", DefUnit,
1183 0, FieldSize, FieldAlign,
1184 FieldOffset, 0, FieldTy);
1185 EltTys.push_back(FieldTy);
1186 FieldOffset += FieldSize;
1187
1188 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1189 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1190 FieldSize = M->getContext().getTypeSize(FType);
1191 FieldAlign = M->getContext().getTypeAlign(FType);
1192 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1193 "__size", DefUnit,
1194 0, FieldSize, FieldAlign,
1195 FieldOffset, 0, FieldTy);
1196 EltTys.push_back(FieldTy);
1197 FieldOffset += FieldSize;
1198
1199 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1200 if (HasCopyAndDispose) {
1201 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1202 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1203 FieldSize = M->getContext().getTypeSize(FType);
1204 FieldAlign = M->getContext().getTypeAlign(FType);
1205 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1206 "__copy_helper", DefUnit,
1207 0, FieldSize, FieldAlign,
1208 FieldOffset, 0, FieldTy);
1209 EltTys.push_back(FieldTy);
1210 FieldOffset += FieldSize;
1211
1212 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1213 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1214 FieldSize = M->getContext().getTypeSize(FType);
1215 FieldAlign = M->getContext().getTypeAlign(FType);
1216 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1217 "__destroy_helper", DefUnit,
1218 0, FieldSize, FieldAlign,
1219 FieldOffset, 0, FieldTy);
1220 EltTys.push_back(FieldTy);
1221 FieldOffset += FieldSize;
1222 }
1223
1224 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1225 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1226 unsigned AlignedOffsetInBytes
1227 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1228 unsigned NumPaddingBytes
1229 = AlignedOffsetInBytes - FieldOffset/8;
1230
1231 if (NumPaddingBytes > 0) {
1232 llvm::APInt pad(32, NumPaddingBytes);
1233 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1234 pad, ArrayType::Normal, 0);
1235 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1236 FieldSize = M->getContext().getTypeSize(FType);
1237 FieldAlign = M->getContext().getTypeAlign(FType);
1238 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1239 Unit, "", DefUnit,
1240 0, FieldSize, FieldAlign,
1241 FieldOffset, 0, FieldTy);
1242 EltTys.push_back(FieldTy);
1243 FieldOffset += FieldSize;
1244 }
1245 }
1246
1247 FType = Type;
1248 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1249 FieldSize = M->getContext().getTypeSize(FType);
1250 FieldAlign = Align*8;
1251 std::string Name = Decl->getNameAsString();
1252
1253 XOffset = FieldOffset;
1254 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1255 Name, DefUnit,
1256 0, FieldSize, FieldAlign,
1257 FieldOffset, 0, FieldTy);
1258 EltTys.push_back(FieldTy);
1259 FieldOffset += FieldSize;
1260
1261 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1262
1263 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1264
1265 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1266 llvm::DICompileUnit(),
1267 0, FieldOffset, 0, 0, Flags,
1268 llvm::DIType(), Elements);
1269 }
1270
1271 // Get location information.
1272 SourceManager &SM = M->getContext().getSourceManager();
1273 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1274 unsigned Line = 0;
1275 if (!PLoc.isInvalid())
1276 Line = PLoc.getLine();
1277 else
1278 Unit = llvm::DICompileUnit();
1279
1280 uint64_t offset = CGF->BlockDecls[Decl];
1281 llvm::SmallVector<llvm::Value *, 9> addr;
1282 llvm::LLVMContext &VMContext = M->getLLVMContext();
1283 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1284 llvm::DIFactory::OpDeref));
1285 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1286 llvm::DIFactory::OpPlus));
1287 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1288 offset));
1289 if (BDRE->isByRef()) {
1290 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1291 llvm::DIFactory::OpDeref));
1292 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1293 llvm::DIFactory::OpPlus));
1294 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1295 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1296 offset));
1297 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1298 llvm::DIFactory::OpDeref));
1299 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1300 llvm::DIFactory::OpPlus));
1301 offset = XOffset/8; // offset of x field
1302 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1303 offset));
1304 }
1305
1306 // Create the descriptor for the variable.
1307 llvm::DIVariable D =
1308 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1309 Decl->getNameAsString(), Unit, Line, Ty,
1310 addr);
1311 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001312 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001313}
1314
Chris Lattner9c85ba32008-11-10 06:08:34 +00001315void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1316 llvm::Value *Storage,
1317 CGBuilderTy &Builder) {
1318 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1319}
1320
Mike Stumpb1a6e682009-09-30 02:43:10 +00001321void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1322 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1323 CodeGenFunction *CGF) {
1324 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1325}
1326
Chris Lattner9c85ba32008-11-10 06:08:34 +00001327/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1328/// variable declaration.
1329void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1330 CGBuilderTy &Builder) {
1331 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1332}
1333
1334
1335
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001336/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001337void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001338 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001339
1340 // Do not emit variable debug information while generating optimized code.
1341 // The llvm optimizer and code generator are not yet ready to support
1342 // optimized code debugging.
1343 const CompileOptions &CO = M->getCompileOpts();
1344 if (CO.OptimizationLevel)
1345 return;
1346
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001347 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001348 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001349 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001350 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1351 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001352
1353 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001354
1355 QualType T = Decl->getType();
1356 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001358 // CodeGen turns int[] into int[1] so we'll do the same here.
1359 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001361 ConstVal = 1;
1362 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001363
1364 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001365 ArrayType::Normal, 0);
1366 }
1367
Devang Patel979ec2e2009-10-06 00:35:31 +00001368 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
1369 Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001370 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001371 Var->hasInternalLinkage(),
1372 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001373}
1374
Devang Patel9ca36b62009-02-26 21:10:26 +00001375/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001376void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001377 ObjCInterfaceDecl *Decl) {
1378 // Create global variable debug descriptor.
1379 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1380 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001381 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1382 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001383
1384 std::string Name = Decl->getNameAsString();
1385
Chris Lattner03d9f342009-04-01 06:23:52 +00001386 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001387 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Devang Patel9ca36b62009-02-26 21:10:26 +00001389 // CodeGen turns int[] into int[1] so we'll do the same here.
1390 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Devang Patel9ca36b62009-02-26 21:10:26 +00001392 ConstVal = 1;
1393 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001394
1395 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001396 ArrayType::Normal, 0);
1397 }
1398
Devang Patel6dba4322009-07-14 21:31:22 +00001399 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001400 getOrCreateType(T, Unit),
1401 Var->hasInternalLinkage(),
1402 true/*definition*/, Var);
1403}