blob: 89f3820d39cc1585f56af765597efeabf1b85395 [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
Douglas Gregor840943d2009-12-21 20:18:30 +0000833static QualType UnwrapTypeForDebugInfo(QualType T) {
834 do {
835 QualType LastT = T;
836 switch (T->getTypeClass()) {
837 default:
838 return T;
839 case Type::TemplateSpecialization:
840 T = cast<TemplateSpecializationType>(T)->desugar();
841 break;
842 case Type::TypeOfExpr: {
843 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
844 T = Ty->getUnderlyingExpr()->getType();
845 break;
846 }
847 case Type::TypeOf:
848 T = cast<TypeOfType>(T)->getUnderlyingType();
849 break;
850 case Type::Decltype:
851 T = cast<DecltypeType>(T)->getUnderlyingType();
852 break;
853 case Type::QualifiedName:
854 T = cast<QualifiedNameType>(T)->getNamedType();
855 break;
856 case Type::SubstTemplateTypeParm:
857 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
858 break;
859 case Type::Elaborated:
860 T = cast<ElaboratedType>(T)->getUnderlyingType();
861 break;
862 }
863
864 assert(T != LastT && "Type unwrapping failed to unwrap!");
865 if (T == LastT)
866 return T;
867 } while (true);
868
869 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000870}
871
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000872/// getOrCreateType - Get the type from the cache or create a new
873/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000874llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
875 llvm::DICompileUnit Unit) {
876 if (Ty.isNull())
877 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregor840943d2009-12-21 20:18:30 +0000879 // Unwrap the type as needed for debug information.
880 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000881
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000882 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000883 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000884 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000885 if (it != TypeCache.end()) {
886 // Verify that the debug info still exists.
887 if (&*it->second)
888 return llvm::DIType(cast<llvm::MDNode>(it->second));
889 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000890
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000891 // Otherwise create the type.
892 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000893
894 // And update the type cache.
895 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000896 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000897}
898
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000899/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000900llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
901 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000902 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000903 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000904 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000905
Douglas Gregor2101a822009-12-21 19:57:21 +0000906 const char *Diag = 0;
907
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000908 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000909 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000910#define TYPE(Class, Base)
911#define ABSTRACT_TYPE(Class, Base)
912#define NON_CANONICAL_TYPE(Class, Base)
913#define DEPENDENT_TYPE(Class, Base) case Type::Class:
914#include "clang/AST/TypeNodes.def"
915 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000916
Anders Carlssonbfe69952009-11-06 18:24:04 +0000917 // FIXME: Handle these.
918 case Type::ExtVector:
919 case Type::Vector:
920 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +0000921
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000922 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000923 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000924 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000925 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
926 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
927 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
928 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000929 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000930 return CreateType(cast<BlockPointerType>(Ty), Unit);
931 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000932 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000933 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000934 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000935 case Type::FunctionProto:
936 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000937 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000938 case Type::ConstantArray:
939 case Type::VariableArray:
940 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000941 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +0000942
943 case Type::LValueReference:
944 return CreateType(cast<LValueReferenceType>(Ty), Unit);
945
Anders Carlsson20f12a22009-12-06 18:00:51 +0000946 case Type::MemberPointer:
947 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +0000948
949 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +0000950 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +0000951 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +0000952 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +0000953 case Type::TypeOfExpr:
954 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +0000955 case Type::Decltype:
956 llvm_unreachable("type should have been unwrapped!");
957 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +0000958
959 case Type::RValueReference:
960 // FIXME: Implement!
961 Diag = "rvalue references";
962 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000963 }
Douglas Gregor2101a822009-12-21 19:57:21 +0000964
965 assert(Diag && "Fall through without a diagnostic?");
966 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
967 "debug information for %0 is not yet supported");
968 CGM.getDiags().Report(FullSourceLoc(), DiagID)
969 << Diag;
970 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000971}
972
973/// EmitFunctionStart - Constructs the debug code for entering a function -
974/// "llvm.dbg.func.start.".
Benjamin Kramer155fd792009-12-08 14:04:35 +0000975void CGDebugInfo::EmitFunctionStart(llvm::StringRef Name, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000976 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 CGBuilderTy &Builder) {
Benjamin Kramer155fd792009-12-08 14:04:35 +0000978 llvm::StringRef LinkageName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Daniel Dunbara2893932009-05-13 23:08:57 +0000980 // Skip the asm prefix if it exists.
Daniel Dunbarbbd53af2009-05-14 01:45:24 +0000981 //
982 // FIXME: This should probably be the unmangled name?
Daniel Dunbara2893932009-05-13 23:08:57 +0000983 if (Name[0] == '\01')
Benjamin Kramer155fd792009-12-08 14:04:35 +0000984 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986 // FIXME: Why is this using CurLoc???
987 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000988 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +0000989 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +0000992 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +0000993 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +0000994 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000996 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +0000997 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000998}
999
1000
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001002 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001004 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001005 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001006 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001007 || (SM.getInstantiationLineNumber(CurLoc) ==
1008 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001009 && SM.isFromSameFile(CurLoc, PrevLoc)))
1010 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001011
1012 // Update last state.
1013 PrevLoc = CurLoc;
1014
1015 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001016 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001017 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001018
Devang Patel8fae0602009-11-13 19:10:24 +00001019 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001020 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1021 llvm::DILocation DO(NULL);
1022 llvm::DILocation DL =
1023 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1024 DS, DO);
1025 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001026}
1027
1028/// EmitRegionStart- Constructs the debug code for entering a declarative
1029/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001030void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001031 llvm::DIDescriptor D =
1032 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1033 llvm::DIDescriptor() :
1034 llvm::DIDescriptor(RegionStack.back()));
1035 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001036}
1037
1038/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1039/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001041 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1042
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001043 // Provide an region stop point.
1044 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001046 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001047}
1048
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001049/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001050void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1051 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001052 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1053
Devang Patel07739032009-03-27 23:16:32 +00001054 // Do not emit variable debug information while generating optimized code.
1055 // The llvm optimizer and code generator are not yet ready to support
1056 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001057 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001058 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001059 return;
1060
Chris Lattner650cea92009-05-05 04:57:08 +00001061 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001062 QualType Type = Decl->getType();
1063 llvm::DIType Ty = getOrCreateType(Type, Unit);
1064 if (Decl->hasAttr<BlocksAttr>()) {
1065 llvm::DICompileUnit DefUnit;
1066 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1067
1068 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1069
1070 llvm::DIType FieldTy;
1071
1072 QualType FType;
1073 uint64_t FieldSize, FieldOffset;
1074 unsigned FieldAlign;
1075
1076 llvm::DIArray Elements;
1077 llvm::DIType EltTy;
1078
1079 // Build up structure for the byref. See BuildByRefType.
1080 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001081 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001082 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001083 FieldSize = CGM.getContext().getTypeSize(FType);
1084 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001085 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1086 "__isa", DefUnit,
1087 0, FieldSize, FieldAlign,
1088 FieldOffset, 0, FieldTy);
1089 EltTys.push_back(FieldTy);
1090 FieldOffset += FieldSize;
1091
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001093 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001094 FieldSize = CGM.getContext().getTypeSize(FType);
1095 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001096 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1097 "__forwarding", DefUnit,
1098 0, FieldSize, FieldAlign,
1099 FieldOffset, 0, FieldTy);
1100 EltTys.push_back(FieldTy);
1101 FieldOffset += FieldSize;
1102
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001103 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001104 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001105 FieldSize = CGM.getContext().getTypeSize(FType);
1106 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001107 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1108 "__flags", DefUnit,
1109 0, FieldSize, FieldAlign,
1110 FieldOffset, 0, FieldTy);
1111 EltTys.push_back(FieldTy);
1112 FieldOffset += FieldSize;
1113
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001114 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001115 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001116 FieldSize = CGM.getContext().getTypeSize(FType);
1117 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001118 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1119 "__size", DefUnit,
1120 0, FieldSize, FieldAlign,
1121 FieldOffset, 0, FieldTy);
1122 EltTys.push_back(FieldTy);
1123 FieldOffset += FieldSize;
1124
Anders Carlsson20f12a22009-12-06 18:00:51 +00001125 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001126 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001127 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001128 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001129 FieldSize = CGM.getContext().getTypeSize(FType);
1130 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001131 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1132 "__copy_helper", DefUnit,
1133 0, FieldSize, FieldAlign,
1134 FieldOffset, 0, FieldTy);
1135 EltTys.push_back(FieldTy);
1136 FieldOffset += FieldSize;
1137
Anders Carlsson20f12a22009-12-06 18:00:51 +00001138 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001139 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001140 FieldSize = CGM.getContext().getTypeSize(FType);
1141 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001142 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1143 "__destroy_helper", DefUnit,
1144 0, FieldSize, FieldAlign,
1145 FieldOffset, 0, FieldTy);
1146 EltTys.push_back(FieldTy);
1147 FieldOffset += FieldSize;
1148 }
1149
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1151 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001152 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001153 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001154 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001155 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001156
1157 if (NumPaddingBytes > 0) {
1158 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001160 pad, ArrayType::Normal, 0);
1161 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001162 FieldSize = CGM.getContext().getTypeSize(FType);
1163 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001164 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1165 Unit, "", DefUnit,
1166 0, FieldSize, FieldAlign,
1167 FieldOffset, 0, FieldTy);
1168 EltTys.push_back(FieldTy);
1169 FieldOffset += FieldSize;
1170 }
1171 }
1172
1173 FType = Type;
1174 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001175 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001176 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001177
1178 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001179 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001180 0, FieldSize, FieldAlign,
1181 FieldOffset, 0, FieldTy);
1182 EltTys.push_back(FieldTy);
1183 FieldOffset += FieldSize;
1184
1185 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1186
1187 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1188
1189 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1190 llvm::DICompileUnit(),
1191 0, FieldOffset, 0, 0, Flags,
1192 llvm::DIType(), Elements);
1193 }
Chris Lattner650cea92009-05-05 04:57:08 +00001194
Chris Lattner9c85ba32008-11-10 06:08:34 +00001195 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001196 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001197 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001198 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001199 unsigned Column = 0;
1200 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001201 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001202 Column = PLoc.getColumn();
1203 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001204 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Chris Lattner9c85ba32008-11-10 06:08:34 +00001207 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001208 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001209 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001210 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001211 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001212 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001213 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001214 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001215
Devang Patel8fae0602009-11-13 19:10:24 +00001216 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001217 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001218 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1219
Chris Lattner23e92c02009-12-28 23:41:39 +00001220 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001221}
1222
Mike Stumpb1a6e682009-09-30 02:43:10 +00001223/// EmitDeclare - Emit local variable declaration debug info.
1224void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1225 llvm::Value *Storage, CGBuilderTy &Builder,
1226 CodeGenFunction *CGF) {
1227 const ValueDecl *Decl = BDRE->getDecl();
1228 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1229
1230 // Do not emit variable debug information while generating optimized code.
1231 // The llvm optimizer and code generator are not yet ready to support
1232 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001233 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001234 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001235 return;
1236
1237 uint64_t XOffset = 0;
1238 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1239 QualType Type = Decl->getType();
1240 llvm::DIType Ty = getOrCreateType(Type, Unit);
1241 if (Decl->hasAttr<BlocksAttr>()) {
1242 llvm::DICompileUnit DefUnit;
1243 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1244
1245 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1246
1247 llvm::DIType FieldTy;
1248
1249 QualType FType;
1250 uint64_t FieldSize, FieldOffset;
1251 unsigned FieldAlign;
1252
1253 llvm::DIArray Elements;
1254 llvm::DIType EltTy;
1255
1256 // Build up structure for the byref. See BuildByRefType.
1257 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001258 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001259 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001260 FieldSize = CGM.getContext().getTypeSize(FType);
1261 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001262 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1263 "__isa", DefUnit,
1264 0, FieldSize, FieldAlign,
1265 FieldOffset, 0, FieldTy);
1266 EltTys.push_back(FieldTy);
1267 FieldOffset += FieldSize;
1268
Anders Carlsson20f12a22009-12-06 18:00:51 +00001269 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001270 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001271 FieldSize = CGM.getContext().getTypeSize(FType);
1272 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001273 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1274 "__forwarding", DefUnit,
1275 0, FieldSize, FieldAlign,
1276 FieldOffset, 0, FieldTy);
1277 EltTys.push_back(FieldTy);
1278 FieldOffset += FieldSize;
1279
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001280 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001281 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001282 FieldSize = CGM.getContext().getTypeSize(FType);
1283 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001284 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1285 "__flags", DefUnit,
1286 0, FieldSize, FieldAlign,
1287 FieldOffset, 0, FieldTy);
1288 EltTys.push_back(FieldTy);
1289 FieldOffset += FieldSize;
1290
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001291 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001292 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293 FieldSize = CGM.getContext().getTypeSize(FType);
1294 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001295 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1296 "__size", DefUnit,
1297 0, FieldSize, FieldAlign,
1298 FieldOffset, 0, FieldTy);
1299 EltTys.push_back(FieldTy);
1300 FieldOffset += FieldSize;
1301
Anders Carlsson20f12a22009-12-06 18:00:51 +00001302 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001303 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001304 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001305 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001306 FieldSize = CGM.getContext().getTypeSize(FType);
1307 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001308 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1309 "__copy_helper", DefUnit,
1310 0, FieldSize, FieldAlign,
1311 FieldOffset, 0, FieldTy);
1312 EltTys.push_back(FieldTy);
1313 FieldOffset += FieldSize;
1314
Anders Carlsson20f12a22009-12-06 18:00:51 +00001315 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001316 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001317 FieldSize = CGM.getContext().getTypeSize(FType);
1318 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001319 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1320 "__destroy_helper", DefUnit,
1321 0, FieldSize, FieldAlign,
1322 FieldOffset, 0, FieldTy);
1323 EltTys.push_back(FieldTy);
1324 FieldOffset += FieldSize;
1325 }
1326
Anders Carlsson20f12a22009-12-06 18:00:51 +00001327 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1328 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001329 unsigned AlignedOffsetInBytes
1330 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1331 unsigned NumPaddingBytes
1332 = AlignedOffsetInBytes - FieldOffset/8;
1333
1334 if (NumPaddingBytes > 0) {
1335 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001336 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001337 pad, ArrayType::Normal, 0);
1338 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001339 FieldSize = CGM.getContext().getTypeSize(FType);
1340 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001341 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1342 Unit, "", DefUnit,
1343 0, FieldSize, FieldAlign,
1344 FieldOffset, 0, FieldTy);
1345 EltTys.push_back(FieldTy);
1346 FieldOffset += FieldSize;
1347 }
1348 }
1349
1350 FType = Type;
1351 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001352 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001353 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001354
1355 XOffset = FieldOffset;
1356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001357 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001358 0, FieldSize, FieldAlign,
1359 FieldOffset, 0, FieldTy);
1360 EltTys.push_back(FieldTy);
1361 FieldOffset += FieldSize;
1362
1363 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1364
1365 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1366
1367 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1368 llvm::DICompileUnit(),
1369 0, FieldOffset, 0, 0, Flags,
1370 llvm::DIType(), Elements);
1371 }
1372
1373 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001374 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001375 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1376 unsigned Line = 0;
1377 if (!PLoc.isInvalid())
1378 Line = PLoc.getLine();
1379 else
1380 Unit = llvm::DICompileUnit();
1381
Ken Dyck199c3d62010-01-11 17:06:35 +00001382 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001383 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001384 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001385 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1386 llvm::DIFactory::OpDeref));
1387 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1388 llvm::DIFactory::OpPlus));
1389 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001390 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001391 if (BDRE->isByRef()) {
1392 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1393 llvm::DIFactory::OpDeref));
1394 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1395 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001396 // offset of __forwarding field
1397 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001398 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001399 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001400 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1401 llvm::DIFactory::OpDeref));
1402 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1403 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001404 // offset of x field
1405 offset = CharUnits::fromQuantity(XOffset/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001406 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001407 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001408 }
1409
1410 // Create the descriptor for the variable.
1411 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001412 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001413 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001414 addr);
1415 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001416 llvm::Instruction *Call =
1417 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001418
Devang Patel8fae0602009-11-13 19:10:24 +00001419 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001420 llvm::DILocation DO(NULL);
1421 llvm::DILocation DL =
1422 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001423
Chris Lattner23e92c02009-12-28 23:41:39 +00001424 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001425}
1426
Chris Lattner9c85ba32008-11-10 06:08:34 +00001427void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1428 llvm::Value *Storage,
1429 CGBuilderTy &Builder) {
1430 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1431}
1432
Mike Stumpb1a6e682009-09-30 02:43:10 +00001433void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1434 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1435 CodeGenFunction *CGF) {
1436 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1437}
1438
Chris Lattner9c85ba32008-11-10 06:08:34 +00001439/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1440/// variable declaration.
1441void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1442 CGBuilderTy &Builder) {
1443 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1444}
1445
1446
1447
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001448/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001449void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001450 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001451
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001452 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001453 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001454 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001455 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1456 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001457
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001458 QualType T = Decl->getType();
1459 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001461 // CodeGen turns int[] into int[1] so we'll do the same here.
1462 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001464 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001465 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Anders Carlsson20f12a22009-12-06 18:00:51 +00001467 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001468 ArrayType::Normal, 0);
1469 }
Devang Patel73621622009-11-25 17:37:31 +00001470 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001471 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001472 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001473 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001474 Var->hasInternalLinkage(),
1475 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001476}
1477
Devang Patel9ca36b62009-02-26 21:10:26 +00001478/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001479void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001480 ObjCInterfaceDecl *Decl) {
1481 // Create global variable debug descriptor.
1482 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001483 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001484 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1485 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001486
Devang Patel73621622009-11-25 17:37:31 +00001487 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001488
Anders Carlsson20f12a22009-12-06 18:00:51 +00001489 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001490 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Devang Patel9ca36b62009-02-26 21:10:26 +00001492 // CodeGen turns int[] into int[1] so we'll do the same here.
1493 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Devang Patel9ca36b62009-02-26 21:10:26 +00001495 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001496 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Anders Carlsson20f12a22009-12-06 18:00:51 +00001498 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001499 ArrayType::Normal, 0);
1500 }
1501
Devang Patelf6a39b72009-10-20 18:26:30 +00001502 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001503 getOrCreateType(T, Unit),
1504 Var->hasInternalLinkage(),
1505 true/*definition*/, Var);
1506}