blob: fffba853480bfa7069f0dd99ceb0a9e2008c8ef4 [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()));
549 Elts.push_back(getOrCreateType(ThisPtr, Unit));
550
551 // Copy rest of the arguments.
552 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
553 Elts.push_back(Args.getElement(i));
554
555 llvm::DIArray EltTypeArray =
556 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
557
558 return
559 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
560 Unit, "", llvm::DICompileUnit(),
561 0, 0, 0, 0, 0,
562 llvm::DIType(), EltTypeArray);
563}
564
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000565/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
566/// a single member function GlobalDecl.
567llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000568CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000569 llvm::DICompileUnit Unit,
570 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571 bool IsCtorOrDtor =
572 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
573
574 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000575 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000576 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000577
578 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
579 // make sense to give a single ctor/dtor a linkage name.
580 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000581 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000582
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000583 SourceManager &SM = CGM.getContext().getSourceManager();
584
585 // Get the location for the method.
586 SourceLocation MethodDefLoc = Method->getLocation();
587 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
588 llvm::DICompileUnit MethodDefUnit;
589 unsigned MethodLine = 0;
590
591 if (!PLoc.isInvalid()) {
592 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
593 MethodLine = PLoc.getLine();
594 }
595
596 // Collect virtual method info.
597 llvm::DIType ContainingType;
598 unsigned Virtuality = 0;
599 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000600
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000601 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000602 if (Method->isPure())
603 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
604 else
605 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
606
607 // It doesn't make sense to give a virtual destructor a vtable index,
608 // since a single destructor has two entries in the vtable.
609 if (!isa<CXXDestructorDecl>(Method))
610 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000611 ContainingType = RecordTy;
612 }
613
614 llvm::DISubprogram SP =
615 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
616 MethodLinkageName,
617 MethodDefUnit, MethodLine,
618 MethodTy, /*isLocalToUnit=*/false,
619 Method->isThisDeclarationADefinition(),
620 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000621
622 // Don't cache ctors or dtors since we have to emit multiple functions for
623 // a single ctor or dtor.
624 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
625 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626
627 return SP;
628}
629
Devang Patel4125fd22010-01-19 01:54:44 +0000630/// CollectCXXMemberFunctions - A helper function to collect debug info for
631/// C++ member functions.This is used while creating debug info entry for
632/// a Record.
633void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000634CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000635 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
636 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000637 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
638 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000639 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000640
641 if (Method->isImplicit())
642 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000643
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000644 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000645 }
646}
647
Devang Patela245c5b2010-01-25 23:32:18 +0000648/// CollectCXXBases - A helper function to collect debug info for
649/// C++ base classes. This is used while creating debug info entry for
650/// a Record.
651void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000652CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000653 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
654 llvm::DICompositeType &RecordTy) {
655
Devang Patel239cec62010-02-01 21:39:52 +0000656 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
657 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
658 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000659 unsigned BFlags = 0;
660 uint64_t BaseOffset;
661
662 const CXXRecordDecl *Base =
663 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
664
665 if (BI->isVirtual()) {
666 BaseOffset = RL.getVBaseClassOffset(Base);
667 BFlags = llvm::DIType::FlagVirtual;
668 } else
669 BaseOffset = RL.getBaseClassOffset(Base);
670
671 AccessSpecifier Access = BI->getAccessSpecifier();
672 if (Access == clang::AS_private)
673 BFlags |= llvm::DIType::FlagPrivate;
674 else if (Access == clang::AS_protected)
675 BFlags |= llvm::DIType::FlagProtected;
676
677 llvm::DIType DTy =
678 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
679 RecordTy, llvm::StringRef(),
680 llvm::DICompileUnit(), 0, 0, 0,
681 BaseOffset, BFlags,
682 getOrCreateType(BI->getType(),
683 Unit));
684 EltTys.push_back(DTy);
685 }
Devang Patela245c5b2010-01-25 23:32:18 +0000686}
687
Devang Patel4ce3f202010-01-28 18:11:52 +0000688/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
689llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
690 if (!VTablePtrType.isNull())
691 return VTablePtrType;
692
693 ASTContext &Context = CGM.getContext();
694
695 /* Function type */
696 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
697 STys.push_back(getOrCreateType(Context.IntTy, Unit));
698 llvm::DIArray SElements =
699 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
700 llvm::DIType SubTy =
701 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
702 Unit, "", llvm::DICompileUnit(),
703 0, 0, 0, 0, 0, llvm::DIType(), SElements);
704
705 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
706 llvm::DIType vtbl_ptr_type
707 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
708 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
709 0, Size, 0, 0, 0, SubTy);
710
711 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
712 Unit, "", llvm::DICompileUnit(),
713 0, Size, 0, 0, 0, vtbl_ptr_type);
714 return VTablePtrType;
715}
716
717/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000718llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000719 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000720 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000721
722 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000723 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000724 memcpy(StrPtr, Name.data(), Name.length());
725 return llvm::StringRef(StrPtr, Name.length());
726}
727
728
729/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
730/// debug info entry in EltTys vector.
731void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000732CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000733 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000734 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000735
736 // If there is a primary base then it will hold vtable info.
737 if (RL.getPrimaryBase())
738 return;
739
740 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000741 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000742 return;
743
744 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
745 llvm::DIType VPTR
746 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +0000747 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000748 0, Size, 0, 0, 0,
749 getOrCreateVTablePtrType(Unit));
750 EltTys.push_back(VPTR);
751}
752
Devang Patel65e99f22009-02-25 01:36:11 +0000753/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
755 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000756 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000759 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000760 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000761 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 Tag = llvm::dwarf::DW_TAG_union_type;
763 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000764 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000765 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000766 }
767
Anders Carlsson20f12a22009-12-06 18:00:51 +0000768 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000769
Chris Lattner9c85ba32008-11-10 06:08:34 +0000770 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000771 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000772 llvm::DICompileUnit DefUnit;
773 unsigned Line = 0;
774 if (!PLoc.isInvalid()) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000775 DefUnit = getOrCreateCompileUnit(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000776 Line = PLoc.getLine();
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 // Records and classes and unions can all be recursive. To handle them, we
780 // first generate a debug descriptor for the struct as a forward declaration.
781 // Then (if it is a definition) we go through and get debug info for all of
782 // its members. Finally, we create a descriptor for the complete type (which
783 // may refer to the forward decl if the struct is recursive) and replace all
784 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000785
Devang Pateld6c5a262010-02-01 21:52:22 +0000786 // A RD->getName() is not unique. However, the debug info descriptors
Devang Pateld0f251b2010-01-20 23:56:40 +0000787 // are uniqued. The debug info descriptor describing record's context is
788 // necessary to keep two Decl's descriptor unique if their name match.
789 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
790 // use type's name in FwdDecl.
791 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel411894b2010-02-01 22:40:08 +0000792 llvm::DIDescriptor FDContext =
793 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000794 llvm::DICompositeType FwdDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000795 DebugFactory.CreateCompositeType(Tag, FDContext,
796 STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000797 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattner9c85ba32008-11-10 06:08:34 +0000800 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000801 if (!RD->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000803
Eli Friedman14d63652009-11-16 21:04:30 +0000804 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805 // Otherwise, insert it into the TypeCache so that recursive uses will find
806 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000807 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808
809 // Convert all the elements.
810 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
811
Devang Pateld6c5a262010-02-01 21:52:22 +0000812 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000813 if (CXXDecl) {
814 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000815 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000816 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000817 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000818 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000819 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000820 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000821
822 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000823 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000824 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
825 ContainingType =
826 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
827 else if (CXXDecl->isDynamicClass())
828 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattner9c85ba32008-11-10 06:08:34 +0000831 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000832 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833
834 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000835 uint64_t Size = CGM.getContext().getTypeSize(Ty);
836 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Devang Patel411894b2010-02-01 22:40:08 +0000838 llvm::DIDescriptor RDContext =
839 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000840 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000841 DebugFactory.CreateCompositeType(Tag, RDContext,
842 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000843 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000844 llvm::DIType(), Elements,
845 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000846
847 // Now that we have a real decl for the struct, replace anything using the
848 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000849 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000850
Chris Lattner9c85ba32008-11-10 06:08:34 +0000851 return RealDecl;
852}
853
Devang Patel9ca36b62009-02-26 21:10:26 +0000854/// CreateType - get objective-c interface type.
855llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
856 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000857 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Devang Patel9ca36b62009-02-26 21:10:26 +0000859 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000860 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000861
862 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000863 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation());
864 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000865 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
866
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Daniel Dunbard86d3362009-05-18 20:51:58 +0000868 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000869
Devang Patel9ca36b62009-02-26 21:10:26 +0000870 // To handle recursive interface, we
871 // first generate a debug descriptor for the struct as a forward declaration.
872 // Then (if it is a definition) we go through and get debug info for all of
873 // its members. Finally, we create a descriptor for the complete type (which
874 // may refer to the forward decl if the struct is recursive) and replace all
875 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000876 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000877 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000878 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000879 llvm::DIType(), llvm::DIArray(),
880 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Devang Patel9ca36b62009-02-26 21:10:26 +0000882 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000884 return FwdDecl;
885
Devang Patelffffb032009-11-16 20:09:38 +0000886 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000887 // Otherwise, insert it into the TypeCache so that recursive uses will find
888 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000889 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000890
891 // Convert all the elements.
892 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
893
Devang Pateld6c5a262010-02-01 21:52:22 +0000894 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000895 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000897 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000898 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000899 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000900 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000901 0 /* offset */, 0, SClassTy);
902 EltTys.push_back(InhTag);
903 }
904
Devang Pateld6c5a262010-02-01 21:52:22 +0000905 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000906
907 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000908 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
909 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000910 ObjCIvarDecl *Field = *I;
911 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
912
Devang Patel73621622009-11-25 17:37:31 +0000913 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000914
Devang Patelde135022009-04-27 22:40:36 +0000915 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000916 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000917 continue;
918
Devang Patel9ca36b62009-02-26 21:10:26 +0000919 // Get the location for the field.
920 SourceLocation FieldDefLoc = Field->getLocation();
921 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000922 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
923 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Devang Patel99c20eb2009-03-20 18:24:39 +0000926 QualType FType = Field->getType();
927 uint64_t FieldSize = 0;
928 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000929
Devang Patel99c20eb2009-03-20 18:24:39 +0000930 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Devang Patel99c20eb2009-03-20 18:24:39 +0000932 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000933 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000934 Expr *BitWidth = Field->getBitWidth();
935 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000936 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000937
Anders Carlsson20f12a22009-12-06 18:00:51 +0000938 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000939 }
940
Mike Stump1eb44332009-09-09 15:08:12 +0000941 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
942
Devang Patelc20482b2009-03-19 00:23:53 +0000943 unsigned Flags = 0;
944 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
945 Flags = llvm::DIType::FlagProtected;
946 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
947 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Devang Patel9ca36b62009-02-26 21:10:26 +0000949 // Create a DW_TAG_member node to remember the offset of this field in the
950 // struct. FIXME: This is an absolutely insane way to capture this
951 // information. When we gut debug info, this should be fixed.
952 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
953 FieldName, FieldDefUnit,
954 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000955 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000956 EltTys.push_back(FieldTy);
957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Devang Patel9ca36b62009-02-26 21:10:26 +0000959 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000960 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000961
962 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000963 uint64_t Size = CGM.getContext().getTypeSize(Ty);
964 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Devang Patel6c1fddf2009-07-27 18:42:03 +0000966 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000967 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000968 Line, Size, Align, 0, 0, llvm::DIType(),
969 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000970
971 // Now that we have a real decl for the struct, replace anything using the
972 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000973 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000974
Devang Patel9ca36b62009-02-26 21:10:26 +0000975 return RealDecl;
976}
977
Chris Lattner9c85ba32008-11-10 06:08:34 +0000978llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
979 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000980 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981
982 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
983
984 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000985 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000986 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000987 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000988 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000989 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner9c85ba32008-11-10 06:08:34 +0000992 // Return a CompositeType for the enum itself.
993 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000994 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000995
Devang Pateld6c5a262010-02-01 21:52:22 +0000996 SourceLocation DefLoc = ED->getLocation();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000997 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000998 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000999 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1000 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1001
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattner9c85ba32008-11-10 06:08:34 +00001003 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001004 uint64_t Size = 0;
1005 unsigned Align = 0;
1006 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001007 Size = CGM.getContext().getTypeSize(Ty);
1008 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Devang Patelca80a5f2009-10-20 19:55:01 +00001011 llvm::DIType DbgTy =
1012 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001013 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001014 Size, Align, 0, 0,
1015 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001016 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001017}
1018
1019llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1020 llvm::DICompileUnit Unit) {
1021 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1022 return CreateType(RT, Unit);
1023 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1024 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner9c85ba32008-11-10 06:08:34 +00001026 return llvm::DIType();
1027}
1028
1029llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1030 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001031 uint64_t Size;
1032 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001033
1034
Nuno Lopes010d5142009-01-28 00:35:17 +00001035 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001036 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 Size = 0;
1038 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001039 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001040 } else if (Ty->isIncompleteArrayType()) {
1041 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001042 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001043 } else {
1044 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001045 Size = CGM.getContext().getTypeSize(Ty);
1046 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1050 // interior arrays, do we care? Why aren't nested arrays represented the
1051 // obvious/recursive way?
1052 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1053 QualType EltTy(Ty, 0);
1054 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001055 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001056 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001057 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001058 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001059 // FIXME: Verify this is right for VLAs.
1060 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1061 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Chris Lattner9c85ba32008-11-10 06:08:34 +00001064 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001065 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001066
Devang Patelca80a5f2009-10-20 19:55:01 +00001067 llvm::DIType DbgTy =
1068 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1069 Unit, "", llvm::DICompileUnit(),
1070 0, Size, Align, 0, 0,
1071 getOrCreateType(EltTy, Unit),
1072 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001073 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001074}
1075
Anders Carlssona031b352009-11-06 19:19:55 +00001076llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1077 llvm::DICompileUnit Unit) {
1078 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1079 Ty, Ty->getPointeeType(), Unit);
1080}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081
Anders Carlsson20f12a22009-12-06 18:00:51 +00001082llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1083 llvm::DICompileUnit U) {
1084 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1085 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1086
1087 if (!Ty->getPointeeType()->isFunctionType()) {
1088 // We have a data member pointer type.
1089 return PointerDiffDITy;
1090 }
1091
1092 // We have a member function pointer type. Treat it as a struct with two
1093 // ptrdiff_t members.
1094 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1095
1096 uint64_t FieldOffset = 0;
1097 llvm::DIDescriptor ElementTypes[2];
1098
1099 // FIXME: This should probably be a function type instead.
1100 ElementTypes[0] =
1101 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1102 "ptr", llvm::DICompileUnit(), 0,
1103 Info.first, Info.second, FieldOffset, 0,
1104 PointerDiffDITy);
1105 FieldOffset += Info.first;
1106
1107 ElementTypes[1] =
1108 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1109 "ptr", llvm::DICompileUnit(), 0,
1110 Info.first, Info.second, FieldOffset, 0,
1111 PointerDiffDITy);
1112
1113 llvm::DIArray Elements =
1114 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1115 llvm::array_lengthof(ElementTypes));
1116
1117 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1118 U, llvm::StringRef("test"),
1119 llvm::DICompileUnit(), 0, FieldOffset,
1120 0, 0, 0, llvm::DIType(), Elements);
1121}
1122
Douglas Gregor840943d2009-12-21 20:18:30 +00001123static QualType UnwrapTypeForDebugInfo(QualType T) {
1124 do {
1125 QualType LastT = T;
1126 switch (T->getTypeClass()) {
1127 default:
1128 return T;
1129 case Type::TemplateSpecialization:
1130 T = cast<TemplateSpecializationType>(T)->desugar();
1131 break;
1132 case Type::TypeOfExpr: {
1133 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1134 T = Ty->getUnderlyingExpr()->getType();
1135 break;
1136 }
1137 case Type::TypeOf:
1138 T = cast<TypeOfType>(T)->getUnderlyingType();
1139 break;
1140 case Type::Decltype:
1141 T = cast<DecltypeType>(T)->getUnderlyingType();
1142 break;
1143 case Type::QualifiedName:
1144 T = cast<QualifiedNameType>(T)->getNamedType();
1145 break;
1146 case Type::SubstTemplateTypeParm:
1147 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1148 break;
1149 case Type::Elaborated:
1150 T = cast<ElaboratedType>(T)->getUnderlyingType();
1151 break;
1152 }
1153
1154 assert(T != LastT && "Type unwrapping failed to unwrap!");
1155 if (T == LastT)
1156 return T;
1157 } while (true);
1158
1159 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001160}
1161
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001162/// getOrCreateType - Get the type from the cache or create a new
1163/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001164llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1165 llvm::DICompileUnit Unit) {
1166 if (Ty.isNull())
1167 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor840943d2009-12-21 20:18:30 +00001169 // Unwrap the type as needed for debug information.
1170 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001171
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001172 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001173 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001174 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001175 if (it != TypeCache.end()) {
1176 // Verify that the debug info still exists.
1177 if (&*it->second)
1178 return llvm::DIType(cast<llvm::MDNode>(it->second));
1179 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001180
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001181 // Otherwise create the type.
1182 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001183
1184 // And update the type cache.
1185 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001186 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001187}
1188
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001189/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001190llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1191 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001192 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001193 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001194 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001195
Douglas Gregor2101a822009-12-21 19:57:21 +00001196 const char *Diag = 0;
1197
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001198 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001199 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001200#define TYPE(Class, Base)
1201#define ABSTRACT_TYPE(Class, Base)
1202#define NON_CANONICAL_TYPE(Class, Base)
1203#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1204#include "clang/AST/TypeNodes.def"
1205 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001206
Anders Carlssonbfe69952009-11-06 18:24:04 +00001207 // FIXME: Handle these.
1208 case Type::ExtVector:
1209 case Type::Vector:
1210 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001211
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001212 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001213 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001214 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001215 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1216 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1217 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1218 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001219 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001220 return CreateType(cast<BlockPointerType>(Ty), Unit);
1221 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001222 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001223 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001224 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225 case Type::FunctionProto:
1226 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001227 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001228 case Type::ConstantArray:
1229 case Type::VariableArray:
1230 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001231 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001232
1233 case Type::LValueReference:
1234 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1235
Anders Carlsson20f12a22009-12-06 18:00:51 +00001236 case Type::MemberPointer:
1237 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001238
1239 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001240 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001241 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001242 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001243 case Type::TypeOfExpr:
1244 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001245 case Type::Decltype:
1246 llvm_unreachable("type should have been unwrapped!");
1247 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001248
1249 case Type::RValueReference:
1250 // FIXME: Implement!
1251 Diag = "rvalue references";
1252 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001253 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001254
1255 assert(Diag && "Fall through without a diagnostic?");
1256 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1257 "debug information for %0 is not yet supported");
1258 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1259 << Diag;
1260 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001261}
1262
1263/// EmitFunctionStart - Constructs the debug code for entering a function -
1264/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001265void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001266 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001267 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Devang Patel9c6c3a02010-01-14 00:36:21 +00001269 llvm::StringRef Name;
1270 llvm::StringRef LinkageName;
1271
1272 const Decl *D = GD.getDecl();
1273 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001274 // If there is a DISubprogram for this function available then use it.
1275 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1276 FI = SPCache.find(FD);
1277 if (FI != SPCache.end()) {
1278 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1279 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1280 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001281 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001282 return;
1283 }
1284 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001285 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001286 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001287 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001288 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001289 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001290 } else {
1291 // Use llvm function name as linkage name.
1292 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001293 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001294 if (!Name.empty() && Name[0] == '\01')
1295 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Devang Patel98a200b2010-01-14 18:06:13 +00001298 // It is expected that CurLoc is set before using EmitFunctionStart.
1299 // Usually, CurLoc points to the left bracket location of compound
1300 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001301 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001302 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001303 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Chris Lattner9c85ba32008-11-10 06:08:34 +00001305 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001306 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001307 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001308 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001310 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001311 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001312 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001313}
1314
1315
Chris Lattner9c85ba32008-11-10 06:08:34 +00001316void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001317 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001319 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001320 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001321 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001322 || (SM.getInstantiationLineNumber(CurLoc) ==
1323 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001324 && SM.isFromSameFile(CurLoc, PrevLoc)))
1325 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001326
1327 // Update last state.
1328 PrevLoc = CurLoc;
1329
1330 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001331 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001332 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001333
Devang Patel8fae0602009-11-13 19:10:24 +00001334 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001335 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1336 llvm::DILocation DO(NULL);
1337 llvm::DILocation DL =
1338 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1339 DS, DO);
1340 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001341}
1342
1343/// EmitRegionStart- Constructs the debug code for entering a declarative
1344/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001345void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001346 llvm::DIDescriptor D =
1347 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1348 llvm::DIDescriptor() :
1349 llvm::DIDescriptor(RegionStack.back()));
1350 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001351}
1352
1353/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1354/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001355void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001356 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1357
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001358 // Provide an region stop point.
1359 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001361 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001362}
1363
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001364/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001365void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001366 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001367 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1368
Devang Patel07739032009-03-27 23:16:32 +00001369 // Do not emit variable debug information while generating optimized code.
1370 // The llvm optimizer and code generator are not yet ready to support
1371 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001372 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001373 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001374 return;
1375
Devang Patel239cec62010-02-01 21:39:52 +00001376 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1377 QualType Type = VD->getType();
Mike Stump39605b42009-09-22 02:12:52 +00001378 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Patel239cec62010-02-01 21:39:52 +00001379 if (VD->hasAttr<BlocksAttr>()) {
Mike Stump39605b42009-09-22 02:12:52 +00001380 llvm::DICompileUnit DefUnit;
1381 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1382
1383 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1384
1385 llvm::DIType FieldTy;
1386
1387 QualType FType;
1388 uint64_t FieldSize, FieldOffset;
1389 unsigned FieldAlign;
1390
1391 llvm::DIArray Elements;
1392 llvm::DIType EltTy;
1393
1394 // Build up structure for the byref. See BuildByRefType.
1395 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001396 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001397 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001398 FieldSize = CGM.getContext().getTypeSize(FType);
1399 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001400 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1401 "__isa", DefUnit,
1402 0, FieldSize, FieldAlign,
1403 FieldOffset, 0, FieldTy);
1404 EltTys.push_back(FieldTy);
1405 FieldOffset += FieldSize;
1406
Anders Carlsson20f12a22009-12-06 18:00:51 +00001407 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001408 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001409 FieldSize = CGM.getContext().getTypeSize(FType);
1410 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001411 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1412 "__forwarding", DefUnit,
1413 0, FieldSize, FieldAlign,
1414 FieldOffset, 0, FieldTy);
1415 EltTys.push_back(FieldTy);
1416 FieldOffset += FieldSize;
1417
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001418 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001419 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001420 FieldSize = CGM.getContext().getTypeSize(FType);
1421 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001422 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1423 "__flags", DefUnit,
1424 0, FieldSize, FieldAlign,
1425 FieldOffset, 0, FieldTy);
1426 EltTys.push_back(FieldTy);
1427 FieldOffset += FieldSize;
1428
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001429 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001430 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001431 FieldSize = CGM.getContext().getTypeSize(FType);
1432 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001433 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1434 "__size", DefUnit,
1435 0, FieldSize, FieldAlign,
1436 FieldOffset, 0, FieldTy);
1437 EltTys.push_back(FieldTy);
1438 FieldOffset += FieldSize;
1439
Anders Carlsson20f12a22009-12-06 18:00:51 +00001440 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001441 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001442 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001443 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001444 FieldSize = CGM.getContext().getTypeSize(FType);
1445 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001446 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1447 "__copy_helper", DefUnit,
1448 0, FieldSize, FieldAlign,
1449 FieldOffset, 0, FieldTy);
1450 EltTys.push_back(FieldTy);
1451 FieldOffset += FieldSize;
1452
Anders Carlsson20f12a22009-12-06 18:00:51 +00001453 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001454 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001455 FieldSize = CGM.getContext().getTypeSize(FType);
1456 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001457 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1458 "__destroy_helper", DefUnit,
1459 0, FieldSize, FieldAlign,
1460 FieldOffset, 0, FieldTy);
1461 EltTys.push_back(FieldTy);
1462 FieldOffset += FieldSize;
1463 }
1464
Devang Patel239cec62010-02-01 21:39:52 +00001465 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8b752f12010-01-27 17:10:57 +00001466 if (Align > CharUnits::fromQuantity(
1467 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001468 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001469 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001470 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001471 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001472
1473 if (NumPaddingBytes > 0) {
1474 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001475 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001476 pad, ArrayType::Normal, 0);
1477 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001478 FieldSize = CGM.getContext().getTypeSize(FType);
1479 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001480 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1481 Unit, "", DefUnit,
1482 0, FieldSize, FieldAlign,
1483 FieldOffset, 0, FieldTy);
1484 EltTys.push_back(FieldTy);
1485 FieldOffset += FieldSize;
1486 }
1487 }
1488
1489 FType = Type;
1490 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001491 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001492 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001493
1494 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +00001495 VD->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001496 0, FieldSize, FieldAlign,
1497 FieldOffset, 0, FieldTy);
1498 EltTys.push_back(FieldTy);
1499 FieldOffset += FieldSize;
1500
1501 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1502
1503 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1504
1505 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1506 llvm::DICompileUnit(),
1507 0, FieldOffset, 0, 0, Flags,
1508 llvm::DIType(), Elements);
1509 }
Chris Lattner650cea92009-05-05 04:57:08 +00001510
Chris Lattner9c85ba32008-11-10 06:08:34 +00001511 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001512 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001513 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001514 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001515 unsigned Column = 0;
1516 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001517 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001518 Column = PLoc.getColumn();
1519 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001520 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Chris Lattner9c85ba32008-11-10 06:08:34 +00001523 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001524 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001525 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001526 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001527 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001528 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001529 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001530 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001531
Devang Patel8fae0602009-11-13 19:10:24 +00001532 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001533 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001534 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1535
Chris Lattner23e92c02009-12-28 23:41:39 +00001536 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001537}
1538
Mike Stumpb1a6e682009-09-30 02:43:10 +00001539/// EmitDeclare - Emit local variable declaration debug info.
1540void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1541 llvm::Value *Storage, CGBuilderTy &Builder,
1542 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001543 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001544 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1545
1546 // Do not emit variable debug information while generating optimized code.
1547 // The llvm optimizer and code generator are not yet ready to support
1548 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001549 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001550 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001551 return;
1552
1553 uint64_t XOffset = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001554 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1555 QualType Type = VD->getType();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001556 llvm::DIType Ty = getOrCreateType(Type, Unit);
Devang Pateld6c5a262010-02-01 21:52:22 +00001557 if (VD->hasAttr<BlocksAttr>()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001558 llvm::DICompileUnit DefUnit;
1559 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1560
1561 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1562
1563 llvm::DIType FieldTy;
1564
1565 QualType FType;
1566 uint64_t FieldSize, FieldOffset;
1567 unsigned FieldAlign;
1568
1569 llvm::DIArray Elements;
1570 llvm::DIType EltTy;
1571
1572 // Build up structure for the byref. See BuildByRefType.
1573 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001574 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001575 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001576 FieldSize = CGM.getContext().getTypeSize(FType);
1577 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001578 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1579 "__isa", DefUnit,
1580 0, FieldSize, FieldAlign,
1581 FieldOffset, 0, FieldTy);
1582 EltTys.push_back(FieldTy);
1583 FieldOffset += FieldSize;
1584
Anders Carlsson20f12a22009-12-06 18:00:51 +00001585 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001586 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001587 FieldSize = CGM.getContext().getTypeSize(FType);
1588 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001589 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1590 "__forwarding", DefUnit,
1591 0, FieldSize, FieldAlign,
1592 FieldOffset, 0, FieldTy);
1593 EltTys.push_back(FieldTy);
1594 FieldOffset += FieldSize;
1595
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001596 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001597 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001598 FieldSize = CGM.getContext().getTypeSize(FType);
1599 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001600 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1601 "__flags", DefUnit,
1602 0, FieldSize, FieldAlign,
1603 FieldOffset, 0, FieldTy);
1604 EltTys.push_back(FieldTy);
1605 FieldOffset += FieldSize;
1606
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001607 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001608 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001609 FieldSize = CGM.getContext().getTypeSize(FType);
1610 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001611 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1612 "__size", DefUnit,
1613 0, FieldSize, FieldAlign,
1614 FieldOffset, 0, FieldTy);
1615 EltTys.push_back(FieldTy);
1616 FieldOffset += FieldSize;
1617
Anders Carlsson20f12a22009-12-06 18:00:51 +00001618 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001619 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001620 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001621 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001622 FieldSize = CGM.getContext().getTypeSize(FType);
1623 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001624 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1625 "__copy_helper", DefUnit,
1626 0, FieldSize, FieldAlign,
1627 FieldOffset, 0, FieldTy);
1628 EltTys.push_back(FieldTy);
1629 FieldOffset += FieldSize;
1630
Anders Carlsson20f12a22009-12-06 18:00:51 +00001631 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001632 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001633 FieldSize = CGM.getContext().getTypeSize(FType);
1634 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001635 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1636 "__destroy_helper", DefUnit,
1637 0, FieldSize, FieldAlign,
1638 FieldOffset, 0, FieldTy);
1639 EltTys.push_back(FieldTy);
1640 FieldOffset += FieldSize;
1641 }
1642
Devang Pateld6c5a262010-02-01 21:52:22 +00001643 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8b752f12010-01-27 17:10:57 +00001644 if (Align > CharUnits::fromQuantity(
1645 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001646 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001647 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001648 unsigned NumPaddingBytes
1649 = AlignedOffsetInBytes - FieldOffset/8;
1650
1651 if (NumPaddingBytes > 0) {
1652 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001653 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001654 pad, ArrayType::Normal, 0);
1655 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001656 FieldSize = CGM.getContext().getTypeSize(FType);
1657 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001658 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1659 Unit, "", DefUnit,
1660 0, FieldSize, FieldAlign,
1661 FieldOffset, 0, FieldTy);
1662 EltTys.push_back(FieldTy);
1663 FieldOffset += FieldSize;
1664 }
1665 }
1666
1667 FType = Type;
1668 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001669 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001670 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001671
1672 XOffset = FieldOffset;
1673 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld6c5a262010-02-01 21:52:22 +00001674 VD->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001675 0, FieldSize, FieldAlign,
1676 FieldOffset, 0, FieldTy);
1677 EltTys.push_back(FieldTy);
1678 FieldOffset += FieldSize;
1679
1680 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1681
1682 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1683
1684 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1685 llvm::DICompileUnit(),
1686 0, FieldOffset, 0, 0, Flags,
1687 llvm::DIType(), Elements);
1688 }
1689
1690 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001691 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001692 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001693 unsigned Line = 0;
1694 if (!PLoc.isInvalid())
1695 Line = PLoc.getLine();
1696 else
1697 Unit = llvm::DICompileUnit();
1698
Devang Pateld6c5a262010-02-01 21:52:22 +00001699 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001700 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001701 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1702 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1703 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1704 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001705 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001706 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1707 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001708 // offset of __forwarding field
1709 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001710 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1711 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1712 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001713 // offset of x field
1714 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001715 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001716 }
1717
1718 // Create the descriptor for the variable.
1719 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001720 DebugFactory.CreateComplexVariable(Tag,
1721 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001722 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001723 addr);
1724 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001725 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001726 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001727
Devang Patel8fae0602009-11-13 19:10:24 +00001728 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001729 llvm::DILocation DO(NULL);
1730 llvm::DILocation DL =
1731 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001732
Chris Lattner23e92c02009-12-28 23:41:39 +00001733 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001734}
1735
Devang Pateld6c5a262010-02-01 21:52:22 +00001736void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001737 llvm::Value *Storage,
1738 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001739 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001740}
1741
Mike Stumpb1a6e682009-09-30 02:43:10 +00001742void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1743 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1744 CodeGenFunction *CGF) {
1745 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1746}
1747
Chris Lattner9c85ba32008-11-10 06:08:34 +00001748/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1749/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001750void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001751 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001752 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001753}
1754
1755
1756
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001757/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001758void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001759 const VarDecl *D) {
1760
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001761 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001762 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001763 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001764 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001765 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001766
Devang Pateleb6d79b2010-02-01 21:34:11 +00001767 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001768 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001770 // CodeGen turns int[] into int[1] so we'll do the same here.
1771 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001773 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001774 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Anders Carlsson20f12a22009-12-06 18:00:51 +00001776 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001777 ArrayType::Normal, 0);
1778 }
Devang Pateleb6d79b2010-02-01 21:34:11 +00001779 llvm::StringRef DeclName = D->getName();
1780 llvm::DIDescriptor DContext =
1781 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1782 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001783 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001784 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001785 Var->hasInternalLinkage(),
1786 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001787}
1788
Devang Patel9ca36b62009-02-26 21:10:26 +00001789/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001790void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001791 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001792 // Create global variable debug descriptor.
Devang Pateld6c5a262010-02-01 21:52:22 +00001793 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001794 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001795 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001796 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001797
Devang Pateld6c5a262010-02-01 21:52:22 +00001798 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001799
Devang Pateld6c5a262010-02-01 21:52:22 +00001800 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001801 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Devang Patel9ca36b62009-02-26 21:10:26 +00001803 // CodeGen turns int[] into int[1] so we'll do the same here.
1804 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Devang Patel9ca36b62009-02-26 21:10:26 +00001806 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001807 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Anders Carlsson20f12a22009-12-06 18:00:51 +00001809 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001810 ArrayType::Normal, 0);
1811 }
1812
Devang Patelf6a39b72009-10-20 18:26:30 +00001813 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001814 getOrCreateType(T, Unit),
1815 Var->hasInternalLinkage(),
1816 true/*definition*/, Var);
1817}
Devang Patelabb485f2010-02-01 19:16:32 +00001818
1819/// getOrCreateNamesSpace - Return namespace descriptor for the given
1820/// namespace decl.
1821llvm::DINameSpace
1822CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1823 llvm::DIDescriptor Unit) {
1824 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1825 NameSpaceCache.find(NSDecl);
1826 if (I != NameSpaceCache.end())
1827 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1828
1829 SourceManager &SM = CGM.getContext().getSourceManager();
1830 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1831 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1832
1833 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001834 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001835 llvm::DINameSpace NS =
1836 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1837 llvm::DICompileUnit(Unit.getNode()), LineNo);
1838 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1839 return NS;
1840}