blob: df1a24ce31cfff4bf54e19ff4dee4af4467be6b6 [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)
Devang Patel17800552010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051}
52
Devang Patel33583052010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000056 if (!Context)
57 return CompileUnit;
58
59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
60 I = RegionMap.find(Context);
61 if (I != RegionMap.end())
62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000063
Devang Pateleb6d79b2010-02-01 21:34:11 +000064 // Check namespace.
65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
67
Devang Patel979ec2e2009-10-06 00:35:31 +000068 return CompileUnit;
69}
70
Devang Patel9c6c3a02010-01-14 00:36:21 +000071/// getFunctionName - Get function name for the given FunctionDecl. If the
72/// name is constructred on demand (e.g. C++ destructor) then the name
73/// is stored on the side.
74llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
75 assert (FD && "Invalid FunctionDecl!");
76 IdentifierInfo *FII = FD->getIdentifier();
77 if (FII)
78 return FII->getName();
79
80 // Otherwise construct human readable name for debug info.
81 std::string NS = FD->getNameAsString();
82
83 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000084 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000085 memcpy(StrPtr, NS.data(), NS.length());
86 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000087}
88
Devang Patel17800552010-03-09 00:44:50 +000089/// getOrCreateFile - Get the file debug info descriptor for the input location.
90llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
91 if (!Loc.isValid())
92 // If Location is not valid then use main input file.
93 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
94 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +000095 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
97 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +000098 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +000099
Devang Patel683f6db2010-03-09 19:14:07 +0000100 return DebugFactory.CreateFile(AbsFileName.getLast(),
Devang Patel17800552010-03-09 00:44:50 +0000101 AbsFileName.getDirname(), TheCU);
102}
103/// CreateCompileUnit - Create new compile unit.
104void CGDebugInfo::CreateCompileUnit() {
105
106 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000107 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000108 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
109 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000110 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000111
Devang Patel22fe5852010-03-12 21:04:27 +0000112 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000113 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000114
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000115 // The main file name provided via the "-main-file-name" option contains just
116 // the file name itself with no path information. This file name may have had
117 // a relative path, so we look into the actual file entry for the main
118 // file to determine the real absolute path for the file.
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000119 std::string MainFileDir;
120 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
121 MainFileDir = MainFile->getDir()->getName();
122 else
123 MainFileDir = AbsFileName.getDirname();
124
Chris Lattner515455a2009-03-25 03:28:08 +0000125 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000126 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000127 if (LO.CPlusPlus) {
128 if (LO.ObjC1)
129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
130 else
131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
132 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000133 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000134 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000135 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000136 } else {
137 LangTag = llvm::dwarf::DW_LANG_C89;
138 }
Devang Patel446c6192009-04-17 21:06:59 +0000139
Benjamin Kramer47daf682009-12-08 11:02:29 +0000140 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000141#ifdef CLANG_VENDOR
142 CLANG_VENDOR
143#endif
144 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000145
146 // Figure out which version of the ObjC runtime we have.
147 unsigned RuntimeVers = 0;
148 if (LO.ObjC1)
149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000152 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000153 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000155}
156
Devang Patel65e99f22009-02-25 01:36:11 +0000157/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158/// one if necessary.
159llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000160 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161 unsigned Encoding = 0;
162 switch (BT->getKind()) {
163 default:
164 case BuiltinType::Void:
165 return llvm::DIType();
166 case BuiltinType::UChar:
167 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
168 case BuiltinType::Char_S:
169 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
170 case BuiltinType::UShort:
171 case BuiltinType::UInt:
172 case BuiltinType::ULong:
173 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
174 case BuiltinType::Short:
175 case BuiltinType::Int:
176 case BuiltinType::Long:
177 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
178 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
179 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000180 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000181 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000182 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000183 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000184 uint64_t Size = CGM.getContext().getTypeSize(BT);
185 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Devang Patelca80a5f2009-10-20 19:55:01 +0000188 llvm::DIType DbgTy =
189 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000190 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000191 Unit, 0, Size, Align,
192 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000193 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000195
Chris Lattnerb7003772009-04-23 06:13:01 +0000196llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000197 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000198 // Bit size, align and offset of the type.
199 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
200 if (Ty->isComplexIntegerType())
201 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Anders Carlsson20f12a22009-12-06 18:00:51 +0000203 uint64_t Size = CGM.getContext().getTypeSize(Ty);
204 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000205 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Devang Patelca80a5f2009-10-20 19:55:01 +0000207 llvm::DIType DbgTy =
208 DebugFactory.CreateBasicType(Unit, "complex",
209 Unit, 0, Size, Align,
210 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000211 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000212}
213
John McCalla1805292009-09-25 01:40:47 +0000214/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000215/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000216llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000217 QualifierCollector Qc;
218 const Type *T = Qc.strip(Ty);
219
220 // Ignore these qualifiers for now.
221 Qc.removeObjCGCAttr();
222 Qc.removeAddressSpace();
223
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224 // We will create one Derived type for one qualifier and recurse to handle any
225 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000226 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000227 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000228 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000229 Qc.removeConst();
230 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000232 Qc.removeVolatile();
233 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000234 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000235 Qc.removeRestrict();
236 } else {
237 assert(Qc.empty() && "Unknown type qualifier for debug info");
238 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
John McCalla1805292009-09-25 01:40:47 +0000241 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
242
Daniel Dunbar3845f862008-10-31 03:54:29 +0000243 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
244 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000245 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000246 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000247 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000248 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000249}
250
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000251llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000252 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000253 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000254 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
255 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000256 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000257}
258
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000260 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000261 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
262 Ty->getPointeeType(), Unit);
263}
264
265llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
266 const Type *Ty,
267 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000268 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000269 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000271 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000272
273 // Size is always the size of a pointer. We can't use getTypeSize here
274 // because that does not return the correct value for references.
275 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000276 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
277 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Devang Patelca80a5f2009-10-20 19:55:01 +0000279 return
Devang Pateld58562e2010-03-09 22:49:11 +0000280 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000281 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000282
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000283}
284
Mike Stump9bc093c2009-05-14 02:03:51 +0000285llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000286 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000287 if (BlockLiteralGenericSet)
288 return BlockLiteralGeneric;
289
Mike Stump9bc093c2009-05-14 02:03:51 +0000290 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
291
292 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
293
294 llvm::DIType FieldTy;
295
296 QualType FType;
297 uint64_t FieldSize, FieldOffset;
298 unsigned FieldAlign;
299
300 llvm::DIArray Elements;
301 llvm::DIType EltTy, DescTy;
302
303 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000304 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000306 FieldSize = CGM.getContext().getTypeSize(FType);
307 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000308 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000309 "reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000310 0, FieldSize, FieldAlign,
311 FieldOffset, 0, FieldTy);
312 EltTys.push_back(FieldTy);
313
314 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000315 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000316 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000317 FieldSize = CGM.getContext().getTypeSize(FType);
318 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000319 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000320 "Size", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 0, FieldSize, FieldAlign,
322 FieldOffset, 0, FieldTy);
323 EltTys.push_back(FieldTy);
324
325 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000326 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 EltTys.clear();
328
Mike Stump3d363c52009-10-02 02:30:50 +0000329 unsigned Flags = llvm::DIType::FlagAppleBlock;
330
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Pateld58562e2010-03-09 22:49:11 +0000332 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(Ty);
337 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Mike Stump9bc093c2009-05-14 02:03:51 +0000339 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000340 Unit, "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 0, Size, Align, 0, 0, EltTy);
342
343 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000345 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 FieldSize = CGM.getContext().getTypeSize(FType);
347 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000348 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000349 "__isa", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000350 0, FieldSize, FieldAlign,
351 FieldOffset, 0, FieldTy);
352 EltTys.push_back(FieldTy);
353
354 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000357 FieldSize = CGM.getContext().getTypeSize(FType);
358 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000359 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000360 "__flags", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000361 0, FieldSize, FieldAlign,
362 FieldOffset, 0, FieldTy);
363 EltTys.push_back(FieldTy);
364
365 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000366 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000368 FieldSize = CGM.getContext().getTypeSize(FType);
369 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000370 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000371 "__reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000372 0, FieldSize, FieldAlign,
373 FieldOffset, 0, FieldTy);
374 EltTys.push_back(FieldTy);
375
376 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000377 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FieldSize = CGM.getContext().getTypeSize(FType);
380 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000381 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000382 "__FuncPtr", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000383 0, FieldSize, FieldAlign,
384 FieldOffset, 0, FieldTy);
385 EltTys.push_back(FieldTy);
386
387 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000388 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000390 FieldSize = CGM.getContext().getTypeSize(Ty);
391 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000392 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000393 "__descriptor", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 0, FieldSize, FieldAlign,
395 FieldOffset, 0, FieldTy);
396 EltTys.push_back(FieldTy);
397
398 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000399 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000400
401 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Pateld58562e2010-03-09 22:49:11 +0000402 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000403 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Mike Stump9bc093c2009-05-14 02:03:51 +0000405 BlockLiteralGenericSet = true;
406 BlockLiteralGeneric
407 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000408 "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000409 0, Size, Align, 0, 0, EltTy);
410 return BlockLiteralGeneric;
411}
412
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000414 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415 // Typedefs are derived from some other type. If we have a typedef of a
416 // typedef, make sure to emit the whole chain.
417 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419 // We don't set size information, but do specify where the typedef was
420 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000421 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000422 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000423 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000424
Devang Pateleb6d79b2010-02-01 21:34:11 +0000425 llvm::DIDescriptor TyContext
426 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
427 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000428 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000429 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000430 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000431 Ty->getDecl()->getName(), Unit,
432 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000433 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000434}
435
Chris Lattner9c85ba32008-11-10 06:08:34 +0000436llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000437 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000439
Chris Lattner9c85ba32008-11-10 06:08:34 +0000440 // Add the result type at least.
441 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443 // Set up remainder of arguments if there is a prototype.
444 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000445 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000446 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
447 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
448 } else {
449 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000450 }
451
Chris Lattner9c85ba32008-11-10 06:08:34 +0000452 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000453 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Devang Patelca80a5f2009-10-20 19:55:01 +0000455 llvm::DIType DbgTy =
456 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000457 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000458 0, 0, 0, 0, 0,
459 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000460 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000461}
462
Devang Patel428deb52010-01-19 00:00:59 +0000463/// CollectRecordFields - A helper function to collect debug info for
464/// record fields. This is used while creating debug info entry for a Record.
465void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000466CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000467 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
468 unsigned FieldNo = 0;
469 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000470 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
471 for (RecordDecl::field_iterator I = RD->field_begin(),
472 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000473 I != E; ++I, ++FieldNo) {
474 FieldDecl *Field = *I;
475 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
476
477 llvm::StringRef FieldName = Field->getName();
478
Devang Patel4835fdd2010-02-12 01:31:06 +0000479 // Ignore unnamed fields. Do not ignore unnamed records.
480 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000481 continue;
482
483 // Get the location for the field.
484 SourceLocation FieldDefLoc = Field->getLocation();
485 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000486 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000487 unsigned FieldLine = 0;
488
489 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000490 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000491 FieldLine = PLoc.getLine();
492 }
493
494 QualType FType = Field->getType();
495 uint64_t FieldSize = 0;
496 unsigned FieldAlign = 0;
497 if (!FType->isIncompleteArrayType()) {
498
499 // Bit size, align and offset of the type.
500 FieldSize = CGM.getContext().getTypeSize(FType);
501 Expr *BitWidth = Field->getBitWidth();
502 if (BitWidth)
503 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
504
505 FieldAlign = CGM.getContext().getTypeAlign(FType);
506 }
507
508 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
509
510 // Create a DW_TAG_member node to remember the offset of this field in the
511 // struct. FIXME: This is an absolutely insane way to capture this
512 // information. When we gut debug info, this should be fixed.
513 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
514 FieldName, FieldDefUnit,
515 FieldLine, FieldSize, FieldAlign,
516 FieldOffset, 0, FieldTy);
517 EltTys.push_back(FieldTy);
518 }
519}
520
Devang Patela6da1922010-01-28 00:28:01 +0000521/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
522/// function type is not updated to include implicit "this" pointer. Use this
523/// routine to get a method type which includes "this" pointer.
524llvm::DIType
525CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000526 llvm::DIFile Unit) {
Devang Patela6da1922010-01-28 00:28:01 +0000527 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000528
529 // Static methods do not need "this" pointer argument.
530 if (Method->isStatic())
531 return FnTy;
532
Devang Patela6da1922010-01-28 00:28:01 +0000533 // Add "this" pointer.
534
535 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
536 assert (Args.getNumElements() && "Invalid number of arguments!");
537
538 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
539
540 // First element is always return type. For 'void' functions it is NULL.
541 Elts.push_back(Args.getElement(0));
542
543 // "this" pointer is always first argument.
544 ASTContext &Context = CGM.getContext();
545 QualType ThisPtr =
546 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000547 llvm::DIType ThisPtrType =
548 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
549 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
550 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000551
552 // Copy rest of the arguments.
553 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
554 Elts.push_back(Args.getElement(i));
555
556 llvm::DIArray EltTypeArray =
557 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
558
559 return
560 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000561 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000562 0, 0, 0, 0, 0,
563 llvm::DIType(), EltTypeArray);
564}
565
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000566/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
567/// a single member function GlobalDecl.
568llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000569CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000570 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000571 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000572 bool IsCtorOrDtor =
573 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
574
575 llvm::StringRef MethodName = getFunctionName(Method);
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.
John McCallf746aa62010-03-19 23:29:14 +0000580 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000581 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000582 CGM.getMangledName(MethodLinkageName, Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000583
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000584 SourceManager &SM = CGM.getContext().getSourceManager();
585
586 // Get the location for the method.
587 SourceLocation MethodDefLoc = Method->getLocation();
588 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000589 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000590 unsigned MethodLine = 0;
591
592 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000593 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000594 MethodLine = PLoc.getLine();
595 }
596
597 // Collect virtual method info.
598 llvm::DIType ContainingType;
599 unsigned Virtuality = 0;
600 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000601
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000602 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000603 if (Method->isPure())
604 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
605 else
606 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
607
608 // It doesn't make sense to give a virtual destructor a vtable index,
609 // since a single destructor has two entries in the vtable.
610 if (!isa<CXXDestructorDecl>(Method))
611 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000612 ContainingType = RecordTy;
613 }
614
615 llvm::DISubprogram SP =
616 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
617 MethodLinkageName,
618 MethodDefUnit, MethodLine,
619 MethodTy, /*isLocalToUnit=*/false,
620 Method->isThisDeclarationADefinition(),
621 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000622
623 // Don't cache ctors or dtors since we have to emit multiple functions for
624 // a single ctor or dtor.
625 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
626 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000627
628 return SP;
629}
630
Devang Patel4125fd22010-01-19 01:54:44 +0000631/// CollectCXXMemberFunctions - A helper function to collect debug info for
632/// C++ member functions.This is used while creating debug info entry for
633/// a Record.
634void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000635CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000636 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
637 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000638 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
639 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000640 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000641
Devang Pateld5322da2010-02-09 19:09:28 +0000642 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000643 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000644
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000645 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000646 }
647}
648
Devang Patela245c5b2010-01-25 23:32:18 +0000649/// CollectCXXBases - A helper function to collect debug info for
650/// C++ base classes. This is used while creating debug info entry for
651/// a Record.
652void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000653CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000654 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
655 llvm::DICompositeType &RecordTy) {
656
Devang Patel239cec62010-02-01 21:39:52 +0000657 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
658 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
659 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000660 unsigned BFlags = 0;
661 uint64_t BaseOffset;
662
663 const CXXRecordDecl *Base =
664 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
665
666 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000667 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000668 // expression where it expects +ve number.
Anders Carlssonbba16072010-03-11 07:15:17 +0000669 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000670 BFlags = llvm::DIType::FlagVirtual;
671 } else
672 BaseOffset = RL.getBaseClassOffset(Base);
673
674 AccessSpecifier Access = BI->getAccessSpecifier();
675 if (Access == clang::AS_private)
676 BFlags |= llvm::DIType::FlagPrivate;
677 else if (Access == clang::AS_protected)
678 BFlags |= llvm::DIType::FlagProtected;
679
680 llvm::DIType DTy =
681 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
682 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000683 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000684 BaseOffset, BFlags,
685 getOrCreateType(BI->getType(),
686 Unit));
687 EltTys.push_back(DTy);
688 }
Devang Patela245c5b2010-01-25 23:32:18 +0000689}
690
Devang Patel4ce3f202010-01-28 18:11:52 +0000691/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000692llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000693 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000694 return VTablePtrType;
695
696 ASTContext &Context = CGM.getContext();
697
698 /* Function type */
699 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
700 STys.push_back(getOrCreateType(Context.IntTy, Unit));
701 llvm::DIArray SElements =
702 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
703 llvm::DIType SubTy =
704 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000705 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000706 0, 0, 0, 0, 0, llvm::DIType(), SElements);
707
708 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
709 llvm::DIType vtbl_ptr_type
710 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000711 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000712 0, Size, 0, 0, 0, SubTy);
713
Devang Pateld58562e2010-03-09 22:49:11 +0000714 VTablePtrType =
715 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
716 Unit, "", Unit,
717 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000718 return VTablePtrType;
719}
720
721/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000722llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000723 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000724 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000725
726 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000727 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000728 memcpy(StrPtr, Name.data(), Name.length());
729 return llvm::StringRef(StrPtr, Name.length());
730}
731
732
733/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
734/// debug info entry in EltTys vector.
735void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000736CollectVtableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000737 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000738 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000739
740 // If there is a primary base then it will hold vtable info.
741 if (RL.getPrimaryBase())
742 return;
743
744 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000745 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000746 return;
747
748 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
749 llvm::DIType VPTR
750 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000751 getVtableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000752 0, Size, 0, 0, 0,
753 getOrCreateVTablePtrType(Unit));
754 EltTys.push_back(VPTR);
755}
756
Devang Patel65e99f22009-02-25 01:36:11 +0000757/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000759 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000760 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000763 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000765 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000766 Tag = llvm::dwarf::DW_TAG_union_type;
767 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000768 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000769 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000770 }
771
Anders Carlsson20f12a22009-12-06 18:00:51 +0000772 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000773
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000775 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000776 llvm::DIFile DefUnit;
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000777 unsigned Line = 0;
778 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000779 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000780 Line = PLoc.getLine();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783 // Records and classes and unions can all be recursive. To handle them, we
784 // first generate a debug descriptor for the struct as a forward declaration.
785 // Then (if it is a definition) we go through and get debug info for all of
786 // its members. Finally, we create a descriptor for the complete type (which
787 // may refer to the forward decl if the struct is recursive) and replace all
788 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000789
Devang Pateld6c5a262010-02-01 21:52:22 +0000790 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000791 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000792 llvm::SmallString<128> FwdDeclName;
793 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000794 llvm::DIDescriptor FDContext =
795 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000796 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000797 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000798 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000802 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000804
Eli Friedman14d63652009-11-16 21:04:30 +0000805 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 // Otherwise, insert it into the TypeCache so that recursive uses will find
807 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000808 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000809 // Push the struct on region stack.
810 RegionStack.push_back(FwdDecl.getNode());
811 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000812
813 // Convert all the elements.
814 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
815
Devang Pateld6c5a262010-02-01 21:52:22 +0000816 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000817 if (CXXDecl) {
818 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000819 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000820 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000821 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000822 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000823 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000824 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000825
826 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000827 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000828 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
829 ContainingType =
830 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
831 else if (CXXDecl->isDynamicClass())
832 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattner9c85ba32008-11-10 06:08:34 +0000835 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000836 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837
838 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000839 uint64_t Size = CGM.getContext().getTypeSize(Ty);
840 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Devang Patele4c1ea02010-03-11 20:01:48 +0000842 RegionStack.pop_back();
843 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
844 RegionMap.find(Ty->getDecl());
845 if (RI != RegionMap.end())
846 RegionMap.erase(RI);
847
Devang Patel411894b2010-02-01 22:40:08 +0000848 llvm::DIDescriptor RDContext =
849 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000850 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000851 DebugFactory.CreateCompositeType(Tag, RDContext,
852 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000853 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000854 llvm::DIType(), Elements,
855 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000856
857 // Now that we have a real decl for the struct, replace anything using the
858 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000859 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +0000860 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000861 return RealDecl;
862}
863
Devang Patel9ca36b62009-02-26 21:10:26 +0000864/// CreateType - get objective-c interface type.
865llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000866 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000867 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Devang Patel9ca36b62009-02-26 21:10:26 +0000869 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000870 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000871
872 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000873 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000874 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000875 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
876
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Devang Patel17800552010-03-09 00:44:50 +0000878 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000879
Devang Patel9ca36b62009-02-26 21:10:26 +0000880 // To handle recursive interface, we
881 // first generate a debug descriptor for the struct as a forward declaration.
882 // Then (if it is a definition) we go through and get debug info for all of
883 // its members. Finally, we create a descriptor for the complete type (which
884 // may refer to the forward decl if the struct is recursive) and replace all
885 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000886 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000887 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000888 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000889 llvm::DIType(), llvm::DIArray(),
890 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Devang Patel9ca36b62009-02-26 21:10:26 +0000892 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000893 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000894 return FwdDecl;
895
Devang Patelffffb032009-11-16 20:09:38 +0000896 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000897 // Otherwise, insert it into the TypeCache so that recursive uses will find
898 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000899 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000900 // Push the struct on region stack.
901 RegionStack.push_back(FwdDecl.getNode());
902 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Devang Patel9ca36b62009-02-26 21:10:26 +0000903
904 // Convert all the elements.
905 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
906
Devang Pateld6c5a262010-02-01 21:52:22 +0000907 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000908 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000910 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000911 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000912 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000913 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000914 0 /* offset */, 0, SClassTy);
915 EltTys.push_back(InhTag);
916 }
917
Devang Pateld6c5a262010-02-01 21:52:22 +0000918 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000919
920 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000921 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
922 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000923 ObjCIvarDecl *Field = *I;
924 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
925
Devang Patel73621622009-11-25 17:37:31 +0000926 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000927
Devang Patelde135022009-04-27 22:40:36 +0000928 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000929 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000930 continue;
931
Devang Patel9ca36b62009-02-26 21:10:26 +0000932 // Get the location for the field.
933 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000934 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000935 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
936 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
937
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Devang Patel99c20eb2009-03-20 18:24:39 +0000939 QualType FType = Field->getType();
940 uint64_t FieldSize = 0;
941 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000942
Devang Patel99c20eb2009-03-20 18:24:39 +0000943 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Devang Patel99c20eb2009-03-20 18:24:39 +0000945 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000946 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000947 Expr *BitWidth = Field->getBitWidth();
948 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000949 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000950
Anders Carlsson20f12a22009-12-06 18:00:51 +0000951 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000952 }
953
Mike Stump1eb44332009-09-09 15:08:12 +0000954 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
955
Devang Patelc20482b2009-03-19 00:23:53 +0000956 unsigned Flags = 0;
957 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
958 Flags = llvm::DIType::FlagProtected;
959 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
960 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Devang Patel9ca36b62009-02-26 21:10:26 +0000962 // Create a DW_TAG_member node to remember the offset of this field in the
963 // struct. FIXME: This is an absolutely insane way to capture this
964 // information. When we gut debug info, this should be fixed.
965 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
966 FieldName, FieldDefUnit,
967 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000968 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000969 EltTys.push_back(FieldTy);
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Devang Patel9ca36b62009-02-26 21:10:26 +0000972 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000973 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000974
Devang Patele4c1ea02010-03-11 20:01:48 +0000975 RegionStack.pop_back();
976 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
977 RegionMap.find(Ty->getDecl());
978 if (RI != RegionMap.end())
979 RegionMap.erase(RI);
980
Devang Patel9ca36b62009-02-26 21:10:26 +0000981 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000982 uint64_t Size = CGM.getContext().getTypeSize(Ty);
983 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Devang Patel6c1fddf2009-07-27 18:42:03 +0000985 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000986 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000987 Line, Size, Align, 0, 0, llvm::DIType(),
988 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000989
990 // Now that we have a real decl for the struct, replace anything using the
991 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000992 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +0000993 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode());
Devang Patelfe09eab2009-07-13 17:03:14 +0000994
Devang Patel9ca36b62009-02-26 21:10:26 +0000995 return RealDecl;
996}
997
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000999 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001000 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001
1002 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1003
1004 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001005 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001006 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001007 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001008 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001009 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner9c85ba32008-11-10 06:08:34 +00001012 // Return a CompositeType for the enum itself.
1013 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001014 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001015
Devang Pateld6c5a262010-02-01 21:52:22 +00001016 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +00001017 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001018 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001019 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1020 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1021
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattner9c85ba32008-11-10 06:08:34 +00001023 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001024 uint64_t Size = 0;
1025 unsigned Align = 0;
1026 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001027 Size = CGM.getContext().getTypeSize(Ty);
1028 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Devang Patelca80a5f2009-10-20 19:55:01 +00001031 llvm::DIType DbgTy =
1032 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001033 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001034 Size, Align, 0, 0,
1035 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001036 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037}
1038
1039llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001040 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001041 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1042 return CreateType(RT, Unit);
1043 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1044 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattner9c85ba32008-11-10 06:08:34 +00001046 return llvm::DIType();
1047}
1048
Devang Patel70c23cd2010-02-23 22:59:39 +00001049llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001050 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001051 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1052 uint64_t NumElems = Ty->getNumElements();
1053 if (NumElems > 0)
1054 --NumElems;
1055 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1056 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1057
1058 llvm::DIArray SubscriptArray =
1059 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1060
1061 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1062 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1063
1064 return
1065 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001066 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001067 0, Size, Align, 0, 0,
1068 ElementTy, SubscriptArray);
1069}
1070
Chris Lattner9c85ba32008-11-10 06:08:34 +00001071llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001072 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001073 uint64_t Size;
1074 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076
Nuno Lopes010d5142009-01-28 00:35:17 +00001077 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001078 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001079 Size = 0;
1080 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001081 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001082 } else if (Ty->isIncompleteArrayType()) {
1083 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001084 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001085 } else {
1086 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001087 Size = CGM.getContext().getTypeSize(Ty);
1088 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001089 }
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner9c85ba32008-11-10 06:08:34 +00001091 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1092 // interior arrays, do we care? Why aren't nested arrays represented the
1093 // obvious/recursive way?
1094 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1095 QualType EltTy(Ty, 0);
1096 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001097 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001098 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001099 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001100 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001101 // FIXME: Verify this is right for VLAs.
1102 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1103 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner9c85ba32008-11-10 06:08:34 +00001106 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001107 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001108
Devang Patelca80a5f2009-10-20 19:55:01 +00001109 llvm::DIType DbgTy =
1110 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001111 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001112 0, Size, Align, 0, 0,
1113 getOrCreateType(EltTy, Unit),
1114 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001115 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001116}
1117
Anders Carlssona031b352009-11-06 19:19:55 +00001118llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001119 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001120 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1121 Ty, Ty->getPointeeType(), Unit);
1122}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001123
Anders Carlsson20f12a22009-12-06 18:00:51 +00001124llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001125 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1127 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1128
1129 if (!Ty->getPointeeType()->isFunctionType()) {
1130 // We have a data member pointer type.
1131 return PointerDiffDITy;
1132 }
1133
1134 // We have a member function pointer type. Treat it as a struct with two
1135 // ptrdiff_t members.
1136 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1137
1138 uint64_t FieldOffset = 0;
1139 llvm::DIDescriptor ElementTypes[2];
1140
1141 // FIXME: This should probably be a function type instead.
1142 ElementTypes[0] =
1143 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001144 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001145 Info.first, Info.second, FieldOffset, 0,
1146 PointerDiffDITy);
1147 FieldOffset += Info.first;
1148
1149 ElementTypes[1] =
1150 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001151 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001152 Info.first, Info.second, FieldOffset, 0,
1153 PointerDiffDITy);
1154
1155 llvm::DIArray Elements =
1156 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1157 llvm::array_lengthof(ElementTypes));
1158
1159 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1160 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001161 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001162 0, 0, 0, llvm::DIType(), Elements);
1163}
1164
Douglas Gregor840943d2009-12-21 20:18:30 +00001165static QualType UnwrapTypeForDebugInfo(QualType T) {
1166 do {
1167 QualType LastT = T;
1168 switch (T->getTypeClass()) {
1169 default:
1170 return T;
1171 case Type::TemplateSpecialization:
1172 T = cast<TemplateSpecializationType>(T)->desugar();
1173 break;
1174 case Type::TypeOfExpr: {
1175 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1176 T = Ty->getUnderlyingExpr()->getType();
1177 break;
1178 }
1179 case Type::TypeOf:
1180 T = cast<TypeOfType>(T)->getUnderlyingType();
1181 break;
1182 case Type::Decltype:
1183 T = cast<DecltypeType>(T)->getUnderlyingType();
1184 break;
1185 case Type::QualifiedName:
1186 T = cast<QualifiedNameType>(T)->getNamedType();
1187 break;
1188 case Type::SubstTemplateTypeParm:
1189 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1190 break;
1191 case Type::Elaborated:
1192 T = cast<ElaboratedType>(T)->getUnderlyingType();
1193 break;
1194 }
1195
1196 assert(T != LastT && "Type unwrapping failed to unwrap!");
1197 if (T == LastT)
1198 return T;
1199 } while (true);
1200
1201 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001202}
1203
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001204/// getOrCreateType - Get the type from the cache or create a new
1205/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001206llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001207 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001208 if (Ty.isNull())
1209 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Douglas Gregor840943d2009-12-21 20:18:30 +00001211 // Unwrap the type as needed for debug information.
1212 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001213
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001214 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001215 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001216 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001217 if (it != TypeCache.end()) {
1218 // Verify that the debug info still exists.
1219 if (&*it->second)
1220 return llvm::DIType(cast<llvm::MDNode>(it->second));
1221 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001223 // Otherwise create the type.
1224 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001225
1226 // And update the type cache.
1227 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001228 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001229}
1230
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001231/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001232llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001233 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001234 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001235 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001236 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001237
Douglas Gregor2101a822009-12-21 19:57:21 +00001238 const char *Diag = 0;
1239
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001240 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001242#define TYPE(Class, Base)
1243#define ABSTRACT_TYPE(Class, Base)
1244#define NON_CANONICAL_TYPE(Class, Base)
1245#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1246#include "clang/AST/TypeNodes.def"
1247 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001248
Anders Carlssonbfe69952009-11-06 18:24:04 +00001249 // FIXME: Handle these.
1250 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001251 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001252
1253 case Type::Vector:
1254 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001255 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001256 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001257 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001258 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1259 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1260 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1261 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001262 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001263 return CreateType(cast<BlockPointerType>(Ty), Unit);
1264 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001266 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001267 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001268 case Type::FunctionProto:
1269 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001270 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001271 case Type::ConstantArray:
1272 case Type::VariableArray:
1273 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001274 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001275
1276 case Type::LValueReference:
1277 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1278
Anders Carlsson20f12a22009-12-06 18:00:51 +00001279 case Type::MemberPointer:
1280 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001281
John McCall3cb0ebd2010-03-10 03:28:59 +00001282 case Type::InjectedClassName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001283 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001284 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001285 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001286 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001287 case Type::TypeOfExpr:
1288 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001289 case Type::Decltype:
1290 llvm_unreachable("type should have been unwrapped!");
1291 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001292
1293 case Type::RValueReference:
1294 // FIXME: Implement!
1295 Diag = "rvalue references";
1296 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001297 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001298
1299 assert(Diag && "Fall through without a diagnostic?");
1300 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1301 "debug information for %0 is not yet supported");
1302 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1303 << Diag;
1304 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001305}
1306
1307/// EmitFunctionStart - Constructs the debug code for entering a function -
1308/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001309void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001310 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001311 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Devang Patel9c6c3a02010-01-14 00:36:21 +00001313 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001314 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001315
1316 const Decl *D = GD.getDecl();
1317 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001318 // If there is a DISubprogram for this function available then use it.
1319 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1320 FI = SPCache.find(FD);
1321 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001322 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1323 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001324 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001325 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001326 return;
1327 }
1328 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001329 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001330 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001331 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001332 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001333 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001334 } else {
1335 // Use llvm function name as linkage name.
1336 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001337 LinkageName.setString(Name);
Devang Patel17584202010-01-19 00:25:12 +00001338 if (!Name.empty() && Name[0] == '\01')
1339 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Devang Patel98a200b2010-01-14 18:06:13 +00001342 // It is expected that CurLoc is set before using EmitFunctionStart.
1343 // Usually, CurLoc points to the left bracket location of compound
1344 // statement representing function body.
Devang Patel17800552010-03-09 00:44:50 +00001345 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001346 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001347 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattner9c85ba32008-11-10 06:08:34 +00001349 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001350 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001351 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001352 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001354 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001355 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001356 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001357}
1358
1359
Chris Lattner9c85ba32008-11-10 06:08:34 +00001360void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001361 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001363 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001364 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001365 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001366 || (SM.getInstantiationLineNumber(CurLoc) ==
1367 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001368 && SM.isFromSameFile(CurLoc, PrevLoc)))
1369 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001370
1371 // Update last state.
1372 PrevLoc = CurLoc;
1373
1374 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001375 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001376 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001377
Devang Patel8fae0602009-11-13 19:10:24 +00001378 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001379 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1380 llvm::DILocation DO(NULL);
1381 llvm::DILocation DL =
1382 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1383 DS, DO);
1384 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001385}
1386
1387/// EmitRegionStart- Constructs the debug code for entering a declarative
1388/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001389void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001390 SourceManager &SM = CGM.getContext().getSourceManager();
1391 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001392 llvm::DIDescriptor D =
1393 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1394 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001395 llvm::DIDescriptor(RegionStack.back()),
1396 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001397 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001398}
1399
1400/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1401/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001402void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001403 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1404
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001405 // Provide an region stop point.
1406 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001408 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001409}
1410
Devang Patel809b9bb2010-02-10 18:49:08 +00001411// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1412// See BuildByRefType.
1413llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1414 uint64_t *XOffset) {
1415
1416 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1417
1418 QualType FType;
1419 uint64_t FieldSize, FieldOffset;
1420 unsigned FieldAlign;
1421
Devang Patel17800552010-03-09 00:44:50 +00001422 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001423 QualType Type = VD->getType();
1424
1425 FieldOffset = 0;
1426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1427 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1428 FieldSize = CGM.getContext().getTypeSize(FType);
1429 FieldAlign = CGM.getContext().getTypeAlign(FType);
1430 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001431 "__isa", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001432 0, FieldSize, FieldAlign,
1433 FieldOffset, 0, FieldTy);
1434 EltTys.push_back(FieldTy);
1435 FieldOffset += FieldSize;
1436
1437 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1438 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1439 FieldSize = CGM.getContext().getTypeSize(FType);
1440 FieldAlign = CGM.getContext().getTypeAlign(FType);
1441 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001442 "__forwarding", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001443 0, FieldSize, FieldAlign,
1444 FieldOffset, 0, FieldTy);
1445 EltTys.push_back(FieldTy);
1446 FieldOffset += FieldSize;
1447
1448 FType = CGM.getContext().IntTy;
1449 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1450 FieldSize = CGM.getContext().getTypeSize(FType);
1451 FieldAlign = CGM.getContext().getTypeAlign(FType);
1452 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001453 "__flags", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001454 0, FieldSize, FieldAlign,
1455 FieldOffset, 0, FieldTy);
1456 EltTys.push_back(FieldTy);
1457 FieldOffset += FieldSize;
1458
1459 FType = CGM.getContext().IntTy;
1460 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1461 FieldSize = CGM.getContext().getTypeSize(FType);
1462 FieldAlign = CGM.getContext().getTypeAlign(FType);
1463 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001464 "__size", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001465 0, FieldSize, FieldAlign,
1466 FieldOffset, 0, FieldTy);
1467 EltTys.push_back(FieldTy);
1468 FieldOffset += FieldSize;
1469
1470 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1471 if (HasCopyAndDispose) {
1472 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1473 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1474 FieldSize = CGM.getContext().getTypeSize(FType);
1475 FieldAlign = CGM.getContext().getTypeAlign(FType);
1476 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001477 "__copy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001478 0, FieldSize, FieldAlign,
1479 FieldOffset, 0, FieldTy);
1480 EltTys.push_back(FieldTy);
1481 FieldOffset += FieldSize;
1482
1483 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1484 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1485 FieldSize = CGM.getContext().getTypeSize(FType);
1486 FieldAlign = CGM.getContext().getTypeAlign(FType);
1487 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001488 "__destroy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001489 0, FieldSize, FieldAlign,
1490 FieldOffset, 0, FieldTy);
1491 EltTys.push_back(FieldTy);
1492 FieldOffset += FieldSize;
1493 }
1494
1495 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1496 if (Align > CharUnits::fromQuantity(
1497 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1498 unsigned AlignedOffsetInBytes
1499 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1500 unsigned NumPaddingBytes
1501 = AlignedOffsetInBytes - FieldOffset/8;
1502
1503 if (NumPaddingBytes > 0) {
1504 llvm::APInt pad(32, NumPaddingBytes);
1505 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1506 pad, ArrayType::Normal, 0);
1507 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1508 FieldSize = CGM.getContext().getTypeSize(FType);
1509 FieldAlign = CGM.getContext().getTypeAlign(FType);
1510 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
Devang Pateld58562e2010-03-09 22:49:11 +00001511 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001512 0, FieldSize, FieldAlign,
1513 FieldOffset, 0, FieldTy);
1514 EltTys.push_back(FieldTy);
1515 FieldOffset += FieldSize;
1516 }
1517 }
1518
1519 FType = Type;
1520 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1521 FieldSize = CGM.getContext().getTypeSize(FType);
1522 FieldAlign = Align.getQuantity()*8;
1523
1524 *XOffset = FieldOffset;
1525 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001526 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001527 0, FieldSize, FieldAlign,
1528 FieldOffset, 0, FieldTy);
1529 EltTys.push_back(FieldTy);
1530 FieldOffset += FieldSize;
1531
1532 llvm::DIArray Elements =
1533 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1534
1535 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1536
1537 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001538 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001539 0, FieldOffset, 0, 0, Flags,
1540 llvm::DIType(), Elements);
1541
1542}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001543/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001544void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001545 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001546 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1547
Devang Patel07739032009-03-27 23:16:32 +00001548 // Do not emit variable debug information while generating optimized code.
1549 // The llvm optimizer and code generator are not yet ready to support
1550 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001551 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001552 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001553 return;
1554
Devang Patel17800552010-03-09 00:44:50 +00001555 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001556 llvm::DIType Ty;
1557 uint64_t XOffset = 0;
1558 if (VD->hasAttr<BlocksAttr>())
1559 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1560 else
1561 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001562
Chris Lattner9c85ba32008-11-10 06:08:34 +00001563 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001564 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001565 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001566 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001567 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001568 if (PLoc.isInvalid())
1569 PLoc = SM.getPresumedLoc(CurLoc);
1570 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001571 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001572 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001573 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001574 } else {
Devang Patel17800552010-03-09 00:44:50 +00001575 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Chris Lattner9c85ba32008-11-10 06:08:34 +00001578 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001579 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001580 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001581 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001582 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001583 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001584 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001585 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001586
Devang Patel8fae0602009-11-13 19:10:24 +00001587 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001588 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001589 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1590
Chris Lattner23e92c02009-12-28 23:41:39 +00001591 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001592}
1593
Mike Stumpb1a6e682009-09-30 02:43:10 +00001594/// EmitDeclare - Emit local variable declaration debug info.
1595void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1596 llvm::Value *Storage, CGBuilderTy &Builder,
1597 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001598 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1600
1601 // Do not emit variable debug information while generating optimized code.
1602 // The llvm optimizer and code generator are not yet ready to support
1603 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001604 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001605 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001606 return;
1607
1608 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001609 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001610 llvm::DIType Ty;
1611 if (VD->hasAttr<BlocksAttr>())
1612 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1613 else
1614 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001615
1616 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001617 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001618 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001619 unsigned Line = 0;
1620 if (!PLoc.isInvalid())
1621 Line = PLoc.getLine();
1622 else
Devang Patel17800552010-03-09 00:44:50 +00001623 Unit = llvm::DIFile();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001624
Devang Pateld6c5a262010-02-01 21:52:22 +00001625 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001626 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001627 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1628 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1629 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1630 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001631 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001632 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1633 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001634 // offset of __forwarding field
1635 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001636 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1637 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1638 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001639 // offset of x field
1640 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001641 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001642 }
1643
1644 // Create the descriptor for the variable.
1645 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001646 DebugFactory.CreateComplexVariable(Tag,
1647 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001648 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001649 addr);
1650 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001651 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001652 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001653
Devang Patel8fae0602009-11-13 19:10:24 +00001654 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001655 llvm::DILocation DO(NULL);
1656 llvm::DILocation DL =
1657 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001658
Chris Lattner23e92c02009-12-28 23:41:39 +00001659 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001660}
1661
Devang Pateld6c5a262010-02-01 21:52:22 +00001662void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001663 llvm::Value *Storage,
1664 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001665 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001666}
1667
Mike Stumpb1a6e682009-09-30 02:43:10 +00001668void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1669 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1670 CodeGenFunction *CGF) {
1671 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1672}
1673
Chris Lattner9c85ba32008-11-10 06:08:34 +00001674/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1675/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001676void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001677 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001678 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001679}
1680
1681
1682
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001683/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001684void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001685 const VarDecl *D) {
1686
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001687 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001688 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001689 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001690 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001691 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001692
Devang Pateleb6d79b2010-02-01 21:34:11 +00001693 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001694 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001696 // CodeGen turns int[] into int[1] so we'll do the same here.
1697 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001699 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001700 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Anders Carlsson20f12a22009-12-06 18:00:51 +00001702 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001703 ArrayType::Normal, 0);
1704 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001705 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001706 llvm::DIDescriptor DContext =
1707 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1708 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001709 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001710 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001711 Var->hasInternalLinkage(),
1712 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001713}
1714
Devang Patel9ca36b62009-02-26 21:10:26 +00001715/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001716void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001717 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001718 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001719 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001720 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001721 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001722 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001723
Devang Pateld6c5a262010-02-01 21:52:22 +00001724 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001725
Devang Pateld6c5a262010-02-01 21:52:22 +00001726 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001727 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Devang Patel9ca36b62009-02-26 21:10:26 +00001729 // CodeGen turns int[] into int[1] so we'll do the same here.
1730 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Devang Patel9ca36b62009-02-26 21:10:26 +00001732 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001733 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Anders Carlsson20f12a22009-12-06 18:00:51 +00001735 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001736 ArrayType::Normal, 0);
1737 }
1738
Devang Patelf6a39b72009-10-20 18:26:30 +00001739 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001740 getOrCreateType(T, Unit),
1741 Var->hasInternalLinkage(),
1742 true/*definition*/, Var);
1743}
Devang Patelabb485f2010-02-01 19:16:32 +00001744
1745/// getOrCreateNamesSpace - Return namespace descriptor for the given
1746/// namespace decl.
1747llvm::DINameSpace
1748CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1749 llvm::DIDescriptor Unit) {
1750 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1751 NameSpaceCache.find(NSDecl);
1752 if (I != NameSpaceCache.end())
1753 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1754
1755 SourceManager &SM = CGM.getContext().getSourceManager();
1756 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1757 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1758
1759 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001760 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001761 llvm::DINameSpace NS =
1762 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel17800552010-03-09 00:44:50 +00001763 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patelabb485f2010-02-01 19:16:32 +00001764 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1765 return NS;
1766}