blob: 6f84add037285db72be3f2a2a999ce9f3c3a5ee6 [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));
Devang Patel411894b2010-02-01 22:40:08 +000062
Devang Pateleb6d79b2010-02-01 21:34:11 +000063 // Check namespace.
64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
66
Devang Patel979ec2e2009-10-06 00:35:31 +000067 return CompileUnit;
68}
69
Devang Patel9c6c3a02010-01-14 00:36:21 +000070/// getFunctionName - Get function name for the given FunctionDecl. If the
71/// name is constructred on demand (e.g. C++ destructor) then the name
72/// is stored on the side.
73llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
74 assert (FD && "Invalid FunctionDecl!");
75 IdentifierInfo *FII = FD->getIdentifier();
76 if (FII)
77 return FII->getName();
78
79 // Otherwise construct human readable name for debug info.
80 std::string NS = FD->getNameAsString();
81
82 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000083 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000084 memcpy(StrPtr, NS.data(), NS.length());
85 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000086}
87
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000088/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000089/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000090llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000091 // Get source file information.
92 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000094 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000095 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
97 FileName = PLoc.getFilename();
98 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000101 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +0000102 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +0000103 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000104
Devang Patel446c6192009-04-17 21:06:59 +0000105 // Get absolute path name.
106 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000107 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000108
Devang Patel72240d72009-06-26 18:32:22 +0000109 // See if thie compile unit is representing main source file. Each source
110 // file has corresponding compile unit. There is only one main source
111 // file at a time.
112 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000113 const LangOptions &LO = CGM.getLangOptions();
114 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000115 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000116 if (!CGO.MainFileName.empty()) {
117 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000118 isMain = true;
119 } else {
120 if (Loc.isValid() && SM.isFromMainFile(Loc))
121 isMain = true;
122 }
123 if (isMain)
124 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000125 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000126
Chris Lattner515455a2009-03-25 03:28:08 +0000127 unsigned LangTag;
128 if (LO.CPlusPlus) {
129 if (LO.ObjC1)
130 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
131 else
132 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
133 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000134 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000135 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000136 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000137 } else {
138 LangTag = llvm::dwarf::DW_LANG_C89;
139 }
Devang Patel446c6192009-04-17 21:06:59 +0000140
Benjamin Kramer47daf682009-12-08 11:02:29 +0000141 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000142#ifdef CLANG_VENDOR
143 CLANG_VENDOR
144#endif
145 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000146
147 // Figure out which version of the ObjC runtime we have.
148 unsigned RuntimeVers = 0;
149 if (LO.ObjC1)
150 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000152 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000153 return Unit = DebugFactory.CreateCompileUnit(
154 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
155 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000156}
157
Devang Patel65e99f22009-02-25 01:36:11 +0000158/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000159/// one if necessary.
160llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000161 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000162 unsigned Encoding = 0;
163 switch (BT->getKind()) {
164 default:
165 case BuiltinType::Void:
166 return llvm::DIType();
167 case BuiltinType::UChar:
168 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
169 case BuiltinType::Char_S:
170 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
171 case BuiltinType::UShort:
172 case BuiltinType::UInt:
173 case BuiltinType::ULong:
174 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
175 case BuiltinType::Short:
176 case BuiltinType::Int:
177 case BuiltinType::Long:
178 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
179 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
180 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000181 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000183 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000184 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000185 uint64_t Size = CGM.getContext().getTypeSize(BT);
186 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000187 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Devang Patelca80a5f2009-10-20 19:55:01 +0000189 llvm::DIType DbgTy =
190 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000191 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 Unit, 0, Size, Align,
193 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000194 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000195}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000196
Chris Lattnerb7003772009-04-23 06:13:01 +0000197llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
198 llvm::DICompileUnit Unit) {
199 // Bit size, align and offset of the type.
200 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
201 if (Ty->isComplexIntegerType())
202 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Anders Carlsson20f12a22009-12-06 18:00:51 +0000204 uint64_t Size = CGM.getContext().getTypeSize(Ty);
205 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000206 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Devang Patelca80a5f2009-10-20 19:55:01 +0000208 llvm::DIType DbgTy =
209 DebugFactory.CreateBasicType(Unit, "complex",
210 Unit, 0, Size, Align,
211 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000212 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000213}
214
John McCalla1805292009-09-25 01:40:47 +0000215/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000216/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000217llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
218 QualifierCollector Qc;
219 const Type *T = Qc.strip(Ty);
220
221 // Ignore these qualifiers for now.
222 Qc.removeObjCGCAttr();
223 Qc.removeAddressSpace();
224
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 // We will create one Derived type for one qualifier and recurse to handle any
226 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000228 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000230 Qc.removeConst();
231 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000232 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000233 Qc.removeVolatile();
234 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000235 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000236 Qc.removeRestrict();
237 } else {
238 assert(Qc.empty() && "Unknown type qualifier for debug info");
239 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
John McCalla1805292009-09-25 01:40:47 +0000242 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
243
Daniel Dunbar3845f862008-10-31 03:54:29 +0000244 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
245 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000246 llvm::DIType DbgTy =
247 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
248 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000249 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000250}
251
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000252llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
253 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000254 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000255 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
256 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000257 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000258}
259
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
261 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000262 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
263 Ty->getPointeeType(), Unit);
264}
265
266llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
267 const Type *Ty,
268 QualType PointeeTy,
269 llvm::DICompileUnit Unit) {
270 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000272 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000273
274 // Size is always the size of a pointer. We can't use getTypeSize here
275 // because that does not return the correct value for references.
276 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000277 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
278 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Devang Patelca80a5f2009-10-20 19:55:01 +0000280 return
Anders Carlssona031b352009-11-06 19:19:55 +0000281 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000282 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000283
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000284}
285
Mike Stump9bc093c2009-05-14 02:03:51 +0000286llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
287 llvm::DICompileUnit Unit) {
288 if (BlockLiteralGenericSet)
289 return BlockLiteralGeneric;
290
291 llvm::DICompileUnit DefUnit;
292 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
293
294 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
295
296 llvm::DIType FieldTy;
297
298 QualType FType;
299 uint64_t FieldSize, FieldOffset;
300 unsigned FieldAlign;
301
302 llvm::DIArray Elements;
303 llvm::DIType EltTy, DescTy;
304
305 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000306 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000307 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000308 FieldSize = CGM.getContext().getTypeSize(FType);
309 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000310 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
311 "reserved", DefUnit,
312 0, FieldSize, FieldAlign,
313 FieldOffset, 0, FieldTy);
314 EltTys.push_back(FieldTy);
315
316 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000317 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000318 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000319 FieldSize = CGM.getContext().getTypeSize(FType);
320 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
322 "Size", DefUnit,
323 0, FieldSize, FieldAlign,
324 FieldOffset, 0, FieldTy);
325 EltTys.push_back(FieldTy);
326
327 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000328 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000329 EltTys.clear();
330
Mike Stump3d363c52009-10-02 02:30:50 +0000331 unsigned Flags = llvm::DIType::FlagAppleBlock;
332
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000334 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Mike Stump9bc093c2009-05-14 02:03:51 +0000337 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000338 uint64_t Size = CGM.getContext().getTypeSize(Ty);
339 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
342 Unit, "", llvm::DICompileUnit(),
343 0, Size, Align, 0, 0, EltTy);
344
345 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000347 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000348 FieldSize = CGM.getContext().getTypeSize(FType);
349 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000350 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
351 "__isa", DefUnit,
352 0, FieldSize, FieldAlign,
353 FieldOffset, 0, FieldTy);
354 EltTys.push_back(FieldTy);
355
356 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000357 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000359 FieldSize = CGM.getContext().getTypeSize(FType);
360 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000361 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
362 "__flags", DefUnit,
363 0, FieldSize, FieldAlign,
364 FieldOffset, 0, FieldTy);
365 EltTys.push_back(FieldTy);
366
367 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000368 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000369 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000370 FieldSize = CGM.getContext().getTypeSize(FType);
371 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000372 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
373 "__reserved", DefUnit,
374 0, FieldSize, FieldAlign,
375 FieldOffset, 0, FieldTy);
376 EltTys.push_back(FieldTy);
377
378 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000381 FieldSize = CGM.getContext().getTypeSize(FType);
382 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000383 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
384 "__FuncPtr", DefUnit,
385 0, FieldSize, FieldAlign,
386 FieldOffset, 0, FieldTy);
387 EltTys.push_back(FieldTy);
388
389 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000390 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000391 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000392 FieldSize = CGM.getContext().getTypeSize(Ty);
393 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
395 "__descriptor", DefUnit,
396 0, FieldSize, FieldAlign,
397 FieldOffset, 0, FieldTy);
398 EltTys.push_back(FieldTy);
399
400 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000401 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000402
403 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000404 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000405 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Mike Stump9bc093c2009-05-14 02:03:51 +0000407 BlockLiteralGenericSet = true;
408 BlockLiteralGeneric
409 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
410 "", llvm::DICompileUnit(),
411 0, Size, Align, 0, 0, EltTy);
412 return BlockLiteralGeneric;
413}
414
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
416 llvm::DICompileUnit Unit) {
417 // Typedefs are derived from some other type. If we have a typedef of a
418 // typedef, make sure to emit the whole chain.
419 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421 // We don't set size information, but do specify where the typedef was
422 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000423 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000424 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000425 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000426
Devang Pateleb6d79b2010-02-01 21:34:11 +0000427 llvm::DIDescriptor TyContext
428 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
429 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000430 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000431 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000432 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000433 Ty->getDecl()->getName(), Unit,
434 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000435 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000436}
437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
439 llvm::DICompileUnit Unit) {
440 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000441
Chris Lattner9c85ba32008-11-10 06:08:34 +0000442 // Add the result type at least.
443 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 // Set up remainder of arguments if there is a prototype.
446 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000447 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000448 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
449 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
450 } else {
451 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000452 }
453
Chris Lattner9c85ba32008-11-10 06:08:34 +0000454 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000455 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Devang Patelca80a5f2009-10-20 19:55:01 +0000457 llvm::DIType DbgTy =
458 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
459 Unit, "", llvm::DICompileUnit(),
460 0, 0, 0, 0, 0,
461 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000462 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463}
464
Devang Patel428deb52010-01-19 00:00:59 +0000465/// CollectRecordFields - A helper function to collect debug info for
466/// record fields. This is used while creating debug info entry for a Record.
467void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000468CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000469 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
470 unsigned FieldNo = 0;
471 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000472 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
473 for (RecordDecl::field_iterator I = RD->field_begin(),
474 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000475 I != E; ++I, ++FieldNo) {
476 FieldDecl *Field = *I;
477 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
478
479 llvm::StringRef FieldName = Field->getName();
480
481 // Ignore unnamed fields.
482 if (FieldName.empty())
483 continue;
484
485 // Get the location for the field.
486 SourceLocation FieldDefLoc = Field->getLocation();
487 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
488 llvm::DICompileUnit FieldDefUnit;
489 unsigned FieldLine = 0;
490
491 if (!PLoc.isInvalid()) {
492 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
493 FieldLine = PLoc.getLine();
494 }
495
496 QualType FType = Field->getType();
497 uint64_t FieldSize = 0;
498 unsigned FieldAlign = 0;
499 if (!FType->isIncompleteArrayType()) {
500
501 // Bit size, align and offset of the type.
502 FieldSize = CGM.getContext().getTypeSize(FType);
503 Expr *BitWidth = Field->getBitWidth();
504 if (BitWidth)
505 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
506
507 FieldAlign = CGM.getContext().getTypeAlign(FType);
508 }
509
510 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
511
512 // Create a DW_TAG_member node to remember the offset of this field in the
513 // struct. FIXME: This is an absolutely insane way to capture this
514 // information. When we gut debug info, this should be fixed.
515 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
516 FieldName, FieldDefUnit,
517 FieldLine, FieldSize, FieldAlign,
518 FieldOffset, 0, FieldTy);
519 EltTys.push_back(FieldTy);
520 }
521}
522
Devang Patela6da1922010-01-28 00:28:01 +0000523/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
524/// function type is not updated to include implicit "this" pointer. Use this
525/// routine to get a method type which includes "this" pointer.
526llvm::DIType
527CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
528 llvm::DICompileUnit Unit) {
529 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000530
531 // Static methods do not need "this" pointer argument.
532 if (Method->isStatic())
533 return FnTy;
534
Devang Patela6da1922010-01-28 00:28:01 +0000535 // Add "this" pointer.
536
537 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
538 assert (Args.getNumElements() && "Invalid number of arguments!");
539
540 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
541
542 // First element is always return type. For 'void' functions it is NULL.
543 Elts.push_back(Args.getElement(0));
544
545 // "this" pointer is always first argument.
546 ASTContext &Context = CGM.getContext();
547 QualType ThisPtr =
548 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000549 llvm::DIType ThisPtrType =
550 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
551 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
552 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000553
554 // Copy rest of the arguments.
555 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
556 Elts.push_back(Args.getElement(i));
557
558 llvm::DIArray EltTypeArray =
559 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
560
561 return
562 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
563 Unit, "", llvm::DICompileUnit(),
564 0, 0, 0, 0, 0,
565 llvm::DIType(), EltTypeArray);
566}
567
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000568/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
569/// a single member function GlobalDecl.
570llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000572 llvm::DICompileUnit Unit,
573 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000574 bool IsCtorOrDtor =
575 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
576
577 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000578 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000579 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000580
581 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
582 // make sense to give a single ctor/dtor a linkage name.
583 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000584 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000585
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000586 SourceManager &SM = CGM.getContext().getSourceManager();
587
588 // Get the location for the method.
589 SourceLocation MethodDefLoc = Method->getLocation();
590 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
591 llvm::DICompileUnit MethodDefUnit;
592 unsigned MethodLine = 0;
593
594 if (!PLoc.isInvalid()) {
595 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
596 MethodLine = PLoc.getLine();
597 }
598
599 // Collect virtual method info.
600 llvm::DIType ContainingType;
601 unsigned Virtuality = 0;
602 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000603
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000604 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000605 if (Method->isPure())
606 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
607 else
608 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
609
610 // It doesn't make sense to give a virtual destructor a vtable index,
611 // since a single destructor has two entries in the vtable.
612 if (!isa<CXXDestructorDecl>(Method))
613 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000614 ContainingType = RecordTy;
615 }
616
617 llvm::DISubprogram SP =
618 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
619 MethodLinkageName,
620 MethodDefUnit, MethodLine,
621 MethodTy, /*isLocalToUnit=*/false,
622 Method->isThisDeclarationADefinition(),
623 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000624
625 // Don't cache ctors or dtors since we have to emit multiple functions for
626 // a single ctor or dtor.
627 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
628 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000629
630 return SP;
631}
632
Devang Patel4125fd22010-01-19 01:54:44 +0000633/// CollectCXXMemberFunctions - A helper function to collect debug info for
634/// C++ member functions.This is used while creating debug info entry for
635/// a Record.
636void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000637CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000638 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
639 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000640 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
641 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000642 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000643
Devang Pateld5322da2010-02-09 19:09:28 +0000644 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000645 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000646
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000647 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000648 }
649}
650
Devang Patela245c5b2010-01-25 23:32:18 +0000651/// CollectCXXBases - A helper function to collect debug info for
652/// C++ base classes. This is used while creating debug info entry for
653/// a Record.
654void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000655CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000656 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
657 llvm::DICompositeType &RecordTy) {
658
Devang Patel239cec62010-02-01 21:39:52 +0000659 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
660 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
661 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000662 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()) {
Devang Pateld5322da2010-02-09 19:09:28 +0000669 // virtual base offset index is -ve. The code generator emits dwarf
670 // expression where it expects +ve number.
671 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000672 BFlags = llvm::DIType::FlagVirtual;
673 } else
674 BaseOffset = RL.getBaseClassOffset(Base);
675
676 AccessSpecifier Access = BI->getAccessSpecifier();
677 if (Access == clang::AS_private)
678 BFlags |= llvm::DIType::FlagPrivate;
679 else if (Access == clang::AS_protected)
680 BFlags |= llvm::DIType::FlagProtected;
681
682 llvm::DIType DTy =
683 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
684 RecordTy, llvm::StringRef(),
685 llvm::DICompileUnit(), 0, 0, 0,
686 BaseOffset, BFlags,
687 getOrCreateType(BI->getType(),
688 Unit));
689 EltTys.push_back(DTy);
690 }
Devang Patela245c5b2010-01-25 23:32:18 +0000691}
692
Devang Patel4ce3f202010-01-28 18:11:52 +0000693/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
694llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
695 if (!VTablePtrType.isNull())
696 return VTablePtrType;
697
698 ASTContext &Context = CGM.getContext();
699
700 /* Function type */
701 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
702 STys.push_back(getOrCreateType(Context.IntTy, Unit));
703 llvm::DIArray SElements =
704 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
705 llvm::DIType SubTy =
706 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
707 Unit, "", llvm::DICompileUnit(),
708 0, 0, 0, 0, 0, llvm::DIType(), SElements);
709
710 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
711 llvm::DIType vtbl_ptr_type
712 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
713 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
714 0, Size, 0, 0, 0, SubTy);
715
716 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
717 Unit, "", llvm::DICompileUnit(),
718 0, Size, 0, 0, 0, vtbl_ptr_type);
719 return VTablePtrType;
720}
721
722/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000723llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000725 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000726
727 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000728 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000729 memcpy(StrPtr, Name.data(), Name.length());
730 return llvm::StringRef(StrPtr, Name.length());
731}
732
733
734/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
735/// debug info entry in EltTys vector.
736void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000737CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000738 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000739 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000740
741 // If there is a primary base then it will hold vtable info.
742 if (RL.getPrimaryBase())
743 return;
744
745 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000746 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000747 return;
748
749 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
750 llvm::DIType VPTR
751 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +0000752 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000753 0, Size, 0, 0, 0,
754 getOrCreateVTablePtrType(Unit));
755 EltTys.push_back(VPTR);
756}
757
Devang Patel65e99f22009-02-25 01:36:11 +0000758/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000759llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
760 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000761 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattner9c85ba32008-11-10 06:08:34 +0000763 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000764 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000765 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000766 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000767 Tag = llvm::dwarf::DW_TAG_union_type;
768 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000769 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000770 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000771 }
772
Anders Carlsson20f12a22009-12-06 18:00:51 +0000773 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000774
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000776 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000777 llvm::DICompileUnit DefUnit;
778 unsigned Line = 0;
779 if (!PLoc.isInvalid()) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000780 DefUnit = getOrCreateCompileUnit(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000781 Line = PLoc.getLine();
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattner9c85ba32008-11-10 06:08:34 +0000784 // Records and classes and unions can all be recursive. To handle them, we
785 // first generate a debug descriptor for the struct as a forward declaration.
786 // Then (if it is a definition) we go through and get debug info for all of
787 // its members. Finally, we create a descriptor for the complete type (which
788 // may refer to the forward decl if the struct is recursive) and replace all
789 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000790
Devang Pateld6c5a262010-02-01 21:52:22 +0000791 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000792 // are uniqued so use type name to ensure uniquness.
Devang Pateld0f251b2010-01-20 23:56:40 +0000793 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel411894b2010-02-01 22:40:08 +0000794 llvm::DIDescriptor FDContext =
795 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000796 llvm::DICompositeType FwdDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000797 DebugFactory.CreateCompositeType(Tag, FDContext,
798 STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000799 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000803 if (!RD->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000804 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000805
Eli Friedman14d63652009-11-16 21:04:30 +0000806 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000807 // Otherwise, insert it into the TypeCache so that recursive uses will find
808 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000809 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810
811 // Convert all the elements.
812 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
813
Devang Pateld6c5a262010-02-01 21:52:22 +0000814 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000815 if (CXXDecl) {
816 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000817 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000818 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000819 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000820 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000821 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000822 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000823
824 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000825 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000826 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
827 ContainingType =
828 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
829 else if (CXXDecl->isDynamicClass())
830 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000834 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000835
836 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000837 uint64_t Size = CGM.getContext().getTypeSize(Ty);
838 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Devang Patel411894b2010-02-01 22:40:08 +0000840 llvm::DIDescriptor RDContext =
841 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000842 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000843 DebugFactory.CreateCompositeType(Tag, RDContext,
844 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000845 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000846 llvm::DIType(), Elements,
847 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000848
849 // Now that we have a real decl for the struct, replace anything using the
850 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000851 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000852
Chris Lattner9c85ba32008-11-10 06:08:34 +0000853 return RealDecl;
854}
855
Devang Patel9ca36b62009-02-26 21:10:26 +0000856/// CreateType - get objective-c interface type.
857llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
858 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000859 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Devang Patel9ca36b62009-02-26 21:10:26 +0000861 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000862 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000863
864 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000865 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation());
866 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000867 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Daniel Dunbard86d3362009-05-18 20:51:58 +0000870 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000871
Devang Patel9ca36b62009-02-26 21:10:26 +0000872 // To handle recursive interface, we
873 // first generate a debug descriptor for the struct as a forward declaration.
874 // Then (if it is a definition) we go through and get debug info for all of
875 // its members. Finally, we create a descriptor for the complete type (which
876 // may refer to the forward decl if the struct is recursive) and replace all
877 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000878 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000879 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000880 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000881 llvm::DIType(), llvm::DIArray(),
882 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Devang Patel9ca36b62009-02-26 21:10:26 +0000884 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000885 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000886 return FwdDecl;
887
Devang Patelffffb032009-11-16 20:09:38 +0000888 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000889 // Otherwise, insert it into the TypeCache so that recursive uses will find
890 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000891 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000892
893 // Convert all the elements.
894 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
895
Devang Pateld6c5a262010-02-01 21:52:22 +0000896 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000897 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000898 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000899 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000900 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000901 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000902 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000903 0 /* offset */, 0, SClassTy);
904 EltTys.push_back(InhTag);
905 }
906
Devang Pateld6c5a262010-02-01 21:52:22 +0000907 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000908
909 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000910 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
911 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000912 ObjCIvarDecl *Field = *I;
913 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
914
Devang Patel73621622009-11-25 17:37:31 +0000915 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000916
Devang Patelde135022009-04-27 22:40:36 +0000917 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000918 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000919 continue;
920
Devang Patel9ca36b62009-02-26 21:10:26 +0000921 // Get the location for the field.
922 SourceLocation FieldDefLoc = Field->getLocation();
923 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000924 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
925 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
926
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Devang Patel99c20eb2009-03-20 18:24:39 +0000928 QualType FType = Field->getType();
929 uint64_t FieldSize = 0;
930 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000931
Devang Patel99c20eb2009-03-20 18:24:39 +0000932 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patel99c20eb2009-03-20 18:24:39 +0000934 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000935 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000936 Expr *BitWidth = Field->getBitWidth();
937 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000938 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000939
Anders Carlsson20f12a22009-12-06 18:00:51 +0000940 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000941 }
942
Mike Stump1eb44332009-09-09 15:08:12 +0000943 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
944
Devang Patelc20482b2009-03-19 00:23:53 +0000945 unsigned Flags = 0;
946 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
947 Flags = llvm::DIType::FlagProtected;
948 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
949 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Devang Patel9ca36b62009-02-26 21:10:26 +0000951 // Create a DW_TAG_member node to remember the offset of this field in the
952 // struct. FIXME: This is an absolutely insane way to capture this
953 // information. When we gut debug info, this should be fixed.
954 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
955 FieldName, FieldDefUnit,
956 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000957 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000958 EltTys.push_back(FieldTy);
959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Devang Patel9ca36b62009-02-26 21:10:26 +0000961 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000962 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000963
964 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000965 uint64_t Size = CGM.getContext().getTypeSize(Ty);
966 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Devang Patel6c1fddf2009-07-27 18:42:03 +0000968 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000969 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000970 Line, Size, Align, 0, 0, llvm::DIType(),
971 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000972
973 // Now that we have a real decl for the struct, replace anything using the
974 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000975 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000976
Devang Patel9ca36b62009-02-26 21:10:26 +0000977 return RealDecl;
978}
979
Chris Lattner9c85ba32008-11-10 06:08:34 +0000980llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
981 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000982 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000983
984 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
985
986 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000987 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000988 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000989 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000990 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000991 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Chris Lattner9c85ba32008-11-10 06:08:34 +0000994 // Return a CompositeType for the enum itself.
995 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000996 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000997
Devang Pateld6c5a262010-02-01 21:52:22 +0000998 SourceLocation DefLoc = ED->getLocation();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000999 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001000 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001001 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1002 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1003
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattner9c85ba32008-11-10 06:08:34 +00001005 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001006 uint64_t Size = 0;
1007 unsigned Align = 0;
1008 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001009 Size = CGM.getContext().getTypeSize(Ty);
1010 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Devang Patelca80a5f2009-10-20 19:55:01 +00001013 llvm::DIType DbgTy =
1014 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001015 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001016 Size, Align, 0, 0,
1017 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001018 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001019}
1020
1021llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1022 llvm::DICompileUnit Unit) {
1023 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1024 return CreateType(RT, Unit);
1025 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1026 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner9c85ba32008-11-10 06:08:34 +00001028 return llvm::DIType();
1029}
1030
1031llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1032 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001033 uint64_t Size;
1034 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
1036
Nuno Lopes010d5142009-01-28 00:35:17 +00001037 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001038 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001039 Size = 0;
1040 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001041 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001042 } else if (Ty->isIncompleteArrayType()) {
1043 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001044 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 } else {
1046 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001047 Size = CGM.getContext().getTypeSize(Ty);
1048 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattner9c85ba32008-11-10 06:08:34 +00001051 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1052 // interior arrays, do we care? Why aren't nested arrays represented the
1053 // obvious/recursive way?
1054 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1055 QualType EltTy(Ty, 0);
1056 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001057 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001058 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001059 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001060 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061 // FIXME: Verify this is right for VLAs.
1062 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1063 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattner9c85ba32008-11-10 06:08:34 +00001066 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001067 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001068
Devang Patelca80a5f2009-10-20 19:55:01 +00001069 llvm::DIType DbgTy =
1070 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1071 Unit, "", llvm::DICompileUnit(),
1072 0, Size, Align, 0, 0,
1073 getOrCreateType(EltTy, Unit),
1074 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001075 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001076}
1077
Anders Carlssona031b352009-11-06 19:19:55 +00001078llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1079 llvm::DICompileUnit Unit) {
1080 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1081 Ty, Ty->getPointeeType(), Unit);
1082}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001083
Anders Carlsson20f12a22009-12-06 18:00:51 +00001084llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1085 llvm::DICompileUnit U) {
1086 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1087 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1088
1089 if (!Ty->getPointeeType()->isFunctionType()) {
1090 // We have a data member pointer type.
1091 return PointerDiffDITy;
1092 }
1093
1094 // We have a member function pointer type. Treat it as a struct with two
1095 // ptrdiff_t members.
1096 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1097
1098 uint64_t FieldOffset = 0;
1099 llvm::DIDescriptor ElementTypes[2];
1100
1101 // FIXME: This should probably be a function type instead.
1102 ElementTypes[0] =
1103 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1104 "ptr", llvm::DICompileUnit(), 0,
1105 Info.first, Info.second, FieldOffset, 0,
1106 PointerDiffDITy);
1107 FieldOffset += Info.first;
1108
1109 ElementTypes[1] =
1110 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1111 "ptr", llvm::DICompileUnit(), 0,
1112 Info.first, Info.second, FieldOffset, 0,
1113 PointerDiffDITy);
1114
1115 llvm::DIArray Elements =
1116 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1117 llvm::array_lengthof(ElementTypes));
1118
1119 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1120 U, llvm::StringRef("test"),
1121 llvm::DICompileUnit(), 0, FieldOffset,
1122 0, 0, 0, llvm::DIType(), Elements);
1123}
1124
Douglas Gregor840943d2009-12-21 20:18:30 +00001125static QualType UnwrapTypeForDebugInfo(QualType T) {
1126 do {
1127 QualType LastT = T;
1128 switch (T->getTypeClass()) {
1129 default:
1130 return T;
1131 case Type::TemplateSpecialization:
1132 T = cast<TemplateSpecializationType>(T)->desugar();
1133 break;
1134 case Type::TypeOfExpr: {
1135 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1136 T = Ty->getUnderlyingExpr()->getType();
1137 break;
1138 }
1139 case Type::TypeOf:
1140 T = cast<TypeOfType>(T)->getUnderlyingType();
1141 break;
1142 case Type::Decltype:
1143 T = cast<DecltypeType>(T)->getUnderlyingType();
1144 break;
1145 case Type::QualifiedName:
1146 T = cast<QualifiedNameType>(T)->getNamedType();
1147 break;
1148 case Type::SubstTemplateTypeParm:
1149 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1150 break;
1151 case Type::Elaborated:
1152 T = cast<ElaboratedType>(T)->getUnderlyingType();
1153 break;
1154 }
1155
1156 assert(T != LastT && "Type unwrapping failed to unwrap!");
1157 if (T == LastT)
1158 return T;
1159 } while (true);
1160
1161 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001162}
1163
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001164/// getOrCreateType - Get the type from the cache or create a new
1165/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001166llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1167 llvm::DICompileUnit Unit) {
1168 if (Ty.isNull())
1169 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregor840943d2009-12-21 20:18:30 +00001171 // Unwrap the type as needed for debug information.
1172 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001173
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001174 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001175 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001176 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001177 if (it != TypeCache.end()) {
1178 // Verify that the debug info still exists.
1179 if (&*it->second)
1180 return llvm::DIType(cast<llvm::MDNode>(it->second));
1181 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001182
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001183 // Otherwise create the type.
1184 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001185
1186 // And update the type cache.
1187 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001188 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001189}
1190
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001191/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001192llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1193 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001194 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001195 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001196 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001197
Douglas Gregor2101a822009-12-21 19:57:21 +00001198 const char *Diag = 0;
1199
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001200 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001201 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001202#define TYPE(Class, Base)
1203#define ABSTRACT_TYPE(Class, Base)
1204#define NON_CANONICAL_TYPE(Class, Base)
1205#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1206#include "clang/AST/TypeNodes.def"
1207 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001208
Anders Carlssonbfe69952009-11-06 18:24:04 +00001209 // FIXME: Handle these.
1210 case Type::ExtVector:
1211 case Type::Vector:
1212 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001213
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001214 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001215 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001216 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001217 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1218 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1219 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1220 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001221 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222 return CreateType(cast<BlockPointerType>(Ty), Unit);
1223 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001224 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001225 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001226 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001227 case Type::FunctionProto:
1228 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001229 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001230 case Type::ConstantArray:
1231 case Type::VariableArray:
1232 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001233 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001234
1235 case Type::LValueReference:
1236 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1237
Anders Carlsson20f12a22009-12-06 18:00:51 +00001238 case Type::MemberPointer:
1239 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001240
1241 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001242 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001243 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001244 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001245 case Type::TypeOfExpr:
1246 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001247 case Type::Decltype:
1248 llvm_unreachable("type should have been unwrapped!");
1249 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001250
1251 case Type::RValueReference:
1252 // FIXME: Implement!
1253 Diag = "rvalue references";
1254 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001255 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001256
1257 assert(Diag && "Fall through without a diagnostic?");
1258 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1259 "debug information for %0 is not yet supported");
1260 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1261 << Diag;
1262 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001263}
1264
1265/// EmitFunctionStart - Constructs the debug code for entering a function -
1266/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001267void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001268 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001269 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Devang Patel9c6c3a02010-01-14 00:36:21 +00001271 llvm::StringRef Name;
1272 llvm::StringRef LinkageName;
1273
1274 const Decl *D = GD.getDecl();
1275 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001276 // If there is a DISubprogram for this function available then use it.
1277 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1278 FI = SPCache.find(FD);
1279 if (FI != SPCache.end()) {
1280 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1281 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1282 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001283 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001284 return;
1285 }
1286 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001287 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001288 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001289 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001290 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001291 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001292 } else {
1293 // Use llvm function name as linkage name.
1294 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001295 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001296 if (!Name.empty() && Name[0] == '\01')
1297 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001298 }
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Devang Patel98a200b2010-01-14 18:06:13 +00001300 // It is expected that CurLoc is set before using EmitFunctionStart.
1301 // Usually, CurLoc points to the left bracket location of compound
1302 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001303 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001304 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001305 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Chris Lattner9c85ba32008-11-10 06:08:34 +00001307 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001308 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001309 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001310 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001312 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001313 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001314 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001315}
1316
1317
Chris Lattner9c85ba32008-11-10 06:08:34 +00001318void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001319 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001321 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001322 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001323 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001324 || (SM.getInstantiationLineNumber(CurLoc) ==
1325 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001326 && SM.isFromSameFile(CurLoc, PrevLoc)))
1327 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001328
1329 // Update last state.
1330 PrevLoc = CurLoc;
1331
1332 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001333 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001334 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001335
Devang Patel8fae0602009-11-13 19:10:24 +00001336 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001337 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1338 llvm::DILocation DO(NULL);
1339 llvm::DILocation DL =
1340 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1341 DS, DO);
1342 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001343}
1344
1345/// EmitRegionStart- Constructs the debug code for entering a declarative
1346/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001347void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001348 llvm::DIDescriptor D =
1349 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1350 llvm::DIDescriptor() :
1351 llvm::DIDescriptor(RegionStack.back()));
1352 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001353}
1354
1355/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1356/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001357void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001358 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1359
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001360 // Provide an region stop point.
1361 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001363 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001364}
1365
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001366/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001367void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001368 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001369 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1370
Devang Patel07739032009-03-27 23:16:32 +00001371 // Do not emit variable debug information while generating optimized code.
1372 // The llvm optimizer and code generator are not yet ready to support
1373 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001374 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001375 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001376 return;
1377
Devang Patel239cec62010-02-01 21:39:52 +00001378 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1379 QualType Type = VD->getType();
Mike Stump39605b42009-09-22 02:12:52 +00001380 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Patel239cec62010-02-01 21:39:52 +00001381 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump39605b42009-09-22 02:12:52 +00001382 llvm::DICompileUnit DefUnit;
1383 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1384
1385 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1386
1387 llvm::DIType FieldTy;
1388
1389 QualType FType;
1390 uint64_t FieldSize, FieldOffset;
1391 unsigned FieldAlign;
1392
1393 llvm::DIArray Elements;
1394 llvm::DIType EltTy;
1395
1396 // Build up structure for the byref. See BuildByRefType.
1397 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001398 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001399 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001400 FieldSize = CGM.getContext().getTypeSize(FType);
1401 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001402 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1403 "__isa", DefUnit,
1404 0, FieldSize, FieldAlign,
1405 FieldOffset, 0, FieldTy);
1406 EltTys.push_back(FieldTy);
1407 FieldOffset += FieldSize;
1408
Anders Carlsson20f12a22009-12-06 18:00:51 +00001409 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001410 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001411 FieldSize = CGM.getContext().getTypeSize(FType);
1412 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001413 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1414 "__forwarding", DefUnit,
1415 0, FieldSize, FieldAlign,
1416 FieldOffset, 0, FieldTy);
1417 EltTys.push_back(FieldTy);
1418 FieldOffset += FieldSize;
1419
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001420 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001421 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001422 FieldSize = CGM.getContext().getTypeSize(FType);
1423 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001424 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1425 "__flags", DefUnit,
1426 0, FieldSize, FieldAlign,
1427 FieldOffset, 0, FieldTy);
1428 EltTys.push_back(FieldTy);
1429 FieldOffset += FieldSize;
1430
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001431 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001432 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001433 FieldSize = CGM.getContext().getTypeSize(FType);
1434 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001435 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1436 "__size", DefUnit,
1437 0, FieldSize, FieldAlign,
1438 FieldOffset, 0, FieldTy);
1439 EltTys.push_back(FieldTy);
1440 FieldOffset += FieldSize;
1441
Anders Carlsson20f12a22009-12-06 18:00:51 +00001442 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001443 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001444 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001445 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001446 FieldSize = CGM.getContext().getTypeSize(FType);
1447 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001448 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1449 "__copy_helper", DefUnit,
1450 0, FieldSize, FieldAlign,
1451 FieldOffset, 0, FieldTy);
1452 EltTys.push_back(FieldTy);
1453 FieldOffset += FieldSize;
1454
Anders Carlsson20f12a22009-12-06 18:00:51 +00001455 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001456 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001457 FieldSize = CGM.getContext().getTypeSize(FType);
1458 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001459 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1460 "__destroy_helper", DefUnit,
1461 0, FieldSize, FieldAlign,
1462 FieldOffset, 0, FieldTy);
1463 EltTys.push_back(FieldTy);
1464 FieldOffset += FieldSize;
1465 }
1466
Devang Patel239cec62010-02-01 21:39:52 +00001467 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8b752f12010-01-27 17:10:57 +00001468 if (Align > CharUnits::fromQuantity(
1469 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001470 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001471 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001472 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001473 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001474
1475 if (NumPaddingBytes > 0) {
1476 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001477 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001478 pad, ArrayType::Normal, 0);
1479 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001480 FieldSize = CGM.getContext().getTypeSize(FType);
1481 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001482 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1483 Unit, "", DefUnit,
1484 0, FieldSize, FieldAlign,
1485 FieldOffset, 0, FieldTy);
1486 EltTys.push_back(FieldTy);
1487 FieldOffset += FieldSize;
1488 }
1489 }
1490
1491 FType = Type;
1492 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001493 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001494 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001495
1496 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +00001497 VD->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001498 0, FieldSize, FieldAlign,
1499 FieldOffset, 0, FieldTy);
1500 EltTys.push_back(FieldTy);
1501 FieldOffset += FieldSize;
1502
1503 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1504
1505 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1506
1507 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1508 llvm::DICompileUnit(),
1509 0, FieldOffset, 0, 0, Flags,
1510 llvm::DIType(), Elements);
1511 }
Chris Lattner650cea92009-05-05 04:57:08 +00001512
Chris Lattner9c85ba32008-11-10 06:08:34 +00001513 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001514 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001515 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001516 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001517 unsigned Column = 0;
1518 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001519 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001520 Column = PLoc.getColumn();
1521 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001522 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Chris Lattner9c85ba32008-11-10 06:08:34 +00001525 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001526 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001527 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001528 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001529 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001530 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001531 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001532 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001533
Devang Patel8fae0602009-11-13 19:10:24 +00001534 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001535 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001536 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1537
Chris Lattner23e92c02009-12-28 23:41:39 +00001538 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001539}
1540
Mike Stumpb1a6e682009-09-30 02:43:10 +00001541/// EmitDeclare - Emit local variable declaration debug info.
1542void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1543 llvm::Value *Storage, CGBuilderTy &Builder,
1544 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001545 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001546 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1547
1548 // Do not emit variable debug information while generating optimized code.
1549 // The llvm optimizer and code generator are not yet ready to support
1550 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001551 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001552 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001553 return;
1554
1555 uint64_t XOffset = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001556 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1557 QualType Type = VD->getType();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001558 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Pateld6c5a262010-02-01 21:52:22 +00001559 if (VD->hasAttr<BlocksAttr>()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001560 llvm::DICompileUnit DefUnit;
1561 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1562
1563 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1564
1565 llvm::DIType FieldTy;
1566
1567 QualType FType;
1568 uint64_t FieldSize, FieldOffset;
1569 unsigned FieldAlign;
1570
1571 llvm::DIArray Elements;
1572 llvm::DIType EltTy;
1573
1574 // Build up structure for the byref. See BuildByRefType.
1575 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001576 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001577 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001578 FieldSize = CGM.getContext().getTypeSize(FType);
1579 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001580 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1581 "__isa", DefUnit,
1582 0, FieldSize, FieldAlign,
1583 FieldOffset, 0, FieldTy);
1584 EltTys.push_back(FieldTy);
1585 FieldOffset += FieldSize;
1586
Anders Carlsson20f12a22009-12-06 18:00:51 +00001587 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001588 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001589 FieldSize = CGM.getContext().getTypeSize(FType);
1590 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001591 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1592 "__forwarding", DefUnit,
1593 0, FieldSize, FieldAlign,
1594 FieldOffset, 0, FieldTy);
1595 EltTys.push_back(FieldTy);
1596 FieldOffset += FieldSize;
1597
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001598 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001600 FieldSize = CGM.getContext().getTypeSize(FType);
1601 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001602 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1603 "__flags", DefUnit,
1604 0, FieldSize, FieldAlign,
1605 FieldOffset, 0, FieldTy);
1606 EltTys.push_back(FieldTy);
1607 FieldOffset += FieldSize;
1608
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001609 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001610 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001611 FieldSize = CGM.getContext().getTypeSize(FType);
1612 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001613 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1614 "__size", DefUnit,
1615 0, FieldSize, FieldAlign,
1616 FieldOffset, 0, FieldTy);
1617 EltTys.push_back(FieldTy);
1618 FieldOffset += FieldSize;
1619
Anders Carlsson20f12a22009-12-06 18:00:51 +00001620 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001621 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001622 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001623 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001624 FieldSize = CGM.getContext().getTypeSize(FType);
1625 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001626 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1627 "__copy_helper", DefUnit,
1628 0, FieldSize, FieldAlign,
1629 FieldOffset, 0, FieldTy);
1630 EltTys.push_back(FieldTy);
1631 FieldOffset += FieldSize;
1632
Anders Carlsson20f12a22009-12-06 18:00:51 +00001633 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001634 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001635 FieldSize = CGM.getContext().getTypeSize(FType);
1636 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001637 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1638 "__destroy_helper", DefUnit,
1639 0, FieldSize, FieldAlign,
1640 FieldOffset, 0, FieldTy);
1641 EltTys.push_back(FieldTy);
1642 FieldOffset += FieldSize;
1643 }
1644
Devang Pateld6c5a262010-02-01 21:52:22 +00001645 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8b752f12010-01-27 17:10:57 +00001646 if (Align > CharUnits::fromQuantity(
1647 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001648 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001649 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001650 unsigned NumPaddingBytes
1651 = AlignedOffsetInBytes - FieldOffset/8;
1652
1653 if (NumPaddingBytes > 0) {
1654 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001655 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001656 pad, ArrayType::Normal, 0);
1657 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001658 FieldSize = CGM.getContext().getTypeSize(FType);
1659 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001660 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1661 Unit, "", DefUnit,
1662 0, FieldSize, FieldAlign,
1663 FieldOffset, 0, FieldTy);
1664 EltTys.push_back(FieldTy);
1665 FieldOffset += FieldSize;
1666 }
1667 }
1668
1669 FType = Type;
1670 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001671 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001672 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001673
1674 XOffset = FieldOffset;
1675 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld6c5a262010-02-01 21:52:22 +00001676 VD->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001677 0, FieldSize, FieldAlign,
1678 FieldOffset, 0, FieldTy);
1679 EltTys.push_back(FieldTy);
1680 FieldOffset += FieldSize;
1681
1682 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1683
1684 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1685
1686 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1687 llvm::DICompileUnit(),
1688 0, FieldOffset, 0, 0, Flags,
1689 llvm::DIType(), Elements);
1690 }
1691
1692 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001693 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001694 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001695 unsigned Line = 0;
1696 if (!PLoc.isInvalid())
1697 Line = PLoc.getLine();
1698 else
1699 Unit = llvm::DICompileUnit();
1700
Devang Pateld6c5a262010-02-01 21:52:22 +00001701 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001702 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001703 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1704 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1705 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1706 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001707 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001708 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1709 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001710 // offset of __forwarding field
1711 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001712 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1713 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1714 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001715 // offset of x field
1716 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001717 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001718 }
1719
1720 // Create the descriptor for the variable.
1721 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001722 DebugFactory.CreateComplexVariable(Tag,
1723 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001724 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001725 addr);
1726 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001727 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001728 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001729
Devang Patel8fae0602009-11-13 19:10:24 +00001730 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001731 llvm::DILocation DO(NULL);
1732 llvm::DILocation DL =
1733 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001734
Chris Lattner23e92c02009-12-28 23:41:39 +00001735 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001736}
1737
Devang Pateld6c5a262010-02-01 21:52:22 +00001738void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001739 llvm::Value *Storage,
1740 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001741 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001742}
1743
Mike Stumpb1a6e682009-09-30 02:43:10 +00001744void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1745 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1746 CodeGenFunction *CGF) {
1747 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1748}
1749
Chris Lattner9c85ba32008-11-10 06:08:34 +00001750/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1751/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001752void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001753 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001754 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001755}
1756
1757
1758
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001759/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001760void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001761 const VarDecl *D) {
1762
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001763 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001764 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001765 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001766 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001767 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001768
Devang Pateleb6d79b2010-02-01 21:34:11 +00001769 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001770 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001772 // CodeGen turns int[] into int[1] so we'll do the same here.
1773 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001775 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001776 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Anders Carlsson20f12a22009-12-06 18:00:51 +00001778 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001779 ArrayType::Normal, 0);
1780 }
Devang Pateleb6d79b2010-02-01 21:34:11 +00001781 llvm::StringRef DeclName = D->getName();
1782 llvm::DIDescriptor DContext =
1783 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1784 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001785 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001786 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001787 Var->hasInternalLinkage(),
1788 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001789}
1790
Devang Patel9ca36b62009-02-26 21:10:26 +00001791/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001792void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001793 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001794 // Create global variable debug descriptor.
Devang Pateld6c5a262010-02-01 21:52:22 +00001795 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001796 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001797 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001798 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001799
Devang Pateld6c5a262010-02-01 21:52:22 +00001800 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001801
Devang Pateld6c5a262010-02-01 21:52:22 +00001802 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001803 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Devang Patel9ca36b62009-02-26 21:10:26 +00001805 // CodeGen turns int[] into int[1] so we'll do the same here.
1806 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Devang Patel9ca36b62009-02-26 21:10:26 +00001808 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001809 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Anders Carlsson20f12a22009-12-06 18:00:51 +00001811 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001812 ArrayType::Normal, 0);
1813 }
1814
Devang Patelf6a39b72009-10-20 18:26:30 +00001815 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001816 getOrCreateType(T, Unit),
1817 Var->hasInternalLinkage(),
1818 true/*definition*/, Var);
1819}
Devang Patelabb485f2010-02-01 19:16:32 +00001820
1821/// getOrCreateNamesSpace - Return namespace descriptor for the given
1822/// namespace decl.
1823llvm::DINameSpace
1824CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1825 llvm::DIDescriptor Unit) {
1826 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1827 NameSpaceCache.find(NSDecl);
1828 if (I != NameSpaceCache.end())
1829 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1830
1831 SourceManager &SM = CGM.getContext().getSourceManager();
1832 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1833 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1834
1835 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001836 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001837 llvm::DINameSpace NS =
1838 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1839 llvm::DICompileUnit(Unit.getNode()), LineNo);
1840 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1841 return NS;
1842}