blob: 620037c275010e34fa01796e714b369c4c1f4ee5 [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Devang Patelafc1c1d2009-03-27 23:16:32 +000024#include "clang/Frontend/CompileOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Devang Patel9be7b202009-07-14 21:31:22 +000038CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Mike Stump31f099c2009-05-14 02:03:51 +000039 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
40 BlockLiteralGenericSet(false) {
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000041}
42
Chris Lattneraffb3732008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000045}
46
Chris Lattneraffb3732008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Chris Lattner8a425862009-01-16 07:36:28 +000049 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000050}
51
Devang Patelfaf7e9a2009-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 Gupta15cb6692008-05-08 08:54:20 +000068/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbardec8a892008-10-24 08:38:36 +000069/// one if necessary. This returns null for invalid source locations.
Chris Lattneraffb3732008-11-10 06:08:34 +000070llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel75009452009-04-17 21:06:59 +000071 // Get source file information.
72 const char *FileName = "<unknown>";
Devang Patelab19eca2009-02-24 23:16:03 +000073 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner96adcd52009-04-19 06:50:29 +000074 unsigned FID = 0;
Daniel Dunbar56493b02009-01-22 00:09:25 +000075 if (Loc.isValid()) {
Devang Patel75009452009-04-17 21:06:59 +000076 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
77 FileName = PLoc.getFilename();
78 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar56493b02009-01-22 00:09:25 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000081 // See if this compile unit has been used before.
Devang Patel75009452009-04-17 21:06:59 +000082 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattneraffb3732008-11-10 06:08:34 +000083 if (!Unit.isNull()) return Unit;
Daniel Dunbar3b358a32009-04-08 05:11:16 +000084
Devang Patel75009452009-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 Patel0d425352009-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 Patel75009452009-04-17 21:06:59 +0000109 }
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000110
Chris Lattner8c37df42009-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 Patel94406c92009-03-24 20:35:51 +0000118 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000119 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000120 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000121 } else {
122 LangTag = llvm::dwarf::DW_LANG_C89;
123 }
Devang Patel75009452009-04-17 21:06:59 +0000124
Mike Stumpfc8ff632009-10-09 18:38:12 +0000125 std::string Producer =
126#ifdef CLANG_VENDOR
127 CLANG_VENDOR
128#endif
129 "clang " CLANG_VERSION_STRING;
Chris Lattner44f3ea72009-05-02 01:04:13 +0000130 bool isOptimized = LO.Optimize;
Chris Lattner5912de12009-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 Stump11289f42009-09-09 15:08:12 +0000137
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000138 // Create new compile unit.
Devang Patel0d425352009-06-26 18:32:22 +0000139 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
Mike Stump11289f42009-09-09 15:08:12 +0000140 AbsFileName.getDirname(),
Devang Patel0d425352009-06-26 18:32:22 +0000141 Producer, isMain, isOptimized,
142 Flags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000143}
144
Devang Patel410dc002009-02-25 01:36:11 +0000145/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000146/// one if necessary.
147llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel410dc002009-02-25 01:36:11 +0000148 llvm::DICompileUnit Unit) {
Chris Lattneraffb3732008-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:
168 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000169 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000170 // Bit size, align and offset of the type.
171 uint64_t Size = M->getContext().getTypeSize(BT);
172 uint64_t Align = M->getContext().getTypeAlign(BT);
173 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000174
175 return DebugFactory.CreateBasicType(Unit,
Chris Lattnerc61089a2009-06-30 01:26:17 +0000176 BT->getName(M->getContext().getLangOptions()),
Douglas Gregor7de59662009-05-29 20:38:28 +0000177 Unit, 0, Size, Align,
Chris Lattneraffb3732008-11-10 06:08:34 +0000178 Offset, /*flags*/ 0, Encoding);
179}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000180
Chris Lattner7b0344f2009-04-23 06:13:01 +0000181llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
182 llvm::DICompileUnit Unit) {
183 // Bit size, align and offset of the type.
184 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
185 if (Ty->isComplexIntegerType())
186 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner7b0344f2009-04-23 06:13:01 +0000188 uint64_t Size = M->getContext().getTypeSize(Ty);
189 uint64_t Align = M->getContext().getTypeAlign(Ty);
190 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattner7b0344f2009-04-23 06:13:01 +0000192 return DebugFactory.CreateBasicType(Unit, "complex",
193 Unit, 0, Size, Align,
194 Offset, /*flags*/ 0, Encoding);
195}
196
John McCall0cf15512009-09-25 01:40:47 +0000197/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000198/// a new one if necessary.
John McCall0cf15512009-09-25 01:40:47 +0000199llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
200 QualifierCollector Qc;
201 const Type *T = Qc.strip(Ty);
202
203 // Ignore these qualifiers for now.
204 Qc.removeObjCGCAttr();
205 Qc.removeAddressSpace();
206
Chris Lattneraffb3732008-11-10 06:08:34 +0000207 // We will create one Derived type for one qualifier and recurse to handle any
208 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000209 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000210 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000211 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000212 Qc.removeConst();
213 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000214 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000215 Qc.removeVolatile();
216 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000217 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000218 Qc.removeRestrict();
219 } else {
220 assert(Qc.empty() && "Unknown type qualifier for debug info");
221 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
John McCall0cf15512009-09-25 01:40:47 +0000224 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
225
Daniel Dunbara290ded2008-10-31 03:54:29 +0000226 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
227 // CVR derived types.
Chris Lattneraffb3732008-11-10 06:08:34 +0000228 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
229 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000230}
231
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000232llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
233 llvm::DICompileUnit Unit) {
234 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000235
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000236 // Bit size, align and offset of the type.
237 uint64_t Size = M->getContext().getTypeSize(Ty);
238 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000239
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000240 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
241 "", llvm::DICompileUnit(),
242 0, Size, Align, 0, 0, EltTy);
243}
244
Chris Lattneraffb3732008-11-10 06:08:34 +0000245llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
246 llvm::DICompileUnit Unit) {
247 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000248
Sanjiv Gupta98070572008-05-25 05:15:42 +0000249 // Bit size, align and offset of the type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000250 uint64_t Size = M->getContext().getTypeSize(Ty);
251 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattneraffb3732008-11-10 06:08:34 +0000253 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
254 "", llvm::DICompileUnit(),
255 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000256}
257
Mike Stump31f099c2009-05-14 02:03:51 +0000258llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
259 llvm::DICompileUnit Unit) {
260 if (BlockLiteralGenericSet)
261 return BlockLiteralGeneric;
262
263 llvm::DICompileUnit DefUnit;
264 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
265
266 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
267
268 llvm::DIType FieldTy;
269
270 QualType FType;
271 uint64_t FieldSize, FieldOffset;
272 unsigned FieldAlign;
273
274 llvm::DIArray Elements;
275 llvm::DIType EltTy, DescTy;
276
277 FieldOffset = 0;
278 FType = M->getContext().UnsignedLongTy;
279 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
280 FieldSize = M->getContext().getTypeSize(FType);
281 FieldAlign = M->getContext().getTypeAlign(FType);
282 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
283 "reserved", DefUnit,
284 0, FieldSize, FieldAlign,
285 FieldOffset, 0, FieldTy);
286 EltTys.push_back(FieldTy);
287
288 FieldOffset += FieldSize;
289 FType = M->getContext().UnsignedLongTy;
290 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
291 FieldSize = M->getContext().getTypeSize(FType);
292 FieldAlign = M->getContext().getTypeAlign(FType);
293 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
294 "Size", DefUnit,
295 0, FieldSize, FieldAlign,
296 FieldOffset, 0, FieldTy);
297 EltTys.push_back(FieldTy);
298
299 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000300 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000301 EltTys.clear();
302
Mike Stump581b9ad2009-10-02 02:30:50 +0000303 unsigned Flags = llvm::DIType::FlagAppleBlock;
304
Mike Stump31f099c2009-05-14 02:03:51 +0000305 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump581b9ad2009-10-02 02:30:50 +0000306 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000307 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Mike Stump31f099c2009-05-14 02:03:51 +0000309 // Bit size, align and offset of the type.
310 uint64_t Size = M->getContext().getTypeSize(Ty);
311 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Mike Stump31f099c2009-05-14 02:03:51 +0000313 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
314 Unit, "", llvm::DICompileUnit(),
315 0, Size, Align, 0, 0, EltTy);
316
317 FieldOffset = 0;
318 FType = M->getContext().getPointerType(M->getContext().VoidTy);
319 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
320 FieldSize = M->getContext().getTypeSize(FType);
321 FieldAlign = M->getContext().getTypeAlign(FType);
322 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
323 "__isa", DefUnit,
324 0, FieldSize, FieldAlign,
325 FieldOffset, 0, FieldTy);
326 EltTys.push_back(FieldTy);
327
328 FieldOffset += FieldSize;
329 FType = M->getContext().IntTy;
330 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
331 FieldSize = M->getContext().getTypeSize(FType);
332 FieldAlign = M->getContext().getTypeAlign(FType);
333 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
334 "__flags", DefUnit,
335 0, FieldSize, FieldAlign,
336 FieldOffset, 0, FieldTy);
337 EltTys.push_back(FieldTy);
338
339 FieldOffset += FieldSize;
340 FType = M->getContext().IntTy;
341 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
342 FieldSize = M->getContext().getTypeSize(FType);
343 FieldAlign = M->getContext().getTypeAlign(FType);
344 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
345 "__reserved", DefUnit,
346 0, FieldSize, FieldAlign,
347 FieldOffset, 0, FieldTy);
348 EltTys.push_back(FieldTy);
349
350 FieldOffset += FieldSize;
351 FType = M->getContext().getPointerType(M->getContext().VoidTy);
352 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
353 FieldSize = M->getContext().getTypeSize(FType);
354 FieldAlign = M->getContext().getTypeAlign(FType);
355 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
356 "__FuncPtr", DefUnit,
357 0, FieldSize, FieldAlign,
358 FieldOffset, 0, FieldTy);
359 EltTys.push_back(FieldTy);
360
361 FieldOffset += FieldSize;
362 FType = M->getContext().getPointerType(M->getContext().VoidTy);
363 FieldTy = DescTy;
364 FieldSize = M->getContext().getTypeSize(Ty);
365 FieldAlign = M->getContext().getTypeAlign(Ty);
366 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
367 "__descriptor", DefUnit,
368 0, FieldSize, FieldAlign,
369 FieldOffset, 0, FieldTy);
370 EltTys.push_back(FieldTy);
371
372 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000373 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000374
375 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump440af3d2009-10-02 02:23:37 +0000376 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000377 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000378
Mike Stump31f099c2009-05-14 02:03:51 +0000379 BlockLiteralGenericSet = true;
380 BlockLiteralGeneric
381 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
382 "", llvm::DICompileUnit(),
383 0, Size, Align, 0, 0, EltTy);
384 return BlockLiteralGeneric;
385}
386
Chris Lattneraffb3732008-11-10 06:08:34 +0000387llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
388 llvm::DICompileUnit Unit) {
389 // Typedefs are derived from some other type. If we have a typedef of a
390 // typedef, make sure to emit the whole chain.
391 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000392
Chris Lattneraffb3732008-11-10 06:08:34 +0000393 // We don't set size information, but do specify where the typedef was
394 // declared.
Chris Lattner86d7d912008-11-24 03:54:41 +0000395 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattneraffb3732008-11-10 06:08:34 +0000396 SourceLocation DefLoc = Ty->getDecl()->getLocation();
397 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000398
Sanjiv Gupta98070572008-05-25 05:15:42 +0000399 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +0000400 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
401 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta98070572008-05-25 05:15:42 +0000402
Chris Lattneraffb3732008-11-10 06:08:34 +0000403 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
404 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000405}
406
Chris Lattneraffb3732008-11-10 06:08:34 +0000407llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
408 llvm::DICompileUnit Unit) {
409 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000410
Chris Lattneraffb3732008-11-10 06:08:34 +0000411 // Add the result type at least.
412 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattneraffb3732008-11-10 06:08:34 +0000414 // Set up remainder of arguments if there is a prototype.
415 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000416 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000417 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
418 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
419 } else {
420 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000421 }
422
Chris Lattneraffb3732008-11-10 06:08:34 +0000423 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000424 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattneraffb3732008-11-10 06:08:34 +0000426 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
427 Unit, "", llvm::DICompileUnit(),
428 0, 0, 0, 0, 0,
429 llvm::DIType(), EltTypeArray);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000430}
431
Devang Patel410dc002009-02-25 01:36:11 +0000432/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000433llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
434 llvm::DICompileUnit Unit) {
Douglas Gregore0295612008-12-11 17:59:21 +0000435 RecordDecl *Decl = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattneraffb3732008-11-10 06:08:34 +0000437 unsigned Tag;
438 if (Decl->isStruct())
439 Tag = llvm::dwarf::DW_TAG_structure_type;
440 else if (Decl->isUnion())
441 Tag = llvm::dwarf::DW_TAG_union_type;
442 else {
443 assert(Decl->isClass() && "Unknown RecordType!");
444 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000445 }
446
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000447 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000448
Chris Lattneraffb3732008-11-10 06:08:34 +0000449 // Get overall information about the record type for the debug info.
Chris Lattner86d7d912008-11-24 03:54:41 +0000450 std::string Name = Decl->getNameAsString();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000451
Devang Patel12f0dea2009-04-17 21:35:15 +0000452 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner448a2282009-05-05 05:16:17 +0000453 llvm::DICompileUnit DefUnit;
454 unsigned Line = 0;
455 if (!PLoc.isInvalid()) {
456 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
457 Line = PLoc.getLine();
458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattneraffb3732008-11-10 06:08:34 +0000460 // Records and classes and unions can all be recursive. To handle them, we
461 // first generate a debug descriptor for the struct as a forward declaration.
462 // Then (if it is a definition) we go through and get debug info for all of
463 // its members. Finally, we create a descriptor for the complete type (which
464 // may refer to the forward decl if the struct is recursive) and replace all
465 // uses of the forward declaration with the final definition.
Devang Patel06cceef2009-07-22 18:57:00 +0000466 llvm::DICompositeType FwdDecl =
Chris Lattneraffb3732008-11-10 06:08:34 +0000467 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
468 llvm::DIType(), llvm::DIArray());
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattneraffb3732008-11-10 06:08:34 +0000470 // If this is just a forward declaration, return it.
471 if (!Decl->getDefinition(M->getContext()))
472 return FwdDecl;
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000473
Chris Lattneraffb3732008-11-10 06:08:34 +0000474 // Otherwise, insert it into the TypeCache so that recursive uses will find
475 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000476 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000477
478 // Convert all the elements.
479 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
480
481 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
482
483 unsigned FieldNo = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000484 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000485 E = Decl->field_end();
Douglas Gregore0295612008-12-11 17:59:21 +0000486 I != E; ++I, ++FieldNo) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000487 FieldDecl *Field = *I;
488 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner86d7d912008-11-24 03:54:41 +0000489
490 std::string FieldName = Field->getNameAsString();
Chris Lattneraffb3732008-11-10 06:08:34 +0000491
Devang Pateldf348f12009-04-27 22:40:36 +0000492 // Ignore unnamed fields.
493 if (FieldName.empty())
494 continue;
495
Chris Lattneraffb3732008-11-10 06:08:34 +0000496 // Get the location for the field.
497 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel12f0dea2009-04-17 21:35:15 +0000498 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattner448a2282009-05-05 05:16:17 +0000499 llvm::DICompileUnit FieldDefUnit;
500 unsigned FieldLine = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner448a2282009-05-05 05:16:17 +0000502 if (!PLoc.isInvalid()) {
503 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
504 FieldLine = PLoc.getLine();
505 }
Devang Patelbd933512009-03-16 23:47:53 +0000506
507 QualType FType = Field->getType();
508 uint64_t FieldSize = 0;
509 unsigned FieldAlign = 0;
510 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000511
Devang Patelbd933512009-03-16 23:47:53 +0000512 // Bit size, align and offset of the type.
513 FieldSize = M->getContext().getTypeSize(FType);
514 Expr *BitWidth = Field->getBitWidth();
515 if (BitWidth)
Eli Friedman1c4a1752009-04-26 19:19:15 +0000516 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000517
Devang Patelbd933512009-03-16 23:47:53 +0000518 FieldAlign = M->getContext().getTypeAlign(FType);
519 }
520
Mike Stump11289f42009-09-09 15:08:12 +0000521 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
522
Chris Lattneraffb3732008-11-10 06:08:34 +0000523 // Create a DW_TAG_member node to remember the offset of this field in the
524 // struct. FIXME: This is an absolutely insane way to capture this
525 // information. When we gut debug info, this should be fixed.
526 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
527 FieldName, FieldDefUnit,
528 FieldLine, FieldSize, FieldAlign,
529 FieldOffset, 0, FieldTy);
Chris Lattneraffb3732008-11-10 06:08:34 +0000530 EltTys.push_back(FieldTy);
531 }
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattneraffb3732008-11-10 06:08:34 +0000533 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000534 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000535
536 // Bit size, align and offset of the type.
537 uint64_t Size = M->getContext().getTypeSize(Ty);
538 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000539
Devang Patel06cceef2009-07-22 18:57:00 +0000540 llvm::DICompositeType RealDecl =
Chris Lattneraffb3732008-11-10 06:08:34 +0000541 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
542 Align, 0, 0, llvm::DIType(), Elements);
543
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000544 // Update TypeCache.
545 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
546
Chris Lattneraffb3732008-11-10 06:08:34 +0000547 // Now that we have a real decl for the struct, replace anything using the
548 // old decl with the new one. This will recursively update the debug info.
Devang Patel06cceef2009-07-22 18:57:00 +0000549 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000550
Chris Lattneraffb3732008-11-10 06:08:34 +0000551 return RealDecl;
552}
553
Devang Patelf4c205b2009-02-26 21:10:26 +0000554/// CreateType - get objective-c interface type.
555llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
556 llvm::DICompileUnit Unit) {
557 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000558
Devang Patelf4c205b2009-02-26 21:10:26 +0000559 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
560 SourceManager &SM = M->getContext().getSourceManager();
561
562 // Get overall information about the record type for the debug info.
563 std::string Name = Decl->getNameAsString();
564
565 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000566 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
567 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
568
Mike Stump11289f42009-09-09 15:08:12 +0000569
Daniel Dunbarc61d0bd2009-05-18 20:51:58 +0000570 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000571
Devang Patelf4c205b2009-02-26 21:10:26 +0000572 // To handle recursive interface, we
573 // first generate a debug descriptor for the struct as a forward declaration.
574 // Then (if it is a definition) we go through and get debug info for all of
575 // its members. Finally, we create a descriptor for the complete type (which
576 // may refer to the forward decl if the struct is recursive) and replace all
577 // uses of the forward declaration with the final definition.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000578 llvm::DICompositeType FwdDecl =
Devang Patelf4c205b2009-02-26 21:10:26 +0000579 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000580 llvm::DIType(), llvm::DIArray(),
581 RuntimeLang);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Devang Patelf4c205b2009-02-26 21:10:26 +0000583 // If this is just a forward declaration, return it.
584 if (Decl->isForwardDecl())
585 return FwdDecl;
586
587 // Otherwise, insert it into the TypeCache so that recursive uses will find
588 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000589 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000590
591 // Convert all the elements.
592 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
593
Devang Patelc0f58ea2009-03-10 21:30:26 +0000594 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
595 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +0000596 llvm::DIType SClassTy =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000597 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000598 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000599 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattnerf216fd92009-05-05 05:05:36 +0000600 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +0000601 0 /* offset */, 0, SClassTy);
602 EltTys.push_back(InhTag);
603 }
604
Devang Patelf4c205b2009-02-26 21:10:26 +0000605 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
606
607 unsigned FieldNo = 0;
608 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
609 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
610 ObjCIvarDecl *Field = *I;
611 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
612
613 std::string FieldName = Field->getNameAsString();
614
Devang Pateldf348f12009-04-27 22:40:36 +0000615 // Ignore unnamed fields.
616 if (FieldName.empty())
617 continue;
618
Devang Patelf4c205b2009-02-26 21:10:26 +0000619 // Get the location for the field.
620 SourceLocation FieldDefLoc = Field->getLocation();
621 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel12f0dea2009-04-17 21:35:15 +0000622 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
623 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
624
Mike Stump11289f42009-09-09 15:08:12 +0000625
Devang Patel9f804932009-03-20 18:24:39 +0000626 QualType FType = Field->getType();
627 uint64_t FieldSize = 0;
628 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +0000629
Devang Patel9f804932009-03-20 18:24:39 +0000630 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000631
Devang Patel9f804932009-03-20 18:24:39 +0000632 // Bit size, align and offset of the type.
633 FieldSize = M->getContext().getTypeSize(FType);
634 Expr *BitWidth = Field->getBitWidth();
635 if (BitWidth)
Eli Friedman1c4a1752009-04-26 19:19:15 +0000636 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
637
Devang Patel9f804932009-03-20 18:24:39 +0000638 FieldAlign = M->getContext().getTypeAlign(FType);
639 }
640
Mike Stump11289f42009-09-09 15:08:12 +0000641 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
642
Devang Patelec4bad52009-03-19 00:23:53 +0000643 unsigned Flags = 0;
644 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
645 Flags = llvm::DIType::FlagProtected;
646 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
647 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Devang Patelf4c205b2009-02-26 21:10:26 +0000649 // Create a DW_TAG_member node to remember the offset of this field in the
650 // struct. FIXME: This is an absolutely insane way to capture this
651 // information. When we gut debug info, this should be fixed.
652 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
653 FieldName, FieldDefUnit,
654 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +0000655 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +0000656 EltTys.push_back(FieldTy);
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Devang Patelf4c205b2009-02-26 21:10:26 +0000659 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +0000660 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +0000661
662 // Bit size, align and offset of the type.
663 uint64_t Size = M->getContext().getTypeSize(Ty);
664 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000666 llvm::DICompositeType RealDecl =
Devang Patelf4c205b2009-02-26 21:10:26 +0000667 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000668 Align, 0, 0, llvm::DIType(), Elements,
669 RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +0000670
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000671 // Update TypeCache.
672 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
673
Devang Patelf4c205b2009-02-26 21:10:26 +0000674 // Now that we have a real decl for the struct, replace anything using the
675 // old decl with the new one. This will recursively update the debug info.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000676 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000677
Devang Patelf4c205b2009-02-26 21:10:26 +0000678 return RealDecl;
679}
680
Chris Lattneraffb3732008-11-10 06:08:34 +0000681llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
682 llvm::DICompileUnit Unit) {
683 EnumDecl *Decl = Ty->getDecl();
684
685 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
686
687 // Create DIEnumerator elements for each enumerator.
Mike Stump11289f42009-09-09 15:08:12 +0000688 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000689 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000690 Enum != EnumEnd; ++Enum) {
691 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
692 Enum->getInitVal().getZExtValue()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattneraffb3732008-11-10 06:08:34 +0000695 // Return a CompositeType for the enum itself.
696 llvm::DIArray EltArray =
Jay Foad7d0479f2009-05-21 09:52:38 +0000697 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000698
Chris Lattner86d7d912008-11-24 03:54:41 +0000699 std::string EnumName = Decl->getNameAsString();
Chris Lattneraffb3732008-11-10 06:08:34 +0000700 SourceLocation DefLoc = Decl->getLocation();
701 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
702 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +0000703 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
704 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
705
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattneraffb3732008-11-10 06:08:34 +0000707 // Size and align of the type.
Eli Friedman2ad7e172009-05-04 04:39:55 +0000708 uint64_t Size = 0;
709 unsigned Align = 0;
710 if (!Ty->isIncompleteType()) {
711 Size = M->getContext().getTypeSize(Ty);
712 Align = M->getContext().getTypeAlign(Ty);
713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattneraffb3732008-11-10 06:08:34 +0000715 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
716 Unit, EnumName, DefUnit, Line,
717 Size, Align, 0, 0,
718 llvm::DIType(), EltArray);
719}
720
721llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
722 llvm::DICompileUnit Unit) {
723 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
724 return CreateType(RT, Unit);
725 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
726 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattneraffb3732008-11-10 06:08:34 +0000728 return llvm::DIType();
729}
730
731llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
732 llvm::DICompileUnit Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +0000733 uint64_t Size;
734 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +0000735
736
Nuno Lopesbb537dc2009-01-28 00:35:17 +0000737 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +0000738 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +0000739 Size = 0;
740 Align =
Nuno Lopesbb537dc2009-01-28 00:35:17 +0000741 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
742 } else if (Ty->isIncompleteArrayType()) {
743 Size = 0;
744 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +0000745 } else {
746 // Size and align of the whole array, not the element type.
747 Size = M->getContext().getTypeSize(Ty);
748 Align = M->getContext().getTypeAlign(Ty);
749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattneraffb3732008-11-10 06:08:34 +0000751 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
752 // interior arrays, do we care? Why aren't nested arrays represented the
753 // obvious/recursive way?
754 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
755 QualType EltTy(Ty, 0);
756 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000757 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000758 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +0000759 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +0000760 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +0000761 // FIXME: Verify this is right for VLAs.
762 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
763 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattneraffb3732008-11-10 06:08:34 +0000766 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000767 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000768
769 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
770 Unit, "", llvm::DICompileUnit(),
771 0, Size, Align, 0, 0,
772 getOrCreateType(EltTy, Unit),
773 SubscriptArray);
774}
775
776
Sanjiv Gupta98070572008-05-25 05:15:42 +0000777/// getOrCreateType - Get the type from the cache or create a new
778/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +0000779llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
780 llvm::DICompileUnit Unit) {
781 if (Ty.isNull())
782 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000784 // Check for existing entry.
Daniel Dunbar99961382009-09-19 20:17:48 +0000785 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000786 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +0000787 if (it != TypeCache.end()) {
788 // Verify that the debug info still exists.
789 if (&*it->second)
790 return llvm::DIType(cast<llvm::MDNode>(it->second));
791 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000792
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000793 // Otherwise create the type.
794 llvm::DIType Res = CreateTypeNode(Ty, Unit);
795 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
796 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000797}
798
799/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
800/// new one if necessary.
801llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
802 llvm::DICompileUnit Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000803 // Handle qualifiers, which recursively handles what they refer to.
804 if (Ty.hasQualifiers())
805 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000806
807 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000808 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000809#define TYPE(Class, Base)
810#define ABSTRACT_TYPE(Class, Base)
811#define NON_CANONICAL_TYPE(Class, Base)
812#define DEPENDENT_TYPE(Class, Base) case Type::Class:
813#include "clang/AST/TypeNodes.def"
814 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +0000815
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000816 default:
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000817 case Type::LValueReference:
818 case Type::RValueReference:
Chris Lattneraffb3732008-11-10 06:08:34 +0000819 case Type::Vector:
820 case Type::ExtVector:
Eli Friedman015a4742009-02-27 23:15:07 +0000821 case Type::FixedWidthInt:
Eli Friedman015a4742009-02-27 23:15:07 +0000822 case Type::MemberPointer:
Douglas Gregordc572a32009-03-30 22:58:21 +0000823 case Type::TemplateSpecialization:
Douglas Gregor52537682009-03-19 00:18:19 +0000824 case Type::QualifiedName:
Eli Friedman015a4742009-02-27 23:15:07 +0000825 // Unsupported types
Chris Lattneraffb3732008-11-10 06:08:34 +0000826 return llvm::DIType();
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000827 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000828 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000829 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000830 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
831 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
832 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
833 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +0000834 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000835 return CreateType(cast<BlockPointerType>(Ty), Unit);
836 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000837 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000838 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000839 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +0000840 case Type::FunctionProto:
841 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000842 return CreateType(cast<FunctionType>(Ty), Unit);
John McCallfcc33b02009-09-05 00:15:47 +0000843 case Type::Elaborated:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000844 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
845 Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattneraffb3732008-11-10 06:08:34 +0000847 case Type::ConstantArray:
Douglas Gregor04318252009-07-06 15:59:29 +0000848 case Type::ConstantArrayWithExpr:
849 case Type::ConstantArrayWithoutExpr:
Chris Lattneraffb3732008-11-10 06:08:34 +0000850 case Type::VariableArray:
851 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000852 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000853 case Type::TypeOfExpr:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000854 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
855 ->getType(), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000856 case Type::TypeOf:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000857 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000858 case Type::Decltype:
Daniel Dunbarde870bd2009-09-19 19:27:14 +0000859 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000860 }
Sanjiv Gupta98070572008-05-25 05:15:42 +0000861}
862
863/// EmitFunctionStart - Constructs the debug code for entering a function -
864/// "llvm.dbg.func.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +0000865void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +0000866 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +0000867 CGBuilderTy &Builder) {
Devang Patel9be7b202009-07-14 21:31:22 +0000868 const char *LinkageName = Name;
Mike Stump11289f42009-09-09 15:08:12 +0000869
Daniel Dunbar0beb7892009-05-13 23:08:57 +0000870 // Skip the asm prefix if it exists.
Daniel Dunbar79110022009-05-14 01:45:24 +0000871 //
872 // FIXME: This should probably be the unmangled name?
Daniel Dunbar0beb7892009-05-13 23:08:57 +0000873 if (Name[0] == '\01')
874 ++Name;
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattneraffb3732008-11-10 06:08:34 +0000876 // FIXME: Why is this using CurLoc???
877 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000878 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel2ac33a02009-04-08 19:47:04 +0000879 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattneraffb3732008-11-10 06:08:34 +0000881 llvm::DISubprogram SP =
Devang Patel9be7b202009-07-14 21:31:22 +0000882 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattneraffb3732008-11-10 06:08:34 +0000883 getOrCreateType(ReturnType, Unit),
884 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump11289f42009-09-09 15:08:12 +0000885
Devang Patel542ab5f2009-10-06 21:53:41 +0000886#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
887 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
888#endif
Mike Stump11289f42009-09-09 15:08:12 +0000889
Sanjiv Gupta98070572008-05-25 05:15:42 +0000890 // Push function on region stack.
Chris Lattneraffb3732008-11-10 06:08:34 +0000891 RegionStack.push_back(SP);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000892}
893
894
Chris Lattneraffb3732008-11-10 06:08:34 +0000895void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +0000896 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000897
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000898 // Don't bother if things are the same as last time.
899 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000900 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +0000901 || (SM.getInstantiationLineNumber(CurLoc) ==
902 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000903 && SM.isFromSameFile(CurLoc, PrevLoc)))
904 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000905
906 // Update last state.
907 PrevLoc = CurLoc;
908
909 // Get the appropriate compile unit.
Chris Lattneraffb3732008-11-10 06:08:34 +0000910 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel2ac33a02009-04-08 19:47:04 +0000911 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel5d90d622009-10-06 18:36:08 +0000912
913#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
914 llvm::DIDescriptor DR = RegionStack.back();
915 llvm::DIScope DS = llvm::DIScope(DR.getNode());
916 llvm::DILocation DO(NULL);
917 llvm::DILocation DL =
918 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
919 DS, DO);
920 Builder.SetCurrentDebugLocation(DL.getNode());
921#else
Devang Patel2ac33a02009-04-08 19:47:04 +0000922 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump11289f42009-09-09 15:08:12 +0000923 Builder.GetInsertBlock());
Devang Patel5d90d622009-10-06 18:36:08 +0000924#endif
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000925}
926
927/// EmitRegionStart- Constructs the debug code for entering a declarative
928/// region - "llvm.dbg.region.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +0000929void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
930 llvm::DIDescriptor D;
Daniel Dunbar380827c2008-10-17 01:07:56 +0000931 if (!RegionStack.empty())
Chris Lattneraffb3732008-11-10 06:08:34 +0000932 D = RegionStack.back();
Devang Patel124095b2009-08-31 22:00:32 +0000933 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattneraffb3732008-11-10 06:08:34 +0000934 RegionStack.push_back(D);
Devang Patel5d90d622009-10-06 18:36:08 +0000935#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattneraffb3732008-11-10 06:08:34 +0000936 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Devang Patel5d90d622009-10-06 18:36:08 +0000937#endif
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000938}
939
940/// EmitRegionEnd - Constructs the debug code for exiting a declarative
941/// region - "llvm.dbg.region.end."
Chris Lattneraffb3732008-11-10 06:08:34 +0000942void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +0000943 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
944
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000945 // Provide an region stop point.
946 EmitStopPoint(Fn, Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000947
Devang Patel5d90d622009-10-06 18:36:08 +0000948#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattneraffb3732008-11-10 06:08:34 +0000949 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Devang Patel5d90d622009-10-06 18:36:08 +0000950#endif
Sanjiv Gupta98070572008-05-25 05:15:42 +0000951 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000952}
953
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000954/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattneraffb3732008-11-10 06:08:34 +0000955void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
956 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +0000957 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
958
Devang Patelafc1c1d2009-03-27 23:16:32 +0000959 // Do not emit variable debug information while generating optimized code.
960 // The llvm optimizer and code generator are not yet ready to support
961 // optimized code debugging.
962 const CompileOptions &CO = M->getCompileOpts();
963 if (CO.OptimizationLevel)
964 return;
965
Chris Lattner362d8ae2009-05-05 04:57:08 +0000966 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump2114d7c2009-09-22 02:12:52 +0000967 QualType Type = Decl->getType();
968 llvm::DIType Ty = getOrCreateType(Type, Unit);
969 if (Decl->hasAttr<BlocksAttr>()) {
970 llvm::DICompileUnit DefUnit;
971 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
972
973 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
974
975 llvm::DIType FieldTy;
976
977 QualType FType;
978 uint64_t FieldSize, FieldOffset;
979 unsigned FieldAlign;
980
981 llvm::DIArray Elements;
982 llvm::DIType EltTy;
983
984 // Build up structure for the byref. See BuildByRefType.
985 FieldOffset = 0;
986 FType = M->getContext().getPointerType(M->getContext().VoidTy);
987 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
988 FieldSize = M->getContext().getTypeSize(FType);
989 FieldAlign = M->getContext().getTypeAlign(FType);
990 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
991 "__isa", DefUnit,
992 0, FieldSize, FieldAlign,
993 FieldOffset, 0, FieldTy);
994 EltTys.push_back(FieldTy);
995 FieldOffset += FieldSize;
996
997 FType = M->getContext().getPointerType(M->getContext().VoidTy);
998 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
999 FieldSize = M->getContext().getTypeSize(FType);
1000 FieldAlign = M->getContext().getTypeAlign(FType);
1001 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1002 "__forwarding", DefUnit,
1003 0, FieldSize, FieldAlign,
1004 FieldOffset, 0, FieldTy);
1005 EltTys.push_back(FieldTy);
1006 FieldOffset += FieldSize;
1007
1008 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1009 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1010 FieldSize = M->getContext().getTypeSize(FType);
1011 FieldAlign = M->getContext().getTypeAlign(FType);
1012 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1013 "__flags", DefUnit,
1014 0, FieldSize, FieldAlign,
1015 FieldOffset, 0, FieldTy);
1016 EltTys.push_back(FieldTy);
1017 FieldOffset += FieldSize;
1018
1019 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1020 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1021 FieldSize = M->getContext().getTypeSize(FType);
1022 FieldAlign = M->getContext().getTypeAlign(FType);
1023 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1024 "__size", DefUnit,
1025 0, FieldSize, FieldAlign,
1026 FieldOffset, 0, FieldTy);
1027 EltTys.push_back(FieldTy);
1028 FieldOffset += FieldSize;
1029
1030 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1031 if (HasCopyAndDispose) {
1032 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1033 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1034 FieldSize = M->getContext().getTypeSize(FType);
1035 FieldAlign = M->getContext().getTypeAlign(FType);
1036 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1037 "__copy_helper", DefUnit,
1038 0, FieldSize, FieldAlign,
1039 FieldOffset, 0, FieldTy);
1040 EltTys.push_back(FieldTy);
1041 FieldOffset += FieldSize;
1042
1043 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1044 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1045 FieldSize = M->getContext().getTypeSize(FType);
1046 FieldAlign = M->getContext().getTypeAlign(FType);
1047 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1048 "__destroy_helper", DefUnit,
1049 0, FieldSize, FieldAlign,
1050 FieldOffset, 0, FieldTy);
1051 EltTys.push_back(FieldTy);
1052 FieldOffset += FieldSize;
1053 }
1054
1055 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1056 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1057 unsigned AlignedOffsetInBytes
Mike Stump207c680f2009-09-22 02:44:17 +00001058 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump2114d7c2009-09-22 02:12:52 +00001059 unsigned NumPaddingBytes
Mike Stump207c680f2009-09-22 02:44:17 +00001060 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001061
1062 if (NumPaddingBytes > 0) {
1063 llvm::APInt pad(32, NumPaddingBytes);
1064 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1065 pad, ArrayType::Normal, 0);
1066 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1067 FieldSize = M->getContext().getTypeSize(FType);
1068 FieldAlign = M->getContext().getTypeAlign(FType);
1069 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1070 Unit, "", DefUnit,
1071 0, FieldSize, FieldAlign,
1072 FieldOffset, 0, FieldTy);
1073 EltTys.push_back(FieldTy);
1074 FieldOffset += FieldSize;
1075 }
1076 }
1077
1078 FType = Type;
1079 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1080 FieldSize = M->getContext().getTypeSize(FType);
Mike Stump207c680f2009-09-22 02:44:17 +00001081 FieldAlign = Align*8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001082 std::string Name = Decl->getNameAsString();
1083
1084 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1085 Name, DefUnit,
1086 0, FieldSize, FieldAlign,
1087 FieldOffset, 0, FieldTy);
1088 EltTys.push_back(FieldTy);
1089 FieldOffset += FieldSize;
1090
1091 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1092
1093 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1094
1095 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1096 llvm::DICompileUnit(),
1097 0, FieldOffset, 0, 0, Flags,
1098 llvm::DIType(), Elements);
1099 }
Chris Lattner362d8ae2009-05-05 04:57:08 +00001100
Chris Lattneraffb3732008-11-10 06:08:34 +00001101 // Get location information.
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001102 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001103 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner362d8ae2009-05-05 04:57:08 +00001104 unsigned Line = 0;
1105 if (!PLoc.isInvalid())
1106 Line = PLoc.getLine();
1107 else
1108 Unit = llvm::DICompileUnit();
1109
Mike Stump11289f42009-09-09 15:08:12 +00001110
Chris Lattneraffb3732008-11-10 06:08:34 +00001111 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001112 llvm::DIVariable D =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001113 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner362d8ae2009-05-05 04:57:08 +00001114 Unit, Line, Ty);
Chris Lattneraffb3732008-11-10 06:08:34 +00001115 // Insert an llvm.dbg.declare into the current block.
1116 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001117}
1118
Mike Stump2e722b92009-09-30 02:43:10 +00001119/// EmitDeclare - Emit local variable declaration debug info.
1120void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1121 llvm::Value *Storage, CGBuilderTy &Builder,
1122 CodeGenFunction *CGF) {
1123 const ValueDecl *Decl = BDRE->getDecl();
1124 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1125
1126 // Do not emit variable debug information while generating optimized code.
1127 // The llvm optimizer and code generator are not yet ready to support
1128 // optimized code debugging.
1129 const CompileOptions &CO = M->getCompileOpts();
Mike Stump017460a2009-10-01 22:29:41 +00001130 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001131 return;
1132
1133 uint64_t XOffset = 0;
1134 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1135 QualType Type = Decl->getType();
1136 llvm::DIType Ty = getOrCreateType(Type, Unit);
1137 if (Decl->hasAttr<BlocksAttr>()) {
1138 llvm::DICompileUnit DefUnit;
1139 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1140
1141 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1142
1143 llvm::DIType FieldTy;
1144
1145 QualType FType;
1146 uint64_t FieldSize, FieldOffset;
1147 unsigned FieldAlign;
1148
1149 llvm::DIArray Elements;
1150 llvm::DIType EltTy;
1151
1152 // Build up structure for the byref. See BuildByRefType.
1153 FieldOffset = 0;
1154 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1155 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1156 FieldSize = M->getContext().getTypeSize(FType);
1157 FieldAlign = M->getContext().getTypeAlign(FType);
1158 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1159 "__isa", DefUnit,
1160 0, FieldSize, FieldAlign,
1161 FieldOffset, 0, FieldTy);
1162 EltTys.push_back(FieldTy);
1163 FieldOffset += FieldSize;
1164
1165 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1166 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1167 FieldSize = M->getContext().getTypeSize(FType);
1168 FieldAlign = M->getContext().getTypeAlign(FType);
1169 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1170 "__forwarding", DefUnit,
1171 0, FieldSize, FieldAlign,
1172 FieldOffset, 0, FieldTy);
1173 EltTys.push_back(FieldTy);
1174 FieldOffset += FieldSize;
1175
1176 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1177 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1178 FieldSize = M->getContext().getTypeSize(FType);
1179 FieldAlign = M->getContext().getTypeAlign(FType);
1180 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1181 "__flags", DefUnit,
1182 0, FieldSize, FieldAlign,
1183 FieldOffset, 0, FieldTy);
1184 EltTys.push_back(FieldTy);
1185 FieldOffset += FieldSize;
1186
1187 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1188 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1189 FieldSize = M->getContext().getTypeSize(FType);
1190 FieldAlign = M->getContext().getTypeAlign(FType);
1191 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1192 "__size", DefUnit,
1193 0, FieldSize, FieldAlign,
1194 FieldOffset, 0, FieldTy);
1195 EltTys.push_back(FieldTy);
1196 FieldOffset += FieldSize;
1197
1198 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1199 if (HasCopyAndDispose) {
1200 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1201 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1202 FieldSize = M->getContext().getTypeSize(FType);
1203 FieldAlign = M->getContext().getTypeAlign(FType);
1204 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1205 "__copy_helper", DefUnit,
1206 0, FieldSize, FieldAlign,
1207 FieldOffset, 0, FieldTy);
1208 EltTys.push_back(FieldTy);
1209 FieldOffset += FieldSize;
1210
1211 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1212 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1213 FieldSize = M->getContext().getTypeSize(FType);
1214 FieldAlign = M->getContext().getTypeAlign(FType);
1215 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1216 "__destroy_helper", DefUnit,
1217 0, FieldSize, FieldAlign,
1218 FieldOffset, 0, FieldTy);
1219 EltTys.push_back(FieldTy);
1220 FieldOffset += FieldSize;
1221 }
1222
1223 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1224 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1225 unsigned AlignedOffsetInBytes
1226 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1227 unsigned NumPaddingBytes
1228 = AlignedOffsetInBytes - FieldOffset/8;
1229
1230 if (NumPaddingBytes > 0) {
1231 llvm::APInt pad(32, NumPaddingBytes);
1232 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1233 pad, ArrayType::Normal, 0);
1234 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1235 FieldSize = M->getContext().getTypeSize(FType);
1236 FieldAlign = M->getContext().getTypeAlign(FType);
1237 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1238 Unit, "", DefUnit,
1239 0, FieldSize, FieldAlign,
1240 FieldOffset, 0, FieldTy);
1241 EltTys.push_back(FieldTy);
1242 FieldOffset += FieldSize;
1243 }
1244 }
1245
1246 FType = Type;
1247 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1248 FieldSize = M->getContext().getTypeSize(FType);
1249 FieldAlign = Align*8;
1250 std::string Name = Decl->getNameAsString();
1251
1252 XOffset = FieldOffset;
1253 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1254 Name, DefUnit,
1255 0, FieldSize, FieldAlign,
1256 FieldOffset, 0, FieldTy);
1257 EltTys.push_back(FieldTy);
1258 FieldOffset += FieldSize;
1259
1260 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1261
1262 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1263
1264 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1265 llvm::DICompileUnit(),
1266 0, FieldOffset, 0, 0, Flags,
1267 llvm::DIType(), Elements);
1268 }
1269
1270 // Get location information.
1271 SourceManager &SM = M->getContext().getSourceManager();
1272 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1273 unsigned Line = 0;
1274 if (!PLoc.isInvalid())
1275 Line = PLoc.getLine();
1276 else
1277 Unit = llvm::DICompileUnit();
1278
1279 uint64_t offset = CGF->BlockDecls[Decl];
1280 llvm::SmallVector<llvm::Value *, 9> addr;
1281 llvm::LLVMContext &VMContext = M->getLLVMContext();
1282 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1283 llvm::DIFactory::OpDeref));
1284 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1285 llvm::DIFactory::OpPlus));
1286 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1287 offset));
1288 if (BDRE->isByRef()) {
1289 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1290 llvm::DIFactory::OpDeref));
1291 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1292 llvm::DIFactory::OpPlus));
1293 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1294 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1295 offset));
1296 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1297 llvm::DIFactory::OpDeref));
1298 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1299 llvm::DIFactory::OpPlus));
1300 offset = XOffset/8; // offset of x field
1301 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1302 offset));
1303 }
1304
1305 // Create the descriptor for the variable.
1306 llvm::DIVariable D =
1307 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1308 Decl->getNameAsString(), Unit, Line, Ty,
1309 addr);
1310 // Insert an llvm.dbg.declare into the current block.
Mike Stump017460a2009-10-01 22:29:41 +00001311 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stump2e722b92009-09-30 02:43:10 +00001312}
1313
Chris Lattneraffb3732008-11-10 06:08:34 +00001314void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1315 llvm::Value *Storage,
1316 CGBuilderTy &Builder) {
1317 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1318}
1319
Mike Stump2e722b92009-09-30 02:43:10 +00001320void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1321 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1322 CodeGenFunction *CGF) {
1323 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1324}
1325
Chris Lattneraffb3732008-11-10 06:08:34 +00001326/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1327/// variable declaration.
1328void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1329 CGBuilderTy &Builder) {
1330 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1331}
1332
1333
1334
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001335/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001336void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattneraffb3732008-11-10 06:08:34 +00001337 const VarDecl *Decl) {
Devang Patelafc1c1d2009-03-27 23:16:32 +00001338
1339 // Do not emit variable debug information while generating optimized code.
1340 // The llvm optimizer and code generator are not yet ready to support
1341 // optimized code debugging.
1342 const CompileOptions &CO = M->getCompileOpts();
1343 if (CO.OptimizationLevel)
1344 return;
1345
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001346 // Create global variable debug descriptor.
Chris Lattneraffb3732008-11-10 06:08:34 +00001347 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001348 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001349 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1350 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner86d7d912008-11-24 03:54:41 +00001351
1352 std::string Name = Decl->getNameAsString();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001353
1354 QualType T = Decl->getType();
1355 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001356
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001357 // CodeGen turns int[] into int[1] so we'll do the same here.
1358 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001360 ConstVal = 1;
1361 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001362
1363 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001364 ArrayType::Normal, 0);
1365 }
1366
Devang Patelfaf7e9a2009-10-06 00:35:31 +00001367 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
1368 Name, Name, "", Unit, LineNo,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001369 getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001370 Var->hasInternalLinkage(),
1371 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001372}
1373
Devang Patelf4c205b2009-02-26 21:10:26 +00001374/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001375void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patelf4c205b2009-02-26 21:10:26 +00001376 ObjCInterfaceDecl *Decl) {
1377 // Create global variable debug descriptor.
1378 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1379 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001380 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1381 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patelf4c205b2009-02-26 21:10:26 +00001382
1383 std::string Name = Decl->getNameAsString();
1384
Chris Lattner3088a312009-04-01 06:23:52 +00001385 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001386 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001387
Devang Patelf4c205b2009-02-26 21:10:26 +00001388 // CodeGen turns int[] into int[1] so we'll do the same here.
1389 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001390
Devang Patelf4c205b2009-02-26 21:10:26 +00001391 ConstVal = 1;
1392 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001393
1394 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001395 ArrayType::Normal, 0);
1396 }
1397
Devang Patel9be7b202009-07-14 21:31:22 +00001398 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001399 getOrCreateType(T, Unit),
1400 Var->hasInternalLinkage(),
1401 true/*definition*/, Var);
1402}