blob: f9a40cb0b5fa059fa78434d3564b5219a97f8cd0 [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
Devang Patela6da1922010-01-28 00:28:01 +0000521/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
522/// function type is not updated to include implicit "this" pointer. Use this
523/// routine to get a method type which includes "this" pointer.
524llvm::DIType
525CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
526 llvm::DICompileUnit Unit) {
527 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
528
529 // Add "this" pointer.
530
531 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
532 assert (Args.getNumElements() && "Invalid number of arguments!");
533
534 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
535
536 // First element is always return type. For 'void' functions it is NULL.
537 Elts.push_back(Args.getElement(0));
538
539 // "this" pointer is always first argument.
540 ASTContext &Context = CGM.getContext();
541 QualType ThisPtr =
542 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
543 Elts.push_back(getOrCreateType(ThisPtr, Unit));
544
545 // Copy rest of the arguments.
546 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
547 Elts.push_back(Args.getElement(i));
548
549 llvm::DIArray EltTypeArray =
550 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
551
552 return
553 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
554 Unit, "", llvm::DICompileUnit(),
555 0, 0, 0, 0, 0,
556 llvm::DIType(), EltTypeArray);
557}
558
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000559/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
560/// a single member function GlobalDecl.
561llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000562CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000563 llvm::DICompileUnit Unit,
564 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000565 bool IsCtorOrDtor =
566 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
567
568 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000569 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000570 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571
572 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
573 // make sense to give a single ctor/dtor a linkage name.
574 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000575 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000576
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000577 SourceManager &SM = CGM.getContext().getSourceManager();
578
579 // Get the location for the method.
580 SourceLocation MethodDefLoc = Method->getLocation();
581 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
582 llvm::DICompileUnit MethodDefUnit;
583 unsigned MethodLine = 0;
584
585 if (!PLoc.isInvalid()) {
586 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
587 MethodLine = PLoc.getLine();
588 }
589
590 // Collect virtual method info.
591 llvm::DIType ContainingType;
592 unsigned Virtuality = 0;
593 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000594
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000595 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000596 if (Method->isPure())
597 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
598 else
599 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
600
601 // It doesn't make sense to give a virtual destructor a vtable index,
602 // since a single destructor has two entries in the vtable.
603 if (!isa<CXXDestructorDecl>(Method))
604 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000605 ContainingType = RecordTy;
606 }
607
608 llvm::DISubprogram SP =
609 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
610 MethodLinkageName,
611 MethodDefUnit, MethodLine,
612 MethodTy, /*isLocalToUnit=*/false,
613 Method->isThisDeclarationADefinition(),
614 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000615
616 // Don't cache ctors or dtors since we have to emit multiple functions for
617 // a single ctor or dtor.
618 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
619 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000620
621 return SP;
622}
623
Devang Patel4125fd22010-01-19 01:54:44 +0000624/// CollectCXXMemberFunctions - A helper function to collect debug info for
625/// C++ member functions.This is used while creating debug info entry for
626/// a Record.
627void CGDebugInfo::
628CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
629 llvm::DICompileUnit Unit,
630 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
631 llvm::DICompositeType &RecordTy) {
Devang Patel4125fd22010-01-19 01:54:44 +0000632 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
633 E = Decl->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000634 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000635
636 if (Method->isImplicit())
637 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000638
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000639 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000640 }
641}
642
Devang Patela245c5b2010-01-25 23:32:18 +0000643/// CollectCXXBases - A helper function to collect debug info for
644/// C++ base classes. This is used while creating debug info entry for
645/// a Record.
646void CGDebugInfo::
647CollectCXXBases(const CXXRecordDecl *Decl,
648 llvm::DICompileUnit Unit,
649 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
650 llvm::DICompositeType &RecordTy) {
651
652 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
653 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
654 BE = Decl->bases_end(); BI != BE; ++BI) {
655 unsigned BFlags = 0;
Anders Carlssone70d3912010-01-26 05:26:39 +0000656 uint64_t BaseOffset;
657
658 const CXXRecordDecl *Base =
659 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
660
661 if (BI->isVirtual()) {
662 BaseOffset = RL.getVBaseClassOffset(Base);
Devang Patela245c5b2010-01-25 23:32:18 +0000663 BFlags = llvm::DIType::FlagVirtual;
Anders Carlssone70d3912010-01-26 05:26:39 +0000664 } else
665 BaseOffset = RL.getBaseClassOffset(Base);
666
Devang Patela245c5b2010-01-25 23:32:18 +0000667 AccessSpecifier Access = BI->getAccessSpecifier();
668 if (Access == clang::AS_private)
669 BFlags |= llvm::DIType::FlagPrivate;
670 else if (Access == clang::AS_protected)
671 BFlags |= llvm::DIType::FlagProtected;
672
Devang Patela245c5b2010-01-25 23:32:18 +0000673 llvm::DIType DTy =
674 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
675 RecordTy, llvm::StringRef(),
676 llvm::DICompileUnit(), 0, 0, 0,
Anders Carlssone70d3912010-01-26 05:26:39 +0000677 BaseOffset, BFlags,
Devang Patela245c5b2010-01-25 23:32:18 +0000678 getOrCreateType(BI->getType(),
679 Unit));
680 EltTys.push_back(DTy);
681 }
682}
683
Devang Patel4ce3f202010-01-28 18:11:52 +0000684/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
685llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
686 if (!VTablePtrType.isNull())
687 return VTablePtrType;
688
689 ASTContext &Context = CGM.getContext();
690
691 /* Function type */
692 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
693 STys.push_back(getOrCreateType(Context.IntTy, Unit));
694 llvm::DIArray SElements =
695 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
696 llvm::DIType SubTy =
697 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
698 Unit, "", llvm::DICompileUnit(),
699 0, 0, 0, 0, 0, llvm::DIType(), SElements);
700
701 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
702 llvm::DIType vtbl_ptr_type
703 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
704 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
705 0, Size, 0, 0, 0, SubTy);
706
707 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
708 Unit, "", llvm::DICompileUnit(),
709 0, Size, 0, 0, 0, vtbl_ptr_type);
710 return VTablePtrType;
711}
712
713/// getVtableName - Get vtable name for the given Class.
714llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *Decl) {
715 // Otherwise construct gdb compatible name name.
716 std::string Name = "_vptr$" + Decl->getNameAsString();
717
718 // Copy this name on the side and use its reference.
719 char *StrPtr = FunctionNames.Allocate<char>(Name.length());
720 memcpy(StrPtr, Name.data(), Name.length());
721 return llvm::StringRef(StrPtr, Name.length());
722}
723
724
725/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
726/// debug info entry in EltTys vector.
727void CGDebugInfo::
728CollectVtableInfo(const CXXRecordDecl *Decl,
729 llvm::DICompileUnit Unit,
730 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
731 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
732
733 // If there is a primary base then it will hold vtable info.
734 if (RL.getPrimaryBase())
735 return;
736
737 // If this class is not dynamic then there is not any vtable info to collect.
738 if (!Decl->isDynamicClass())
739 return;
740
741 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
742 llvm::DIType VPTR
743 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
744 getVtableName(Decl), llvm::DICompileUnit(),
745 0, Size, 0, 0, 0,
746 getOrCreateVTablePtrType(Unit));
747 EltTys.push_back(VPTR);
748}
749
Devang Patel65e99f22009-02-25 01:36:11 +0000750/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
752 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000753 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755 unsigned Tag;
756 if (Decl->isStruct())
757 Tag = llvm::dwarf::DW_TAG_structure_type;
758 else if (Decl->isUnion())
759 Tag = llvm::dwarf::DW_TAG_union_type;
760 else {
761 assert(Decl->isClass() && "Unknown RecordType!");
762 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000763 }
764
Anders Carlsson20f12a22009-12-06 18:00:51 +0000765 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000766
Chris Lattner9c85ba32008-11-10 06:08:34 +0000767 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000768 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000769 llvm::DICompileUnit DefUnit;
770 unsigned Line = 0;
771 if (!PLoc.isInvalid()) {
772 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
773 Line = PLoc.getLine();
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattner9c85ba32008-11-10 06:08:34 +0000776 // Records and classes and unions can all be recursive. To handle them, we
777 // first generate a debug descriptor for the struct as a forward declaration.
778 // Then (if it is a definition) we go through and get debug info for all of
779 // its members. Finally, we create a descriptor for the complete type (which
780 // may refer to the forward decl if the struct is recursive) and replace all
781 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000782
783 // A Decl->getName() is not unique. However, the debug info descriptors
784 // are uniqued. The debug info descriptor describing record's context is
785 // necessary to keep two Decl's descriptor unique if their name match.
786 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
787 // use type's name in FwdDecl.
788 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000789 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000790 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000791 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000795 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000797
Eli Friedman14d63652009-11-16 21:04:30 +0000798 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 // Otherwise, insert it into the TypeCache so that recursive uses will find
800 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000801 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802
803 // Convert all the elements.
804 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
805
Devang Patel4ce3f202010-01-28 18:11:52 +0000806 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
807 if (CXXDecl)
808 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel428deb52010-01-19 00:00:59 +0000809 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000810 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000811 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000812 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patela245c5b2010-01-25 23:32:18 +0000813 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000814
815 // A class's primary base or the class itself contains the vtable.
816 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
817 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
818 ContainingType =
819 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
820 else if (CXXDecl->isDynamicClass())
821 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattner9c85ba32008-11-10 06:08:34 +0000824 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000825 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000826
827 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000828 uint64_t Size = CGM.getContext().getTypeSize(Ty);
829 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Devang Patel0ce73f62009-07-22 18:57:00 +0000831 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000832 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000833 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000834 llvm::DIType(), Elements,
835 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836
837 // Now that we have a real decl for the struct, replace anything using the
838 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000839 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000840
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841 return RealDecl;
842}
843
Devang Patel9ca36b62009-02-26 21:10:26 +0000844/// CreateType - get objective-c interface type.
845llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
846 llvm::DICompileUnit Unit) {
847 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Devang Patel9ca36b62009-02-26 21:10:26 +0000849 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000850 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000851
852 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000853 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000854 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
855 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
856
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Daniel Dunbard86d3362009-05-18 20:51:58 +0000858 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000859
Devang Patel9ca36b62009-02-26 21:10:26 +0000860 // To handle recursive interface, we
861 // first generate a debug descriptor for the struct as a forward declaration.
862 // Then (if it is a definition) we go through and get debug info for all of
863 // its members. Finally, we create a descriptor for the complete type (which
864 // may refer to the forward decl if the struct is recursive) and replace all
865 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000866 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000867 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000868 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000869 llvm::DIType(), llvm::DIArray(),
870 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Devang Patel9ca36b62009-02-26 21:10:26 +0000872 // If this is just a forward declaration, return it.
873 if (Decl->isForwardDecl())
874 return FwdDecl;
875
Devang Patelffffb032009-11-16 20:09:38 +0000876 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000877 // Otherwise, insert it into the TypeCache so that recursive uses will find
878 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000879 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000880
881 // Convert all the elements.
882 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
883
Devang Patelfbe899f2009-03-10 21:30:26 +0000884 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
885 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000886 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000887 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000888 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000889 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000890 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000891 0 /* offset */, 0, SClassTy);
892 EltTys.push_back(InhTag);
893 }
894
Anders Carlsson20f12a22009-12-06 18:00:51 +0000895 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000896
897 unsigned FieldNo = 0;
898 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
899 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
900 ObjCIvarDecl *Field = *I;
901 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
902
Devang Patel73621622009-11-25 17:37:31 +0000903 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000904
Devang Patelde135022009-04-27 22:40:36 +0000905 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000906 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000907 continue;
908
Devang Patel9ca36b62009-02-26 21:10:26 +0000909 // Get the location for the field.
910 SourceLocation FieldDefLoc = Field->getLocation();
911 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000912 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
913 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
914
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Devang Patel99c20eb2009-03-20 18:24:39 +0000916 QualType FType = Field->getType();
917 uint64_t FieldSize = 0;
918 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000919
Devang Patel99c20eb2009-03-20 18:24:39 +0000920 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Devang Patel99c20eb2009-03-20 18:24:39 +0000922 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000923 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000924 Expr *BitWidth = Field->getBitWidth();
925 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000926 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000927
Anders Carlsson20f12a22009-12-06 18:00:51 +0000928 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000929 }
930
Mike Stump1eb44332009-09-09 15:08:12 +0000931 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
932
Devang Patelc20482b2009-03-19 00:23:53 +0000933 unsigned Flags = 0;
934 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
935 Flags = llvm::DIType::FlagProtected;
936 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
937 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Devang Patel9ca36b62009-02-26 21:10:26 +0000939 // Create a DW_TAG_member node to remember the offset of this field in the
940 // struct. FIXME: This is an absolutely insane way to capture this
941 // information. When we gut debug info, this should be fixed.
942 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
943 FieldName, FieldDefUnit,
944 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000945 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000946 EltTys.push_back(FieldTy);
947 }
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Devang Patel9ca36b62009-02-26 21:10:26 +0000949 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000950 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000951
952 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000953 uint64_t Size = CGM.getContext().getTypeSize(Ty);
954 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Devang Patel6c1fddf2009-07-27 18:42:03 +0000956 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000957 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000958 Line, Size, Align, 0, 0, llvm::DIType(),
959 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000960
961 // Now that we have a real decl for the struct, replace anything using the
962 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000963 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000964
Devang Patel9ca36b62009-02-26 21:10:26 +0000965 return RealDecl;
966}
967
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
969 llvm::DICompileUnit Unit) {
970 EnumDecl *Decl = Ty->getDecl();
971
972 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
973
974 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000975 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000976 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000977 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000978 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000979 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner9c85ba32008-11-10 06:08:34 +0000982 // Return a CompositeType for the enum itself.
983 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000984 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000985
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986 SourceLocation DefLoc = Decl->getLocation();
987 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000988 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000989 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
990 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
991
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattner9c85ba32008-11-10 06:08:34 +0000993 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000994 uint64_t Size = 0;
995 unsigned Align = 0;
996 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000997 Size = CGM.getContext().getTypeSize(Ty);
998 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Devang Patelca80a5f2009-10-20 19:55:01 +00001001 llvm::DIType DbgTy =
1002 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +00001003 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001004 Size, Align, 0, 0,
1005 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001006 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001007}
1008
1009llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1010 llvm::DICompileUnit Unit) {
1011 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1012 return CreateType(RT, Unit);
1013 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1014 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattner9c85ba32008-11-10 06:08:34 +00001016 return llvm::DIType();
1017}
1018
1019llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1020 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001021 uint64_t Size;
1022 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001023
1024
Nuno Lopes010d5142009-01-28 00:35:17 +00001025 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001026 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001027 Size = 0;
1028 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001029 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001030 } else if (Ty->isIncompleteArrayType()) {
1031 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001032 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001033 } else {
1034 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001035 Size = CGM.getContext().getTypeSize(Ty);
1036 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 }
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattner9c85ba32008-11-10 06:08:34 +00001039 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1040 // interior arrays, do we care? Why aren't nested arrays represented the
1041 // obvious/recursive way?
1042 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1043 QualType EltTy(Ty, 0);
1044 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001045 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001046 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001047 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001048 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049 // FIXME: Verify this is right for VLAs.
1050 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1051 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattner9c85ba32008-11-10 06:08:34 +00001054 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001055 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001056
Devang Patelca80a5f2009-10-20 19:55:01 +00001057 llvm::DIType DbgTy =
1058 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1059 Unit, "", llvm::DICompileUnit(),
1060 0, Size, Align, 0, 0,
1061 getOrCreateType(EltTy, Unit),
1062 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001063 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001064}
1065
Anders Carlssona031b352009-11-06 19:19:55 +00001066llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1067 llvm::DICompileUnit Unit) {
1068 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1069 Ty, Ty->getPointeeType(), Unit);
1070}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001071
Anders Carlsson20f12a22009-12-06 18:00:51 +00001072llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1073 llvm::DICompileUnit U) {
1074 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1075 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1076
1077 if (!Ty->getPointeeType()->isFunctionType()) {
1078 // We have a data member pointer type.
1079 return PointerDiffDITy;
1080 }
1081
1082 // We have a member function pointer type. Treat it as a struct with two
1083 // ptrdiff_t members.
1084 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1085
1086 uint64_t FieldOffset = 0;
1087 llvm::DIDescriptor ElementTypes[2];
1088
1089 // FIXME: This should probably be a function type instead.
1090 ElementTypes[0] =
1091 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1092 "ptr", llvm::DICompileUnit(), 0,
1093 Info.first, Info.second, FieldOffset, 0,
1094 PointerDiffDITy);
1095 FieldOffset += Info.first;
1096
1097 ElementTypes[1] =
1098 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1099 "ptr", llvm::DICompileUnit(), 0,
1100 Info.first, Info.second, FieldOffset, 0,
1101 PointerDiffDITy);
1102
1103 llvm::DIArray Elements =
1104 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1105 llvm::array_lengthof(ElementTypes));
1106
1107 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1108 U, llvm::StringRef("test"),
1109 llvm::DICompileUnit(), 0, FieldOffset,
1110 0, 0, 0, llvm::DIType(), Elements);
1111}
1112
Douglas Gregor840943d2009-12-21 20:18:30 +00001113static QualType UnwrapTypeForDebugInfo(QualType T) {
1114 do {
1115 QualType LastT = T;
1116 switch (T->getTypeClass()) {
1117 default:
1118 return T;
1119 case Type::TemplateSpecialization:
1120 T = cast<TemplateSpecializationType>(T)->desugar();
1121 break;
1122 case Type::TypeOfExpr: {
1123 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1124 T = Ty->getUnderlyingExpr()->getType();
1125 break;
1126 }
1127 case Type::TypeOf:
1128 T = cast<TypeOfType>(T)->getUnderlyingType();
1129 break;
1130 case Type::Decltype:
1131 T = cast<DecltypeType>(T)->getUnderlyingType();
1132 break;
1133 case Type::QualifiedName:
1134 T = cast<QualifiedNameType>(T)->getNamedType();
1135 break;
1136 case Type::SubstTemplateTypeParm:
1137 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1138 break;
1139 case Type::Elaborated:
1140 T = cast<ElaboratedType>(T)->getUnderlyingType();
1141 break;
1142 }
1143
1144 assert(T != LastT && "Type unwrapping failed to unwrap!");
1145 if (T == LastT)
1146 return T;
1147 } while (true);
1148
1149 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001150}
1151
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001152/// getOrCreateType - Get the type from the cache or create a new
1153/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001154llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1155 llvm::DICompileUnit Unit) {
1156 if (Ty.isNull())
1157 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor840943d2009-12-21 20:18:30 +00001159 // Unwrap the type as needed for debug information.
1160 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001161
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001162 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001163 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001164 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001165 if (it != TypeCache.end()) {
1166 // Verify that the debug info still exists.
1167 if (&*it->second)
1168 return llvm::DIType(cast<llvm::MDNode>(it->second));
1169 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001170
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001171 // Otherwise create the type.
1172 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001173
1174 // And update the type cache.
1175 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001176 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001177}
1178
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001179/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001180llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1181 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001182 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001183 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001184 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001185
Douglas Gregor2101a822009-12-21 19:57:21 +00001186 const char *Diag = 0;
1187
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001188 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001189 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001190#define TYPE(Class, Base)
1191#define ABSTRACT_TYPE(Class, Base)
1192#define NON_CANONICAL_TYPE(Class, Base)
1193#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1194#include "clang/AST/TypeNodes.def"
1195 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001196
Anders Carlssonbfe69952009-11-06 18:24:04 +00001197 // FIXME: Handle these.
1198 case Type::ExtVector:
1199 case Type::Vector:
1200 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001201
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001202 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001203 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001204 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001205 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1206 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1207 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1208 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001209 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001210 return CreateType(cast<BlockPointerType>(Ty), Unit);
1211 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001212 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001214 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001215 case Type::FunctionProto:
1216 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001217 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001218 case Type::ConstantArray:
1219 case Type::VariableArray:
1220 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001221 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001222
1223 case Type::LValueReference:
1224 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1225
Anders Carlsson20f12a22009-12-06 18:00:51 +00001226 case Type::MemberPointer:
1227 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001228
1229 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001230 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001231 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001232 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001233 case Type::TypeOfExpr:
1234 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001235 case Type::Decltype:
1236 llvm_unreachable("type should have been unwrapped!");
1237 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001238
1239 case Type::RValueReference:
1240 // FIXME: Implement!
1241 Diag = "rvalue references";
1242 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001243 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001244
1245 assert(Diag && "Fall through without a diagnostic?");
1246 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1247 "debug information for %0 is not yet supported");
1248 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1249 << Diag;
1250 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001251}
1252
1253/// EmitFunctionStart - Constructs the debug code for entering a function -
1254/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001255void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001256 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001257 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Devang Patel9c6c3a02010-01-14 00:36:21 +00001259 llvm::StringRef Name;
1260 llvm::StringRef LinkageName;
1261
1262 const Decl *D = GD.getDecl();
1263 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001264 // If there is a DISubprogram for this function available then use it.
1265 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1266 FI = SPCache.find(FD);
1267 if (FI != SPCache.end()) {
1268 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1269 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1270 RegionStack.push_back(SP.getNode());
1271 return;
1272 }
1273 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001274 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001275 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001276 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001277 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001278 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001279 } else {
1280 // Use llvm function name as linkage name.
1281 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001282 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001283 if (!Name.empty() && Name[0] == '\01')
1284 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Devang Patel98a200b2010-01-14 18:06:13 +00001287 // It is expected that CurLoc is set before using EmitFunctionStart.
1288 // Usually, CurLoc points to the left bracket location of compound
1289 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001290 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001291 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001292 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Chris Lattner9c85ba32008-11-10 06:08:34 +00001294 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001295 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001296 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001297 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001299 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001300 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001301}
1302
1303
Chris Lattner9c85ba32008-11-10 06:08:34 +00001304void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001305 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001307 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001308 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001309 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001310 || (SM.getInstantiationLineNumber(CurLoc) ==
1311 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001312 && SM.isFromSameFile(CurLoc, PrevLoc)))
1313 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001314
1315 // Update last state.
1316 PrevLoc = CurLoc;
1317
1318 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001319 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001320 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001321
Devang Patel8fae0602009-11-13 19:10:24 +00001322 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001323 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1324 llvm::DILocation DO(NULL);
1325 llvm::DILocation DL =
1326 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1327 DS, DO);
1328 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001329}
1330
1331/// EmitRegionStart- Constructs the debug code for entering a declarative
1332/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001333void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001334 llvm::DIDescriptor D =
1335 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1336 llvm::DIDescriptor() :
1337 llvm::DIDescriptor(RegionStack.back()));
1338 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001339}
1340
1341/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1342/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001343void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001344 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1345
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001346 // Provide an region stop point.
1347 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001349 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001350}
1351
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001352/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001353void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1354 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001355 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1356
Devang Patel07739032009-03-27 23:16:32 +00001357 // Do not emit variable debug information while generating optimized code.
1358 // The llvm optimizer and code generator are not yet ready to support
1359 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001360 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001361 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001362 return;
1363
Chris Lattner650cea92009-05-05 04:57:08 +00001364 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001365 QualType Type = Decl->getType();
1366 llvm::DIType Ty = getOrCreateType(Type, Unit);
1367 if (Decl->hasAttr<BlocksAttr>()) {
1368 llvm::DICompileUnit DefUnit;
1369 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1370
1371 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1372
1373 llvm::DIType FieldTy;
1374
1375 QualType FType;
1376 uint64_t FieldSize, FieldOffset;
1377 unsigned FieldAlign;
1378
1379 llvm::DIArray Elements;
1380 llvm::DIType EltTy;
1381
1382 // Build up structure for the byref. See BuildByRefType.
1383 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001384 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001385 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001386 FieldSize = CGM.getContext().getTypeSize(FType);
1387 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001388 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1389 "__isa", DefUnit,
1390 0, FieldSize, FieldAlign,
1391 FieldOffset, 0, FieldTy);
1392 EltTys.push_back(FieldTy);
1393 FieldOffset += FieldSize;
1394
Anders Carlsson20f12a22009-12-06 18:00:51 +00001395 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001396 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001397 FieldSize = CGM.getContext().getTypeSize(FType);
1398 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001399 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1400 "__forwarding", DefUnit,
1401 0, FieldSize, FieldAlign,
1402 FieldOffset, 0, FieldTy);
1403 EltTys.push_back(FieldTy);
1404 FieldOffset += FieldSize;
1405
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001406 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001407 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001408 FieldSize = CGM.getContext().getTypeSize(FType);
1409 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001410 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1411 "__flags", DefUnit,
1412 0, FieldSize, FieldAlign,
1413 FieldOffset, 0, FieldTy);
1414 EltTys.push_back(FieldTy);
1415 FieldOffset += FieldSize;
1416
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001417 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001418 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001419 FieldSize = CGM.getContext().getTypeSize(FType);
1420 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001421 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1422 "__size", DefUnit,
1423 0, FieldSize, FieldAlign,
1424 FieldOffset, 0, FieldTy);
1425 EltTys.push_back(FieldTy);
1426 FieldOffset += FieldSize;
1427
Anders Carlsson20f12a22009-12-06 18:00:51 +00001428 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001429 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001430 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001431 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001432 FieldSize = CGM.getContext().getTypeSize(FType);
1433 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001434 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1435 "__copy_helper", DefUnit,
1436 0, FieldSize, FieldAlign,
1437 FieldOffset, 0, FieldTy);
1438 EltTys.push_back(FieldTy);
1439 FieldOffset += FieldSize;
1440
Anders Carlsson20f12a22009-12-06 18:00:51 +00001441 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001442 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001443 FieldSize = CGM.getContext().getTypeSize(FType);
1444 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001445 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1446 "__destroy_helper", DefUnit,
1447 0, FieldSize, FieldAlign,
1448 FieldOffset, 0, FieldTy);
1449 EltTys.push_back(FieldTy);
1450 FieldOffset += FieldSize;
1451 }
1452
Ken Dyck8b752f12010-01-27 17:10:57 +00001453 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1454 if (Align > CharUnits::fromQuantity(
1455 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001456 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001457 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001458 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001459 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001460
1461 if (NumPaddingBytes > 0) {
1462 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001463 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001464 pad, ArrayType::Normal, 0);
1465 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001466 FieldSize = CGM.getContext().getTypeSize(FType);
1467 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001468 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1469 Unit, "", DefUnit,
1470 0, FieldSize, FieldAlign,
1471 FieldOffset, 0, FieldTy);
1472 EltTys.push_back(FieldTy);
1473 FieldOffset += FieldSize;
1474 }
1475 }
1476
1477 FType = Type;
1478 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001479 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001480 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001481
1482 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001483 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001484 0, FieldSize, FieldAlign,
1485 FieldOffset, 0, FieldTy);
1486 EltTys.push_back(FieldTy);
1487 FieldOffset += FieldSize;
1488
1489 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1490
1491 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1492
1493 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1494 llvm::DICompileUnit(),
1495 0, FieldOffset, 0, 0, Flags,
1496 llvm::DIType(), Elements);
1497 }
Chris Lattner650cea92009-05-05 04:57:08 +00001498
Chris Lattner9c85ba32008-11-10 06:08:34 +00001499 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001500 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001501 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001502 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001503 unsigned Column = 0;
1504 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001505 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001506 Column = PLoc.getColumn();
1507 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001508 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattner9c85ba32008-11-10 06:08:34 +00001511 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001512 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001513 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001514 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001515 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001516 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001517 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001518 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001519
Devang Patel8fae0602009-11-13 19:10:24 +00001520 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001521 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001522 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1523
Chris Lattner23e92c02009-12-28 23:41:39 +00001524 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001525}
1526
Mike Stumpb1a6e682009-09-30 02:43:10 +00001527/// EmitDeclare - Emit local variable declaration debug info.
1528void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1529 llvm::Value *Storage, CGBuilderTy &Builder,
1530 CodeGenFunction *CGF) {
1531 const ValueDecl *Decl = BDRE->getDecl();
1532 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1533
1534 // Do not emit variable debug information while generating optimized code.
1535 // The llvm optimizer and code generator are not yet ready to support
1536 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001537 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001538 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001539 return;
1540
1541 uint64_t XOffset = 0;
1542 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1543 QualType Type = Decl->getType();
1544 llvm::DIType Ty = getOrCreateType(Type, Unit);
1545 if (Decl->hasAttr<BlocksAttr>()) {
1546 llvm::DICompileUnit DefUnit;
1547 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1548
1549 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1550
1551 llvm::DIType FieldTy;
1552
1553 QualType FType;
1554 uint64_t FieldSize, FieldOffset;
1555 unsigned FieldAlign;
1556
1557 llvm::DIArray Elements;
1558 llvm::DIType EltTy;
1559
1560 // Build up structure for the byref. See BuildByRefType.
1561 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001562 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001563 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001564 FieldSize = CGM.getContext().getTypeSize(FType);
1565 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001566 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1567 "__isa", DefUnit,
1568 0, FieldSize, FieldAlign,
1569 FieldOffset, 0, FieldTy);
1570 EltTys.push_back(FieldTy);
1571 FieldOffset += FieldSize;
1572
Anders Carlsson20f12a22009-12-06 18:00:51 +00001573 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001574 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001575 FieldSize = CGM.getContext().getTypeSize(FType);
1576 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001577 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1578 "__forwarding", DefUnit,
1579 0, FieldSize, FieldAlign,
1580 FieldOffset, 0, FieldTy);
1581 EltTys.push_back(FieldTy);
1582 FieldOffset += FieldSize;
1583
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001584 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001585 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001586 FieldSize = CGM.getContext().getTypeSize(FType);
1587 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001588 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1589 "__flags", DefUnit,
1590 0, FieldSize, FieldAlign,
1591 FieldOffset, 0, FieldTy);
1592 EltTys.push_back(FieldTy);
1593 FieldOffset += FieldSize;
1594
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001595 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001596 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001597 FieldSize = CGM.getContext().getTypeSize(FType);
1598 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1600 "__size", DefUnit,
1601 0, FieldSize, FieldAlign,
1602 FieldOffset, 0, FieldTy);
1603 EltTys.push_back(FieldTy);
1604 FieldOffset += FieldSize;
1605
Anders Carlsson20f12a22009-12-06 18:00:51 +00001606 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001607 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001608 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001609 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001610 FieldSize = CGM.getContext().getTypeSize(FType);
1611 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001612 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1613 "__copy_helper", DefUnit,
1614 0, FieldSize, FieldAlign,
1615 FieldOffset, 0, FieldTy);
1616 EltTys.push_back(FieldTy);
1617 FieldOffset += FieldSize;
1618
Anders Carlsson20f12a22009-12-06 18:00:51 +00001619 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001620 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001621 FieldSize = CGM.getContext().getTypeSize(FType);
1622 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001623 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1624 "__destroy_helper", DefUnit,
1625 0, FieldSize, FieldAlign,
1626 FieldOffset, 0, FieldTy);
1627 EltTys.push_back(FieldTy);
1628 FieldOffset += FieldSize;
1629 }
1630
Ken Dyck8b752f12010-01-27 17:10:57 +00001631 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1632 if (Align > CharUnits::fromQuantity(
1633 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001634 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001635 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001636 unsigned NumPaddingBytes
1637 = AlignedOffsetInBytes - FieldOffset/8;
1638
1639 if (NumPaddingBytes > 0) {
1640 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001641 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001642 pad, ArrayType::Normal, 0);
1643 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001644 FieldSize = CGM.getContext().getTypeSize(FType);
1645 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001646 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1647 Unit, "", DefUnit,
1648 0, FieldSize, FieldAlign,
1649 FieldOffset, 0, FieldTy);
1650 EltTys.push_back(FieldTy);
1651 FieldOffset += FieldSize;
1652 }
1653 }
1654
1655 FType = Type;
1656 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001657 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001658 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001659
1660 XOffset = FieldOffset;
1661 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001662 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001663 0, FieldSize, FieldAlign,
1664 FieldOffset, 0, FieldTy);
1665 EltTys.push_back(FieldTy);
1666 FieldOffset += FieldSize;
1667
1668 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1669
1670 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1671
1672 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1673 llvm::DICompileUnit(),
1674 0, FieldOffset, 0, 0, Flags,
1675 llvm::DIType(), Elements);
1676 }
1677
1678 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001679 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001680 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1681 unsigned Line = 0;
1682 if (!PLoc.isInvalid())
1683 Line = PLoc.getLine();
1684 else
1685 Unit = llvm::DICompileUnit();
1686
Ken Dyck199c3d62010-01-11 17:06:35 +00001687 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001688 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001689 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1690 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1691 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1692 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001693 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001694 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1695 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001696 // offset of __forwarding field
1697 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001698 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1699 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1700 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001701 // offset of x field
1702 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001703 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001704 }
1705
1706 // Create the descriptor for the variable.
1707 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001708 DebugFactory.CreateComplexVariable(Tag,
1709 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001710 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001711 addr);
1712 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001713 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001714 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001715
Devang Patel8fae0602009-11-13 19:10:24 +00001716 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001717 llvm::DILocation DO(NULL);
1718 llvm::DILocation DL =
1719 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001720
Chris Lattner23e92c02009-12-28 23:41:39 +00001721 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001722}
1723
Chris Lattner9c85ba32008-11-10 06:08:34 +00001724void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1725 llvm::Value *Storage,
1726 CGBuilderTy &Builder) {
1727 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1728}
1729
Mike Stumpb1a6e682009-09-30 02:43:10 +00001730void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1731 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1732 CodeGenFunction *CGF) {
1733 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1734}
1735
Chris Lattner9c85ba32008-11-10 06:08:34 +00001736/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1737/// variable declaration.
1738void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1739 CGBuilderTy &Builder) {
1740 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1741}
1742
1743
1744
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001745/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001746void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001747 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001748
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001749 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001750 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001751 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001752 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1753 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001754
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001755 QualType T = Decl->getType();
1756 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001758 // CodeGen turns int[] into int[1] so we'll do the same here.
1759 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001761 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001762 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Anders Carlsson20f12a22009-12-06 18:00:51 +00001764 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001765 ArrayType::Normal, 0);
1766 }
Devang Patel73621622009-11-25 17:37:31 +00001767 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001768 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001769 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001770 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001771 Var->hasInternalLinkage(),
1772 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001773}
1774
Devang Patel9ca36b62009-02-26 21:10:26 +00001775/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001776void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001777 ObjCInterfaceDecl *Decl) {
1778 // Create global variable debug descriptor.
1779 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001780 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001781 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1782 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001783
Devang Patel73621622009-11-25 17:37:31 +00001784 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001785
Anders Carlsson20f12a22009-12-06 18:00:51 +00001786 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001787 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Devang Patel9ca36b62009-02-26 21:10:26 +00001789 // CodeGen turns int[] into int[1] so we'll do the same here.
1790 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Devang Patel9ca36b62009-02-26 21:10:26 +00001792 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001793 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Anders Carlsson20f12a22009-12-06 18:00:51 +00001795 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001796 ArrayType::Normal, 0);
1797 }
1798
Devang Patelf6a39b72009-10-20 18:26:30 +00001799 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001800 getOrCreateType(T, Unit),
1801 Var->hasInternalLinkage(),
1802 true/*definition*/, Var);
1803}