blob: 14f34a7c7295093af7ff301c01f7536f355492bc [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.
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000081 char *StrPtr = FunctionNames.Allocate<char>(NS.length());
82 memcpy(StrPtr, NS.data(), NS.length());
83 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000084}
85
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000086/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000087/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000088llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000089 // Get source file information.
90 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000091 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000092 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000093 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000094 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
95 FileName = PLoc.getFilename();
96 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000099 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +0000100 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +0000101 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000102
Devang Patel446c6192009-04-17 21:06:59 +0000103 // Get absolute path name.
104 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000105 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000106
Devang Patel72240d72009-06-26 18:32:22 +0000107 // See if thie compile unit is representing main source file. Each source
108 // file has corresponding compile unit. There is only one main source
109 // file at a time.
110 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000111 const LangOptions &LO = CGM.getLangOptions();
112 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000113 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000114 if (!CGO.MainFileName.empty()) {
115 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000116 isMain = true;
117 } else {
118 if (Loc.isValid() && SM.isFromMainFile(Loc))
119 isMain = true;
120 }
121 if (isMain)
122 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000123 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000124
Chris Lattner515455a2009-03-25 03:28:08 +0000125 unsigned LangTag;
126 if (LO.CPlusPlus) {
127 if (LO.ObjC1)
128 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
129 else
130 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
131 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000132 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000133 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000134 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000135 } else {
136 LangTag = llvm::dwarf::DW_LANG_C89;
137 }
Devang Patel446c6192009-04-17 21:06:59 +0000138
Benjamin Kramer47daf682009-12-08 11:02:29 +0000139 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000140#ifdef CLANG_VENDOR
141 CLANG_VENDOR
142#endif
143 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000144
145 // Figure out which version of the ObjC runtime we have.
146 unsigned RuntimeVers = 0;
147 if (LO.ObjC1)
148 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000150 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000151 return Unit = DebugFactory.CreateCompileUnit(
152 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
153 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000154}
155
Devang Patel65e99f22009-02-25 01:36:11 +0000156/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000157/// one if necessary.
158llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000159 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000160 unsigned Encoding = 0;
161 switch (BT->getKind()) {
162 default:
163 case BuiltinType::Void:
164 return llvm::DIType();
165 case BuiltinType::UChar:
166 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
167 case BuiltinType::Char_S:
168 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
169 case BuiltinType::UShort:
170 case BuiltinType::UInt:
171 case BuiltinType::ULong:
172 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
173 case BuiltinType::Short:
174 case BuiltinType::Int:
175 case BuiltinType::Long:
176 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
177 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
178 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000179 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000180 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000181 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000183 uint64_t Size = CGM.getContext().getTypeSize(BT);
184 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000185 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Devang Patelca80a5f2009-10-20 19:55:01 +0000187 llvm::DIType DbgTy =
188 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000189 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000190 Unit, 0, Size, Align,
191 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000193}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000194
Chris Lattnerb7003772009-04-23 06:13:01 +0000195llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
196 llvm::DICompileUnit Unit) {
197 // Bit size, align and offset of the type.
198 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
199 if (Ty->isComplexIntegerType())
200 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Anders Carlsson20f12a22009-12-06 18:00:51 +0000202 uint64_t Size = CGM.getContext().getTypeSize(Ty);
203 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000204 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Devang Patelca80a5f2009-10-20 19:55:01 +0000206 llvm::DIType DbgTy =
207 DebugFactory.CreateBasicType(Unit, "complex",
208 Unit, 0, Size, Align,
209 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000210 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000211}
212
John McCalla1805292009-09-25 01:40:47 +0000213/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000214/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000215llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
216 QualifierCollector Qc;
217 const Type *T = Qc.strip(Ty);
218
219 // Ignore these qualifiers for now.
220 Qc.removeObjCGCAttr();
221 Qc.removeAddressSpace();
222
Chris Lattner9c85ba32008-11-10 06:08:34 +0000223 // We will create one Derived type for one qualifier and recurse to handle any
224 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000226 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000228 Qc.removeConst();
229 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000231 Qc.removeVolatile();
232 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000234 Qc.removeRestrict();
235 } else {
236 assert(Qc.empty() && "Unknown type qualifier for debug info");
237 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
John McCalla1805292009-09-25 01:40:47 +0000240 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
241
Daniel Dunbar3845f862008-10-31 03:54:29 +0000242 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
243 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000244 llvm::DIType DbgTy =
245 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
246 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000247 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000248}
249
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000250llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
251 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000252 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000253 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
254 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000255 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000256}
257
Chris Lattner9c85ba32008-11-10 06:08:34 +0000258llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
259 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000260 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
261 Ty->getPointeeType(), Unit);
262}
263
264llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
265 const Type *Ty,
266 QualType PointeeTy,
267 llvm::DICompileUnit Unit) {
268 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000270 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000271
272 // Size is always the size of a pointer. We can't use getTypeSize here
273 // because that does not return the correct value for references.
274 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000275 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
276 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Devang Patelca80a5f2009-10-20 19:55:01 +0000278 return
Anders Carlssona031b352009-11-06 19:19:55 +0000279 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000280 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000281
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000282}
283
Mike Stump9bc093c2009-05-14 02:03:51 +0000284llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
285 llvm::DICompileUnit Unit) {
286 if (BlockLiteralGenericSet)
287 return BlockLiteralGeneric;
288
289 llvm::DICompileUnit DefUnit;
290 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
291
292 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
293
294 llvm::DIType FieldTy;
295
296 QualType FType;
297 uint64_t FieldSize, FieldOffset;
298 unsigned FieldAlign;
299
300 llvm::DIArray Elements;
301 llvm::DIType EltTy, DescTy;
302
303 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000304 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000306 FieldSize = CGM.getContext().getTypeSize(FType);
307 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000308 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
309 "reserved", DefUnit,
310 0, FieldSize, FieldAlign,
311 FieldOffset, 0, FieldTy);
312 EltTys.push_back(FieldTy);
313
314 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000315 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000316 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000317 FieldSize = CGM.getContext().getTypeSize(FType);
318 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000319 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
320 "Size", DefUnit,
321 0, FieldSize, FieldAlign,
322 FieldOffset, 0, FieldTy);
323 EltTys.push_back(FieldTy);
324
325 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000326 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 EltTys.clear();
328
Mike Stump3d363c52009-10-02 02:30:50 +0000329 unsigned Flags = llvm::DIType::FlagAppleBlock;
330
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000332 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(Ty);
337 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Mike Stump9bc093c2009-05-14 02:03:51 +0000339 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
340 Unit, "", llvm::DICompileUnit(),
341 0, Size, Align, 0, 0, EltTy);
342
343 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000345 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 FieldSize = CGM.getContext().getTypeSize(FType);
347 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000348 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
349 "__isa", DefUnit,
350 0, FieldSize, FieldAlign,
351 FieldOffset, 0, FieldTy);
352 EltTys.push_back(FieldTy);
353
354 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000357 FieldSize = CGM.getContext().getTypeSize(FType);
358 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000359 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
360 "__flags", DefUnit,
361 0, FieldSize, FieldAlign,
362 FieldOffset, 0, FieldTy);
363 EltTys.push_back(FieldTy);
364
365 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000366 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000368 FieldSize = CGM.getContext().getTypeSize(FType);
369 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000370 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
371 "__reserved", DefUnit,
372 0, FieldSize, FieldAlign,
373 FieldOffset, 0, FieldTy);
374 EltTys.push_back(FieldTy);
375
376 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000377 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FieldSize = CGM.getContext().getTypeSize(FType);
380 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000381 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
382 "__FuncPtr", DefUnit,
383 0, FieldSize, FieldAlign,
384 FieldOffset, 0, FieldTy);
385 EltTys.push_back(FieldTy);
386
387 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000388 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000390 FieldSize = CGM.getContext().getTypeSize(Ty);
391 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000392 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
393 "__descriptor", DefUnit,
394 0, FieldSize, FieldAlign,
395 FieldOffset, 0, FieldTy);
396 EltTys.push_back(FieldTy);
397
398 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000399 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000400
401 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000402 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000403 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Mike Stump9bc093c2009-05-14 02:03:51 +0000405 BlockLiteralGenericSet = true;
406 BlockLiteralGeneric
407 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
408 "", llvm::DICompileUnit(),
409 0, Size, Align, 0, 0, EltTy);
410 return BlockLiteralGeneric;
411}
412
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
414 llvm::DICompileUnit Unit) {
415 // Typedefs are derived from some other type. If we have a typedef of a
416 // typedef, make sure to emit the whole chain.
417 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419 // We don't set size information, but do specify where the typedef was
420 // declared.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421 SourceLocation DefLoc = Ty->getDecl()->getLocation();
422 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000423
Anders Carlsson20f12a22009-12-06 18:00:51 +0000424 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000425 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
426 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000427
Devang Patelca80a5f2009-10-20 19:55:01 +0000428 llvm::DIType DbgTy =
429 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
Devang Patel73621622009-11-25 17:37:31 +0000430 Ty->getDecl()->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000431 DefUnit, Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000432 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000433}
434
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
436 llvm::DICompileUnit Unit) {
437 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000438
Chris Lattner9c85ba32008-11-10 06:08:34 +0000439 // Add the result type at least.
440 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattner9c85ba32008-11-10 06:08:34 +0000442 // Set up remainder of arguments if there is a prototype.
443 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000444 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
446 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
447 } else {
448 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000449 }
450
Chris Lattner9c85ba32008-11-10 06:08:34 +0000451 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000452 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Devang Patelca80a5f2009-10-20 19:55:01 +0000454 llvm::DIType DbgTy =
455 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
456 Unit, "", llvm::DICompileUnit(),
457 0, 0, 0, 0, 0,
458 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000459 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000460}
461
Devang Patel428deb52010-01-19 00:00:59 +0000462/// CollectRecordFields - A helper function to collect debug info for
463/// record fields. This is used while creating debug info entry for a Record.
464void CGDebugInfo::
465CollectRecordFields(const RecordDecl *Decl,
466 llvm::DICompileUnit Unit,
467 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
468 unsigned FieldNo = 0;
469 SourceManager &SM = CGM.getContext().getSourceManager();
470 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
471 for (RecordDecl::field_iterator I = Decl->field_begin(),
472 E = Decl->field_end();
473 I != E; ++I, ++FieldNo) {
474 FieldDecl *Field = *I;
475 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
476
477 llvm::StringRef FieldName = Field->getName();
478
479 // Ignore unnamed fields.
480 if (FieldName.empty())
481 continue;
482
483 // Get the location for the field.
484 SourceLocation FieldDefLoc = Field->getLocation();
485 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
486 llvm::DICompileUnit FieldDefUnit;
487 unsigned FieldLine = 0;
488
489 if (!PLoc.isInvalid()) {
490 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
491 FieldLine = PLoc.getLine();
492 }
493
494 QualType FType = Field->getType();
495 uint64_t FieldSize = 0;
496 unsigned FieldAlign = 0;
497 if (!FType->isIncompleteArrayType()) {
498
499 // Bit size, align and offset of the type.
500 FieldSize = CGM.getContext().getTypeSize(FType);
501 Expr *BitWidth = Field->getBitWidth();
502 if (BitWidth)
503 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
504
505 FieldAlign = CGM.getContext().getTypeAlign(FType);
506 }
507
508 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
509
510 // Create a DW_TAG_member node to remember the offset of this field in the
511 // struct. FIXME: This is an absolutely insane way to capture this
512 // information. When we gut debug info, this should be fixed.
513 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
514 FieldName, FieldDefUnit,
515 FieldLine, FieldSize, FieldAlign,
516 FieldOffset, 0, FieldTy);
517 EltTys.push_back(FieldTy);
518 }
519}
520
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000521/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
522/// a single member function GlobalDecl.
523llvm::DISubprogram
524CGDebugInfo::CreateCXXMemberFunction(GlobalDecl GD,
525 llvm::DICompileUnit Unit,
526 llvm::DICompositeType &RecordTy) {
527 const CXXMethodDecl *Method = cast<CXXMethodDecl>(GD.getDecl());
528
529 llvm::StringRef MethodName;
530 llvm::StringRef MethodLinkageName;
531 llvm::DIType MethodTy = getOrCreateType(Method->getType(), Unit);
532 if (const CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(Method)) {
533 (void)CDecl;
534 MethodName = Method->getName();
535 // FIXME : Find linkage name.
536 } else if (const CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(Method)) {
537 (void)DDecl;
538 MethodName = getFunctionName(Method);
539 // FIXME : Find linkage name.
540 } else {
541 // regular method
542 MethodName = getFunctionName(Method);
543 MethodLinkageName = CGM.getMangledName(Method);
544 }
545 SourceManager &SM = CGM.getContext().getSourceManager();
546
547 // Get the location for the method.
548 SourceLocation MethodDefLoc = Method->getLocation();
549 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
550 llvm::DICompileUnit MethodDefUnit;
551 unsigned MethodLine = 0;
552
553 if (!PLoc.isInvalid()) {
554 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
555 MethodLine = PLoc.getLine();
556 }
557
558 // Collect virtual method info.
559 llvm::DIType ContainingType;
560 unsigned Virtuality = 0;
561 unsigned VIndex = 0;
562 if (Method->isVirtual()) {
563 // FIXME: Identify pure virtual functions.
564 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
565 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
566 ContainingType = RecordTy;
567 }
568
569 llvm::DISubprogram SP =
570 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
571 MethodLinkageName,
572 MethodDefUnit, MethodLine,
573 MethodTy, /*isLocalToUnit=*/false,
574 Method->isThisDeclarationADefinition(),
575 Virtuality, VIndex, ContainingType);
576 if (Method->isThisDeclarationADefinition())
577 SPCache[cast<FunctionDecl>(Method)] = llvm::WeakVH(SP.getNode());
578
579 return SP;
580}
581
Devang Patel4125fd22010-01-19 01:54:44 +0000582/// CollectCXXMemberFunctions - A helper function to collect debug info for
583/// C++ member functions.This is used while creating debug info entry for
584/// a Record.
585void CGDebugInfo::
586CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
587 llvm::DICompileUnit Unit,
588 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
589 llvm::DICompositeType &RecordTy) {
Devang Patel4125fd22010-01-19 01:54:44 +0000590 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
591 E = Decl->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000592 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000593
594 if (Method->isImplicit())
595 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000596
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000597 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000598 }
599}
600
Devang Patela245c5b2010-01-25 23:32:18 +0000601/// CollectCXXBases - A helper function to collect debug info for
602/// C++ base classes. This is used while creating debug info entry for
603/// a Record.
604void CGDebugInfo::
605CollectCXXBases(const CXXRecordDecl *Decl,
606 llvm::DICompileUnit Unit,
607 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
608 llvm::DICompositeType &RecordTy) {
609
610 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
611 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
612 BE = Decl->bases_end(); BI != BE; ++BI) {
613 unsigned BFlags = 0;
614 if (BI->isVirtual())
615 BFlags = llvm::DIType::FlagVirtual;
616 AccessSpecifier Access = BI->getAccessSpecifier();
617 if (Access == clang::AS_private)
618 BFlags |= llvm::DIType::FlagPrivate;
619 else if (Access == clang::AS_protected)
620 BFlags |= llvm::DIType::FlagProtected;
621
622 const CXXRecordDecl *Base =
623 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
624 llvm::DIType DTy =
625 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
626 RecordTy, llvm::StringRef(),
627 llvm::DICompileUnit(), 0, 0, 0,
628 RL.getBaseClassOffset(Base), BFlags,
629 getOrCreateType(BI->getType(),
630 Unit));
631 EltTys.push_back(DTy);
632 }
633}
634
Devang Patel65e99f22009-02-25 01:36:11 +0000635/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000636llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
637 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000638 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattner9c85ba32008-11-10 06:08:34 +0000640 unsigned Tag;
641 if (Decl->isStruct())
642 Tag = llvm::dwarf::DW_TAG_structure_type;
643 else if (Decl->isUnion())
644 Tag = llvm::dwarf::DW_TAG_union_type;
645 else {
646 assert(Decl->isClass() && "Unknown RecordType!");
647 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000648 }
649
Anders Carlsson20f12a22009-12-06 18:00:51 +0000650 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000651
Chris Lattner9c85ba32008-11-10 06:08:34 +0000652 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000653 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000654 llvm::DICompileUnit DefUnit;
655 unsigned Line = 0;
656 if (!PLoc.isInvalid()) {
657 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
658 Line = PLoc.getLine();
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Chris Lattner9c85ba32008-11-10 06:08:34 +0000661 // Records and classes and unions can all be recursive. To handle them, we
662 // first generate a debug descriptor for the struct as a forward declaration.
663 // Then (if it is a definition) we go through and get debug info for all of
664 // its members. Finally, we create a descriptor for the complete type (which
665 // may refer to the forward decl if the struct is recursive) and replace all
666 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000667
668 // A Decl->getName() is not unique. However, the debug info descriptors
669 // are uniqued. The debug info descriptor describing record's context is
670 // necessary to keep two Decl's descriptor unique if their name match.
671 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
672 // use type's name in FwdDecl.
673 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000674 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000675 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000676 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000677 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattner9c85ba32008-11-10 06:08:34 +0000679 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000680 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000681 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000682
Eli Friedman14d63652009-11-16 21:04:30 +0000683 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000684 // Otherwise, insert it into the TypeCache so that recursive uses will find
685 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000686 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000687
688 // Convert all the elements.
689 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
690
Devang Patel428deb52010-01-19 00:00:59 +0000691 CollectRecordFields(Decl, Unit, EltTys);
Devang Patela245c5b2010-01-25 23:32:18 +0000692 if (CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl)) {
Devang Patel4125fd22010-01-19 01:54:44 +0000693 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patela245c5b2010-01-25 23:32:18 +0000694 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattner9c85ba32008-11-10 06:08:34 +0000697 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000698 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000699
700 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000701 uint64_t Size = CGM.getContext().getTypeSize(Ty);
702 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Devang Patel0ce73f62009-07-22 18:57:00 +0000704 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000705 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000706 DefUnit, Line, Size, Align, 0, 0,
707 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000708
709 // Now that we have a real decl for the struct, replace anything using the
710 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000711 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000712
Chris Lattner9c85ba32008-11-10 06:08:34 +0000713 return RealDecl;
714}
715
Devang Patel9ca36b62009-02-26 21:10:26 +0000716/// CreateType - get objective-c interface type.
717llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
718 llvm::DICompileUnit Unit) {
719 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Devang Patel9ca36b62009-02-26 21:10:26 +0000721 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000722 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000723
724 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000725 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000726 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
727 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
728
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Daniel Dunbard86d3362009-05-18 20:51:58 +0000730 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000731
Devang Patel9ca36b62009-02-26 21:10:26 +0000732 // To handle recursive interface, we
733 // first generate a debug descriptor for the struct as a forward declaration.
734 // Then (if it is a definition) we go through and get debug info for all of
735 // its members. Finally, we create a descriptor for the complete type (which
736 // may refer to the forward decl if the struct is recursive) and replace all
737 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000738 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000739 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000740 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000741 llvm::DIType(), llvm::DIArray(),
742 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Devang Patel9ca36b62009-02-26 21:10:26 +0000744 // If this is just a forward declaration, return it.
745 if (Decl->isForwardDecl())
746 return FwdDecl;
747
Devang Patelffffb032009-11-16 20:09:38 +0000748 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000749 // Otherwise, insert it into the TypeCache so that recursive uses will find
750 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000751 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000752
753 // Convert all the elements.
754 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
755
Devang Patelfbe899f2009-03-10 21:30:26 +0000756 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
757 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000758 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000759 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000760 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000761 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000762 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000763 0 /* offset */, 0, SClassTy);
764 EltTys.push_back(InhTag);
765 }
766
Anders Carlsson20f12a22009-12-06 18:00:51 +0000767 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000768
769 unsigned FieldNo = 0;
770 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
771 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
772 ObjCIvarDecl *Field = *I;
773 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
774
Devang Patel73621622009-11-25 17:37:31 +0000775 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000776
Devang Patelde135022009-04-27 22:40:36 +0000777 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000778 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000779 continue;
780
Devang Patel9ca36b62009-02-26 21:10:26 +0000781 // Get the location for the field.
782 SourceLocation FieldDefLoc = Field->getLocation();
783 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000784 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
785 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
786
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Devang Patel99c20eb2009-03-20 18:24:39 +0000788 QualType FType = Field->getType();
789 uint64_t FieldSize = 0;
790 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000791
Devang Patel99c20eb2009-03-20 18:24:39 +0000792 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Devang Patel99c20eb2009-03-20 18:24:39 +0000794 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000795 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000796 Expr *BitWidth = Field->getBitWidth();
797 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000798 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000799
Anders Carlsson20f12a22009-12-06 18:00:51 +0000800 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000801 }
802
Mike Stump1eb44332009-09-09 15:08:12 +0000803 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
804
Devang Patelc20482b2009-03-19 00:23:53 +0000805 unsigned Flags = 0;
806 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
807 Flags = llvm::DIType::FlagProtected;
808 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
809 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Devang Patel9ca36b62009-02-26 21:10:26 +0000811 // Create a DW_TAG_member node to remember the offset of this field in the
812 // struct. FIXME: This is an absolutely insane way to capture this
813 // information. When we gut debug info, this should be fixed.
814 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
815 FieldName, FieldDefUnit,
816 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000817 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000818 EltTys.push_back(FieldTy);
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Devang Patel9ca36b62009-02-26 21:10:26 +0000821 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000822 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000823
824 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000825 uint64_t Size = CGM.getContext().getTypeSize(Ty);
826 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Devang Patel6c1fddf2009-07-27 18:42:03 +0000828 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000829 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000830 Line, Size, Align, 0, 0, llvm::DIType(),
831 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000832
833 // Now that we have a real decl for the struct, replace anything using the
834 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000835 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000836
Devang Patel9ca36b62009-02-26 21:10:26 +0000837 return RealDecl;
838}
839
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
841 llvm::DICompileUnit Unit) {
842 EnumDecl *Decl = Ty->getDecl();
843
844 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
845
846 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000847 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000848 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000849 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000850 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000851 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner9c85ba32008-11-10 06:08:34 +0000854 // Return a CompositeType for the enum itself.
855 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000856 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000857
Chris Lattner9c85ba32008-11-10 06:08:34 +0000858 SourceLocation DefLoc = Decl->getLocation();
859 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000860 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000861 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
862 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
863
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattner9c85ba32008-11-10 06:08:34 +0000865 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000866 uint64_t Size = 0;
867 unsigned Align = 0;
868 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000869 Size = CGM.getContext().getTypeSize(Ty);
870 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Devang Patelca80a5f2009-10-20 19:55:01 +0000873 llvm::DIType DbgTy =
874 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000875 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000876 Size, Align, 0, 0,
877 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000878 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879}
880
881llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
882 llvm::DICompileUnit Unit) {
883 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
884 return CreateType(RT, Unit);
885 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
886 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattner9c85ba32008-11-10 06:08:34 +0000888 return llvm::DIType();
889}
890
891llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
892 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000893 uint64_t Size;
894 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000895
896
Nuno Lopes010d5142009-01-28 00:35:17 +0000897 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000898 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000899 Size = 0;
900 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000901 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000902 } else if (Ty->isIncompleteArrayType()) {
903 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000904 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000905 } else {
906 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000907 Size = CGM.getContext().getTypeSize(Ty);
908 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chris Lattner9c85ba32008-11-10 06:08:34 +0000911 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
912 // interior arrays, do we care? Why aren't nested arrays represented the
913 // obvious/recursive way?
914 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
915 QualType EltTy(Ty, 0);
916 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000917 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000918 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000919 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000920 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000921 // FIXME: Verify this is right for VLAs.
922 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
923 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner9c85ba32008-11-10 06:08:34 +0000926 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000927 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000928
Devang Patelca80a5f2009-10-20 19:55:01 +0000929 llvm::DIType DbgTy =
930 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
931 Unit, "", llvm::DICompileUnit(),
932 0, Size, Align, 0, 0,
933 getOrCreateType(EltTy, Unit),
934 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000935 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936}
937
Anders Carlssona031b352009-11-06 19:19:55 +0000938llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
939 llvm::DICompileUnit Unit) {
940 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
941 Ty, Ty->getPointeeType(), Unit);
942}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943
Anders Carlsson20f12a22009-12-06 18:00:51 +0000944llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
945 llvm::DICompileUnit U) {
946 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
947 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
948
949 if (!Ty->getPointeeType()->isFunctionType()) {
950 // We have a data member pointer type.
951 return PointerDiffDITy;
952 }
953
954 // We have a member function pointer type. Treat it as a struct with two
955 // ptrdiff_t members.
956 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
957
958 uint64_t FieldOffset = 0;
959 llvm::DIDescriptor ElementTypes[2];
960
961 // FIXME: This should probably be a function type instead.
962 ElementTypes[0] =
963 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
964 "ptr", llvm::DICompileUnit(), 0,
965 Info.first, Info.second, FieldOffset, 0,
966 PointerDiffDITy);
967 FieldOffset += Info.first;
968
969 ElementTypes[1] =
970 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
971 "ptr", llvm::DICompileUnit(), 0,
972 Info.first, Info.second, FieldOffset, 0,
973 PointerDiffDITy);
974
975 llvm::DIArray Elements =
976 DebugFactory.GetOrCreateArray(&ElementTypes[0],
977 llvm::array_lengthof(ElementTypes));
978
979 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
980 U, llvm::StringRef("test"),
981 llvm::DICompileUnit(), 0, FieldOffset,
982 0, 0, 0, llvm::DIType(), Elements);
983}
984
Douglas Gregor840943d2009-12-21 20:18:30 +0000985static QualType UnwrapTypeForDebugInfo(QualType T) {
986 do {
987 QualType LastT = T;
988 switch (T->getTypeClass()) {
989 default:
990 return T;
991 case Type::TemplateSpecialization:
992 T = cast<TemplateSpecializationType>(T)->desugar();
993 break;
994 case Type::TypeOfExpr: {
995 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
996 T = Ty->getUnderlyingExpr()->getType();
997 break;
998 }
999 case Type::TypeOf:
1000 T = cast<TypeOfType>(T)->getUnderlyingType();
1001 break;
1002 case Type::Decltype:
1003 T = cast<DecltypeType>(T)->getUnderlyingType();
1004 break;
1005 case Type::QualifiedName:
1006 T = cast<QualifiedNameType>(T)->getNamedType();
1007 break;
1008 case Type::SubstTemplateTypeParm:
1009 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1010 break;
1011 case Type::Elaborated:
1012 T = cast<ElaboratedType>(T)->getUnderlyingType();
1013 break;
1014 }
1015
1016 assert(T != LastT && "Type unwrapping failed to unwrap!");
1017 if (T == LastT)
1018 return T;
1019 } while (true);
1020
1021 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001022}
1023
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001024/// getOrCreateType - Get the type from the cache or create a new
1025/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001026llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1027 llvm::DICompileUnit Unit) {
1028 if (Ty.isNull())
1029 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Douglas Gregor840943d2009-12-21 20:18:30 +00001031 // Unwrap the type as needed for debug information.
1032 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001033
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001034 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001035 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001036 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001037 if (it != TypeCache.end()) {
1038 // Verify that the debug info still exists.
1039 if (&*it->second)
1040 return llvm::DIType(cast<llvm::MDNode>(it->second));
1041 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001042
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001043 // Otherwise create the type.
1044 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001045
1046 // And update the type cache.
1047 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001048 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001049}
1050
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001051/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001052llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1053 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001054 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001055 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001056 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001057
Douglas Gregor2101a822009-12-21 19:57:21 +00001058 const char *Diag = 0;
1059
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001060 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001062#define TYPE(Class, Base)
1063#define ABSTRACT_TYPE(Class, Base)
1064#define NON_CANONICAL_TYPE(Class, Base)
1065#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1066#include "clang/AST/TypeNodes.def"
1067 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001068
Anders Carlssonbfe69952009-11-06 18:24:04 +00001069 // FIXME: Handle these.
1070 case Type::ExtVector:
1071 case Type::Vector:
1072 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001073
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001074 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001075 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001076 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001077 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1078 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1079 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1080 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001081 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001082 return CreateType(cast<BlockPointerType>(Ty), Unit);
1083 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001084 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001085 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001086 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001087 case Type::FunctionProto:
1088 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001089 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 case Type::ConstantArray:
1091 case Type::VariableArray:
1092 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001093 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001094
1095 case Type::LValueReference:
1096 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1097
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098 case Type::MemberPointer:
1099 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001100
1101 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001102 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001103 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001104 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001105 case Type::TypeOfExpr:
1106 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001107 case Type::Decltype:
1108 llvm_unreachable("type should have been unwrapped!");
1109 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001110
1111 case Type::RValueReference:
1112 // FIXME: Implement!
1113 Diag = "rvalue references";
1114 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001115 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001116
1117 assert(Diag && "Fall through without a diagnostic?");
1118 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1119 "debug information for %0 is not yet supported");
1120 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1121 << Diag;
1122 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001123}
1124
1125/// EmitFunctionStart - Constructs the debug code for entering a function -
1126/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001127void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001128 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001129 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Devang Patel9c6c3a02010-01-14 00:36:21 +00001131 llvm::StringRef Name;
1132 llvm::StringRef LinkageName;
1133
1134 const Decl *D = GD.getDecl();
1135 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001136 // If there is a DISubprogram for this function available then use it.
1137 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1138 FI = SPCache.find(FD);
1139 if (FI != SPCache.end()) {
1140 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1141 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1142 RegionStack.push_back(SP.getNode());
1143 return;
1144 }
1145 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001146 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001147 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001148 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001149 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001150 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001151 } else {
1152 // Use llvm function name as linkage name.
1153 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001154 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001155 if (!Name.empty() && Name[0] == '\01')
1156 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Devang Patel98a200b2010-01-14 18:06:13 +00001159 // It is expected that CurLoc is set before using EmitFunctionStart.
1160 // Usually, CurLoc points to the left bracket location of compound
1161 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001162 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001163 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001164 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Chris Lattner9c85ba32008-11-10 06:08:34 +00001166 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001167 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001168 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001169 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001171 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001172 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001173}
1174
1175
Chris Lattner9c85ba32008-11-10 06:08:34 +00001176void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001177 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001179 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001180 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001181 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001182 || (SM.getInstantiationLineNumber(CurLoc) ==
1183 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001184 && SM.isFromSameFile(CurLoc, PrevLoc)))
1185 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001186
1187 // Update last state.
1188 PrevLoc = CurLoc;
1189
1190 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001191 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001192 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001193
Devang Patel8fae0602009-11-13 19:10:24 +00001194 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001195 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1196 llvm::DILocation DO(NULL);
1197 llvm::DILocation DL =
1198 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1199 DS, DO);
1200 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001201}
1202
1203/// EmitRegionStart- Constructs the debug code for entering a declarative
1204/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001205void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001206 llvm::DIDescriptor D =
1207 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1208 llvm::DIDescriptor() :
1209 llvm::DIDescriptor(RegionStack.back()));
1210 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001211}
1212
1213/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1214/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001215void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001216 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1217
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001218 // Provide an region stop point.
1219 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001221 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001222}
1223
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001224/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1226 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001227 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1228
Devang Patel07739032009-03-27 23:16:32 +00001229 // Do not emit variable debug information while generating optimized code.
1230 // The llvm optimizer and code generator are not yet ready to support
1231 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001232 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001233 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001234 return;
1235
Chris Lattner650cea92009-05-05 04:57:08 +00001236 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001237 QualType Type = Decl->getType();
1238 llvm::DIType Ty = getOrCreateType(Type, Unit);
1239 if (Decl->hasAttr<BlocksAttr>()) {
1240 llvm::DICompileUnit DefUnit;
1241 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1242
1243 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1244
1245 llvm::DIType FieldTy;
1246
1247 QualType FType;
1248 uint64_t FieldSize, FieldOffset;
1249 unsigned FieldAlign;
1250
1251 llvm::DIArray Elements;
1252 llvm::DIType EltTy;
1253
1254 // Build up structure for the byref. See BuildByRefType.
1255 FieldOffset = 0;
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 "__isa", DefUnit,
1262 0, FieldSize, FieldAlign,
1263 FieldOffset, 0, FieldTy);
1264 EltTys.push_back(FieldTy);
1265 FieldOffset += FieldSize;
1266
Anders Carlsson20f12a22009-12-06 18:00:51 +00001267 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001268 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001269 FieldSize = CGM.getContext().getTypeSize(FType);
1270 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001271 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1272 "__forwarding", DefUnit,
1273 0, FieldSize, FieldAlign,
1274 FieldOffset, 0, FieldTy);
1275 EltTys.push_back(FieldTy);
1276 FieldOffset += FieldSize;
1277
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001278 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001279 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001280 FieldSize = CGM.getContext().getTypeSize(FType);
1281 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001282 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1283 "__flags", DefUnit,
1284 0, FieldSize, FieldAlign,
1285 FieldOffset, 0, FieldTy);
1286 EltTys.push_back(FieldTy);
1287 FieldOffset += FieldSize;
1288
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001289 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001290 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001291 FieldSize = CGM.getContext().getTypeSize(FType);
1292 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001293 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1294 "__size", DefUnit,
1295 0, FieldSize, FieldAlign,
1296 FieldOffset, 0, FieldTy);
1297 EltTys.push_back(FieldTy);
1298 FieldOffset += FieldSize;
1299
Anders Carlsson20f12a22009-12-06 18:00:51 +00001300 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001301 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001302 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001303 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001304 FieldSize = CGM.getContext().getTypeSize(FType);
1305 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001306 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1307 "__copy_helper", DefUnit,
1308 0, FieldSize, FieldAlign,
1309 FieldOffset, 0, FieldTy);
1310 EltTys.push_back(FieldTy);
1311 FieldOffset += FieldSize;
1312
Anders Carlsson20f12a22009-12-06 18:00:51 +00001313 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001314 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001315 FieldSize = CGM.getContext().getTypeSize(FType);
1316 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001317 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1318 "__destroy_helper", DefUnit,
1319 0, FieldSize, FieldAlign,
1320 FieldOffset, 0, FieldTy);
1321 EltTys.push_back(FieldTy);
1322 FieldOffset += FieldSize;
1323 }
1324
Anders Carlsson20f12a22009-12-06 18:00:51 +00001325 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1326 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001327 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001328 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001329 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001330 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001331
1332 if (NumPaddingBytes > 0) {
1333 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001334 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001335 pad, ArrayType::Normal, 0);
1336 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001337 FieldSize = CGM.getContext().getTypeSize(FType);
1338 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001339 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1340 Unit, "", DefUnit,
1341 0, FieldSize, FieldAlign,
1342 FieldOffset, 0, FieldTy);
1343 EltTys.push_back(FieldTy);
1344 FieldOffset += FieldSize;
1345 }
1346 }
1347
1348 FType = Type;
1349 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001350 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001351 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001352
1353 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001354 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001355 0, FieldSize, FieldAlign,
1356 FieldOffset, 0, FieldTy);
1357 EltTys.push_back(FieldTy);
1358 FieldOffset += FieldSize;
1359
1360 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1361
1362 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1363
1364 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1365 llvm::DICompileUnit(),
1366 0, FieldOffset, 0, 0, Flags,
1367 llvm::DIType(), Elements);
1368 }
Chris Lattner650cea92009-05-05 04:57:08 +00001369
Chris Lattner9c85ba32008-11-10 06:08:34 +00001370 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001371 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001372 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001373 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001374 unsigned Column = 0;
1375 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001376 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001377 Column = PLoc.getColumn();
1378 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001379 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Chris Lattner9c85ba32008-11-10 06:08:34 +00001382 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001383 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001384 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001385 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001386 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001387 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001388 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001389 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001390
Devang Patel8fae0602009-11-13 19:10:24 +00001391 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001392 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001393 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1394
Chris Lattner23e92c02009-12-28 23:41:39 +00001395 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001396}
1397
Mike Stumpb1a6e682009-09-30 02:43:10 +00001398/// EmitDeclare - Emit local variable declaration debug info.
1399void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1400 llvm::Value *Storage, CGBuilderTy &Builder,
1401 CodeGenFunction *CGF) {
1402 const ValueDecl *Decl = BDRE->getDecl();
1403 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1404
1405 // Do not emit variable debug information while generating optimized code.
1406 // The llvm optimizer and code generator are not yet ready to support
1407 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001408 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001409 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001410 return;
1411
1412 uint64_t XOffset = 0;
1413 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1414 QualType Type = Decl->getType();
1415 llvm::DIType Ty = getOrCreateType(Type, Unit);
1416 if (Decl->hasAttr<BlocksAttr>()) {
1417 llvm::DICompileUnit DefUnit;
1418 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1419
1420 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1421
1422 llvm::DIType FieldTy;
1423
1424 QualType FType;
1425 uint64_t FieldSize, FieldOffset;
1426 unsigned FieldAlign;
1427
1428 llvm::DIArray Elements;
1429 llvm::DIType EltTy;
1430
1431 // Build up structure for the byref. See BuildByRefType.
1432 FieldOffset = 0;
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 "__isa", DefUnit,
1439 0, FieldSize, FieldAlign,
1440 FieldOffset, 0, FieldTy);
1441 EltTys.push_back(FieldTy);
1442 FieldOffset += FieldSize;
1443
Anders Carlsson20f12a22009-12-06 18:00:51 +00001444 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001445 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001446 FieldSize = CGM.getContext().getTypeSize(FType);
1447 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001448 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1449 "__forwarding", DefUnit,
1450 0, FieldSize, FieldAlign,
1451 FieldOffset, 0, FieldTy);
1452 EltTys.push_back(FieldTy);
1453 FieldOffset += FieldSize;
1454
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001455 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001456 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, Unit,
1460 "__flags", DefUnit,
1461 0, FieldSize, FieldAlign,
1462 FieldOffset, 0, FieldTy);
1463 EltTys.push_back(FieldTy);
1464 FieldOffset += FieldSize;
1465
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001466 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001467 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001468 FieldSize = CGM.getContext().getTypeSize(FType);
1469 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001470 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1471 "__size", DefUnit,
1472 0, FieldSize, FieldAlign,
1473 FieldOffset, 0, FieldTy);
1474 EltTys.push_back(FieldTy);
1475 FieldOffset += FieldSize;
1476
Anders Carlsson20f12a22009-12-06 18:00:51 +00001477 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001478 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001479 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001480 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001481 FieldSize = CGM.getContext().getTypeSize(FType);
1482 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001483 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1484 "__copy_helper", DefUnit,
1485 0, FieldSize, FieldAlign,
1486 FieldOffset, 0, FieldTy);
1487 EltTys.push_back(FieldTy);
1488 FieldOffset += FieldSize;
1489
Anders Carlsson20f12a22009-12-06 18:00:51 +00001490 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001491 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001492 FieldSize = CGM.getContext().getTypeSize(FType);
1493 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001494 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1495 "__destroy_helper", DefUnit,
1496 0, FieldSize, FieldAlign,
1497 FieldOffset, 0, FieldTy);
1498 EltTys.push_back(FieldTy);
1499 FieldOffset += FieldSize;
1500 }
1501
Anders Carlsson20f12a22009-12-06 18:00:51 +00001502 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1503 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001504 unsigned AlignedOffsetInBytes
1505 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1506 unsigned NumPaddingBytes
1507 = AlignedOffsetInBytes - FieldOffset/8;
1508
1509 if (NumPaddingBytes > 0) {
1510 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001511 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001512 pad, ArrayType::Normal, 0);
1513 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001514 FieldSize = CGM.getContext().getTypeSize(FType);
1515 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001516 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1517 Unit, "", DefUnit,
1518 0, FieldSize, FieldAlign,
1519 FieldOffset, 0, FieldTy);
1520 EltTys.push_back(FieldTy);
1521 FieldOffset += FieldSize;
1522 }
1523 }
1524
1525 FType = Type;
1526 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001527 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001528 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001529
1530 XOffset = FieldOffset;
1531 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001532 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001533 0, FieldSize, FieldAlign,
1534 FieldOffset, 0, FieldTy);
1535 EltTys.push_back(FieldTy);
1536 FieldOffset += FieldSize;
1537
1538 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1539
1540 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1541
1542 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1543 llvm::DICompileUnit(),
1544 0, FieldOffset, 0, 0, Flags,
1545 llvm::DIType(), Elements);
1546 }
1547
1548 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001549 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001550 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1551 unsigned Line = 0;
1552 if (!PLoc.isInvalid())
1553 Line = PLoc.getLine();
1554 else
1555 Unit = llvm::DICompileUnit();
1556
Ken Dyck199c3d62010-01-11 17:06:35 +00001557 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001558 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001559 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1560 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1561 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1562 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001563 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001564 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1565 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001566 // offset of __forwarding field
1567 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001568 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1569 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1570 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001571 // offset of x field
1572 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001573 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001574 }
1575
1576 // Create the descriptor for the variable.
1577 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001578 DebugFactory.CreateComplexVariable(Tag,
1579 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001580 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001581 addr);
1582 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001583 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001584 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001585
Devang Patel8fae0602009-11-13 19:10:24 +00001586 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001587 llvm::DILocation DO(NULL);
1588 llvm::DILocation DL =
1589 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001590
Chris Lattner23e92c02009-12-28 23:41:39 +00001591 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001592}
1593
Chris Lattner9c85ba32008-11-10 06:08:34 +00001594void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1595 llvm::Value *Storage,
1596 CGBuilderTy &Builder) {
1597 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1598}
1599
Mike Stumpb1a6e682009-09-30 02:43:10 +00001600void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1601 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1602 CodeGenFunction *CGF) {
1603 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1604}
1605
Chris Lattner9c85ba32008-11-10 06:08:34 +00001606/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1607/// variable declaration.
1608void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1609 CGBuilderTy &Builder) {
1610 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1611}
1612
1613
1614
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001615/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001616void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001617 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001618
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001619 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001620 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001621 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001622 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1623 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001624
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001625 QualType T = Decl->getType();
1626 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001628 // CodeGen turns int[] into int[1] so we'll do the same here.
1629 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001631 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001632 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Anders Carlsson20f12a22009-12-06 18:00:51 +00001634 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001635 ArrayType::Normal, 0);
1636 }
Devang Patel73621622009-11-25 17:37:31 +00001637 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001638 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001639 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001640 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001641 Var->hasInternalLinkage(),
1642 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001643}
1644
Devang Patel9ca36b62009-02-26 21:10:26 +00001645/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001646void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001647 ObjCInterfaceDecl *Decl) {
1648 // Create global variable debug descriptor.
1649 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001650 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001651 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1652 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001653
Devang Patel73621622009-11-25 17:37:31 +00001654 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001655
Anders Carlsson20f12a22009-12-06 18:00:51 +00001656 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001657 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Devang Patel9ca36b62009-02-26 21:10:26 +00001659 // CodeGen turns int[] into int[1] so we'll do the same here.
1660 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Devang Patel9ca36b62009-02-26 21:10:26 +00001662 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001663 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Anders Carlsson20f12a22009-12-06 18:00:51 +00001665 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001666 ArrayType::Normal, 0);
1667 }
1668
Devang Patelf6a39b72009-10-20 18:26:30 +00001669 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001670 getOrCreateType(T, Unit),
1671 Var->hasInternalLinkage(),
1672 true/*definition*/, Var);
1673}