blob: 0a92710ff415361c9d269d594b869e7146f83649 [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
890 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000892#define TYPE(Class, Base)
893#define ABSTRACT_TYPE(Class, Base)
894#define NON_CANONICAL_TYPE(Class, Base)
895#define DEPENDENT_TYPE(Class, Base) case Type::Class:
896#include "clang/AST/TypeNodes.def"
897 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000898
Anders Carlssonbfe69952009-11-06 18:24:04 +0000899 // FIXME: Handle these.
900 case Type::ExtVector:
901 case Type::Vector:
Anders Carlssonba578cb2009-11-07 01:19:37 +0000902 case Type::FixedWidthInt:
Anders Carlssonbfe69952009-11-06 18:24:04 +0000903 return llvm::DIType();
Daniel Dunbar03faac32009-09-19 19:27:14 +0000904 default:
Anders Carlssonba578cb2009-11-07 01:19:37 +0000905 assert(false && "Unhandled type class!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000906 return llvm::DIType();
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);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000933 }
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000934}
935
936/// EmitFunctionStart - Constructs the debug code for entering a function -
937/// "llvm.dbg.func.start.".
Benjamin Kramer155fd792009-12-08 14:04:35 +0000938void CGDebugInfo::EmitFunctionStart(llvm::StringRef Name, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000939 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000940 CGBuilderTy &Builder) {
Benjamin Kramer155fd792009-12-08 14:04:35 +0000941 llvm::StringRef LinkageName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Daniel Dunbara2893932009-05-13 23:08:57 +0000943 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000944 //
945 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000946 if (Name[0] == '\01')
Benjamin Kramer155fd792009-12-08 14:04:35 +0000947 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattner9c85ba32008-11-10 06:08:34 +0000949 // FIXME: Why is this using CurLoc???
950 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000951 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000952 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000955 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +0000956 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000957 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000959 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +0000960 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000961}
962
963
Chris Lattner9c85ba32008-11-10 06:08:34 +0000964void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000965 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000967 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000968 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000969 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +0000970 || (SM.getInstantiationLineNumber(CurLoc) ==
971 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000972 && SM.isFromSameFile(CurLoc, PrevLoc)))
973 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000974
975 // Update last state.
976 PrevLoc = CurLoc;
977
978 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +0000980 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +0000981
Devang Patel8fae0602009-11-13 19:10:24 +0000982 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +0000983 llvm::DIScope DS = llvm::DIScope(DR.getNode());
984 llvm::DILocation DO(NULL);
985 llvm::DILocation DL =
986 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
987 DS, DO);
988 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000989}
990
991/// EmitRegionStart- Constructs the debug code for entering a declarative
992/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +0000993void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +0000994 llvm::DIDescriptor D =
995 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
996 llvm::DIDescriptor() :
997 llvm::DIDescriptor(RegionStack.back()));
998 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000999}
1000
1001/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1002/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001003void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001004 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1005
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001006 // Provide an region stop point.
1007 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001009 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001010}
1011
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001012/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001013void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1014 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001015 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1016
Devang Patel07739032009-03-27 23:16:32 +00001017 // Do not emit variable debug information while generating optimized code.
1018 // The llvm optimizer and code generator are not yet ready to support
1019 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001020 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001021 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001022 return;
1023
Chris Lattner650cea92009-05-05 04:57:08 +00001024 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001025 QualType Type = Decl->getType();
1026 llvm::DIType Ty = getOrCreateType(Type, Unit);
1027 if (Decl->hasAttr<BlocksAttr>()) {
1028 llvm::DICompileUnit DefUnit;
1029 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1030
1031 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1032
1033 llvm::DIType FieldTy;
1034
1035 QualType FType;
1036 uint64_t FieldSize, FieldOffset;
1037 unsigned FieldAlign;
1038
1039 llvm::DIArray Elements;
1040 llvm::DIType EltTy;
1041
1042 // Build up structure for the byref. See BuildByRefType.
1043 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001044 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001045 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 FieldSize = CGM.getContext().getTypeSize(FType);
1047 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001048 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1049 "__isa", DefUnit,
1050 0, FieldSize, FieldAlign,
1051 FieldOffset, 0, FieldTy);
1052 EltTys.push_back(FieldTy);
1053 FieldOffset += FieldSize;
1054
Anders Carlsson20f12a22009-12-06 18:00:51 +00001055 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001056 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001057 FieldSize = CGM.getContext().getTypeSize(FType);
1058 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001059 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1060 "__forwarding", DefUnit,
1061 0, FieldSize, FieldAlign,
1062 FieldOffset, 0, FieldTy);
1063 EltTys.push_back(FieldTy);
1064 FieldOffset += FieldSize;
1065
Anders Carlsson20f12a22009-12-06 18:00:51 +00001066 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001067 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001068 FieldSize = CGM.getContext().getTypeSize(FType);
1069 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001070 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1071 "__flags", DefUnit,
1072 0, FieldSize, FieldAlign,
1073 FieldOffset, 0, FieldTy);
1074 EltTys.push_back(FieldTy);
1075 FieldOffset += FieldSize;
1076
Anders Carlsson20f12a22009-12-06 18:00:51 +00001077 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stump39605b42009-09-22 02:12:52 +00001078 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001079 FieldSize = CGM.getContext().getTypeSize(FType);
1080 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001081 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1082 "__size", DefUnit,
1083 0, FieldSize, FieldAlign,
1084 FieldOffset, 0, FieldTy);
1085 EltTys.push_back(FieldTy);
1086 FieldOffset += FieldSize;
1087
Anders Carlsson20f12a22009-12-06 18:00:51 +00001088 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001089 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001090 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001091 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 FieldSize = CGM.getContext().getTypeSize(FType);
1093 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001094 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1095 "__copy_helper", DefUnit,
1096 0, FieldSize, FieldAlign,
1097 FieldOffset, 0, FieldTy);
1098 EltTys.push_back(FieldTy);
1099 FieldOffset += FieldSize;
1100
Anders Carlsson20f12a22009-12-06 18:00:51 +00001101 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001102 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001103 FieldSize = CGM.getContext().getTypeSize(FType);
1104 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001105 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1106 "__destroy_helper", DefUnit,
1107 0, FieldSize, FieldAlign,
1108 FieldOffset, 0, FieldTy);
1109 EltTys.push_back(FieldTy);
1110 FieldOffset += FieldSize;
1111 }
1112
Anders Carlsson20f12a22009-12-06 18:00:51 +00001113 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1114 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001115 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001116 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001117 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001118 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001119
1120 if (NumPaddingBytes > 0) {
1121 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001122 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001123 pad, ArrayType::Normal, 0);
1124 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001125 FieldSize = CGM.getContext().getTypeSize(FType);
1126 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001127 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1128 Unit, "", DefUnit,
1129 0, FieldSize, FieldAlign,
1130 FieldOffset, 0, FieldTy);
1131 EltTys.push_back(FieldTy);
1132 FieldOffset += FieldSize;
1133 }
1134 }
1135
1136 FType = Type;
1137 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001138 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001139 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001140
1141 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001142 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001143 0, FieldSize, FieldAlign,
1144 FieldOffset, 0, FieldTy);
1145 EltTys.push_back(FieldTy);
1146 FieldOffset += FieldSize;
1147
1148 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1149
1150 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1151
1152 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1153 llvm::DICompileUnit(),
1154 0, FieldOffset, 0, 0, Flags,
1155 llvm::DIType(), Elements);
1156 }
Chris Lattner650cea92009-05-05 04:57:08 +00001157
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001160 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001161 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001162 unsigned Column = 0;
1163 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001164 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001165 Column = PLoc.getColumn();
1166 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001167 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattner9c85ba32008-11-10 06:08:34 +00001170 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001171 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001172 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001173 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001174 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001175 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001176 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001177 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001178
Devang Patel8fae0602009-11-13 19:10:24 +00001179 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001180 llvm::DILocation DO(NULL);
1181 llvm::DILocation DL =
Eli Friedman1468ac72009-11-16 20:33:31 +00001182 DebugFactory.CreateLocation(Line, Column, DS, DO);
Devang Patel23908b82009-11-12 18:21:39 +00001183 Builder.SetDebugLocation(Call, DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001184}
1185
Mike Stumpb1a6e682009-09-30 02:43:10 +00001186/// EmitDeclare - Emit local variable declaration debug info.
1187void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1188 llvm::Value *Storage, CGBuilderTy &Builder,
1189 CodeGenFunction *CGF) {
1190 const ValueDecl *Decl = BDRE->getDecl();
1191 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1192
1193 // Do not emit variable debug information while generating optimized code.
1194 // The llvm optimizer and code generator are not yet ready to support
1195 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001196 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001197 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001198 return;
1199
1200 uint64_t XOffset = 0;
1201 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1202 QualType Type = Decl->getType();
1203 llvm::DIType Ty = getOrCreateType(Type, Unit);
1204 if (Decl->hasAttr<BlocksAttr>()) {
1205 llvm::DICompileUnit DefUnit;
1206 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1207
1208 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1209
1210 llvm::DIType FieldTy;
1211
1212 QualType FType;
1213 uint64_t FieldSize, FieldOffset;
1214 unsigned FieldAlign;
1215
1216 llvm::DIArray Elements;
1217 llvm::DIType EltTy;
1218
1219 // Build up structure for the byref. See BuildByRefType.
1220 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001221 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001222 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001223 FieldSize = CGM.getContext().getTypeSize(FType);
1224 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001225 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1226 "__isa", DefUnit,
1227 0, FieldSize, FieldAlign,
1228 FieldOffset, 0, FieldTy);
1229 EltTys.push_back(FieldTy);
1230 FieldOffset += FieldSize;
1231
Anders Carlsson20f12a22009-12-06 18:00:51 +00001232 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001233 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001234 FieldSize = CGM.getContext().getTypeSize(FType);
1235 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001236 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1237 "__forwarding", DefUnit,
1238 0, FieldSize, FieldAlign,
1239 FieldOffset, 0, FieldTy);
1240 EltTys.push_back(FieldTy);
1241 FieldOffset += FieldSize;
1242
Anders Carlsson20f12a22009-12-06 18:00:51 +00001243 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001244 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001245 FieldSize = CGM.getContext().getTypeSize(FType);
1246 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001247 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1248 "__flags", DefUnit,
1249 0, FieldSize, FieldAlign,
1250 FieldOffset, 0, FieldTy);
1251 EltTys.push_back(FieldTy);
1252 FieldOffset += FieldSize;
1253
Anders Carlsson20f12a22009-12-06 18:00:51 +00001254 FType = CGM.getContext().getFixedWidthIntType(32, true); // Int32Ty;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001255 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001256 FieldSize = CGM.getContext().getTypeSize(FType);
1257 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001258 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1259 "__size", DefUnit,
1260 0, FieldSize, FieldAlign,
1261 FieldOffset, 0, FieldTy);
1262 EltTys.push_back(FieldTy);
1263 FieldOffset += FieldSize;
1264
Anders Carlsson20f12a22009-12-06 18:00:51 +00001265 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001266 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001267 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001268 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001269 FieldSize = CGM.getContext().getTypeSize(FType);
1270 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001271 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1272 "__copy_helper", DefUnit,
1273 0, FieldSize, FieldAlign,
1274 FieldOffset, 0, FieldTy);
1275 EltTys.push_back(FieldTy);
1276 FieldOffset += FieldSize;
1277
Anders Carlsson20f12a22009-12-06 18:00:51 +00001278 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001279 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001280 FieldSize = CGM.getContext().getTypeSize(FType);
1281 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001282 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1283 "__destroy_helper", DefUnit,
1284 0, FieldSize, FieldAlign,
1285 FieldOffset, 0, FieldTy);
1286 EltTys.push_back(FieldTy);
1287 FieldOffset += FieldSize;
1288 }
1289
Anders Carlsson20f12a22009-12-06 18:00:51 +00001290 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1291 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001292 unsigned AlignedOffsetInBytes
1293 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1294 unsigned NumPaddingBytes
1295 = AlignedOffsetInBytes - FieldOffset/8;
1296
1297 if (NumPaddingBytes > 0) {
1298 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001299 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001300 pad, ArrayType::Normal, 0);
1301 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001302 FieldSize = CGM.getContext().getTypeSize(FType);
1303 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001304 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1305 Unit, "", DefUnit,
1306 0, FieldSize, FieldAlign,
1307 FieldOffset, 0, FieldTy);
1308 EltTys.push_back(FieldTy);
1309 FieldOffset += FieldSize;
1310 }
1311 }
1312
1313 FType = Type;
1314 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001315 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001316 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001317
1318 XOffset = FieldOffset;
1319 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001320 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001321 0, FieldSize, FieldAlign,
1322 FieldOffset, 0, FieldTy);
1323 EltTys.push_back(FieldTy);
1324 FieldOffset += FieldSize;
1325
1326 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1327
1328 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1329
1330 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1331 llvm::DICompileUnit(),
1332 0, FieldOffset, 0, 0, Flags,
1333 llvm::DIType(), Elements);
1334 }
1335
1336 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001337 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001338 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1339 unsigned Line = 0;
1340 if (!PLoc.isInvalid())
1341 Line = PLoc.getLine();
1342 else
1343 Unit = llvm::DICompileUnit();
1344
1345 uint64_t offset = CGF->BlockDecls[Decl];
1346 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001347 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001348 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1349 llvm::DIFactory::OpDeref));
1350 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1351 llvm::DIFactory::OpPlus));
1352 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1353 offset));
1354 if (BDRE->isByRef()) {
1355 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1356 llvm::DIFactory::OpDeref));
1357 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1358 llvm::DIFactory::OpPlus));
1359 offset = CGF->LLVMPointerWidth/8; // offset of __forwarding field
1360 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1361 offset));
1362 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1363 llvm::DIFactory::OpDeref));
1364 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1365 llvm::DIFactory::OpPlus));
1366 offset = XOffset/8; // offset of x field
1367 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1368 offset));
1369 }
1370
1371 // Create the descriptor for the variable.
1372 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001373 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001374 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001375 addr);
1376 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001377 llvm::Instruction *Call =
1378 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001379
Devang Patel8fae0602009-11-13 19:10:24 +00001380 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001381 llvm::DILocation DO(NULL);
1382 llvm::DILocation DL =
1383 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
1384 Builder.SetDebugLocation(Call, DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001385}
1386
Chris Lattner9c85ba32008-11-10 06:08:34 +00001387void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1388 llvm::Value *Storage,
1389 CGBuilderTy &Builder) {
1390 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1391}
1392
Mike Stumpb1a6e682009-09-30 02:43:10 +00001393void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1394 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1395 CodeGenFunction *CGF) {
1396 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1397}
1398
Chris Lattner9c85ba32008-11-10 06:08:34 +00001399/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1400/// variable declaration.
1401void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1402 CGBuilderTy &Builder) {
1403 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1404}
1405
1406
1407
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001408/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001409void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001410 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001411
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001412 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001413 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001414 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001415 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1416 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001417
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001418 QualType T = Decl->getType();
1419 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001421 // CodeGen turns int[] into int[1] so we'll do the same here.
1422 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001424 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001425 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Anders Carlsson20f12a22009-12-06 18:00:51 +00001427 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001428 ArrayType::Normal, 0);
1429 }
Devang Patel73621622009-11-25 17:37:31 +00001430 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001431 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001432 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001433 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001434 Var->hasInternalLinkage(),
1435 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001436}
1437
Devang Patel9ca36b62009-02-26 21:10:26 +00001438/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001439void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001440 ObjCInterfaceDecl *Decl) {
1441 // Create global variable debug descriptor.
1442 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001443 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001444 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1445 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001446
Devang Patel73621622009-11-25 17:37:31 +00001447 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001448
Anders Carlsson20f12a22009-12-06 18:00:51 +00001449 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001450 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Devang Patel9ca36b62009-02-26 21:10:26 +00001452 // CodeGen turns int[] into int[1] so we'll do the same here.
1453 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Devang Patel9ca36b62009-02-26 21:10:26 +00001455 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001456 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Anders Carlsson20f12a22009-12-06 18:00:51 +00001458 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001459 ArrayType::Normal, 0);
1460 }
1461
Devang Patelf6a39b72009-10-20 18:26:30 +00001462 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001463 getOrCreateType(T, Unit),
1464 Var->hasInternalLinkage(),
1465 true/*definition*/, Var);
1466}