blob: 7138bc05190eba21cb4ceda8cba54d8d153de8f2 [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruthbc55fe22009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson3efc6e62009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump31f099c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000041}
42
Chris Lattneraffb3732008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000045}
46
Chris Lattneraffb3732008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000050}
51
Devang Patel7bfc5962010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Patel7b7f46f2010-02-01 21:34:11 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel7bfc5962010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Patel7b7f46f2010-02-01 21:34:11 +000055 if (!Context)
56 return CompileUnit;
57
58 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
59 I = RegionMap.find(Context);
60 if (I != RegionMap.end())
61 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000062
Devang Patel7b7f46f2010-02-01 21:34:11 +000063 // Check namespace.
64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
66
Devang Patelfaf7e9a2009-10-06 00:35:31 +000067 return CompileUnit;
68}
69
Devang Patel934661e2010-01-14 00:36:21 +000070/// getFunctionName - Get function name for the given FunctionDecl. If the
71/// name is constructred on demand (e.g. C++ destructor) then the name
72/// is stored on the side.
73llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
74 assert (FD && "Invalid FunctionDecl!");
75 IdentifierInfo *FII = FD->getIdentifier();
76 if (FII)
77 return FII->getName();
78
79 // Otherwise construct human readable name for debug info.
80 std::string NS = FD->getNameAsString();
81
82 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +000083 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000084 memcpy(StrPtr, NS.data(), NS.length());
85 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000086}
87
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000088/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbardec8a892008-10-24 08:38:36 +000089/// one if necessary. This returns null for invalid source locations.
Chris Lattneraffb3732008-11-10 06:08:34 +000090llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel75009452009-04-17 21:06:59 +000091 // Get source file information.
92 const char *FileName = "<unknown>";
Anders Carlsson3efc6e62009-12-06 18:00:51 +000093 SourceManager &SM = CGM.getContext().getSourceManager();
Daniel Dunbar56493b02009-01-22 00:09:25 +000094 if (Loc.isValid()) {
Devang Patel75009452009-04-17 21:06:59 +000095 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
96 FileName = PLoc.getFilename();
Devang Patel1b5330a2010-02-10 01:09:50 +000097 unsigned FID = PLoc.getIncludeLoc().getRawEncoding();
Mike Stump11289f42009-09-09 15:08:12 +000098
Devang Patel1b5330a2010-02-10 01:09:50 +000099 // See if this compile unit has been used before for this valid location.
100 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
101 if (!Unit.isNull()) return Unit;
102 }
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000103
Devang Patel75009452009-04-17 21:06:59 +0000104 // Get absolute path name.
105 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer1d205642009-12-08 11:02:29 +0000106 AbsFileName.makeAbsolute();
Devang Patel75009452009-04-17 21:06:59 +0000107
Devang Patel0d425352009-06-26 18:32:22 +0000108 // See if thie compile unit is representing main source file. Each source
109 // file has corresponding compile unit. There is only one main source
110 // file at a time.
111 bool isMain = false;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000112 const LangOptions &LO = CGM.getLangOptions();
113 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel0d425352009-06-26 18:32:22 +0000114 if (isMainCompileUnitCreated == false) {
Daniel Dunbar9eac0652009-11-29 02:38:34 +0000115 if (!CGO.MainFileName.empty()) {
116 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel0d425352009-06-26 18:32:22 +0000117 isMain = true;
118 } else {
119 if (Loc.isValid() && SM.isFromMainFile(Loc))
120 isMain = true;
121 }
122 if (isMain)
123 isMainCompileUnitCreated = true;
Devang Patel75009452009-04-17 21:06:59 +0000124 }
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000125
Chris Lattner8c37df42009-03-25 03:28:08 +0000126 unsigned LangTag;
127 if (LO.CPlusPlus) {
128 if (LO.ObjC1)
129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
130 else
131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
132 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000133 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000134 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000135 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000136 } else {
137 LangTag = llvm::dwarf::DW_LANG_C89;
138 }
Devang Patel75009452009-04-17 21:06:59 +0000139
Benjamin Kramer1d205642009-12-08 11:02:29 +0000140 const char *Producer =
Mike Stumpfc8ff632009-10-09 18:38:12 +0000141#ifdef CLANG_VENDOR
142 CLANG_VENDOR
143#endif
144 "clang " CLANG_VERSION_STRING;
Chris Lattner5912de12009-05-02 01:00:04 +0000145
146 // Figure out which version of the ObjC runtime we have.
147 unsigned RuntimeVers = 0;
148 if (LO.ObjC1)
149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000150
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000151 // Create new compile unit.
Devang Patel1b5330a2010-02-10 01:09:50 +0000152 return DebugFactory.CreateCompileUnit(
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000153 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000155}
156
Devang Patel410dc002009-02-25 01:36:11 +0000157/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000158/// one if necessary.
159llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel410dc002009-02-25 01:36:11 +0000160 llvm::DICompileUnit Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000161 unsigned Encoding = 0;
162 switch (BT->getKind()) {
163 default:
164 case BuiltinType::Void:
165 return llvm::DIType();
166 case BuiltinType::UChar:
167 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
168 case BuiltinType::Char_S:
169 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
170 case BuiltinType::UShort:
171 case BuiltinType::UInt:
172 case BuiltinType::ULong:
173 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
174 case BuiltinType::Short:
175 case BuiltinType::Int:
176 case BuiltinType::Long:
177 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
178 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
179 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000180 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000181 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000182 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000183 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000184 uint64_t Size = CGM.getContext().getTypeSize(BT);
185 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000186 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Devang Patele21912d2009-10-20 19:55:01 +0000188 llvm::DIType DbgTy =
189 DebugFactory.CreateBasicType(Unit,
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000190 BT->getName(CGM.getContext().getLangOptions()),
Devang Patele21912d2009-10-20 19:55:01 +0000191 Unit, 0, Size, Align,
192 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000193 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000194}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000195
Chris Lattner7b0344f2009-04-23 06:13:01 +0000196llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
197 llvm::DICompileUnit Unit) {
198 // Bit size, align and offset of the type.
199 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
200 if (Ty->isComplexIntegerType())
201 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000203 uint64_t Size = CGM.getContext().getTypeSize(Ty);
204 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000205 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Devang Patele21912d2009-10-20 19:55:01 +0000207 llvm::DIType DbgTy =
208 DebugFactory.CreateBasicType(Unit, "complex",
209 Unit, 0, Size, Align,
210 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000211 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000212}
213
John McCall0cf15512009-09-25 01:40:47 +0000214/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000215/// a new one if necessary.
John McCall0cf15512009-09-25 01:40:47 +0000216llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
217 QualifierCollector Qc;
218 const Type *T = Qc.strip(Ty);
219
220 // Ignore these qualifiers for now.
221 Qc.removeObjCGCAttr();
222 Qc.removeAddressSpace();
223
Chris Lattneraffb3732008-11-10 06:08:34 +0000224 // We will create one Derived type for one qualifier and recurse to handle any
225 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000226 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000227 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000228 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000229 Qc.removeConst();
230 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000231 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000232 Qc.removeVolatile();
233 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000234 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000235 Qc.removeRestrict();
236 } else {
237 assert(Qc.empty() && "Unknown type qualifier for debug info");
238 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
John McCall0cf15512009-09-25 01:40:47 +0000241 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
242
Daniel Dunbara290ded2008-10-31 03:54:29 +0000243 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
244 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000245 llvm::DIType DbgTy =
246 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000248 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000249}
250
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000251llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
252 llvm::DICompileUnit Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000253 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000254 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
255 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000256 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000257}
258
Chris Lattneraffb3732008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
260 llvm::DICompileUnit Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000261 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
262 Ty->getPointeeType(), Unit);
263}
264
265llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
266 const Type *Ty,
267 QualType PointeeTy,
268 llvm::DICompileUnit Unit) {
269 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000270
Sanjiv Gupta98070572008-05-25 05:15:42 +0000271 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000272
273 // Size is always the size of a pointer. We can't use getTypeSize here
274 // because that does not return the correct value for references.
275 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000276 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
277 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000278
Devang Patele21912d2009-10-20 19:55:01 +0000279 return
Anders Carlsson443f6772009-11-06 19:19:55 +0000280 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patele21912d2009-10-20 19:55:01 +0000281 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000282
Sanjiv Gupta98070572008-05-25 05:15:42 +0000283}
284
Mike Stump31f099c2009-05-14 02:03:51 +0000285llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
286 llvm::DICompileUnit Unit) {
287 if (BlockLiteralGenericSet)
288 return BlockLiteralGeneric;
289
290 llvm::DICompileUnit DefUnit;
291 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
292
293 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
294
295 llvm::DIType FieldTy;
296
297 QualType FType;
298 uint64_t FieldSize, FieldOffset;
299 unsigned FieldAlign;
300
301 llvm::DIArray Elements;
302 llvm::DIType EltTy, DescTy;
303
304 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000305 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000307 FieldSize = CGM.getContext().getTypeSize(FType);
308 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 "reserved", DefUnit,
311 0, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
313 EltTys.push_back(FieldTy);
314
315 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000316 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000318 FieldSize = CGM.getContext().getTypeSize(FType);
319 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
321 "Size", DefUnit,
322 0, FieldSize, FieldAlign,
323 FieldOffset, 0, FieldTy);
324 EltTys.push_back(FieldTy);
325
326 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000327 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000328 EltTys.clear();
329
Mike Stump581b9ad2009-10-02 02:30:50 +0000330 unsigned Flags = llvm::DIType::FlagAppleBlock;
331
Mike Stump31f099c2009-05-14 02:03:51 +0000332 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump581b9ad2009-10-02 02:30:50 +0000333 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000334 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000335
Mike Stump31f099c2009-05-14 02:03:51 +0000336 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000337 uint64_t Size = CGM.getContext().getTypeSize(Ty);
338 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000339
Mike Stump31f099c2009-05-14 02:03:51 +0000340 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
341 Unit, "", llvm::DICompileUnit(),
342 0, Size, Align, 0, 0, EltTy);
343
344 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000345 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000346 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000347 FieldSize = CGM.getContext().getTypeSize(FType);
348 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000349 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
350 "__isa", DefUnit,
351 0, FieldSize, FieldAlign,
352 FieldOffset, 0, FieldTy);
353 EltTys.push_back(FieldTy);
354
355 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000356 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000357 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000358 FieldSize = CGM.getContext().getTypeSize(FType);
359 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000360 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
361 "__flags", DefUnit,
362 0, FieldSize, FieldAlign,
363 FieldOffset, 0, FieldTy);
364 EltTys.push_back(FieldTy);
365
366 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000367 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000368 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000369 FieldSize = CGM.getContext().getTypeSize(FType);
370 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000371 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
372 "__reserved", DefUnit,
373 0, FieldSize, FieldAlign,
374 FieldOffset, 0, FieldTy);
375 EltTys.push_back(FieldTy);
376
377 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000378 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000379 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000380 FieldSize = CGM.getContext().getTypeSize(FType);
381 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000382 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
383 "__FuncPtr", DefUnit,
384 0, FieldSize, FieldAlign,
385 FieldOffset, 0, FieldTy);
386 EltTys.push_back(FieldTy);
387
388 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000390 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000391 FieldSize = CGM.getContext().getTypeSize(Ty);
392 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000393 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
394 "__descriptor", DefUnit,
395 0, FieldSize, FieldAlign,
396 FieldOffset, 0, FieldTy);
397 EltTys.push_back(FieldTy);
398
399 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000400 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000401
402 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump440af3d2009-10-02 02:23:37 +0000403 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000404 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000405
Mike Stump31f099c2009-05-14 02:03:51 +0000406 BlockLiteralGenericSet = true;
407 BlockLiteralGeneric
408 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
409 "", llvm::DICompileUnit(),
410 0, Size, Align, 0, 0, EltTy);
411 return BlockLiteralGeneric;
412}
413
Chris Lattneraffb3732008-11-10 06:08:34 +0000414llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
415 llvm::DICompileUnit Unit) {
416 // Typedefs are derived from some other type. If we have a typedef of a
417 // typedef, make sure to emit the whole chain.
418 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattneraffb3732008-11-10 06:08:34 +0000420 // We don't set size information, but do specify where the typedef was
421 // declared.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000422 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelbb4820d2010-01-29 22:29:31 +0000423 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000424 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta98070572008-05-25 05:15:42 +0000425
Devang Patel7b7f46f2010-02-01 21:34:11 +0000426 llvm::DIDescriptor TyContext
427 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
428 Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000429 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000430 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Patel7b7f46f2010-02-01 21:34:11 +0000431 TyContext,
Devang Patelbb4820d2010-01-29 22:29:31 +0000432 Ty->getDecl()->getName(), Unit,
433 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000434 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000435}
436
Chris Lattneraffb3732008-11-10 06:08:34 +0000437llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
438 llvm::DICompileUnit Unit) {
439 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000440
Chris Lattneraffb3732008-11-10 06:08:34 +0000441 // Add the result type at least.
442 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattneraffb3732008-11-10 06:08:34 +0000444 // Set up remainder of arguments if there is a prototype.
445 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000446 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000447 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
448 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
449 } else {
450 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000451 }
452
Chris Lattneraffb3732008-11-10 06:08:34 +0000453 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000454 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000455
Devang Patele21912d2009-10-20 19:55:01 +0000456 llvm::DIType DbgTy =
457 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
458 Unit, "", llvm::DICompileUnit(),
459 0, 0, 0, 0, 0,
460 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000461 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000462}
463
Devang Patel889ce762010-01-19 00:00:59 +0000464/// CollectRecordFields - A helper function to collect debug info for
465/// record fields. This is used while creating debug info entry for a Record.
466void CGDebugInfo::
Devang Patel1c0954c2010-02-01 21:39:52 +0000467CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel889ce762010-01-19 00:00:59 +0000468 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
469 unsigned FieldNo = 0;
470 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel1c0954c2010-02-01 21:39:52 +0000471 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
472 for (RecordDecl::field_iterator I = RD->field_begin(),
473 E = RD->field_end();
Devang Patel889ce762010-01-19 00:00:59 +0000474 I != E; ++I, ++FieldNo) {
475 FieldDecl *Field = *I;
476 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
477
478 llvm::StringRef FieldName = Field->getName();
479
480 // Ignore unnamed fields.
481 if (FieldName.empty())
482 continue;
483
484 // Get the location for the field.
485 SourceLocation FieldDefLoc = Field->getLocation();
486 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
487 llvm::DICompileUnit FieldDefUnit;
488 unsigned FieldLine = 0;
489
490 if (!PLoc.isInvalid()) {
491 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
492 FieldLine = PLoc.getLine();
493 }
494
495 QualType FType = Field->getType();
496 uint64_t FieldSize = 0;
497 unsigned FieldAlign = 0;
498 if (!FType->isIncompleteArrayType()) {
499
500 // Bit size, align and offset of the type.
501 FieldSize = CGM.getContext().getTypeSize(FType);
502 Expr *BitWidth = Field->getBitWidth();
503 if (BitWidth)
504 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
505
506 FieldAlign = CGM.getContext().getTypeAlign(FType);
507 }
508
509 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
510
511 // Create a DW_TAG_member node to remember the offset of this field in the
512 // struct. FIXME: This is an absolutely insane way to capture this
513 // information. When we gut debug info, this should be fixed.
514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
515 FieldName, FieldDefUnit,
516 FieldLine, FieldSize, FieldAlign,
517 FieldOffset, 0, FieldTy);
518 EltTys.push_back(FieldTy);
519 }
520}
521
Devang Patel3d4e6d92010-01-28 00:28:01 +0000522/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
523/// function type is not updated to include implicit "this" pointer. Use this
524/// routine to get a method type which includes "this" pointer.
525llvm::DIType
526CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
527 llvm::DICompileUnit Unit) {
528 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000529
530 // Static methods do not need "this" pointer argument.
531 if (Method->isStatic())
532 return FnTy;
533
Devang Patel3d4e6d92010-01-28 00:28:01 +0000534 // Add "this" pointer.
535
536 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
537 assert (Args.getNumElements() && "Invalid number of arguments!");
538
539 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
540
541 // First element is always return type. For 'void' functions it is NULL.
542 Elts.push_back(Args.getElement(0));
543
544 // "this" pointer is always first argument.
545 ASTContext &Context = CGM.getContext();
546 QualType ThisPtr =
547 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patelcce7e852010-02-09 17:57:50 +0000548 llvm::DIType ThisPtrType =
549 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
550 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
551 Elts.push_back(ThisPtrType);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000552
553 // Copy rest of the arguments.
554 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
555 Elts.push_back(Args.getElement(i));
556
557 llvm::DIArray EltTypeArray =
558 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
559
560 return
561 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
562 Unit, "", llvm::DICompileUnit(),
563 0, 0, 0, 0, 0,
564 llvm::DIType(), EltTypeArray);
565}
566
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000567/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
568/// a single member function GlobalDecl.
569llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000570CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000571 llvm::DICompileUnit Unit,
572 llvm::DICompositeType &RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000573 bool IsCtorOrDtor =
574 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
575
576 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000577 llvm::StringRef MethodLinkageName;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000578 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000579
580 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
581 // make sense to give a single ctor/dtor a linkage name.
582 if (!IsCtorOrDtor)
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000583 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000584
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000585 SourceManager &SM = CGM.getContext().getSourceManager();
586
587 // Get the location for the method.
588 SourceLocation MethodDefLoc = Method->getLocation();
589 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
590 llvm::DICompileUnit MethodDefUnit;
591 unsigned MethodLine = 0;
592
593 if (!PLoc.isInvalid()) {
594 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
595 MethodLine = PLoc.getLine();
596 }
597
598 // Collect virtual method info.
599 llvm::DIType ContainingType;
600 unsigned Virtuality = 0;
601 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000602
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000603 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000604 if (Method->isPure())
605 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
606 else
607 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
608
609 // It doesn't make sense to give a virtual destructor a vtable index,
610 // since a single destructor has two entries in the vtable.
611 if (!isa<CXXDestructorDecl>(Method))
612 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000613 ContainingType = RecordTy;
614 }
615
616 llvm::DISubprogram SP =
617 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
618 MethodLinkageName,
619 MethodDefUnit, MethodLine,
620 MethodTy, /*isLocalToUnit=*/false,
621 Method->isThisDeclarationADefinition(),
622 Virtuality, VIndex, ContainingType);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000623
624 // Don't cache ctors or dtors since we have to emit multiple functions for
625 // a single ctor or dtor.
626 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
627 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000628
629 return SP;
630}
631
Devang Patel7a12ad02010-01-19 01:54:44 +0000632/// CollectCXXMemberFunctions - A helper function to collect debug info for
633/// C++ member functions.This is used while creating debug info entry for
634/// a Record.
635void CGDebugInfo::
Devang Patel1c0954c2010-02-01 21:39:52 +0000636CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel7a12ad02010-01-19 01:54:44 +0000637 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
638 llvm::DICompositeType &RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000639 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
640 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000641 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000642
Devang Patel0ae70d12010-02-09 19:09:28 +0000643 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000644 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000645
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000646 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000647 }
648}
649
Devang Patelc54353d2010-01-25 23:32:18 +0000650/// CollectCXXBases - A helper function to collect debug info for
651/// C++ base classes. This is used while creating debug info entry for
652/// a Record.
653void CGDebugInfo::
Devang Patel1c0954c2010-02-01 21:39:52 +0000654CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patelc54353d2010-01-25 23:32:18 +0000655 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
656 llvm::DICompositeType &RecordTy) {
657
Devang Patel1c0954c2010-02-01 21:39:52 +0000658 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
659 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
660 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000661 unsigned BFlags = 0;
662 uint64_t BaseOffset;
663
664 const CXXRecordDecl *Base =
665 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
666
667 if (BI->isVirtual()) {
Devang Patel0ae70d12010-02-09 19:09:28 +0000668 // virtual base offset index is -ve. The code generator emits dwarf
669 // expression where it expects +ve number.
670 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
Devang Patel128aa9d2010-01-28 21:54:15 +0000671 BFlags = llvm::DIType::FlagVirtual;
672 } else
673 BaseOffset = RL.getBaseClassOffset(Base);
674
675 AccessSpecifier Access = BI->getAccessSpecifier();
676 if (Access == clang::AS_private)
677 BFlags |= llvm::DIType::FlagPrivate;
678 else if (Access == clang::AS_protected)
679 BFlags |= llvm::DIType::FlagProtected;
680
681 llvm::DIType DTy =
682 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
683 RecordTy, llvm::StringRef(),
684 llvm::DICompileUnit(), 0, 0, 0,
685 BaseOffset, BFlags,
686 getOrCreateType(BI->getType(),
687 Unit));
688 EltTys.push_back(DTy);
689 }
Devang Patelc54353d2010-01-25 23:32:18 +0000690}
691
Devang Patel84033fb2010-01-28 18:11:52 +0000692/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
693llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
694 if (!VTablePtrType.isNull())
695 return VTablePtrType;
696
697 ASTContext &Context = CGM.getContext();
698
699 /* Function type */
700 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
701 STys.push_back(getOrCreateType(Context.IntTy, Unit));
702 llvm::DIArray SElements =
703 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
704 llvm::DIType SubTy =
705 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
706 Unit, "", llvm::DICompileUnit(),
707 0, 0, 0, 0, 0, llvm::DIType(), SElements);
708
709 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
710 llvm::DIType vtbl_ptr_type
711 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
712 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
713 0, Size, 0, 0, 0, SubTy);
714
715 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
716 Unit, "", llvm::DICompileUnit(),
717 0, Size, 0, 0, 0, vtbl_ptr_type);
718 return VTablePtrType;
719}
720
721/// getVtableName - Get vtable name for the given Class.
Devang Patel1c0954c2010-02-01 21:39:52 +0000722llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000723 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000724 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000725
726 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000727 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000728 memcpy(StrPtr, Name.data(), Name.length());
729 return llvm::StringRef(StrPtr, Name.length());
730}
731
732
733/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
734/// debug info entry in EltTys vector.
735void CGDebugInfo::
Devang Patel1c0954c2010-02-01 21:39:52 +0000736CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000737 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000738 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000739
740 // If there is a primary base then it will hold vtable info.
741 if (RL.getPrimaryBase())
742 return;
743
744 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000745 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000746 return;
747
748 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
749 llvm::DIType VPTR
750 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel1c0954c2010-02-01 21:39:52 +0000751 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel84033fb2010-01-28 18:11:52 +0000752 0, Size, 0, 0, 0,
753 getOrCreateVTablePtrType(Unit));
754 EltTys.push_back(VPTR);
755}
756
Devang Patel410dc002009-02-25 01:36:11 +0000757/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000758llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
759 llvm::DICompileUnit Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000760 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattneraffb3732008-11-10 06:08:34 +0000762 unsigned Tag;
Devang Patel3efd1472010-02-01 21:52:22 +0000763 if (RD->isStruct())
Chris Lattneraffb3732008-11-10 06:08:34 +0000764 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel3efd1472010-02-01 21:52:22 +0000765 else if (RD->isUnion())
Chris Lattneraffb3732008-11-10 06:08:34 +0000766 Tag = llvm::dwarf::DW_TAG_union_type;
767 else {
Devang Patel3efd1472010-02-01 21:52:22 +0000768 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattneraffb3732008-11-10 06:08:34 +0000769 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000770 }
771
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000772 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000773
Chris Lattneraffb3732008-11-10 06:08:34 +0000774 // Get overall information about the record type for the debug info.
Devang Patel3efd1472010-02-01 21:52:22 +0000775 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Chris Lattner448a2282009-05-05 05:16:17 +0000776 llvm::DICompileUnit DefUnit;
777 unsigned Line = 0;
778 if (!PLoc.isInvalid()) {
Devang Patel3efd1472010-02-01 21:52:22 +0000779 DefUnit = getOrCreateCompileUnit(RD->getLocation());
Chris Lattner448a2282009-05-05 05:16:17 +0000780 Line = PLoc.getLine();
781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattneraffb3732008-11-10 06:08:34 +0000783 // Records and classes and unions can all be recursive. To handle them, we
784 // first generate a debug descriptor for the struct as a forward declaration.
785 // Then (if it is a definition) we go through and get debug info for all of
786 // its members. Finally, we create a descriptor for the complete type (which
787 // may refer to the forward decl if the struct is recursive) and replace all
788 // uses of the forward declaration with the final definition.
Devang Patel3f4a77e2010-01-20 23:56:40 +0000789
Devang Patel3efd1472010-02-01 21:52:22 +0000790 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelab793232010-02-01 22:51:29 +0000791 // are uniqued so use type name to ensure uniquness.
Devang Patel3f4a77e2010-01-20 23:56:40 +0000792 std::string STy = QualType(Ty, 0).getAsString();
Devang Patele8fb4b72010-02-01 22:40:08 +0000793 llvm::DIDescriptor FDContext =
794 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel06cceef2009-07-22 18:57:00 +0000795 llvm::DICompositeType FwdDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000796 DebugFactory.CreateCompositeType(Tag, FDContext,
797 STy.c_str(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000798 DefUnit, Line, 0, 0, 0, 0,
Chris Lattneraffb3732008-11-10 06:08:34 +0000799 llvm::DIType(), llvm::DIArray());
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattneraffb3732008-11-10 06:08:34 +0000801 // If this is just a forward declaration, return it.
Devang Patel3efd1472010-02-01 21:52:22 +0000802 if (!RD->getDefinition(CGM.getContext()))
Chris Lattneraffb3732008-11-10 06:08:34 +0000803 return FwdDecl;
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000804
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000805 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000806 // Otherwise, insert it into the TypeCache so that recursive uses will find
807 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000808 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000809
810 // Convert all the elements.
811 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
812
Devang Patel3efd1472010-02-01 21:52:22 +0000813 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +0000814 if (CXXDecl) {
815 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel84033fb2010-01-28 18:11:52 +0000816 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000817 }
Devang Patel3efd1472010-02-01 21:52:22 +0000818 CollectRecordFields(RD, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000819 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000820 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000821 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000822
823 // A class's primary base or the class itself contains the vtable.
Devang Patel3efd1472010-02-01 21:52:22 +0000824 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patelabb44132010-01-28 00:54:21 +0000825 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
826 ContainingType =
827 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
828 else if (CXXDecl->isDynamicClass())
829 ContainingType = FwdDecl.getNode();
Devang Patelc54353d2010-01-25 23:32:18 +0000830 }
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattneraffb3732008-11-10 06:08:34 +0000832 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000833 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000834
835 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000836 uint64_t Size = CGM.getContext().getTypeSize(Ty);
837 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000838
Devang Patele8fb4b72010-02-01 22:40:08 +0000839 llvm::DIDescriptor RDContext =
840 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel06cceef2009-07-22 18:57:00 +0000841 llvm::DICompositeType RealDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000842 DebugFactory.CreateCompositeType(Tag, RDContext,
843 RD->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000844 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000845 llvm::DIType(), Elements,
846 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000847
848 // Now that we have a real decl for the struct, replace anything using the
849 // old decl with the new one. This will recursively update the debug info.
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000850 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000851
Chris Lattneraffb3732008-11-10 06:08:34 +0000852 return RealDecl;
853}
854
Devang Patelf4c205b2009-02-26 21:10:26 +0000855/// CreateType - get objective-c interface type.
856llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
857 llvm::DICompileUnit Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000858 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000859
Devang Patelf4c205b2009-02-26 21:10:26 +0000860 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000861 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelf4c205b2009-02-26 21:10:26 +0000862
863 // Get overall information about the record type for the debug info.
Devang Patel3efd1472010-02-01 21:52:22 +0000864 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation());
865 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000866 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
867
Mike Stump11289f42009-09-09 15:08:12 +0000868
Daniel Dunbarc61d0bd2009-05-18 20:51:58 +0000869 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000870
Devang Patelf4c205b2009-02-26 21:10:26 +0000871 // To handle recursive interface, we
872 // first generate a debug descriptor for the struct as a forward declaration.
873 // Then (if it is a definition) we go through and get debug info for all of
874 // its members. Finally, we create a descriptor for the complete type (which
875 // may refer to the forward decl if the struct is recursive) and replace all
876 // uses of the forward declaration with the final definition.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000877 llvm::DICompositeType FwdDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000878 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000879 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000880 llvm::DIType(), llvm::DIArray(),
881 RuntimeLang);
Mike Stump11289f42009-09-09 15:08:12 +0000882
Devang Patelf4c205b2009-02-26 21:10:26 +0000883 // If this is just a forward declaration, return it.
Devang Patel3efd1472010-02-01 21:52:22 +0000884 if (ID->isForwardDecl())
Devang Patelf4c205b2009-02-26 21:10:26 +0000885 return FwdDecl;
886
Devang Patel10909d52009-11-16 20:09:38 +0000887 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000888 // Otherwise, insert it into the TypeCache so that recursive uses will find
889 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000890 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000891
892 // Convert all the elements.
893 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
894
Devang Patel3efd1472010-02-01 21:52:22 +0000895 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +0000896 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +0000897 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000898 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000899 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000900 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattnerf216fd92009-05-05 05:05:36 +0000901 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +0000902 0 /* offset */, 0, SClassTy);
903 EltTys.push_back(InhTag);
904 }
905
Devang Patel3efd1472010-02-01 21:52:22 +0000906 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +0000907
908 unsigned FieldNo = 0;
Devang Patel3efd1472010-02-01 21:52:22 +0000909 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
910 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +0000911 ObjCIvarDecl *Field = *I;
912 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
913
Devang Patel58bf6e12009-11-25 17:37:31 +0000914 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +0000915
Devang Pateldf348f12009-04-27 22:40:36 +0000916 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +0000917 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +0000918 continue;
919
Devang Patelf4c205b2009-02-26 21:10:26 +0000920 // Get the location for the field.
921 SourceLocation FieldDefLoc = Field->getLocation();
922 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel12f0dea2009-04-17 21:35:15 +0000923 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
924 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
925
Mike Stump11289f42009-09-09 15:08:12 +0000926
Devang Patel9f804932009-03-20 18:24:39 +0000927 QualType FType = Field->getType();
928 uint64_t FieldSize = 0;
929 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +0000930
Devang Patel9f804932009-03-20 18:24:39 +0000931 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000932
Devang Patel9f804932009-03-20 18:24:39 +0000933 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000934 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000935 Expr *BitWidth = Field->getBitWidth();
936 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000937 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000938
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000939 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000940 }
941
Mike Stump11289f42009-09-09 15:08:12 +0000942 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
943
Devang Patelec4bad52009-03-19 00:23:53 +0000944 unsigned Flags = 0;
945 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
946 Flags = llvm::DIType::FlagProtected;
947 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
948 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +0000949
Devang Patelf4c205b2009-02-26 21:10:26 +0000950 // Create a DW_TAG_member node to remember the offset of this field in the
951 // struct. FIXME: This is an absolutely insane way to capture this
952 // information. When we gut debug info, this should be fixed.
953 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
954 FieldName, FieldDefUnit,
955 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +0000956 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +0000957 EltTys.push_back(FieldTy);
958 }
Mike Stump11289f42009-09-09 15:08:12 +0000959
Devang Patelf4c205b2009-02-26 21:10:26 +0000960 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +0000961 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +0000962
963 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000964 uint64_t Size = CGM.getContext().getTypeSize(Ty);
965 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000967 llvm::DICompositeType RealDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000968 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +0000969 Line, Size, Align, 0, 0, llvm::DIType(),
970 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +0000971
972 // Now that we have a real decl for the struct, replace anything using the
973 // old decl with the new one. This will recursively update the debug info.
Devang Patel10909d52009-11-16 20:09:38 +0000974 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000975
Devang Patelf4c205b2009-02-26 21:10:26 +0000976 return RealDecl;
977}
978
Chris Lattneraffb3732008-11-10 06:08:34 +0000979llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
980 llvm::DICompileUnit Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000981 EnumDecl *ED = Ty->getDecl();
Chris Lattneraffb3732008-11-10 06:08:34 +0000982
983 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
984
985 // Create DIEnumerator elements for each enumerator.
Mike Stump11289f42009-09-09 15:08:12 +0000986 for (EnumDecl::enumerator_iterator
Devang Patel3efd1472010-02-01 21:52:22 +0000987 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000988 Enum != EnumEnd; ++Enum) {
Devang Patel58bf6e12009-11-25 17:37:31 +0000989 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor91f84212008-12-11 16:49:14 +0000990 Enum->getInitVal().getZExtValue()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000991 }
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattneraffb3732008-11-10 06:08:34 +0000993 // Return a CompositeType for the enum itself.
994 llvm::DIArray EltArray =
Jay Foad7d0479f2009-05-21 09:52:38 +0000995 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000996
Devang Patel3efd1472010-02-01 21:52:22 +0000997 SourceLocation DefLoc = ED->getLocation();
Chris Lattneraffb3732008-11-10 06:08:34 +0000998 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000999 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001000 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1001 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1002
Mike Stump11289f42009-09-09 15:08:12 +00001003
Chris Lattneraffb3732008-11-10 06:08:34 +00001004 // Size and align of the type.
Eli Friedman2ad7e172009-05-04 04:39:55 +00001005 uint64_t Size = 0;
1006 unsigned Align = 0;
1007 if (!Ty->isIncompleteType()) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001008 Size = CGM.getContext().getTypeSize(Ty);
1009 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman2ad7e172009-05-04 04:39:55 +00001010 }
Mike Stump11289f42009-09-09 15:08:12 +00001011
Devang Patele21912d2009-10-20 19:55:01 +00001012 llvm::DIType DbgTy =
1013 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel3efd1472010-02-01 21:52:22 +00001014 Unit, ED->getName(), DefUnit, Line,
Devang Patele21912d2009-10-20 19:55:01 +00001015 Size, Align, 0, 0,
1016 llvm::DIType(), EltArray);
Devang Patele21912d2009-10-20 19:55:01 +00001017 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001018}
1019
1020llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1021 llvm::DICompileUnit Unit) {
1022 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1023 return CreateType(RT, Unit);
1024 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1025 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001026
Chris Lattneraffb3732008-11-10 06:08:34 +00001027 return llvm::DIType();
1028}
1029
1030llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1031 llvm::DICompileUnit Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001032 uint64_t Size;
1033 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001034
1035
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001036 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001037 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001038 Size = 0;
1039 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001040 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001041 } else if (Ty->isIncompleteArrayType()) {
1042 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001043 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001044 } else {
1045 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001046 Size = CGM.getContext().getTypeSize(Ty);
1047 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattneraffb3732008-11-10 06:08:34 +00001050 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1051 // interior arrays, do we care? Why aren't nested arrays represented the
1052 // obvious/recursive way?
1053 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1054 QualType EltTy(Ty, 0);
1055 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001056 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001057 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001058 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001059 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001060 // FIXME: Verify this is right for VLAs.
1061 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1062 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattneraffb3732008-11-10 06:08:34 +00001065 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001066 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001067
Devang Patele21912d2009-10-20 19:55:01 +00001068 llvm::DIType DbgTy =
1069 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1070 Unit, "", llvm::DICompileUnit(),
1071 0, Size, Align, 0, 0,
1072 getOrCreateType(EltTy, Unit),
1073 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001074 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001075}
1076
Anders Carlsson443f6772009-11-06 19:19:55 +00001077llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1078 llvm::DICompileUnit Unit) {
1079 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1080 Ty, Ty->getPointeeType(), Unit);
1081}
Chris Lattneraffb3732008-11-10 06:08:34 +00001082
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001083llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1084 llvm::DICompileUnit U) {
1085 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1086 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1087
1088 if (!Ty->getPointeeType()->isFunctionType()) {
1089 // We have a data member pointer type.
1090 return PointerDiffDITy;
1091 }
1092
1093 // We have a member function pointer type. Treat it as a struct with two
1094 // ptrdiff_t members.
1095 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1096
1097 uint64_t FieldOffset = 0;
1098 llvm::DIDescriptor ElementTypes[2];
1099
1100 // FIXME: This should probably be a function type instead.
1101 ElementTypes[0] =
1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1103 "ptr", llvm::DICompileUnit(), 0,
1104 Info.first, Info.second, FieldOffset, 0,
1105 PointerDiffDITy);
1106 FieldOffset += Info.first;
1107
1108 ElementTypes[1] =
1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1110 "ptr", llvm::DICompileUnit(), 0,
1111 Info.first, Info.second, FieldOffset, 0,
1112 PointerDiffDITy);
1113
1114 llvm::DIArray Elements =
1115 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1116 llvm::array_lengthof(ElementTypes));
1117
1118 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1119 U, llvm::StringRef("test"),
1120 llvm::DICompileUnit(), 0, FieldOffset,
1121 0, 0, 0, llvm::DIType(), Elements);
1122}
1123
Douglas Gregor0f139a12009-12-21 20:18:30 +00001124static QualType UnwrapTypeForDebugInfo(QualType T) {
1125 do {
1126 QualType LastT = T;
1127 switch (T->getTypeClass()) {
1128 default:
1129 return T;
1130 case Type::TemplateSpecialization:
1131 T = cast<TemplateSpecializationType>(T)->desugar();
1132 break;
1133 case Type::TypeOfExpr: {
1134 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1135 T = Ty->getUnderlyingExpr()->getType();
1136 break;
1137 }
1138 case Type::TypeOf:
1139 T = cast<TypeOfType>(T)->getUnderlyingType();
1140 break;
1141 case Type::Decltype:
1142 T = cast<DecltypeType>(T)->getUnderlyingType();
1143 break;
1144 case Type::QualifiedName:
1145 T = cast<QualifiedNameType>(T)->getNamedType();
1146 break;
1147 case Type::SubstTemplateTypeParm:
1148 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1149 break;
1150 case Type::Elaborated:
1151 T = cast<ElaboratedType>(T)->getUnderlyingType();
1152 break;
1153 }
1154
1155 assert(T != LastT && "Type unwrapping failed to unwrap!");
1156 if (T == LastT)
1157 return T;
1158 } while (true);
1159
1160 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001161}
1162
Sanjiv Gupta98070572008-05-25 05:15:42 +00001163/// getOrCreateType - Get the type from the cache or create a new
1164/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001165llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1166 llvm::DICompileUnit Unit) {
1167 if (Ty.isNull())
1168 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregor0f139a12009-12-21 20:18:30 +00001170 // Unwrap the type as needed for debug information.
1171 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001172
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001173 // Check for existing entry.
Daniel Dunbar99961382009-09-19 20:17:48 +00001174 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001175 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001176 if (it != TypeCache.end()) {
1177 // Verify that the debug info still exists.
1178 if (&*it->second)
1179 return llvm::DIType(cast<llvm::MDNode>(it->second));
1180 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001181
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001182 // Otherwise create the type.
1183 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001184
1185 // And update the type cache.
1186 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001187 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001188}
1189
Anders Carlsson6037e782009-11-14 20:52:05 +00001190/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001191llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1192 llvm::DICompileUnit Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001193 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001194 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001195 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001196
Douglas Gregor0915b432009-12-21 19:57:21 +00001197 const char *Diag = 0;
1198
Sanjiv Gupta98070572008-05-25 05:15:42 +00001199 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001200 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001201#define TYPE(Class, Base)
1202#define ABSTRACT_TYPE(Class, Base)
1203#define NON_CANONICAL_TYPE(Class, Base)
1204#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1205#include "clang/AST/TypeNodes.def"
1206 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001207
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001208 // FIXME: Handle these.
1209 case Type::ExtVector:
1210 case Type::Vector:
1211 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001212
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001213 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001214 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001215 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001216 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1217 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1218 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1219 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001220 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001221 return CreateType(cast<BlockPointerType>(Ty), Unit);
1222 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001223 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001224 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001225 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001226 case Type::FunctionProto:
1227 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001228 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001229 case Type::ConstantArray:
1230 case Type::VariableArray:
1231 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001232 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001233
1234 case Type::LValueReference:
1235 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1236
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001237 case Type::MemberPointer:
1238 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001239
1240 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001241 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001242 case Type::QualifiedName:
Douglas Gregor0915b432009-12-21 19:57:21 +00001243 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001244 case Type::TypeOfExpr:
1245 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001246 case Type::Decltype:
1247 llvm_unreachable("type should have been unwrapped!");
1248 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001249
1250 case Type::RValueReference:
1251 // FIXME: Implement!
1252 Diag = "rvalue references";
1253 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001254 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001255
1256 assert(Diag && "Fall through without a diagnostic?");
1257 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1258 "debug information for %0 is not yet supported");
1259 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1260 << Diag;
1261 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001262}
1263
1264/// EmitFunctionStart - Constructs the debug code for entering a function -
1265/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001266void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001267 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001268 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001269
Devang Patel934661e2010-01-14 00:36:21 +00001270 llvm::StringRef Name;
1271 llvm::StringRef LinkageName;
1272
1273 const Decl *D = GD.getDecl();
1274 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001275 // If there is a DISubprogram for this function available then use it.
1276 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1277 FI = SPCache.find(FD);
1278 if (FI != SPCache.end()) {
1279 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1280 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1281 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001282 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel7a12ad02010-01-19 01:54:44 +00001283 return;
1284 }
1285 }
Devang Patel934661e2010-01-14 00:36:21 +00001286 Name = getFunctionName(FD);
Eli Friedman8fdc2cb2010-01-16 00:43:13 +00001287 if (!Name.empty() && Name[0] == '\01')
Devang Patel2d630102010-01-14 21:46:57 +00001288 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001289 // Use mangled name as linkage name for c/c++ functions.
Devang Patel2d630102010-01-14 21:46:57 +00001290 LinkageName = CGM.getMangledName(GD);
Devang Patel934661e2010-01-14 00:36:21 +00001291 } else {
1292 // Use llvm function name as linkage name.
1293 Name = Fn->getName();
Devang Patel934661e2010-01-14 00:36:21 +00001294 LinkageName = Name;
Devang Patel757daca2010-01-19 00:25:12 +00001295 if (!Name.empty() && Name[0] == '\01')
1296 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Devang Patelea84f592010-01-14 18:06:13 +00001299 // It is expected that CurLoc is set before using EmitFunctionStart.
1300 // Usually, CurLoc points to the left bracket location of compound
1301 // statement representing function body.
Chris Lattneraffb3732008-11-10 06:08:34 +00001302 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001303 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel2ac33a02009-04-08 19:47:04 +00001304 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump11289f42009-09-09 15:08:12 +00001305
Chris Lattneraffb3732008-11-10 06:08:34 +00001306 llvm::DISubprogram SP =
Devang Patel9be7b202009-07-14 21:31:22 +00001307 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001308 getOrCreateType(FnType, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001309 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump11289f42009-09-09 15:08:12 +00001310
Sanjiv Gupta98070572008-05-25 05:15:42 +00001311 // Push function on region stack.
Devang Patelb40f2952009-11-13 19:10:24 +00001312 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001313 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta98070572008-05-25 05:15:42 +00001314}
1315
1316
Chris Lattneraffb3732008-11-10 06:08:34 +00001317void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001318 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001320 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001321 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001322 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001323 || (SM.getInstantiationLineNumber(CurLoc) ==
1324 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001325 && SM.isFromSameFile(CurLoc, PrevLoc)))
1326 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001327
1328 // Update last state.
1329 PrevLoc = CurLoc;
1330
1331 // Get the appropriate compile unit.
Chris Lattneraffb3732008-11-10 06:08:34 +00001332 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel2ac33a02009-04-08 19:47:04 +00001333 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel5d90d622009-10-06 18:36:08 +00001334
Devang Patelb40f2952009-11-13 19:10:24 +00001335 llvm::DIDescriptor DR(RegionStack.back());
Devang Patel5d90d622009-10-06 18:36:08 +00001336 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1337 llvm::DILocation DO(NULL);
1338 llvm::DILocation DL =
1339 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1340 DS, DO);
1341 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001342}
1343
1344/// EmitRegionStart- Constructs the debug code for entering a declarative
1345/// region - "llvm.dbg.region.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +00001346void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001347 llvm::DIDescriptor D =
1348 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1349 llvm::DIDescriptor() :
1350 llvm::DIDescriptor(RegionStack.back()));
1351 RegionStack.push_back(D.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001352}
1353
1354/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1355/// region - "llvm.dbg.region.end."
Chris Lattneraffb3732008-11-10 06:08:34 +00001356void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001357 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1358
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001359 // Provide an region stop point.
1360 EmitStopPoint(Fn, Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001361
Sanjiv Gupta98070572008-05-25 05:15:42 +00001362 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001363}
1364
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001365/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001366void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattneraffb3732008-11-10 06:08:34 +00001367 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001368 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1369
Devang Patelafc1c1d2009-03-27 23:16:32 +00001370 // Do not emit variable debug information while generating optimized code.
1371 // The llvm optimizer and code generator are not yet ready to support
1372 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001373 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001374 if (CGO.OptimizationLevel)
Devang Patelafc1c1d2009-03-27 23:16:32 +00001375 return;
1376
Devang Patel1c0954c2010-02-01 21:39:52 +00001377 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1378 QualType Type = VD->getType();
Mike Stump2114d7c2009-09-22 02:12:52 +00001379 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Patel1c0954c2010-02-01 21:39:52 +00001380 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump2114d7c2009-09-22 02:12:52 +00001381 llvm::DICompileUnit DefUnit;
1382 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1383
1384 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1385
1386 llvm::DIType FieldTy;
1387
1388 QualType FType;
1389 uint64_t FieldSize, FieldOffset;
1390 unsigned FieldAlign;
1391
1392 llvm::DIArray Elements;
1393 llvm::DIType EltTy;
1394
1395 // Build up structure for the byref. See BuildByRefType.
1396 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001397 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001398 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001399 FieldSize = CGM.getContext().getTypeSize(FType);
1400 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001401 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1402 "__isa", DefUnit,
1403 0, FieldSize, FieldAlign,
1404 FieldOffset, 0, FieldTy);
1405 EltTys.push_back(FieldTy);
1406 FieldOffset += FieldSize;
1407
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001408 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001409 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001410 FieldSize = CGM.getContext().getTypeSize(FType);
1411 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001412 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1413 "__forwarding", DefUnit,
1414 0, FieldSize, FieldAlign,
1415 FieldOffset, 0, FieldTy);
1416 EltTys.push_back(FieldTy);
1417 FieldOffset += FieldSize;
1418
Anders Carlsson88ea2452009-12-29 07:07:36 +00001419 FType = CGM.getContext().IntTy;
Mike Stump2114d7c2009-09-22 02:12:52 +00001420 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001421 FieldSize = CGM.getContext().getTypeSize(FType);
1422 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001423 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1424 "__flags", DefUnit,
1425 0, FieldSize, FieldAlign,
1426 FieldOffset, 0, FieldTy);
1427 EltTys.push_back(FieldTy);
1428 FieldOffset += FieldSize;
1429
Anders Carlsson88ea2452009-12-29 07:07:36 +00001430 FType = CGM.getContext().IntTy;
Mike Stump2114d7c2009-09-22 02:12:52 +00001431 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001432 FieldSize = CGM.getContext().getTypeSize(FType);
1433 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001434 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1435 "__size", DefUnit,
1436 0, FieldSize, FieldAlign,
1437 FieldOffset, 0, FieldTy);
1438 EltTys.push_back(FieldTy);
1439 FieldOffset += FieldSize;
1440
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001441 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump2114d7c2009-09-22 02:12:52 +00001442 if (HasCopyAndDispose) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001443 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001444 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001445 FieldSize = CGM.getContext().getTypeSize(FType);
1446 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001447 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1448 "__copy_helper", DefUnit,
1449 0, FieldSize, FieldAlign,
1450 FieldOffset, 0, FieldTy);
1451 EltTys.push_back(FieldTy);
1452 FieldOffset += FieldSize;
1453
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001454 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001455 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001456 FieldSize = CGM.getContext().getTypeSize(FType);
1457 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001458 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1459 "__destroy_helper", DefUnit,
1460 0, FieldSize, FieldAlign,
1461 FieldOffset, 0, FieldTy);
1462 EltTys.push_back(FieldTy);
1463 FieldOffset += FieldSize;
1464 }
1465
Devang Patel1c0954c2010-02-01 21:39:52 +00001466 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck160146e2010-01-27 17:10:57 +00001467 if (Align > CharUnits::fromQuantity(
1468 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump2114d7c2009-09-22 02:12:52 +00001469 unsigned AlignedOffsetInBytes
Ken Dyck160146e2010-01-27 17:10:57 +00001470 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump2114d7c2009-09-22 02:12:52 +00001471 unsigned NumPaddingBytes
Mike Stump207c680f2009-09-22 02:44:17 +00001472 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001473
1474 if (NumPaddingBytes > 0) {
1475 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001476 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump2114d7c2009-09-22 02:12:52 +00001477 pad, ArrayType::Normal, 0);
1478 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001479 FieldSize = CGM.getContext().getTypeSize(FType);
1480 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001481 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1482 Unit, "", DefUnit,
1483 0, FieldSize, FieldAlign,
1484 FieldOffset, 0, FieldTy);
1485 EltTys.push_back(FieldTy);
1486 FieldOffset += FieldSize;
1487 }
1488 }
1489
1490 FType = Type;
1491 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001492 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck160146e2010-01-27 17:10:57 +00001493 FieldAlign = Align.getQuantity()*8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001494
1495 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel1c0954c2010-02-01 21:39:52 +00001496 VD->getName(), DefUnit,
Mike Stump2114d7c2009-09-22 02:12:52 +00001497 0, FieldSize, FieldAlign,
1498 FieldOffset, 0, FieldTy);
1499 EltTys.push_back(FieldTy);
1500 FieldOffset += FieldSize;
1501
1502 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1503
1504 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1505
1506 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1507 llvm::DICompileUnit(),
1508 0, FieldOffset, 0, 0, Flags,
1509 llvm::DIType(), Elements);
1510 }
Chris Lattner362d8ae2009-05-05 04:57:08 +00001511
Chris Lattneraffb3732008-11-10 06:08:34 +00001512 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001513 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel1c0954c2010-02-01 21:39:52 +00001514 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner362d8ae2009-05-05 04:57:08 +00001515 unsigned Line = 0;
Eli Friedmanb05d0822009-11-16 20:33:31 +00001516 unsigned Column = 0;
Devang Patel1b5330a2010-02-10 01:09:50 +00001517 if (PLoc.isInvalid())
1518 PLoc = SM.getPresumedLoc(CurLoc);
1519 if (PLoc.isValid()) {
Chris Lattner362d8ae2009-05-05 04:57:08 +00001520 Line = PLoc.getLine();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001521 Column = PLoc.getColumn();
Devang Patel1b5330a2010-02-10 01:09:50 +00001522 Unit = getOrCreateCompileUnit(CurLoc);
Eli Friedmanb05d0822009-11-16 20:33:31 +00001523 } else {
Chris Lattner362d8ae2009-05-05 04:57:08 +00001524 Unit = llvm::DICompileUnit();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001525 }
Mike Stump11289f42009-09-09 15:08:12 +00001526
Chris Lattneraffb3732008-11-10 06:08:34 +00001527 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001528 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001529 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel1c0954c2010-02-01 21:39:52 +00001530 VD->getName(),
Chris Lattner362d8ae2009-05-05 04:57:08 +00001531 Unit, Line, Ty);
Chris Lattneraffb3732008-11-10 06:08:34 +00001532 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001533 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001534 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001535
Devang Patelb40f2952009-11-13 19:10:24 +00001536 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001537 llvm::DILocation DO(NULL);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001538 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1539
Chris Lattner9f021fd2009-12-28 23:41:39 +00001540 Call->setMetadata("dbg", DL.getNode());
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001541}
1542
Mike Stump2e722b92009-09-30 02:43:10 +00001543/// EmitDeclare - Emit local variable declaration debug info.
1544void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1545 llvm::Value *Storage, CGBuilderTy &Builder,
1546 CodeGenFunction *CGF) {
Devang Patel3efd1472010-02-01 21:52:22 +00001547 const ValueDecl *VD = BDRE->getDecl();
Mike Stump2e722b92009-09-30 02:43:10 +00001548 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1549
1550 // Do not emit variable debug information while generating optimized code.
1551 // The llvm optimizer and code generator are not yet ready to support
1552 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001553 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001554 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001555 return;
1556
1557 uint64_t XOffset = 0;
Devang Patel3efd1472010-02-01 21:52:22 +00001558 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1559 QualType Type = VD->getType();
Mike Stump2e722b92009-09-30 02:43:10 +00001560 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Patel3efd1472010-02-01 21:52:22 +00001561 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump2e722b92009-09-30 02:43:10 +00001562 llvm::DICompileUnit DefUnit;
1563 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1564
1565 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1566
1567 llvm::DIType FieldTy;
1568
1569 QualType FType;
1570 uint64_t FieldSize, FieldOffset;
1571 unsigned FieldAlign;
1572
1573 llvm::DIArray Elements;
1574 llvm::DIType EltTy;
1575
1576 // Build up structure for the byref. See BuildByRefType.
1577 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001578 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001579 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001580 FieldSize = CGM.getContext().getTypeSize(FType);
1581 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001582 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1583 "__isa", DefUnit,
1584 0, FieldSize, FieldAlign,
1585 FieldOffset, 0, FieldTy);
1586 EltTys.push_back(FieldTy);
1587 FieldOffset += FieldSize;
1588
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001589 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001590 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001591 FieldSize = CGM.getContext().getTypeSize(FType);
1592 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001593 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1594 "__forwarding", DefUnit,
1595 0, FieldSize, FieldAlign,
1596 FieldOffset, 0, FieldTy);
1597 EltTys.push_back(FieldTy);
1598 FieldOffset += FieldSize;
1599
Anders Carlsson88ea2452009-12-29 07:07:36 +00001600 FType = CGM.getContext().IntTy;
Mike Stump2e722b92009-09-30 02:43:10 +00001601 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001602 FieldSize = CGM.getContext().getTypeSize(FType);
1603 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001604 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1605 "__flags", DefUnit,
1606 0, FieldSize, FieldAlign,
1607 FieldOffset, 0, FieldTy);
1608 EltTys.push_back(FieldTy);
1609 FieldOffset += FieldSize;
1610
Anders Carlsson88ea2452009-12-29 07:07:36 +00001611 FType = CGM.getContext().IntTy;
Mike Stump2e722b92009-09-30 02:43:10 +00001612 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001613 FieldSize = CGM.getContext().getTypeSize(FType);
1614 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001615 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1616 "__size", DefUnit,
1617 0, FieldSize, FieldAlign,
1618 FieldOffset, 0, FieldTy);
1619 EltTys.push_back(FieldTy);
1620 FieldOffset += FieldSize;
1621
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001622 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump2e722b92009-09-30 02:43:10 +00001623 if (HasCopyAndDispose) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001624 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001625 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001626 FieldSize = CGM.getContext().getTypeSize(FType);
1627 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001628 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1629 "__copy_helper", DefUnit,
1630 0, FieldSize, FieldAlign,
1631 FieldOffset, 0, FieldTy);
1632 EltTys.push_back(FieldTy);
1633 FieldOffset += FieldSize;
1634
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001635 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001636 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001637 FieldSize = CGM.getContext().getTypeSize(FType);
1638 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001639 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1640 "__destroy_helper", DefUnit,
1641 0, FieldSize, FieldAlign,
1642 FieldOffset, 0, FieldTy);
1643 EltTys.push_back(FieldTy);
1644 FieldOffset += FieldSize;
1645 }
1646
Devang Patel3efd1472010-02-01 21:52:22 +00001647 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck160146e2010-01-27 17:10:57 +00001648 if (Align > CharUnits::fromQuantity(
1649 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump2e722b92009-09-30 02:43:10 +00001650 unsigned AlignedOffsetInBytes
Ken Dyck160146e2010-01-27 17:10:57 +00001651 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump2e722b92009-09-30 02:43:10 +00001652 unsigned NumPaddingBytes
1653 = AlignedOffsetInBytes - FieldOffset/8;
1654
1655 if (NumPaddingBytes > 0) {
1656 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001657 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump2e722b92009-09-30 02:43:10 +00001658 pad, ArrayType::Normal, 0);
1659 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001660 FieldSize = CGM.getContext().getTypeSize(FType);
1661 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001662 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1663 Unit, "", DefUnit,
1664 0, FieldSize, FieldAlign,
1665 FieldOffset, 0, FieldTy);
1666 EltTys.push_back(FieldTy);
1667 FieldOffset += FieldSize;
1668 }
1669 }
1670
1671 FType = Type;
1672 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001673 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck160146e2010-01-27 17:10:57 +00001674 FieldAlign = Align.getQuantity()*8;
Mike Stump2e722b92009-09-30 02:43:10 +00001675
1676 XOffset = FieldOffset;
1677 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel3efd1472010-02-01 21:52:22 +00001678 VD->getName(), DefUnit,
Mike Stump2e722b92009-09-30 02:43:10 +00001679 0, FieldSize, FieldAlign,
1680 FieldOffset, 0, FieldTy);
1681 EltTys.push_back(FieldTy);
1682 FieldOffset += FieldSize;
1683
1684 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1685
1686 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1687
1688 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1689 llvm::DICompileUnit(),
1690 0, FieldOffset, 0, 0, Flags,
1691 llvm::DIType(), Elements);
1692 }
1693
1694 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001695 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel3efd1472010-02-01 21:52:22 +00001696 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00001697 unsigned Line = 0;
1698 if (!PLoc.isInvalid())
1699 Line = PLoc.getLine();
1700 else
1701 Unit = llvm::DICompileUnit();
1702
Devang Patel3efd1472010-02-01 21:52:22 +00001703 CharUnits offset = CGF->BlockDecls[VD];
Mike Stump2e722b92009-09-30 02:43:10 +00001704 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001705 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1706 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1707 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1708 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001709 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001710 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1711 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001712 // offset of __forwarding field
1713 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001714 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1715 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1716 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001717 // offset of x field
1718 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001719 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001720 }
1721
1722 // Create the descriptor for the variable.
1723 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001724 DebugFactory.CreateComplexVariable(Tag,
1725 llvm::DIDescriptor(RegionStack.back()),
Devang Patel3efd1472010-02-01 21:52:22 +00001726 VD->getName(), Unit, Line, Ty,
Mike Stump2e722b92009-09-30 02:43:10 +00001727 addr);
1728 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001729 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001730 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001731
Devang Patelb40f2952009-11-13 19:10:24 +00001732 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001733 llvm::DILocation DO(NULL);
1734 llvm::DILocation DL =
1735 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001736
Chris Lattner9f021fd2009-12-28 23:41:39 +00001737 Call->setMetadata("dbg", DL.getNode());
Mike Stump2e722b92009-09-30 02:43:10 +00001738}
1739
Devang Patel3efd1472010-02-01 21:52:22 +00001740void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattneraffb3732008-11-10 06:08:34 +00001741 llvm::Value *Storage,
1742 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001743 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001744}
1745
Mike Stump2e722b92009-09-30 02:43:10 +00001746void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1747 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1748 CodeGenFunction *CGF) {
1749 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1750}
1751
Chris Lattneraffb3732008-11-10 06:08:34 +00001752/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1753/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00001754void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattneraffb3732008-11-10 06:08:34 +00001755 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001756 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001757}
1758
1759
1760
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001761/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001762void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00001763 const VarDecl *D) {
1764
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001765 // Create global variable debug descriptor.
Devang Patel7b7f46f2010-02-01 21:34:11 +00001766 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001767 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001768 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +00001769 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner86d7d912008-11-24 03:54:41 +00001770
Devang Patel7b7f46f2010-02-01 21:34:11 +00001771 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001772 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001773
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001774 // CodeGen turns int[] into int[1] so we'll do the same here.
1775 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001776
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001777 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001778 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001779
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001780 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001781 ArrayType::Normal, 0);
1782 }
Devang Patel7b7f46f2010-02-01 21:34:11 +00001783 llvm::StringRef DeclName = D->getName();
1784 llvm::DIDescriptor DContext =
1785 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1786 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel7bfc5962010-01-28 23:15:27 +00001787 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001788 getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001789 Var->hasInternalLinkage(),
1790 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001791}
1792
Devang Patelf4c205b2009-02-26 21:10:26 +00001793/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001794void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00001795 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001796 // Create global variable debug descriptor.
Devang Patel3efd1472010-02-01 21:52:22 +00001797 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001798 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel3efd1472010-02-01 21:52:22 +00001799 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +00001800 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patelf4c205b2009-02-26 21:10:26 +00001801
Devang Patel3efd1472010-02-01 21:52:22 +00001802 llvm::StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001803
Devang Patel3efd1472010-02-01 21:52:22 +00001804 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001805 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001806
Devang Patelf4c205b2009-02-26 21:10:26 +00001807 // CodeGen turns int[] into int[1] so we'll do the same here.
1808 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001809
Devang Patelf4c205b2009-02-26 21:10:26 +00001810 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001811 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001812
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001813 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001814 ArrayType::Normal, 0);
1815 }
1816
Devang Patele4f2b2a2009-10-20 18:26:30 +00001817 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001818 getOrCreateType(T, Unit),
1819 Var->hasInternalLinkage(),
1820 true/*definition*/, Var);
1821}
Devang Patel973f2eb2010-02-01 19:16:32 +00001822
1823/// getOrCreateNamesSpace - Return namespace descriptor for the given
1824/// namespace decl.
1825llvm::DINameSpace
1826CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1827 llvm::DIDescriptor Unit) {
1828 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1829 NameSpaceCache.find(NSDecl);
1830 if (I != NameSpaceCache.end())
1831 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1832
1833 SourceManager &SM = CGM.getContext().getSourceManager();
1834 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1835 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1836
1837 llvm::DIDescriptor Context =
Devang Patel7b7f46f2010-02-01 21:34:11 +00001838 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patel973f2eb2010-02-01 19:16:32 +00001839 llvm::DINameSpace NS =
1840 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1841 llvm::DICompileUnit(Unit.getNode()), LineNo);
1842 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1843 return NS;
1844}