blob: c54791b5c3c6354d2cc951b85217d1edef1d9c10 [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"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.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
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 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())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel979ec2e2009-10-06 00:35:31 +000052/// getContext - Get context info for the decl.
53llvm::DIDescriptor CGDebugInfo::getContext(const VarDecl *Decl,
54 llvm::DIDescriptor &CompileUnit) {
55 if (Decl->isFileVarDecl())
56 return CompileUnit;
57 if (Decl->getDeclContext()->isFunctionOrMethod()) {
58 // Find the last subprogram in region stack.
59 for (unsigned RI = RegionStack.size(), RE = 0; RI != RE; --RI) {
Devang Patel8fae0602009-11-13 19:10:24 +000060 llvm::DIDescriptor R(RegionStack[RI - 1]);
Devang Patel979ec2e2009-10-06 00:35:31 +000061 if (R.isSubprogram())
62 return R;
63 }
64 }
65 return CompileUnit;
66}
67
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000068/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000069/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000070llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000071 // Get source file information.
72 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000073 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000074 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000075 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000076 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
77 FileName = PLoc.getFilename();
78 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000079 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000081 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000082 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000083 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +000084
Devang Patel446c6192009-04-17 21:06:59 +000085 // Get absolute path name.
86 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +000087 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +000088
Devang Patel72240d72009-06-26 18:32:22 +000089 // See if thie compile unit is representing main source file. Each source
90 // file has corresponding compile unit. There is only one main source
91 // file at a time.
92 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 const LangOptions &LO = CGM.getLangOptions();
94 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +000095 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +000096 if (!CGO.MainFileName.empty()) {
97 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +000098 isMain = true;
99 } else {
100 if (Loc.isValid() && SM.isFromMainFile(Loc))
101 isMain = true;
102 }
103 if (isMain)
104 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000105 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000106
Chris Lattner515455a2009-03-25 03:28:08 +0000107 unsigned LangTag;
108 if (LO.CPlusPlus) {
109 if (LO.ObjC1)
110 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
111 else
112 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
113 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000114 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000115 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000116 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000117 } else {
118 LangTag = llvm::dwarf::DW_LANG_C89;
119 }
Devang Patel446c6192009-04-17 21:06:59 +0000120
Benjamin Kramer47daf682009-12-08 11:02:29 +0000121 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000122#ifdef CLANG_VENDOR
123 CLANG_VENDOR
124#endif
125 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000126
127 // Figure out which version of the ObjC runtime we have.
128 unsigned RuntimeVers = 0;
129 if (LO.ObjC1)
130 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000132 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000133 return Unit = DebugFactory.CreateCompileUnit(
134 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
135 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000136}
137
Devang Patel65e99f22009-02-25 01:36:11 +0000138/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000139/// one if necessary.
140llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000141 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000142 unsigned Encoding = 0;
143 switch (BT->getKind()) {
144 default:
145 case BuiltinType::Void:
146 return llvm::DIType();
147 case BuiltinType::UChar:
148 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
149 case BuiltinType::Char_S:
150 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
151 case BuiltinType::UShort:
152 case BuiltinType::UInt:
153 case BuiltinType::ULong:
154 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
155 case BuiltinType::Short:
156 case BuiltinType::Int:
157 case BuiltinType::Long:
158 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
159 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
160 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000161 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000162 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000163 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000164 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000165 uint64_t Size = CGM.getContext().getTypeSize(BT);
166 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000167 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Devang Patelca80a5f2009-10-20 19:55:01 +0000169 llvm::DIType DbgTy =
170 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000171 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000172 Unit, 0, Size, Align,
173 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000174 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000175}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000176
Chris Lattnerb7003772009-04-23 06:13:01 +0000177llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
178 llvm::DICompileUnit Unit) {
179 // Bit size, align and offset of the type.
180 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
181 if (Ty->isComplexIntegerType())
182 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Anders Carlsson20f12a22009-12-06 18:00:51 +0000184 uint64_t Size = CGM.getContext().getTypeSize(Ty);
185 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000186 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Devang Patelca80a5f2009-10-20 19:55:01 +0000188 llvm::DIType DbgTy =
189 DebugFactory.CreateBasicType(Unit, "complex",
190 Unit, 0, Size, Align,
191 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000193}
194
John McCalla1805292009-09-25 01:40:47 +0000195/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000196/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000197llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
198 QualifierCollector Qc;
199 const Type *T = Qc.strip(Ty);
200
201 // Ignore these qualifiers for now.
202 Qc.removeObjCGCAttr();
203 Qc.removeAddressSpace();
204
Chris Lattner9c85ba32008-11-10 06:08:34 +0000205 // We will create one Derived type for one qualifier and recurse to handle any
206 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000207 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000208 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000209 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000210 Qc.removeConst();
211 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000213 Qc.removeVolatile();
214 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000215 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000216 Qc.removeRestrict();
217 } else {
218 assert(Qc.empty() && "Unknown type qualifier for debug info");
219 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
John McCalla1805292009-09-25 01:40:47 +0000222 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
223
Daniel Dunbar3845f862008-10-31 03:54:29 +0000224 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
225 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000226 llvm::DIType DbgTy =
227 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
228 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000229 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000230}
231
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000232llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
233 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000234 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000235 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
236 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000237 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000238}
239
Chris Lattner9c85ba32008-11-10 06:08:34 +0000240llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
241 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000242 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
243 Ty->getPointeeType(), Unit);
244}
245
246llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
247 const Type *Ty,
248 QualType PointeeTy,
249 llvm::DICompileUnit Unit) {
250 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000252 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000253
254 // Size is always the size of a pointer. We can't use getTypeSize here
255 // because that does not return the correct value for references.
256 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000257 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
258 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Devang Patelca80a5f2009-10-20 19:55:01 +0000260 return
Anders Carlssona031b352009-11-06 19:19:55 +0000261 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000262 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000263
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000264}
265
Mike Stump9bc093c2009-05-14 02:03:51 +0000266llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
267 llvm::DICompileUnit Unit) {
268 if (BlockLiteralGenericSet)
269 return BlockLiteralGeneric;
270
271 llvm::DICompileUnit DefUnit;
272 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
273
274 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
275
276 llvm::DIType FieldTy;
277
278 QualType FType;
279 uint64_t FieldSize, FieldOffset;
280 unsigned FieldAlign;
281
282 llvm::DIArray Elements;
283 llvm::DIType EltTy, DescTy;
284
285 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000286 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000287 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000288 FieldSize = CGM.getContext().getTypeSize(FType);
289 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000290 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
291 "reserved", DefUnit,
292 0, FieldSize, FieldAlign,
293 FieldOffset, 0, FieldTy);
294 EltTys.push_back(FieldTy);
295
296 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000297 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000298 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000299 FieldSize = CGM.getContext().getTypeSize(FType);
300 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000301 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
302 "Size", DefUnit,
303 0, FieldSize, FieldAlign,
304 FieldOffset, 0, FieldTy);
305 EltTys.push_back(FieldTy);
306
307 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000308 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000309 EltTys.clear();
310
Mike Stump3d363c52009-10-02 02:30:50 +0000311 unsigned Flags = llvm::DIType::FlagAppleBlock;
312
Mike Stump9bc093c2009-05-14 02:03:51 +0000313 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000314 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000315 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000318 uint64_t Size = CGM.getContext().getTypeSize(Ty);
319 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
322 Unit, "", llvm::DICompileUnit(),
323 0, Size, Align, 0, 0, EltTy);
324
325 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000326 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000328 FieldSize = CGM.getContext().getTypeSize(FType);
329 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000330 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
331 "__isa", DefUnit,
332 0, FieldSize, FieldAlign,
333 FieldOffset, 0, FieldTy);
334 EltTys.push_back(FieldTy);
335
336 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000337 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000338 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000339 FieldSize = CGM.getContext().getTypeSize(FType);
340 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
342 "__flags", DefUnit,
343 0, FieldSize, FieldAlign,
344 FieldOffset, 0, FieldTy);
345 EltTys.push_back(FieldTy);
346
347 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000348 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000349 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000350 FieldSize = CGM.getContext().getTypeSize(FType);
351 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000352 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
353 "__reserved", DefUnit,
354 0, FieldSize, FieldAlign,
355 FieldOffset, 0, FieldTy);
356 EltTys.push_back(FieldTy);
357
358 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000359 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000360 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000361 FieldSize = CGM.getContext().getTypeSize(FType);
362 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000363 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
364 "__FuncPtr", DefUnit,
365 0, FieldSize, FieldAlign,
366 FieldOffset, 0, FieldTy);
367 EltTys.push_back(FieldTy);
368
369 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000370 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000371 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000372 FieldSize = CGM.getContext().getTypeSize(Ty);
373 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000374 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
375 "__descriptor", DefUnit,
376 0, FieldSize, FieldAlign,
377 FieldOffset, 0, FieldTy);
378 EltTys.push_back(FieldTy);
379
380 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000381 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000382
383 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000384 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000385 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Mike Stump9bc093c2009-05-14 02:03:51 +0000387 BlockLiteralGenericSet = true;
388 BlockLiteralGeneric
389 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
390 "", llvm::DICompileUnit(),
391 0, Size, Align, 0, 0, EltTy);
392 return BlockLiteralGeneric;
393}
394
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
396 llvm::DICompileUnit Unit) {
397 // Typedefs are derived from some other type. If we have a typedef of a
398 // typedef, make sure to emit the whole chain.
399 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401 // We don't set size information, but do specify where the typedef was
402 // declared.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 SourceLocation DefLoc = Ty->getDecl()->getLocation();
404 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000405
Anders Carlsson20f12a22009-12-06 18:00:51 +0000406 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000407 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
408 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409
Devang Patelca80a5f2009-10-20 19:55:01 +0000410 llvm::DIType DbgTy =
411 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
Devang Patel73621622009-11-25 17:37:31 +0000412 Ty->getDecl()->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000413 DefUnit, Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000414 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000415}
416
Chris Lattner9c85ba32008-11-10 06:08:34 +0000417llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
418 llvm::DICompileUnit Unit) {
419 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421 // Add the result type at least.
422 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424 // Set up remainder of arguments if there is a prototype.
425 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000426 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
428 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
429 } else {
430 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000431 }
432
Chris Lattner9c85ba32008-11-10 06:08:34 +0000433 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000434 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Devang Patelca80a5f2009-10-20 19:55:01 +0000436 llvm::DIType DbgTy =
437 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
438 Unit, "", llvm::DICompileUnit(),
439 0, 0, 0, 0, 0,
440 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000441 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000442}
443
Devang Patel65e99f22009-02-25 01:36:11 +0000444/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
446 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000447 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattner9c85ba32008-11-10 06:08:34 +0000449 unsigned Tag;
450 if (Decl->isStruct())
451 Tag = llvm::dwarf::DW_TAG_structure_type;
452 else if (Decl->isUnion())
453 Tag = llvm::dwarf::DW_TAG_union_type;
454 else {
455 assert(Decl->isClass() && "Unknown RecordType!");
456 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000457 }
458
Anders Carlsson20f12a22009-12-06 18:00:51 +0000459 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000460
Chris Lattner9c85ba32008-11-10 06:08:34 +0000461 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000462 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000463 llvm::DICompileUnit DefUnit;
464 unsigned Line = 0;
465 if (!PLoc.isInvalid()) {
466 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
467 Line = PLoc.getLine();
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 // Records and classes and unions can all be recursive. To handle them, we
471 // first generate a debug descriptor for the struct as a forward declaration.
472 // Then (if it is a definition) we go through and get debug info for all of
473 // its members. Finally, we create a descriptor for the complete type (which
474 // may refer to the forward decl if the struct is recursive) and replace all
475 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000476 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000477 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000478 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000479 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner9c85ba32008-11-10 06:08:34 +0000481 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000482 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000483 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000484
Eli Friedman14d63652009-11-16 21:04:30 +0000485 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000486 // Otherwise, insert it into the TypeCache so that recursive uses will find
487 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000488 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000489
490 // Convert all the elements.
491 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
492
Anders Carlsson20f12a22009-12-06 18:00:51 +0000493 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000494
495 unsigned FieldNo = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000496 for (RecordDecl::field_iterator I = Decl->field_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000497 E = Decl->field_end();
Douglas Gregora4c46df2008-12-11 17:59:21 +0000498 I != E; ++I, ++FieldNo) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000499 FieldDecl *Field = *I;
500 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000501
Devang Patel73621622009-11-25 17:37:31 +0000502 llvm::StringRef FieldName = Field->getName();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000503
Devang Patelde135022009-04-27 22:40:36 +0000504 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000505 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000506 continue;
507
Chris Lattner9c85ba32008-11-10 06:08:34 +0000508 // Get the location for the field.
509 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel4f6fa232009-04-17 21:35:15 +0000510 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000511 llvm::DICompileUnit FieldDefUnit;
512 unsigned FieldLine = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000514 if (!PLoc.isInvalid()) {
515 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
516 FieldLine = PLoc.getLine();
517 }
Devang Patelec9b5d52009-03-16 23:47:53 +0000518
519 QualType FType = Field->getType();
520 uint64_t FieldSize = 0;
521 unsigned FieldAlign = 0;
522 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Devang Patelec9b5d52009-03-16 23:47:53 +0000524 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000525 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patelec9b5d52009-03-16 23:47:53 +0000526 Expr *BitWidth = Field->getBitWidth();
527 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000528 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlsson20f12a22009-12-06 18:00:51 +0000530 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patelec9b5d52009-03-16 23:47:53 +0000531 }
532
Mike Stump1eb44332009-09-09 15:08:12 +0000533 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
534
Chris Lattner9c85ba32008-11-10 06:08:34 +0000535 // Create a DW_TAG_member node to remember the offset of this field in the
536 // struct. FIXME: This is an absolutely insane way to capture this
537 // information. When we gut debug info, this should be fixed.
538 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
539 FieldName, FieldDefUnit,
540 FieldLine, FieldSize, FieldAlign,
541 FieldOffset, 0, FieldTy);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542 EltTys.push_back(FieldTy);
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner9c85ba32008-11-10 06:08:34 +0000545 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000546 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000547
548 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000549 uint64_t Size = CGM.getContext().getTypeSize(Ty);
550 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Devang Patel0ce73f62009-07-22 18:57:00 +0000552 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000553 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000554 DefUnit, Line, Size, Align, 0, 0,
555 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556
557 // Now that we have a real decl for the struct, replace anything using the
558 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000559 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000560
Chris Lattner9c85ba32008-11-10 06:08:34 +0000561 return RealDecl;
562}
563
Devang Patel9ca36b62009-02-26 21:10:26 +0000564/// CreateType - get objective-c interface type.
565llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
566 llvm::DICompileUnit Unit) {
567 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Devang Patel9ca36b62009-02-26 21:10:26 +0000569 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000570 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000571
572 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000573 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000574 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
575 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
576
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Daniel Dunbard86d3362009-05-18 20:51:58 +0000578 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000579
Devang Patel9ca36b62009-02-26 21:10:26 +0000580 // To handle recursive interface, we
581 // first generate a debug descriptor for the struct as a forward declaration.
582 // Then (if it is a definition) we go through and get debug info for all of
583 // its members. Finally, we create a descriptor for the complete type (which
584 // may refer to the forward decl if the struct is recursive) and replace all
585 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000586 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000587 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000588 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000589 llvm::DIType(), llvm::DIArray(),
590 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Devang Patel9ca36b62009-02-26 21:10:26 +0000592 // If this is just a forward declaration, return it.
593 if (Decl->isForwardDecl())
594 return FwdDecl;
595
Devang Patelffffb032009-11-16 20:09:38 +0000596 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000597 // Otherwise, insert it into the TypeCache so that recursive uses will find
598 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000599 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000600
601 // Convert all the elements.
602 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
603
Devang Patelfbe899f2009-03-10 21:30:26 +0000604 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
605 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000606 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000607 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000608 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000609 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000610 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000611 0 /* offset */, 0, SClassTy);
612 EltTys.push_back(InhTag);
613 }
614
Anders Carlsson20f12a22009-12-06 18:00:51 +0000615 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000616
617 unsigned FieldNo = 0;
618 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
619 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
620 ObjCIvarDecl *Field = *I;
621 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
622
Devang Patel73621622009-11-25 17:37:31 +0000623 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000624
Devang Patelde135022009-04-27 22:40:36 +0000625 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000626 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000627 continue;
628
Devang Patel9ca36b62009-02-26 21:10:26 +0000629 // Get the location for the field.
630 SourceLocation FieldDefLoc = Field->getLocation();
631 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000632 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
633 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
634
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Devang Patel99c20eb2009-03-20 18:24:39 +0000636 QualType FType = Field->getType();
637 uint64_t FieldSize = 0;
638 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000639
Devang Patel99c20eb2009-03-20 18:24:39 +0000640 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Devang Patel99c20eb2009-03-20 18:24:39 +0000642 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000643 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000644 Expr *BitWidth = Field->getBitWidth();
645 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000646 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000647
Anders Carlsson20f12a22009-12-06 18:00:51 +0000648 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000649 }
650
Mike Stump1eb44332009-09-09 15:08:12 +0000651 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
652
Devang Patelc20482b2009-03-19 00:23:53 +0000653 unsigned Flags = 0;
654 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
655 Flags = llvm::DIType::FlagProtected;
656 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
657 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Devang Patel9ca36b62009-02-26 21:10:26 +0000659 // Create a DW_TAG_member node to remember the offset of this field in the
660 // struct. FIXME: This is an absolutely insane way to capture this
661 // information. When we gut debug info, this should be fixed.
662 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
663 FieldName, FieldDefUnit,
664 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000665 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000666 EltTys.push_back(FieldTy);
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Devang Patel9ca36b62009-02-26 21:10:26 +0000669 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000670 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000671
672 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000673 uint64_t Size = CGM.getContext().getTypeSize(Ty);
674 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Devang Patel6c1fddf2009-07-27 18:42:03 +0000676 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000677 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000678 Line, Size, Align, 0, 0, llvm::DIType(),
679 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000680
681 // Now that we have a real decl for the struct, replace anything using the
682 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000683 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000684
Devang Patel9ca36b62009-02-26 21:10:26 +0000685 return RealDecl;
686}
687
Chris Lattner9c85ba32008-11-10 06:08:34 +0000688llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
689 llvm::DICompileUnit Unit) {
690 EnumDecl *Decl = Ty->getDecl();
691
692 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
693
694 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000695 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000696 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000697 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000698 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000699 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Chris Lattner9c85ba32008-11-10 06:08:34 +0000702 // Return a CompositeType for the enum itself.
703 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000704 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000705
Chris Lattner9c85ba32008-11-10 06:08:34 +0000706 SourceLocation DefLoc = Decl->getLocation();
707 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000708 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000709 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
710 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
711
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattner9c85ba32008-11-10 06:08:34 +0000713 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000714 uint64_t Size = 0;
715 unsigned Align = 0;
716 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000717 Size = CGM.getContext().getTypeSize(Ty);
718 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Devang Patelca80a5f2009-10-20 19:55:01 +0000721 llvm::DIType DbgTy =
722 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000723 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000724 Size, Align, 0, 0,
725 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000726 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000727}
728
729llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
730 llvm::DICompileUnit Unit) {
731 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
732 return CreateType(RT, Unit);
733 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
734 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 return llvm::DIType();
737}
738
739llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
740 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000741 uint64_t Size;
742 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000743
744
Nuno Lopes010d5142009-01-28 00:35:17 +0000745 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000746 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000747 Size = 0;
748 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000749 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000750 } else if (Ty->isIncompleteArrayType()) {
751 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000752 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000753 } else {
754 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000755 Size = CGM.getContext().getTypeSize(Ty);
756 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner9c85ba32008-11-10 06:08:34 +0000759 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
760 // interior arrays, do we care? Why aren't nested arrays represented the
761 // obvious/recursive way?
762 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
763 QualType EltTy(Ty, 0);
764 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000765 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000766 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000767 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000768 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000769 // FIXME: Verify this is right for VLAs.
770 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
771 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000775 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000776
Devang Patelca80a5f2009-10-20 19:55:01 +0000777 llvm::DIType DbgTy =
778 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
779 Unit, "", llvm::DICompileUnit(),
780 0, Size, Align, 0, 0,
781 getOrCreateType(EltTy, Unit),
782 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000783 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000784}
785
Anders Carlssona031b352009-11-06 19:19:55 +0000786llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
787 llvm::DICompileUnit Unit) {
788 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
789 Ty, Ty->getPointeeType(), Unit);
790}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791
Anders Carlsson20f12a22009-12-06 18:00:51 +0000792llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
793 llvm::DICompileUnit U) {
794 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
795 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
796
797 if (!Ty->getPointeeType()->isFunctionType()) {
798 // We have a data member pointer type.
799 return PointerDiffDITy;
800 }
801
802 // We have a member function pointer type. Treat it as a struct with two
803 // ptrdiff_t members.
804 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
805
806 uint64_t FieldOffset = 0;
807 llvm::DIDescriptor ElementTypes[2];
808
809 // FIXME: This should probably be a function type instead.
810 ElementTypes[0] =
811 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
812 "ptr", llvm::DICompileUnit(), 0,
813 Info.first, Info.second, FieldOffset, 0,
814 PointerDiffDITy);
815 FieldOffset += Info.first;
816
817 ElementTypes[1] =
818 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
819 "ptr", llvm::DICompileUnit(), 0,
820 Info.first, Info.second, FieldOffset, 0,
821 PointerDiffDITy);
822
823 llvm::DIArray Elements =
824 DebugFactory.GetOrCreateArray(&ElementTypes[0],
825 llvm::array_lengthof(ElementTypes));
826
827 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
828 U, llvm::StringRef("test"),
829 llvm::DICompileUnit(), 0, FieldOffset,
830 0, 0, 0, llvm::DIType(), Elements);
831}
832
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000833static QualType CanonicalizeTypeForDebugInfo(QualType T) {
834 switch (T->getTypeClass()) {
835 default:
836 return T;
837 case Type::TemplateSpecialization:
838 return cast<TemplateSpecializationType>(T)->desugar();
839 case Type::TypeOfExpr: {
840 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
841 return CanonicalizeTypeForDebugInfo(Ty->getUnderlyingExpr()->getType());
842 }
843 case Type::TypeOf:
844 return cast<TypeOfType>(T)->getUnderlyingType();
845 case Type::Decltype:
846 return cast<DecltypeType>(T)->getUnderlyingType();
847 case Type::QualifiedName:
848 return cast<QualifiedNameType>(T)->getNamedType();
849 case Type::SubstTemplateTypeParm:
850 return cast<SubstTemplateTypeParmType>(T)->getReplacementType();
851 case Type::Elaborated:
852 return cast<ElaboratedType>(T)->getUnderlyingType();
853 }
854}
855
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000856/// getOrCreateType - Get the type from the cache or create a new
857/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000858llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
859 llvm::DICompileUnit Unit) {
860 if (Ty.isNull())
861 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000863 // Canonicalize the type.
864 Ty = CanonicalizeTypeForDebugInfo(Ty);
865
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000866 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000867 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000868 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000869 if (it != TypeCache.end()) {
870 // Verify that the debug info still exists.
871 if (&*it->second)
872 return llvm::DIType(cast<llvm::MDNode>(it->second));
873 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000874
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000875 // Otherwise create the type.
876 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000877
878 // And update the type cache.
879 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000880 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000881}
882
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000883/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000884llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
885 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000886 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000887 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000888 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000889
Douglas Gregor2101a822009-12-21 19:57:21 +0000890 const char *Diag = 0;
891
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000892 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000894#define TYPE(Class, Base)
895#define ABSTRACT_TYPE(Class, Base)
896#define NON_CANONICAL_TYPE(Class, Base)
897#define DEPENDENT_TYPE(Class, Base) case Type::Class:
898#include "clang/AST/TypeNodes.def"
899 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000900
Anders Carlssonbfe69952009-11-06 18:24:04 +0000901 // FIXME: Handle these.
902 case Type::ExtVector:
903 case Type::Vector:
Anders Carlssonba578cb2009-11-07 01:19:37 +0000904 case Type::FixedWidthInt:
Anders Carlssonbfe69952009-11-06 18:24:04 +0000905 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +0000906
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000907 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000908 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000909 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000910 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
911 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
912 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
913 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000914 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000915 return CreateType(cast<BlockPointerType>(Ty), Unit);
916 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000917 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000918 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000919 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000920 case Type::FunctionProto:
921 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000922 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000923 case Type::ConstantArray:
924 case Type::VariableArray:
925 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000926 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +0000927
928 case Type::LValueReference:
929 return CreateType(cast<LValueReferenceType>(Ty), Unit);
930
Anders Carlsson20f12a22009-12-06 18:00:51 +0000931 case Type::MemberPointer:
932 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +0000933
934 case Type::TemplateSpecialization:
935 // DWARF can't represent template specialization types; instead,
936 // we drill down to the canonical type, which will be a record type.
937 return CreateType(cast<RecordType>(CGM.getContext().getCanonicalType(Ty)),
938 Unit);
939
940 case Type::Elaborated:
941 // DWARF can't represent elaborated type specifiers any differently from
942 // the underlying type, so create a type node for the underlying type.
943 return CreateTypeNode(cast<ElaboratedType>(Ty)->getUnderlyingType(), Unit);
944
945 case Type::QualifiedName:
946 // DWARF can't represent qualified names any differently from the type
947 // being named, so create a type node for that type.
948 return CreateTypeNode(cast<QualifiedNameType>(Ty)->getNamedType(), Unit);
949
950 case Type::SubstTemplateTypeParm:
951 // DWARF can't represent substituted template type parameter types,
952 // so create a type node for the type that the template type parameter was
953 // replaced with.
954 return CreateTypeNode(cast<SubstTemplateTypeParmType>(Ty)
955 ->getReplacementType(),
956 Unit);
957
958 case Type::TypeOfExpr:
959 case Type::TypeOf:
960 // FIXME: Implement!
961 Diag = "typeof";
962 break;
963
964 case Type::RValueReference:
965 // FIXME: Implement!
966 Diag = "rvalue references";
967 break;
968
969 case Type::Decltype:
970 // FIXME: Implement!
971 Diag = "decltype";
972 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000973 }
Douglas Gregor2101a822009-12-21 19:57:21 +0000974
975 assert(Diag && "Fall through without a diagnostic?");
976 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
977 "debug information for %0 is not yet supported");
978 CGM.getDiags().Report(FullSourceLoc(), DiagID)
979 << Diag;
980 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000981}
982
983/// EmitFunctionStart - Constructs the debug code for entering a function -
984/// "llvm.dbg.func.start.".
Benjamin Kramer155fd792009-12-08 14:04:35 +0000985void CGDebugInfo::EmitFunctionStart(llvm::StringRef Name, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000986 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987 CGBuilderTy &Builder) {
Benjamin Kramer155fd792009-12-08 14:04:35 +0000988 llvm::StringRef LinkageName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Daniel Dunbara2893932009-05-13 23:08:57 +0000990 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000991 //
992 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000993 if (Name[0] == '\01')
Benjamin Kramer155fd792009-12-08 14:04:35 +0000994 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner9c85ba32008-11-10 06:08:34 +0000996 // FIXME: Why is this using CurLoc???
997 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000998 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000999 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001002 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001003 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001006 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001007 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001008}
1009
1010
Chris Lattner9c85ba32008-11-10 06:08:34 +00001011void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001012 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001014 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001015 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001016 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001017 || (SM.getInstantiationLineNumber(CurLoc) ==
1018 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001019 && SM.isFromSameFile(CurLoc, PrevLoc)))
1020 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001021
1022 // Update last state.
1023 PrevLoc = CurLoc;
1024
1025 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001026 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001027 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001028
Devang Patel8fae0602009-11-13 19:10:24 +00001029 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001030 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1031 llvm::DILocation DO(NULL);
1032 llvm::DILocation DL =
1033 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1034 DS, DO);
1035 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001036}
1037
1038/// EmitRegionStart- Constructs the debug code for entering a declarative
1039/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001041 llvm::DIDescriptor D =
1042 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1043 llvm::DIDescriptor() :
1044 llvm::DIDescriptor(RegionStack.back()));
1045 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001046}
1047
1048/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1049/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001050void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001051 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1052
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001053 // Provide an region stop point.
1054 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001056 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001057}
1058
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001059/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1061 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001062 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1063
Devang Patel07739032009-03-27 23:16:32 +00001064 // Do not emit variable debug information while generating optimized code.
1065 // The llvm optimizer and code generator are not yet ready to support
1066 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001067 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001068 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001069 return;
1070
Chris Lattner650cea92009-05-05 04:57:08 +00001071 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001072 QualType Type = Decl->getType();
1073 llvm::DIType Ty = getOrCreateType(Type, Unit);
1074 if (Decl->hasAttr<BlocksAttr>()) {
1075 llvm::DICompileUnit DefUnit;
1076 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1077
1078 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1079
1080 llvm::DIType FieldTy;
1081
1082 QualType FType;
1083 uint64_t FieldSize, FieldOffset;
1084 unsigned FieldAlign;
1085
1086 llvm::DIArray Elements;
1087 llvm::DIType EltTy;
1088
1089 // Build up structure for the byref. See BuildByRefType.
1090 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001091 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001092 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001093 FieldSize = CGM.getContext().getTypeSize(FType);
1094 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001095 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1096 "__isa", DefUnit,
1097 0, FieldSize, FieldAlign,
1098 FieldOffset, 0, FieldTy);
1099 EltTys.push_back(FieldTy);
1100 FieldOffset += FieldSize;
1101
Anders Carlsson20f12a22009-12-06 18:00:51 +00001102 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001103 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001104 FieldSize = CGM.getContext().getTypeSize(FType);
1105 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001106 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1107 "__forwarding", DefUnit,
1108 0, FieldSize, FieldAlign,
1109 FieldOffset, 0, FieldTy);
1110 EltTys.push_back(FieldTy);
1111 FieldOffset += FieldSize;
1112
Anders Carlsson20f12a22009-12-06 18:00:51 +00001113 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001114 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001115 FieldSize = CGM.getContext().getTypeSize(FType);
1116 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001117 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1118 "__flags", DefUnit,
1119 0, FieldSize, FieldAlign,
1120 FieldOffset, 0, FieldTy);
1121 EltTys.push_back(FieldTy);
1122 FieldOffset += FieldSize;
1123
Anders Carlsson20f12a22009-12-06 18:00:51 +00001124 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001125 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 FieldSize = CGM.getContext().getTypeSize(FType);
1127 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001128 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1129 "__size", DefUnit,
1130 0, FieldSize, FieldAlign,
1131 FieldOffset, 0, FieldTy);
1132 EltTys.push_back(FieldTy);
1133 FieldOffset += FieldSize;
1134
Anders Carlsson20f12a22009-12-06 18:00:51 +00001135 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001136 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001137 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001138 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001139 FieldSize = CGM.getContext().getTypeSize(FType);
1140 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001141 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1142 "__copy_helper", DefUnit,
1143 0, FieldSize, FieldAlign,
1144 FieldOffset, 0, FieldTy);
1145 EltTys.push_back(FieldTy);
1146 FieldOffset += FieldSize;
1147
Anders Carlsson20f12a22009-12-06 18:00:51 +00001148 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001149 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 FieldSize = CGM.getContext().getTypeSize(FType);
1151 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001152 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1153 "__destroy_helper", DefUnit,
1154 0, FieldSize, FieldAlign,
1155 FieldOffset, 0, FieldTy);
1156 EltTys.push_back(FieldTy);
1157 FieldOffset += FieldSize;
1158 }
1159
Anders Carlsson20f12a22009-12-06 18:00:51 +00001160 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1161 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001162 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001163 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001164 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001165 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001166
1167 if (NumPaddingBytes > 0) {
1168 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001169 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001170 pad, ArrayType::Normal, 0);
1171 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001172 FieldSize = CGM.getContext().getTypeSize(FType);
1173 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001174 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1175 Unit, "", DefUnit,
1176 0, FieldSize, FieldAlign,
1177 FieldOffset, 0, FieldTy);
1178 EltTys.push_back(FieldTy);
1179 FieldOffset += FieldSize;
1180 }
1181 }
1182
1183 FType = Type;
1184 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001185 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001186 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001187
1188 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001189 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001190 0, FieldSize, FieldAlign,
1191 FieldOffset, 0, FieldTy);
1192 EltTys.push_back(FieldTy);
1193 FieldOffset += FieldSize;
1194
1195 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1196
1197 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1198
1199 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1200 llvm::DICompileUnit(),
1201 0, FieldOffset, 0, 0, Flags,
1202 llvm::DIType(), Elements);
1203 }
Chris Lattner650cea92009-05-05 04:57:08 +00001204
Chris Lattner9c85ba32008-11-10 06:08:34 +00001205 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001206 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001207 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001208 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001209 unsigned Column = 0;
1210 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001211 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001212 Column = PLoc.getColumn();
1213 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001214 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Chris Lattner9c85ba32008-11-10 06:08:34 +00001217 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001218 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001219 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001220 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001221 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001222 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001223 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001224 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001225
Devang Patel8fae0602009-11-13 19:10:24 +00001226 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001227 llvm::DILocation DO(NULL);
1228 llvm::DILocation DL =
Eli Friedman1468ac72009-11-16 20:33:31 +00001229 DebugFactory.CreateLocation(Line, Column, DS, DO);
Devang Patel23908b82009-11-12 18:21:39 +00001230 Builder.SetDebugLocation(Call, DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001231}
1232
Mike Stumpb1a6e682009-09-30 02:43:10 +00001233/// EmitDeclare - Emit local variable declaration debug info.
1234void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1235 llvm::Value *Storage, CGBuilderTy &Builder,
1236 CodeGenFunction *CGF) {
1237 const ValueDecl *Decl = BDRE->getDecl();
1238 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1239
1240 // Do not emit variable debug information while generating optimized code.
1241 // The llvm optimizer and code generator are not yet ready to support
1242 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001243 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001244 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001245 return;
1246
1247 uint64_t XOffset = 0;
1248 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1249 QualType Type = Decl->getType();
1250 llvm::DIType Ty = getOrCreateType(Type, Unit);
1251 if (Decl->hasAttr<BlocksAttr>()) {
1252 llvm::DICompileUnit DefUnit;
1253 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1254
1255 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1256
1257 llvm::DIType FieldTy;
1258
1259 QualType FType;
1260 uint64_t FieldSize, FieldOffset;
1261 unsigned FieldAlign;
1262
1263 llvm::DIArray Elements;
1264 llvm::DIType EltTy;
1265
1266 // Build up structure for the byref. See BuildByRefType.
1267 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001268 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001269 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001270 FieldSize = CGM.getContext().getTypeSize(FType);
1271 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001272 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1273 "__isa", DefUnit,
1274 0, FieldSize, FieldAlign,
1275 FieldOffset, 0, FieldTy);
1276 EltTys.push_back(FieldTy);
1277 FieldOffset += FieldSize;
1278
Anders Carlsson20f12a22009-12-06 18:00:51 +00001279 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001280 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001281 FieldSize = CGM.getContext().getTypeSize(FType);
1282 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001283 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1284 "__forwarding", DefUnit,
1285 0, FieldSize, FieldAlign,
1286 FieldOffset, 0, FieldTy);
1287 EltTys.push_back(FieldTy);
1288 FieldOffset += FieldSize;
1289
Anders Carlsson20f12a22009-12-06 18:00:51 +00001290 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001291 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001292 FieldSize = CGM.getContext().getTypeSize(FType);
1293 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001294 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1295 "__flags", DefUnit,
1296 0, FieldSize, FieldAlign,
1297 FieldOffset, 0, FieldTy);
1298 EltTys.push_back(FieldTy);
1299 FieldOffset += FieldSize;
1300
Anders Carlsson20f12a22009-12-06 18:00:51 +00001301 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001302 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303 FieldSize = CGM.getContext().getTypeSize(FType);
1304 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001305 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1306 "__size", DefUnit,
1307 0, FieldSize, FieldAlign,
1308 FieldOffset, 0, FieldTy);
1309 EltTys.push_back(FieldTy);
1310 FieldOffset += FieldSize;
1311
Anders Carlsson20f12a22009-12-06 18:00:51 +00001312 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001313 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001314 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001315 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001316 FieldSize = CGM.getContext().getTypeSize(FType);
1317 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001318 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1319 "__copy_helper", DefUnit,
1320 0, FieldSize, FieldAlign,
1321 FieldOffset, 0, FieldTy);
1322 EltTys.push_back(FieldTy);
1323 FieldOffset += FieldSize;
1324
Anders Carlsson20f12a22009-12-06 18:00:51 +00001325 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001326 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001327 FieldSize = CGM.getContext().getTypeSize(FType);
1328 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001329 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1330 "__destroy_helper", DefUnit,
1331 0, FieldSize, FieldAlign,
1332 FieldOffset, 0, FieldTy);
1333 EltTys.push_back(FieldTy);
1334 FieldOffset += FieldSize;
1335 }
1336
Anders Carlsson20f12a22009-12-06 18:00:51 +00001337 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1338 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001339 unsigned AlignedOffsetInBytes
1340 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1341 unsigned NumPaddingBytes
1342 = AlignedOffsetInBytes - FieldOffset/8;
1343
1344 if (NumPaddingBytes > 0) {
1345 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001346 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001347 pad, ArrayType::Normal, 0);
1348 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001349 FieldSize = CGM.getContext().getTypeSize(FType);
1350 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001351 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1352 Unit, "", DefUnit,
1353 0, FieldSize, FieldAlign,
1354 FieldOffset, 0, FieldTy);
1355 EltTys.push_back(FieldTy);
1356 FieldOffset += FieldSize;
1357 }
1358 }
1359
1360 FType = Type;
1361 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001362 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001363 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001364
1365 XOffset = FieldOffset;
1366 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001367 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001368 0, FieldSize, FieldAlign,
1369 FieldOffset, 0, FieldTy);
1370 EltTys.push_back(FieldTy);
1371 FieldOffset += FieldSize;
1372
1373 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1374
1375 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1376
1377 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1378 llvm::DICompileUnit(),
1379 0, FieldOffset, 0, 0, Flags,
1380 llvm::DIType(), Elements);
1381 }
1382
1383 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001384 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001385 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1386 unsigned Line = 0;
1387 if (!PLoc.isInvalid())
1388 Line = PLoc.getLine();
1389 else
1390 Unit = llvm::DICompileUnit();
1391
1392 uint64_t offset = CGF->BlockDecls[Decl];
1393 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001394 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001395 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1396 llvm::DIFactory::OpDeref));
1397 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1398 llvm::DIFactory::OpPlus));
1399 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1400 offset));
1401 if (BDRE->isByRef()) {
1402 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1403 llvm::DIFactory::OpDeref));
1404 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1405 llvm::DIFactory::OpPlus));
1406 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1407 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1408 offset));
1409 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1410 llvm::DIFactory::OpDeref));
1411 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1412 llvm::DIFactory::OpPlus));
1413 offset = XOffset/8; // offset of x field
1414 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1415 offset));
1416 }
1417
1418 // Create the descriptor for the variable.
1419 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001420 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001421 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001422 addr);
1423 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001424 llvm::Instruction *Call =
1425 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001426
Devang Patel8fae0602009-11-13 19:10:24 +00001427 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001428 llvm::DILocation DO(NULL);
1429 llvm::DILocation DL =
1430 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
1431 Builder.SetDebugLocation(Call, DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001432}
1433
Chris Lattner9c85ba32008-11-10 06:08:34 +00001434void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1435 llvm::Value *Storage,
1436 CGBuilderTy &Builder) {
1437 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1438}
1439
Mike Stumpb1a6e682009-09-30 02:43:10 +00001440void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1441 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1442 CodeGenFunction *CGF) {
1443 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1444}
1445
Chris Lattner9c85ba32008-11-10 06:08:34 +00001446/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1447/// variable declaration.
1448void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1449 CGBuilderTy &Builder) {
1450 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1451}
1452
1453
1454
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001455/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001456void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001457 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001458
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001459 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001460 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001461 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001462 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1463 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001464
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001465 QualType T = Decl->getType();
1466 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001468 // CodeGen turns int[] into int[1] so we'll do the same here.
1469 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001471 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001472 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Anders Carlsson20f12a22009-12-06 18:00:51 +00001474 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001475 ArrayType::Normal, 0);
1476 }
Devang Patel73621622009-11-25 17:37:31 +00001477 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001478 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001479 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001480 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001481 Var->hasInternalLinkage(),
1482 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001483}
1484
Devang Patel9ca36b62009-02-26 21:10:26 +00001485/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001486void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001487 ObjCInterfaceDecl *Decl) {
1488 // Create global variable debug descriptor.
1489 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001490 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001491 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1492 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001493
Devang Patel73621622009-11-25 17:37:31 +00001494 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001495
Anders Carlsson20f12a22009-12-06 18:00:51 +00001496 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001497 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Devang Patel9ca36b62009-02-26 21:10:26 +00001499 // CodeGen turns int[] into int[1] so we'll do the same here.
1500 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Devang Patel9ca36b62009-02-26 21:10:26 +00001502 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001503 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Anders Carlsson20f12a22009-12-06 18:00:51 +00001505 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001506 ArrayType::Normal, 0);
1507 }
1508
Devang Patelf6a39b72009-10-20 18:26:30 +00001509 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001510 getOrCreateType(T, Unit),
1511 Var->hasInternalLinkage(),
1512 true/*definition*/, Var);
1513}