blob: de9390eb9f415f054fc06851cb6f77e896bd4cac [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
Devang Patelbbd9fa42009-10-06 18:36:08 +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);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000906
907#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
908 llvm::DIDescriptor DR = RegionStack.back();
909 llvm::DIScope DS = llvm::DIScope(DR.getNode());
910 llvm::DILocation DO(NULL);
911 llvm::DILocation DL =
912 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
913 DS, DO);
914 Builder.SetCurrentDebugLocation(DL.getNode());
915#else
Devang Patel0f78fea2009-04-08 19:47:04 +0000916 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000917 Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000918#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000919}
920
921/// EmitRegionStart- Constructs the debug code for entering a declarative
922/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000923void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
924 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000925 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000926 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000927 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000928 RegionStack.push_back(D);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000929#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000931#endif
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000932}
933
934/// EmitRegionEnd - Constructs the debug code for exiting a declarative
935/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000937 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
938
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000939 // Provide an region stop point.
940 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Devang Patelbbd9fa42009-10-06 18:36:08 +0000942#ifndef ATTACH_DEBUG_INFO_TO_AN_INSN
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000944#endif
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000945 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000946}
947
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000948/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000949void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
950 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000951 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
952
Devang Patel07739032009-03-27 23:16:32 +0000953 // Do not emit variable debug information while generating optimized code.
954 // The llvm optimizer and code generator are not yet ready to support
955 // optimized code debugging.
956 const CompileOptions &CO = M->getCompileOpts();
957 if (CO.OptimizationLevel)
958 return;
959
Chris Lattner650cea92009-05-05 04:57:08 +0000960 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000961 QualType Type = Decl->getType();
962 llvm::DIType Ty = getOrCreateType(Type, Unit);
963 if (Decl->hasAttr<BlocksAttr>()) {
964 llvm::DICompileUnit DefUnit;
965 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
966
967 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
968
969 llvm::DIType FieldTy;
970
971 QualType FType;
972 uint64_t FieldSize, FieldOffset;
973 unsigned FieldAlign;
974
975 llvm::DIArray Elements;
976 llvm::DIType EltTy;
977
978 // Build up structure for the byref. See BuildByRefType.
979 FieldOffset = 0;
980 FType = M->getContext().getPointerType(M->getContext().VoidTy);
981 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
982 FieldSize = M->getContext().getTypeSize(FType);
983 FieldAlign = M->getContext().getTypeAlign(FType);
984 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
985 "__isa", DefUnit,
986 0, FieldSize, FieldAlign,
987 FieldOffset, 0, FieldTy);
988 EltTys.push_back(FieldTy);
989 FieldOffset += FieldSize;
990
991 FType = M->getContext().getPointerType(M->getContext().VoidTy);
992 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
993 FieldSize = M->getContext().getTypeSize(FType);
994 FieldAlign = M->getContext().getTypeAlign(FType);
995 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
996 "__forwarding", DefUnit,
997 0, FieldSize, FieldAlign,
998 FieldOffset, 0, FieldTy);
999 EltTys.push_back(FieldTy);
1000 FieldOffset += FieldSize;
1001
1002 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1003 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1004 FieldSize = M->getContext().getTypeSize(FType);
1005 FieldAlign = M->getContext().getTypeAlign(FType);
1006 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1007 "__flags", DefUnit,
1008 0, FieldSize, FieldAlign,
1009 FieldOffset, 0, FieldTy);
1010 EltTys.push_back(FieldTy);
1011 FieldOffset += FieldSize;
1012
1013 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1014 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1015 FieldSize = M->getContext().getTypeSize(FType);
1016 FieldAlign = M->getContext().getTypeAlign(FType);
1017 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1018 "__size", DefUnit,
1019 0, FieldSize, FieldAlign,
1020 FieldOffset, 0, FieldTy);
1021 EltTys.push_back(FieldTy);
1022 FieldOffset += FieldSize;
1023
1024 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1025 if (HasCopyAndDispose) {
1026 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1027 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1028 FieldSize = M->getContext().getTypeSize(FType);
1029 FieldAlign = M->getContext().getTypeAlign(FType);
1030 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1031 "__copy_helper", DefUnit,
1032 0, FieldSize, FieldAlign,
1033 FieldOffset, 0, FieldTy);
1034 EltTys.push_back(FieldTy);
1035 FieldOffset += FieldSize;
1036
1037 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1038 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1039 FieldSize = M->getContext().getTypeSize(FType);
1040 FieldAlign = M->getContext().getTypeAlign(FType);
1041 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1042 "__destroy_helper", DefUnit,
1043 0, FieldSize, FieldAlign,
1044 FieldOffset, 0, FieldTy);
1045 EltTys.push_back(FieldTy);
1046 FieldOffset += FieldSize;
1047 }
1048
1049 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1050 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1051 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001052 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001053 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001054 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001055
1056 if (NumPaddingBytes > 0) {
1057 llvm::APInt pad(32, NumPaddingBytes);
1058 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1059 pad, ArrayType::Normal, 0);
1060 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1061 FieldSize = M->getContext().getTypeSize(FType);
1062 FieldAlign = M->getContext().getTypeAlign(FType);
1063 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1064 Unit, "", DefUnit,
1065 0, FieldSize, FieldAlign,
1066 FieldOffset, 0, FieldTy);
1067 EltTys.push_back(FieldTy);
1068 FieldOffset += FieldSize;
1069 }
1070 }
1071
1072 FType = Type;
1073 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1074 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001075 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001076 std::string Name = Decl->getNameAsString();
1077
1078 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1079 Name, DefUnit,
1080 0, FieldSize, FieldAlign,
1081 FieldOffset, 0, FieldTy);
1082 EltTys.push_back(FieldTy);
1083 FieldOffset += FieldSize;
1084
1085 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1086
1087 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1088
1089 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1090 llvm::DICompileUnit(),
1091 0, FieldOffset, 0, 0, Flags,
1092 llvm::DIType(), Elements);
1093 }
Chris Lattner650cea92009-05-05 04:57:08 +00001094
Chris Lattner9c85ba32008-11-10 06:08:34 +00001095 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001096 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001097 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001098 unsigned Line = 0;
1099 if (!PLoc.isInvalid())
1100 Line = PLoc.getLine();
1101 else
1102 Unit = llvm::DICompileUnit();
1103
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner9c85ba32008-11-10 06:08:34 +00001105 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001107 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001108 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001109 // Insert an llvm.dbg.declare into the current block.
1110 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001111}
1112
Mike Stumpb1a6e682009-09-30 02:43:10 +00001113/// EmitDeclare - Emit local variable declaration debug info.
1114void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1115 llvm::Value *Storage, CGBuilderTy &Builder,
1116 CodeGenFunction *CGF) {
1117 const ValueDecl *Decl = BDRE->getDecl();
1118 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1119
1120 // Do not emit variable debug information while generating optimized code.
1121 // The llvm optimizer and code generator are not yet ready to support
1122 // optimized code debugging.
1123 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001124 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001125 return;
1126
1127 uint64_t XOffset = 0;
1128 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1129 QualType Type = Decl->getType();
1130 llvm::DIType Ty = getOrCreateType(Type, Unit);
1131 if (Decl->hasAttr<BlocksAttr>()) {
1132 llvm::DICompileUnit DefUnit;
1133 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1134
1135 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1136
1137 llvm::DIType FieldTy;
1138
1139 QualType FType;
1140 uint64_t FieldSize, FieldOffset;
1141 unsigned FieldAlign;
1142
1143 llvm::DIArray Elements;
1144 llvm::DIType EltTy;
1145
1146 // Build up structure for the byref. See BuildByRefType.
1147 FieldOffset = 0;
1148 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1149 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1150 FieldSize = M->getContext().getTypeSize(FType);
1151 FieldAlign = M->getContext().getTypeAlign(FType);
1152 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1153 "__isa", DefUnit,
1154 0, FieldSize, FieldAlign,
1155 FieldOffset, 0, FieldTy);
1156 EltTys.push_back(FieldTy);
1157 FieldOffset += FieldSize;
1158
1159 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1160 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1161 FieldSize = M->getContext().getTypeSize(FType);
1162 FieldAlign = M->getContext().getTypeAlign(FType);
1163 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1164 "__forwarding", DefUnit,
1165 0, FieldSize, FieldAlign,
1166 FieldOffset, 0, FieldTy);
1167 EltTys.push_back(FieldTy);
1168 FieldOffset += FieldSize;
1169
1170 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1171 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1172 FieldSize = M->getContext().getTypeSize(FType);
1173 FieldAlign = M->getContext().getTypeAlign(FType);
1174 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1175 "__flags", DefUnit,
1176 0, FieldSize, FieldAlign,
1177 FieldOffset, 0, FieldTy);
1178 EltTys.push_back(FieldTy);
1179 FieldOffset += FieldSize;
1180
1181 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1182 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1183 FieldSize = M->getContext().getTypeSize(FType);
1184 FieldAlign = M->getContext().getTypeAlign(FType);
1185 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1186 "__size", DefUnit,
1187 0, FieldSize, FieldAlign,
1188 FieldOffset, 0, FieldTy);
1189 EltTys.push_back(FieldTy);
1190 FieldOffset += FieldSize;
1191
1192 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1193 if (HasCopyAndDispose) {
1194 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1195 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1196 FieldSize = M->getContext().getTypeSize(FType);
1197 FieldAlign = M->getContext().getTypeAlign(FType);
1198 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1199 "__copy_helper", DefUnit,
1200 0, FieldSize, FieldAlign,
1201 FieldOffset, 0, FieldTy);
1202 EltTys.push_back(FieldTy);
1203 FieldOffset += FieldSize;
1204
1205 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1206 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1207 FieldSize = M->getContext().getTypeSize(FType);
1208 FieldAlign = M->getContext().getTypeAlign(FType);
1209 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1210 "__destroy_helper", DefUnit,
1211 0, FieldSize, FieldAlign,
1212 FieldOffset, 0, FieldTy);
1213 EltTys.push_back(FieldTy);
1214 FieldOffset += FieldSize;
1215 }
1216
1217 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1218 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1219 unsigned AlignedOffsetInBytes
1220 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1221 unsigned NumPaddingBytes
1222 = AlignedOffsetInBytes - FieldOffset/8;
1223
1224 if (NumPaddingBytes > 0) {
1225 llvm::APInt pad(32, NumPaddingBytes);
1226 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1227 pad, ArrayType::Normal, 0);
1228 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1229 FieldSize = M->getContext().getTypeSize(FType);
1230 FieldAlign = M->getContext().getTypeAlign(FType);
1231 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1232 Unit, "", DefUnit,
1233 0, FieldSize, FieldAlign,
1234 FieldOffset, 0, FieldTy);
1235 EltTys.push_back(FieldTy);
1236 FieldOffset += FieldSize;
1237 }
1238 }
1239
1240 FType = Type;
1241 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1242 FieldSize = M->getContext().getTypeSize(FType);
1243 FieldAlign = Align*8;
1244 std::string Name = Decl->getNameAsString();
1245
1246 XOffset = FieldOffset;
1247 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1248 Name, DefUnit,
1249 0, FieldSize, FieldAlign,
1250 FieldOffset, 0, FieldTy);
1251 EltTys.push_back(FieldTy);
1252 FieldOffset += FieldSize;
1253
1254 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1255
1256 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1257
1258 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1259 llvm::DICompileUnit(),
1260 0, FieldOffset, 0, 0, Flags,
1261 llvm::DIType(), Elements);
1262 }
1263
1264 // Get location information.
1265 SourceManager &SM = M->getContext().getSourceManager();
1266 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1267 unsigned Line = 0;
1268 if (!PLoc.isInvalid())
1269 Line = PLoc.getLine();
1270 else
1271 Unit = llvm::DICompileUnit();
1272
1273 uint64_t offset = CGF->BlockDecls[Decl];
1274 llvm::SmallVector<llvm::Value *, 9> addr;
1275 llvm::LLVMContext &VMContext = M->getLLVMContext();
1276 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1277 llvm::DIFactory::OpDeref));
1278 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1279 llvm::DIFactory::OpPlus));
1280 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1281 offset));
1282 if (BDRE->isByRef()) {
1283 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1284 llvm::DIFactory::OpDeref));
1285 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1286 llvm::DIFactory::OpPlus));
1287 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1288 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1289 offset));
1290 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1291 llvm::DIFactory::OpDeref));
1292 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1293 llvm::DIFactory::OpPlus));
1294 offset = XOffset/8; // offset of x field
1295 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1296 offset));
1297 }
1298
1299 // Create the descriptor for the variable.
1300 llvm::DIVariable D =
1301 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1302 Decl->getNameAsString(), Unit, Line, Ty,
1303 addr);
1304 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001305 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001306}
1307
Chris Lattner9c85ba32008-11-10 06:08:34 +00001308void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1309 llvm::Value *Storage,
1310 CGBuilderTy &Builder) {
1311 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1312}
1313
Mike Stumpb1a6e682009-09-30 02:43:10 +00001314void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1315 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1316 CodeGenFunction *CGF) {
1317 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1318}
1319
Chris Lattner9c85ba32008-11-10 06:08:34 +00001320/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1321/// variable declaration.
1322void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1323 CGBuilderTy &Builder) {
1324 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1325}
1326
1327
1328
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001329/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001330void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001331 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001332
1333 // Do not emit variable debug information while generating optimized code.
1334 // The llvm optimizer and code generator are not yet ready to support
1335 // optimized code debugging.
1336 const CompileOptions &CO = M->getCompileOpts();
1337 if (CO.OptimizationLevel)
1338 return;
1339
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001340 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001341 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001342 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001343 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1344 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001345
1346 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001347
1348 QualType T = Decl->getType();
1349 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001351 // CodeGen turns int[] into int[1] so we'll do the same here.
1352 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001354 ConstVal = 1;
1355 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001356
1357 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001358 ArrayType::Normal, 0);
1359 }
1360
Devang Patel979ec2e2009-10-06 00:35:31 +00001361 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit),
1362 Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001363 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001364 Var->hasInternalLinkage(),
1365 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001366}
1367
Devang Patel9ca36b62009-02-26 21:10:26 +00001368/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001369void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001370 ObjCInterfaceDecl *Decl) {
1371 // Create global variable debug descriptor.
1372 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1373 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001374 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1375 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001376
1377 std::string Name = Decl->getNameAsString();
1378
Chris Lattner03d9f342009-04-01 06:23:52 +00001379 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001380 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Devang Patel9ca36b62009-02-26 21:10:26 +00001382 // CodeGen turns int[] into int[1] so we'll do the same here.
1383 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Devang Patel9ca36b62009-02-26 21:10:26 +00001385 ConstVal = 1;
1386 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001387
1388 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001389 ArrayType::Normal, 0);
1390 }
1391
Devang Patel6dba4322009-07-14 21:31:22 +00001392 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001393 getOrCreateType(T, Unit),
1394 Var->hasInternalLinkage(),
1395 true/*definition*/, Var);
1396}