blob: 645125667c9a5f46f5935b5193d16e3f34ca8fce [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
283 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
284 DefUnit, 0, FieldOffset, 0, 0, 0,
285 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Mike Stump9bc093c2009-05-14 02:03:51 +0000287 // Bit size, align and offset of the type.
288 uint64_t Size = M->getContext().getTypeSize(Ty);
289 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Mike Stump9bc093c2009-05-14 02:03:51 +0000291 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
292 Unit, "", llvm::DICompileUnit(),
293 0, Size, Align, 0, 0, EltTy);
294
295 FieldOffset = 0;
296 FType = M->getContext().getPointerType(M->getContext().VoidTy);
297 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
298 FieldSize = M->getContext().getTypeSize(FType);
299 FieldAlign = M->getContext().getTypeAlign(FType);
300 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
301 "__isa", DefUnit,
302 0, FieldSize, FieldAlign,
303 FieldOffset, 0, FieldTy);
304 EltTys.push_back(FieldTy);
305
306 FieldOffset += FieldSize;
307 FType = M->getContext().IntTy;
308 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
309 FieldSize = M->getContext().getTypeSize(FType);
310 FieldAlign = M->getContext().getTypeAlign(FType);
311 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
312 "__flags", DefUnit,
313 0, FieldSize, FieldAlign,
314 FieldOffset, 0, FieldTy);
315 EltTys.push_back(FieldTy);
316
317 FieldOffset += FieldSize;
318 FType = M->getContext().IntTy;
319 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
320 FieldSize = M->getContext().getTypeSize(FType);
321 FieldAlign = M->getContext().getTypeAlign(FType);
322 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
323 "__reserved", DefUnit,
324 0, FieldSize, FieldAlign,
325 FieldOffset, 0, FieldTy);
326 EltTys.push_back(FieldTy);
327
328 FieldOffset += FieldSize;
329 FType = M->getContext().getPointerType(M->getContext().VoidTy);
330 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
331 FieldSize = M->getContext().getTypeSize(FType);
332 FieldAlign = M->getContext().getTypeAlign(FType);
333 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
334 "__FuncPtr", DefUnit,
335 0, FieldSize, FieldAlign,
336 FieldOffset, 0, FieldTy);
337 EltTys.push_back(FieldTy);
338
339 FieldOffset += FieldSize;
340 FType = M->getContext().getPointerType(M->getContext().VoidTy);
341 FieldTy = DescTy;
342 FieldSize = M->getContext().getTypeSize(Ty);
343 FieldAlign = M->getContext().getTypeAlign(Ty);
344 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
345 "__descriptor", DefUnit,
346 0, FieldSize, FieldAlign,
347 FieldOffset, 0, FieldTy);
348 EltTys.push_back(FieldTy);
349
350 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000351 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000352
353 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
354 DefUnit, 0, FieldOffset, 0, 0, 0,
355 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 BlockLiteralGenericSet = true;
358 BlockLiteralGeneric
359 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
360 "", llvm::DICompileUnit(),
361 0, Size, Align, 0, 0, EltTy);
362 return BlockLiteralGeneric;
363}
364
Chris Lattner9c85ba32008-11-10 06:08:34 +0000365llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
366 llvm::DICompileUnit Unit) {
367 // Typedefs are derived from some other type. If we have a typedef of a
368 // typedef, make sure to emit the whole chain.
369 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattner9c85ba32008-11-10 06:08:34 +0000371 // We don't set size information, but do specify where the typedef was
372 // declared.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000373 std::string TyName = Ty->getDecl()->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374 SourceLocation DefLoc = Ty->getDecl()->getLocation();
375 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000376
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000377 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000378 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
379 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000380
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
382 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000383}
384
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
386 llvm::DICompileUnit Unit) {
387 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000388
Chris Lattner9c85ba32008-11-10 06:08:34 +0000389 // Add the result type at least.
390 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner9c85ba32008-11-10 06:08:34 +0000392 // Set up remainder of arguments if there is a prototype.
393 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000394 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
396 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
397 } else {
398 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399 }
400
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000402 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
405 Unit, "", llvm::DICompileUnit(),
406 0, 0, 0, 0, 0,
407 llvm::DIType(), EltTypeArray);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000408}
409
Devang Patel65e99f22009-02-25 01:36:11 +0000410/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
412 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000413 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415 unsigned Tag;
416 if (Decl->isStruct())
417 Tag = llvm::dwarf::DW_TAG_structure_type;
418 else if (Decl->isUnion())
419 Tag = llvm::dwarf::DW_TAG_union_type;
420 else {
421 assert(Decl->isClass() && "Unknown RecordType!");
422 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000423 }
424
Sanjiv Gupta507de852008-06-09 10:47:41 +0000425 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000426
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 // Get overall information about the record type for the debug info.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000428 std::string Name = Decl->getNameAsString();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000429
Devang Patel4f6fa232009-04-17 21:35:15 +0000430 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000431 llvm::DICompileUnit DefUnit;
432 unsigned Line = 0;
433 if (!PLoc.isInvalid()) {
434 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
435 Line = PLoc.getLine();
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438 // Records and classes and unions can all be recursive. To handle them, we
439 // first generate a debug descriptor for the struct as a forward declaration.
440 // Then (if it is a definition) we go through and get debug info for all of
441 // its members. Finally, we create a descriptor for the complete type (which
442 // may refer to the forward decl if the struct is recursive) and replace all
443 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000444 llvm::DICompositeType FwdDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
446 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner9c85ba32008-11-10 06:08:34 +0000448 // If this is just a forward declaration, return it.
449 if (!Decl->getDefinition(M->getContext()))
450 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452 // Otherwise, insert it into the TypeCache so that recursive uses will find
453 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000454 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000455
456 // Convert all the elements.
457 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
458
459 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
460
461 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000462 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000463 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000464 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000465 FieldDecl *Field = *I;
466 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000467
468 std::string FieldName = Field->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000469
Devang Patelde135022009-04-27 22:40:36 +0000470 // Ignore unnamed fields.
471 if (FieldName.empty())
472 continue;
473
Chris Lattner9c85ba32008-11-10 06:08:34 +0000474 // Get the location for the field.
475 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000476 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000477 llvm::DICompileUnit FieldDefUnit;
478 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000480 if (!PLoc.isInvalid()) {
481 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
482 FieldLine = PLoc.getLine();
483 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000484
485 QualType FType = Field->getType();
486 uint64_t FieldSize = 0;
487 unsigned FieldAlign = 0;
488 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Devang Patelec9b5d52009-03-16 23:47:53 +0000490 // Bit size, align and offset of the type.
491 FieldSize = M->getContext().getTypeSize(FType);
492 Expr *BitWidth = Field->getBitWidth();
493 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000494 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Devang Patelec9b5d52009-03-16 23:47:53 +0000496 FieldAlign = M->getContext().getTypeAlign(FType);
497 }
498
Mike Stump1eb44332009-09-09 15:08:12 +0000499 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
500
Chris Lattner9c85ba32008-11-10 06:08:34 +0000501 // Create a DW_TAG_member node to remember the offset of this field in the
502 // struct. FIXME: This is an absolutely insane way to capture this
503 // information. When we gut debug info, this should be fixed.
504 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
505 FieldName, FieldDefUnit,
506 FieldLine, FieldSize, FieldAlign,
507 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 EltTys.push_back(FieldTy);
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000512 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000513
514 // Bit size, align and offset of the type.
515 uint64_t Size = M->getContext().getTypeSize(Ty);
516 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Devang Patel0ce73f62009-07-22 18:57:00 +0000518 llvm::DICompositeType RealDecl =
Chris Lattner9c85ba32008-11-10 06:08:34 +0000519 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
520 Align, 0, 0, llvm::DIType(), Elements);
521
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000522 // Update TypeCache.
523 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
524
Chris Lattner9c85ba32008-11-10 06:08:34 +0000525 // Now that we have a real decl for the struct, replace anything using the
526 // old decl with the new one. This will recursively update the debug info.
Devang Patel0ce73f62009-07-22 18:57:00 +0000527 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529 return RealDecl;
530}
531
Devang Patel9ca36b62009-02-26 21:10:26 +0000532/// CreateType - get objective-c interface type.
533llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
534 llvm::DICompileUnit Unit) {
535 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Devang Patel9ca36b62009-02-26 21:10:26 +0000537 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
538 SourceManager &SM = M->getContext().getSourceManager();
539
540 // Get overall information about the record type for the debug info.
541 std::string Name = Decl->getNameAsString();
542
543 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000544 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
545 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
546
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Daniel Dunbard86d3362009-05-18 20:51:58 +0000548 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000549
Devang Patel9ca36b62009-02-26 21:10:26 +0000550 // To handle recursive interface, we
551 // first generate a debug descriptor for the struct as a forward declaration.
552 // Then (if it is a definition) we go through and get debug info for all of
553 // its members. Finally, we create a descriptor for the complete type (which
554 // may refer to the forward decl if the struct is recursive) and replace all
555 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000556 llvm::DICompositeType FwdDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000557 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000558 llvm::DIType(), llvm::DIArray(),
559 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Devang Patel9ca36b62009-02-26 21:10:26 +0000561 // If this is just a forward declaration, return it.
562 if (Decl->isForwardDecl())
563 return FwdDecl;
564
565 // Otherwise, insert it into the TypeCache so that recursive uses will find
566 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000567 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000568
569 // Convert all the elements.
570 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
571
Devang Patelfbe899f2009-03-10 21:30:26 +0000572 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
573 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000574 llvm::DIType SClassTy =
Devang Patelfbe899f2009-03-10 21:30:26 +0000575 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000576 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000577 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000578 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000579 0 /* offset */, 0, SClassTy);
580 EltTys.push_back(InhTag);
581 }
582
Devang Patel9ca36b62009-02-26 21:10:26 +0000583 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl);
584
585 unsigned FieldNo = 0;
586 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
587 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
588 ObjCIvarDecl *Field = *I;
589 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
590
591 std::string FieldName = Field->getNameAsString();
592
Devang Patelde135022009-04-27 22:40:36 +0000593 // Ignore unnamed fields.
594 if (FieldName.empty())
595 continue;
596
Devang Patel9ca36b62009-02-26 21:10:26 +0000597 // Get the location for the field.
598 SourceLocation FieldDefLoc = Field->getLocation();
599 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000600 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
601 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
602
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Devang Patel99c20eb2009-03-20 18:24:39 +0000604 QualType FType = Field->getType();
605 uint64_t FieldSize = 0;
606 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000607
Devang Patel99c20eb2009-03-20 18:24:39 +0000608 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Devang Patel99c20eb2009-03-20 18:24:39 +0000610 // Bit size, align and offset of the type.
611 FieldSize = M->getContext().getTypeSize(FType);
612 Expr *BitWidth = Field->getBitWidth();
613 if (BitWidth)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000614 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue();
615
Devang Patel99c20eb2009-03-20 18:24:39 +0000616 FieldAlign = M->getContext().getTypeAlign(FType);
617 }
618
Mike Stump1eb44332009-09-09 15:08:12 +0000619 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
620
Devang Patelc20482b2009-03-19 00:23:53 +0000621 unsigned Flags = 0;
622 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
623 Flags = llvm::DIType::FlagProtected;
624 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
625 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Devang Patel9ca36b62009-02-26 21:10:26 +0000627 // Create a DW_TAG_member node to remember the offset of this field in the
628 // struct. FIXME: This is an absolutely insane way to capture this
629 // information. When we gut debug info, this should be fixed.
630 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
631 FieldName, FieldDefUnit,
632 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000633 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000634 EltTys.push_back(FieldTy);
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Devang Patel9ca36b62009-02-26 21:10:26 +0000637 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000638 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000639
640 // Bit size, align and offset of the type.
641 uint64_t Size = M->getContext().getTypeSize(Ty);
642 uint64_t Align = M->getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Devang Patel6c1fddf2009-07-27 18:42:03 +0000644 llvm::DICompositeType RealDecl =
Devang Patel9ca36b62009-02-26 21:10:26 +0000645 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000646 Align, 0, 0, llvm::DIType(), Elements,
647 RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000648
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000649 // Update TypeCache.
650 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl.getNode();
651
Devang Patel9ca36b62009-02-26 21:10:26 +0000652 // Now that we have a real decl for the struct, replace anything using the
653 // old decl with the new one. This will recursively update the debug info.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000654 FwdDecl.replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000655
Devang Patel9ca36b62009-02-26 21:10:26 +0000656 return RealDecl;
657}
658
Chris Lattner9c85ba32008-11-10 06:08:34 +0000659llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
660 llvm::DICompileUnit Unit) {
661 EnumDecl *Decl = Ty->getDecl();
662
663 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
664
665 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000666 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000667 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000668 Enum != EnumEnd; ++Enum) {
669 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
670 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Chris Lattner9c85ba32008-11-10 06:08:34 +0000673 // Return a CompositeType for the enum itself.
674 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000675 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000676
Chris Lattner8ec03f52008-11-24 03:54:41 +0000677 std::string EnumName = Decl->getNameAsString();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000678 SourceLocation DefLoc = Decl->getLocation();
679 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
680 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000681 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
682 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
683
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Chris Lattner9c85ba32008-11-10 06:08:34 +0000685 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000686 uint64_t Size = 0;
687 unsigned Align = 0;
688 if (!Ty->isIncompleteType()) {
689 Size = M->getContext().getTypeSize(Ty);
690 Align = M->getContext().getTypeAlign(Ty);
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Chris Lattner9c85ba32008-11-10 06:08:34 +0000693 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
694 Unit, EnumName, DefUnit, Line,
695 Size, Align, 0, 0,
696 llvm::DIType(), EltArray);
697}
698
699llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
700 llvm::DICompileUnit Unit) {
701 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
702 return CreateType(RT, Unit);
703 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
704 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattner9c85ba32008-11-10 06:08:34 +0000706 return llvm::DIType();
707}
708
709llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
710 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000711 uint64_t Size;
712 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714
Nuno Lopes010d5142009-01-28 00:35:17 +0000715 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000716 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000717 Size = 0;
718 Align =
Nuno Lopes010d5142009-01-28 00:35:17 +0000719 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT));
720 } else if (Ty->isIncompleteArrayType()) {
721 Size = 0;
722 Align = M->getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000723 } else {
724 // Size and align of the whole array, not the element type.
725 Size = M->getContext().getTypeSize(Ty);
726 Align = M->getContext().getTypeAlign(Ty);
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattner9c85ba32008-11-10 06:08:34 +0000729 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
730 // interior arrays, do we care? Why aren't nested arrays represented the
731 // obvious/recursive way?
732 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
733 QualType EltTy(Ty, 0);
734 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000735 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000736 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000737 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000738 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000739 // FIXME: Verify this is right for VLAs.
740 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
741 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Chris Lattner9c85ba32008-11-10 06:08:34 +0000744 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000745 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000746
747 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
748 Unit, "", llvm::DICompileUnit(),
749 0, Size, Align, 0, 0,
750 getOrCreateType(EltTy, Unit),
751 SubscriptArray);
752}
753
754
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000755/// getOrCreateType - Get the type from the cache or create a new
756/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000757llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
758 llvm::DICompileUnit Unit) {
759 if (Ty.isNull())
760 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000762 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000763 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000764 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000765 if (it != TypeCache.end()) {
766 // Verify that the debug info still exists.
767 if (&*it->second)
768 return llvm::DIType(cast<llvm::MDNode>(it->second));
769 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000770
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000771 // Otherwise create the type.
772 llvm::DIType Res = CreateTypeNode(Ty, Unit);
773 TypeCache.insert(std::make_pair(Ty.getAsOpaquePtr(), Res.getNode()));
774 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000775}
776
777/// getOrCreateTypeNode - Get the type metadata node from the cache or create a
778/// new one if necessary.
779llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
780 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000781 // Handle qualifiers, which recursively handles what they refer to.
782 if (Ty.hasQualifiers())
783 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000784
785 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000786 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000787#define TYPE(Class, Base)
788#define ABSTRACT_TYPE(Class, Base)
789#define NON_CANONICAL_TYPE(Class, Base)
790#define DEPENDENT_TYPE(Class, Base) case Type::Class:
791#include "clang/AST/TypeNodes.def"
792 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000793
Daniel Dunbar03faac32009-09-19 19:27:14 +0000794 default:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000795 case Type::LValueReference:
796 case Type::RValueReference:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000797 case Type::Vector:
798 case Type::ExtVector:
Eli Friedman00524e32009-02-27 23:15:07 +0000799 case Type::FixedWidthInt:
Eli Friedman00524e32009-02-27 23:15:07 +0000800 case Type::MemberPointer:
Douglas Gregor7532dc62009-03-30 22:58:21 +0000801 case Type::TemplateSpecialization:
Douglas Gregore4e5b052009-03-19 00:18:19 +0000802 case Type::QualifiedName:
Eli Friedman00524e32009-02-27 23:15:07 +0000803 // Unsupported types
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804 return llvm::DIType();
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000805 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000806 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000807 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000808 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
809 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
810 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
811 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000812 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000813 return CreateType(cast<BlockPointerType>(Ty), Unit);
814 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000815 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000816 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000817 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000818 case Type::FunctionProto:
819 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000820 return CreateType(cast<FunctionType>(Ty), Unit);
John McCall7da24312009-09-05 00:15:47 +0000821 case Type::Elaborated:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000822 return getOrCreateType(cast<ElaboratedType>(Ty)->getUnderlyingType(),
823 Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattner9c85ba32008-11-10 06:08:34 +0000825 case Type::ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000826 case Type::ConstantArrayWithExpr:
827 case Type::ConstantArrayWithoutExpr:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000828 case Type::VariableArray:
829 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000830 return CreateType(cast<ArrayType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000831 case Type::TypeOfExpr:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000832 return getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr()
833 ->getType(), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000834 case Type::TypeOf:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000835 return getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), Unit);
Anders Carlsson395b4752009-06-24 19:06:50 +0000836 case Type::Decltype:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000837 return getOrCreateType(cast<DecltypeType>(Ty)->getUnderlyingType(), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000838 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000839}
840
841/// EmitFunctionStart - Constructs the debug code for entering a function -
842/// "llvm.dbg.func.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000843void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000844 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000845 CGBuilderTy &Builder) {
Devang Patel6dba4322009-07-14 21:31:22 +0000846 const char *LinkageName = Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Daniel Dunbara2893932009-05-13 23:08:57 +0000848 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000849 //
850 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000851 if (Name[0] == '\01')
852 ++Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner9c85ba32008-11-10 06:08:34 +0000854 // FIXME: Why is this using CurLoc???
855 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000856 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000857 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattner9c85ba32008-11-10 06:08:34 +0000859 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000860 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000861 getOrCreateType(ReturnType, Unit),
862 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattner9c85ba32008-11-10 06:08:34 +0000864 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000866 // Push function on region stack.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000867 RegionStack.push_back(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000868}
869
870
Chris Lattner9c85ba32008-11-10 06:08:34 +0000871void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000872 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000874 // Don't bother if things are the same as last time.
875 SourceManager &SM = M->getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000876 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000877 || (SM.getInstantiationLineNumber(CurLoc) ==
878 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000879 && SM.isFromSameFile(CurLoc, PrevLoc)))
880 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000881
882 // Update last state.
883 PrevLoc = CurLoc;
884
885 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000886 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000887 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
888 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(),
Mike Stump1eb44332009-09-09 15:08:12 +0000889 Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000890}
891
892/// EmitRegionStart- Constructs the debug code for entering a declarative
893/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000894void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
895 llvm::DIDescriptor D;
Daniel Dunbar5273f512008-10-17 01:07:56 +0000896 if (!RegionStack.empty())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000897 D = RegionStack.back();
Devang Patel49a8b982009-08-31 22:00:32 +0000898 D = DebugFactory.CreateLexicalBlock(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000899 RegionStack.push_back(D);
900 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000901}
902
903/// EmitRegionEnd - Constructs the debug code for exiting a declarative
904/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +0000905void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000906 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
907
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000908 // Provide an region stop point.
909 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chris Lattner9c85ba32008-11-10 06:08:34 +0000911 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000912 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000913}
914
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000915/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000916void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
917 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +0000918 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
919
Devang Patel07739032009-03-27 23:16:32 +0000920 // Do not emit variable debug information while generating optimized code.
921 // The llvm optimizer and code generator are not yet ready to support
922 // optimized code debugging.
923 const CompileOptions &CO = M->getCompileOpts();
924 if (CO.OptimizationLevel)
925 return;
926
Chris Lattner650cea92009-05-05 04:57:08 +0000927 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +0000928 QualType Type = Decl->getType();
929 llvm::DIType Ty = getOrCreateType(Type, Unit);
930 if (Decl->hasAttr<BlocksAttr>()) {
931 llvm::DICompileUnit DefUnit;
932 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
933
934 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
935
936 llvm::DIType FieldTy;
937
938 QualType FType;
939 uint64_t FieldSize, FieldOffset;
940 unsigned FieldAlign;
941
942 llvm::DIArray Elements;
943 llvm::DIType EltTy;
944
945 // Build up structure for the byref. See BuildByRefType.
946 FieldOffset = 0;
947 FType = M->getContext().getPointerType(M->getContext().VoidTy);
948 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
949 FieldSize = M->getContext().getTypeSize(FType);
950 FieldAlign = M->getContext().getTypeAlign(FType);
951 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
952 "__isa", DefUnit,
953 0, FieldSize, FieldAlign,
954 FieldOffset, 0, FieldTy);
955 EltTys.push_back(FieldTy);
956 FieldOffset += FieldSize;
957
958 FType = M->getContext().getPointerType(M->getContext().VoidTy);
959 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
960 FieldSize = M->getContext().getTypeSize(FType);
961 FieldAlign = M->getContext().getTypeAlign(FType);
962 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
963 "__forwarding", DefUnit,
964 0, FieldSize, FieldAlign,
965 FieldOffset, 0, FieldTy);
966 EltTys.push_back(FieldTy);
967 FieldOffset += FieldSize;
968
969 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
970 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
971 FieldSize = M->getContext().getTypeSize(FType);
972 FieldAlign = M->getContext().getTypeAlign(FType);
973 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
974 "__flags", DefUnit,
975 0, FieldSize, FieldAlign,
976 FieldOffset, 0, FieldTy);
977 EltTys.push_back(FieldTy);
978 FieldOffset += FieldSize;
979
980 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
981 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
982 FieldSize = M->getContext().getTypeSize(FType);
983 FieldAlign = M->getContext().getTypeAlign(FType);
984 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
985 "__size", DefUnit,
986 0, FieldSize, FieldAlign,
987 FieldOffset, 0, FieldTy);
988 EltTys.push_back(FieldTy);
989 FieldOffset += FieldSize;
990
991 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
992 if (HasCopyAndDispose) {
993 FType = M->getContext().getPointerType(M->getContext().VoidTy);
994 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
995 FieldSize = M->getContext().getTypeSize(FType);
996 FieldAlign = M->getContext().getTypeAlign(FType);
997 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
998 "__copy_helper", DefUnit,
999 0, FieldSize, FieldAlign,
1000 FieldOffset, 0, FieldTy);
1001 EltTys.push_back(FieldTy);
1002 FieldOffset += FieldSize;
1003
1004 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1005 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1006 FieldSize = M->getContext().getTypeSize(FType);
1007 FieldAlign = M->getContext().getTypeAlign(FType);
1008 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1009 "__destroy_helper", DefUnit,
1010 0, FieldSize, FieldAlign,
1011 FieldOffset, 0, FieldTy);
1012 EltTys.push_back(FieldTy);
1013 FieldOffset += FieldSize;
1014 }
1015
1016 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1017 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1018 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001019 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001020 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001021 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001022
1023 if (NumPaddingBytes > 0) {
1024 llvm::APInt pad(32, NumPaddingBytes);
1025 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1026 pad, ArrayType::Normal, 0);
1027 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1028 FieldSize = M->getContext().getTypeSize(FType);
1029 FieldAlign = M->getContext().getTypeAlign(FType);
1030 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1031 Unit, "", DefUnit,
1032 0, FieldSize, FieldAlign,
1033 FieldOffset, 0, FieldTy);
1034 EltTys.push_back(FieldTy);
1035 FieldOffset += FieldSize;
1036 }
1037 }
1038
1039 FType = Type;
1040 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1041 FieldSize = M->getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001042 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001043 std::string Name = Decl->getNameAsString();
1044
1045 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1046 Name, DefUnit,
1047 0, FieldSize, FieldAlign,
1048 FieldOffset, 0, FieldTy);
1049 EltTys.push_back(FieldTy);
1050 FieldOffset += FieldSize;
1051
1052 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1053
1054 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1055
1056 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1057 llvm::DICompileUnit(),
1058 0, FieldOffset, 0, 0, Flags,
1059 llvm::DIType(), Elements);
1060 }
Chris Lattner650cea92009-05-05 04:57:08 +00001061
Chris Lattner9c85ba32008-11-10 06:08:34 +00001062 // Get location information.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001063 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001064 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001065 unsigned Line = 0;
1066 if (!PLoc.isInvalid())
1067 Line = PLoc.getLine();
1068 else
1069 Unit = llvm::DICompileUnit();
1070
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001073 llvm::DIVariable D =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001074 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(),
Chris Lattner650cea92009-05-05 04:57:08 +00001075 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001076 // Insert an llvm.dbg.declare into the current block.
1077 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001078}
1079
Mike Stumpb1a6e682009-09-30 02:43:10 +00001080/// EmitDeclare - Emit local variable declaration debug info.
1081void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1082 llvm::Value *Storage, CGBuilderTy &Builder,
1083 CodeGenFunction *CGF) {
1084 const ValueDecl *Decl = BDRE->getDecl();
1085 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1086
1087 // Do not emit variable debug information while generating optimized code.
1088 // The llvm optimizer and code generator are not yet ready to support
1089 // optimized code debugging.
1090 const CompileOptions &CO = M->getCompileOpts();
1091 if (CO.OptimizationLevel)
1092 return;
1093
1094 uint64_t XOffset = 0;
1095 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1096 QualType Type = Decl->getType();
1097 llvm::DIType Ty = getOrCreateType(Type, Unit);
1098 if (Decl->hasAttr<BlocksAttr>()) {
1099 llvm::DICompileUnit DefUnit;
1100 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1101
1102 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1103
1104 llvm::DIType FieldTy;
1105
1106 QualType FType;
1107 uint64_t FieldSize, FieldOffset;
1108 unsigned FieldAlign;
1109
1110 llvm::DIArray Elements;
1111 llvm::DIType EltTy;
1112
1113 // Build up structure for the byref. See BuildByRefType.
1114 FieldOffset = 0;
1115 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1116 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1117 FieldSize = M->getContext().getTypeSize(FType);
1118 FieldAlign = M->getContext().getTypeAlign(FType);
1119 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1120 "__isa", DefUnit,
1121 0, FieldSize, FieldAlign,
1122 FieldOffset, 0, FieldTy);
1123 EltTys.push_back(FieldTy);
1124 FieldOffset += FieldSize;
1125
1126 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1127 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1128 FieldSize = M->getContext().getTypeSize(FType);
1129 FieldAlign = M->getContext().getTypeAlign(FType);
1130 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1131 "__forwarding", DefUnit,
1132 0, FieldSize, FieldAlign,
1133 FieldOffset, 0, FieldTy);
1134 EltTys.push_back(FieldTy);
1135 FieldOffset += FieldSize;
1136
1137 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1138 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1139 FieldSize = M->getContext().getTypeSize(FType);
1140 FieldAlign = M->getContext().getTypeAlign(FType);
1141 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1142 "__flags", DefUnit,
1143 0, FieldSize, FieldAlign,
1144 FieldOffset, 0, FieldTy);
1145 EltTys.push_back(FieldTy);
1146 FieldOffset += FieldSize;
1147
1148 FType = M->getContext().getFixedWidthIntType(32, true); // Int32Ty;
1149 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1150 FieldSize = M->getContext().getTypeSize(FType);
1151 FieldAlign = M->getContext().getTypeAlign(FType);
1152 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1153 "__size", DefUnit,
1154 0, FieldSize, FieldAlign,
1155 FieldOffset, 0, FieldTy);
1156 EltTys.push_back(FieldTy);
1157 FieldOffset += FieldSize;
1158
1159 bool HasCopyAndDispose = M->BlockRequiresCopying(Type);
1160 if (HasCopyAndDispose) {
1161 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1162 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1163 FieldSize = M->getContext().getTypeSize(FType);
1164 FieldAlign = M->getContext().getTypeAlign(FType);
1165 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1166 "__copy_helper", DefUnit,
1167 0, FieldSize, FieldAlign,
1168 FieldOffset, 0, FieldTy);
1169 EltTys.push_back(FieldTy);
1170 FieldOffset += FieldSize;
1171
1172 FType = M->getContext().getPointerType(M->getContext().VoidTy);
1173 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1174 FieldSize = M->getContext().getTypeSize(FType);
1175 FieldAlign = M->getContext().getTypeAlign(FType);
1176 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1177 "__destroy_helper", DefUnit,
1178 0, FieldSize, FieldAlign,
1179 FieldOffset, 0, FieldTy);
1180 EltTys.push_back(FieldTy);
1181 FieldOffset += FieldSize;
1182 }
1183
1184 unsigned Align = M->getContext().getDeclAlignInBytes(Decl);
1185 if (Align > M->getContext().Target.getPointerAlign(0) / 8) {
1186 unsigned AlignedOffsetInBytes
1187 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1188 unsigned NumPaddingBytes
1189 = AlignedOffsetInBytes - FieldOffset/8;
1190
1191 if (NumPaddingBytes > 0) {
1192 llvm::APInt pad(32, NumPaddingBytes);
1193 FType = M->getContext().getConstantArrayType(M->getContext().CharTy,
1194 pad, ArrayType::Normal, 0);
1195 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1196 FieldSize = M->getContext().getTypeSize(FType);
1197 FieldAlign = M->getContext().getTypeAlign(FType);
1198 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1199 Unit, "", DefUnit,
1200 0, FieldSize, FieldAlign,
1201 FieldOffset, 0, FieldTy);
1202 EltTys.push_back(FieldTy);
1203 FieldOffset += FieldSize;
1204 }
1205 }
1206
1207 FType = Type;
1208 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1209 FieldSize = M->getContext().getTypeSize(FType);
1210 FieldAlign = Align*8;
1211 std::string Name = Decl->getNameAsString();
1212
1213 XOffset = FieldOffset;
1214 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1215 Name, DefUnit,
1216 0, FieldSize, FieldAlign,
1217 FieldOffset, 0, FieldTy);
1218 EltTys.push_back(FieldTy);
1219 FieldOffset += FieldSize;
1220
1221 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1222
1223 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1224
1225 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1226 llvm::DICompileUnit(),
1227 0, FieldOffset, 0, 0, Flags,
1228 llvm::DIType(), Elements);
1229 }
1230
1231 // Get location information.
1232 SourceManager &SM = M->getContext().getSourceManager();
1233 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1234 unsigned Line = 0;
1235 if (!PLoc.isInvalid())
1236 Line = PLoc.getLine();
1237 else
1238 Unit = llvm::DICompileUnit();
1239
1240 uint64_t offset = CGF->BlockDecls[Decl];
1241 llvm::SmallVector<llvm::Value *, 9> addr;
1242 llvm::LLVMContext &VMContext = M->getLLVMContext();
1243 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1244 llvm::DIFactory::OpDeref));
1245 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1246 llvm::DIFactory::OpPlus));
1247 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1248 offset));
1249 if (BDRE->isByRef()) {
1250 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1251 llvm::DIFactory::OpDeref));
1252 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1253 llvm::DIFactory::OpPlus));
1254 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1255 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1256 offset));
1257 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1258 llvm::DIFactory::OpDeref));
1259 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1260 llvm::DIFactory::OpPlus));
1261 offset = XOffset/8; // offset of x field
1262 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1263 offset));
1264 }
1265
1266 // Create the descriptor for the variable.
1267 llvm::DIVariable D =
1268 DebugFactory.CreateComplexVariable(Tag, RegionStack.back(),
1269 Decl->getNameAsString(), Unit, Line, Ty,
1270 addr);
1271 // Insert an llvm.dbg.declare into the current block.
1272 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1273}
1274
Chris Lattner9c85ba32008-11-10 06:08:34 +00001275void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1276 llvm::Value *Storage,
1277 CGBuilderTy &Builder) {
1278 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1279}
1280
Mike Stumpb1a6e682009-09-30 02:43:10 +00001281void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1282 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1283 CodeGenFunction *CGF) {
1284 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1285}
1286
Chris Lattner9c85ba32008-11-10 06:08:34 +00001287/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1288/// variable declaration.
1289void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1290 CGBuilderTy &Builder) {
1291 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1292}
1293
1294
1295
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001296/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001297void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001298 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001299
1300 // Do not emit variable debug information while generating optimized code.
1301 // The llvm optimizer and code generator are not yet ready to support
1302 // optimized code debugging.
1303 const CompileOptions &CO = M->getCompileOpts();
1304 if (CO.OptimizationLevel)
1305 return;
1306
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001307 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001308 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001309 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001310 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1311 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001312
1313 std::string Name = Decl->getNameAsString();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001314
1315 QualType T = Decl->getType();
1316 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001318 // CodeGen turns int[] into int[1] so we'll do the same here.
1319 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001321 ConstVal = 1;
1322 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001323
1324 T = M->getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001325 ArrayType::Normal, 0);
1326 }
1327
Devang Patel6dba4322009-07-14 21:31:22 +00001328 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001329 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001330 Var->hasInternalLinkage(),
1331 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001332}
1333
Devang Patel9ca36b62009-02-26 21:10:26 +00001334/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001335void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001336 ObjCInterfaceDecl *Decl) {
1337 // Create global variable debug descriptor.
1338 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1339 SourceManager &SM = M->getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001340 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1341 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001342
1343 std::string Name = Decl->getNameAsString();
1344
Chris Lattner03d9f342009-04-01 06:23:52 +00001345 QualType T = M->getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001346 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Devang Patel9ca36b62009-02-26 21:10:26 +00001348 // CodeGen turns int[] into int[1] so we'll do the same here.
1349 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Devang Patel9ca36b62009-02-26 21:10:26 +00001351 ConstVal = 1;
1352 QualType ET = M->getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001353
1354 T = M->getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001355 ArrayType::Normal, 0);
1356 }
1357
Devang Patel6dba4322009-07-14 21:31:22 +00001358 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001359 getOrCreateType(T, Unit),
1360 Var->hasInternalLinkage(),
1361 true/*definition*/, Var);
1362}