blob: 6b2c3c6b6ddb13eb68a5b73590af4d2a7bd4dd11 [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
Devang Patel9c6c3a02010-01-14 00:36:21 +000068/// getFunctionName - Get function name for the given FunctionDecl. If the
69/// name is constructred on demand (e.g. C++ destructor) then the name
70/// is stored on the side.
71llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
72 assert (FD && "Invalid FunctionDecl!");
73 IdentifierInfo *FII = FD->getIdentifier();
74 if (FII)
75 return FII->getName();
76
77 // Otherwise construct human readable name for debug info.
78 std::string NS = FD->getNameAsString();
79
80 // Copy this name on the side and use its reference.
81 unsigned Length = NS.length() + 1;
82 char *StrPtr = FunctionNames.Allocate<char>(Length);
83 strncpy(StrPtr, NS.c_str(), Length);
84 return llvm::StringRef(StrPtr);
85}
86
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000087/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000088/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000089llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000090 // Get source file information.
91 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000092 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000093 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000094 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000095 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
96 FileName = PLoc.getFilename();
97 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000100 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +0000101 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +0000102 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000103
Devang Patel446c6192009-04-17 21:06:59 +0000104 // Get absolute path name.
105 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000106 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000107
Devang Patel72240d72009-06-26 18:32:22 +0000108 // See if thie compile unit is representing main source file. Each source
109 // file has corresponding compile unit. There is only one main source
110 // file at a time.
111 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000112 const LangOptions &LO = CGM.getLangOptions();
113 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000114 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000115 if (!CGO.MainFileName.empty()) {
116 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000117 isMain = true;
118 } else {
119 if (Loc.isValid() && SM.isFromMainFile(Loc))
120 isMain = true;
121 }
122 if (isMain)
123 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000124 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000125
Chris Lattner515455a2009-03-25 03:28:08 +0000126 unsigned LangTag;
127 if (LO.CPlusPlus) {
128 if (LO.ObjC1)
129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
130 else
131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
132 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000133 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000134 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000135 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000136 } else {
137 LangTag = llvm::dwarf::DW_LANG_C89;
138 }
Devang Patel446c6192009-04-17 21:06:59 +0000139
Benjamin Kramer47daf682009-12-08 11:02:29 +0000140 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000141#ifdef CLANG_VENDOR
142 CLANG_VENDOR
143#endif
144 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000145
146 // Figure out which version of the ObjC runtime we have.
147 unsigned RuntimeVers = 0;
148 if (LO.ObjC1)
149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000152 return Unit = DebugFactory.CreateCompileUnit(
153 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000155}
156
Devang Patel65e99f22009-02-25 01:36:11 +0000157/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158/// one if necessary.
159llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000160 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161 unsigned Encoding = 0;
162 switch (BT->getKind()) {
163 default:
164 case BuiltinType::Void:
165 return llvm::DIType();
166 case BuiltinType::UChar:
167 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
168 case BuiltinType::Char_S:
169 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
170 case BuiltinType::UShort:
171 case BuiltinType::UInt:
172 case BuiltinType::ULong:
173 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
174 case BuiltinType::Short:
175 case BuiltinType::Int:
176 case BuiltinType::Long:
177 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
178 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
179 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000180 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000181 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000182 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000183 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000184 uint64_t Size = CGM.getContext().getTypeSize(BT);
185 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +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,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000190 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000191 Unit, 0, Size, Align,
192 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000193 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000195
Chris Lattnerb7003772009-04-23 06:13:01 +0000196llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
197 llvm::DICompileUnit Unit) {
198 // Bit size, align and offset of the type.
199 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
200 if (Ty->isComplexIntegerType())
201 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Anders Carlsson20f12a22009-12-06 18:00:51 +0000203 uint64_t Size = CGM.getContext().getTypeSize(Ty);
204 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000205 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Devang Patelca80a5f2009-10-20 19:55:01 +0000207 llvm::DIType DbgTy =
208 DebugFactory.CreateBasicType(Unit, "complex",
209 Unit, 0, Size, Align,
210 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000211 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000212}
213
John McCalla1805292009-09-25 01:40:47 +0000214/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000215/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000216llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
217 QualifierCollector Qc;
218 const Type *T = Qc.strip(Ty);
219
220 // Ignore these qualifiers for now.
221 Qc.removeObjCGCAttr();
222 Qc.removeAddressSpace();
223
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224 // We will create one Derived type for one qualifier and recurse to handle any
225 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000226 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000227 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000228 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000229 Qc.removeConst();
230 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000232 Qc.removeVolatile();
233 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000234 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000235 Qc.removeRestrict();
236 } else {
237 assert(Qc.empty() && "Unknown type qualifier for debug info");
238 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
John McCalla1805292009-09-25 01:40:47 +0000241 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
242
Daniel Dunbar3845f862008-10-31 03:54:29 +0000243 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
244 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000245 llvm::DIType DbgTy =
246 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000248 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000249}
250
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000251llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
252 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000253 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000254 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
255 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000256 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000257}
258
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
260 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000261 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
262 Ty->getPointeeType(), Unit);
263}
264
265llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
266 const Type *Ty,
267 QualType PointeeTy,
268 llvm::DICompileUnit Unit) {
269 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000271 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000272
273 // Size is always the size of a pointer. We can't use getTypeSize here
274 // because that does not return the correct value for references.
275 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000276 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
277 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Devang Patelca80a5f2009-10-20 19:55:01 +0000279 return
Anders Carlssona031b352009-11-06 19:19:55 +0000280 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000281 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000282
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000283}
284
Mike Stump9bc093c2009-05-14 02:03:51 +0000285llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
286 llvm::DICompileUnit Unit) {
287 if (BlockLiteralGenericSet)
288 return BlockLiteralGeneric;
289
290 llvm::DICompileUnit DefUnit;
291 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
292
293 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
294
295 llvm::DIType FieldTy;
296
297 QualType FType;
298 uint64_t FieldSize, FieldOffset;
299 unsigned FieldAlign;
300
301 llvm::DIArray Elements;
302 llvm::DIType EltTy, DescTy;
303
304 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000305 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000307 FieldSize = CGM.getContext().getTypeSize(FType);
308 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 "reserved", DefUnit,
311 0, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
313 EltTys.push_back(FieldTy);
314
315 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000316 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000318 FieldSize = CGM.getContext().getTypeSize(FType);
319 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
321 "Size", DefUnit,
322 0, FieldSize, FieldAlign,
323 FieldOffset, 0, FieldTy);
324 EltTys.push_back(FieldTy);
325
326 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000327 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000328 EltTys.clear();
329
Mike Stump3d363c52009-10-02 02:30:50 +0000330 unsigned Flags = llvm::DIType::FlagAppleBlock;
331
Mike Stump9bc093c2009-05-14 02:03:51 +0000332 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000333 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Mike Stump9bc093c2009-05-14 02:03:51 +0000336 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000337 uint64_t Size = CGM.getContext().getTypeSize(Ty);
338 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Mike Stump9bc093c2009-05-14 02:03:51 +0000340 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
341 Unit, "", llvm::DICompileUnit(),
342 0, Size, Align, 0, 0, EltTy);
343
344 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000345 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000346 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000347 FieldSize = CGM.getContext().getTypeSize(FType);
348 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000349 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
350 "__isa", DefUnit,
351 0, FieldSize, FieldAlign,
352 FieldOffset, 0, FieldTy);
353 EltTys.push_back(FieldTy);
354
355 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000358 FieldSize = CGM.getContext().getTypeSize(FType);
359 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000360 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
361 "__flags", DefUnit,
362 0, FieldSize, FieldAlign,
363 FieldOffset, 0, FieldTy);
364 EltTys.push_back(FieldTy);
365
366 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000367 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000368 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000369 FieldSize = CGM.getContext().getTypeSize(FType);
370 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000371 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
372 "__reserved", DefUnit,
373 0, FieldSize, FieldAlign,
374 FieldOffset, 0, FieldTy);
375 EltTys.push_back(FieldTy);
376
377 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000378 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000380 FieldSize = CGM.getContext().getTypeSize(FType);
381 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000382 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
383 "__FuncPtr", DefUnit,
384 0, FieldSize, FieldAlign,
385 FieldOffset, 0, FieldTy);
386 EltTys.push_back(FieldTy);
387
388 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000391 FieldSize = CGM.getContext().getTypeSize(Ty);
392 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000393 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
394 "__descriptor", DefUnit,
395 0, FieldSize, FieldAlign,
396 FieldOffset, 0, FieldTy);
397 EltTys.push_back(FieldTy);
398
399 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000400 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000401
402 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000403 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000404 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Mike Stump9bc093c2009-05-14 02:03:51 +0000406 BlockLiteralGenericSet = true;
407 BlockLiteralGeneric
408 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
409 "", llvm::DICompileUnit(),
410 0, Size, Align, 0, 0, EltTy);
411 return BlockLiteralGeneric;
412}
413
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
415 llvm::DICompileUnit Unit) {
416 // Typedefs are derived from some other type. If we have a typedef of a
417 // typedef, make sure to emit the whole chain.
418 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner9c85ba32008-11-10 06:08:34 +0000420 // We don't set size information, but do specify where the typedef was
421 // declared.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422 SourceLocation DefLoc = Ty->getDecl()->getLocation();
423 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000424
Anders Carlsson20f12a22009-12-06 18:00:51 +0000425 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000426 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
427 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000428
Devang Patelca80a5f2009-10-20 19:55:01 +0000429 llvm::DIType DbgTy =
430 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
Devang Patel73621622009-11-25 17:37:31 +0000431 Ty->getDecl()->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000432 DefUnit, Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000433 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000434}
435
Chris Lattner9c85ba32008-11-10 06:08:34 +0000436llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
437 llvm::DICompileUnit Unit) {
438 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000439
Chris Lattner9c85ba32008-11-10 06:08:34 +0000440 // Add the result type at least.
441 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443 // Set up remainder of arguments if there is a prototype.
444 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000445 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000446 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
447 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
448 } else {
449 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000450 }
451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000453 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Devang Patelca80a5f2009-10-20 19:55:01 +0000455 llvm::DIType DbgTy =
456 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
457 Unit, "", llvm::DICompileUnit(),
458 0, 0, 0, 0, 0,
459 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000460 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000461}
462
Devang Patel428deb52010-01-19 00:00:59 +0000463/// CollectRecordFields - A helper function to collect debug info for
464/// record fields. This is used while creating debug info entry for a Record.
465void CGDebugInfo::
466CollectRecordFields(const RecordDecl *Decl,
467 llvm::DICompileUnit Unit,
468 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
469 unsigned FieldNo = 0;
470 SourceManager &SM = CGM.getContext().getSourceManager();
471 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
472 for (RecordDecl::field_iterator I = Decl->field_begin(),
473 E = Decl->field_end();
474 I != E; ++I, ++FieldNo) {
475 FieldDecl *Field = *I;
476 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
477
478 llvm::StringRef FieldName = Field->getName();
479
480 // Ignore unnamed fields.
481 if (FieldName.empty())
482 continue;
483
484 // Get the location for the field.
485 SourceLocation FieldDefLoc = Field->getLocation();
486 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
487 llvm::DICompileUnit FieldDefUnit;
488 unsigned FieldLine = 0;
489
490 if (!PLoc.isInvalid()) {
491 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
492 FieldLine = PLoc.getLine();
493 }
494
495 QualType FType = Field->getType();
496 uint64_t FieldSize = 0;
497 unsigned FieldAlign = 0;
498 if (!FType->isIncompleteArrayType()) {
499
500 // Bit size, align and offset of the type.
501 FieldSize = CGM.getContext().getTypeSize(FType);
502 Expr *BitWidth = Field->getBitWidth();
503 if (BitWidth)
504 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
505
506 FieldAlign = CGM.getContext().getTypeAlign(FType);
507 }
508
509 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
510
511 // Create a DW_TAG_member node to remember the offset of this field in the
512 // struct. FIXME: This is an absolutely insane way to capture this
513 // information. When we gut debug info, this should be fixed.
514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
515 FieldName, FieldDefUnit,
516 FieldLine, FieldSize, FieldAlign,
517 FieldOffset, 0, FieldTy);
518 EltTys.push_back(FieldTy);
519 }
520}
521
Devang Patel4125fd22010-01-19 01:54:44 +0000522/// CollectCXXMemberFunctions - A helper function to collect debug info for
523/// C++ member functions.This is used while creating debug info entry for
524/// a Record.
525void CGDebugInfo::
526CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
527 llvm::DICompileUnit Unit,
528 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
529 llvm::DICompositeType &RecordTy) {
530 SourceManager &SM = CGM.getContext().getSourceManager();
531 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
532 E = Decl->method_end(); I != E; ++I) {
533 CXXMethodDecl *Method = *I;
534 llvm::StringRef MethodName;
535 llvm::StringRef MethodLinkageName;
536 llvm::DIType MethodTy = getOrCreateType(Method->getType(), Unit);
537 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(Method)) {
538 if (CDecl->isImplicit())
539 continue;
540 MethodName = Decl->getName();
541 // FIXME : Find linkage name.
542 } else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(Method)) {
543 if (DDecl->isImplicit())
544 continue;
545 MethodName = getFunctionName(Method);
546 // FIXME : Find linkage name.
547 } else {
548 // regular method
549 IdentifierInfo *II = Method->getIdentifier();
550 if (!II)
551 continue;
552 MethodName = Method->getIdentifier()->getName();
553 MethodLinkageName = CGM.getMangledName(Method);
554 }
555
556 // Get the location for the method.
557 SourceLocation MethodDefLoc = Method->getLocation();
558 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
559 llvm::DICompileUnit MethodDefUnit;
560 unsigned MethodLine = 0;
561
562 if (!PLoc.isInvalid()) {
563 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
564 MethodLine = PLoc.getLine();
565 }
566
567 llvm::DISubprogram SP =
568 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
569 MethodLinkageName,
570 MethodDefUnit, MethodLine,
571 MethodTy, false,
572 Method->isThisDeclarationADefinition(),
573 0 /*Virtuality*/, 0 /*VIndex*/,
574 llvm::DIType() /*ContainingType*/);
575 if (Method->isThisDeclarationADefinition())
576 SPCache[cast<FunctionDecl>(Method)] = llvm::WeakVH(SP.getNode());
577 EltTys.push_back(SP);
578 }
579}
580
Devang Patel65e99f22009-02-25 01:36:11 +0000581/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000582llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
583 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000584 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586 unsigned Tag;
587 if (Decl->isStruct())
588 Tag = llvm::dwarf::DW_TAG_structure_type;
589 else if (Decl->isUnion())
590 Tag = llvm::dwarf::DW_TAG_union_type;
591 else {
592 assert(Decl->isClass() && "Unknown RecordType!");
593 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000594 }
595
Anders Carlsson20f12a22009-12-06 18:00:51 +0000596 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000597
Chris Lattner9c85ba32008-11-10 06:08:34 +0000598 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000599 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000600 llvm::DICompileUnit DefUnit;
601 unsigned Line = 0;
602 if (!PLoc.isInvalid()) {
603 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
604 Line = PLoc.getLine();
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattner9c85ba32008-11-10 06:08:34 +0000607 // Records and classes and unions can all be recursive. To handle them, we
608 // first generate a debug descriptor for the struct as a forward declaration.
609 // Then (if it is a definition) we go through and get debug info for all of
610 // its members. Finally, we create a descriptor for the complete type (which
611 // may refer to the forward decl if the struct is recursive) and replace all
612 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000613 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000614 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000615 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000616 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner9c85ba32008-11-10 06:08:34 +0000618 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000619 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000620 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000621
Eli Friedman14d63652009-11-16 21:04:30 +0000622 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000623 // Otherwise, insert it into the TypeCache so that recursive uses will find
624 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000625 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000626
627 // Convert all the elements.
628 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
629
Devang Patel428deb52010-01-19 00:00:59 +0000630 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel4125fd22010-01-19 01:54:44 +0000631 if (CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
632 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattner9c85ba32008-11-10 06:08:34 +0000634 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000635 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000636
637 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000638 uint64_t Size = CGM.getContext().getTypeSize(Ty);
639 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Devang Patel0ce73f62009-07-22 18:57:00 +0000641 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000642 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000643 DefUnit, Line, Size, Align, 0, 0,
644 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000645
646 // Now that we have a real decl for the struct, replace anything using the
647 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000648 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000649
Chris Lattner9c85ba32008-11-10 06:08:34 +0000650 return RealDecl;
651}
652
Devang Patel9ca36b62009-02-26 21:10:26 +0000653/// CreateType - get objective-c interface type.
654llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
655 llvm::DICompileUnit Unit) {
656 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Devang Patel9ca36b62009-02-26 21:10:26 +0000658 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000659 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000660
661 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000662 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000663 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
664 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Daniel Dunbard86d3362009-05-18 20:51:58 +0000667 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000668
Devang Patel9ca36b62009-02-26 21:10:26 +0000669 // To handle recursive interface, we
670 // first generate a debug descriptor for the struct as a forward declaration.
671 // Then (if it is a definition) we go through and get debug info for all of
672 // its members. Finally, we create a descriptor for the complete type (which
673 // may refer to the forward decl if the struct is recursive) and replace all
674 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000675 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000676 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000677 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000678 llvm::DIType(), llvm::DIArray(),
679 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Devang Patel9ca36b62009-02-26 21:10:26 +0000681 // If this is just a forward declaration, return it.
682 if (Decl->isForwardDecl())
683 return FwdDecl;
684
Devang Patelffffb032009-11-16 20:09:38 +0000685 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000686 // Otherwise, insert it into the TypeCache so that recursive uses will find
687 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000688 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000689
690 // Convert all the elements.
691 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
692
Devang Patelfbe899f2009-03-10 21:30:26 +0000693 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
694 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000695 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000696 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000697 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000698 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000699 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000700 0 /* offset */, 0, SClassTy);
701 EltTys.push_back(InhTag);
702 }
703
Anders Carlsson20f12a22009-12-06 18:00:51 +0000704 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000705
706 unsigned FieldNo = 0;
707 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
708 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
709 ObjCIvarDecl *Field = *I;
710 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
711
Devang Patel73621622009-11-25 17:37:31 +0000712 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000713
Devang Patelde135022009-04-27 22:40:36 +0000714 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000715 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000716 continue;
717
Devang Patel9ca36b62009-02-26 21:10:26 +0000718 // Get the location for the field.
719 SourceLocation FieldDefLoc = Field->getLocation();
720 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000721 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
722 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
723
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Devang Patel99c20eb2009-03-20 18:24:39 +0000725 QualType FType = Field->getType();
726 uint64_t FieldSize = 0;
727 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000728
Devang Patel99c20eb2009-03-20 18:24:39 +0000729 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Devang Patel99c20eb2009-03-20 18:24:39 +0000731 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000732 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000733 Expr *BitWidth = Field->getBitWidth();
734 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000735 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000736
Anders Carlsson20f12a22009-12-06 18:00:51 +0000737 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000738 }
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
741
Devang Patelc20482b2009-03-19 00:23:53 +0000742 unsigned Flags = 0;
743 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
744 Flags = llvm::DIType::FlagProtected;
745 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
746 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Devang Patel9ca36b62009-02-26 21:10:26 +0000748 // Create a DW_TAG_member node to remember the offset of this field in the
749 // struct. FIXME: This is an absolutely insane way to capture this
750 // information. When we gut debug info, this should be fixed.
751 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
752 FieldName, FieldDefUnit,
753 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000754 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000755 EltTys.push_back(FieldTy);
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Devang Patel9ca36b62009-02-26 21:10:26 +0000758 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000759 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000760
761 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000762 uint64_t Size = CGM.getContext().getTypeSize(Ty);
763 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Devang Patel6c1fddf2009-07-27 18:42:03 +0000765 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000766 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000767 Line, Size, Align, 0, 0, llvm::DIType(),
768 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000769
770 // Now that we have a real decl for the struct, replace anything using the
771 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000772 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000773
Devang Patel9ca36b62009-02-26 21:10:26 +0000774 return RealDecl;
775}
776
Chris Lattner9c85ba32008-11-10 06:08:34 +0000777llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
778 llvm::DICompileUnit Unit) {
779 EnumDecl *Decl = Ty->getDecl();
780
781 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
782
783 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000784 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000785 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000786 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000787 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000788 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791 // Return a CompositeType for the enum itself.
792 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000793 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 SourceLocation DefLoc = Decl->getLocation();
796 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000797 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000798 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
799 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
800
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000803 uint64_t Size = 0;
804 unsigned Align = 0;
805 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000806 Size = CGM.getContext().getTypeSize(Ty);
807 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Devang Patelca80a5f2009-10-20 19:55:01 +0000810 llvm::DIType DbgTy =
811 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000812 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000813 Size, Align, 0, 0,
814 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000815 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000816}
817
818llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
819 llvm::DICompileUnit Unit) {
820 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
821 return CreateType(RT, Unit);
822 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
823 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattner9c85ba32008-11-10 06:08:34 +0000825 return llvm::DIType();
826}
827
828llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
829 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000830 uint64_t Size;
831 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
833
Nuno Lopes010d5142009-01-28 00:35:17 +0000834 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000835 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000836 Size = 0;
837 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000838 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000839 } else if (Ty->isIncompleteArrayType()) {
840 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000841 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000842 } else {
843 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000844 Size = CGM.getContext().getTypeSize(Ty);
845 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattner9c85ba32008-11-10 06:08:34 +0000848 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
849 // interior arrays, do we care? Why aren't nested arrays represented the
850 // obvious/recursive way?
851 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
852 QualType EltTy(Ty, 0);
853 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000854 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000855 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000856 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000857 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000858 // FIXME: Verify this is right for VLAs.
859 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
860 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattner9c85ba32008-11-10 06:08:34 +0000863 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000864 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000865
Devang Patelca80a5f2009-10-20 19:55:01 +0000866 llvm::DIType DbgTy =
867 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
868 Unit, "", llvm::DICompileUnit(),
869 0, Size, Align, 0, 0,
870 getOrCreateType(EltTy, Unit),
871 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000872 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000873}
874
Anders Carlssona031b352009-11-06 19:19:55 +0000875llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
876 llvm::DICompileUnit Unit) {
877 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
878 Ty, Ty->getPointeeType(), Unit);
879}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000880
Anders Carlsson20f12a22009-12-06 18:00:51 +0000881llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
882 llvm::DICompileUnit U) {
883 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
884 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
885
886 if (!Ty->getPointeeType()->isFunctionType()) {
887 // We have a data member pointer type.
888 return PointerDiffDITy;
889 }
890
891 // We have a member function pointer type. Treat it as a struct with two
892 // ptrdiff_t members.
893 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
894
895 uint64_t FieldOffset = 0;
896 llvm::DIDescriptor ElementTypes[2];
897
898 // FIXME: This should probably be a function type instead.
899 ElementTypes[0] =
900 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
901 "ptr", llvm::DICompileUnit(), 0,
902 Info.first, Info.second, FieldOffset, 0,
903 PointerDiffDITy);
904 FieldOffset += Info.first;
905
906 ElementTypes[1] =
907 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
908 "ptr", llvm::DICompileUnit(), 0,
909 Info.first, Info.second, FieldOffset, 0,
910 PointerDiffDITy);
911
912 llvm::DIArray Elements =
913 DebugFactory.GetOrCreateArray(&ElementTypes[0],
914 llvm::array_lengthof(ElementTypes));
915
916 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
917 U, llvm::StringRef("test"),
918 llvm::DICompileUnit(), 0, FieldOffset,
919 0, 0, 0, llvm::DIType(), Elements);
920}
921
Douglas Gregor840943d2009-12-21 20:18:30 +0000922static QualType UnwrapTypeForDebugInfo(QualType T) {
923 do {
924 QualType LastT = T;
925 switch (T->getTypeClass()) {
926 default:
927 return T;
928 case Type::TemplateSpecialization:
929 T = cast<TemplateSpecializationType>(T)->desugar();
930 break;
931 case Type::TypeOfExpr: {
932 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
933 T = Ty->getUnderlyingExpr()->getType();
934 break;
935 }
936 case Type::TypeOf:
937 T = cast<TypeOfType>(T)->getUnderlyingType();
938 break;
939 case Type::Decltype:
940 T = cast<DecltypeType>(T)->getUnderlyingType();
941 break;
942 case Type::QualifiedName:
943 T = cast<QualifiedNameType>(T)->getNamedType();
944 break;
945 case Type::SubstTemplateTypeParm:
946 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
947 break;
948 case Type::Elaborated:
949 T = cast<ElaboratedType>(T)->getUnderlyingType();
950 break;
951 }
952
953 assert(T != LastT && "Type unwrapping failed to unwrap!");
954 if (T == LastT)
955 return T;
956 } while (true);
957
958 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000959}
960
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000961/// getOrCreateType - Get the type from the cache or create a new
962/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000963llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
964 llvm::DICompileUnit Unit) {
965 if (Ty.isNull())
966 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregor840943d2009-12-21 20:18:30 +0000968 // Unwrap the type as needed for debug information.
969 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000970
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000971 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000972 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000973 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000974 if (it != TypeCache.end()) {
975 // Verify that the debug info still exists.
976 if (&*it->second)
977 return llvm::DIType(cast<llvm::MDNode>(it->second));
978 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000979
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000980 // Otherwise create the type.
981 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000982
983 // And update the type cache.
984 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000985 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000986}
987
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000988/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000989llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
990 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000991 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000992 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000993 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000994
Douglas Gregor2101a822009-12-21 19:57:21 +0000995 const char *Diag = 0;
996
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000997 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000999#define TYPE(Class, Base)
1000#define ABSTRACT_TYPE(Class, Base)
1001#define NON_CANONICAL_TYPE(Class, Base)
1002#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1003#include "clang/AST/TypeNodes.def"
1004 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001005
Anders Carlssonbfe69952009-11-06 18:24:04 +00001006 // FIXME: Handle these.
1007 case Type::ExtVector:
1008 case Type::Vector:
1009 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001010
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001011 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001012 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001013 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001014 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1015 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1016 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1017 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001018 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001019 return CreateType(cast<BlockPointerType>(Ty), Unit);
1020 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001021 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001022 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001023 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001024 case Type::FunctionProto:
1025 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001026 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001027 case Type::ConstantArray:
1028 case Type::VariableArray:
1029 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001030 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001031
1032 case Type::LValueReference:
1033 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1034
Anders Carlsson20f12a22009-12-06 18:00:51 +00001035 case Type::MemberPointer:
1036 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001037
1038 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001039 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001040 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001041 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001042 case Type::TypeOfExpr:
1043 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001044 case Type::Decltype:
1045 llvm_unreachable("type should have been unwrapped!");
1046 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001047
1048 case Type::RValueReference:
1049 // FIXME: Implement!
1050 Diag = "rvalue references";
1051 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001052 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001053
1054 assert(Diag && "Fall through without a diagnostic?");
1055 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1056 "debug information for %0 is not yet supported");
1057 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1058 << Diag;
1059 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001060}
1061
1062/// EmitFunctionStart - Constructs the debug code for entering a function -
1063/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001064void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001065 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001066 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Devang Patel9c6c3a02010-01-14 00:36:21 +00001068 llvm::StringRef Name;
1069 llvm::StringRef LinkageName;
1070
1071 const Decl *D = GD.getDecl();
1072 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001073 // If there is a DISubprogram for this function available then use it.
1074 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1075 FI = SPCache.find(FD);
1076 if (FI != SPCache.end()) {
1077 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1078 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1079 RegionStack.push_back(SP.getNode());
1080 return;
1081 }
1082 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001083 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001084 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001085 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001086 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001087 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001088 } else {
1089 // Use llvm function name as linkage name.
1090 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001091 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001092 if (!Name.empty() && Name[0] == '\01')
1093 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Devang Patel98a200b2010-01-14 18:06:13 +00001096 // It is expected that CurLoc is set before using EmitFunctionStart.
1097 // Usually, CurLoc points to the left bracket location of compound
1098 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001099 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001100 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001101 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner9c85ba32008-11-10 06:08:34 +00001103 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001104 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001105 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001106 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001108 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001109 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001110}
1111
1112
Chris Lattner9c85ba32008-11-10 06:08:34 +00001113void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001114 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001116 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001117 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001118 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001119 || (SM.getInstantiationLineNumber(CurLoc) ==
1120 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001121 && SM.isFromSameFile(CurLoc, PrevLoc)))
1122 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001123
1124 // Update last state.
1125 PrevLoc = CurLoc;
1126
1127 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001128 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001129 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001130
Devang Patel8fae0602009-11-13 19:10:24 +00001131 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001132 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1133 llvm::DILocation DO(NULL);
1134 llvm::DILocation DL =
1135 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1136 DS, DO);
1137 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001138}
1139
1140/// EmitRegionStart- Constructs the debug code for entering a declarative
1141/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001142void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001143 llvm::DIDescriptor D =
1144 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1145 llvm::DIDescriptor() :
1146 llvm::DIDescriptor(RegionStack.back()));
1147 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001148}
1149
1150/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1151/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001152void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001153 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1154
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001155 // Provide an region stop point.
1156 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001158 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001159}
1160
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001161/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001162void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1163 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001164 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1165
Devang Patel07739032009-03-27 23:16:32 +00001166 // Do not emit variable debug information while generating optimized code.
1167 // The llvm optimizer and code generator are not yet ready to support
1168 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001169 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001170 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001171 return;
1172
Chris Lattner650cea92009-05-05 04:57:08 +00001173 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001174 QualType Type = Decl->getType();
1175 llvm::DIType Ty = getOrCreateType(Type, Unit);
1176 if (Decl->hasAttr<BlocksAttr>()) {
1177 llvm::DICompileUnit DefUnit;
1178 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1179
1180 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1181
1182 llvm::DIType FieldTy;
1183
1184 QualType FType;
1185 uint64_t FieldSize, FieldOffset;
1186 unsigned FieldAlign;
1187
1188 llvm::DIArray Elements;
1189 llvm::DIType EltTy;
1190
1191 // Build up structure for the byref. See BuildByRefType.
1192 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001193 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001194 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001195 FieldSize = CGM.getContext().getTypeSize(FType);
1196 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001197 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1198 "__isa", DefUnit,
1199 0, FieldSize, FieldAlign,
1200 FieldOffset, 0, FieldTy);
1201 EltTys.push_back(FieldTy);
1202 FieldOffset += FieldSize;
1203
Anders Carlsson20f12a22009-12-06 18:00:51 +00001204 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001205 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001206 FieldSize = CGM.getContext().getTypeSize(FType);
1207 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001208 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1209 "__forwarding", DefUnit,
1210 0, FieldSize, FieldAlign,
1211 FieldOffset, 0, FieldTy);
1212 EltTys.push_back(FieldTy);
1213 FieldOffset += FieldSize;
1214
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001215 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001216 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001217 FieldSize = CGM.getContext().getTypeSize(FType);
1218 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001219 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1220 "__flags", DefUnit,
1221 0, FieldSize, FieldAlign,
1222 FieldOffset, 0, FieldTy);
1223 EltTys.push_back(FieldTy);
1224 FieldOffset += FieldSize;
1225
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001226 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001227 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001228 FieldSize = CGM.getContext().getTypeSize(FType);
1229 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001230 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1231 "__size", DefUnit,
1232 0, FieldSize, FieldAlign,
1233 FieldOffset, 0, FieldTy);
1234 EltTys.push_back(FieldTy);
1235 FieldOffset += FieldSize;
1236
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001238 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001239 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001240 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001241 FieldSize = CGM.getContext().getTypeSize(FType);
1242 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001243 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1244 "__copy_helper", DefUnit,
1245 0, FieldSize, FieldAlign,
1246 FieldOffset, 0, FieldTy);
1247 EltTys.push_back(FieldTy);
1248 FieldOffset += FieldSize;
1249
Anders Carlsson20f12a22009-12-06 18:00:51 +00001250 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001251 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001252 FieldSize = CGM.getContext().getTypeSize(FType);
1253 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001254 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1255 "__destroy_helper", DefUnit,
1256 0, FieldSize, FieldAlign,
1257 FieldOffset, 0, FieldTy);
1258 EltTys.push_back(FieldTy);
1259 FieldOffset += FieldSize;
1260 }
1261
Anders Carlsson20f12a22009-12-06 18:00:51 +00001262 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1263 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001264 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001265 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001266 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001267 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001268
1269 if (NumPaddingBytes > 0) {
1270 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001271 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001272 pad, ArrayType::Normal, 0);
1273 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001274 FieldSize = CGM.getContext().getTypeSize(FType);
1275 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001276 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1277 Unit, "", DefUnit,
1278 0, FieldSize, FieldAlign,
1279 FieldOffset, 0, FieldTy);
1280 EltTys.push_back(FieldTy);
1281 FieldOffset += FieldSize;
1282 }
1283 }
1284
1285 FType = Type;
1286 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001287 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001288 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001289
1290 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001291 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001292 0, FieldSize, FieldAlign,
1293 FieldOffset, 0, FieldTy);
1294 EltTys.push_back(FieldTy);
1295 FieldOffset += FieldSize;
1296
1297 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1298
1299 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1300
1301 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1302 llvm::DICompileUnit(),
1303 0, FieldOffset, 0, 0, Flags,
1304 llvm::DIType(), Elements);
1305 }
Chris Lattner650cea92009-05-05 04:57:08 +00001306
Chris Lattner9c85ba32008-11-10 06:08:34 +00001307 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001308 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001309 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001310 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001311 unsigned Column = 0;
1312 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001313 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001314 Column = PLoc.getColumn();
1315 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001316 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattner9c85ba32008-11-10 06:08:34 +00001319 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001320 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001321 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001322 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001323 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001324 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001325 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001326 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001327
Devang Patel8fae0602009-11-13 19:10:24 +00001328 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001329 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001330 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1331
Chris Lattner23e92c02009-12-28 23:41:39 +00001332 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001333}
1334
Mike Stumpb1a6e682009-09-30 02:43:10 +00001335/// EmitDeclare - Emit local variable declaration debug info.
1336void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1337 llvm::Value *Storage, CGBuilderTy &Builder,
1338 CodeGenFunction *CGF) {
1339 const ValueDecl *Decl = BDRE->getDecl();
1340 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1341
1342 // Do not emit variable debug information while generating optimized code.
1343 // The llvm optimizer and code generator are not yet ready to support
1344 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001345 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001346 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001347 return;
1348
1349 uint64_t XOffset = 0;
1350 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1351 QualType Type = Decl->getType();
1352 llvm::DIType Ty = getOrCreateType(Type, Unit);
1353 if (Decl->hasAttr<BlocksAttr>()) {
1354 llvm::DICompileUnit DefUnit;
1355 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1356
1357 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1358
1359 llvm::DIType FieldTy;
1360
1361 QualType FType;
1362 uint64_t FieldSize, FieldOffset;
1363 unsigned FieldAlign;
1364
1365 llvm::DIArray Elements;
1366 llvm::DIType EltTy;
1367
1368 // Build up structure for the byref. See BuildByRefType.
1369 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001370 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001371 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001372 FieldSize = CGM.getContext().getTypeSize(FType);
1373 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001374 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1375 "__isa", DefUnit,
1376 0, FieldSize, FieldAlign,
1377 FieldOffset, 0, FieldTy);
1378 EltTys.push_back(FieldTy);
1379 FieldOffset += FieldSize;
1380
Anders Carlsson20f12a22009-12-06 18:00:51 +00001381 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001382 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001383 FieldSize = CGM.getContext().getTypeSize(FType);
1384 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001385 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1386 "__forwarding", DefUnit,
1387 0, FieldSize, FieldAlign,
1388 FieldOffset, 0, FieldTy);
1389 EltTys.push_back(FieldTy);
1390 FieldOffset += FieldSize;
1391
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001392 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001393 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001394 FieldSize = CGM.getContext().getTypeSize(FType);
1395 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001396 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1397 "__flags", DefUnit,
1398 0, FieldSize, FieldAlign,
1399 FieldOffset, 0, FieldTy);
1400 EltTys.push_back(FieldTy);
1401 FieldOffset += FieldSize;
1402
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001403 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001404 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001405 FieldSize = CGM.getContext().getTypeSize(FType);
1406 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001407 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1408 "__size", DefUnit,
1409 0, FieldSize, FieldAlign,
1410 FieldOffset, 0, FieldTy);
1411 EltTys.push_back(FieldTy);
1412 FieldOffset += FieldSize;
1413
Anders Carlsson20f12a22009-12-06 18:00:51 +00001414 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001415 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001416 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001417 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001418 FieldSize = CGM.getContext().getTypeSize(FType);
1419 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001420 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1421 "__copy_helper", DefUnit,
1422 0, FieldSize, FieldAlign,
1423 FieldOffset, 0, FieldTy);
1424 EltTys.push_back(FieldTy);
1425 FieldOffset += FieldSize;
1426
Anders Carlsson20f12a22009-12-06 18:00:51 +00001427 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001428 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001429 FieldSize = CGM.getContext().getTypeSize(FType);
1430 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001431 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1432 "__destroy_helper", DefUnit,
1433 0, FieldSize, FieldAlign,
1434 FieldOffset, 0, FieldTy);
1435 EltTys.push_back(FieldTy);
1436 FieldOffset += FieldSize;
1437 }
1438
Anders Carlsson20f12a22009-12-06 18:00:51 +00001439 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1440 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001441 unsigned AlignedOffsetInBytes
1442 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1443 unsigned NumPaddingBytes
1444 = AlignedOffsetInBytes - FieldOffset/8;
1445
1446 if (NumPaddingBytes > 0) {
1447 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001448 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001449 pad, ArrayType::Normal, 0);
1450 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001451 FieldSize = CGM.getContext().getTypeSize(FType);
1452 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001453 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1454 Unit, "", DefUnit,
1455 0, FieldSize, FieldAlign,
1456 FieldOffset, 0, FieldTy);
1457 EltTys.push_back(FieldTy);
1458 FieldOffset += FieldSize;
1459 }
1460 }
1461
1462 FType = Type;
1463 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001464 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001465 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001466
1467 XOffset = FieldOffset;
1468 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001469 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001470 0, FieldSize, FieldAlign,
1471 FieldOffset, 0, FieldTy);
1472 EltTys.push_back(FieldTy);
1473 FieldOffset += FieldSize;
1474
1475 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1476
1477 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1478
1479 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1480 llvm::DICompileUnit(),
1481 0, FieldOffset, 0, 0, Flags,
1482 llvm::DIType(), Elements);
1483 }
1484
1485 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001486 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001487 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1488 unsigned Line = 0;
1489 if (!PLoc.isInvalid())
1490 Line = PLoc.getLine();
1491 else
1492 Unit = llvm::DICompileUnit();
1493
Ken Dyck199c3d62010-01-11 17:06:35 +00001494 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001495 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001496 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001497 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1498 llvm::DIFactory::OpDeref));
1499 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1500 llvm::DIFactory::OpPlus));
1501 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001502 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001503 if (BDRE->isByRef()) {
1504 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1505 llvm::DIFactory::OpDeref));
1506 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1507 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001508 // offset of __forwarding field
1509 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001510 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001511 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001512 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1513 llvm::DIFactory::OpDeref));
1514 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1515 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001516 // offset of x field
1517 offset = CharUnits::fromQuantity(XOffset/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001518 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001519 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001520 }
1521
1522 // Create the descriptor for the variable.
1523 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001524 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001525 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001526 addr);
1527 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001528 llvm::Instruction *Call =
1529 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001530
Devang Patel8fae0602009-11-13 19:10:24 +00001531 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001532 llvm::DILocation DO(NULL);
1533 llvm::DILocation DL =
1534 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001535
Chris Lattner23e92c02009-12-28 23:41:39 +00001536 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001537}
1538
Chris Lattner9c85ba32008-11-10 06:08:34 +00001539void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1540 llvm::Value *Storage,
1541 CGBuilderTy &Builder) {
1542 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1543}
1544
Mike Stumpb1a6e682009-09-30 02:43:10 +00001545void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1546 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1547 CodeGenFunction *CGF) {
1548 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1549}
1550
Chris Lattner9c85ba32008-11-10 06:08:34 +00001551/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1552/// variable declaration.
1553void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1554 CGBuilderTy &Builder) {
1555 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1556}
1557
1558
1559
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001560/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001561void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001562 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001563
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001564 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001565 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001566 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001567 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1568 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001569
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001570 QualType T = Decl->getType();
1571 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001573 // CodeGen turns int[] into int[1] so we'll do the same here.
1574 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001576 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001577 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Anders Carlsson20f12a22009-12-06 18:00:51 +00001579 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001580 ArrayType::Normal, 0);
1581 }
Devang Patel73621622009-11-25 17:37:31 +00001582 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001583 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001584 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001585 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001586 Var->hasInternalLinkage(),
1587 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001588}
1589
Devang Patel9ca36b62009-02-26 21:10:26 +00001590/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001591void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001592 ObjCInterfaceDecl *Decl) {
1593 // Create global variable debug descriptor.
1594 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001595 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001596 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1597 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001598
Devang Patel73621622009-11-25 17:37:31 +00001599 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001600
Anders Carlsson20f12a22009-12-06 18:00:51 +00001601 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001602 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Devang Patel9ca36b62009-02-26 21:10:26 +00001604 // CodeGen turns int[] into int[1] so we'll do the same here.
1605 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Devang Patel9ca36b62009-02-26 21:10:26 +00001607 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001608 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Anders Carlsson20f12a22009-12-06 18:00:51 +00001610 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001611 ArrayType::Normal, 0);
1612 }
1613
Devang Patelf6a39b72009-10-20 18:26:30 +00001614 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001615 getOrCreateType(T, Unit),
1616 Var->hasInternalLinkage(),
1617 true/*definition*/, Var);
1618}