blob: 8dc2ac8d1f057622e6e4b7f962a060f1b7726b45 [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"
15#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000018#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000019#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000020#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000022#include "clang/Basic/Version.h"
Devang Patel07739032009-03-27 23:16:32 +000023#include "clang/Frontend/CompileOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000029#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000031#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000032#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000034using namespace clang;
35using namespace clang::CodeGen;
36
Devang Patel6dba4322009-07-14 21:31:22 +000037CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Mike Stump9bc093c2009-05-14 02:03:51 +000038 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()),
39 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000040}
41
Chris Lattner9c85ba32008-11-10 06:08:34 +000042CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000043 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046void CGDebugInfo::setLocation(SourceLocation Loc) {
47 if (Loc.isValid())
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000048 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000049}
50
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000051/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000052/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000053llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000054 // Get source file information.
55 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000056 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000057 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000058 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000059 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
60 FileName = PLoc.getFilename();
61 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000062 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000065 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000066 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000067
Devang Patel446c6192009-04-17 21:06:59 +000068 // Get absolute path name.
69 llvm::sys::Path AbsFileName(FileName);
70 if (!AbsFileName.isAbsolute()) {
71 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
72 tmp.appendComponent(FileName);
73 AbsFileName = tmp;
74 }
75
Devang Patel72240d72009-06-26 18:32:22 +000076 // See if thie compile unit is representing main source file. Each source
77 // file has corresponding compile unit. There is only one main source
78 // file at a time.
79 bool isMain = false;
80 const LangOptions &LO = M->getLangOptions();
81 const char *MainFileName = LO.getMainFileName();
82 if (isMainCompileUnitCreated == false) {
83 if (MainFileName) {
84 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
85 isMain = true;
86 } else {
87 if (Loc.isValid() && SM.isFromMainFile(Loc))
88 isMain = true;
89 }
90 if (isMain)
91 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000092 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000093
Chris Lattner515455a2009-03-25 03:28:08 +000094 unsigned LangTag;
95 if (LO.CPlusPlus) {
96 if (LO.ObjC1)
97 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
98 else
99 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
100 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000101 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000102 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000103 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000104 } else {
105 LangTag = llvm::dwarf::DW_LANG_C89;
106 }
Devang Patel446c6192009-04-17 21:06:59 +0000107
Mike Stump5a862172009-09-15 21:48:34 +0000108 std::string Producer = "clang " CLANG_VERSION_STRING;
Chris Lattnerb95ee582009-05-02 01:04:13 +0000109 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000110 const char *Flags = ""; // FIXME: Encode command line options.
111
112 // Figure out which version of the ObjC runtime we have.
113 unsigned RuntimeVers = 0;
114 if (LO.ObjC1)
115 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000117 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000118 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
Mike Stump1eb44332009-09-09 15:08:12 +0000119 AbsFileName.getDirname(),
Devang Patel72240d72009-06-26 18:32:22 +0000120 Producer, isMain, isOptimized,
121 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000122}
123
Devang Patel65e99f22009-02-25 01:36:11 +0000124/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000125/// one if necessary.
126llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000127 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000128 unsigned Encoding = 0;
129 switch (BT->getKind()) {
130 default:
131 case BuiltinType::Void:
132 return llvm::DIType();
133 case BuiltinType::UChar:
134 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
135 case BuiltinType::Char_S:
136 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
137 case BuiltinType::UShort:
138 case BuiltinType::UInt:
139 case BuiltinType::ULong:
140 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
141 case BuiltinType::Short:
142 case BuiltinType::Int:
143 case BuiltinType::Long:
144 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
145 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
146 case BuiltinType::Float:
147 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000148 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000149 // Bit size, align and offset of the type.
150 uint64_t Size = M->getContext().getTypeSize(BT);
151 uint64_t Align = M->getContext().getTypeAlign(BT);
152 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000155 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000156 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000157 Offset, /*flags*/ 0, Encoding);
158}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000159
Chris Lattnerb7003772009-04-23 06:13:01 +0000160llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
161 llvm::DICompileUnit Unit) {
162 // Bit size, align and offset of the type.
163 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
164 if (Ty->isComplexIntegerType())
165 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattnerb7003772009-04-23 06:13:01 +0000167 uint64_t Size = M->getContext().getTypeSize(Ty);
168 uint64_t Align = M->getContext().getTypeAlign(Ty);
169 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattnerb7003772009-04-23 06:13:01 +0000171 return DebugFactory.CreateBasicType(Unit, "complex",
172 Unit, 0, Size, Align,
173 Offset, /*flags*/ 0, Encoding);
174}
175
John McCalla1805292009-09-25 01:40:47 +0000176/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000177/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000178llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
179 QualifierCollector Qc;
180 const Type *T = Qc.strip(Ty);
181
182 // Ignore these qualifiers for now.
183 Qc.removeObjCGCAttr();
184 Qc.removeAddressSpace();
185
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186 // We will create one Derived type for one qualifier and recurse to handle any
187 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000188 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000189 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000190 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000191 Qc.removeConst();
192 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000194 Qc.removeVolatile();
195 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000196 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000197 Qc.removeRestrict();
198 } else {
199 assert(Qc.empty() && "Unknown type qualifier for debug info");
200 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
John McCalla1805292009-09-25 01:40:47 +0000203 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
204
Daniel Dunbar3845f862008-10-31 03:54:29 +0000205 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
206 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
208 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000209}
210
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000211llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
212 llvm::DICompileUnit Unit) {
213 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000215 // Bit size, align and offset of the type.
216 uint64_t Size = M->getContext().getTypeSize(Ty);
217 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000219 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
220 "", llvm::DICompileUnit(),
221 0, Size, Align, 0, 0, EltTy);
222}
223
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
225 llvm::DICompileUnit Unit) {
226 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000228 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 uint64_t Size = M->getContext().getTypeSize(Ty);
230 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattner9c85ba32008-11-10 06:08:34 +0000232 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
233 "", llvm::DICompileUnit(),
234 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000235}
236
Mike Stump9bc093c2009-05-14 02:03:51 +0000237llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
238 llvm::DICompileUnit Unit) {
239 if (BlockLiteralGenericSet)
240 return BlockLiteralGeneric;
241
242 llvm::DICompileUnit DefUnit;
243 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
244
245 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
246
247 llvm::DIType FieldTy;
248
249 QualType FType;
250 uint64_t FieldSize, FieldOffset;
251 unsigned FieldAlign;
252
253 llvm::DIArray Elements;
254 llvm::DIType EltTy, DescTy;
255
256 FieldOffset = 0;
257 FType = M->getContext().UnsignedLongTy;
258 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
259 FieldSize = M->getContext().getTypeSize(FType);
260 FieldAlign = M->getContext().getTypeAlign(FType);
261 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
262 "reserved", DefUnit,
263 0, FieldSize, FieldAlign,
264 FieldOffset, 0, FieldTy);
265 EltTys.push_back(FieldTy);
266
267 FieldOffset += FieldSize;
268 FType = M->getContext().UnsignedLongTy;
269 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
270 FieldSize = M->getContext().getTypeSize(FType);
271 FieldAlign = M->getContext().getTypeAlign(FType);
272 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
273 "Size", DefUnit,
274 0, FieldSize, FieldAlign,
275 FieldOffset, 0, FieldTy);
276 EltTys.push_back(FieldTy);
277
278 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000279 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000280 EltTys.clear();
281
282 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
283 DefUnit, 0, FieldOffset, 0, 0, 0,
284 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Mike Stump9bc093c2009-05-14 02:03:51 +0000286 // Bit size, align and offset of the type.
287 uint64_t Size = M->getContext().getTypeSize(Ty);
288 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Mike Stump9bc093c2009-05-14 02:03:51 +0000290 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
291 Unit, "", llvm::DICompileUnit(),
292 0, Size, Align, 0, 0, EltTy);
293
294 FieldOffset = 0;
295 FType = M->getContext().getPointerType(M->getContext().VoidTy);
296 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
297 FieldSize = M->getContext().getTypeSize(FType);
298 FieldAlign = M->getContext().getTypeAlign(FType);
299 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
300 "__isa", DefUnit,
301 0, FieldSize, FieldAlign,
302 FieldOffset, 0, FieldTy);
303 EltTys.push_back(FieldTy);
304
305 FieldOffset += FieldSize;
306 FType = M->getContext().IntTy;
307 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
308 FieldSize = M->getContext().getTypeSize(FType);
309 FieldAlign = M->getContext().getTypeAlign(FType);
310 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
311 "__flags", DefUnit,
312 0, FieldSize, FieldAlign,
313 FieldOffset, 0, FieldTy);
314 EltTys.push_back(FieldTy);
315
316 FieldOffset += FieldSize;
317 FType = M->getContext().IntTy;
318 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
319 FieldSize = M->getContext().getTypeSize(FType);
320 FieldAlign = M->getContext().getTypeAlign(FType);
321 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
322 "__reserved", DefUnit,
323 0, FieldSize, FieldAlign,
324 FieldOffset, 0, FieldTy);
325 EltTys.push_back(FieldTy);
326
327 FieldOffset += FieldSize;
328 FType = M->getContext().getPointerType(M->getContext().VoidTy);
329 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
330 FieldSize = M->getContext().getTypeSize(FType);
331 FieldAlign = M->getContext().getTypeAlign(FType);
332 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
333 "__FuncPtr", DefUnit,
334 0, FieldSize, FieldAlign,
335 FieldOffset, 0, FieldTy);
336 EltTys.push_back(FieldTy);
337
338 FieldOffset += FieldSize;
339 FType = M->getContext().getPointerType(M->getContext().VoidTy);
340 FieldTy = DescTy;
341 FieldSize = M->getContext().getTypeSize(Ty);
342 FieldAlign = M->getContext().getTypeAlign(Ty);
343 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
344 "__descriptor", DefUnit,
345 0, FieldSize, FieldAlign,
346 FieldOffset, 0, FieldTy);
347 EltTys.push_back(FieldTy);
348
349 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000350 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000351
352 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
353 DefUnit, 0, FieldOffset, 0, 0, 0,
354 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 BlockLiteralGenericSet = true;
357 BlockLiteralGeneric
358 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
359 "", llvm::DICompileUnit(),
360 0, Size, Align, 0, 0, EltTy);
361 return BlockLiteralGeneric;
362}
363
Chris Lattner9c85ba32008-11-10 06:08:34 +0000364llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
365 llvm::DICompileUnit Unit) {
366 // Typedefs are derived from some other type. If we have a typedef of a
367 // typedef, make sure to emit the whole chain.
368 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner9c85ba32008-11-10 06:08:34 +0000370 // We don't set size information, but do specify where the typedef was
371 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000372 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000373 SourceLocation DefLoc = Ty->getDecl()->getLocation();
374 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000376 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000377 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
378 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
381 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000382}
383
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
385 llvm::DICompileUnit Unit) {
386 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000387
Chris Lattner9c85ba32008-11-10 06:08:34 +0000388 // Add the result type at least.
389 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 // Set up remainder of arguments if there is a prototype.
392 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000393 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
395 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
396 } else {
397 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398 }
399
Chris Lattner9c85ba32008-11-10 06:08:34 +0000400 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000401 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
404 Unit, "", llvm::DICompileUnit(),
405 0, 0, 0, 0, 0,
406 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000407}
408
Devang Patel65e99f22009-02-25 01:36:11 +0000409/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
411 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000412 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414 unsigned Tag;
415 if (Decl->isStruct())
416 Tag = llvm::dwarf::DW_TAG_structure_type;
417 else if (Decl->isUnion())
418 Tag = llvm::dwarf::DW_TAG_union_type;
419 else {
420 assert(Decl->isClass() && "Unknown RecordType!");
421 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000422 }
423
Sanjiv Gupta507de852008-06-09 10:47:41 +0000424 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000425
Chris Lattner9c85ba32008-11-10 06:08:34 +0000426 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000427 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000428
Devang Patel4f6fa232009-04-17 21:35:15 +0000429 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000430 llvm::DICompileUnit DefUnit;
431 unsigned Line = 0;
432 if (!PLoc.isInvalid()) {
433 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
434 Line = PLoc.getLine();
435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner9c85ba32008-11-10 06:08:34 +0000437 // Records and classes and unions can all be recursive. To handle them, we
438 // first generate a debug descriptor for the struct as a forward declaration.
439 // Then (if it is a definition) we go through and get debug info for all of
440 // its members. Finally, we create a descriptor for the complete type (which
441 // may refer to the forward decl if the struct is recursive) and replace all
442 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000443 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000444 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
445 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 // If this is just a forward declaration, return it.
448 if (!Decl->getDefinition(M->getContext()))
449 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000450
Chris Lattner9c85ba32008-11-10 06:08:34 +0000451 // Otherwise, insert it into the TypeCache so that recursive uses will find
452 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000453 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454
455 // Convert all the elements.
456 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
457
458 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
459
460 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000461 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000462 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000463 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000464 FieldDecl *Field = *I;
465 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000466
467 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000468
Devang Patelde135022009-04-27 22:40:36 +0000469 // Ignore unnamed fields.
470 if (FieldName.empty())
471 continue;
472
Chris Lattner9c85ba32008-11-10 06:08:34 +0000473 // Get the location for the field.
474 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000475 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000476 llvm::DICompileUnit FieldDefUnit;
477 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000479 if (!PLoc.isInvalid()) {
480 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
481 FieldLine = PLoc.getLine();
482 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000483
484 QualType FType = Field->getType();
485 uint64_t FieldSize = 0;
486 unsigned FieldAlign = 0;
487 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Devang Patelec9b5d52009-03-16 23:47:53 +0000489 // Bit size, align and offset of the type.
490 FieldSize = M->getContext().getTypeSize(FType);
491 Expr *BitWidth = Field->getBitWidth();
492 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000493 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Devang Patelec9b5d52009-03-16 23:47:53 +0000495 FieldAlign = M->getContext().getTypeAlign(FType);
496 }
497
Mike Stump1eb44332009-09-09 15:08:12 +0000498 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
499
Chris Lattner9c85ba32008-11-10 06:08:34 +0000500 // Create a DW_TAG_member node to remember the offset of this field in the
501 // struct. FIXME: This is an absolutely insane way to capture this
502 // information. When we gut debug info, this should be fixed.
503 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
504 FieldName, FieldDefUnit,
505 FieldLine, FieldSize, FieldAlign,
506 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000507 EltTys.push_back(FieldTy);
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner9c85ba32008-11-10 06:08:34 +0000510 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000511 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000512
513 // Bit size, align and offset of the type.
514 uint64_t Size = M->getContext().getTypeSize(Ty);
515 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Devang Patel0ce73f62009-07-22 18:57:00 +0000517 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000518 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
519 Align, 0, 0, llvm::DIType(), Elements);
520
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000521 // Update TypeCache.
522 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
523
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 // Now that we have a real decl for the struct, replace anything using the
525 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000526 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000527
Chris Lattner9c85ba32008-11-10 06:08:34 +0000528 return RealDecl;
529}
530
Devang Patel9ca36b62009-02-26 21:10:26 +0000531/// CreateType - get objective-c interface type.
532llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
533 llvm::DICompileUnit Unit) {
534 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Devang Patel9ca36b62009-02-26 21:10:26 +0000536 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
537 SourceManager &SM = M->getContext().getSourceManager();
538
539 // Get overall information about the record type for the debug info.
540 std::string Name = Decl->getNameAsString();
541
542 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000543 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
544 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
545
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Daniel Dunbard86d3362009-05-18 20:51:58 +0000547 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000548
Devang Patel9ca36b62009-02-26 21:10:26 +0000549 // To handle recursive interface, we
550 // first generate a debug descriptor for the struct as a forward declaration.
551 // Then (if it is a definition) we go through and get debug info for all of
552 // its members. Finally, we create a descriptor for the complete type (which
553 // may refer to the forward decl if the struct is recursive) and replace all
554 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000555 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000556 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000557 llvm::DIType(), llvm::DIArray(),
558 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Devang Patel9ca36b62009-02-26 21:10:26 +0000560 // If this is just a forward declaration, return it.
561 if (Decl->isForwardDecl())
562 return FwdDecl;
563
564 // Otherwise, insert it into the TypeCache so that recursive uses will find
565 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000566 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000567
568 // Convert all the elements.
569 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
570
Devang Patelfbe899f2009-03-10 21:30:26 +0000571 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
572 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000573 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000574 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000575 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000576 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000577 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000578 0 /* offset */, 0, SClassTy);
579 EltTys.push_back(InhTag);
580 }
581
Devang Patel9ca36b62009-02-26 21:10:26 +0000582 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
583
584 unsigned FieldNo = 0;
585 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
586 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
587 ObjCIvarDecl *Field = *I;
588 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
589
590 std::string FieldName = Field->getNameAsString();
591
Devang Patelde135022009-04-27 22:40:36 +0000592 // Ignore unnamed fields.
593 if (FieldName.empty())
594 continue;
595
Devang Patel9ca36b62009-02-26 21:10:26 +0000596 // Get the location for the field.
597 SourceLocation FieldDefLoc = Field->getLocation();
598 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000599 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
600 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
601
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Devang Patel99c20eb2009-03-20 18:24:39 +0000603 QualType FType = Field->getType();
604 uint64_t FieldSize = 0;
605 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000606
Devang Patel99c20eb2009-03-20 18:24:39 +0000607 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Devang Patel99c20eb2009-03-20 18:24:39 +0000609 // Bit size, align and offset of the type.
610 FieldSize = M->getContext().getTypeSize(FType);
611 Expr *BitWidth = Field->getBitWidth();
612 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000613 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
614
Devang Patel99c20eb2009-03-20 18:24:39 +0000615 FieldAlign = M->getContext().getTypeAlign(FType);
616 }
617
Mike Stump1eb44332009-09-09 15:08:12 +0000618 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
619
Devang Patelc20482b2009-03-19 00:23:53 +0000620 unsigned Flags = 0;
621 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
622 Flags = llvm::DIType::FlagProtected;
623 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
624 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Devang Patel9ca36b62009-02-26 21:10:26 +0000626 // Create a DW_TAG_member node to remember the offset of this field in the
627 // struct. FIXME: This is an absolutely insane way to capture this
628 // information. When we gut debug info, this should be fixed.
629 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
630 FieldName, FieldDefUnit,
631 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000632 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000633 EltTys.push_back(FieldTy);
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Devang Patel9ca36b62009-02-26 21:10:26 +0000636 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000637 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000638
639 // Bit size, align and offset of the type.
640 uint64_t Size = M->getContext().getTypeSize(Ty);
641 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Devang Patel6c1fddf2009-07-27 18:42:03 +0000643 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000644 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000645 Align, 0, 0, llvm::DIType(), Elements,
646 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000647
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000648 // Update TypeCache.
649 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
650
Devang Patel9ca36b62009-02-26 21:10:26 +0000651 // Now that we have a real decl for the struct, replace anything using the
652 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000653 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000654
Devang Patel9ca36b62009-02-26 21:10:26 +0000655 return RealDecl;
656}
657
Chris Lattner9c85ba32008-11-10 06:08:34 +0000658llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
659 llvm::DICompileUnit Unit) {
660 EnumDecl *Decl = Ty->getDecl();
661
662 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
663
664 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000665 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000666 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000667 Enum != EnumEnd; ++Enum) {
668 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
669 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner9c85ba32008-11-10 06:08:34 +0000672 // Return a CompositeType for the enum itself.
673 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000674 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000675
Chris Lattner8ec03f52008-11-24 03:54:41 +0000676 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000677 SourceLocation DefLoc = Decl->getLocation();
678 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
679 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000680 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
681 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
682
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattner9c85ba32008-11-10 06:08:34 +0000684 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000685 uint64_t Size = 0;
686 unsigned Align = 0;
687 if (!Ty->isIncompleteType()) {
688 Size = M->getContext().getTypeSize(Ty);
689 Align = M->getContext().getTypeAlign(Ty);
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner9c85ba32008-11-10 06:08:34 +0000692 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
693 Unit, EnumName, DefUnit, Line,
694 Size, Align, 0, 0,
695 llvm::DIType(), EltArray);
696}
697
698llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
699 llvm::DICompileUnit Unit) {
700 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
701 return CreateType(RT, Unit);
702 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
703 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattner9c85ba32008-11-10 06:08:34 +0000705 return llvm::DIType();
706}
707
708llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
709 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000710 uint64_t Size;
711 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000712
713
Nuno Lopes010d5142009-01-28 00:35:17 +0000714 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000715 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000716 Size = 0;
717 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000718 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
719 } else if (Ty->isIncompleteArrayType()) {
720 Size = 0;
721 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000722 } else {
723 // Size and align of the whole array, not the element type.
724 Size = M->getContext().getTypeSize(Ty);
725 Align = M->getContext().getTypeAlign(Ty);
726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattner9c85ba32008-11-10 06:08:34 +0000728 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
729 // interior arrays, do we care? Why aren't nested arrays represented the
730 // obvious/recursive way?
731 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
732 QualType EltTy(Ty, 0);
733 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000734 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000735 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000736 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000737 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000738 // FIXME: Verify this is right for VLAs.
739 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
740 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000744 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000745
746 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
747 Unit, "", llvm::DICompileUnit(),
748 0, Size, Align, 0, 0,
749 getOrCreateType(EltTy, Unit),
750 SubscriptArray);
751}
752
753
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000754/// getOrCreateType - Get the type from the cache or create a new
755/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000756llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
757 llvm::DICompileUnit Unit) {
758 if (Ty.isNull())
759 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000761 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000762 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000763 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000764 if (it != TypeCache.end()) {
765 // Verify that the debug info still exists.
766 if (&*it->second)
767 return llvm::DIType(cast<llvm::MDNode>(it->second));
768 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000769
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000770 // Otherwise create the type.
771 llvm::DIType Res = CreateTypeNode(Ty, Unit);
772 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
773 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000774}
775
776/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
777/// new one if necessary.
778llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
779 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000780 // Handle qualifiers, which recursively handles what they refer to.
781 if (Ty.hasQualifiers())
782 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000783
784 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000786#define TYPE(Class, Base)
787#define ABSTRACT_TYPE(Class, Base)
788#define NON_CANONICAL_TYPE(Class, Base)
789#define DEPENDENT_TYPE(Class, Base) case Type::Class:
790#include "clang/AST/TypeNodes.def"
791 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000792
Daniel Dunbar03faac32009-09-19 19:27:14 +0000793 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000794 case Type::LValueReference:
795 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 case Type::Vector:
797 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000798 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000799 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000800 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000801 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000802 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000804 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000805 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000806 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000807 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
808 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
809 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
810 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000811 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000812 return CreateType(cast<BlockPointerType>(Ty), Unit);
813 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000814 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000815 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000816 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000817 case Type::FunctionProto:
818 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000819 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000820 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000821 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
822 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattner9c85ba32008-11-10 06:08:34 +0000824 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000825 case Type::ConstantArrayWithExpr:
826 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827 case Type::VariableArray:
828 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000829 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000830 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000831 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
832 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000833 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000834 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000835 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000836 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000837 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000838}
839
840/// EmitFunctionStart - Constructs the debug code for entering a function -
841/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000842void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000843 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000845 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Daniel Dunbara2893932009-05-13 23:08:57 +0000847 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000848 //
849 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000850 if (Name[0] == '\01')
851 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattner9c85ba32008-11-10 06:08:34 +0000853 // FIXME: Why is this using CurLoc???
854 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000855 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000856 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattner9c85ba32008-11-10 06:08:34 +0000858 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000859 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000860 getOrCreateType(ReturnType, Unit),
861 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattner9c85ba32008-11-10 06:08:34 +0000863 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000865 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000867}
868
869
Chris Lattner9c85ba32008-11-10 06:08:34 +0000870void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000871 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000873 // Don't bother if things are the same as last time.
874 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000875 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000876 || (SM.getInstantiationLineNumber(CurLoc) ==
877 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000878 && SM.isFromSameFile(CurLoc, PrevLoc)))
879 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000880
881 // Update last state.
882 PrevLoc = CurLoc;
883
884 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000885 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000886 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
887 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000888 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000889}
890
891/// EmitRegionStart- Constructs the debug code for entering a declarative
892/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
894 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000895 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000896 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000897 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000898 RegionStack.push_back(D);
899 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000900}
901
902/// EmitRegionEnd - Constructs the debug code for exiting a declarative
903/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000905 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
906
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000907 // Provide an region stop point.
908 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattner9c85ba32008-11-10 06:08:34 +0000910 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000911 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000912}
913
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000914/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000915void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
916 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000917 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
918
Devang Patel07739032009-03-27 23:16:32 +0000919 // Do not emit variable debug information while generating optimized code.
920 // The llvm optimizer and code generator are not yet ready to support
921 // optimized code debugging.
922 const CompileOptions &CO = M->getCompileOpts();
923 if (CO.OptimizationLevel)
924 return;
925
Chris Lattner650cea92009-05-05 04:57:08 +0000926 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000927 QualType Type = Decl->getType();
928 llvm::DIType Ty = getOrCreateType(Type, Unit);
929 if (Decl->hasAttr<BlocksAttr>()) {
930 llvm::DICompileUnit DefUnit;
931 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
932
933 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
934
935 llvm::DIType FieldTy;
936
937 QualType FType;
938 uint64_t FieldSize, FieldOffset;
939 unsigned FieldAlign;
940
941 llvm::DIArray Elements;
942 llvm::DIType EltTy;
943
944 // Build up structure for the byref. See BuildByRefType.
945 FieldOffset = 0;
946 FType = M->getContext().getPointerType(M->getContext().VoidTy);
947 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
948 FieldSize = M->getContext().getTypeSize(FType);
949 FieldAlign = M->getContext().getTypeAlign(FType);
950 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
951 "__isa", DefUnit,
952 0, FieldSize, FieldAlign,
953 FieldOffset, 0, FieldTy);
954 EltTys.push_back(FieldTy);
955 FieldOffset += FieldSize;
956
957 FType = M->getContext().getPointerType(M->getContext().VoidTy);
958 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
959 FieldSize = M->getContext().getTypeSize(FType);
960 FieldAlign = M->getContext().getTypeAlign(FType);
961 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
962 "__forwarding", DefUnit,
963 0, FieldSize, FieldAlign,
964 FieldOffset, 0, FieldTy);
965 EltTys.push_back(FieldTy);
966 FieldOffset += FieldSize;
967
968 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
969 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
970 FieldSize = M->getContext().getTypeSize(FType);
971 FieldAlign = M->getContext().getTypeAlign(FType);
972 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
973 "__flags", DefUnit,
974 0, FieldSize, FieldAlign,
975 FieldOffset, 0, FieldTy);
976 EltTys.push_back(FieldTy);
977 FieldOffset += FieldSize;
978
979 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
980 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
981 FieldSize = M->getContext().getTypeSize(FType);
982 FieldAlign = M->getContext().getTypeAlign(FType);
983 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
984 "__size", DefUnit,
985 0, FieldSize, FieldAlign,
986 FieldOffset, 0, FieldTy);
987 EltTys.push_back(FieldTy);
988 FieldOffset += FieldSize;
989
990 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
991 if (HasCopyAndDispose) {
992 FType = M->getContext().getPointerType(M->getContext().VoidTy);
993 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
994 FieldSize = M->getContext().getTypeSize(FType);
995 FieldAlign = M->getContext().getTypeAlign(FType);
996 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
997 "__copy_helper", DefUnit,
998 0, FieldSize, FieldAlign,
999 FieldOffset, 0, FieldTy);
1000 EltTys.push_back(FieldTy);
1001 FieldOffset += FieldSize;
1002
1003 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1004 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1005 FieldSize = M->getContext().getTypeSize(FType);
1006 FieldAlign = M->getContext().getTypeAlign(FType);
1007 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1008 "__destroy_helper", DefUnit,
1009 0, FieldSize, FieldAlign,
1010 FieldOffset, 0, FieldTy);
1011 EltTys.push_back(FieldTy);
1012 FieldOffset += FieldSize;
1013 }
1014
1015 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1016 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1017 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001018 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001019 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001020 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001021
1022 if (NumPaddingBytes > 0) {
1023 llvm::APInt pad(32, NumPaddingBytes);
1024 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1025 pad, ArrayType::Normal, 0);
1026 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1027 FieldSize = M->getContext().getTypeSize(FType);
1028 FieldAlign = M->getContext().getTypeAlign(FType);
1029 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1030 Unit, "", DefUnit,
1031 0, FieldSize, FieldAlign,
1032 FieldOffset, 0, FieldTy);
1033 EltTys.push_back(FieldTy);
1034 FieldOffset += FieldSize;
1035 }
1036 }
1037
1038 FType = Type;
1039 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1040 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001041 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001042 std::string Name = Decl->getNameAsString();
1043
1044 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1045 Name, DefUnit,
1046 0, FieldSize, FieldAlign,
1047 FieldOffset, 0, FieldTy);
1048 EltTys.push_back(FieldTy);
1049 FieldOffset += FieldSize;
1050
1051 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1052
1053 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1054
1055 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1056 llvm::DICompileUnit(),
1057 0, FieldOffset, 0, 0, Flags,
1058 llvm::DIType(), Elements);
1059 }
Chris Lattner650cea92009-05-05 04:57:08 +00001060
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001062 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001063 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001064 unsigned Line = 0;
1065 if (!PLoc.isInvalid())
1066 Line = PLoc.getLine();
1067 else
1068 Unit = llvm::DICompileUnit();
1069
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner9c85ba32008-11-10 06:08:34 +00001071 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001072 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001073 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001074 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075 // Insert an llvm.dbg.declare into the current block.
1076 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001077}
1078
Chris Lattner9c85ba32008-11-10 06:08:34 +00001079void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1080 llvm::Value *Storage,
1081 CGBuilderTy &Builder) {
1082 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1083}
1084
1085/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1086/// variable declaration.
1087void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1088 CGBuilderTy &Builder) {
1089 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1090}
1091
1092
1093
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001094/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001095void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001096 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001097
1098 // Do not emit variable debug information while generating optimized code.
1099 // The llvm optimizer and code generator are not yet ready to support
1100 // optimized code debugging.
1101 const CompileOptions &CO = M->getCompileOpts();
1102 if (CO.OptimizationLevel)
1103 return;
1104
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001105 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001106 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001107 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001108 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1109 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001110
1111 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001112
1113 QualType T = Decl->getType();
1114 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001116 // CodeGen turns int[] into int[1] so we'll do the same here.
1117 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001119 ConstVal = 1;
1120 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001121
1122 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001123 ArrayType::Normal, 0);
1124 }
1125
Devang Patel6dba4322009-07-14 21:31:22 +00001126 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001127 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001128 Var->hasInternalLinkage(),
1129 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001130}
1131
Devang Patel9ca36b62009-02-26 21:10:26 +00001132/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001133void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001134 ObjCInterfaceDecl *Decl) {
1135 // Create global variable debug descriptor.
1136 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1137 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001138 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1139 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001140
1141 std::string Name = Decl->getNameAsString();
1142
Chris Lattner03d9f342009-04-01 06:23:52 +00001143 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001144 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Devang Patel9ca36b62009-02-26 21:10:26 +00001146 // CodeGen turns int[] into int[1] so we'll do the same here.
1147 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Devang Patel9ca36b62009-02-26 21:10:26 +00001149 ConstVal = 1;
1150 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001151
1152 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001153 ArrayType::Normal, 0);
1154 }
1155
Devang Patel6dba4322009-07-14 21:31:22 +00001156 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001157 getOrCreateType(T, Unit),
1158 Var->hasInternalLinkage(),
1159 true/*definition*/, Var);
1160}