blob: c8d981870938448a1c0448b5b837714bbc819a4c [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel33583052010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-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));
62
63 // Check namespace.
64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
66
Devang Patel979ec2e2009-10-06 00:35:31 +000067 return CompileUnit;
68}
69
Devang Patel9c6c3a02010-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 Patel89f05f82010-01-28 18:21:00 +000083 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000084 memcpy(StrPtr, NS.data(), NS.length());
85 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000086}
87
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000088/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000089/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000090llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000091 // Get source file information.
92 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000094 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000095 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
97 FileName = PLoc.getFilename();
98 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000101 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +0000102 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +0000103 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000104
Devang Patel446c6192009-04-17 21:06:59 +0000105 // Get absolute path name.
106 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000107 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000108
Devang Patel72240d72009-06-26 18:32:22 +0000109 // See if thie compile unit is representing main source file. Each source
110 // file has corresponding compile unit. There is only one main source
111 // file at a time.
112 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000113 const LangOptions &LO = CGM.getLangOptions();
114 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000115 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000116 if (!CGO.MainFileName.empty()) {
117 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000118 isMain = true;
119 } else {
120 if (Loc.isValid() && SM.isFromMainFile(Loc))
121 isMain = true;
122 }
123 if (isMain)
124 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000125 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000126
Chris Lattner515455a2009-03-25 03:28:08 +0000127 unsigned LangTag;
128 if (LO.CPlusPlus) {
129 if (LO.ObjC1)
130 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
131 else
132 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
133 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000134 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000135 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000136 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000137 } else {
138 LangTag = llvm::dwarf::DW_LANG_C89;
139 }
Devang Patel446c6192009-04-17 21:06:59 +0000140
Benjamin Kramer47daf682009-12-08 11:02:29 +0000141 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000142#ifdef CLANG_VENDOR
143 CLANG_VENDOR
144#endif
145 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000146
147 // Figure out which version of the ObjC runtime we have.
148 unsigned RuntimeVers = 0;
149 if (LO.ObjC1)
150 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000152 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000153 return Unit = DebugFactory.CreateCompileUnit(
154 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
155 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000156}
157
Devang Patel65e99f22009-02-25 01:36:11 +0000158/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000159/// one if necessary.
160llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000161 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000162 unsigned Encoding = 0;
163 switch (BT->getKind()) {
164 default:
165 case BuiltinType::Void:
166 return llvm::DIType();
167 case BuiltinType::UChar:
168 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
169 case BuiltinType::Char_S:
170 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
171 case BuiltinType::UShort:
172 case BuiltinType::UInt:
173 case BuiltinType::ULong:
174 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
175 case BuiltinType::Short:
176 case BuiltinType::Int:
177 case BuiltinType::Long:
178 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
179 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
180 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000181 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000183 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000184 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000185 uint64_t Size = CGM.getContext().getTypeSize(BT);
186 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000187 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Devang Patelca80a5f2009-10-20 19:55:01 +0000189 llvm::DIType DbgTy =
190 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000191 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 Unit, 0, Size, Align,
193 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000194 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000195}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000196
Chris Lattnerb7003772009-04-23 06:13:01 +0000197llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
198 llvm::DICompileUnit Unit) {
199 // Bit size, align and offset of the type.
200 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
201 if (Ty->isComplexIntegerType())
202 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Anders Carlsson20f12a22009-12-06 18:00:51 +0000204 uint64_t Size = CGM.getContext().getTypeSize(Ty);
205 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000206 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Devang Patelca80a5f2009-10-20 19:55:01 +0000208 llvm::DIType DbgTy =
209 DebugFactory.CreateBasicType(Unit, "complex",
210 Unit, 0, Size, Align,
211 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000212 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000213}
214
John McCalla1805292009-09-25 01:40:47 +0000215/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000216/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000217llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
218 QualifierCollector Qc;
219 const Type *T = Qc.strip(Ty);
220
221 // Ignore these qualifiers for now.
222 Qc.removeObjCGCAttr();
223 Qc.removeAddressSpace();
224
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 // We will create one Derived type for one qualifier and recurse to handle any
226 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000228 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000230 Qc.removeConst();
231 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000232 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000233 Qc.removeVolatile();
234 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000236 Qc.removeRestrict();
237 } else {
238 assert(Qc.empty() && "Unknown type qualifier for debug info");
239 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
John McCalla1805292009-09-25 01:40:47 +0000242 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
243
Daniel Dunbar3845f862008-10-31 03:54:29 +0000244 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
245 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000246 llvm::DIType DbgTy =
247 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
248 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000249 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000250}
251
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000252llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
253 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000254 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000255 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
256 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000257 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000258}
259
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
261 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000262 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
263 Ty->getPointeeType(), Unit);
264}
265
266llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
267 const Type *Ty,
268 QualType PointeeTy,
269 llvm::DICompileUnit Unit) {
270 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000272 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000273
274 // Size is always the size of a pointer. We can't use getTypeSize here
275 // because that does not return the correct value for references.
276 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000277 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
278 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Devang Patelca80a5f2009-10-20 19:55:01 +0000280 return
Anders Carlssona031b352009-11-06 19:19:55 +0000281 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000282 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000283
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000284}
285
Mike Stump9bc093c2009-05-14 02:03:51 +0000286llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
287 llvm::DICompileUnit Unit) {
288 if (BlockLiteralGenericSet)
289 return BlockLiteralGeneric;
290
291 llvm::DICompileUnit DefUnit;
292 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
293
294 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
295
296 llvm::DIType FieldTy;
297
298 QualType FType;
299 uint64_t FieldSize, FieldOffset;
300 unsigned FieldAlign;
301
302 llvm::DIArray Elements;
303 llvm::DIType EltTy, DescTy;
304
305 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000306 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000307 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000308 FieldSize = CGM.getContext().getTypeSize(FType);
309 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000310 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
311 "reserved", DefUnit,
312 0, FieldSize, FieldAlign,
313 FieldOffset, 0, FieldTy);
314 EltTys.push_back(FieldTy);
315
316 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000317 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000318 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000319 FieldSize = CGM.getContext().getTypeSize(FType);
320 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
322 "Size", DefUnit,
323 0, FieldSize, FieldAlign,
324 FieldOffset, 0, FieldTy);
325 EltTys.push_back(FieldTy);
326
327 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000328 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000329 EltTys.clear();
330
Mike Stump3d363c52009-10-02 02:30:50 +0000331 unsigned Flags = llvm::DIType::FlagAppleBlock;
332
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000334 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Mike Stump9bc093c2009-05-14 02:03:51 +0000337 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000338 uint64_t Size = CGM.getContext().getTypeSize(Ty);
339 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
342 Unit, "", llvm::DICompileUnit(),
343 0, Size, Align, 0, 0, EltTy);
344
345 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000347 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000348 FieldSize = CGM.getContext().getTypeSize(FType);
349 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000350 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
351 "__isa", DefUnit,
352 0, FieldSize, FieldAlign,
353 FieldOffset, 0, FieldTy);
354 EltTys.push_back(FieldTy);
355
356 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000357 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000359 FieldSize = CGM.getContext().getTypeSize(FType);
360 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000361 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
362 "__flags", DefUnit,
363 0, FieldSize, FieldAlign,
364 FieldOffset, 0, FieldTy);
365 EltTys.push_back(FieldTy);
366
367 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000368 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000369 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000370 FieldSize = CGM.getContext().getTypeSize(FType);
371 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000372 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
373 "__reserved", DefUnit,
374 0, FieldSize, FieldAlign,
375 FieldOffset, 0, FieldTy);
376 EltTys.push_back(FieldTy);
377
378 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000381 FieldSize = CGM.getContext().getTypeSize(FType);
382 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000383 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
384 "__FuncPtr", DefUnit,
385 0, FieldSize, FieldAlign,
386 FieldOffset, 0, FieldTy);
387 EltTys.push_back(FieldTy);
388
389 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000390 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000391 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000392 FieldSize = CGM.getContext().getTypeSize(Ty);
393 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
395 "__descriptor", DefUnit,
396 0, FieldSize, FieldAlign,
397 FieldOffset, 0, FieldTy);
398 EltTys.push_back(FieldTy);
399
400 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000401 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000402
403 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000404 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000405 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Mike Stump9bc093c2009-05-14 02:03:51 +0000407 BlockLiteralGenericSet = true;
408 BlockLiteralGeneric
409 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
410 "", llvm::DICompileUnit(),
411 0, Size, Align, 0, 0, EltTy);
412 return BlockLiteralGeneric;
413}
414
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
416 llvm::DICompileUnit Unit) {
417 // Typedefs are derived from some other type. If we have a typedef of a
418 // typedef, make sure to emit the whole chain.
419 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421 // We don't set size information, but do specify where the typedef was
422 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000423 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000424 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000425 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000426
Devang Pateleb6d79b2010-02-01 21:34:11 +0000427 llvm::DIDescriptor TyContext
428 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
429 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000430 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000431 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000432 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000433 Ty->getDecl()->getName(), Unit,
434 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000435 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000436}
437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
439 llvm::DICompileUnit Unit) {
440 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000441
Chris Lattner9c85ba32008-11-10 06:08:34 +0000442 // Add the result type at least.
443 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 // Set up remainder of arguments if there is a prototype.
446 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000447 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000448 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
449 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
450 } else {
451 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000452 }
453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000455 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Devang Patelca80a5f2009-10-20 19:55:01 +0000457 llvm::DIType DbgTy =
458 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
459 Unit, "", llvm::DICompileUnit(),
460 0, 0, 0, 0, 0,
461 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000462 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463}
464
Devang Patel428deb52010-01-19 00:00:59 +0000465/// CollectRecordFields - A helper function to collect debug info for
466/// record fields. This is used while creating debug info entry for a Record.
467void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000468CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000469 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
470 unsigned FieldNo = 0;
471 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000472 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
473 for (RecordDecl::field_iterator I = RD->field_begin(),
474 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000475 I != E; ++I, ++FieldNo) {
476 FieldDecl *Field = *I;
477 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
478
479 llvm::StringRef FieldName = Field->getName();
480
481 // Ignore unnamed fields.
482 if (FieldName.empty())
483 continue;
484
485 // Get the location for the field.
486 SourceLocation FieldDefLoc = Field->getLocation();
487 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
488 llvm::DICompileUnit FieldDefUnit;
489 unsigned FieldLine = 0;
490
491 if (!PLoc.isInvalid()) {
492 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
493 FieldLine = PLoc.getLine();
494 }
495
496 QualType FType = Field->getType();
497 uint64_t FieldSize = 0;
498 unsigned FieldAlign = 0;
499 if (!FType->isIncompleteArrayType()) {
500
501 // Bit size, align and offset of the type.
502 FieldSize = CGM.getContext().getTypeSize(FType);
503 Expr *BitWidth = Field->getBitWidth();
504 if (BitWidth)
505 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
506
507 FieldAlign = CGM.getContext().getTypeAlign(FType);
508 }
509
510 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
511
512 // Create a DW_TAG_member node to remember the offset of this field in the
513 // struct. FIXME: This is an absolutely insane way to capture this
514 // information. When we gut debug info, this should be fixed.
515 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
516 FieldName, FieldDefUnit,
517 FieldLine, FieldSize, FieldAlign,
518 FieldOffset, 0, FieldTy);
519 EltTys.push_back(FieldTy);
520 }
521}
522
Devang Patela6da1922010-01-28 00:28:01 +0000523/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
524/// function type is not updated to include implicit "this" pointer. Use this
525/// routine to get a method type which includes "this" pointer.
526llvm::DIType
527CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
528 llvm::DICompileUnit Unit) {
529 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000530
531 // Static methods do not need "this" pointer argument.
532 if (Method->isStatic())
533 return FnTy;
534
Devang Patela6da1922010-01-28 00:28:01 +0000535 // Add "this" pointer.
536
537 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
538 assert (Args.getNumElements() && "Invalid number of arguments!");
539
540 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
541
542 // First element is always return type. For 'void' functions it is NULL.
543 Elts.push_back(Args.getElement(0));
544
545 // "this" pointer is always first argument.
546 ASTContext &Context = CGM.getContext();
547 QualType ThisPtr =
548 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
549 Elts.push_back(getOrCreateType(ThisPtr, Unit));
550
551 // Copy rest of the arguments.
552 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
553 Elts.push_back(Args.getElement(i));
554
555 llvm::DIArray EltTypeArray =
556 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
557
558 return
559 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
560 Unit, "", llvm::DICompileUnit(),
561 0, 0, 0, 0, 0,
562 llvm::DIType(), EltTypeArray);
563}
564
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000565/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
566/// a single member function GlobalDecl.
567llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000568CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000569 llvm::DICompileUnit Unit,
570 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571 bool IsCtorOrDtor =
572 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
573
574 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000575 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000576 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000577
578 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
579 // make sense to give a single ctor/dtor a linkage name.
580 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000581 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000582
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000583 SourceManager &SM = CGM.getContext().getSourceManager();
584
585 // Get the location for the method.
586 SourceLocation MethodDefLoc = Method->getLocation();
587 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
588 llvm::DICompileUnit MethodDefUnit;
589 unsigned MethodLine = 0;
590
591 if (!PLoc.isInvalid()) {
592 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
593 MethodLine = PLoc.getLine();
594 }
595
596 // Collect virtual method info.
597 llvm::DIType ContainingType;
598 unsigned Virtuality = 0;
599 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000600
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000601 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000602 if (Method->isPure())
603 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
604 else
605 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
606
607 // It doesn't make sense to give a virtual destructor a vtable index,
608 // since a single destructor has two entries in the vtable.
609 if (!isa<CXXDestructorDecl>(Method))
610 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000611 ContainingType = RecordTy;
612 }
613
614 llvm::DISubprogram SP =
615 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
616 MethodLinkageName,
617 MethodDefUnit, MethodLine,
618 MethodTy, /*isLocalToUnit=*/false,
619 Method->isThisDeclarationADefinition(),
620 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000621
622 // Don't cache ctors or dtors since we have to emit multiple functions for
623 // a single ctor or dtor.
624 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
625 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626
627 return SP;
628}
629
Devang Patel4125fd22010-01-19 01:54:44 +0000630/// CollectCXXMemberFunctions - A helper function to collect debug info for
631/// C++ member functions.This is used while creating debug info entry for
632/// a Record.
633void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000634CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000635 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
636 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000637 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
638 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000639 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000640
641 if (Method->isImplicit())
642 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000643
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000644 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000645 }
646}
647
Devang Patela245c5b2010-01-25 23:32:18 +0000648/// CollectCXXBases - A helper function to collect debug info for
649/// C++ base classes. This is used while creating debug info entry for
650/// a Record.
651void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000652CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000653 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
654 llvm::DICompositeType &RecordTy) {
655
Devang Patel239cec62010-02-01 21:39:52 +0000656 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
657 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
658 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000659 unsigned BFlags = 0;
660 uint64_t BaseOffset;
661
662 const CXXRecordDecl *Base =
663 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
664
665 if (BI->isVirtual()) {
666 BaseOffset = RL.getVBaseClassOffset(Base);
667 BFlags = llvm::DIType::FlagVirtual;
668 } else
669 BaseOffset = RL.getBaseClassOffset(Base);
670
671 AccessSpecifier Access = BI->getAccessSpecifier();
672 if (Access == clang::AS_private)
673 BFlags |= llvm::DIType::FlagPrivate;
674 else if (Access == clang::AS_protected)
675 BFlags |= llvm::DIType::FlagProtected;
676
677 llvm::DIType DTy =
678 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
679 RecordTy, llvm::StringRef(),
680 llvm::DICompileUnit(), 0, 0, 0,
681 BaseOffset, BFlags,
682 getOrCreateType(BI->getType(),
683 Unit));
684 EltTys.push_back(DTy);
685 }
Devang Patela245c5b2010-01-25 23:32:18 +0000686}
687
Devang Patel4ce3f202010-01-28 18:11:52 +0000688/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
689llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
690 if (!VTablePtrType.isNull())
691 return VTablePtrType;
692
693 ASTContext &Context = CGM.getContext();
694
695 /* Function type */
696 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
697 STys.push_back(getOrCreateType(Context.IntTy, Unit));
698 llvm::DIArray SElements =
699 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
700 llvm::DIType SubTy =
701 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
702 Unit, "", llvm::DICompileUnit(),
703 0, 0, 0, 0, 0, llvm::DIType(), SElements);
704
705 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
706 llvm::DIType vtbl_ptr_type
707 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
708 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
709 0, Size, 0, 0, 0, SubTy);
710
711 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
712 Unit, "", llvm::DICompileUnit(),
713 0, Size, 0, 0, 0, vtbl_ptr_type);
714 return VTablePtrType;
715}
716
717/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000718llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000719 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000720 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000721
722 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000723 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 memcpy(StrPtr, Name.data(), Name.length());
725 return llvm::StringRef(StrPtr, Name.length());
726}
727
728
729/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
730/// debug info entry in EltTys vector.
731void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000732CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000733 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000734 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000735
736 // If there is a primary base then it will hold vtable info.
737 if (RL.getPrimaryBase())
738 return;
739
740 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000741 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000742 return;
743
744 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
745 llvm::DIType VPTR
746 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +0000747 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000748 0, Size, 0, 0, 0,
749 getOrCreateVTablePtrType(Unit));
750 EltTys.push_back(VPTR);
751}
752
Devang Patel65e99f22009-02-25 01:36:11 +0000753/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
755 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000756 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758 unsigned Tag;
759 if (Decl->isStruct())
760 Tag = llvm::dwarf::DW_TAG_structure_type;
761 else if (Decl->isUnion())
762 Tag = llvm::dwarf::DW_TAG_union_type;
763 else {
764 assert(Decl->isClass() && "Unknown RecordType!");
765 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000766 }
767
Anders Carlsson20f12a22009-12-06 18:00:51 +0000768 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000769
Chris Lattner9c85ba32008-11-10 06:08:34 +0000770 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000771 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000772 llvm::DICompileUnit DefUnit;
773 unsigned Line = 0;
774 if (!PLoc.isInvalid()) {
775 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
776 Line = PLoc.getLine();
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 // Records and classes and unions can all be recursive. To handle them, we
780 // first generate a debug descriptor for the struct as a forward declaration.
781 // Then (if it is a definition) we go through and get debug info for all of
782 // its members. Finally, we create a descriptor for the complete type (which
783 // may refer to the forward decl if the struct is recursive) and replace all
784 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000785
786 // A Decl->getName() is not unique. However, the debug info descriptors
787 // are uniqued. The debug info descriptor describing record's context is
788 // necessary to keep two Decl's descriptor unique if their name match.
789 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
790 // use type's name in FwdDecl.
791 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000792 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000793 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000794 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Chris Lattner9c85ba32008-11-10 06:08:34 +0000797 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000798 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000800
Eli Friedman14d63652009-11-16 21:04:30 +0000801 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802 // Otherwise, insert it into the TypeCache so that recursive uses will find
803 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000804 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805
806 // Convert all the elements.
807 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
808
Devang Patel4ce3f202010-01-28 18:11:52 +0000809 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
Devang Patel3064afe2010-01-28 21:41:35 +0000810 if (CXXDecl) {
811 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000812 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000813 }
Devang Patel428deb52010-01-19 00:00:59 +0000814 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000815 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000816 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000817 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000818
819 // A class's primary base or the class itself contains the vtable.
820 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
821 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
822 ContainingType =
823 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
824 else if (CXXDecl->isDynamicClass())
825 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattner9c85ba32008-11-10 06:08:34 +0000828 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000829 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000830
831 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000832 uint64_t Size = CGM.getContext().getTypeSize(Ty);
833 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Devang Patel0ce73f62009-07-22 18:57:00 +0000835 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000836 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000837 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000838 llvm::DIType(), Elements,
839 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840
841 // Now that we have a real decl for the struct, replace anything using the
842 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000843 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000844
Chris Lattner9c85ba32008-11-10 06:08:34 +0000845 return RealDecl;
846}
847
Devang Patel9ca36b62009-02-26 21:10:26 +0000848/// CreateType - get objective-c interface type.
849llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
850 llvm::DICompileUnit Unit) {
851 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Devang Patel9ca36b62009-02-26 21:10:26 +0000853 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000854 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000855
856 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000857 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000858 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
859 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
860
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Daniel Dunbard86d3362009-05-18 20:51:58 +0000862 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000863
Devang Patel9ca36b62009-02-26 21:10:26 +0000864 // To handle recursive interface, we
865 // first generate a debug descriptor for the struct as a forward declaration.
866 // Then (if it is a definition) we go through and get debug info for all of
867 // its members. Finally, we create a descriptor for the complete type (which
868 // may refer to the forward decl if the struct is recursive) and replace all
869 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000870 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000871 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000872 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000873 llvm::DIType(), llvm::DIArray(),
874 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Devang Patel9ca36b62009-02-26 21:10:26 +0000876 // If this is just a forward declaration, return it.
877 if (Decl->isForwardDecl())
878 return FwdDecl;
879
Devang Patelffffb032009-11-16 20:09:38 +0000880 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000881 // Otherwise, insert it into the TypeCache so that recursive uses will find
882 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000883 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000884
885 // Convert all the elements.
886 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
887
Devang Patelfbe899f2009-03-10 21:30:26 +0000888 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
889 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000890 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000891 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000892 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000893 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000894 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000895 0 /* offset */, 0, SClassTy);
896 EltTys.push_back(InhTag);
897 }
898
Anders Carlsson20f12a22009-12-06 18:00:51 +0000899 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000900
901 unsigned FieldNo = 0;
902 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
903 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
904 ObjCIvarDecl *Field = *I;
905 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
906
Devang Patel73621622009-11-25 17:37:31 +0000907 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000908
Devang Patelde135022009-04-27 22:40:36 +0000909 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000910 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000911 continue;
912
Devang Patel9ca36b62009-02-26 21:10:26 +0000913 // Get the location for the field.
914 SourceLocation FieldDefLoc = Field->getLocation();
915 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000916 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
917 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
918
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Devang Patel99c20eb2009-03-20 18:24:39 +0000920 QualType FType = Field->getType();
921 uint64_t FieldSize = 0;
922 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000923
Devang Patel99c20eb2009-03-20 18:24:39 +0000924 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Devang Patel99c20eb2009-03-20 18:24:39 +0000926 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000927 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000928 Expr *BitWidth = Field->getBitWidth();
929 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000930 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000931
Anders Carlsson20f12a22009-12-06 18:00:51 +0000932 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000933 }
934
Mike Stump1eb44332009-09-09 15:08:12 +0000935 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
936
Devang Patelc20482b2009-03-19 00:23:53 +0000937 unsigned Flags = 0;
938 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
939 Flags = llvm::DIType::FlagProtected;
940 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
941 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Devang Patel9ca36b62009-02-26 21:10:26 +0000943 // Create a DW_TAG_member node to remember the offset of this field in the
944 // struct. FIXME: This is an absolutely insane way to capture this
945 // information. When we gut debug info, this should be fixed.
946 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
947 FieldName, FieldDefUnit,
948 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000949 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000950 EltTys.push_back(FieldTy);
951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Devang Patel9ca36b62009-02-26 21:10:26 +0000953 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000954 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000955
956 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000957 uint64_t Size = CGM.getContext().getTypeSize(Ty);
958 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Devang Patel6c1fddf2009-07-27 18:42:03 +0000960 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000961 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000962 Line, Size, Align, 0, 0, llvm::DIType(),
963 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000964
965 // Now that we have a real decl for the struct, replace anything using the
966 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000967 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000968
Devang Patel9ca36b62009-02-26 21:10:26 +0000969 return RealDecl;
970}
971
Chris Lattner9c85ba32008-11-10 06:08:34 +0000972llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
973 llvm::DICompileUnit Unit) {
974 EnumDecl *Decl = Ty->getDecl();
975
976 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
977
978 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000979 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000980 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000981 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000982 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000983 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986 // Return a CompositeType for the enum itself.
987 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000988 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000989
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990 SourceLocation DefLoc = Decl->getLocation();
991 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000992 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000993 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
994 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
995
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner9c85ba32008-11-10 06:08:34 +0000997 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000998 uint64_t Size = 0;
999 unsigned Align = 0;
1000 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001001 Size = CGM.getContext().getTypeSize(Ty);
1002 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Devang Patelca80a5f2009-10-20 19:55:01 +00001005 llvm::DIType DbgTy =
1006 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +00001007 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001008 Size, Align, 0, 0,
1009 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001010 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001011}
1012
1013llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1014 llvm::DICompileUnit Unit) {
1015 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1016 return CreateType(RT, Unit);
1017 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1018 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner9c85ba32008-11-10 06:08:34 +00001020 return llvm::DIType();
1021}
1022
1023llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1024 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001025 uint64_t Size;
1026 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
1028
Nuno Lopes010d5142009-01-28 00:35:17 +00001029 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001030 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001031 Size = 0;
1032 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001033 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001034 } else if (Ty->isIncompleteArrayType()) {
1035 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001036 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 } else {
1038 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001039 Size = CGM.getContext().getTypeSize(Ty);
1040 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Chris Lattner9c85ba32008-11-10 06:08:34 +00001043 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1044 // interior arrays, do we care? Why aren't nested arrays represented the
1045 // obvious/recursive way?
1046 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1047 QualType EltTy(Ty, 0);
1048 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001049 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001050 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001051 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001052 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001053 // FIXME: Verify this is right for VLAs.
1054 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1055 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001059 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060
Devang Patelca80a5f2009-10-20 19:55:01 +00001061 llvm::DIType DbgTy =
1062 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1063 Unit, "", llvm::DICompileUnit(),
1064 0, Size, Align, 0, 0,
1065 getOrCreateType(EltTy, Unit),
1066 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001067 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001068}
1069
Anders Carlssona031b352009-11-06 19:19:55 +00001070llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1071 llvm::DICompileUnit Unit) {
1072 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1073 Ty, Ty->getPointeeType(), Unit);
1074}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075
Anders Carlsson20f12a22009-12-06 18:00:51 +00001076llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1077 llvm::DICompileUnit U) {
1078 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1079 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1080
1081 if (!Ty->getPointeeType()->isFunctionType()) {
1082 // We have a data member pointer type.
1083 return PointerDiffDITy;
1084 }
1085
1086 // We have a member function pointer type. Treat it as a struct with two
1087 // ptrdiff_t members.
1088 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1089
1090 uint64_t FieldOffset = 0;
1091 llvm::DIDescriptor ElementTypes[2];
1092
1093 // FIXME: This should probably be a function type instead.
1094 ElementTypes[0] =
1095 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1096 "ptr", llvm::DICompileUnit(), 0,
1097 Info.first, Info.second, FieldOffset, 0,
1098 PointerDiffDITy);
1099 FieldOffset += Info.first;
1100
1101 ElementTypes[1] =
1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1103 "ptr", llvm::DICompileUnit(), 0,
1104 Info.first, Info.second, FieldOffset, 0,
1105 PointerDiffDITy);
1106
1107 llvm::DIArray Elements =
1108 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1109 llvm::array_lengthof(ElementTypes));
1110
1111 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1112 U, llvm::StringRef("test"),
1113 llvm::DICompileUnit(), 0, FieldOffset,
1114 0, 0, 0, llvm::DIType(), Elements);
1115}
1116
Douglas Gregor840943d2009-12-21 20:18:30 +00001117static QualType UnwrapTypeForDebugInfo(QualType T) {
1118 do {
1119 QualType LastT = T;
1120 switch (T->getTypeClass()) {
1121 default:
1122 return T;
1123 case Type::TemplateSpecialization:
1124 T = cast<TemplateSpecializationType>(T)->desugar();
1125 break;
1126 case Type::TypeOfExpr: {
1127 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1128 T = Ty->getUnderlyingExpr()->getType();
1129 break;
1130 }
1131 case Type::TypeOf:
1132 T = cast<TypeOfType>(T)->getUnderlyingType();
1133 break;
1134 case Type::Decltype:
1135 T = cast<DecltypeType>(T)->getUnderlyingType();
1136 break;
1137 case Type::QualifiedName:
1138 T = cast<QualifiedNameType>(T)->getNamedType();
1139 break;
1140 case Type::SubstTemplateTypeParm:
1141 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1142 break;
1143 case Type::Elaborated:
1144 T = cast<ElaboratedType>(T)->getUnderlyingType();
1145 break;
1146 }
1147
1148 assert(T != LastT && "Type unwrapping failed to unwrap!");
1149 if (T == LastT)
1150 return T;
1151 } while (true);
1152
1153 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001154}
1155
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001156/// getOrCreateType - Get the type from the cache or create a new
1157/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1159 llvm::DICompileUnit Unit) {
1160 if (Ty.isNull())
1161 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor840943d2009-12-21 20:18:30 +00001163 // Unwrap the type as needed for debug information.
1164 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001165
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001166 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001167 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001168 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001169 if (it != TypeCache.end()) {
1170 // Verify that the debug info still exists.
1171 if (&*it->second)
1172 return llvm::DIType(cast<llvm::MDNode>(it->second));
1173 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001174
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001175 // Otherwise create the type.
1176 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001177
1178 // And update the type cache.
1179 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001180 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001181}
1182
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001183/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001184llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1185 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001186 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001187 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001188 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001189
Douglas Gregor2101a822009-12-21 19:57:21 +00001190 const char *Diag = 0;
1191
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001192 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001193 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001194#define TYPE(Class, Base)
1195#define ABSTRACT_TYPE(Class, Base)
1196#define NON_CANONICAL_TYPE(Class, Base)
1197#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1198#include "clang/AST/TypeNodes.def"
1199 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001200
Anders Carlssonbfe69952009-11-06 18:24:04 +00001201 // FIXME: Handle these.
1202 case Type::ExtVector:
1203 case Type::Vector:
1204 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001205
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001206 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001207 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001208 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001209 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1210 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1211 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1212 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001213 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001214 return CreateType(cast<BlockPointerType>(Ty), Unit);
1215 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001216 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001217 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001218 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001219 case Type::FunctionProto:
1220 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001221 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001222 case Type::ConstantArray:
1223 case Type::VariableArray:
1224 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001225 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001226
1227 case Type::LValueReference:
1228 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1229
Anders Carlsson20f12a22009-12-06 18:00:51 +00001230 case Type::MemberPointer:
1231 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001232
1233 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001234 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001235 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001236 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001237 case Type::TypeOfExpr:
1238 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001239 case Type::Decltype:
1240 llvm_unreachable("type should have been unwrapped!");
1241 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001242
1243 case Type::RValueReference:
1244 // FIXME: Implement!
1245 Diag = "rvalue references";
1246 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001247 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001248
1249 assert(Diag && "Fall through without a diagnostic?");
1250 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1251 "debug information for %0 is not yet supported");
1252 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1253 << Diag;
1254 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001255}
1256
1257/// EmitFunctionStart - Constructs the debug code for entering a function -
1258/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001259void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001260 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001261 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Devang Patel9c6c3a02010-01-14 00:36:21 +00001263 llvm::StringRef Name;
1264 llvm::StringRef LinkageName;
1265
1266 const Decl *D = GD.getDecl();
1267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001268 // If there is a DISubprogram for this function available then use it.
1269 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1270 FI = SPCache.find(FD);
1271 if (FI != SPCache.end()) {
1272 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1273 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1274 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001275 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001276 return;
1277 }
1278 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001279 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001280 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001281 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001282 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001283 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001284 } else {
1285 // Use llvm function name as linkage name.
1286 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001287 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001288 if (!Name.empty() && Name[0] == '\01')
1289 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Devang Patel98a200b2010-01-14 18:06:13 +00001292 // It is expected that CurLoc is set before using EmitFunctionStart.
1293 // Usually, CurLoc points to the left bracket location of compound
1294 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001295 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001296 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001297 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001300 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001301 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001302 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001304 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001305 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001306 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001307}
1308
1309
Chris Lattner9c85ba32008-11-10 06:08:34 +00001310void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001311 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001313 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001314 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001315 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001316 || (SM.getInstantiationLineNumber(CurLoc) ==
1317 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001318 && SM.isFromSameFile(CurLoc, PrevLoc)))
1319 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001320
1321 // Update last state.
1322 PrevLoc = CurLoc;
1323
1324 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001325 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001326 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001327
Devang Patel8fae0602009-11-13 19:10:24 +00001328 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001329 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1330 llvm::DILocation DO(NULL);
1331 llvm::DILocation DL =
1332 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1333 DS, DO);
1334 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001335}
1336
1337/// EmitRegionStart- Constructs the debug code for entering a declarative
1338/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001339void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001340 llvm::DIDescriptor D =
1341 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1342 llvm::DIDescriptor() :
1343 llvm::DIDescriptor(RegionStack.back()));
1344 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001345}
1346
1347/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1348/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001349void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001350 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1351
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001352 // Provide an region stop point.
1353 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001355 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001356}
1357
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001358/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001359void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001360 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001361 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1362
Devang Patel07739032009-03-27 23:16:32 +00001363 // Do not emit variable debug information while generating optimized code.
1364 // The llvm optimizer and code generator are not yet ready to support
1365 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001366 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001367 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001368 return;
1369
Devang Patel239cec62010-02-01 21:39:52 +00001370 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1371 QualType Type = VD->getType();
Mike Stump39605b42009-09-22 02:12:52 +00001372 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Patel239cec62010-02-01 21:39:52 +00001373 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump39605b42009-09-22 02:12:52 +00001374 llvm::DICompileUnit DefUnit;
1375 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1376
1377 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1378
1379 llvm::DIType FieldTy;
1380
1381 QualType FType;
1382 uint64_t FieldSize, FieldOffset;
1383 unsigned FieldAlign;
1384
1385 llvm::DIArray Elements;
1386 llvm::DIType EltTy;
1387
1388 // Build up structure for the byref. See BuildByRefType.
1389 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001390 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001391 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001392 FieldSize = CGM.getContext().getTypeSize(FType);
1393 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001394 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1395 "__isa", DefUnit,
1396 0, FieldSize, FieldAlign,
1397 FieldOffset, 0, FieldTy);
1398 EltTys.push_back(FieldTy);
1399 FieldOffset += FieldSize;
1400
Anders Carlsson20f12a22009-12-06 18:00:51 +00001401 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001402 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001403 FieldSize = CGM.getContext().getTypeSize(FType);
1404 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001405 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1406 "__forwarding", DefUnit,
1407 0, FieldSize, FieldAlign,
1408 FieldOffset, 0, FieldTy);
1409 EltTys.push_back(FieldTy);
1410 FieldOffset += FieldSize;
1411
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001412 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001413 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001414 FieldSize = CGM.getContext().getTypeSize(FType);
1415 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001416 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1417 "__flags", DefUnit,
1418 0, FieldSize, FieldAlign,
1419 FieldOffset, 0, FieldTy);
1420 EltTys.push_back(FieldTy);
1421 FieldOffset += FieldSize;
1422
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001423 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001424 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001425 FieldSize = CGM.getContext().getTypeSize(FType);
1426 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001427 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1428 "__size", DefUnit,
1429 0, FieldSize, FieldAlign,
1430 FieldOffset, 0, FieldTy);
1431 EltTys.push_back(FieldTy);
1432 FieldOffset += FieldSize;
1433
Anders Carlsson20f12a22009-12-06 18:00:51 +00001434 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001435 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001436 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001437 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001438 FieldSize = CGM.getContext().getTypeSize(FType);
1439 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001440 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1441 "__copy_helper", DefUnit,
1442 0, FieldSize, FieldAlign,
1443 FieldOffset, 0, FieldTy);
1444 EltTys.push_back(FieldTy);
1445 FieldOffset += FieldSize;
1446
Anders Carlsson20f12a22009-12-06 18:00:51 +00001447 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001448 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001449 FieldSize = CGM.getContext().getTypeSize(FType);
1450 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001451 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1452 "__destroy_helper", DefUnit,
1453 0, FieldSize, FieldAlign,
1454 FieldOffset, 0, FieldTy);
1455 EltTys.push_back(FieldTy);
1456 FieldOffset += FieldSize;
1457 }
1458
Devang Patel239cec62010-02-01 21:39:52 +00001459 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8b752f12010-01-27 17:10:57 +00001460 if (Align > CharUnits::fromQuantity(
1461 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001462 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001463 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001464 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001465 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001466
1467 if (NumPaddingBytes > 0) {
1468 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001469 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001470 pad, ArrayType::Normal, 0);
1471 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001472 FieldSize = CGM.getContext().getTypeSize(FType);
1473 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001474 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1475 Unit, "", DefUnit,
1476 0, FieldSize, FieldAlign,
1477 FieldOffset, 0, FieldTy);
1478 EltTys.push_back(FieldTy);
1479 FieldOffset += FieldSize;
1480 }
1481 }
1482
1483 FType = Type;
1484 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001485 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001486 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001487
1488 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +00001489 VD->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001490 0, FieldSize, FieldAlign,
1491 FieldOffset, 0, FieldTy);
1492 EltTys.push_back(FieldTy);
1493 FieldOffset += FieldSize;
1494
1495 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1496
1497 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1498
1499 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1500 llvm::DICompileUnit(),
1501 0, FieldOffset, 0, 0, Flags,
1502 llvm::DIType(), Elements);
1503 }
Chris Lattner650cea92009-05-05 04:57:08 +00001504
Chris Lattner9c85ba32008-11-10 06:08:34 +00001505 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001506 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001507 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001508 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001509 unsigned Column = 0;
1510 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001511 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001512 Column = PLoc.getColumn();
1513 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001514 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattner9c85ba32008-11-10 06:08:34 +00001517 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001519 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001520 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001521 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001522 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001523 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001524 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001525
Devang Patel8fae0602009-11-13 19:10:24 +00001526 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001527 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001528 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1529
Chris Lattner23e92c02009-12-28 23:41:39 +00001530 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001531}
1532
Mike Stumpb1a6e682009-09-30 02:43:10 +00001533/// EmitDeclare - Emit local variable declaration debug info.
1534void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1535 llvm::Value *Storage, CGBuilderTy &Builder,
1536 CodeGenFunction *CGF) {
1537 const ValueDecl *Decl = BDRE->getDecl();
1538 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1539
1540 // Do not emit variable debug information while generating optimized code.
1541 // The llvm optimizer and code generator are not yet ready to support
1542 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001543 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001544 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001545 return;
1546
1547 uint64_t XOffset = 0;
1548 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1549 QualType Type = Decl->getType();
1550 llvm::DIType Ty = getOrCreateType(Type, Unit);
1551 if (Decl->hasAttr<BlocksAttr>()) {
1552 llvm::DICompileUnit DefUnit;
1553 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1554
1555 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1556
1557 llvm::DIType FieldTy;
1558
1559 QualType FType;
1560 uint64_t FieldSize, FieldOffset;
1561 unsigned FieldAlign;
1562
1563 llvm::DIArray Elements;
1564 llvm::DIType EltTy;
1565
1566 // Build up structure for the byref. See BuildByRefType.
1567 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001568 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001569 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001570 FieldSize = CGM.getContext().getTypeSize(FType);
1571 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001572 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1573 "__isa", DefUnit,
1574 0, FieldSize, FieldAlign,
1575 FieldOffset, 0, FieldTy);
1576 EltTys.push_back(FieldTy);
1577 FieldOffset += FieldSize;
1578
Anders Carlsson20f12a22009-12-06 18:00:51 +00001579 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001580 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001581 FieldSize = CGM.getContext().getTypeSize(FType);
1582 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001583 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1584 "__forwarding", DefUnit,
1585 0, FieldSize, FieldAlign,
1586 FieldOffset, 0, FieldTy);
1587 EltTys.push_back(FieldTy);
1588 FieldOffset += FieldSize;
1589
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001590 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001591 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001592 FieldSize = CGM.getContext().getTypeSize(FType);
1593 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001594 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1595 "__flags", DefUnit,
1596 0, FieldSize, FieldAlign,
1597 FieldOffset, 0, FieldTy);
1598 EltTys.push_back(FieldTy);
1599 FieldOffset += FieldSize;
1600
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001601 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001602 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001603 FieldSize = CGM.getContext().getTypeSize(FType);
1604 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001605 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1606 "__size", DefUnit,
1607 0, FieldSize, FieldAlign,
1608 FieldOffset, 0, FieldTy);
1609 EltTys.push_back(FieldTy);
1610 FieldOffset += FieldSize;
1611
Anders Carlsson20f12a22009-12-06 18:00:51 +00001612 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001613 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001614 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001615 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001616 FieldSize = CGM.getContext().getTypeSize(FType);
1617 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001618 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1619 "__copy_helper", DefUnit,
1620 0, FieldSize, FieldAlign,
1621 FieldOffset, 0, FieldTy);
1622 EltTys.push_back(FieldTy);
1623 FieldOffset += FieldSize;
1624
Anders Carlsson20f12a22009-12-06 18:00:51 +00001625 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001626 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001627 FieldSize = CGM.getContext().getTypeSize(FType);
1628 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001629 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1630 "__destroy_helper", DefUnit,
1631 0, FieldSize, FieldAlign,
1632 FieldOffset, 0, FieldTy);
1633 EltTys.push_back(FieldTy);
1634 FieldOffset += FieldSize;
1635 }
1636
Ken Dyck8b752f12010-01-27 17:10:57 +00001637 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1638 if (Align > CharUnits::fromQuantity(
1639 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001640 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001641 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001642 unsigned NumPaddingBytes
1643 = AlignedOffsetInBytes - FieldOffset/8;
1644
1645 if (NumPaddingBytes > 0) {
1646 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001647 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001648 pad, ArrayType::Normal, 0);
1649 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001650 FieldSize = CGM.getContext().getTypeSize(FType);
1651 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001652 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1653 Unit, "", DefUnit,
1654 0, FieldSize, FieldAlign,
1655 FieldOffset, 0, FieldTy);
1656 EltTys.push_back(FieldTy);
1657 FieldOffset += FieldSize;
1658 }
1659 }
1660
1661 FType = Type;
1662 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001663 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001664 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001665
1666 XOffset = FieldOffset;
1667 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001668 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001669 0, FieldSize, FieldAlign,
1670 FieldOffset, 0, FieldTy);
1671 EltTys.push_back(FieldTy);
1672 FieldOffset += FieldSize;
1673
1674 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1675
1676 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1677
1678 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1679 llvm::DICompileUnit(),
1680 0, FieldOffset, 0, 0, Flags,
1681 llvm::DIType(), Elements);
1682 }
1683
1684 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001685 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001686 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1687 unsigned Line = 0;
1688 if (!PLoc.isInvalid())
1689 Line = PLoc.getLine();
1690 else
1691 Unit = llvm::DICompileUnit();
1692
Ken Dyck199c3d62010-01-11 17:06:35 +00001693 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001694 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001695 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1696 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1697 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1698 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001699 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001700 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1701 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001702 // offset of __forwarding field
1703 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001704 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1705 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1706 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001707 // offset of x field
1708 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001709 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001710 }
1711
1712 // Create the descriptor for the variable.
1713 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001714 DebugFactory.CreateComplexVariable(Tag,
1715 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001716 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001717 addr);
1718 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001719 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001720 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001721
Devang Patel8fae0602009-11-13 19:10:24 +00001722 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001723 llvm::DILocation DO(NULL);
1724 llvm::DILocation DL =
1725 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001726
Chris Lattner23e92c02009-12-28 23:41:39 +00001727 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001728}
1729
Chris Lattner9c85ba32008-11-10 06:08:34 +00001730void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1731 llvm::Value *Storage,
1732 CGBuilderTy &Builder) {
1733 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1734}
1735
Mike Stumpb1a6e682009-09-30 02:43:10 +00001736void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1737 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1738 CodeGenFunction *CGF) {
1739 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1740}
1741
Chris Lattner9c85ba32008-11-10 06:08:34 +00001742/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1743/// variable declaration.
1744void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1745 CGBuilderTy &Builder) {
1746 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1747}
1748
1749
1750
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001751/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001752void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001753 const VarDecl *D) {
1754
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001755 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001756 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001757 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001758 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001759 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001760
Devang Pateleb6d79b2010-02-01 21:34:11 +00001761 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001762 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001764 // CodeGen turns int[] into int[1] so we'll do the same here.
1765 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001767 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001768 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Anders Carlsson20f12a22009-12-06 18:00:51 +00001770 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001771 ArrayType::Normal, 0);
1772 }
Devang Pateleb6d79b2010-02-01 21:34:11 +00001773 llvm::StringRef DeclName = D->getName();
1774 llvm::DIDescriptor DContext =
1775 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1776 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001777 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001778 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001779 Var->hasInternalLinkage(),
1780 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001781}
1782
Devang Patel9ca36b62009-02-26 21:10:26 +00001783/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001784void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001785 ObjCInterfaceDecl *Decl) {
1786 // Create global variable debug descriptor.
1787 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001788 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001789 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1790 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001791
Devang Patel73621622009-11-25 17:37:31 +00001792 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001793
Anders Carlsson20f12a22009-12-06 18:00:51 +00001794 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001795 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Devang Patel9ca36b62009-02-26 21:10:26 +00001797 // CodeGen turns int[] into int[1] so we'll do the same here.
1798 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Devang Patel9ca36b62009-02-26 21:10:26 +00001800 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001801 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Anders Carlsson20f12a22009-12-06 18:00:51 +00001803 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001804 ArrayType::Normal, 0);
1805 }
1806
Devang Patelf6a39b72009-10-20 18:26:30 +00001807 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001808 getOrCreateType(T, Unit),
1809 Var->hasInternalLinkage(),
1810 true/*definition*/, Var);
1811}
Devang Patelabb485f2010-02-01 19:16:32 +00001812
1813/// getOrCreateNamesSpace - Return namespace descriptor for the given
1814/// namespace decl.
1815llvm::DINameSpace
1816CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1817 llvm::DIDescriptor Unit) {
1818 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1819 NameSpaceCache.find(NSDecl);
1820 if (I != NameSpaceCache.end())
1821 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1822
1823 SourceManager &SM = CGM.getContext().getSourceManager();
1824 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1825 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1826
1827 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001828 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001829 llvm::DINameSpace NS =
1830 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1831 llvm::DICompileUnit(Unit.getNode()), LineNo);
1832 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1833 return NS;
1834}