blob: b5c19eaa7f7dd54a1b5ab38a687c81c91dcc4c55 [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 Patel65e99f22009-02-25 01:36:11 +0000522/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000523llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
524 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000525 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattner9c85ba32008-11-10 06:08:34 +0000527 unsigned Tag;
528 if (Decl->isStruct())
529 Tag = llvm::dwarf::DW_TAG_structure_type;
530 else if (Decl->isUnion())
531 Tag = llvm::dwarf::DW_TAG_union_type;
532 else {
533 assert(Decl->isClass() && "Unknown RecordType!");
534 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000535 }
536
Anders Carlsson20f12a22009-12-06 18:00:51 +0000537 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000538
Chris Lattner9c85ba32008-11-10 06:08:34 +0000539 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000540 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000541 llvm::DICompileUnit DefUnit;
542 unsigned Line = 0;
543 if (!PLoc.isInvalid()) {
544 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
545 Line = PLoc.getLine();
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // Records and classes and unions can all be recursive. To handle them, we
549 // first generate a debug descriptor for the struct as a forward declaration.
550 // Then (if it is a definition) we go through and get debug info for all of
551 // its members. Finally, we create a descriptor for the complete type (which
552 // may refer to the forward decl if the struct is recursive) and replace all
553 // uses of the forward declaration with the final definition.
Devang Patel0ce73f62009-07-22 18:57:00 +0000554 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000555 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000556 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000557 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner9c85ba32008-11-10 06:08:34 +0000559 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000560 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000561 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000562
Eli Friedman14d63652009-11-16 21:04:30 +0000563 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000564 // Otherwise, insert it into the TypeCache so that recursive uses will find
565 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000566 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567
568 // Convert all the elements.
569 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
570
Chris Lattner9c85ba32008-11-10 06:08:34 +0000571
Chris Lattner8ec03f52008-11-24 03:54:41 +0000572
Devang Patel428deb52010-01-19 00:00:59 +0000573 CollectRecordFields(Decl, Unit, EltTys);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner9c85ba32008-11-10 06:08:34 +0000575 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000576 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000577
578 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000579 uint64_t Size = CGM.getContext().getTypeSize(Ty);
580 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Devang Patel0ce73f62009-07-22 18:57:00 +0000582 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000583 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000584 DefUnit, Line, Size, Align, 0, 0,
585 llvm::DIType(), Elements);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586
587 // Now that we have a real decl for the struct, replace anything using the
588 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000589 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000590
Chris Lattner9c85ba32008-11-10 06:08:34 +0000591 return RealDecl;
592}
593
Devang Patel9ca36b62009-02-26 21:10:26 +0000594/// CreateType - get objective-c interface type.
595llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
596 llvm::DICompileUnit Unit) {
597 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Devang Patel9ca36b62009-02-26 21:10:26 +0000599 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000600 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000601
602 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000603 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000604 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
605 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
606
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Daniel Dunbard86d3362009-05-18 20:51:58 +0000608 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000609
Devang Patel9ca36b62009-02-26 21:10:26 +0000610 // To handle recursive interface, we
611 // first generate a debug descriptor for the struct as a forward declaration.
612 // Then (if it is a definition) we go through and get debug info for all of
613 // its members. Finally, we create a descriptor for the complete type (which
614 // may refer to the forward decl if the struct is recursive) and replace all
615 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000616 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000617 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000618 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000619 llvm::DIType(), llvm::DIArray(),
620 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Devang Patel9ca36b62009-02-26 21:10:26 +0000622 // If this is just a forward declaration, return it.
623 if (Decl->isForwardDecl())
624 return FwdDecl;
625
Devang Patelffffb032009-11-16 20:09:38 +0000626 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000627 // Otherwise, insert it into the TypeCache so that recursive uses will find
628 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000629 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000630
631 // Convert all the elements.
632 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
633
Devang Patelfbe899f2009-03-10 21:30:26 +0000634 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
635 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000636 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000637 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000638 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000639 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000640 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000641 0 /* offset */, 0, SClassTy);
642 EltTys.push_back(InhTag);
643 }
644
Anders Carlsson20f12a22009-12-06 18:00:51 +0000645 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000646
647 unsigned FieldNo = 0;
648 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
649 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
650 ObjCIvarDecl *Field = *I;
651 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
652
Devang Patel73621622009-11-25 17:37:31 +0000653 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000654
Devang Patelde135022009-04-27 22:40:36 +0000655 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000656 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000657 continue;
658
Devang Patel9ca36b62009-02-26 21:10:26 +0000659 // Get the location for the field.
660 SourceLocation FieldDefLoc = Field->getLocation();
661 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000662 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
663 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Devang Patel99c20eb2009-03-20 18:24:39 +0000666 QualType FType = Field->getType();
667 uint64_t FieldSize = 0;
668 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000669
Devang Patel99c20eb2009-03-20 18:24:39 +0000670 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Devang Patel99c20eb2009-03-20 18:24:39 +0000672 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000673 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000674 Expr *BitWidth = Field->getBitWidth();
675 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000676 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000677
Anders Carlsson20f12a22009-12-06 18:00:51 +0000678 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000679 }
680
Mike Stump1eb44332009-09-09 15:08:12 +0000681 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
682
Devang Patelc20482b2009-03-19 00:23:53 +0000683 unsigned Flags = 0;
684 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
685 Flags = llvm::DIType::FlagProtected;
686 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
687 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Devang Patel9ca36b62009-02-26 21:10:26 +0000689 // Create a DW_TAG_member node to remember the offset of this field in the
690 // struct. FIXME: This is an absolutely insane way to capture this
691 // information. When we gut debug info, this should be fixed.
692 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
693 FieldName, FieldDefUnit,
694 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000695 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000696 EltTys.push_back(FieldTy);
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Devang Patel9ca36b62009-02-26 21:10:26 +0000699 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000700 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000701
702 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000703 uint64_t Size = CGM.getContext().getTypeSize(Ty);
704 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Devang Patel6c1fddf2009-07-27 18:42:03 +0000706 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000707 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000708 Line, Size, Align, 0, 0, llvm::DIType(),
709 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000710
711 // Now that we have a real decl for the struct, replace anything using the
712 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000713 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000714
Devang Patel9ca36b62009-02-26 21:10:26 +0000715 return RealDecl;
716}
717
Chris Lattner9c85ba32008-11-10 06:08:34 +0000718llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
719 llvm::DICompileUnit Unit) {
720 EnumDecl *Decl = Ty->getDecl();
721
722 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
723
724 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000725 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000726 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000727 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000728 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000729 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Chris Lattner9c85ba32008-11-10 06:08:34 +0000732 // Return a CompositeType for the enum itself.
733 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000734 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000735
Chris Lattner9c85ba32008-11-10 06:08:34 +0000736 SourceLocation DefLoc = Decl->getLocation();
737 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000738 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000739 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
740 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
741
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000744 uint64_t Size = 0;
745 unsigned Align = 0;
746 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000747 Size = CGM.getContext().getTypeSize(Ty);
748 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Devang Patelca80a5f2009-10-20 19:55:01 +0000751 llvm::DIType DbgTy =
752 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +0000753 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000754 Size, Align, 0, 0,
755 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000756 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000757}
758
759llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
760 llvm::DICompileUnit Unit) {
761 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
762 return CreateType(RT, Unit);
763 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
764 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattner9c85ba32008-11-10 06:08:34 +0000766 return llvm::DIType();
767}
768
769llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
770 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000771 uint64_t Size;
772 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +0000773
774
Nuno Lopes010d5142009-01-28 00:35:17 +0000775 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +0000776 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +0000777 Size = 0;
778 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000779 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +0000780 } else if (Ty->isIncompleteArrayType()) {
781 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000782 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +0000783 } else {
784 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000785 Size = CGM.getContext().getTypeSize(Ty);
786 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +0000787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattner9c85ba32008-11-10 06:08:34 +0000789 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
790 // interior arrays, do we care? Why aren't nested arrays represented the
791 // obvious/recursive way?
792 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
793 QualType EltTy(Ty, 0);
794 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000795 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000796 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +0000797 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +0000798 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 // FIXME: Verify this is right for VLAs.
800 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
801 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000805 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806
Devang Patelca80a5f2009-10-20 19:55:01 +0000807 llvm::DIType DbgTy =
808 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
809 Unit, "", llvm::DICompileUnit(),
810 0, Size, Align, 0, 0,
811 getOrCreateType(EltTy, Unit),
812 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000813 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000814}
815
Anders Carlssona031b352009-11-06 19:19:55 +0000816llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
817 llvm::DICompileUnit Unit) {
818 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
819 Ty, Ty->getPointeeType(), Unit);
820}
Chris Lattner9c85ba32008-11-10 06:08:34 +0000821
Anders Carlsson20f12a22009-12-06 18:00:51 +0000822llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
823 llvm::DICompileUnit U) {
824 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
825 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
826
827 if (!Ty->getPointeeType()->isFunctionType()) {
828 // We have a data member pointer type.
829 return PointerDiffDITy;
830 }
831
832 // We have a member function pointer type. Treat it as a struct with two
833 // ptrdiff_t members.
834 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
835
836 uint64_t FieldOffset = 0;
837 llvm::DIDescriptor ElementTypes[2];
838
839 // FIXME: This should probably be a function type instead.
840 ElementTypes[0] =
841 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
842 "ptr", llvm::DICompileUnit(), 0,
843 Info.first, Info.second, FieldOffset, 0,
844 PointerDiffDITy);
845 FieldOffset += Info.first;
846
847 ElementTypes[1] =
848 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
849 "ptr", llvm::DICompileUnit(), 0,
850 Info.first, Info.second, FieldOffset, 0,
851 PointerDiffDITy);
852
853 llvm::DIArray Elements =
854 DebugFactory.GetOrCreateArray(&ElementTypes[0],
855 llvm::array_lengthof(ElementTypes));
856
857 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
858 U, llvm::StringRef("test"),
859 llvm::DICompileUnit(), 0, FieldOffset,
860 0, 0, 0, llvm::DIType(), Elements);
861}
862
Douglas Gregor840943d2009-12-21 20:18:30 +0000863static QualType UnwrapTypeForDebugInfo(QualType T) {
864 do {
865 QualType LastT = T;
866 switch (T->getTypeClass()) {
867 default:
868 return T;
869 case Type::TemplateSpecialization:
870 T = cast<TemplateSpecializationType>(T)->desugar();
871 break;
872 case Type::TypeOfExpr: {
873 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
874 T = Ty->getUnderlyingExpr()->getType();
875 break;
876 }
877 case Type::TypeOf:
878 T = cast<TypeOfType>(T)->getUnderlyingType();
879 break;
880 case Type::Decltype:
881 T = cast<DecltypeType>(T)->getUnderlyingType();
882 break;
883 case Type::QualifiedName:
884 T = cast<QualifiedNameType>(T)->getNamedType();
885 break;
886 case Type::SubstTemplateTypeParm:
887 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
888 break;
889 case Type::Elaborated:
890 T = cast<ElaboratedType>(T)->getUnderlyingType();
891 break;
892 }
893
894 assert(T != LastT && "Type unwrapping failed to unwrap!");
895 if (T == LastT)
896 return T;
897 } while (true);
898
899 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000900}
901
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000902/// getOrCreateType - Get the type from the cache or create a new
903/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
905 llvm::DICompileUnit Unit) {
906 if (Ty.isNull())
907 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor840943d2009-12-21 20:18:30 +0000909 // Unwrap the type as needed for debug information.
910 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +0000911
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000912 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000913 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000914 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +0000915 if (it != TypeCache.end()) {
916 // Verify that the debug info still exists.
917 if (&*it->second)
918 return llvm::DIType(cast<llvm::MDNode>(it->second));
919 }
Daniel Dunbar03faac32009-09-19 19:27:14 +0000920
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000921 // Otherwise create the type.
922 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000923
924 // And update the type cache.
925 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000926 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +0000927}
928
Anders Carlsson0dd57c62009-11-14 20:52:05 +0000929/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +0000930llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
931 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +0000932 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000933 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +0000934 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000935
Douglas Gregor2101a822009-12-21 19:57:21 +0000936 const char *Diag = 0;
937
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000938 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000939 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000940#define TYPE(Class, Base)
941#define ABSTRACT_TYPE(Class, Base)
942#define NON_CANONICAL_TYPE(Class, Base)
943#define DEPENDENT_TYPE(Class, Base) case Type::Class:
944#include "clang/AST/TypeNodes.def"
945 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +0000946
Anders Carlssonbfe69952009-11-06 18:24:04 +0000947 // FIXME: Handle these.
948 case Type::ExtVector:
949 case Type::Vector:
950 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +0000951
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000952 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000953 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000954 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000955 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
956 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
957 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
958 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +0000959 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000960 return CreateType(cast<BlockPointerType>(Ty), Unit);
961 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +0000962 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000963 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000964 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000965 case Type::FunctionProto:
966 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000967 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968 case Type::ConstantArray:
969 case Type::VariableArray:
970 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +0000971 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +0000972
973 case Type::LValueReference:
974 return CreateType(cast<LValueReferenceType>(Ty), Unit);
975
Anders Carlsson20f12a22009-12-06 18:00:51 +0000976 case Type::MemberPointer:
977 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +0000978
979 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +0000980 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +0000981 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +0000982 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +0000983 case Type::TypeOfExpr:
984 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +0000985 case Type::Decltype:
986 llvm_unreachable("type should have been unwrapped!");
987 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +0000988
989 case Type::RValueReference:
990 // FIXME: Implement!
991 Diag = "rvalue references";
992 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000993 }
Douglas Gregor2101a822009-12-21 19:57:21 +0000994
995 assert(Diag && "Fall through without a diagnostic?");
996 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
997 "debug information for %0 is not yet supported");
998 CGM.getDiags().Report(FullSourceLoc(), DiagID)
999 << Diag;
1000 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001001}
1002
1003/// EmitFunctionStart - Constructs the debug code for entering a function -
1004/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001005void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001006 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001007 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Devang Patel9c6c3a02010-01-14 00:36:21 +00001009 llvm::StringRef Name;
1010 llvm::StringRef LinkageName;
1011
1012 const Decl *D = GD.getDecl();
1013 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1014 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001015 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001016 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001017 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001018 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001019 } else {
1020 // Use llvm function name as linkage name.
1021 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001022 LinkageName = Name;
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Devang Patel98a200b2010-01-14 18:06:13 +00001025 // It is expected that CurLoc is set before using EmitFunctionStart.
1026 // Usually, CurLoc points to the left bracket location of compound
1027 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001028 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001029 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001030 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattner9c85ba32008-11-10 06:08:34 +00001032 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001033 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001034 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001035 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001037 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001038 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001039}
1040
1041
Chris Lattner9c85ba32008-11-10 06:08:34 +00001042void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001043 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001045 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001047 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001048 || (SM.getInstantiationLineNumber(CurLoc) ==
1049 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001050 && SM.isFromSameFile(CurLoc, PrevLoc)))
1051 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001052
1053 // Update last state.
1054 PrevLoc = CurLoc;
1055
1056 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001058 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001059
Devang Patel8fae0602009-11-13 19:10:24 +00001060 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001061 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1062 llvm::DILocation DO(NULL);
1063 llvm::DILocation DL =
1064 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1065 DS, DO);
1066 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001067}
1068
1069/// EmitRegionStart- Constructs the debug code for entering a declarative
1070/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001071void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001072 llvm::DIDescriptor D =
1073 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1074 llvm::DIDescriptor() :
1075 llvm::DIDescriptor(RegionStack.back()));
1076 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001077}
1078
1079/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1080/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001082 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1083
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001084 // Provide an region stop point.
1085 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001087 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001088}
1089
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001090/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001091void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1092 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001093 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1094
Devang Patel07739032009-03-27 23:16:32 +00001095 // Do not emit variable debug information while generating optimized code.
1096 // The llvm optimizer and code generator are not yet ready to support
1097 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001098 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001099 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001100 return;
1101
Chris Lattner650cea92009-05-05 04:57:08 +00001102 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001103 QualType Type = Decl->getType();
1104 llvm::DIType Ty = getOrCreateType(Type, Unit);
1105 if (Decl->hasAttr<BlocksAttr>()) {
1106 llvm::DICompileUnit DefUnit;
1107 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1108
1109 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1110
1111 llvm::DIType FieldTy;
1112
1113 QualType FType;
1114 uint64_t FieldSize, FieldOffset;
1115 unsigned FieldAlign;
1116
1117 llvm::DIArray Elements;
1118 llvm::DIType EltTy;
1119
1120 // Build up structure for the byref. See BuildByRefType.
1121 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001122 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001123 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001124 FieldSize = CGM.getContext().getTypeSize(FType);
1125 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001126 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1127 "__isa", DefUnit,
1128 0, FieldSize, FieldAlign,
1129 FieldOffset, 0, FieldTy);
1130 EltTys.push_back(FieldTy);
1131 FieldOffset += FieldSize;
1132
Anders Carlsson20f12a22009-12-06 18:00:51 +00001133 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001134 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001135 FieldSize = CGM.getContext().getTypeSize(FType);
1136 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001137 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1138 "__forwarding", DefUnit,
1139 0, FieldSize, FieldAlign,
1140 FieldOffset, 0, FieldTy);
1141 EltTys.push_back(FieldTy);
1142 FieldOffset += FieldSize;
1143
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001144 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001145 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001146 FieldSize = CGM.getContext().getTypeSize(FType);
1147 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001148 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1149 "__flags", DefUnit,
1150 0, FieldSize, FieldAlign,
1151 FieldOffset, 0, FieldTy);
1152 EltTys.push_back(FieldTy);
1153 FieldOffset += FieldSize;
1154
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001155 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001156 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001157 FieldSize = CGM.getContext().getTypeSize(FType);
1158 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001159 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1160 "__size", DefUnit,
1161 0, FieldSize, FieldAlign,
1162 FieldOffset, 0, FieldTy);
1163 EltTys.push_back(FieldTy);
1164 FieldOffset += FieldSize;
1165
Anders Carlsson20f12a22009-12-06 18:00:51 +00001166 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001167 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001168 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001169 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001170 FieldSize = CGM.getContext().getTypeSize(FType);
1171 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001172 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1173 "__copy_helper", DefUnit,
1174 0, FieldSize, FieldAlign,
1175 FieldOffset, 0, FieldTy);
1176 EltTys.push_back(FieldTy);
1177 FieldOffset += FieldSize;
1178
Anders Carlsson20f12a22009-12-06 18:00:51 +00001179 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001180 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001181 FieldSize = CGM.getContext().getTypeSize(FType);
1182 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001183 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1184 "__destroy_helper", DefUnit,
1185 0, FieldSize, FieldAlign,
1186 FieldOffset, 0, FieldTy);
1187 EltTys.push_back(FieldTy);
1188 FieldOffset += FieldSize;
1189 }
1190
Anders Carlsson20f12a22009-12-06 18:00:51 +00001191 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1192 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stump39605b42009-09-22 02:12:52 +00001193 unsigned AlignedOffsetInBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001194 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
Mike Stump39605b42009-09-22 02:12:52 +00001195 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001196 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001197
1198 if (NumPaddingBytes > 0) {
1199 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001200 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001201 pad, ArrayType::Normal, 0);
1202 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001203 FieldSize = CGM.getContext().getTypeSize(FType);
1204 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001205 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1206 Unit, "", DefUnit,
1207 0, FieldSize, FieldAlign,
1208 FieldOffset, 0, FieldTy);
1209 EltTys.push_back(FieldTy);
1210 FieldOffset += FieldSize;
1211 }
1212 }
1213
1214 FType = Type;
1215 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001216 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpfd47b312009-09-22 02:44:17 +00001217 FieldAlign = Align*8;
Mike Stump39605b42009-09-22 02:12:52 +00001218
1219 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001220 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001221 0, FieldSize, FieldAlign,
1222 FieldOffset, 0, FieldTy);
1223 EltTys.push_back(FieldTy);
1224 FieldOffset += FieldSize;
1225
1226 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1227
1228 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1229
1230 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1231 llvm::DICompileUnit(),
1232 0, FieldOffset, 0, 0, Flags,
1233 llvm::DIType(), Elements);
1234 }
Chris Lattner650cea92009-05-05 04:57:08 +00001235
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001238 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001239 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001240 unsigned Column = 0;
1241 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001242 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001243 Column = PLoc.getColumn();
1244 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001245 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattner9c85ba32008-11-10 06:08:34 +00001248 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001249 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001250 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001251 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001252 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001253 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001254 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001255 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001256
Devang Patel8fae0602009-11-13 19:10:24 +00001257 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001258 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001259 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1260
Chris Lattner23e92c02009-12-28 23:41:39 +00001261 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001262}
1263
Mike Stumpb1a6e682009-09-30 02:43:10 +00001264/// EmitDeclare - Emit local variable declaration debug info.
1265void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1266 llvm::Value *Storage, CGBuilderTy &Builder,
1267 CodeGenFunction *CGF) {
1268 const ValueDecl *Decl = BDRE->getDecl();
1269 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1270
1271 // Do not emit variable debug information while generating optimized code.
1272 // The llvm optimizer and code generator are not yet ready to support
1273 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001274 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001275 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001276 return;
1277
1278 uint64_t XOffset = 0;
1279 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1280 QualType Type = Decl->getType();
1281 llvm::DIType Ty = getOrCreateType(Type, Unit);
1282 if (Decl->hasAttr<BlocksAttr>()) {
1283 llvm::DICompileUnit DefUnit;
1284 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1285
1286 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1287
1288 llvm::DIType FieldTy;
1289
1290 QualType FType;
1291 uint64_t FieldSize, FieldOffset;
1292 unsigned FieldAlign;
1293
1294 llvm::DIArray Elements;
1295 llvm::DIType EltTy;
1296
1297 // Build up structure for the byref. See BuildByRefType.
1298 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001299 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001300 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001301 FieldSize = CGM.getContext().getTypeSize(FType);
1302 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001303 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1304 "__isa", DefUnit,
1305 0, FieldSize, FieldAlign,
1306 FieldOffset, 0, FieldTy);
1307 EltTys.push_back(FieldTy);
1308 FieldOffset += FieldSize;
1309
Anders Carlsson20f12a22009-12-06 18:00:51 +00001310 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001311 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001312 FieldSize = CGM.getContext().getTypeSize(FType);
1313 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001314 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1315 "__forwarding", DefUnit,
1316 0, FieldSize, FieldAlign,
1317 FieldOffset, 0, FieldTy);
1318 EltTys.push_back(FieldTy);
1319 FieldOffset += FieldSize;
1320
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001321 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001322 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001323 FieldSize = CGM.getContext().getTypeSize(FType);
1324 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001325 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1326 "__flags", DefUnit,
1327 0, FieldSize, FieldAlign,
1328 FieldOffset, 0, FieldTy);
1329 EltTys.push_back(FieldTy);
1330 FieldOffset += FieldSize;
1331
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001332 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001333 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001334 FieldSize = CGM.getContext().getTypeSize(FType);
1335 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001336 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1337 "__size", DefUnit,
1338 0, FieldSize, FieldAlign,
1339 FieldOffset, 0, FieldTy);
1340 EltTys.push_back(FieldTy);
1341 FieldOffset += FieldSize;
1342
Anders Carlsson20f12a22009-12-06 18:00:51 +00001343 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001344 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001345 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001346 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001347 FieldSize = CGM.getContext().getTypeSize(FType);
1348 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001349 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1350 "__copy_helper", DefUnit,
1351 0, FieldSize, FieldAlign,
1352 FieldOffset, 0, FieldTy);
1353 EltTys.push_back(FieldTy);
1354 FieldOffset += FieldSize;
1355
Anders Carlsson20f12a22009-12-06 18:00:51 +00001356 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001357 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001358 FieldSize = CGM.getContext().getTypeSize(FType);
1359 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001360 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1361 "__destroy_helper", DefUnit,
1362 0, FieldSize, FieldAlign,
1363 FieldOffset, 0, FieldTy);
1364 EltTys.push_back(FieldTy);
1365 FieldOffset += FieldSize;
1366 }
1367
Anders Carlsson20f12a22009-12-06 18:00:51 +00001368 unsigned Align = CGM.getContext().getDeclAlignInBytes(Decl);
1369 if (Align > CGM.getContext().Target.getPointerAlign(0) / 8) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001370 unsigned AlignedOffsetInBytes
1371 = llvm::RoundUpToAlignment(FieldOffset/8, Align);
1372 unsigned NumPaddingBytes
1373 = AlignedOffsetInBytes - FieldOffset/8;
1374
1375 if (NumPaddingBytes > 0) {
1376 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001377 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001378 pad, ArrayType::Normal, 0);
1379 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001380 FieldSize = CGM.getContext().getTypeSize(FType);
1381 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001382 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1383 Unit, "", DefUnit,
1384 0, FieldSize, FieldAlign,
1385 FieldOffset, 0, FieldTy);
1386 EltTys.push_back(FieldTy);
1387 FieldOffset += FieldSize;
1388 }
1389 }
1390
1391 FType = Type;
1392 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001393 FieldSize = CGM.getContext().getTypeSize(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001394 FieldAlign = Align*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001395
1396 XOffset = FieldOffset;
1397 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001398 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001399 0, FieldSize, FieldAlign,
1400 FieldOffset, 0, FieldTy);
1401 EltTys.push_back(FieldTy);
1402 FieldOffset += FieldSize;
1403
1404 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1405
1406 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1407
1408 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1409 llvm::DICompileUnit(),
1410 0, FieldOffset, 0, 0, Flags,
1411 llvm::DIType(), Elements);
1412 }
1413
1414 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001415 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001416 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1417 unsigned Line = 0;
1418 if (!PLoc.isInvalid())
1419 Line = PLoc.getLine();
1420 else
1421 Unit = llvm::DICompileUnit();
1422
Ken Dyck199c3d62010-01-11 17:06:35 +00001423 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001424 llvm::SmallVector<llvm::Value *, 9> addr;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001425 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001426 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1427 llvm::DIFactory::OpDeref));
1428 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1429 llvm::DIFactory::OpPlus));
1430 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001431 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001432 if (BDRE->isByRef()) {
1433 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1434 llvm::DIFactory::OpDeref));
1435 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1436 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001437 // offset of __forwarding field
1438 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001439 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001440 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001441 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1442 llvm::DIFactory::OpDeref));
1443 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1444 llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001445 // offset of x field
1446 offset = CharUnits::fromQuantity(XOffset/8);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001447 addr.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +00001448 offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001449 }
1450
1451 // Create the descriptor for the variable.
1452 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001453 DebugFactory.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001454 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001455 addr);
1456 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001457 llvm::Instruction *Call =
1458 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertPoint());
Devang Patel23908b82009-11-12 18:21:39 +00001459
Devang Patel8fae0602009-11-13 19:10:24 +00001460 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001461 llvm::DILocation DO(NULL);
1462 llvm::DILocation DL =
1463 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001464
Chris Lattner23e92c02009-12-28 23:41:39 +00001465 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001466}
1467
Chris Lattner9c85ba32008-11-10 06:08:34 +00001468void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1469 llvm::Value *Storage,
1470 CGBuilderTy &Builder) {
1471 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1472}
1473
Mike Stumpb1a6e682009-09-30 02:43:10 +00001474void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1475 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1476 CodeGenFunction *CGF) {
1477 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1478}
1479
Chris Lattner9c85ba32008-11-10 06:08:34 +00001480/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1481/// variable declaration.
1482void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1483 CGBuilderTy &Builder) {
1484 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1485}
1486
1487
1488
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001489/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001490void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001491 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001492
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001493 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001494 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001495 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001496 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1497 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001498
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001499 QualType T = Decl->getType();
1500 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001502 // CodeGen turns int[] into int[1] so we'll do the same here.
1503 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001505 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001506 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Anders Carlsson20f12a22009-12-06 18:00:51 +00001508 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001509 ArrayType::Normal, 0);
1510 }
Devang Patel73621622009-11-25 17:37:31 +00001511 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001512 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001513 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001514 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001515 Var->hasInternalLinkage(),
1516 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001517}
1518
Devang Patel9ca36b62009-02-26 21:10:26 +00001519/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001520void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001521 ObjCInterfaceDecl *Decl) {
1522 // Create global variable debug descriptor.
1523 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001524 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001525 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1526 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001527
Devang Patel73621622009-11-25 17:37:31 +00001528 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001529
Anders Carlsson20f12a22009-12-06 18:00:51 +00001530 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001531 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Devang Patel9ca36b62009-02-26 21:10:26 +00001533 // CodeGen turns int[] into int[1] so we'll do the same here.
1534 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Devang Patel9ca36b62009-02-26 21:10:26 +00001536 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001537 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Anders Carlsson20f12a22009-12-06 18:00:51 +00001539 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001540 ArrayType::Normal, 0);
1541 }
1542
Devang Patelf6a39b72009-10-20 18:26:30 +00001543 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001544 getOrCreateType(T, Unit),
1545 Var->hasInternalLinkage(),
1546 true/*definition*/, Var);
1547}