blob: 1ffad3edcac7be3ac85e205ba704a8e01fe0e995 [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 {
Devang Patel6d012762010-01-19 02:02:11 +0000548 if (Method->isImplicit())
Devang Patel4125fd22010-01-19 01:54:44 +0000549 continue;
Devang Patel6d012762010-01-19 02:02:11 +0000550 // regular method
551 MethodName = getFunctionName(Method);
Devang Patel4125fd22010-01-19 01:54:44 +0000552 MethodLinkageName = CGM.getMangledName(Method);
553 }
554
555 // Get the location for the method.
556 SourceLocation MethodDefLoc = Method->getLocation();
557 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
558 llvm::DICompileUnit MethodDefUnit;
559 unsigned MethodLine = 0;
560
561 if (!PLoc.isInvalid()) {
562 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
563 MethodLine = PLoc.getLine();
564 }
565
566 llvm::DISubprogram SP =
567 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
568 MethodLinkageName,
569 MethodDefUnit, MethodLine,
570 MethodTy, false,
571 Method->isThisDeclarationADefinition(),
572 0 /*Virtuality*/, 0 /*VIndex*/,
573 llvm::DIType() /*ContainingType*/);
574 if (Method->isThisDeclarationADefinition())
575 SPCache[cast<FunctionDecl>(Method)] = llvm::WeakVH(SP.getNode());
576 EltTys.push_back(SP);
577 }
578}
579
Devang Patel65e99f22009-02-25 01:36:11 +0000580/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
582 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000583 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner9c85ba32008-11-10 06:08:34 +0000585 unsigned Tag;
586 if (Decl->isStruct())
587 Tag = llvm::dwarf::DW_TAG_structure_type;
588 else if (Decl->isUnion())
589 Tag = llvm::dwarf::DW_TAG_union_type;
590 else {
591 assert(Decl->isClass() && "Unknown RecordType!");
592 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000593 }
594
Anders Carlsson20f12a22009-12-06 18:00:51 +0000595 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000596
Chris Lattner9c85ba32008-11-10 06:08:34 +0000597 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000598 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000599 llvm::DICompileUnit DefUnit;
600 unsigned Line = 0;
601 if (!PLoc.isInvalid()) {
602 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
603 Line = PLoc.getLine();
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner9c85ba32008-11-10 06:08:34 +0000606 // Records and classes and unions can all be recursive. To handle them, we
607 // first generate a debug descriptor for the struct as a forward declaration.
608 // Then (if it is a definition) we go through and get debug info for all of
609 // its members. Finally, we create a descriptor for the complete type (which
610 // may refer to the forward decl if the struct is recursive) and replace all
611 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000612
613 // A Decl->getName() is not unique. However, the debug info descriptors
614 // are uniqued. The debug info descriptor describing record's context is
615 // necessary to keep two Decl's descriptor unique if their name match.
616 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
617 // use type's name in FwdDecl.
618 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000619 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000620 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000621 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000622 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner9c85ba32008-11-10 06:08:34 +0000624 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000625 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000626 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000627
Eli Friedman14d63652009-11-16 21:04:30 +0000628 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000629 // Otherwise, insert it into the TypeCache so that recursive uses will find
630 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000631 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000632
633 // Convert all the elements.
634 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
635
Devang Patel428deb52010-01-19 00:00:59 +0000636 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel4125fd22010-01-19 01:54:44 +0000637 if (CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
638 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000641 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000642
643 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000644 uint64_t Size = CGM.getContext().getTypeSize(Ty);
645 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Devang Patel0ce73f62009-07-22 18:57:00 +0000647 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000648 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000649 DefUnit, Line, Size, Align, 0, 0,
650 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000651
652 // Now that we have a real decl for the struct, replace anything using the
653 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000654 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000655
Chris Lattner9c85ba32008-11-10 06:08:34 +0000656 return RealDecl;
657}
658
Devang Patel9ca36b62009-02-26 21:10:26 +0000659/// CreateType - get objective-c interface type.
660llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
661 llvm::DICompileUnit Unit) {
662 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Devang Patel9ca36b62009-02-26 21:10:26 +0000664 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000665 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000666
667 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000668 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000669 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
670 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
671
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Daniel Dunbard86d3362009-05-18 20:51:58 +0000673 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000674
Devang Patel9ca36b62009-02-26 21:10:26 +0000675 // To handle recursive interface, we
676 // first generate a debug descriptor for the struct as a forward declaration.
677 // Then (if it is a definition) we go through and get debug info for all of
678 // its members. Finally, we create a descriptor for the complete type (which
679 // may refer to the forward decl if the struct is recursive) and replace all
680 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000681 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000682 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000683 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000684 llvm::DIType(), llvm::DIArray(),
685 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Devang Patel9ca36b62009-02-26 21:10:26 +0000687 // If this is just a forward declaration, return it.
688 if (Decl->isForwardDecl())
689 return FwdDecl;
690
Devang Patelffffb032009-11-16 20:09:38 +0000691 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000692 // Otherwise, insert it into the TypeCache so that recursive uses will find
693 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000694 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000695
696 // Convert all the elements.
697 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
698
Devang Patelfbe899f2009-03-10 21:30:26 +0000699 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
700 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000701 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000702 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000703 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000704 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000705 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000706 0 /* offset */, 0, SClassTy);
707 EltTys.push_back(InhTag);
708 }
709
Anders Carlsson20f12a22009-12-06 18:00:51 +0000710 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000711
712 unsigned FieldNo = 0;
713 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
714 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
715 ObjCIvarDecl *Field = *I;
716 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
717
Devang Patel73621622009-11-25 17:37:31 +0000718 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000719
Devang Patelde135022009-04-27 22:40:36 +0000720 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000721 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000722 continue;
723
Devang Patel9ca36b62009-02-26 21:10:26 +0000724 // Get the location for the field.
725 SourceLocation FieldDefLoc = Field->getLocation();
726 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000727 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
728 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
729
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Devang Patel99c20eb2009-03-20 18:24:39 +0000731 QualType FType = Field->getType();
732 uint64_t FieldSize = 0;
733 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000734
Devang Patel99c20eb2009-03-20 18:24:39 +0000735 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Devang Patel99c20eb2009-03-20 18:24:39 +0000737 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000738 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000739 Expr *BitWidth = Field->getBitWidth();
740 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000741 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000742
Anders Carlsson20f12a22009-12-06 18:00:51 +0000743 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000744 }
745
Mike Stump1eb44332009-09-09 15:08:12 +0000746 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
747
Devang Patelc20482b2009-03-19 00:23:53 +0000748 unsigned Flags = 0;
749 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
750 Flags = llvm::DIType::FlagProtected;
751 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
752 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Devang Patel9ca36b62009-02-26 21:10:26 +0000754 // Create a DW_TAG_member node to remember the offset of this field in the
755 // struct. FIXME: This is an absolutely insane way to capture this
756 // information. When we gut debug info, this should be fixed.
757 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
758 FieldName, FieldDefUnit,
759 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000760 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000761 EltTys.push_back(FieldTy);
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Devang Patel9ca36b62009-02-26 21:10:26 +0000764 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000765 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000766
767 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000768 uint64_t Size = CGM.getContext().getTypeSize(Ty);
769 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Devang Patel6c1fddf2009-07-27 18:42:03 +0000771 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000772 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000773 Line, Size, Align, 0, 0, llvm::DIType(),
774 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000775
776 // Now that we have a real decl for the struct, replace anything using the
777 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000778 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000779
Devang Patel9ca36b62009-02-26 21:10:26 +0000780 return RealDecl;
781}
782
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
784 llvm::DICompileUnit Unit) {
785 EnumDecl *Decl = Ty->getDecl();
786
787 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
788
789 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000791 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000792 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000793 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000794 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Chris Lattner9c85ba32008-11-10 06:08:34 +0000797 // Return a CompositeType for the enum itself.
798 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000799 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 SourceLocation DefLoc = Decl->getLocation();
802 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000803 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000804 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
805 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
806
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000809 uint64_t Size = 0;
810 unsigned Align = 0;
811 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000812 Size = CGM.getContext().getTypeSize(Ty);
813 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Devang Patelca80a5f2009-10-20 19:55:01 +0000816 llvm::DIType DbgTy =
817 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000818 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000819 Size, Align, 0, 0,
820 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000821 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822}
823
824llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
825 llvm::DICompileUnit Unit) {
826 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
827 return CreateType(RT, Unit);
828 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
829 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattner9c85ba32008-11-10 06:08:34 +0000831 return llvm::DIType();
832}
833
834llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
835 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000836 uint64_t Size;
837 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
839
Nuno Lopes010d5142009-01-28 00:35:17 +0000840 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000841 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000842 Size = 0;
843 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000844 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000845 } else if (Ty->isIncompleteArrayType()) {
846 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000847 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000848 } else {
849 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000850 Size = CGM.getContext().getTypeSize(Ty);
851 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner9c85ba32008-11-10 06:08:34 +0000854 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
855 // interior arrays, do we care? Why aren't nested arrays represented the
856 // obvious/recursive way?
857 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
858 QualType EltTy(Ty, 0);
859 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000860 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000861 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000862 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000863 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000864 // FIXME: Verify this is right for VLAs.
865 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
866 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000870 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000871
Devang Patelca80a5f2009-10-20 19:55:01 +0000872 llvm::DIType DbgTy =
873 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
874 Unit, "", llvm::DICompileUnit(),
875 0, Size, Align, 0, 0,
876 getOrCreateType(EltTy, Unit),
877 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000878 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879}
880
Anders Carlssona031b352009-11-06 19:19:55 +0000881llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
882 llvm::DICompileUnit Unit) {
883 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
884 Ty, Ty->getPointeeType(), Unit);
885}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000886
Anders Carlsson20f12a22009-12-06 18:00:51 +0000887llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
888 llvm::DICompileUnit U) {
889 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
890 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
891
892 if (!Ty->getPointeeType()->isFunctionType()) {
893 // We have a data member pointer type.
894 return PointerDiffDITy;
895 }
896
897 // We have a member function pointer type. Treat it as a struct with two
898 // ptrdiff_t members.
899 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
900
901 uint64_t FieldOffset = 0;
902 llvm::DIDescriptor ElementTypes[2];
903
904 // FIXME: This should probably be a function type instead.
905 ElementTypes[0] =
906 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
907 "ptr", llvm::DICompileUnit(), 0,
908 Info.first, Info.second, FieldOffset, 0,
909 PointerDiffDITy);
910 FieldOffset += Info.first;
911
912 ElementTypes[1] =
913 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
914 "ptr", llvm::DICompileUnit(), 0,
915 Info.first, Info.second, FieldOffset, 0,
916 PointerDiffDITy);
917
918 llvm::DIArray Elements =
919 DebugFactory.GetOrCreateArray(&ElementTypes[0],
920 llvm::array_lengthof(ElementTypes));
921
922 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
923 U, llvm::StringRef("test"),
924 llvm::DICompileUnit(), 0, FieldOffset,
925 0, 0, 0, llvm::DIType(), Elements);
926}
927
Douglas Gregor840943d2009-12-21 20:18:30 +0000928static QualType UnwrapTypeForDebugInfo(QualType T) {
929 do {
930 QualType LastT = T;
931 switch (T->getTypeClass()) {
932 default:
933 return T;
934 case Type::TemplateSpecialization:
935 T = cast<TemplateSpecializationType>(T)->desugar();
936 break;
937 case Type::TypeOfExpr: {
938 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
939 T = Ty->getUnderlyingExpr()->getType();
940 break;
941 }
942 case Type::TypeOf:
943 T = cast<TypeOfType>(T)->getUnderlyingType();
944 break;
945 case Type::Decltype:
946 T = cast<DecltypeType>(T)->getUnderlyingType();
947 break;
948 case Type::QualifiedName:
949 T = cast<QualifiedNameType>(T)->getNamedType();
950 break;
951 case Type::SubstTemplateTypeParm:
952 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
953 break;
954 case Type::Elaborated:
955 T = cast<ElaboratedType>(T)->getUnderlyingType();
956 break;
957 }
958
959 assert(T != LastT && "Type unwrapping failed to unwrap!");
960 if (T == LastT)
961 return T;
962 } while (true);
963
964 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000965}
966
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000967/// getOrCreateType - Get the type from the cache or create a new
968/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000969llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
970 llvm::DICompileUnit Unit) {
971 if (Ty.isNull())
972 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregor840943d2009-12-21 20:18:30 +0000974 // Unwrap the type as needed for debug information.
975 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000976
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000977 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000978 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000979 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000980 if (it != TypeCache.end()) {
981 // Verify that the debug info still exists.
982 if (&*it->second)
983 return llvm::DIType(cast<llvm::MDNode>(it->second));
984 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000985
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000986 // Otherwise create the type.
987 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000988
989 // And update the type cache.
990 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000991 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000992}
993
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000994/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000995llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
996 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000997 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000998 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000999 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001000
Douglas Gregor2101a822009-12-21 19:57:21 +00001001 const char *Diag = 0;
1002
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001003 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001005#define TYPE(Class, Base)
1006#define ABSTRACT_TYPE(Class, Base)
1007#define NON_CANONICAL_TYPE(Class, Base)
1008#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1009#include "clang/AST/TypeNodes.def"
1010 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001011
Anders Carlssonbfe69952009-11-06 18:24:04 +00001012 // FIXME: Handle these.
1013 case Type::ExtVector:
1014 case Type::Vector:
1015 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001016
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001017 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001018 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001019 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001020 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1021 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1022 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1023 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001024 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001025 return CreateType(cast<BlockPointerType>(Ty), Unit);
1026 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001027 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001028 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001029 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001030 case Type::FunctionProto:
1031 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001032 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001033 case Type::ConstantArray:
1034 case Type::VariableArray:
1035 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001036 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001037
1038 case Type::LValueReference:
1039 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1040
Anders Carlsson20f12a22009-12-06 18:00:51 +00001041 case Type::MemberPointer:
1042 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001043
1044 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001045 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001046 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001047 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001048 case Type::TypeOfExpr:
1049 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001050 case Type::Decltype:
1051 llvm_unreachable("type should have been unwrapped!");
1052 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001053
1054 case Type::RValueReference:
1055 // FIXME: Implement!
1056 Diag = "rvalue references";
1057 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001058 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001059
1060 assert(Diag && "Fall through without a diagnostic?");
1061 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1062 "debug information for %0 is not yet supported");
1063 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1064 << Diag;
1065 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001066}
1067
1068/// EmitFunctionStart - Constructs the debug code for entering a function -
1069/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001070void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001071 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Devang Patel9c6c3a02010-01-14 00:36:21 +00001074 llvm::StringRef Name;
1075 llvm::StringRef LinkageName;
1076
1077 const Decl *D = GD.getDecl();
1078 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001079 // If there is a DISubprogram for this function available then use it.
1080 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1081 FI = SPCache.find(FD);
1082 if (FI != SPCache.end()) {
1083 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1084 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1085 RegionStack.push_back(SP.getNode());
1086 return;
1087 }
1088 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001089 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001090 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001091 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001092 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001093 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001094 } else {
1095 // Use llvm function name as linkage name.
1096 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001097 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001098 if (!Name.empty() && Name[0] == '\01')
1099 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Devang Patel98a200b2010-01-14 18:06:13 +00001102 // It is expected that CurLoc is set before using EmitFunctionStart.
1103 // Usually, CurLoc points to the left bracket location of compound
1104 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001105 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001106 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001107 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Chris Lattner9c85ba32008-11-10 06:08:34 +00001109 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001110 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001111 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001112 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001114 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001115 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001116}
1117
1118
Chris Lattner9c85ba32008-11-10 06:08:34 +00001119void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001120 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001122 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001123 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001124 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001125 || (SM.getInstantiationLineNumber(CurLoc) ==
1126 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001127 && SM.isFromSameFile(CurLoc, PrevLoc)))
1128 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001129
1130 // Update last state.
1131 PrevLoc = CurLoc;
1132
1133 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001134 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001135 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001136
Devang Patel8fae0602009-11-13 19:10:24 +00001137 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001138 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1139 llvm::DILocation DO(NULL);
1140 llvm::DILocation DL =
1141 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1142 DS, DO);
1143 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001144}
1145
1146/// EmitRegionStart- Constructs the debug code for entering a declarative
1147/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001148void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001149 llvm::DIDescriptor D =
1150 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1151 llvm::DIDescriptor() :
1152 llvm::DIDescriptor(RegionStack.back()));
1153 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001154}
1155
1156/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1157/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001159 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1160
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001161 // Provide an region stop point.
1162 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001164 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001165}
1166
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001167/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001168void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1169 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001170 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1171
Devang Patel07739032009-03-27 23:16:32 +00001172 // Do not emit variable debug information while generating optimized code.
1173 // The llvm optimizer and code generator are not yet ready to support
1174 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001175 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001176 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001177 return;
1178
Chris Lattner650cea92009-05-05 04:57:08 +00001179 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001180 QualType Type = Decl->getType();
1181 llvm::DIType Ty = getOrCreateType(Type, Unit);
1182 if (Decl->hasAttr<BlocksAttr>()) {
1183 llvm::DICompileUnit DefUnit;
1184 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1185
1186 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1187
1188 llvm::DIType FieldTy;
1189
1190 QualType FType;
1191 uint64_t FieldSize, FieldOffset;
1192 unsigned FieldAlign;
1193
1194 llvm::DIArray Elements;
1195 llvm::DIType EltTy;
1196
1197 // Build up structure for the byref. See BuildByRefType.
1198 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001199 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001200 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001201 FieldSize = CGM.getContext().getTypeSize(FType);
1202 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001203 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1204 "__isa", DefUnit,
1205 0, FieldSize, FieldAlign,
1206 FieldOffset, 0, FieldTy);
1207 EltTys.push_back(FieldTy);
1208 FieldOffset += FieldSize;
1209
Anders Carlsson20f12a22009-12-06 18:00:51 +00001210 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001211 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001212 FieldSize = CGM.getContext().getTypeSize(FType);
1213 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001214 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1215 "__forwarding", DefUnit,
1216 0, FieldSize, FieldAlign,
1217 FieldOffset, 0, FieldTy);
1218 EltTys.push_back(FieldTy);
1219 FieldOffset += FieldSize;
1220
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001221 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001222 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001223 FieldSize = CGM.getContext().getTypeSize(FType);
1224 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001225 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1226 "__flags", DefUnit,
1227 0, FieldSize, FieldAlign,
1228 FieldOffset, 0, FieldTy);
1229 EltTys.push_back(FieldTy);
1230 FieldOffset += FieldSize;
1231
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001232 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001233 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001234 FieldSize = CGM.getContext().getTypeSize(FType);
1235 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001236 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1237 "__size", DefUnit,
1238 0, FieldSize, FieldAlign,
1239 FieldOffset, 0, FieldTy);
1240 EltTys.push_back(FieldTy);
1241 FieldOffset += FieldSize;
1242
Anders Carlsson20f12a22009-12-06 18:00:51 +00001243 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001244 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001245 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001246 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001247 FieldSize = CGM.getContext().getTypeSize(FType);
1248 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001249 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1250 "__copy_helper", DefUnit,
1251 0, FieldSize, FieldAlign,
1252 FieldOffset, 0, FieldTy);
1253 EltTys.push_back(FieldTy);
1254 FieldOffset += FieldSize;
1255
Anders Carlsson20f12a22009-12-06 18:00:51 +00001256 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001257 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001258 FieldSize = CGM.getContext().getTypeSize(FType);
1259 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001260 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1261 "__destroy_helper", DefUnit,
1262 0, FieldSize, FieldAlign,
1263 FieldOffset, 0, FieldTy);
1264 EltTys.push_back(FieldTy);
1265 FieldOffset += FieldSize;
1266 }
1267
Anders Carlsson20f12a22009-12-06 18:00:51 +00001268 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1269 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001270 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001271 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001272 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001273 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001274
1275 if (NumPaddingBytes > 0) {
1276 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001277 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001278 pad, ArrayType::Normal, 0);
1279 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001280 FieldSize = CGM.getContext().getTypeSize(FType);
1281 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001282 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1283 Unit, "", DefUnit,
1284 0, FieldSize, FieldAlign,
1285 FieldOffset, 0, FieldTy);
1286 EltTys.push_back(FieldTy);
1287 FieldOffset += FieldSize;
1288 }
1289 }
1290
1291 FType = Type;
1292 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001294 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001295
1296 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001297 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001298 0, FieldSize, FieldAlign,
1299 FieldOffset, 0, FieldTy);
1300 EltTys.push_back(FieldTy);
1301 FieldOffset += FieldSize;
1302
1303 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1304
1305 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1306
1307 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1308 llvm::DICompileUnit(),
1309 0, FieldOffset, 0, 0, Flags,
1310 llvm::DIType(), Elements);
1311 }
Chris Lattner650cea92009-05-05 04:57:08 +00001312
Chris Lattner9c85ba32008-11-10 06:08:34 +00001313 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001314 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001315 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001316 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001317 unsigned Column = 0;
1318 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001319 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001320 Column = PLoc.getColumn();
1321 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001322 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Chris Lattner9c85ba32008-11-10 06:08:34 +00001325 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001326 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001327 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001328 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001329 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001330 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001331 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001332 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001333
Devang Patel8fae0602009-11-13 19:10:24 +00001334 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001335 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001336 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1337
Chris Lattner23e92c02009-12-28 23:41:39 +00001338 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001339}
1340
Mike Stumpb1a6e682009-09-30 02:43:10 +00001341/// EmitDeclare - Emit local variable declaration debug info.
1342void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1343 llvm::Value *Storage, CGBuilderTy &Builder,
1344 CodeGenFunction *CGF) {
1345 const ValueDecl *Decl = BDRE->getDecl();
1346 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1347
1348 // Do not emit variable debug information while generating optimized code.
1349 // The llvm optimizer and code generator are not yet ready to support
1350 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001351 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001352 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001353 return;
1354
1355 uint64_t XOffset = 0;
1356 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1357 QualType Type = Decl->getType();
1358 llvm::DIType Ty = getOrCreateType(Type, Unit);
1359 if (Decl->hasAttr<BlocksAttr>()) {
1360 llvm::DICompileUnit DefUnit;
1361 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1362
1363 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1364
1365 llvm::DIType FieldTy;
1366
1367 QualType FType;
1368 uint64_t FieldSize, FieldOffset;
1369 unsigned FieldAlign;
1370
1371 llvm::DIArray Elements;
1372 llvm::DIType EltTy;
1373
1374 // Build up structure for the byref. See BuildByRefType.
1375 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001376 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001377 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001378 FieldSize = CGM.getContext().getTypeSize(FType);
1379 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001380 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1381 "__isa", DefUnit,
1382 0, FieldSize, FieldAlign,
1383 FieldOffset, 0, FieldTy);
1384 EltTys.push_back(FieldTy);
1385 FieldOffset += FieldSize;
1386
Anders Carlsson20f12a22009-12-06 18:00:51 +00001387 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001388 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001389 FieldSize = CGM.getContext().getTypeSize(FType);
1390 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001391 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1392 "__forwarding", DefUnit,
1393 0, FieldSize, FieldAlign,
1394 FieldOffset, 0, FieldTy);
1395 EltTys.push_back(FieldTy);
1396 FieldOffset += FieldSize;
1397
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001398 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001399 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001400 FieldSize = CGM.getContext().getTypeSize(FType);
1401 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001402 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1403 "__flags", DefUnit,
1404 0, FieldSize, FieldAlign,
1405 FieldOffset, 0, FieldTy);
1406 EltTys.push_back(FieldTy);
1407 FieldOffset += FieldSize;
1408
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001409 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001410 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001411 FieldSize = CGM.getContext().getTypeSize(FType);
1412 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001413 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1414 "__size", DefUnit,
1415 0, FieldSize, FieldAlign,
1416 FieldOffset, 0, FieldTy);
1417 EltTys.push_back(FieldTy);
1418 FieldOffset += FieldSize;
1419
Anders Carlsson20f12a22009-12-06 18:00:51 +00001420 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001421 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001422 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001423 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001424 FieldSize = CGM.getContext().getTypeSize(FType);
1425 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001426 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1427 "__copy_helper", DefUnit,
1428 0, FieldSize, FieldAlign,
1429 FieldOffset, 0, FieldTy);
1430 EltTys.push_back(FieldTy);
1431 FieldOffset += FieldSize;
1432
Anders Carlsson20f12a22009-12-06 18:00:51 +00001433 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001434 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001435 FieldSize = CGM.getContext().getTypeSize(FType);
1436 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001437 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1438 "__destroy_helper", DefUnit,
1439 0, FieldSize, FieldAlign,
1440 FieldOffset, 0, FieldTy);
1441 EltTys.push_back(FieldTy);
1442 FieldOffset += FieldSize;
1443 }
1444
Anders Carlsson20f12a22009-12-06 18:00:51 +00001445 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1446 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001447 unsigned AlignedOffsetInBytes
1448 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1449 unsigned NumPaddingBytes
1450 = AlignedOffsetInBytes - FieldOffset/8;
1451
1452 if (NumPaddingBytes > 0) {
1453 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001454 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001455 pad, ArrayType::Normal, 0);
1456 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001457 FieldSize = CGM.getContext().getTypeSize(FType);
1458 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001459 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1460 Unit, "", DefUnit,
1461 0, FieldSize, FieldAlign,
1462 FieldOffset, 0, FieldTy);
1463 EltTys.push_back(FieldTy);
1464 FieldOffset += FieldSize;
1465 }
1466 }
1467
1468 FType = Type;
1469 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001470 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001471 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001472
1473 XOffset = FieldOffset;
1474 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001475 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001476 0, FieldSize, FieldAlign,
1477 FieldOffset, 0, FieldTy);
1478 EltTys.push_back(FieldTy);
1479 FieldOffset += FieldSize;
1480
1481 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1482
1483 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1484
1485 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1486 llvm::DICompileUnit(),
1487 0, FieldOffset, 0, 0, Flags,
1488 llvm::DIType(), Elements);
1489 }
1490
1491 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001492 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001493 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1494 unsigned Line = 0;
1495 if (!PLoc.isInvalid())
1496 Line = PLoc.getLine();
1497 else
1498 Unit = llvm::DICompileUnit();
1499
Ken Dyck199c3d62010-01-11 17:06:35 +00001500 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001501 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001502 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001503 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1504 llvm::DIFactory::OpDeref));
1505 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1506 llvm::DIFactory::OpPlus));
1507 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001508 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001509 if (BDRE->isByRef()) {
1510 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1511 llvm::DIFactory::OpDeref));
1512 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1513 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001514 // offset of __forwarding field
1515 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001516 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001517 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001518 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1519 llvm::DIFactory::OpDeref));
1520 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1521 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001522 // offset of x field
1523 offset = CharUnits::fromQuantity(XOffset/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001524 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001525 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001526 }
1527
1528 // Create the descriptor for the variable.
1529 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001530 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001531 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001532 addr);
1533 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001534 llvm::Instruction *Call =
1535 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001536
Devang Patel8fae0602009-11-13 19:10:24 +00001537 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001538 llvm::DILocation DO(NULL);
1539 llvm::DILocation DL =
1540 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001541
Chris Lattner23e92c02009-12-28 23:41:39 +00001542 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001543}
1544
Chris Lattner9c85ba32008-11-10 06:08:34 +00001545void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1546 llvm::Value *Storage,
1547 CGBuilderTy &Builder) {
1548 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1549}
1550
Mike Stumpb1a6e682009-09-30 02:43:10 +00001551void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1552 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1553 CodeGenFunction *CGF) {
1554 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1555}
1556
Chris Lattner9c85ba32008-11-10 06:08:34 +00001557/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1558/// variable declaration.
1559void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1560 CGBuilderTy &Builder) {
1561 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1562}
1563
1564
1565
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001566/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001567void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001568 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001569
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001570 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001571 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001572 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001573 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1574 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001575
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001576 QualType T = Decl->getType();
1577 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001579 // CodeGen turns int[] into int[1] so we'll do the same here.
1580 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001582 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001583 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Anders Carlsson20f12a22009-12-06 18:00:51 +00001585 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001586 ArrayType::Normal, 0);
1587 }
Devang Patel73621622009-11-25 17:37:31 +00001588 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001589 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001590 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001591 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001592 Var->hasInternalLinkage(),
1593 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001594}
1595
Devang Patel9ca36b62009-02-26 21:10:26 +00001596/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001597void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001598 ObjCInterfaceDecl *Decl) {
1599 // Create global variable debug descriptor.
1600 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001601 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001602 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1603 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001604
Devang Patel73621622009-11-25 17:37:31 +00001605 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001606
Anders Carlsson20f12a22009-12-06 18:00:51 +00001607 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001608 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Devang Patel9ca36b62009-02-26 21:10:26 +00001610 // CodeGen turns int[] into int[1] so we'll do the same here.
1611 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Devang Patel9ca36b62009-02-26 21:10:26 +00001613 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001614 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Anders Carlsson20f12a22009-12-06 18:00:51 +00001616 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001617 ArrayType::Normal, 0);
1618 }
1619
Devang Patelf6a39b72009-10-20 18:26:30 +00001620 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001621 getOrCreateType(T, Unit),
1622 Var->hasInternalLinkage(),
1623 true/*definition*/, Var);
1624}