blob: be742d9b8067dc5e62ad814b0451e60b937ffc24 [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 Stump5a862172009-09-15 21:48:34 +0000125 std::string Producer = "clang " CLANG_VERSION_STRING;
Chris Lattnerb95ee582009-05-02 01:04:13 +0000126 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000127 const char *Flags = ""; // FIXME: Encode command line options.
128
129 // Figure out which version of the ObjC runtime we have.
130 unsigned RuntimeVers = 0;
131 if (LO.ObjC1)
132 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000134 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000135 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
Mike Stump1eb44332009-09-09 15:08:12 +0000136 AbsFileName.getDirname(),
Devang Patel72240d72009-06-26 18:32:22 +0000137 Producer, isMain, isOptimized,
138 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000139}
140
Devang Patel65e99f22009-02-25 01:36:11 +0000141/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000142/// one if necessary.
143llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000144 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000145 unsigned Encoding = 0;
146 switch (BT->getKind()) {
147 default:
148 case BuiltinType::Void:
149 return llvm::DIType();
150 case BuiltinType::UChar:
151 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
152 case BuiltinType::Char_S:
153 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
154 case BuiltinType::UShort:
155 case BuiltinType::UInt:
156 case BuiltinType::ULong:
157 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
158 case BuiltinType::Short:
159 case BuiltinType::Int:
160 case BuiltinType::Long:
161 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
162 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
163 case BuiltinType::Float:
164 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000165 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166 // Bit size, align and offset of the type.
167 uint64_t Size = M->getContext().getTypeSize(BT);
168 uint64_t Align = M->getContext().getTypeAlign(BT);
169 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
171 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000172 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000173 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000174 Offset, /*flags*/ 0, Encoding);
175}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000176
Chris Lattnerb7003772009-04-23 06:13:01 +0000177llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
178 llvm::DICompileUnit Unit) {
179 // Bit size, align and offset of the type.
180 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
181 if (Ty->isComplexIntegerType())
182 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattnerb7003772009-04-23 06:13:01 +0000184 uint64_t Size = M->getContext().getTypeSize(Ty);
185 uint64_t Align = M->getContext().getTypeAlign(Ty);
186 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattnerb7003772009-04-23 06:13:01 +0000188 return DebugFactory.CreateBasicType(Unit, "complex",
189 Unit, 0, Size, Align,
190 Offset, /*flags*/ 0, Encoding);
191}
192
John McCalla1805292009-09-25 01:40:47 +0000193/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000194/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000195llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
196 QualifierCollector Qc;
197 const Type *T = Qc.strip(Ty);
198
199 // Ignore these qualifiers for now.
200 Qc.removeObjCGCAttr();
201 Qc.removeAddressSpace();
202
Chris Lattner9c85ba32008-11-10 06:08:34 +0000203 // We will create one Derived type for one qualifier and recurse to handle any
204 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000205 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000206 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000208 Qc.removeConst();
209 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000210 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000211 Qc.removeVolatile();
212 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000213 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000214 Qc.removeRestrict();
215 } else {
216 assert(Qc.empty() && "Unknown type qualifier for debug info");
217 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
John McCalla1805292009-09-25 01:40:47 +0000220 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
221
Daniel Dunbar3845f862008-10-31 03:54:29 +0000222 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
223 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
225 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000226}
227
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000228llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
229 llvm::DICompileUnit Unit) {
230 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000232 // Bit size, align and offset of the type.
233 uint64_t Size = M->getContext().getTypeSize(Ty);
234 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000236 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
237 "", llvm::DICompileUnit(),
238 0, Size, Align, 0, 0, EltTy);
239}
240
Chris Lattner9c85ba32008-11-10 06:08:34 +0000241llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
242 llvm::DICompileUnit Unit) {
243 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000245 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246 uint64_t Size = M->getContext().getTypeSize(Ty);
247 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
250 "", llvm::DICompileUnit(),
251 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000252}
253
Mike Stump9bc093c2009-05-14 02:03:51 +0000254llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
255 llvm::DICompileUnit Unit) {
256 if (BlockLiteralGenericSet)
257 return BlockLiteralGeneric;
258
259 llvm::DICompileUnit DefUnit;
260 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
261
262 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
263
264 llvm::DIType FieldTy;
265
266 QualType FType;
267 uint64_t FieldSize, FieldOffset;
268 unsigned FieldAlign;
269
270 llvm::DIArray Elements;
271 llvm::DIType EltTy, DescTy;
272
273 FieldOffset = 0;
274 FType = M->getContext().UnsignedLongTy;
275 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
276 FieldSize = M->getContext().getTypeSize(FType);
277 FieldAlign = M->getContext().getTypeAlign(FType);
278 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
279 "reserved", DefUnit,
280 0, FieldSize, FieldAlign,
281 FieldOffset, 0, FieldTy);
282 EltTys.push_back(FieldTy);
283
284 FieldOffset += FieldSize;
285 FType = M->getContext().UnsignedLongTy;
286 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
287 FieldSize = M->getContext().getTypeSize(FType);
288 FieldAlign = M->getContext().getTypeAlign(FType);
289 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
290 "Size", DefUnit,
291 0, FieldSize, FieldAlign,
292 FieldOffset, 0, FieldTy);
293 EltTys.push_back(FieldTy);
294
295 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000296 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000297 EltTys.clear();
298
Mike Stump3d363c52009-10-02 02:30:50 +0000299 unsigned Flags = llvm::DIType::FlagAppleBlock;
300
Mike Stump9bc093c2009-05-14 02:03:51 +0000301 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000302 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000303 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 // Bit size, align and offset of the type.
306 uint64_t Size = M->getContext().getTypeSize(Ty);
307 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Mike Stump9bc093c2009-05-14 02:03:51 +0000309 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
310 Unit, "", llvm::DICompileUnit(),
311 0, Size, Align, 0, 0, EltTy);
312
313 FieldOffset = 0;
314 FType = M->getContext().getPointerType(M->getContext().VoidTy);
315 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
316 FieldSize = M->getContext().getTypeSize(FType);
317 FieldAlign = M->getContext().getTypeAlign(FType);
318 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
319 "__isa", DefUnit,
320 0, FieldSize, FieldAlign,
321 FieldOffset, 0, FieldTy);
322 EltTys.push_back(FieldTy);
323
324 FieldOffset += FieldSize;
325 FType = M->getContext().IntTy;
326 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
327 FieldSize = M->getContext().getTypeSize(FType);
328 FieldAlign = M->getContext().getTypeAlign(FType);
329 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
330 "__flags", DefUnit,
331 0, FieldSize, FieldAlign,
332 FieldOffset, 0, FieldTy);
333 EltTys.push_back(FieldTy);
334
335 FieldOffset += FieldSize;
336 FType = M->getContext().IntTy;
337 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
338 FieldSize = M->getContext().getTypeSize(FType);
339 FieldAlign = M->getContext().getTypeAlign(FType);
340 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
341 "__reserved", DefUnit,
342 0, FieldSize, FieldAlign,
343 FieldOffset, 0, FieldTy);
344 EltTys.push_back(FieldTy);
345
346 FieldOffset += FieldSize;
347 FType = M->getContext().getPointerType(M->getContext().VoidTy);
348 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
349 FieldSize = M->getContext().getTypeSize(FType);
350 FieldAlign = M->getContext().getTypeAlign(FType);
351 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
352 "__FuncPtr", DefUnit,
353 0, FieldSize, FieldAlign,
354 FieldOffset, 0, FieldTy);
355 EltTys.push_back(FieldTy);
356
357 FieldOffset += FieldSize;
358 FType = M->getContext().getPointerType(M->getContext().VoidTy);
359 FieldTy = DescTy;
360 FieldSize = M->getContext().getTypeSize(Ty);
361 FieldAlign = M->getContext().getTypeAlign(Ty);
362 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
363 "__descriptor", DefUnit,
364 0, FieldSize, FieldAlign,
365 FieldOffset, 0, FieldTy);
366 EltTys.push_back(FieldTy);
367
368 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000369 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000370
371 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000372 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000373 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Mike Stump9bc093c2009-05-14 02:03:51 +0000375 BlockLiteralGenericSet = true;
376 BlockLiteralGeneric
377 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
378 "", llvm::DICompileUnit(),
379 0, Size, Align, 0, 0, EltTy);
380 return BlockLiteralGeneric;
381}
382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
384 llvm::DICompileUnit Unit) {
385 // Typedefs are derived from some other type. If we have a typedef of a
386 // typedef, make sure to emit the whole chain.
387 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattner9c85ba32008-11-10 06:08:34 +0000389 // We don't set size information, but do specify where the typedef was
390 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000391 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000392 SourceLocation DefLoc = Ty->getDecl()->getLocation();
393 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000394
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000395 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000396 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
397 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398
Chris Lattner9c85ba32008-11-10 06:08:34 +0000399 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
400 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000401}
402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
404 llvm::DICompileUnit Unit) {
405 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000406
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 // Add the result type at least.
408 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410 // Set up remainder of arguments if there is a prototype.
411 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000412 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
414 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
415 } else {
416 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000417 }
418
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000420 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
423 Unit, "", llvm::DICompileUnit(),
424 0, 0, 0, 0, 0,
425 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000426}
427
Devang Patel65e99f22009-02-25 01:36:11 +0000428/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
430 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000431 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattner9c85ba32008-11-10 06:08:34 +0000433 unsigned Tag;
434 if (Decl->isStruct())
435 Tag = llvm::dwarf::DW_TAG_structure_type;
436 else if (Decl->isUnion())
437 Tag = llvm::dwarf::DW_TAG_union_type;
438 else {
439 assert(Decl->isClass() && "Unknown RecordType!");
440 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000441 }
442
Sanjiv Gupta507de852008-06-09 10:47:41 +0000443 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000446 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000447
Devang Patel4f6fa232009-04-17 21:35:15 +0000448 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000449 llvm::DICompileUnit DefUnit;
450 unsigned Line = 0;
451 if (!PLoc.isInvalid()) {
452 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
453 Line = PLoc.getLine();
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner9c85ba32008-11-10 06:08:34 +0000456 // Records and classes and unions can all be recursive. To handle them, we
457 // first generate a debug descriptor for the struct as a forward declaration.
458 // Then (if it is a definition) we go through and get debug info for all of
459 // its members. Finally, we create a descriptor for the complete type (which
460 // may refer to the forward decl if the struct is recursive) and replace all
461 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000462 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000463 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
464 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattner9c85ba32008-11-10 06:08:34 +0000466 // If this is just a forward declaration, return it.
467 if (!Decl->getDefinition(M->getContext()))
468 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000469
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 // Otherwise, insert it into the TypeCache so that recursive uses will find
471 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000472 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000473
474 // Convert all the elements.
475 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
476
477 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
478
479 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000480 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000481 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000482 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000483 FieldDecl *Field = *I;
484 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000485
486 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000487
Devang Patelde135022009-04-27 22:40:36 +0000488 // Ignore unnamed fields.
489 if (FieldName.empty())
490 continue;
491
Chris Lattner9c85ba32008-11-10 06:08:34 +0000492 // Get the location for the field.
493 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000494 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000495 llvm::DICompileUnit FieldDefUnit;
496 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000498 if (!PLoc.isInvalid()) {
499 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
500 FieldLine = PLoc.getLine();
501 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000502
503 QualType FType = Field->getType();
504 uint64_t FieldSize = 0;
505 unsigned FieldAlign = 0;
506 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Devang Patelec9b5d52009-03-16 23:47:53 +0000508 // Bit size, align and offset of the type.
509 FieldSize = M->getContext().getTypeSize(FType);
510 Expr *BitWidth = Field->getBitWidth();
511 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000512 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Devang Patelec9b5d52009-03-16 23:47:53 +0000514 FieldAlign = M->getContext().getTypeAlign(FType);
515 }
516
Mike Stump1eb44332009-09-09 15:08:12 +0000517 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
518
Chris Lattner9c85ba32008-11-10 06:08:34 +0000519 // Create a DW_TAG_member node to remember the offset of this field in the
520 // struct. FIXME: This is an absolutely insane way to capture this
521 // information. When we gut debug info, this should be fixed.
522 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
523 FieldName, FieldDefUnit,
524 FieldLine, FieldSize, FieldAlign,
525 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000526 EltTys.push_back(FieldTy);
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000530 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531
532 // Bit size, align and offset of the type.
533 uint64_t Size = M->getContext().getTypeSize(Ty);
534 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Devang Patel0ce73f62009-07-22 18:57:00 +0000536 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000537 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
538 Align, 0, 0, llvm::DIType(), Elements);
539
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000540 // Update TypeCache.
541 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
542
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543 // Now that we have a real decl for the struct, replace anything using the
544 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000545 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000546
Chris Lattner9c85ba32008-11-10 06:08:34 +0000547 return RealDecl;
548}
549
Devang Patel9ca36b62009-02-26 21:10:26 +0000550/// CreateType - get objective-c interface type.
551llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
552 llvm::DICompileUnit Unit) {
553 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Devang Patel9ca36b62009-02-26 21:10:26 +0000555 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
556 SourceManager &SM = M->getContext().getSourceManager();
557
558 // Get overall information about the record type for the debug info.
559 std::string Name = Decl->getNameAsString();
560
561 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000562 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
563 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
564
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Daniel Dunbard86d3362009-05-18 20:51:58 +0000566 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000567
Devang Patel9ca36b62009-02-26 21:10:26 +0000568 // To handle recursive interface, we
569 // first generate a debug descriptor for the struct as a forward declaration.
570 // Then (if it is a definition) we go through and get debug info for all of
571 // its members. Finally, we create a descriptor for the complete type (which
572 // may refer to the forward decl if the struct is recursive) and replace all
573 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000574 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000575 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000576 llvm::DIType(), llvm::DIArray(),
577 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Devang Patel9ca36b62009-02-26 21:10:26 +0000579 // If this is just a forward declaration, return it.
580 if (Decl->isForwardDecl())
581 return FwdDecl;
582
583 // Otherwise, insert it into the TypeCache so that recursive uses will find
584 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000585 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000586
587 // Convert all the elements.
588 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
589
Devang Patelfbe899f2009-03-10 21:30:26 +0000590 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
591 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000592 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000593 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000594 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000595 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000596 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000597 0 /* offset */, 0, SClassTy);
598 EltTys.push_back(InhTag);
599 }
600
Devang Patel9ca36b62009-02-26 21:10:26 +0000601 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
602
603 unsigned FieldNo = 0;
604 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
605 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
606 ObjCIvarDecl *Field = *I;
607 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
608
609 std::string FieldName = Field->getNameAsString();
610
Devang Patelde135022009-04-27 22:40:36 +0000611 // Ignore unnamed fields.
612 if (FieldName.empty())
613 continue;
614
Devang Patel9ca36b62009-02-26 21:10:26 +0000615 // Get the location for the field.
616 SourceLocation FieldDefLoc = Field->getLocation();
617 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000618 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
619 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
620
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Devang Patel99c20eb2009-03-20 18:24:39 +0000622 QualType FType = Field->getType();
623 uint64_t FieldSize = 0;
624 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000625
Devang Patel99c20eb2009-03-20 18:24:39 +0000626 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Devang Patel99c20eb2009-03-20 18:24:39 +0000628 // Bit size, align and offset of the type.
629 FieldSize = M->getContext().getTypeSize(FType);
630 Expr *BitWidth = Field->getBitWidth();
631 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000632 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
633
Devang Patel99c20eb2009-03-20 18:24:39 +0000634 FieldAlign = M->getContext().getTypeAlign(FType);
635 }
636
Mike Stump1eb44332009-09-09 15:08:12 +0000637 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
638
Devang Patelc20482b2009-03-19 00:23:53 +0000639 unsigned Flags = 0;
640 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
641 Flags = llvm::DIType::FlagProtected;
642 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
643 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Devang Patel9ca36b62009-02-26 21:10:26 +0000645 // Create a DW_TAG_member node to remember the offset of this field in the
646 // struct. FIXME: This is an absolutely insane way to capture this
647 // information. When we gut debug info, this should be fixed.
648 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
649 FieldName, FieldDefUnit,
650 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000651 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000652 EltTys.push_back(FieldTy);
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Devang Patel9ca36b62009-02-26 21:10:26 +0000655 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000656 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000657
658 // Bit size, align and offset of the type.
659 uint64_t Size = M->getContext().getTypeSize(Ty);
660 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Devang Patel6c1fddf2009-07-27 18:42:03 +0000662 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000663 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000664 Align, 0, 0, llvm::DIType(), Elements,
665 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000666
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000667 // Update TypeCache.
668 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
669
Devang Patel9ca36b62009-02-26 21:10:26 +0000670 // Now that we have a real decl for the struct, replace anything using the
671 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000672 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000673
Devang Patel9ca36b62009-02-26 21:10:26 +0000674 return RealDecl;
675}
676
Chris Lattner9c85ba32008-11-10 06:08:34 +0000677llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
678 llvm::DICompileUnit Unit) {
679 EnumDecl *Decl = Ty->getDecl();
680
681 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
682
683 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000684 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000685 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000686 Enum != EnumEnd; ++Enum) {
687 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
688 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattner9c85ba32008-11-10 06:08:34 +0000691 // Return a CompositeType for the enum itself.
692 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000693 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000694
Chris Lattner8ec03f52008-11-24 03:54:41 +0000695 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000696 SourceLocation DefLoc = Decl->getLocation();
697 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
698 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000699 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
700 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Chris Lattner9c85ba32008-11-10 06:08:34 +0000703 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000704 uint64_t Size = 0;
705 unsigned Align = 0;
706 if (!Ty->isIncompleteType()) {
707 Size = M->getContext().getTypeSize(Ty);
708 Align = M->getContext().getTypeAlign(Ty);
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner9c85ba32008-11-10 06:08:34 +0000711 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
712 Unit, EnumName, DefUnit, Line,
713 Size, Align, 0, 0,
714 llvm::DIType(), EltArray);
715}
716
717llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
718 llvm::DICompileUnit Unit) {
719 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
720 return CreateType(RT, Unit);
721 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
722 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Chris Lattner9c85ba32008-11-10 06:08:34 +0000724 return llvm::DIType();
725}
726
727llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
728 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000729 uint64_t Size;
730 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
732
Nuno Lopes010d5142009-01-28 00:35:17 +0000733 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000734 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000735 Size = 0;
736 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000737 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
738 } else if (Ty->isIncompleteArrayType()) {
739 Size = 0;
740 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000741 } else {
742 // Size and align of the whole array, not the element type.
743 Size = M->getContext().getTypeSize(Ty);
744 Align = M->getContext().getTypeAlign(Ty);
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
748 // interior arrays, do we care? Why aren't nested arrays represented the
749 // obvious/recursive way?
750 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
751 QualType EltTy(Ty, 0);
752 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000753 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000754 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000755 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000756 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000757 // FIXME: Verify this is right for VLAs.
758 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
759 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000763 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764
765 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
766 Unit, "", llvm::DICompileUnit(),
767 0, Size, Align, 0, 0,
768 getOrCreateType(EltTy, Unit),
769 SubscriptArray);
770}
771
772
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000773/// getOrCreateType - Get the type from the cache or create a new
774/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
776 llvm::DICompileUnit Unit) {
777 if (Ty.isNull())
778 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000780 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000781 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000782 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000783 if (it != TypeCache.end()) {
784 // Verify that the debug info still exists.
785 if (&*it->second)
786 return llvm::DIType(cast<llvm::MDNode>(it->second));
787 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000788
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000789 // Otherwise create the type.
790 llvm::DIType Res = CreateTypeNode(Ty, Unit);
791 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
792 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000793}
794
795/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
796/// new one if necessary.
797llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
798 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000799 // Handle qualifiers, which recursively handles what they refer to.
800 if (Ty.hasQualifiers())
801 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000802
803 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000805#define TYPE(Class, Base)
806#define ABSTRACT_TYPE(Class, Base)
807#define NON_CANONICAL_TYPE(Class, Base)
808#define DEPENDENT_TYPE(Class, Base) case Type::Class:
809#include "clang/AST/TypeNodes.def"
810 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000811
Daniel Dunbar03faac32009-09-19 19:27:14 +0000812 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000813 case Type::LValueReference:
814 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000815 case Type::Vector:
816 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000817 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000818 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000819 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000820 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000821 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000823 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000824 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000825 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000826 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
827 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
828 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
829 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000830 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000831 return CreateType(cast<BlockPointerType>(Ty), Unit);
832 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000833 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000834 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000835 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 case Type::FunctionProto:
837 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000838 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000839 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000840 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
841 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner9c85ba32008-11-10 06:08:34 +0000843 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000844 case Type::ConstantArrayWithExpr:
845 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000846 case Type::VariableArray:
847 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000848 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000849 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000850 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
851 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000852 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000853 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000854 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000855 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000856 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000857}
858
859/// EmitFunctionStart - Constructs the debug code for entering a function -
860/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000861void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000862 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000863 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000864 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Daniel Dunbara2893932009-05-13 23:08:57 +0000866 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000867 //
868 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000869 if (Name[0] == '\01')
870 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Chris Lattner9c85ba32008-11-10 06:08:34 +0000872 // FIXME: Why is this using CurLoc???
873 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000874 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000875 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Chris Lattner9c85ba32008-11-10 06:08:34 +0000877 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000878 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879 getOrCreateType(ReturnType, Unit),
880 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner9c85ba32008-11-10 06:08:34 +0000882 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000884 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000886}
887
888
Chris Lattner9c85ba32008-11-10 06:08:34 +0000889void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000890 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000892 // Don't bother if things are the same as last time.
893 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000894 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000895 || (SM.getInstantiationLineNumber(CurLoc) ==
896 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000897 && SM.isFromSameFile(CurLoc, PrevLoc)))
898 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000899
900 // Update last state.
901 PrevLoc = CurLoc;
902
903 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000905 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
906 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000907 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000908}
909
910/// EmitRegionStart- Constructs the debug code for entering a declarative
911/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000912void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
913 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000914 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000915 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000916 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000917 RegionStack.push_back(D);
918 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000919}
920
921/// EmitRegionEnd - Constructs the debug code for exiting a declarative
922/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000923void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000924 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
925
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000926 // Provide an region stop point.
927 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Chris Lattner9c85ba32008-11-10 06:08:34 +0000929 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000930 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000931}
932
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000933/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000934void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
935 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000936 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
937
Devang Patel07739032009-03-27 23:16:32 +0000938 // Do not emit variable debug information while generating optimized code.
939 // The llvm optimizer and code generator are not yet ready to support
940 // optimized code debugging.
941 const CompileOptions &CO = M->getCompileOpts();
942 if (CO.OptimizationLevel)
943 return;
944
Chris Lattner650cea92009-05-05 04:57:08 +0000945 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000946 QualType Type = Decl->getType();
947 llvm::DIType Ty = getOrCreateType(Type, Unit);
948 if (Decl->hasAttr<BlocksAttr>()) {
949 llvm::DICompileUnit DefUnit;
950 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
951
952 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
953
954 llvm::DIType FieldTy;
955
956 QualType FType;
957 uint64_t FieldSize, FieldOffset;
958 unsigned FieldAlign;
959
960 llvm::DIArray Elements;
961 llvm::DIType EltTy;
962
963 // Build up structure for the byref. See BuildByRefType.
964 FieldOffset = 0;
965 FType = M->getContext().getPointerType(M->getContext().VoidTy);
966 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
967 FieldSize = M->getContext().getTypeSize(FType);
968 FieldAlign = M->getContext().getTypeAlign(FType);
969 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
970 "__isa", DefUnit,
971 0, FieldSize, FieldAlign,
972 FieldOffset, 0, FieldTy);
973 EltTys.push_back(FieldTy);
974 FieldOffset += FieldSize;
975
976 FType = M->getContext().getPointerType(M->getContext().VoidTy);
977 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
978 FieldSize = M->getContext().getTypeSize(FType);
979 FieldAlign = M->getContext().getTypeAlign(FType);
980 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
981 "__forwarding", DefUnit,
982 0, FieldSize, FieldAlign,
983 FieldOffset, 0, FieldTy);
984 EltTys.push_back(FieldTy);
985 FieldOffset += FieldSize;
986
987 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
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 "__flags", DefUnit,
993 0, FieldSize, FieldAlign,
994 FieldOffset, 0, FieldTy);
995 EltTys.push_back(FieldTy);
996 FieldOffset += FieldSize;
997
998 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
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 "__size", DefUnit,
1004 0, FieldSize, FieldAlign,
1005 FieldOffset, 0, FieldTy);
1006 EltTys.push_back(FieldTy);
1007 FieldOffset += FieldSize;
1008
1009 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1010 if (HasCopyAndDispose) {
1011 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1012 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1013 FieldSize = M->getContext().getTypeSize(FType);
1014 FieldAlign = M->getContext().getTypeAlign(FType);
1015 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1016 "__copy_helper", DefUnit,
1017 0, FieldSize, FieldAlign,
1018 FieldOffset, 0, FieldTy);
1019 EltTys.push_back(FieldTy);
1020 FieldOffset += FieldSize;
1021
1022 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1023 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1024 FieldSize = M->getContext().getTypeSize(FType);
1025 FieldAlign = M->getContext().getTypeAlign(FType);
1026 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1027 "__destroy_helper", DefUnit,
1028 0, FieldSize, FieldAlign,
1029 FieldOffset, 0, FieldTy);
1030 EltTys.push_back(FieldTy);
1031 FieldOffset += FieldSize;
1032 }
1033
1034 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1035 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1036 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001037 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001038 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001039 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001040
1041 if (NumPaddingBytes > 0) {
1042 llvm::APInt pad(32, NumPaddingBytes);
1043 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1044 pad, ArrayType::Normal, 0);
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,
1049 Unit, "", DefUnit,
1050 0, FieldSize, FieldAlign,
1051 FieldOffset, 0, FieldTy);
1052 EltTys.push_back(FieldTy);
1053 FieldOffset += FieldSize;
1054 }
1055 }
1056
1057 FType = Type;
1058 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1059 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001060 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001061 std::string Name = Decl->getNameAsString();
1062
1063 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1064 Name, DefUnit,
1065 0, FieldSize, FieldAlign,
1066 FieldOffset, 0, FieldTy);
1067 EltTys.push_back(FieldTy);
1068 FieldOffset += FieldSize;
1069
1070 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1071
1072 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1073
1074 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1075 llvm::DICompileUnit(),
1076 0, FieldOffset, 0, 0, Flags,
1077 llvm::DIType(), Elements);
1078 }
Chris Lattner650cea92009-05-05 04:57:08 +00001079
Chris Lattner9c85ba32008-11-10 06:08:34 +00001080 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001081 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001082 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001083 unsigned Line = 0;
1084 if (!PLoc.isInvalid())
1085 Line = PLoc.getLine();
1086 else
1087 Unit = llvm::DICompileUnit();
1088
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001091 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001092 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001093 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001094 // Insert an llvm.dbg.declare into the current block.
1095 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001096}
1097
Mike Stumpb1a6e682009-09-30 02:43:10 +00001098/// EmitDeclare - Emit local variable declaration debug info.
1099void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1100 llvm::Value *Storage, CGBuilderTy &Builder,
1101 CodeGenFunction *CGF) {
1102 const ValueDecl *Decl = BDRE->getDecl();
1103 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1104
1105 // Do not emit variable debug information while generating optimized code.
1106 // The llvm optimizer and code generator are not yet ready to support
1107 // optimized code debugging.
1108 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001109 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001110 return;
1111
1112 uint64_t XOffset = 0;
1113 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1114 QualType Type = Decl->getType();
1115 llvm::DIType Ty = getOrCreateType(Type, Unit);
1116 if (Decl->hasAttr<BlocksAttr>()) {
1117 llvm::DICompileUnit DefUnit;
1118 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1119
1120 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1121
1122 llvm::DIType FieldTy;
1123
1124 QualType FType;
1125 uint64_t FieldSize, FieldOffset;
1126 unsigned FieldAlign;
1127
1128 llvm::DIArray Elements;
1129 llvm::DIType EltTy;
1130
1131 // Build up structure for the byref. See BuildByRefType.
1132 FieldOffset = 0;
1133 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1134 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1135 FieldSize = M->getContext().getTypeSize(FType);
1136 FieldAlign = M->getContext().getTypeAlign(FType);
1137 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1138 "__isa", DefUnit,
1139 0, FieldSize, FieldAlign,
1140 FieldOffset, 0, FieldTy);
1141 EltTys.push_back(FieldTy);
1142 FieldOffset += FieldSize;
1143
1144 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1145 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1146 FieldSize = M->getContext().getTypeSize(FType);
1147 FieldAlign = M->getContext().getTypeAlign(FType);
1148 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1149 "__forwarding", DefUnit,
1150 0, FieldSize, FieldAlign,
1151 FieldOffset, 0, FieldTy);
1152 EltTys.push_back(FieldTy);
1153 FieldOffset += FieldSize;
1154
1155 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
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 "__flags", DefUnit,
1161 0, FieldSize, FieldAlign,
1162 FieldOffset, 0, FieldTy);
1163 EltTys.push_back(FieldTy);
1164 FieldOffset += FieldSize;
1165
1166 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
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 "__size", DefUnit,
1172 0, FieldSize, FieldAlign,
1173 FieldOffset, 0, FieldTy);
1174 EltTys.push_back(FieldTy);
1175 FieldOffset += FieldSize;
1176
1177 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1178 if (HasCopyAndDispose) {
1179 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1180 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1181 FieldSize = M->getContext().getTypeSize(FType);
1182 FieldAlign = M->getContext().getTypeAlign(FType);
1183 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1184 "__copy_helper", DefUnit,
1185 0, FieldSize, FieldAlign,
1186 FieldOffset, 0, FieldTy);
1187 EltTys.push_back(FieldTy);
1188 FieldOffset += FieldSize;
1189
1190 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1191 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1192 FieldSize = M->getContext().getTypeSize(FType);
1193 FieldAlign = M->getContext().getTypeAlign(FType);
1194 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1195 "__destroy_helper", DefUnit,
1196 0, FieldSize, FieldAlign,
1197 FieldOffset, 0, FieldTy);
1198 EltTys.push_back(FieldTy);
1199 FieldOffset += FieldSize;
1200 }
1201
1202 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1203 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1204 unsigned AlignedOffsetInBytes
1205 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1206 unsigned NumPaddingBytes
1207 = AlignedOffsetInBytes - FieldOffset/8;
1208
1209 if (NumPaddingBytes > 0) {
1210 llvm::APInt pad(32, NumPaddingBytes);
1211 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1212 pad, ArrayType::Normal, 0);
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,
1217 Unit, "", DefUnit,
1218 0, FieldSize, FieldAlign,
1219 FieldOffset, 0, FieldTy);
1220 EltTys.push_back(FieldTy);
1221 FieldOffset += FieldSize;
1222 }
1223 }
1224
1225 FType = Type;
1226 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1227 FieldSize = M->getContext().getTypeSize(FType);
1228 FieldAlign = Align*8;
1229 std::string Name = Decl->getNameAsString();
1230
1231 XOffset = FieldOffset;
1232 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1233 Name, DefUnit,
1234 0, FieldSize, FieldAlign,
1235 FieldOffset, 0, FieldTy);
1236 EltTys.push_back(FieldTy);
1237 FieldOffset += FieldSize;
1238
1239 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1240
1241 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1242
1243 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1244 llvm::DICompileUnit(),
1245 0, FieldOffset, 0, 0, Flags,
1246 llvm::DIType(), Elements);
1247 }
1248
1249 // Get location information.
1250 SourceManager &SM = M->getContext().getSourceManager();
1251 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1252 unsigned Line = 0;
1253 if (!PLoc.isInvalid())
1254 Line = PLoc.getLine();
1255 else
1256 Unit = llvm::DICompileUnit();
1257
1258 uint64_t offset = CGF->BlockDecls[Decl];
1259 llvm::SmallVector<llvm::Value *, 9> addr;
1260 llvm::LLVMContext &VMContext = M->getLLVMContext();
1261 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1262 llvm::DIFactory::OpDeref));
1263 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1264 llvm::DIFactory::OpPlus));
1265 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1266 offset));
1267 if (BDRE->isByRef()) {
1268 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1269 llvm::DIFactory::OpDeref));
1270 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1271 llvm::DIFactory::OpPlus));
1272 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1273 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1274 offset));
1275 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1276 llvm::DIFactory::OpDeref));
1277 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1278 llvm::DIFactory::OpPlus));
1279 offset = XOffset/8; // offset of x field
1280 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1281 offset));
1282 }
1283
1284 // Create the descriptor for the variable.
1285 llvm::DIVariable D =
1286 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1287 Decl->getNameAsString(), Unit, Line, Ty,
1288 addr);
1289 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001290 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001291}
1292
Chris Lattner9c85ba32008-11-10 06:08:34 +00001293void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1294 llvm::Value *Storage,
1295 CGBuilderTy &Builder) {
1296 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1297}
1298
Mike Stumpb1a6e682009-09-30 02:43:10 +00001299void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1300 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1301 CodeGenFunction *CGF) {
1302 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1303}
1304
Chris Lattner9c85ba32008-11-10 06:08:34 +00001305/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1306/// variable declaration.
1307void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1308 CGBuilderTy &Builder) {
1309 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1310}
1311
1312
1313
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001314/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001315void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001316 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001317
1318 // Do not emit variable debug information while generating optimized code.
1319 // The llvm optimizer and code generator are not yet ready to support
1320 // optimized code debugging.
1321 const CompileOptions &CO = M->getCompileOpts();
1322 if (CO.OptimizationLevel)
1323 return;
1324
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001325 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001326 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001327 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001328 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1329 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001330
1331 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001332
1333 QualType T = Decl->getType();
1334 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001336 // CodeGen turns int[] into int[1] so we'll do the same here.
1337 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001339 ConstVal = 1;
1340 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001341
1342 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001343 ArrayType::Normal, 0);
1344 }
1345
Devang Patel979ec2e2009-10-06 00:35:31 +00001346 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
1347 Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001348 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001349 Var->hasInternalLinkage(),
1350 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001351}
1352
Devang Patel9ca36b62009-02-26 21:10:26 +00001353/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001354void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001355 ObjCInterfaceDecl *Decl) {
1356 // Create global variable debug descriptor.
1357 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1358 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001359 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1360 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001361
1362 std::string Name = Decl->getNameAsString();
1363
Chris Lattner03d9f342009-04-01 06:23:52 +00001364 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001365 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Devang Patel9ca36b62009-02-26 21:10:26 +00001367 // CodeGen turns int[] into int[1] so we'll do the same here.
1368 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Devang Patel9ca36b62009-02-26 21:10:26 +00001370 ConstVal = 1;
1371 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001372
1373 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001374 ArrayType::Normal, 0);
1375 }
1376
Devang Patel6dba4322009-07-14 21:31:22 +00001377 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001378 getOrCreateType(T, Unit),
1379 Var->hasInternalLinkage(),
1380 true/*definition*/, Var);
1381}