blob: 00cda956793eb4f63da5029dbbb67dfc8cf15e68 [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::
468CollectRecordFields(const RecordDecl *Decl,
469 llvm::DICompileUnit Unit,
470 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
471 unsigned FieldNo = 0;
472 SourceManager &SM = CGM.getContext().getSourceManager();
473 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
474 for (RecordDecl::field_iterator I = Decl->field_begin(),
475 E = Decl->field_end();
476 I != E; ++I, ++FieldNo) {
477 FieldDecl *Field = *I;
478 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
479
480 llvm::StringRef FieldName = Field->getName();
481
482 // Ignore unnamed fields.
483 if (FieldName.empty())
484 continue;
485
486 // Get the location for the field.
487 SourceLocation FieldDefLoc = Field->getLocation();
488 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
489 llvm::DICompileUnit FieldDefUnit;
490 unsigned FieldLine = 0;
491
492 if (!PLoc.isInvalid()) {
493 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
494 FieldLine = PLoc.getLine();
495 }
496
497 QualType FType = Field->getType();
498 uint64_t FieldSize = 0;
499 unsigned FieldAlign = 0;
500 if (!FType->isIncompleteArrayType()) {
501
502 // Bit size, align and offset of the type.
503 FieldSize = CGM.getContext().getTypeSize(FType);
504 Expr *BitWidth = Field->getBitWidth();
505 if (BitWidth)
506 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
507
508 FieldAlign = CGM.getContext().getTypeAlign(FType);
509 }
510
511 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
512
513 // Create a DW_TAG_member node to remember the offset of this field in the
514 // struct. FIXME: This is an absolutely insane way to capture this
515 // information. When we gut debug info, this should be fixed.
516 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
517 FieldName, FieldDefUnit,
518 FieldLine, FieldSize, FieldAlign,
519 FieldOffset, 0, FieldTy);
520 EltTys.push_back(FieldTy);
521 }
522}
523
Devang Patela6da1922010-01-28 00:28:01 +0000524/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
525/// function type is not updated to include implicit "this" pointer. Use this
526/// routine to get a method type which includes "this" pointer.
527llvm::DIType
528CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
529 llvm::DICompileUnit Unit) {
530 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000531
532 // Static methods do not need "this" pointer argument.
533 if (Method->isStatic())
534 return FnTy;
535
Devang Patela6da1922010-01-28 00:28:01 +0000536 // Add "this" pointer.
537
538 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
539 assert (Args.getNumElements() && "Invalid number of arguments!");
540
541 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
542
543 // First element is always return type. For 'void' functions it is NULL.
544 Elts.push_back(Args.getElement(0));
545
546 // "this" pointer is always first argument.
547 ASTContext &Context = CGM.getContext();
548 QualType ThisPtr =
549 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
550 Elts.push_back(getOrCreateType(ThisPtr, Unit));
551
552 // Copy rest of the arguments.
553 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
554 Elts.push_back(Args.getElement(i));
555
556 llvm::DIArray EltTypeArray =
557 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
558
559 return
560 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
561 Unit, "", llvm::DICompileUnit(),
562 0, 0, 0, 0, 0,
563 llvm::DIType(), EltTypeArray);
564}
565
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000566/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
567/// a single member function GlobalDecl.
568llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000569CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000570 llvm::DICompileUnit Unit,
571 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000572 bool IsCtorOrDtor =
573 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
574
575 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000576 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000577 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000578
579 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
580 // make sense to give a single ctor/dtor a linkage name.
581 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000582 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000583
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000584 SourceManager &SM = CGM.getContext().getSourceManager();
585
586 // Get the location for the method.
587 SourceLocation MethodDefLoc = Method->getLocation();
588 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
589 llvm::DICompileUnit MethodDefUnit;
590 unsigned MethodLine = 0;
591
592 if (!PLoc.isInvalid()) {
593 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
594 MethodLine = PLoc.getLine();
595 }
596
597 // Collect virtual method info.
598 llvm::DIType ContainingType;
599 unsigned Virtuality = 0;
600 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000601
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000602 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000603 if (Method->isPure())
604 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
605 else
606 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
607
608 // It doesn't make sense to give a virtual destructor a vtable index,
609 // since a single destructor has two entries in the vtable.
610 if (!isa<CXXDestructorDecl>(Method))
611 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000612 ContainingType = RecordTy;
613 }
614
615 llvm::DISubprogram SP =
616 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
617 MethodLinkageName,
618 MethodDefUnit, MethodLine,
619 MethodTy, /*isLocalToUnit=*/false,
620 Method->isThisDeclarationADefinition(),
621 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000622
623 // Don't cache ctors or dtors since we have to emit multiple functions for
624 // a single ctor or dtor.
625 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
626 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000627
628 return SP;
629}
630
Devang Patel4125fd22010-01-19 01:54:44 +0000631/// CollectCXXMemberFunctions - A helper function to collect debug info for
632/// C++ member functions.This is used while creating debug info entry for
633/// a Record.
634void CGDebugInfo::
635CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
636 llvm::DICompileUnit Unit,
637 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
638 llvm::DICompositeType &RecordTy) {
Devang Patel4125fd22010-01-19 01:54:44 +0000639 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
640 E = Decl->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000641 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000642
643 if (Method->isImplicit())
644 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000645
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000646 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000647 }
648}
649
Devang Patela245c5b2010-01-25 23:32:18 +0000650/// CollectCXXBases - A helper function to collect debug info for
651/// C++ base classes. This is used while creating debug info entry for
652/// a Record.
653void CGDebugInfo::
654CollectCXXBases(const CXXRecordDecl *Decl,
655 llvm::DICompileUnit Unit,
656 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
657 llvm::DICompositeType &RecordTy) {
658
Devang Patelca7daed2010-01-28 21:54:15 +0000659 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
660 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
661 BE = Decl->bases_end(); BI != BE; ++BI) {
662 unsigned BFlags = 0;
663 uint64_t BaseOffset;
664
665 const CXXRecordDecl *Base =
666 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
667
668 if (BI->isVirtual()) {
669 BaseOffset = RL.getVBaseClassOffset(Base);
670 BFlags = llvm::DIType::FlagVirtual;
671 } else
672 BaseOffset = RL.getBaseClassOffset(Base);
673
674 AccessSpecifier Access = BI->getAccessSpecifier();
675 if (Access == clang::AS_private)
676 BFlags |= llvm::DIType::FlagPrivate;
677 else if (Access == clang::AS_protected)
678 BFlags |= llvm::DIType::FlagProtected;
679
680 llvm::DIType DTy =
681 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
682 RecordTy, llvm::StringRef(),
683 llvm::DICompileUnit(), 0, 0, 0,
684 BaseOffset, BFlags,
685 getOrCreateType(BI->getType(),
686 Unit));
687 EltTys.push_back(DTy);
688 }
Devang Patela245c5b2010-01-25 23:32:18 +0000689}
690
Devang Patel4ce3f202010-01-28 18:11:52 +0000691/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
692llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
693 if (!VTablePtrType.isNull())
694 return VTablePtrType;
695
696 ASTContext &Context = CGM.getContext();
697
698 /* Function type */
699 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
700 STys.push_back(getOrCreateType(Context.IntTy, Unit));
701 llvm::DIArray SElements =
702 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
703 llvm::DIType SubTy =
704 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
705 Unit, "", llvm::DICompileUnit(),
706 0, 0, 0, 0, 0, llvm::DIType(), SElements);
707
708 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
709 llvm::DIType vtbl_ptr_type
710 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
711 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
712 0, Size, 0, 0, 0, SubTy);
713
714 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
715 Unit, "", llvm::DICompileUnit(),
716 0, Size, 0, 0, 0, vtbl_ptr_type);
717 return VTablePtrType;
718}
719
720/// getVtableName - Get vtable name for the given Class.
721llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *Decl) {
722 // Otherwise construct gdb compatible name name.
723 std::string Name = "_vptr$" + Decl->getNameAsString();
724
725 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000726 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000727 memcpy(StrPtr, Name.data(), Name.length());
728 return llvm::StringRef(StrPtr, Name.length());
729}
730
731
732/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
733/// debug info entry in EltTys vector.
734void CGDebugInfo::
735CollectVtableInfo(const CXXRecordDecl *Decl,
736 llvm::DICompileUnit Unit,
737 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
738 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
739
740 // If there is a primary base then it will hold vtable info.
741 if (RL.getPrimaryBase())
742 return;
743
744 // If this class is not dynamic then there is not any vtable info to collect.
745 if (!Decl->isDynamicClass())
746 return;
747
748 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
749 llvm::DIType VPTR
750 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
751 getVtableName(Decl), llvm::DICompileUnit(),
752 0, Size, 0, 0, 0,
753 getOrCreateVTablePtrType(Unit));
754 EltTys.push_back(VPTR);
755}
756
Devang Patel65e99f22009-02-25 01:36:11 +0000757/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
759 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000760 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 unsigned Tag;
763 if (Decl->isStruct())
764 Tag = llvm::dwarf::DW_TAG_structure_type;
765 else if (Decl->isUnion())
766 Tag = llvm::dwarf::DW_TAG_union_type;
767 else {
768 assert(Decl->isClass() && "Unknown RecordType!");
769 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000770 }
771
Anders Carlsson20f12a22009-12-06 18:00:51 +0000772 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000773
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000775 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000776 llvm::DICompileUnit DefUnit;
777 unsigned Line = 0;
778 if (!PLoc.isInvalid()) {
779 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
780 Line = PLoc.getLine();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783 // Records and classes and unions can all be recursive. To handle them, we
784 // first generate a debug descriptor for the struct as a forward declaration.
785 // Then (if it is a definition) we go through and get debug info for all of
786 // its members. Finally, we create a descriptor for the complete type (which
787 // may refer to the forward decl if the struct is recursive) and replace all
788 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000789
790 // A Decl->getName() is not unique. However, the debug info descriptors
791 // are uniqued. The debug info descriptor describing record's context is
792 // necessary to keep two Decl's descriptor unique if their name match.
793 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
794 // use type's name in FwdDecl.
795 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000796 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000797 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000798 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000802 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000804
Eli Friedman14d63652009-11-16 21:04:30 +0000805 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 // Otherwise, insert it into the TypeCache so that recursive uses will find
807 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000808 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000809
810 // Convert all the elements.
811 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
812
Devang Patel4ce3f202010-01-28 18:11:52 +0000813 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
Devang Patel3064afe2010-01-28 21:41:35 +0000814 if (CXXDecl) {
815 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000816 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000817 }
Devang Patel428deb52010-01-19 00:00:59 +0000818 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000819 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000820 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000821 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000822
823 // A class's primary base or the class itself contains the vtable.
824 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
825 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
826 ContainingType =
827 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
828 else if (CXXDecl->isDynamicClass())
829 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner9c85ba32008-11-10 06:08:34 +0000832 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000833 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834
835 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000836 uint64_t Size = CGM.getContext().getTypeSize(Ty);
837 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Devang Patel0ce73f62009-07-22 18:57:00 +0000839 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000840 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000841 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000842 llvm::DIType(), Elements,
843 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844
845 // Now that we have a real decl for the struct, replace anything using the
846 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000847 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000848
Chris Lattner9c85ba32008-11-10 06:08:34 +0000849 return RealDecl;
850}
851
Devang Patel9ca36b62009-02-26 21:10:26 +0000852/// CreateType - get objective-c interface type.
853llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
854 llvm::DICompileUnit Unit) {
855 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Devang Patel9ca36b62009-02-26 21:10:26 +0000857 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000858 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000859
860 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000861 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000862 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
863 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
864
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Daniel Dunbard86d3362009-05-18 20:51:58 +0000866 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000867
Devang Patel9ca36b62009-02-26 21:10:26 +0000868 // To handle recursive interface, we
869 // first generate a debug descriptor for the struct as a forward declaration.
870 // Then (if it is a definition) we go through and get debug info for all of
871 // its members. Finally, we create a descriptor for the complete type (which
872 // may refer to the forward decl if the struct is recursive) and replace all
873 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000874 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000875 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000876 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000877 llvm::DIType(), llvm::DIArray(),
878 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Devang Patel9ca36b62009-02-26 21:10:26 +0000880 // If this is just a forward declaration, return it.
881 if (Decl->isForwardDecl())
882 return FwdDecl;
883
Devang Patelffffb032009-11-16 20:09:38 +0000884 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000885 // Otherwise, insert it into the TypeCache so that recursive uses will find
886 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000887 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000888
889 // Convert all the elements.
890 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
891
Devang Patelfbe899f2009-03-10 21:30:26 +0000892 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
893 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000894 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000895 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000896 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000897 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000898 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000899 0 /* offset */, 0, SClassTy);
900 EltTys.push_back(InhTag);
901 }
902
Anders Carlsson20f12a22009-12-06 18:00:51 +0000903 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000904
905 unsigned FieldNo = 0;
906 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
907 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
908 ObjCIvarDecl *Field = *I;
909 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
910
Devang Patel73621622009-11-25 17:37:31 +0000911 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000912
Devang Patelde135022009-04-27 22:40:36 +0000913 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000914 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000915 continue;
916
Devang Patel9ca36b62009-02-26 21:10:26 +0000917 // Get the location for the field.
918 SourceLocation FieldDefLoc = Field->getLocation();
919 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000920 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
921 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
922
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Devang Patel99c20eb2009-03-20 18:24:39 +0000924 QualType FType = Field->getType();
925 uint64_t FieldSize = 0;
926 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000927
Devang Patel99c20eb2009-03-20 18:24:39 +0000928 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Devang Patel99c20eb2009-03-20 18:24:39 +0000930 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000931 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000932 Expr *BitWidth = Field->getBitWidth();
933 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000934 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000935
Anders Carlsson20f12a22009-12-06 18:00:51 +0000936 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000937 }
938
Mike Stump1eb44332009-09-09 15:08:12 +0000939 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
940
Devang Patelc20482b2009-03-19 00:23:53 +0000941 unsigned Flags = 0;
942 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
943 Flags = llvm::DIType::FlagProtected;
944 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
945 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Devang Patel9ca36b62009-02-26 21:10:26 +0000947 // Create a DW_TAG_member node to remember the offset of this field in the
948 // struct. FIXME: This is an absolutely insane way to capture this
949 // information. When we gut debug info, this should be fixed.
950 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
951 FieldName, FieldDefUnit,
952 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000953 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000954 EltTys.push_back(FieldTy);
955 }
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Devang Patel9ca36b62009-02-26 21:10:26 +0000957 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000958 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000959
960 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000961 uint64_t Size = CGM.getContext().getTypeSize(Ty);
962 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Devang Patel6c1fddf2009-07-27 18:42:03 +0000964 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000965 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000966 Line, Size, Align, 0, 0, llvm::DIType(),
967 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000968
969 // Now that we have a real decl for the struct, replace anything using the
970 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000971 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000972
Devang Patel9ca36b62009-02-26 21:10:26 +0000973 return RealDecl;
974}
975
Chris Lattner9c85ba32008-11-10 06:08:34 +0000976llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
977 llvm::DICompileUnit Unit) {
978 EnumDecl *Decl = Ty->getDecl();
979
980 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
981
982 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000983 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000985 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000986 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000987 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990 // Return a CompositeType for the enum itself.
991 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000992 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000993
Chris Lattner9c85ba32008-11-10 06:08:34 +0000994 SourceLocation DefLoc = Decl->getLocation();
995 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000996 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000997 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
998 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
999
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001002 uint64_t Size = 0;
1003 unsigned Align = 0;
1004 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001005 Size = CGM.getContext().getTypeSize(Ty);
1006 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Devang Patelca80a5f2009-10-20 19:55:01 +00001009 llvm::DIType DbgTy =
1010 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +00001011 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001012 Size, Align, 0, 0,
1013 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001014 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001015}
1016
1017llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1018 llvm::DICompileUnit Unit) {
1019 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1020 return CreateType(RT, Unit);
1021 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1022 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Chris Lattner9c85ba32008-11-10 06:08:34 +00001024 return llvm::DIType();
1025}
1026
1027llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1028 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001029 uint64_t Size;
1030 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
1032
Nuno Lopes010d5142009-01-28 00:35:17 +00001033 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001034 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001035 Size = 0;
1036 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001037 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001038 } else if (Ty->isIncompleteArrayType()) {
1039 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001040 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001041 } else {
1042 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001043 Size = CGM.getContext().getTypeSize(Ty);
1044 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Chris Lattner9c85ba32008-11-10 06:08:34 +00001047 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1048 // interior arrays, do we care? Why aren't nested arrays represented the
1049 // obvious/recursive way?
1050 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1051 QualType EltTy(Ty, 0);
1052 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001053 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001054 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001055 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001056 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057 // FIXME: Verify this is right for VLAs.
1058 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1059 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattner9c85ba32008-11-10 06:08:34 +00001062 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001063 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001064
Devang Patelca80a5f2009-10-20 19:55:01 +00001065 llvm::DIType DbgTy =
1066 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1067 Unit, "", llvm::DICompileUnit(),
1068 0, Size, Align, 0, 0,
1069 getOrCreateType(EltTy, Unit),
1070 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001071 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072}
1073
Anders Carlssona031b352009-11-06 19:19:55 +00001074llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1075 llvm::DICompileUnit Unit) {
1076 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1077 Ty, Ty->getPointeeType(), Unit);
1078}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001079
Anders Carlsson20f12a22009-12-06 18:00:51 +00001080llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1081 llvm::DICompileUnit U) {
1082 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1083 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1084
1085 if (!Ty->getPointeeType()->isFunctionType()) {
1086 // We have a data member pointer type.
1087 return PointerDiffDITy;
1088 }
1089
1090 // We have a member function pointer type. Treat it as a struct with two
1091 // ptrdiff_t members.
1092 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1093
1094 uint64_t FieldOffset = 0;
1095 llvm::DIDescriptor ElementTypes[2];
1096
1097 // FIXME: This should probably be a function type instead.
1098 ElementTypes[0] =
1099 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1100 "ptr", llvm::DICompileUnit(), 0,
1101 Info.first, Info.second, FieldOffset, 0,
1102 PointerDiffDITy);
1103 FieldOffset += Info.first;
1104
1105 ElementTypes[1] =
1106 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1107 "ptr", llvm::DICompileUnit(), 0,
1108 Info.first, Info.second, FieldOffset, 0,
1109 PointerDiffDITy);
1110
1111 llvm::DIArray Elements =
1112 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1113 llvm::array_lengthof(ElementTypes));
1114
1115 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1116 U, llvm::StringRef("test"),
1117 llvm::DICompileUnit(), 0, FieldOffset,
1118 0, 0, 0, llvm::DIType(), Elements);
1119}
1120
Douglas Gregor840943d2009-12-21 20:18:30 +00001121static QualType UnwrapTypeForDebugInfo(QualType T) {
1122 do {
1123 QualType LastT = T;
1124 switch (T->getTypeClass()) {
1125 default:
1126 return T;
1127 case Type::TemplateSpecialization:
1128 T = cast<TemplateSpecializationType>(T)->desugar();
1129 break;
1130 case Type::TypeOfExpr: {
1131 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1132 T = Ty->getUnderlyingExpr()->getType();
1133 break;
1134 }
1135 case Type::TypeOf:
1136 T = cast<TypeOfType>(T)->getUnderlyingType();
1137 break;
1138 case Type::Decltype:
1139 T = cast<DecltypeType>(T)->getUnderlyingType();
1140 break;
1141 case Type::QualifiedName:
1142 T = cast<QualifiedNameType>(T)->getNamedType();
1143 break;
1144 case Type::SubstTemplateTypeParm:
1145 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1146 break;
1147 case Type::Elaborated:
1148 T = cast<ElaboratedType>(T)->getUnderlyingType();
1149 break;
1150 }
1151
1152 assert(T != LastT && "Type unwrapping failed to unwrap!");
1153 if (T == LastT)
1154 return T;
1155 } while (true);
1156
1157 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001158}
1159
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001160/// getOrCreateType - Get the type from the cache or create a new
1161/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001162llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1163 llvm::DICompileUnit Unit) {
1164 if (Ty.isNull())
1165 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor840943d2009-12-21 20:18:30 +00001167 // Unwrap the type as needed for debug information.
1168 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001169
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001170 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001171 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001172 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001173 if (it != TypeCache.end()) {
1174 // Verify that the debug info still exists.
1175 if (&*it->second)
1176 return llvm::DIType(cast<llvm::MDNode>(it->second));
1177 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001178
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001179 // Otherwise create the type.
1180 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001181
1182 // And update the type cache.
1183 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001184 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001185}
1186
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001187/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001188llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1189 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001190 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001191 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001192 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001193
Douglas Gregor2101a822009-12-21 19:57:21 +00001194 const char *Diag = 0;
1195
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001196 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001197 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001198#define TYPE(Class, Base)
1199#define ABSTRACT_TYPE(Class, Base)
1200#define NON_CANONICAL_TYPE(Class, Base)
1201#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1202#include "clang/AST/TypeNodes.def"
1203 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001204
Anders Carlssonbfe69952009-11-06 18:24:04 +00001205 // FIXME: Handle these.
1206 case Type::ExtVector:
1207 case Type::Vector:
1208 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001209
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001210 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001211 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001212 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001213 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1214 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1215 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1216 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001217 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001218 return CreateType(cast<BlockPointerType>(Ty), Unit);
1219 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001220 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001221 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001223 case Type::FunctionProto:
1224 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001225 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001226 case Type::ConstantArray:
1227 case Type::VariableArray:
1228 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001229 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001230
1231 case Type::LValueReference:
1232 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1233
Anders Carlsson20f12a22009-12-06 18:00:51 +00001234 case Type::MemberPointer:
1235 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001236
1237 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001238 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001239 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001240 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001241 case Type::TypeOfExpr:
1242 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001243 case Type::Decltype:
1244 llvm_unreachable("type should have been unwrapped!");
1245 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001246
1247 case Type::RValueReference:
1248 // FIXME: Implement!
1249 Diag = "rvalue references";
1250 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001251 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001252
1253 assert(Diag && "Fall through without a diagnostic?");
1254 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1255 "debug information for %0 is not yet supported");
1256 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1257 << Diag;
1258 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001259}
1260
1261/// EmitFunctionStart - Constructs the debug code for entering a function -
1262/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001263void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001264 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001265 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Devang Patel9c6c3a02010-01-14 00:36:21 +00001267 llvm::StringRef Name;
1268 llvm::StringRef LinkageName;
1269
1270 const Decl *D = GD.getDecl();
1271 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001272 // If there is a DISubprogram for this function available then use it.
1273 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1274 FI = SPCache.find(FD);
1275 if (FI != SPCache.end()) {
1276 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1277 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1278 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001279 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001280 return;
1281 }
1282 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001283 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001284 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001285 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001286 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001287 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001288 } else {
1289 // Use llvm function name as linkage name.
1290 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001291 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001292 if (!Name.empty() && Name[0] == '\01')
1293 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Devang Patel98a200b2010-01-14 18:06:13 +00001296 // It is expected that CurLoc is set before using EmitFunctionStart.
1297 // Usually, CurLoc points to the left bracket location of compound
1298 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001300 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001301 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattner9c85ba32008-11-10 06:08:34 +00001303 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001304 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001305 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001306 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001308 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001309 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001310 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001311}
1312
1313
Chris Lattner9c85ba32008-11-10 06:08:34 +00001314void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001315 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001317 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001318 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001319 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001320 || (SM.getInstantiationLineNumber(CurLoc) ==
1321 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001322 && SM.isFromSameFile(CurLoc, PrevLoc)))
1323 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001324
1325 // Update last state.
1326 PrevLoc = CurLoc;
1327
1328 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001329 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001330 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001331
Devang Patel8fae0602009-11-13 19:10:24 +00001332 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001333 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1334 llvm::DILocation DO(NULL);
1335 llvm::DILocation DL =
1336 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1337 DS, DO);
1338 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001339}
1340
1341/// EmitRegionStart- Constructs the debug code for entering a declarative
1342/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001343void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001344 llvm::DIDescriptor D =
1345 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1346 llvm::DIDescriptor() :
1347 llvm::DIDescriptor(RegionStack.back()));
1348 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001349}
1350
1351/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1352/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001353void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001354 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1355
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001356 // Provide an region stop point.
1357 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001359 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001360}
1361
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001362/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001363void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1364 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001365 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1366
Devang Patel07739032009-03-27 23:16:32 +00001367 // Do not emit variable debug information while generating optimized code.
1368 // The llvm optimizer and code generator are not yet ready to support
1369 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001370 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001371 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001372 return;
1373
Chris Lattner650cea92009-05-05 04:57:08 +00001374 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001375 QualType Type = Decl->getType();
1376 llvm::DIType Ty = getOrCreateType(Type, Unit);
1377 if (Decl->hasAttr<BlocksAttr>()) {
1378 llvm::DICompileUnit DefUnit;
1379 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1380
1381 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1382
1383 llvm::DIType FieldTy;
1384
1385 QualType FType;
1386 uint64_t FieldSize, FieldOffset;
1387 unsigned FieldAlign;
1388
1389 llvm::DIArray Elements;
1390 llvm::DIType EltTy;
1391
1392 // Build up structure for the byref. See BuildByRefType.
1393 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001394 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001395 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001396 FieldSize = CGM.getContext().getTypeSize(FType);
1397 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001398 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1399 "__isa", DefUnit,
1400 0, FieldSize, FieldAlign,
1401 FieldOffset, 0, FieldTy);
1402 EltTys.push_back(FieldTy);
1403 FieldOffset += FieldSize;
1404
Anders Carlsson20f12a22009-12-06 18:00:51 +00001405 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001406 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001407 FieldSize = CGM.getContext().getTypeSize(FType);
1408 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001409 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1410 "__forwarding", DefUnit,
1411 0, FieldSize, FieldAlign,
1412 FieldOffset, 0, FieldTy);
1413 EltTys.push_back(FieldTy);
1414 FieldOffset += FieldSize;
1415
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001416 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001417 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001418 FieldSize = CGM.getContext().getTypeSize(FType);
1419 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001420 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1421 "__flags", DefUnit,
1422 0, FieldSize, FieldAlign,
1423 FieldOffset, 0, FieldTy);
1424 EltTys.push_back(FieldTy);
1425 FieldOffset += FieldSize;
1426
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001427 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001428 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001429 FieldSize = CGM.getContext().getTypeSize(FType);
1430 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001431 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1432 "__size", DefUnit,
1433 0, FieldSize, FieldAlign,
1434 FieldOffset, 0, FieldTy);
1435 EltTys.push_back(FieldTy);
1436 FieldOffset += FieldSize;
1437
Anders Carlsson20f12a22009-12-06 18:00:51 +00001438 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001439 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001440 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001441 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001442 FieldSize = CGM.getContext().getTypeSize(FType);
1443 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001444 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1445 "__copy_helper", DefUnit,
1446 0, FieldSize, FieldAlign,
1447 FieldOffset, 0, FieldTy);
1448 EltTys.push_back(FieldTy);
1449 FieldOffset += FieldSize;
1450
Anders Carlsson20f12a22009-12-06 18:00:51 +00001451 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001452 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001453 FieldSize = CGM.getContext().getTypeSize(FType);
1454 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001455 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1456 "__destroy_helper", DefUnit,
1457 0, FieldSize, FieldAlign,
1458 FieldOffset, 0, FieldTy);
1459 EltTys.push_back(FieldTy);
1460 FieldOffset += FieldSize;
1461 }
1462
Ken Dyck8b752f12010-01-27 17:10:57 +00001463 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1464 if (Align > CharUnits::fromQuantity(
1465 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001466 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001467 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001468 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001469 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001470
1471 if (NumPaddingBytes > 0) {
1472 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001473 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001474 pad, ArrayType::Normal, 0);
1475 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001476 FieldSize = CGM.getContext().getTypeSize(FType);
1477 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001478 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1479 Unit, "", DefUnit,
1480 0, FieldSize, FieldAlign,
1481 FieldOffset, 0, FieldTy);
1482 EltTys.push_back(FieldTy);
1483 FieldOffset += FieldSize;
1484 }
1485 }
1486
1487 FType = Type;
1488 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001489 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001490 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001491
1492 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001493 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001494 0, FieldSize, FieldAlign,
1495 FieldOffset, 0, FieldTy);
1496 EltTys.push_back(FieldTy);
1497 FieldOffset += FieldSize;
1498
1499 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1500
1501 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1502
1503 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1504 llvm::DICompileUnit(),
1505 0, FieldOffset, 0, 0, Flags,
1506 llvm::DIType(), Elements);
1507 }
Chris Lattner650cea92009-05-05 04:57:08 +00001508
Chris Lattner9c85ba32008-11-10 06:08:34 +00001509 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001510 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001511 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001512 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001513 unsigned Column = 0;
1514 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001515 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001516 Column = PLoc.getColumn();
1517 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001518 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Chris Lattner9c85ba32008-11-10 06:08:34 +00001521 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001522 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001523 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001524 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001525 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001526 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001527 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001528 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001529
Devang Patel8fae0602009-11-13 19:10:24 +00001530 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001531 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001532 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1533
Chris Lattner23e92c02009-12-28 23:41:39 +00001534 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001535}
1536
Mike Stumpb1a6e682009-09-30 02:43:10 +00001537/// EmitDeclare - Emit local variable declaration debug info.
1538void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1539 llvm::Value *Storage, CGBuilderTy &Builder,
1540 CodeGenFunction *CGF) {
1541 const ValueDecl *Decl = BDRE->getDecl();
1542 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1543
1544 // Do not emit variable debug information while generating optimized code.
1545 // The llvm optimizer and code generator are not yet ready to support
1546 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001547 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001548 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001549 return;
1550
1551 uint64_t XOffset = 0;
1552 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1553 QualType Type = Decl->getType();
1554 llvm::DIType Ty = getOrCreateType(Type, Unit);
1555 if (Decl->hasAttr<BlocksAttr>()) {
1556 llvm::DICompileUnit DefUnit;
1557 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1558
1559 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1560
1561 llvm::DIType FieldTy;
1562
1563 QualType FType;
1564 uint64_t FieldSize, FieldOffset;
1565 unsigned FieldAlign;
1566
1567 llvm::DIArray Elements;
1568 llvm::DIType EltTy;
1569
1570 // Build up structure for the byref. See BuildByRefType.
1571 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001572 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001573 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001574 FieldSize = CGM.getContext().getTypeSize(FType);
1575 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001576 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1577 "__isa", DefUnit,
1578 0, FieldSize, FieldAlign,
1579 FieldOffset, 0, FieldTy);
1580 EltTys.push_back(FieldTy);
1581 FieldOffset += FieldSize;
1582
Anders Carlsson20f12a22009-12-06 18:00:51 +00001583 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001584 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001585 FieldSize = CGM.getContext().getTypeSize(FType);
1586 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001587 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1588 "__forwarding", DefUnit,
1589 0, FieldSize, FieldAlign,
1590 FieldOffset, 0, FieldTy);
1591 EltTys.push_back(FieldTy);
1592 FieldOffset += FieldSize;
1593
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001594 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001595 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001596 FieldSize = CGM.getContext().getTypeSize(FType);
1597 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001598 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1599 "__flags", DefUnit,
1600 0, FieldSize, FieldAlign,
1601 FieldOffset, 0, FieldTy);
1602 EltTys.push_back(FieldTy);
1603 FieldOffset += FieldSize;
1604
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001605 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001606 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001607 FieldSize = CGM.getContext().getTypeSize(FType);
1608 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001609 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1610 "__size", DefUnit,
1611 0, FieldSize, FieldAlign,
1612 FieldOffset, 0, FieldTy);
1613 EltTys.push_back(FieldTy);
1614 FieldOffset += FieldSize;
1615
Anders Carlsson20f12a22009-12-06 18:00:51 +00001616 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001617 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001618 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001619 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001620 FieldSize = CGM.getContext().getTypeSize(FType);
1621 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001622 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1623 "__copy_helper", DefUnit,
1624 0, FieldSize, FieldAlign,
1625 FieldOffset, 0, FieldTy);
1626 EltTys.push_back(FieldTy);
1627 FieldOffset += FieldSize;
1628
Anders Carlsson20f12a22009-12-06 18:00:51 +00001629 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001630 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001631 FieldSize = CGM.getContext().getTypeSize(FType);
1632 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001633 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1634 "__destroy_helper", DefUnit,
1635 0, FieldSize, FieldAlign,
1636 FieldOffset, 0, FieldTy);
1637 EltTys.push_back(FieldTy);
1638 FieldOffset += FieldSize;
1639 }
1640
Ken Dyck8b752f12010-01-27 17:10:57 +00001641 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1642 if (Align > CharUnits::fromQuantity(
1643 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001644 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001645 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001646 unsigned NumPaddingBytes
1647 = AlignedOffsetInBytes - FieldOffset/8;
1648
1649 if (NumPaddingBytes > 0) {
1650 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001651 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001652 pad, ArrayType::Normal, 0);
1653 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001654 FieldSize = CGM.getContext().getTypeSize(FType);
1655 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001656 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1657 Unit, "", DefUnit,
1658 0, FieldSize, FieldAlign,
1659 FieldOffset, 0, FieldTy);
1660 EltTys.push_back(FieldTy);
1661 FieldOffset += FieldSize;
1662 }
1663 }
1664
1665 FType = Type;
1666 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001667 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001668 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001669
1670 XOffset = FieldOffset;
1671 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001672 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001673 0, FieldSize, FieldAlign,
1674 FieldOffset, 0, FieldTy);
1675 EltTys.push_back(FieldTy);
1676 FieldOffset += FieldSize;
1677
1678 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1679
1680 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1681
1682 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1683 llvm::DICompileUnit(),
1684 0, FieldOffset, 0, 0, Flags,
1685 llvm::DIType(), Elements);
1686 }
1687
1688 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001689 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001690 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1691 unsigned Line = 0;
1692 if (!PLoc.isInvalid())
1693 Line = PLoc.getLine();
1694 else
1695 Unit = llvm::DICompileUnit();
1696
Ken Dyck199c3d62010-01-11 17:06:35 +00001697 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001698 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001699 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1700 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1701 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1702 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001703 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001704 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1705 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001706 // offset of __forwarding field
1707 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001708 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1709 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1710 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001711 // offset of x field
1712 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001713 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001714 }
1715
1716 // Create the descriptor for the variable.
1717 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001718 DebugFactory.CreateComplexVariable(Tag,
1719 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001720 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001721 addr);
1722 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001723 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001724 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001725
Devang Patel8fae0602009-11-13 19:10:24 +00001726 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001727 llvm::DILocation DO(NULL);
1728 llvm::DILocation DL =
1729 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001730
Chris Lattner23e92c02009-12-28 23:41:39 +00001731 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001732}
1733
Chris Lattner9c85ba32008-11-10 06:08:34 +00001734void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1735 llvm::Value *Storage,
1736 CGBuilderTy &Builder) {
1737 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1738}
1739
Mike Stumpb1a6e682009-09-30 02:43:10 +00001740void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1741 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1742 CodeGenFunction *CGF) {
1743 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1744}
1745
Chris Lattner9c85ba32008-11-10 06:08:34 +00001746/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1747/// variable declaration.
1748void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1749 CGBuilderTy &Builder) {
1750 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1751}
1752
1753
1754
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001755/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001756void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001757 const VarDecl *D) {
1758
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001759 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001760 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001761 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001762 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001763 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001764
Devang Pateleb6d79b2010-02-01 21:34:11 +00001765 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001766 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001768 // CodeGen turns int[] into int[1] so we'll do the same here.
1769 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001771 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001772 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Anders Carlsson20f12a22009-12-06 18:00:51 +00001774 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001775 ArrayType::Normal, 0);
1776 }
Devang Pateleb6d79b2010-02-01 21:34:11 +00001777 llvm::StringRef DeclName = D->getName();
1778 llvm::DIDescriptor DContext =
1779 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1780 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001781 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001782 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001783 Var->hasInternalLinkage(),
1784 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001785}
1786
Devang Patel9ca36b62009-02-26 21:10:26 +00001787/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001788void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001789 ObjCInterfaceDecl *Decl) {
1790 // Create global variable debug descriptor.
1791 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001792 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001793 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1794 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001795
Devang Patel73621622009-11-25 17:37:31 +00001796 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001797
Anders Carlsson20f12a22009-12-06 18:00:51 +00001798 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001799 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Devang Patel9ca36b62009-02-26 21:10:26 +00001801 // CodeGen turns int[] into int[1] so we'll do the same here.
1802 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Devang Patel9ca36b62009-02-26 21:10:26 +00001804 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001805 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Anders Carlsson20f12a22009-12-06 18:00:51 +00001807 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001808 ArrayType::Normal, 0);
1809 }
1810
Devang Patelf6a39b72009-10-20 18:26:30 +00001811 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001812 getOrCreateType(T, Unit),
1813 Var->hasInternalLinkage(),
1814 true/*definition*/, Var);
1815}
Devang Patelabb485f2010-02-01 19:16:32 +00001816
1817/// getOrCreateNamesSpace - Return namespace descriptor for the given
1818/// namespace decl.
1819llvm::DINameSpace
1820CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1821 llvm::DIDescriptor Unit) {
1822 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1823 NameSpaceCache.find(NSDecl);
1824 if (I != NameSpaceCache.end())
1825 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1826
1827 SourceManager &SM = CGM.getContext().getSourceManager();
1828 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1829 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1830
1831 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001832 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001833 llvm::DINameSpace NS =
1834 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1835 llvm::DICompileUnit(Unit.getNode()), LineNo);
1836 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1837 return NS;
1838}