blob: 2840ee6e714d5f813b552fa62094d85e218d7ee0 [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.
Devang Patel89f05f82010-01-28 18:21:00 +000081 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000082 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);
Devang Pateld774d1e2010-01-28 21:43:50 +0000528
529 // Static methods do not need "this" pointer argument.
530 if (Method->isStatic())
531 return FnTy;
532
Devang Patela6da1922010-01-28 00:28:01 +0000533 // Add "this" pointer.
534
535 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
536 assert (Args.getNumElements() && "Invalid number of arguments!");
537
538 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
539
540 // First element is always return type. For 'void' functions it is NULL.
541 Elts.push_back(Args.getElement(0));
542
543 // "this" pointer is always first argument.
544 ASTContext &Context = CGM.getContext();
545 QualType ThisPtr =
546 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
547 Elts.push_back(getOrCreateType(ThisPtr, Unit));
548
549 // Copy rest of the arguments.
550 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
551 Elts.push_back(Args.getElement(i));
552
553 llvm::DIArray EltTypeArray =
554 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
555
556 return
557 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
558 Unit, "", llvm::DICompileUnit(),
559 0, 0, 0, 0, 0,
560 llvm::DIType(), EltTypeArray);
561}
562
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000563/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
564/// a single member function GlobalDecl.
565llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000566CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000567 llvm::DICompileUnit Unit,
568 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000569 bool IsCtorOrDtor =
570 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
571
572 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000573 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000574 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000575
576 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
577 // make sense to give a single ctor/dtor a linkage name.
578 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000579 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000580
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000581 SourceManager &SM = CGM.getContext().getSourceManager();
582
583 // Get the location for the method.
584 SourceLocation MethodDefLoc = Method->getLocation();
585 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
586 llvm::DICompileUnit MethodDefUnit;
587 unsigned MethodLine = 0;
588
589 if (!PLoc.isInvalid()) {
590 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
591 MethodLine = PLoc.getLine();
592 }
593
594 // Collect virtual method info.
595 llvm::DIType ContainingType;
596 unsigned Virtuality = 0;
597 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000598
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000599 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000600 if (Method->isPure())
601 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
602 else
603 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
604
605 // It doesn't make sense to give a virtual destructor a vtable index,
606 // since a single destructor has two entries in the vtable.
607 if (!isa<CXXDestructorDecl>(Method))
608 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000609 ContainingType = RecordTy;
610 }
611
612 llvm::DISubprogram SP =
613 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
614 MethodLinkageName,
615 MethodDefUnit, MethodLine,
616 MethodTy, /*isLocalToUnit=*/false,
617 Method->isThisDeclarationADefinition(),
618 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000619
620 // Don't cache ctors or dtors since we have to emit multiple functions for
621 // a single ctor or dtor.
622 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
623 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000624
625 return SP;
626}
627
Devang Patel4125fd22010-01-19 01:54:44 +0000628/// CollectCXXMemberFunctions - A helper function to collect debug info for
629/// C++ member functions.This is used while creating debug info entry for
630/// a Record.
631void CGDebugInfo::
632CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
633 llvm::DICompileUnit Unit,
634 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
635 llvm::DICompositeType &RecordTy) {
Devang Patel4125fd22010-01-19 01:54:44 +0000636 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
637 E = Decl->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000638 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000639
640 if (Method->isImplicit())
641 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000642
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000643 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000644 }
645}
646
Devang Patela245c5b2010-01-25 23:32:18 +0000647/// CollectCXXBases - A helper function to collect debug info for
648/// C++ base classes. This is used while creating debug info entry for
649/// a Record.
650void CGDebugInfo::
651CollectCXXBases(const CXXRecordDecl *Decl,
652 llvm::DICompileUnit Unit,
653 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
654 llvm::DICompositeType &RecordTy) {
655
Devang Patelca7daed2010-01-28 21:54:15 +0000656 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
657 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
658 BE = Decl->bases_end(); BI != BE; ++BI) {
659 unsigned BFlags = 0;
660 uint64_t BaseOffset;
661
662 const CXXRecordDecl *Base =
663 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
664
665 if (BI->isVirtual()) {
666 BaseOffset = RL.getVBaseClassOffset(Base);
667 BFlags = llvm::DIType::FlagVirtual;
668 } else
669 BaseOffset = RL.getBaseClassOffset(Base);
670
671 AccessSpecifier Access = BI->getAccessSpecifier();
672 if (Access == clang::AS_private)
673 BFlags |= llvm::DIType::FlagPrivate;
674 else if (Access == clang::AS_protected)
675 BFlags |= llvm::DIType::FlagProtected;
676
677 llvm::DIType DTy =
678 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
679 RecordTy, llvm::StringRef(),
680 llvm::DICompileUnit(), 0, 0, 0,
681 BaseOffset, BFlags,
682 getOrCreateType(BI->getType(),
683 Unit));
684 EltTys.push_back(DTy);
685 }
Devang Patela245c5b2010-01-25 23:32:18 +0000686}
687
Devang Patel4ce3f202010-01-28 18:11:52 +0000688/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
689llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
690 if (!VTablePtrType.isNull())
691 return VTablePtrType;
692
693 ASTContext &Context = CGM.getContext();
694
695 /* Function type */
696 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
697 STys.push_back(getOrCreateType(Context.IntTy, Unit));
698 llvm::DIArray SElements =
699 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
700 llvm::DIType SubTy =
701 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
702 Unit, "", llvm::DICompileUnit(),
703 0, 0, 0, 0, 0, llvm::DIType(), SElements);
704
705 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
706 llvm::DIType vtbl_ptr_type
707 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
708 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
709 0, Size, 0, 0, 0, SubTy);
710
711 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
712 Unit, "", llvm::DICompileUnit(),
713 0, Size, 0, 0, 0, vtbl_ptr_type);
714 return VTablePtrType;
715}
716
717/// getVtableName - Get vtable name for the given Class.
718llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *Decl) {
719 // Otherwise construct gdb compatible name name.
720 std::string Name = "_vptr$" + Decl->getNameAsString();
721
722 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000723 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 memcpy(StrPtr, Name.data(), Name.length());
725 return llvm::StringRef(StrPtr, Name.length());
726}
727
728
729/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
730/// debug info entry in EltTys vector.
731void CGDebugInfo::
732CollectVtableInfo(const CXXRecordDecl *Decl,
733 llvm::DICompileUnit Unit,
734 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
735 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
736
737 // If there is a primary base then it will hold vtable info.
738 if (RL.getPrimaryBase())
739 return;
740
741 // If this class is not dynamic then there is not any vtable info to collect.
742 if (!Decl->isDynamicClass())
743 return;
744
745 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
746 llvm::DIType VPTR
747 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
748 getVtableName(Decl), llvm::DICompileUnit(),
749 0, Size, 0, 0, 0,
750 getOrCreateVTablePtrType(Unit));
751 EltTys.push_back(VPTR);
752}
753
Devang Patel65e99f22009-02-25 01:36:11 +0000754/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
756 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000757 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner9c85ba32008-11-10 06:08:34 +0000759 unsigned Tag;
760 if (Decl->isStruct())
761 Tag = llvm::dwarf::DW_TAG_structure_type;
762 else if (Decl->isUnion())
763 Tag = llvm::dwarf::DW_TAG_union_type;
764 else {
765 assert(Decl->isClass() && "Unknown RecordType!");
766 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000767 }
768
Anders Carlsson20f12a22009-12-06 18:00:51 +0000769 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000770
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000772 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000773 llvm::DICompileUnit DefUnit;
774 unsigned Line = 0;
775 if (!PLoc.isInvalid()) {
776 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
777 Line = PLoc.getLine();
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattner9c85ba32008-11-10 06:08:34 +0000780 // Records and classes and unions can all be recursive. To handle them, we
781 // first generate a debug descriptor for the struct as a forward declaration.
782 // Then (if it is a definition) we go through and get debug info for all of
783 // its members. Finally, we create a descriptor for the complete type (which
784 // may refer to the forward decl if the struct is recursive) and replace all
785 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000786
787 // A Decl->getName() is not unique. However, the debug info descriptors
788 // are uniqued. The debug info descriptor describing record's context is
789 // necessary to keep two Decl's descriptor unique if their name match.
790 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
791 // use type's name in FwdDecl.
792 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000793 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000794 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000795 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000799 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000801
Eli Friedman14d63652009-11-16 21:04:30 +0000802 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 // Otherwise, insert it into the TypeCache so that recursive uses will find
804 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000805 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806
807 // Convert all the elements.
808 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
809
Devang Patel4ce3f202010-01-28 18:11:52 +0000810 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
Devang Patel3064afe2010-01-28 21:41:35 +0000811 if (CXXDecl) {
812 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000813 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000814 }
Devang Patel428deb52010-01-19 00:00:59 +0000815 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000816 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000817 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000818 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000819
820 // A class's primary base or the class itself contains the vtable.
821 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
822 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
823 ContainingType =
824 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
825 else if (CXXDecl->isDynamicClass())
826 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Chris Lattner9c85ba32008-11-10 06:08:34 +0000829 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000830 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000831
832 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000833 uint64_t Size = CGM.getContext().getTypeSize(Ty);
834 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Devang Patel0ce73f62009-07-22 18:57:00 +0000836 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000837 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000838 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000839 llvm::DIType(), Elements,
840 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841
842 // Now that we have a real decl for the struct, replace anything using the
843 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000844 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000845
Chris Lattner9c85ba32008-11-10 06:08:34 +0000846 return RealDecl;
847}
848
Devang Patel9ca36b62009-02-26 21:10:26 +0000849/// CreateType - get objective-c interface type.
850llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
851 llvm::DICompileUnit Unit) {
852 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Devang Patel9ca36b62009-02-26 21:10:26 +0000854 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000855 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000856
857 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000858 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000859 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
860 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
861
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Daniel Dunbard86d3362009-05-18 20:51:58 +0000863 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000864
Devang Patel9ca36b62009-02-26 21:10:26 +0000865 // To handle recursive interface, we
866 // first generate a debug descriptor for the struct as a forward declaration.
867 // Then (if it is a definition) we go through and get debug info for all of
868 // its members. Finally, we create a descriptor for the complete type (which
869 // may refer to the forward decl if the struct is recursive) and replace all
870 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000871 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000872 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000873 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000874 llvm::DIType(), llvm::DIArray(),
875 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Devang Patel9ca36b62009-02-26 21:10:26 +0000877 // If this is just a forward declaration, return it.
878 if (Decl->isForwardDecl())
879 return FwdDecl;
880
Devang Patelffffb032009-11-16 20:09:38 +0000881 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000882 // Otherwise, insert it into the TypeCache so that recursive uses will find
883 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000884 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000885
886 // Convert all the elements.
887 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
888
Devang Patelfbe899f2009-03-10 21:30:26 +0000889 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
890 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000891 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000892 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000893 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000894 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000895 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000896 0 /* offset */, 0, SClassTy);
897 EltTys.push_back(InhTag);
898 }
899
Anders Carlsson20f12a22009-12-06 18:00:51 +0000900 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000901
902 unsigned FieldNo = 0;
903 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
904 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
905 ObjCIvarDecl *Field = *I;
906 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
907
Devang Patel73621622009-11-25 17:37:31 +0000908 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000909
Devang Patelde135022009-04-27 22:40:36 +0000910 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000911 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000912 continue;
913
Devang Patel9ca36b62009-02-26 21:10:26 +0000914 // Get the location for the field.
915 SourceLocation FieldDefLoc = Field->getLocation();
916 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000917 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
918 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
919
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Devang Patel99c20eb2009-03-20 18:24:39 +0000921 QualType FType = Field->getType();
922 uint64_t FieldSize = 0;
923 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000924
Devang Patel99c20eb2009-03-20 18:24:39 +0000925 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Devang Patel99c20eb2009-03-20 18:24:39 +0000927 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000928 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000929 Expr *BitWidth = Field->getBitWidth();
930 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000931 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000932
Anders Carlsson20f12a22009-12-06 18:00:51 +0000933 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000934 }
935
Mike Stump1eb44332009-09-09 15:08:12 +0000936 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
937
Devang Patelc20482b2009-03-19 00:23:53 +0000938 unsigned Flags = 0;
939 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
940 Flags = llvm::DIType::FlagProtected;
941 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
942 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Devang Patel9ca36b62009-02-26 21:10:26 +0000944 // Create a DW_TAG_member node to remember the offset of this field in the
945 // struct. FIXME: This is an absolutely insane way to capture this
946 // information. When we gut debug info, this should be fixed.
947 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
948 FieldName, FieldDefUnit,
949 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000950 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000951 EltTys.push_back(FieldTy);
952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Devang Patel9ca36b62009-02-26 21:10:26 +0000954 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000955 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000956
957 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000958 uint64_t Size = CGM.getContext().getTypeSize(Ty);
959 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Devang Patel6c1fddf2009-07-27 18:42:03 +0000961 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000962 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000963 Line, Size, Align, 0, 0, llvm::DIType(),
964 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000965
966 // Now that we have a real decl for the struct, replace anything using the
967 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000968 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000969
Devang Patel9ca36b62009-02-26 21:10:26 +0000970 return RealDecl;
971}
972
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
974 llvm::DICompileUnit Unit) {
975 EnumDecl *Decl = Ty->getDecl();
976
977 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
978
979 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000980 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000981 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000982 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000983 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000984 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987 // Return a CompositeType for the enum itself.
988 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000989 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991 SourceLocation DefLoc = Decl->getLocation();
992 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000993 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000994 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
995 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000999 uint64_t Size = 0;
1000 unsigned Align = 0;
1001 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001002 Size = CGM.getContext().getTypeSize(Ty);
1003 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Devang Patelca80a5f2009-10-20 19:55:01 +00001006 llvm::DIType DbgTy =
1007 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +00001008 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001009 Size, Align, 0, 0,
1010 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001011 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001012}
1013
1014llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1015 llvm::DICompileUnit Unit) {
1016 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1017 return CreateType(RT, Unit);
1018 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1019 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattner9c85ba32008-11-10 06:08:34 +00001021 return llvm::DIType();
1022}
1023
1024llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1025 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001026 uint64_t Size;
1027 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
1029
Nuno Lopes010d5142009-01-28 00:35:17 +00001030 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001031 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001032 Size = 0;
1033 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001034 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001035 } else if (Ty->isIncompleteArrayType()) {
1036 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001037 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001038 } else {
1039 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001040 Size = CGM.getContext().getTypeSize(Ty);
1041 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattner9c85ba32008-11-10 06:08:34 +00001044 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1045 // interior arrays, do we care? Why aren't nested arrays represented the
1046 // obvious/recursive way?
1047 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1048 QualType EltTy(Ty, 0);
1049 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001050 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001051 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001052 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001053 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001054 // FIXME: Verify this is right for VLAs.
1055 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1056 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattner9c85ba32008-11-10 06:08:34 +00001059 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001060 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061
Devang Patelca80a5f2009-10-20 19:55:01 +00001062 llvm::DIType DbgTy =
1063 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1064 Unit, "", llvm::DICompileUnit(),
1065 0, Size, Align, 0, 0,
1066 getOrCreateType(EltTy, Unit),
1067 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001068 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001069}
1070
Anders Carlssona031b352009-11-06 19:19:55 +00001071llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1072 llvm::DICompileUnit Unit) {
1073 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1074 Ty, Ty->getPointeeType(), Unit);
1075}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001076
Anders Carlsson20f12a22009-12-06 18:00:51 +00001077llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1078 llvm::DICompileUnit U) {
1079 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1080 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1081
1082 if (!Ty->getPointeeType()->isFunctionType()) {
1083 // We have a data member pointer type.
1084 return PointerDiffDITy;
1085 }
1086
1087 // We have a member function pointer type. Treat it as a struct with two
1088 // ptrdiff_t members.
1089 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1090
1091 uint64_t FieldOffset = 0;
1092 llvm::DIDescriptor ElementTypes[2];
1093
1094 // FIXME: This should probably be a function type instead.
1095 ElementTypes[0] =
1096 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1097 "ptr", llvm::DICompileUnit(), 0,
1098 Info.first, Info.second, FieldOffset, 0,
1099 PointerDiffDITy);
1100 FieldOffset += Info.first;
1101
1102 ElementTypes[1] =
1103 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1104 "ptr", llvm::DICompileUnit(), 0,
1105 Info.first, Info.second, FieldOffset, 0,
1106 PointerDiffDITy);
1107
1108 llvm::DIArray Elements =
1109 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1110 llvm::array_lengthof(ElementTypes));
1111
1112 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1113 U, llvm::StringRef("test"),
1114 llvm::DICompileUnit(), 0, FieldOffset,
1115 0, 0, 0, llvm::DIType(), Elements);
1116}
1117
Douglas Gregor840943d2009-12-21 20:18:30 +00001118static QualType UnwrapTypeForDebugInfo(QualType T) {
1119 do {
1120 QualType LastT = T;
1121 switch (T->getTypeClass()) {
1122 default:
1123 return T;
1124 case Type::TemplateSpecialization:
1125 T = cast<TemplateSpecializationType>(T)->desugar();
1126 break;
1127 case Type::TypeOfExpr: {
1128 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1129 T = Ty->getUnderlyingExpr()->getType();
1130 break;
1131 }
1132 case Type::TypeOf:
1133 T = cast<TypeOfType>(T)->getUnderlyingType();
1134 break;
1135 case Type::Decltype:
1136 T = cast<DecltypeType>(T)->getUnderlyingType();
1137 break;
1138 case Type::QualifiedName:
1139 T = cast<QualifiedNameType>(T)->getNamedType();
1140 break;
1141 case Type::SubstTemplateTypeParm:
1142 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1143 break;
1144 case Type::Elaborated:
1145 T = cast<ElaboratedType>(T)->getUnderlyingType();
1146 break;
1147 }
1148
1149 assert(T != LastT && "Type unwrapping failed to unwrap!");
1150 if (T == LastT)
1151 return T;
1152 } while (true);
1153
1154 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001155}
1156
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001157/// getOrCreateType - Get the type from the cache or create a new
1158/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001159llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1160 llvm::DICompileUnit Unit) {
1161 if (Ty.isNull())
1162 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor840943d2009-12-21 20:18:30 +00001164 // Unwrap the type as needed for debug information.
1165 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001166
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001167 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001168 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001169 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001170 if (it != TypeCache.end()) {
1171 // Verify that the debug info still exists.
1172 if (&*it->second)
1173 return llvm::DIType(cast<llvm::MDNode>(it->second));
1174 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001175
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001176 // Otherwise create the type.
1177 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001178
1179 // And update the type cache.
1180 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001181 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001182}
1183
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001184/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001185llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1186 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001187 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001188 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001189 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001190
Douglas Gregor2101a822009-12-21 19:57:21 +00001191 const char *Diag = 0;
1192
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001193 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001194 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001195#define TYPE(Class, Base)
1196#define ABSTRACT_TYPE(Class, Base)
1197#define NON_CANONICAL_TYPE(Class, Base)
1198#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1199#include "clang/AST/TypeNodes.def"
1200 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001201
Anders Carlssonbfe69952009-11-06 18:24:04 +00001202 // FIXME: Handle these.
1203 case Type::ExtVector:
1204 case Type::Vector:
1205 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001206
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001207 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001208 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001209 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001210 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1211 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1212 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1213 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001214 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001215 return CreateType(cast<BlockPointerType>(Ty), Unit);
1216 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001217 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001218 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001219 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001220 case Type::FunctionProto:
1221 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001223 case Type::ConstantArray:
1224 case Type::VariableArray:
1225 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001226 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001227
1228 case Type::LValueReference:
1229 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1230
Anders Carlsson20f12a22009-12-06 18:00:51 +00001231 case Type::MemberPointer:
1232 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001233
1234 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001235 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001236 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001237 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001238 case Type::TypeOfExpr:
1239 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001240 case Type::Decltype:
1241 llvm_unreachable("type should have been unwrapped!");
1242 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001243
1244 case Type::RValueReference:
1245 // FIXME: Implement!
1246 Diag = "rvalue references";
1247 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001248 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001249
1250 assert(Diag && "Fall through without a diagnostic?");
1251 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1252 "debug information for %0 is not yet supported");
1253 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1254 << Diag;
1255 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001256}
1257
1258/// EmitFunctionStart - Constructs the debug code for entering a function -
1259/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001260void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001261 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001262 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Devang Patel9c6c3a02010-01-14 00:36:21 +00001264 llvm::StringRef Name;
1265 llvm::StringRef LinkageName;
1266
1267 const Decl *D = GD.getDecl();
1268 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001269 // If there is a DISubprogram for this function available then use it.
1270 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1271 FI = SPCache.find(FD);
1272 if (FI != SPCache.end()) {
1273 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1274 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1275 RegionStack.push_back(SP.getNode());
1276 return;
1277 }
1278 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001279 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001280 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001281 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001282 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001283 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001284 } else {
1285 // Use llvm function name as linkage name.
1286 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001287 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001288 if (!Name.empty() && Name[0] == '\01')
1289 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Devang Patel98a200b2010-01-14 18:06:13 +00001292 // It is expected that CurLoc is set before using EmitFunctionStart.
1293 // Usually, CurLoc points to the left bracket location of compound
1294 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001295 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001296 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001297 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001300 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001301 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001302 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001304 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001305 RegionStack.push_back(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001306}
1307
1308
Chris Lattner9c85ba32008-11-10 06:08:34 +00001309void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001310 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001312 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001313 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001314 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001315 || (SM.getInstantiationLineNumber(CurLoc) ==
1316 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001317 && SM.isFromSameFile(CurLoc, PrevLoc)))
1318 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001319
1320 // Update last state.
1321 PrevLoc = CurLoc;
1322
1323 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001324 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001325 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001326
Devang Patel8fae0602009-11-13 19:10:24 +00001327 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001328 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1329 llvm::DILocation DO(NULL);
1330 llvm::DILocation DL =
1331 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1332 DS, DO);
1333 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001334}
1335
1336/// EmitRegionStart- Constructs the debug code for entering a declarative
1337/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001338void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001339 llvm::DIDescriptor D =
1340 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1341 llvm::DIDescriptor() :
1342 llvm::DIDescriptor(RegionStack.back()));
1343 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001344}
1345
1346/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1347/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001348void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001349 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1350
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001351 // Provide an region stop point.
1352 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001354 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001355}
1356
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001357/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001358void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1359 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001360 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1361
Devang Patel07739032009-03-27 23:16:32 +00001362 // Do not emit variable debug information while generating optimized code.
1363 // The llvm optimizer and code generator are not yet ready to support
1364 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001365 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001366 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001367 return;
1368
Chris Lattner650cea92009-05-05 04:57:08 +00001369 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001370 QualType Type = Decl->getType();
1371 llvm::DIType Ty = getOrCreateType(Type, Unit);
1372 if (Decl->hasAttr<BlocksAttr>()) {
1373 llvm::DICompileUnit DefUnit;
1374 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1375
1376 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1377
1378 llvm::DIType FieldTy;
1379
1380 QualType FType;
1381 uint64_t FieldSize, FieldOffset;
1382 unsigned FieldAlign;
1383
1384 llvm::DIArray Elements;
1385 llvm::DIType EltTy;
1386
1387 // Build up structure for the byref. See BuildByRefType.
1388 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001390 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001391 FieldSize = CGM.getContext().getTypeSize(FType);
1392 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001393 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1394 "__isa", DefUnit,
1395 0, FieldSize, FieldAlign,
1396 FieldOffset, 0, FieldTy);
1397 EltTys.push_back(FieldTy);
1398 FieldOffset += FieldSize;
1399
Anders Carlsson20f12a22009-12-06 18:00:51 +00001400 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001401 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001402 FieldSize = CGM.getContext().getTypeSize(FType);
1403 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001404 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1405 "__forwarding", DefUnit,
1406 0, FieldSize, FieldAlign,
1407 FieldOffset, 0, FieldTy);
1408 EltTys.push_back(FieldTy);
1409 FieldOffset += FieldSize;
1410
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001411 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001412 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001413 FieldSize = CGM.getContext().getTypeSize(FType);
1414 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001415 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1416 "__flags", DefUnit,
1417 0, FieldSize, FieldAlign,
1418 FieldOffset, 0, FieldTy);
1419 EltTys.push_back(FieldTy);
1420 FieldOffset += FieldSize;
1421
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001422 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001423 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001424 FieldSize = CGM.getContext().getTypeSize(FType);
1425 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001426 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1427 "__size", DefUnit,
1428 0, FieldSize, FieldAlign,
1429 FieldOffset, 0, FieldTy);
1430 EltTys.push_back(FieldTy);
1431 FieldOffset += FieldSize;
1432
Anders Carlsson20f12a22009-12-06 18:00:51 +00001433 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001434 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001435 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001436 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001437 FieldSize = CGM.getContext().getTypeSize(FType);
1438 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001439 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1440 "__copy_helper", DefUnit,
1441 0, FieldSize, FieldAlign,
1442 FieldOffset, 0, FieldTy);
1443 EltTys.push_back(FieldTy);
1444 FieldOffset += FieldSize;
1445
Anders Carlsson20f12a22009-12-06 18:00:51 +00001446 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001447 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001448 FieldSize = CGM.getContext().getTypeSize(FType);
1449 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001450 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1451 "__destroy_helper", DefUnit,
1452 0, FieldSize, FieldAlign,
1453 FieldOffset, 0, FieldTy);
1454 EltTys.push_back(FieldTy);
1455 FieldOffset += FieldSize;
1456 }
1457
Ken Dyck8b752f12010-01-27 17:10:57 +00001458 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1459 if (Align > CharUnits::fromQuantity(
1460 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001461 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001462 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001463 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001464 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001465
1466 if (NumPaddingBytes > 0) {
1467 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001468 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001469 pad, ArrayType::Normal, 0);
1470 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001471 FieldSize = CGM.getContext().getTypeSize(FType);
1472 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001473 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1474 Unit, "", DefUnit,
1475 0, FieldSize, FieldAlign,
1476 FieldOffset, 0, FieldTy);
1477 EltTys.push_back(FieldTy);
1478 FieldOffset += FieldSize;
1479 }
1480 }
1481
1482 FType = Type;
1483 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001484 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001485 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001486
1487 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001488 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001489 0, FieldSize, FieldAlign,
1490 FieldOffset, 0, FieldTy);
1491 EltTys.push_back(FieldTy);
1492 FieldOffset += FieldSize;
1493
1494 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1495
1496 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1497
1498 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1499 llvm::DICompileUnit(),
1500 0, FieldOffset, 0, 0, Flags,
1501 llvm::DIType(), Elements);
1502 }
Chris Lattner650cea92009-05-05 04:57:08 +00001503
Chris Lattner9c85ba32008-11-10 06:08:34 +00001504 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001505 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001506 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001507 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001508 unsigned Column = 0;
1509 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001510 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001511 Column = PLoc.getColumn();
1512 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001513 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattner9c85ba32008-11-10 06:08:34 +00001516 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001517 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001518 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001519 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001520 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001521 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001522 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001523 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001524
Devang Patel8fae0602009-11-13 19:10:24 +00001525 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001526 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001527 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1528
Chris Lattner23e92c02009-12-28 23:41:39 +00001529 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001530}
1531
Mike Stumpb1a6e682009-09-30 02:43:10 +00001532/// EmitDeclare - Emit local variable declaration debug info.
1533void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1534 llvm::Value *Storage, CGBuilderTy &Builder,
1535 CodeGenFunction *CGF) {
1536 const ValueDecl *Decl = BDRE->getDecl();
1537 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1538
1539 // Do not emit variable debug information while generating optimized code.
1540 // The llvm optimizer and code generator are not yet ready to support
1541 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001542 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001543 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001544 return;
1545
1546 uint64_t XOffset = 0;
1547 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1548 QualType Type = Decl->getType();
1549 llvm::DIType Ty = getOrCreateType(Type, Unit);
1550 if (Decl->hasAttr<BlocksAttr>()) {
1551 llvm::DICompileUnit DefUnit;
1552 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1553
1554 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1555
1556 llvm::DIType FieldTy;
1557
1558 QualType FType;
1559 uint64_t FieldSize, FieldOffset;
1560 unsigned FieldAlign;
1561
1562 llvm::DIArray Elements;
1563 llvm::DIType EltTy;
1564
1565 // Build up structure for the byref. See BuildByRefType.
1566 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001567 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001568 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001569 FieldSize = CGM.getContext().getTypeSize(FType);
1570 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001571 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1572 "__isa", DefUnit,
1573 0, FieldSize, FieldAlign,
1574 FieldOffset, 0, FieldTy);
1575 EltTys.push_back(FieldTy);
1576 FieldOffset += FieldSize;
1577
Anders Carlsson20f12a22009-12-06 18:00:51 +00001578 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001579 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001580 FieldSize = CGM.getContext().getTypeSize(FType);
1581 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001582 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1583 "__forwarding", DefUnit,
1584 0, FieldSize, FieldAlign,
1585 FieldOffset, 0, FieldTy);
1586 EltTys.push_back(FieldTy);
1587 FieldOffset += FieldSize;
1588
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001589 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001590 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001591 FieldSize = CGM.getContext().getTypeSize(FType);
1592 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001593 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1594 "__flags", DefUnit,
1595 0, FieldSize, FieldAlign,
1596 FieldOffset, 0, FieldTy);
1597 EltTys.push_back(FieldTy);
1598 FieldOffset += FieldSize;
1599
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001600 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001601 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001602 FieldSize = CGM.getContext().getTypeSize(FType);
1603 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001604 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1605 "__size", DefUnit,
1606 0, FieldSize, FieldAlign,
1607 FieldOffset, 0, FieldTy);
1608 EltTys.push_back(FieldTy);
1609 FieldOffset += FieldSize;
1610
Anders Carlsson20f12a22009-12-06 18:00:51 +00001611 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001612 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001613 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001614 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001615 FieldSize = CGM.getContext().getTypeSize(FType);
1616 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001617 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1618 "__copy_helper", DefUnit,
1619 0, FieldSize, FieldAlign,
1620 FieldOffset, 0, FieldTy);
1621 EltTys.push_back(FieldTy);
1622 FieldOffset += FieldSize;
1623
Anders Carlsson20f12a22009-12-06 18:00:51 +00001624 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001625 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001626 FieldSize = CGM.getContext().getTypeSize(FType);
1627 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001628 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1629 "__destroy_helper", DefUnit,
1630 0, FieldSize, FieldAlign,
1631 FieldOffset, 0, FieldTy);
1632 EltTys.push_back(FieldTy);
1633 FieldOffset += FieldSize;
1634 }
1635
Ken Dyck8b752f12010-01-27 17:10:57 +00001636 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1637 if (Align > CharUnits::fromQuantity(
1638 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001639 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001640 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001641 unsigned NumPaddingBytes
1642 = AlignedOffsetInBytes - FieldOffset/8;
1643
1644 if (NumPaddingBytes > 0) {
1645 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001646 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001647 pad, ArrayType::Normal, 0);
1648 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001649 FieldSize = CGM.getContext().getTypeSize(FType);
1650 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001651 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1652 Unit, "", DefUnit,
1653 0, FieldSize, FieldAlign,
1654 FieldOffset, 0, FieldTy);
1655 EltTys.push_back(FieldTy);
1656 FieldOffset += FieldSize;
1657 }
1658 }
1659
1660 FType = Type;
1661 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001662 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001663 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001664
1665 XOffset = FieldOffset;
1666 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001667 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001668 0, FieldSize, FieldAlign,
1669 FieldOffset, 0, FieldTy);
1670 EltTys.push_back(FieldTy);
1671 FieldOffset += FieldSize;
1672
1673 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1674
1675 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1676
1677 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1678 llvm::DICompileUnit(),
1679 0, FieldOffset, 0, 0, Flags,
1680 llvm::DIType(), Elements);
1681 }
1682
1683 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001684 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001685 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1686 unsigned Line = 0;
1687 if (!PLoc.isInvalid())
1688 Line = PLoc.getLine();
1689 else
1690 Unit = llvm::DICompileUnit();
1691
Ken Dyck199c3d62010-01-11 17:06:35 +00001692 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001693 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001694 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1695 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1696 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1697 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001698 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001699 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 __forwarding field
1702 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001703 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1704 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1705 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001706 // offset of x field
1707 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001708 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001709 }
1710
1711 // Create the descriptor for the variable.
1712 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001713 DebugFactory.CreateComplexVariable(Tag,
1714 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001715 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001716 addr);
1717 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001718 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001719 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001720
Devang Patel8fae0602009-11-13 19:10:24 +00001721 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001722 llvm::DILocation DO(NULL);
1723 llvm::DILocation DL =
1724 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001725
Chris Lattner23e92c02009-12-28 23:41:39 +00001726 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001727}
1728
Chris Lattner9c85ba32008-11-10 06:08:34 +00001729void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1730 llvm::Value *Storage,
1731 CGBuilderTy &Builder) {
1732 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1733}
1734
Mike Stumpb1a6e682009-09-30 02:43:10 +00001735void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1736 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1737 CodeGenFunction *CGF) {
1738 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1739}
1740
Chris Lattner9c85ba32008-11-10 06:08:34 +00001741/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1742/// variable declaration.
1743void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1744 CGBuilderTy &Builder) {
1745 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1746}
1747
1748
1749
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001750/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001751void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001752 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001753
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001754 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001755 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001756 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001757 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1758 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001759
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001760 QualType T = Decl->getType();
1761 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001763 // CodeGen turns int[] into int[1] so we'll do the same here.
1764 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001766 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001767 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Anders Carlsson20f12a22009-12-06 18:00:51 +00001769 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001770 ArrayType::Normal, 0);
1771 }
Devang Patel73621622009-11-25 17:37:31 +00001772 llvm::StringRef DeclName = Decl->getName();
Devang Patelab71ff52009-11-12 00:51:46 +00001773 DebugFactory.CreateGlobalVariable(getContext(Decl, Unit), DeclName, DeclName,
Devang Patel73621622009-11-25 17:37:31 +00001774 llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001775 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001776 Var->hasInternalLinkage(),
1777 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001778}
1779
Devang Patel9ca36b62009-02-26 21:10:26 +00001780/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001781void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001782 ObjCInterfaceDecl *Decl) {
1783 // Create global variable debug descriptor.
1784 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001785 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001786 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1787 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001788
Devang Patel73621622009-11-25 17:37:31 +00001789 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001790
Anders Carlsson20f12a22009-12-06 18:00:51 +00001791 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001792 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Devang Patel9ca36b62009-02-26 21:10:26 +00001794 // CodeGen turns int[] into int[1] so we'll do the same here.
1795 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Devang Patel9ca36b62009-02-26 21:10:26 +00001797 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001798 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Anders Carlsson20f12a22009-12-06 18:00:51 +00001800 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001801 ArrayType::Normal, 0);
1802 }
1803
Devang Patelf6a39b72009-10-20 18:26:30 +00001804 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001805 getOrCreateType(T, Unit),
1806 Var->hasInternalLinkage(),
1807 true/*definition*/, Var);
1808}