blob: 7d0c1ad8a55f38876cea678fde37aac905cde575 [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
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000052/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000053/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000054llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000055 // Get source file information.
56 const char *FileName = "<unknown>";
Devang Patel77820222009-02-24 23:16:03 +000057 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000058 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000059 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000060 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
61 FileName = PLoc.getFilename();
62 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000063 }
Mike Stump1eb44332009-09-09 15:08:12 +000064
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000065 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000066 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000067 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000068
Devang Patel446c6192009-04-17 21:06:59 +000069 // Get absolute path name.
70 llvm::sys::Path AbsFileName(FileName);
71 if (!AbsFileName.isAbsolute()) {
72 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
73 tmp.appendComponent(FileName);
74 AbsFileName = tmp;
75 }
76
Devang Patel72240d72009-06-26 18:32:22 +000077 // See if thie compile unit is representing main source file. Each source
78 // file has corresponding compile unit. There is only one main source
79 // file at a time.
80 bool isMain = false;
81 const LangOptions &LO = M->getLangOptions();
82 const char *MainFileName = LO.getMainFileName();
83 if (isMainCompileUnitCreated == false) {
84 if (MainFileName) {
85 if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
86 isMain = true;
87 } else {
88 if (Loc.isValid() && SM.isFromMainFile(Loc))
89 isMain = true;
90 }
91 if (isMain)
92 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +000093 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +000094
Chris Lattner515455a2009-03-25 03:28:08 +000095 unsigned LangTag;
96 if (LO.CPlusPlus) {
97 if (LO.ObjC1)
98 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
99 else
100 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
101 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000102 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000103 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000104 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000105 } else {
106 LangTag = llvm::dwarf::DW_LANG_C89;
107 }
Devang Patel446c6192009-04-17 21:06:59 +0000108
Mike Stump5a862172009-09-15 21:48:34 +0000109 std::string Producer = "clang " CLANG_VERSION_STRING;
Chris Lattnerb95ee582009-05-02 01:04:13 +0000110 bool isOptimized = LO.Optimize;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000111 const char *Flags = ""; // FIXME: Encode command line options.
112
113 // Figure out which version of the ObjC runtime we have.
114 unsigned RuntimeVers = 0;
115 if (LO.ObjC1)
116 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000118 // Create new compile unit.
Devang Patel72240d72009-06-26 18:32:22 +0000119 return Unit = DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(),
Mike Stump1eb44332009-09-09 15:08:12 +0000120 AbsFileName.getDirname(),
Devang Patel72240d72009-06-26 18:32:22 +0000121 Producer, isMain, isOptimized,
122 Flags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000123}
124
Devang Patel65e99f22009-02-25 01:36:11 +0000125/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000126/// one if necessary.
127llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000128 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000129 unsigned Encoding = 0;
130 switch (BT->getKind()) {
131 default:
132 case BuiltinType::Void:
133 return llvm::DIType();
134 case BuiltinType::UChar:
135 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
136 case BuiltinType::Char_S:
137 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
138 case BuiltinType::UShort:
139 case BuiltinType::UInt:
140 case BuiltinType::ULong:
141 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
142 case BuiltinType::Short:
143 case BuiltinType::Int:
144 case BuiltinType::Long:
145 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
146 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
147 case BuiltinType::Float:
148 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000149 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000150 // Bit size, align and offset of the type.
151 uint64_t Size = M->getContext().getTypeSize(BT);
152 uint64_t Align = M->getContext().getTypeAlign(BT);
153 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
155 return DebugFactory.CreateBasicType(Unit,
Chris Lattnere4f21422009-06-30 01:26:17 +0000156 BT->getName(M->getContext().getLangOptions()),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000157 Unit, 0, Size, Align,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158 Offset, /*flags*/ 0, Encoding);
159}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000160
Chris Lattnerb7003772009-04-23 06:13:01 +0000161llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
162 llvm::DICompileUnit Unit) {
163 // Bit size, align and offset of the type.
164 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
165 if (Ty->isComplexIntegerType())
166 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattnerb7003772009-04-23 06:13:01 +0000168 uint64_t Size = M->getContext().getTypeSize(Ty);
169 uint64_t Align = M->getContext().getTypeAlign(Ty);
170 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattnerb7003772009-04-23 06:13:01 +0000172 return DebugFactory.CreateBasicType(Unit, "complex",
173 Unit, 0, Size, Align,
174 Offset, /*flags*/ 0, Encoding);
175}
176
John McCalla1805292009-09-25 01:40:47 +0000177/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000178/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000179llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
180 QualifierCollector Qc;
181 const Type *T = Qc.strip(Ty);
182
183 // Ignore these qualifiers for now.
184 Qc.removeObjCGCAttr();
185 Qc.removeAddressSpace();
186
Chris Lattner9c85ba32008-11-10 06:08:34 +0000187 // We will create one Derived type for one qualifier and recurse to handle any
188 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000189 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000190 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000191 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000192 Qc.removeConst();
193 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000195 Qc.removeVolatile();
196 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000197 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000198 Qc.removeRestrict();
199 } else {
200 assert(Qc.empty() && "Unknown type qualifier for debug info");
201 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
John McCalla1805292009-09-25 01:40:47 +0000204 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
205
Daniel Dunbar3845f862008-10-31 03:54:29 +0000206 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
207 // CVR derived types.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000208 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
209 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000210}
211
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000212llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
213 llvm::DICompileUnit Unit) {
214 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000216 // Bit size, align and offset of the type.
217 uint64_t Size = M->getContext().getTypeSize(Ty);
218 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000220 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
221 "", llvm::DICompileUnit(),
222 0, Size, Align, 0, 0, EltTy);
223}
224
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
226 llvm::DICompileUnit Unit) {
227 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000229 // Bit size, align and offset of the type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 uint64_t Size = M->getContext().getTypeSize(Ty);
231 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
234 "", llvm::DICompileUnit(),
235 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000236}
237
Mike Stump9bc093c2009-05-14 02:03:51 +0000238llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
239 llvm::DICompileUnit Unit) {
240 if (BlockLiteralGenericSet)
241 return BlockLiteralGeneric;
242
243 llvm::DICompileUnit DefUnit;
244 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
245
246 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
247
248 llvm::DIType FieldTy;
249
250 QualType FType;
251 uint64_t FieldSize, FieldOffset;
252 unsigned FieldAlign;
253
254 llvm::DIArray Elements;
255 llvm::DIType EltTy, DescTy;
256
257 FieldOffset = 0;
258 FType = M->getContext().UnsignedLongTy;
259 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
260 FieldSize = M->getContext().getTypeSize(FType);
261 FieldAlign = M->getContext().getTypeAlign(FType);
262 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
263 "reserved", DefUnit,
264 0, FieldSize, FieldAlign,
265 FieldOffset, 0, FieldTy);
266 EltTys.push_back(FieldTy);
267
268 FieldOffset += FieldSize;
269 FType = M->getContext().UnsignedLongTy;
270 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
271 FieldSize = M->getContext().getTypeSize(FType);
272 FieldAlign = M->getContext().getTypeAlign(FType);
273 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
274 "Size", DefUnit,
275 0, FieldSize, FieldAlign,
276 FieldOffset, 0, FieldTy);
277 EltTys.push_back(FieldTy);
278
279 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000280 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000281 EltTys.clear();
282
Mike Stump3d363c52009-10-02 02:30:50 +0000283 unsigned Flags = llvm::DIType::FlagAppleBlock;
284
Mike Stump9bc093c2009-05-14 02:03:51 +0000285 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000286 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000287 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Mike Stump9bc093c2009-05-14 02:03:51 +0000289 // Bit size, align and offset of the type.
290 uint64_t Size = M->getContext().getTypeSize(Ty);
291 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Mike Stump9bc093c2009-05-14 02:03:51 +0000293 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
294 Unit, "", llvm::DICompileUnit(),
295 0, Size, Align, 0, 0, EltTy);
296
297 FieldOffset = 0;
298 FType = M->getContext().getPointerType(M->getContext().VoidTy);
299 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
300 FieldSize = M->getContext().getTypeSize(FType);
301 FieldAlign = M->getContext().getTypeAlign(FType);
302 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
303 "__isa", DefUnit,
304 0, FieldSize, FieldAlign,
305 FieldOffset, 0, FieldTy);
306 EltTys.push_back(FieldTy);
307
308 FieldOffset += FieldSize;
309 FType = M->getContext().IntTy;
310 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
311 FieldSize = M->getContext().getTypeSize(FType);
312 FieldAlign = M->getContext().getTypeAlign(FType);
313 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
314 "__flags", DefUnit,
315 0, FieldSize, FieldAlign,
316 FieldOffset, 0, FieldTy);
317 EltTys.push_back(FieldTy);
318
319 FieldOffset += FieldSize;
320 FType = M->getContext().IntTy;
321 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
322 FieldSize = M->getContext().getTypeSize(FType);
323 FieldAlign = M->getContext().getTypeAlign(FType);
324 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
325 "__reserved", DefUnit,
326 0, FieldSize, FieldAlign,
327 FieldOffset, 0, FieldTy);
328 EltTys.push_back(FieldTy);
329
330 FieldOffset += FieldSize;
331 FType = M->getContext().getPointerType(M->getContext().VoidTy);
332 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
333 FieldSize = M->getContext().getTypeSize(FType);
334 FieldAlign = M->getContext().getTypeAlign(FType);
335 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
336 "__FuncPtr", DefUnit,
337 0, FieldSize, FieldAlign,
338 FieldOffset, 0, FieldTy);
339 EltTys.push_back(FieldTy);
340
341 FieldOffset += FieldSize;
342 FType = M->getContext().getPointerType(M->getContext().VoidTy);
343 FieldTy = DescTy;
344 FieldSize = M->getContext().getTypeSize(Ty);
345 FieldAlign = M->getContext().getTypeAlign(Ty);
346 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
347 "__descriptor", DefUnit,
348 0, FieldSize, FieldAlign,
349 FieldOffset, 0, FieldTy);
350 EltTys.push_back(FieldTy);
351
352 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000353 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000354
355 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000356 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Mike Stump9bc093c2009-05-14 02:03:51 +0000359 BlockLiteralGenericSet = true;
360 BlockLiteralGeneric
361 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
362 "", llvm::DICompileUnit(),
363 0, Size, Align, 0, 0, EltTy);
364 return BlockLiteralGeneric;
365}
366
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
368 llvm::DICompileUnit Unit) {
369 // Typedefs are derived from some other type. If we have a typedef of a
370 // typedef, make sure to emit the whole chain.
371 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner9c85ba32008-11-10 06:08:34 +0000373 // We don't set size information, but do specify where the typedef was
374 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000375 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 SourceLocation DefLoc = Ty->getDecl()->getLocation();
377 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000378
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000379 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000380 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
381 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
384 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000385}
386
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
388 llvm::DICompileUnit Unit) {
389 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 // Add the result type at least.
392 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394 // Set up remainder of arguments if there is a prototype.
395 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000396 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000397 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
398 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
399 } else {
400 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000401 }
402
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000404 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattner9c85ba32008-11-10 06:08:34 +0000406 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
407 Unit, "", llvm::DICompileUnit(),
408 0, 0, 0, 0, 0,
409 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000410}
411
Devang Patel65e99f22009-02-25 01:36:11 +0000412/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
414 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000415 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner9c85ba32008-11-10 06:08:34 +0000417 unsigned Tag;
418 if (Decl->isStruct())
419 Tag = llvm::dwarf::DW_TAG_structure_type;
420 else if (Decl->isUnion())
421 Tag = llvm::dwarf::DW_TAG_union_type;
422 else {
423 assert(Decl->isClass() && "Unknown RecordType!");
424 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000425 }
426
Sanjiv Gupta507de852008-06-09 10:47:41 +0000427 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000430 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000431
Devang Patel4f6fa232009-04-17 21:35:15 +0000432 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000433 llvm::DICompileUnit DefUnit;
434 unsigned Line = 0;
435 if (!PLoc.isInvalid()) {
436 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
437 Line = PLoc.getLine();
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattner9c85ba32008-11-10 06:08:34 +0000440 // Records and classes and unions can all be recursive. To handle them, we
441 // first generate a debug descriptor for the struct as a forward declaration.
442 // Then (if it is a definition) we go through and get debug info for all of
443 // its members. Finally, we create a descriptor for the complete type (which
444 // may refer to the forward decl if the struct is recursive) and replace all
445 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000446 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
448 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner9c85ba32008-11-10 06:08:34 +0000450 // If this is just a forward declaration, return it.
451 if (!Decl->getDefinition(M->getContext()))
452 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 // Otherwise, insert it into the TypeCache so that recursive uses will find
455 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000456 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000457
458 // Convert all the elements.
459 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
460
461 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
462
463 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000465 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000466 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467 FieldDecl *Field = *I;
468 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000469
470 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000471
Devang Patelde135022009-04-27 22:40:36 +0000472 // Ignore unnamed fields.
473 if (FieldName.empty())
474 continue;
475
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476 // Get the location for the field.
477 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000478 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000479 llvm::DICompileUnit FieldDefUnit;
480 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000482 if (!PLoc.isInvalid()) {
483 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
484 FieldLine = PLoc.getLine();
485 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000486
487 QualType FType = Field->getType();
488 uint64_t FieldSize = 0;
489 unsigned FieldAlign = 0;
490 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Devang Patelec9b5d52009-03-16 23:47:53 +0000492 // Bit size, align and offset of the type.
493 FieldSize = M->getContext().getTypeSize(FType);
494 Expr *BitWidth = Field->getBitWidth();
495 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000496 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Devang Patelec9b5d52009-03-16 23:47:53 +0000498 FieldAlign = M->getContext().getTypeAlign(FType);
499 }
500
Mike Stump1eb44332009-09-09 15:08:12 +0000501 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
502
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503 // Create a DW_TAG_member node to remember the offset of this field in the
504 // struct. FIXME: This is an absolutely insane way to capture this
505 // information. When we gut debug info, this should be fixed.
506 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
507 FieldName, FieldDefUnit,
508 FieldLine, FieldSize, FieldAlign,
509 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000510 EltTys.push_back(FieldTy);
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner9c85ba32008-11-10 06:08:34 +0000513 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000514 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000515
516 // Bit size, align and offset of the type.
517 uint64_t Size = M->getContext().getTypeSize(Ty);
518 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Devang Patel0ce73f62009-07-22 18:57:00 +0000520 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
522 Align, 0, 0, llvm::DIType(), Elements);
523
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000524 // Update TypeCache.
525 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
526
Chris Lattner9c85ba32008-11-10 06:08:34 +0000527 // Now that we have a real decl for the struct, replace anything using the
528 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000529 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000530
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531 return RealDecl;
532}
533
Devang Patel9ca36b62009-02-26 21:10:26 +0000534/// CreateType - get objective-c interface type.
535llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
536 llvm::DICompileUnit Unit) {
537 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Devang Patel9ca36b62009-02-26 21:10:26 +0000539 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
540 SourceManager &SM = M->getContext().getSourceManager();
541
542 // Get overall information about the record type for the debug info.
543 std::string Name = Decl->getNameAsString();
544
545 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000546 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
547 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
548
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Daniel Dunbard86d3362009-05-18 20:51:58 +0000550 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000551
Devang Patel9ca36b62009-02-26 21:10:26 +0000552 // To handle recursive interface, we
553 // first generate a debug descriptor for the struct as a forward declaration.
554 // Then (if it is a definition) we go through and get debug info for all of
555 // its members. Finally, we create a descriptor for the complete type (which
556 // may refer to the forward decl if the struct is recursive) and replace all
557 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000558 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000559 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000560 llvm::DIType(), llvm::DIArray(),
561 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Devang Patel9ca36b62009-02-26 21:10:26 +0000563 // If this is just a forward declaration, return it.
564 if (Decl->isForwardDecl())
565 return FwdDecl;
566
567 // Otherwise, insert it into the TypeCache so that recursive uses will find
568 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000569 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000570
571 // Convert all the elements.
572 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
573
Devang Patelfbe899f2009-03-10 21:30:26 +0000574 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
575 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000576 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000577 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000578 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000579 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000580 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000581 0 /* offset */, 0, SClassTy);
582 EltTys.push_back(InhTag);
583 }
584
Devang Patel9ca36b62009-02-26 21:10:26 +0000585 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
586
587 unsigned FieldNo = 0;
588 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
589 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
590 ObjCIvarDecl *Field = *I;
591 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
592
593 std::string FieldName = Field->getNameAsString();
594
Devang Patelde135022009-04-27 22:40:36 +0000595 // Ignore unnamed fields.
596 if (FieldName.empty())
597 continue;
598
Devang Patel9ca36b62009-02-26 21:10:26 +0000599 // Get the location for the field.
600 SourceLocation FieldDefLoc = Field->getLocation();
601 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000602 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
603 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
604
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Devang Patel99c20eb2009-03-20 18:24:39 +0000606 QualType FType = Field->getType();
607 uint64_t FieldSize = 0;
608 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000609
Devang Patel99c20eb2009-03-20 18:24:39 +0000610 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Devang Patel99c20eb2009-03-20 18:24:39 +0000612 // Bit size, align and offset of the type.
613 FieldSize = M->getContext().getTypeSize(FType);
614 Expr *BitWidth = Field->getBitWidth();
615 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000616 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
617
Devang Patel99c20eb2009-03-20 18:24:39 +0000618 FieldAlign = M->getContext().getTypeAlign(FType);
619 }
620
Mike Stump1eb44332009-09-09 15:08:12 +0000621 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
622
Devang Patelc20482b2009-03-19 00:23:53 +0000623 unsigned Flags = 0;
624 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
625 Flags = llvm::DIType::FlagProtected;
626 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
627 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Devang Patel9ca36b62009-02-26 21:10:26 +0000629 // Create a DW_TAG_member node to remember the offset of this field in the
630 // struct. FIXME: This is an absolutely insane way to capture this
631 // information. When we gut debug info, this should be fixed.
632 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
633 FieldName, FieldDefUnit,
634 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000635 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000636 EltTys.push_back(FieldTy);
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Devang Patel9ca36b62009-02-26 21:10:26 +0000639 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000640 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000641
642 // Bit size, align and offset of the type.
643 uint64_t Size = M->getContext().getTypeSize(Ty);
644 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Devang Patel6c1fddf2009-07-27 18:42:03 +0000646 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000647 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000648 Align, 0, 0, llvm::DIType(), Elements,
649 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000650
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000651 // Update TypeCache.
652 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
653
Devang Patel9ca36b62009-02-26 21:10:26 +0000654 // Now that we have a real decl for the struct, replace anything using the
655 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000656 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000657
Devang Patel9ca36b62009-02-26 21:10:26 +0000658 return RealDecl;
659}
660
Chris Lattner9c85ba32008-11-10 06:08:34 +0000661llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
662 llvm::DICompileUnit Unit) {
663 EnumDecl *Decl = Ty->getDecl();
664
665 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
666
667 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000668 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000669 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000670 Enum != EnumEnd; ++Enum) {
671 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
672 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Chris Lattner9c85ba32008-11-10 06:08:34 +0000675 // Return a CompositeType for the enum itself.
676 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000677 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000678
Chris Lattner8ec03f52008-11-24 03:54:41 +0000679 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000680 SourceLocation DefLoc = Decl->getLocation();
681 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
682 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000683 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
684 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
685
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Chris Lattner9c85ba32008-11-10 06:08:34 +0000687 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000688 uint64_t Size = 0;
689 unsigned Align = 0;
690 if (!Ty->isIncompleteType()) {
691 Size = M->getContext().getTypeSize(Ty);
692 Align = M->getContext().getTypeAlign(Ty);
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner9c85ba32008-11-10 06:08:34 +0000695 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
696 Unit, EnumName, DefUnit, Line,
697 Size, Align, 0, 0,
698 llvm::DIType(), EltArray);
699}
700
701llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
702 llvm::DICompileUnit Unit) {
703 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
704 return CreateType(RT, Unit);
705 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
706 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner9c85ba32008-11-10 06:08:34 +0000708 return llvm::DIType();
709}
710
711llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
712 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000713 uint64_t Size;
714 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
716
Nuno Lopes010d5142009-01-28 00:35:17 +0000717 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000718 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000719 Size = 0;
720 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000721 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
722 } else if (Ty->isIncompleteArrayType()) {
723 Size = 0;
724 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000725 } else {
726 // Size and align of the whole array, not the element type.
727 Size = M->getContext().getTypeSize(Ty);
728 Align = M->getContext().getTypeAlign(Ty);
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner9c85ba32008-11-10 06:08:34 +0000731 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
732 // interior arrays, do we care? Why aren't nested arrays represented the
733 // obvious/recursive way?
734 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
735 QualType EltTy(Ty, 0);
736 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000737 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000738 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000739 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000740 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000741 // FIXME: Verify this is right for VLAs.
742 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
743 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattner9c85ba32008-11-10 06:08:34 +0000746 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000747 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000748
749 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
750 Unit, "", llvm::DICompileUnit(),
751 0, Size, Align, 0, 0,
752 getOrCreateType(EltTy, Unit),
753 SubscriptArray);
754}
755
756
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000757/// getOrCreateType - Get the type from the cache or create a new
758/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000759llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
760 llvm::DICompileUnit Unit) {
761 if (Ty.isNull())
762 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000764 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000765 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000766 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000767 if (it != TypeCache.end()) {
768 // Verify that the debug info still exists.
769 if (&*it->second)
770 return llvm::DIType(cast<llvm::MDNode>(it->second));
771 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000772
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000773 // Otherwise create the type.
774 llvm::DIType Res = CreateTypeNode(Ty, Unit);
775 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
776 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000777}
778
779/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
780/// new one if necessary.
781llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
782 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000783 // Handle qualifiers, which recursively handles what they refer to.
784 if (Ty.hasQualifiers())
785 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000786
787 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000788 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000789#define TYPE(Class, Base)
790#define ABSTRACT_TYPE(Class, Base)
791#define NON_CANONICAL_TYPE(Class, Base)
792#define DEPENDENT_TYPE(Class, Base) case Type::Class:
793#include "clang/AST/TypeNodes.def"
794 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000795
Daniel Dunbar03faac32009-09-19 19:27:14 +0000796 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000797 case Type::LValueReference:
798 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 case Type::Vector:
800 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000801 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000802 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000803 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000804 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000805 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000807 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000808 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000809 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000810 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
811 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
812 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
813 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000814 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000815 return CreateType(cast<BlockPointerType>(Ty), Unit);
816 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000817 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000818 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000819 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000820 case Type::FunctionProto:
821 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000822 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000823 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000824 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
825 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000828 case Type::ConstantArrayWithExpr:
829 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000830 case Type::VariableArray:
831 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000832 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000833 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000834 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
835 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000836 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000837 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000838 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000839 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000840 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000841}
842
843/// EmitFunctionStart - Constructs the debug code for entering a function -
844/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000845void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000846 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000848 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Daniel Dunbara2893932009-05-13 23:08:57 +0000850 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000851 //
852 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000853 if (Name[0] == '\01')
854 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Chris Lattner9c85ba32008-11-10 06:08:34 +0000856 // FIXME: Why is this using CurLoc???
857 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000858 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000859 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattner9c85ba32008-11-10 06:08:34 +0000861 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000862 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000863 getOrCreateType(ReturnType, Unit),
864 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000868 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000870}
871
872
Chris Lattner9c85ba32008-11-10 06:08:34 +0000873void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000874 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000876 // Don't bother if things are the same as last time.
877 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000878 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000879 || (SM.getInstantiationLineNumber(CurLoc) ==
880 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000881 && SM.isFromSameFile(CurLoc, PrevLoc)))
882 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000883
884 // Update last state.
885 PrevLoc = CurLoc;
886
887 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000888 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000889 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
890 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000891 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000892}
893
894/// EmitRegionStart- Constructs the debug code for entering a declarative
895/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000896void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
897 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000898 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000900 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000901 RegionStack.push_back(D);
902 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000903}
904
905/// EmitRegionEnd - Constructs the debug code for exiting a declarative
906/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000907void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000908 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
909
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000910 // Provide an region stop point.
911 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattner9c85ba32008-11-10 06:08:34 +0000913 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000914 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000915}
916
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000917/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000918void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
919 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000920 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
921
Devang Patel07739032009-03-27 23:16:32 +0000922 // Do not emit variable debug information while generating optimized code.
923 // The llvm optimizer and code generator are not yet ready to support
924 // optimized code debugging.
925 const CompileOptions &CO = M->getCompileOpts();
926 if (CO.OptimizationLevel)
927 return;
928
Chris Lattner650cea92009-05-05 04:57:08 +0000929 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000930 QualType Type = Decl->getType();
931 llvm::DIType Ty = getOrCreateType(Type, Unit);
932 if (Decl->hasAttr<BlocksAttr>()) {
933 llvm::DICompileUnit DefUnit;
934 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
935
936 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
937
938 llvm::DIType FieldTy;
939
940 QualType FType;
941 uint64_t FieldSize, FieldOffset;
942 unsigned FieldAlign;
943
944 llvm::DIArray Elements;
945 llvm::DIType EltTy;
946
947 // Build up structure for the byref. See BuildByRefType.
948 FieldOffset = 0;
949 FType = M->getContext().getPointerType(M->getContext().VoidTy);
950 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
951 FieldSize = M->getContext().getTypeSize(FType);
952 FieldAlign = M->getContext().getTypeAlign(FType);
953 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
954 "__isa", DefUnit,
955 0, FieldSize, FieldAlign,
956 FieldOffset, 0, FieldTy);
957 EltTys.push_back(FieldTy);
958 FieldOffset += FieldSize;
959
960 FType = M->getContext().getPointerType(M->getContext().VoidTy);
961 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
962 FieldSize = M->getContext().getTypeSize(FType);
963 FieldAlign = M->getContext().getTypeAlign(FType);
964 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
965 "__forwarding", DefUnit,
966 0, FieldSize, FieldAlign,
967 FieldOffset, 0, FieldTy);
968 EltTys.push_back(FieldTy);
969 FieldOffset += FieldSize;
970
971 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
972 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
973 FieldSize = M->getContext().getTypeSize(FType);
974 FieldAlign = M->getContext().getTypeAlign(FType);
975 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
976 "__flags", DefUnit,
977 0, FieldSize, FieldAlign,
978 FieldOffset, 0, FieldTy);
979 EltTys.push_back(FieldTy);
980 FieldOffset += FieldSize;
981
982 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
983 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
984 FieldSize = M->getContext().getTypeSize(FType);
985 FieldAlign = M->getContext().getTypeAlign(FType);
986 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
987 "__size", DefUnit,
988 0, FieldSize, FieldAlign,
989 FieldOffset, 0, FieldTy);
990 EltTys.push_back(FieldTy);
991 FieldOffset += FieldSize;
992
993 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
994 if (HasCopyAndDispose) {
995 FType = M->getContext().getPointerType(M->getContext().VoidTy);
996 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
997 FieldSize = M->getContext().getTypeSize(FType);
998 FieldAlign = M->getContext().getTypeAlign(FType);
999 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1000 "__copy_helper", DefUnit,
1001 0, FieldSize, FieldAlign,
1002 FieldOffset, 0, FieldTy);
1003 EltTys.push_back(FieldTy);
1004 FieldOffset += FieldSize;
1005
1006 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1007 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1008 FieldSize = M->getContext().getTypeSize(FType);
1009 FieldAlign = M->getContext().getTypeAlign(FType);
1010 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1011 "__destroy_helper", DefUnit,
1012 0, FieldSize, FieldAlign,
1013 FieldOffset, 0, FieldTy);
1014 EltTys.push_back(FieldTy);
1015 FieldOffset += FieldSize;
1016 }
1017
1018 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1019 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1020 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001021 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001022 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001023 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001024
1025 if (NumPaddingBytes > 0) {
1026 llvm::APInt pad(32, NumPaddingBytes);
1027 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1028 pad, ArrayType::Normal, 0);
1029 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1030 FieldSize = M->getContext().getTypeSize(FType);
1031 FieldAlign = M->getContext().getTypeAlign(FType);
1032 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1033 Unit, "", DefUnit,
1034 0, FieldSize, FieldAlign,
1035 FieldOffset, 0, FieldTy);
1036 EltTys.push_back(FieldTy);
1037 FieldOffset += FieldSize;
1038 }
1039 }
1040
1041 FType = Type;
1042 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1043 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001044 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001045 std::string Name = Decl->getNameAsString();
1046
1047 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1048 Name, DefUnit,
1049 0, FieldSize, FieldAlign,
1050 FieldOffset, 0, FieldTy);
1051 EltTys.push_back(FieldTy);
1052 FieldOffset += FieldSize;
1053
1054 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1055
1056 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1057
1058 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1059 llvm::DICompileUnit(),
1060 0, FieldOffset, 0, 0, Flags,
1061 llvm::DIType(), Elements);
1062 }
Chris Lattner650cea92009-05-05 04:57:08 +00001063
Chris Lattner9c85ba32008-11-10 06:08:34 +00001064 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001065 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001066 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001067 unsigned Line = 0;
1068 if (!PLoc.isInvalid())
1069 Line = PLoc.getLine();
1070 else
1071 Unit = llvm::DICompileUnit();
1072
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattner9c85ba32008-11-10 06:08:34 +00001074 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001075 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001076 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001077 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001078 // Insert an llvm.dbg.declare into the current block.
1079 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001080}
1081
Mike Stumpb1a6e682009-09-30 02:43:10 +00001082/// EmitDeclare - Emit local variable declaration debug info.
1083void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1084 llvm::Value *Storage, CGBuilderTy &Builder,
1085 CodeGenFunction *CGF) {
1086 const ValueDecl *Decl = BDRE->getDecl();
1087 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1088
1089 // Do not emit variable debug information while generating optimized code.
1090 // The llvm optimizer and code generator are not yet ready to support
1091 // optimized code debugging.
1092 const CompileOptions &CO = M->getCompileOpts();
Mike Stumpb289b3f2009-10-01 22:29:41 +00001093 if (CO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001094 return;
1095
1096 uint64_t XOffset = 0;
1097 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1098 QualType Type = Decl->getType();
1099 llvm::DIType Ty = getOrCreateType(Type, Unit);
1100 if (Decl->hasAttr<BlocksAttr>()) {
1101 llvm::DICompileUnit DefUnit;
1102 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1103
1104 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1105
1106 llvm::DIType FieldTy;
1107
1108 QualType FType;
1109 uint64_t FieldSize, FieldOffset;
1110 unsigned FieldAlign;
1111
1112 llvm::DIArray Elements;
1113 llvm::DIType EltTy;
1114
1115 // Build up structure for the byref. See BuildByRefType.
1116 FieldOffset = 0;
1117 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1118 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1119 FieldSize = M->getContext().getTypeSize(FType);
1120 FieldAlign = M->getContext().getTypeAlign(FType);
1121 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1122 "__isa", DefUnit,
1123 0, FieldSize, FieldAlign,
1124 FieldOffset, 0, FieldTy);
1125 EltTys.push_back(FieldTy);
1126 FieldOffset += FieldSize;
1127
1128 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1129 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1130 FieldSize = M->getContext().getTypeSize(FType);
1131 FieldAlign = M->getContext().getTypeAlign(FType);
1132 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1133 "__forwarding", DefUnit,
1134 0, FieldSize, FieldAlign,
1135 FieldOffset, 0, FieldTy);
1136 EltTys.push_back(FieldTy);
1137 FieldOffset += FieldSize;
1138
1139 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1140 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1141 FieldSize = M->getContext().getTypeSize(FType);
1142 FieldAlign = M->getContext().getTypeAlign(FType);
1143 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1144 "__flags", DefUnit,
1145 0, FieldSize, FieldAlign,
1146 FieldOffset, 0, FieldTy);
1147 EltTys.push_back(FieldTy);
1148 FieldOffset += FieldSize;
1149
1150 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1151 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1152 FieldSize = M->getContext().getTypeSize(FType);
1153 FieldAlign = M->getContext().getTypeAlign(FType);
1154 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1155 "__size", DefUnit,
1156 0, FieldSize, FieldAlign,
1157 FieldOffset, 0, FieldTy);
1158 EltTys.push_back(FieldTy);
1159 FieldOffset += FieldSize;
1160
1161 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1162 if (HasCopyAndDispose) {
1163 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1164 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1165 FieldSize = M->getContext().getTypeSize(FType);
1166 FieldAlign = M->getContext().getTypeAlign(FType);
1167 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1168 "__copy_helper", DefUnit,
1169 0, FieldSize, FieldAlign,
1170 FieldOffset, 0, FieldTy);
1171 EltTys.push_back(FieldTy);
1172 FieldOffset += FieldSize;
1173
1174 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1175 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1176 FieldSize = M->getContext().getTypeSize(FType);
1177 FieldAlign = M->getContext().getTypeAlign(FType);
1178 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1179 "__destroy_helper", DefUnit,
1180 0, FieldSize, FieldAlign,
1181 FieldOffset, 0, FieldTy);
1182 EltTys.push_back(FieldTy);
1183 FieldOffset += FieldSize;
1184 }
1185
1186 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1187 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1188 unsigned AlignedOffsetInBytes
1189 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1190 unsigned NumPaddingBytes
1191 = AlignedOffsetInBytes - FieldOffset/8;
1192
1193 if (NumPaddingBytes > 0) {
1194 llvm::APInt pad(32, NumPaddingBytes);
1195 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1196 pad, ArrayType::Normal, 0);
1197 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1198 FieldSize = M->getContext().getTypeSize(FType);
1199 FieldAlign = M->getContext().getTypeAlign(FType);
1200 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1201 Unit, "", DefUnit,
1202 0, FieldSize, FieldAlign,
1203 FieldOffset, 0, FieldTy);
1204 EltTys.push_back(FieldTy);
1205 FieldOffset += FieldSize;
1206 }
1207 }
1208
1209 FType = Type;
1210 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1211 FieldSize = M->getContext().getTypeSize(FType);
1212 FieldAlign = Align*8;
1213 std::string Name = Decl->getNameAsString();
1214
1215 XOffset = FieldOffset;
1216 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1217 Name, DefUnit,
1218 0, FieldSize, FieldAlign,
1219 FieldOffset, 0, FieldTy);
1220 EltTys.push_back(FieldTy);
1221 FieldOffset += FieldSize;
1222
1223 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1224
1225 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1226
1227 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1228 llvm::DICompileUnit(),
1229 0, FieldOffset, 0, 0, Flags,
1230 llvm::DIType(), Elements);
1231 }
1232
1233 // Get location information.
1234 SourceManager &SM = M->getContext().getSourceManager();
1235 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1236 unsigned Line = 0;
1237 if (!PLoc.isInvalid())
1238 Line = PLoc.getLine();
1239 else
1240 Unit = llvm::DICompileUnit();
1241
1242 uint64_t offset = CGF->BlockDecls[Decl];
1243 llvm::SmallVector<llvm::Value *, 9> addr;
1244 llvm::LLVMContext &VMContext = M->getLLVMContext();
1245 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1246 llvm::DIFactory::OpDeref));
1247 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1248 llvm::DIFactory::OpPlus));
1249 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1250 offset));
1251 if (BDRE->isByRef()) {
1252 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1253 llvm::DIFactory::OpDeref));
1254 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1255 llvm::DIFactory::OpPlus));
1256 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1257 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1258 offset));
1259 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1260 llvm::DIFactory::OpDeref));
1261 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1262 llvm::DIFactory::OpPlus));
1263 offset = XOffset/8; // offset of x field
1264 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1265 offset));
1266 }
1267
1268 // Create the descriptor for the variable.
1269 llvm::DIVariable D =
1270 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1271 Decl->getNameAsString(), Unit, Line, Ty,
1272 addr);
1273 // Insert an llvm.dbg.declare into the current block.
Mike Stumpb289b3f2009-10-01 22:29:41 +00001274 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001275}
1276
Chris Lattner9c85ba32008-11-10 06:08:34 +00001277void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1278 llvm::Value *Storage,
1279 CGBuilderTy &Builder) {
1280 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1281}
1282
Mike Stumpb1a6e682009-09-30 02:43:10 +00001283void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1284 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1285 CodeGenFunction *CGF) {
1286 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1287}
1288
Chris Lattner9c85ba32008-11-10 06:08:34 +00001289/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1290/// variable declaration.
1291void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1292 CGBuilderTy &Builder) {
1293 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1294}
1295
1296
1297
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001298/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001299void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001301
1302 // Do not emit variable debug information while generating optimized code.
1303 // The llvm optimizer and code generator are not yet ready to support
1304 // optimized code debugging.
1305 const CompileOptions &CO = M->getCompileOpts();
1306 if (CO.OptimizationLevel)
1307 return;
1308
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001309 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001310 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001311 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001312 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1313 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001314
1315 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001316
1317 QualType T = Decl->getType();
1318 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001320 // CodeGen turns int[] into int[1] so we'll do the same here.
1321 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001323 ConstVal = 1;
1324 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001325
1326 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001327 ArrayType::Normal, 0);
1328 }
1329
Devang Patel6dba4322009-07-14 21:31:22 +00001330 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001331 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 Var->hasInternalLinkage(),
1333 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001334}
1335
Devang Patel9ca36b62009-02-26 21:10:26 +00001336/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001337void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001338 ObjCInterfaceDecl *Decl) {
1339 // Create global variable debug descriptor.
1340 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1341 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001342 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1343 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001344
1345 std::string Name = Decl->getNameAsString();
1346
Chris Lattner03d9f342009-04-01 06:23:52 +00001347 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001348 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Devang Patel9ca36b62009-02-26 21:10:26 +00001350 // CodeGen turns int[] into int[1] so we'll do the same here.
1351 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Devang Patel9ca36b62009-02-26 21:10:26 +00001353 ConstVal = 1;
1354 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001355
1356 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001357 ArrayType::Normal, 0);
1358 }
1359
Devang Patel6dba4322009-07-14 21:31:22 +00001360 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001361 getOrCreateType(T, Unit),
1362 Var->hasInternalLinkage(),
1363 true/*definition*/, Var);
1364}