blob: 925453a678e9a7bfea6df2976e022a50923edeed [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.
Devang Patel22fe5852010-03-12 21:04:27 +0000107 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
108 if (MainFileName.empty())
109 MainFileName = "<unknown>";
110 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000111 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000112
Chris Lattner515455a2009-03-25 03:28:08 +0000113 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000114 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000115 if (LO.CPlusPlus) {
116 if (LO.ObjC1)
117 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
118 else
119 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
120 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000121 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000122 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000123 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000124 } else {
125 LangTag = llvm::dwarf::DW_LANG_C89;
126 }
Devang Patel446c6192009-04-17 21:06:59 +0000127
Benjamin Kramer47daf682009-12-08 11:02:29 +0000128 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000129#ifdef CLANG_VENDOR
130 CLANG_VENDOR
131#endif
132 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000133
134 // Figure out which version of the ObjC runtime we have.
135 unsigned RuntimeVers = 0;
136 if (LO.ObjC1)
137 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000139 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000140 TheCU = DebugFactory.CreateCompileUnit(
141 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000142 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000143}
144
Devang Patel65e99f22009-02-25 01:36:11 +0000145/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146/// one if necessary.
147llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000148 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000149 unsigned Encoding = 0;
150 switch (BT->getKind()) {
151 default:
152 case BuiltinType::Void:
153 return llvm::DIType();
154 case BuiltinType::UChar:
155 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
156 case BuiltinType::Char_S:
157 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
158 case BuiltinType::UShort:
159 case BuiltinType::UInt:
160 case BuiltinType::ULong:
161 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
162 case BuiltinType::Short:
163 case BuiltinType::Int:
164 case BuiltinType::Long:
165 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
166 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
167 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000168 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000169 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000170 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000172 uint64_t Size = CGM.getContext().getTypeSize(BT);
173 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000174 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Devang Patelca80a5f2009-10-20 19:55:01 +0000176 llvm::DIType DbgTy =
177 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000178 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000179 Unit, 0, Size, Align,
180 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000181 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000182}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000183
Chris Lattnerb7003772009-04-23 06:13:01 +0000184llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000185 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000186 // Bit size, align and offset of the type.
187 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
188 if (Ty->isComplexIntegerType())
189 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson20f12a22009-12-06 18:00:51 +0000191 uint64_t Size = CGM.getContext().getTypeSize(Ty);
192 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000193 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Devang Patelca80a5f2009-10-20 19:55:01 +0000195 llvm::DIType DbgTy =
196 DebugFactory.CreateBasicType(Unit, "complex",
197 Unit, 0, Size, Align,
198 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000199 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000200}
201
John McCalla1805292009-09-25 01:40:47 +0000202/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000203/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000204llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000205 QualifierCollector Qc;
206 const Type *T = Qc.strip(Ty);
207
208 // Ignore these qualifiers for now.
209 Qc.removeObjCGCAttr();
210 Qc.removeAddressSpace();
211
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212 // We will create one Derived type for one qualifier and recurse to handle any
213 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000214 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000215 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000216 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000217 Qc.removeConst();
218 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000220 Qc.removeVolatile();
221 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000222 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000223 Qc.removeRestrict();
224 } else {
225 assert(Qc.empty() && "Unknown type qualifier for debug info");
226 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
John McCalla1805292009-09-25 01:40:47 +0000229 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
230
Daniel Dunbar3845f862008-10-31 03:54:29 +0000231 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
232 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000233 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000234 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000235 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000236 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000237}
238
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000239llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000240 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000241 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000242 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
243 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000244 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000245}
246
Chris Lattner9c85ba32008-11-10 06:08:34 +0000247llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000248 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000249 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
250 Ty->getPointeeType(), Unit);
251}
252
253llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
254 const Type *Ty,
255 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000256 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000257 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000259 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000260
261 // Size is always the size of a pointer. We can't use getTypeSize here
262 // because that does not return the correct value for references.
263 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000264 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
265 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Devang Patelca80a5f2009-10-20 19:55:01 +0000267 return
Devang Pateld58562e2010-03-09 22:49:11 +0000268 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000269 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000270
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000271}
272
Mike Stump9bc093c2009-05-14 02:03:51 +0000273llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000274 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000275 if (BlockLiteralGenericSet)
276 return BlockLiteralGeneric;
277
Mike Stump9bc093c2009-05-14 02:03:51 +0000278 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
279
280 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
281
282 llvm::DIType FieldTy;
283
284 QualType FType;
285 uint64_t FieldSize, FieldOffset;
286 unsigned FieldAlign;
287
288 llvm::DIArray Elements;
289 llvm::DIType EltTy, DescTy;
290
291 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000292 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000293 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000294 FieldSize = CGM.getContext().getTypeSize(FType);
295 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000296 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000297 "reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000298 0, FieldSize, FieldAlign,
299 FieldOffset, 0, FieldTy);
300 EltTys.push_back(FieldTy);
301
302 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000303 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000304 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000305 FieldSize = CGM.getContext().getTypeSize(FType);
306 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000307 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000308 "Size", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000309 0, FieldSize, FieldAlign,
310 FieldOffset, 0, FieldTy);
311 EltTys.push_back(FieldTy);
312
313 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000314 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000315 EltTys.clear();
316
Mike Stump3d363c52009-10-02 02:30:50 +0000317 unsigned Flags = llvm::DIType::FlagAppleBlock;
318
Mike Stump9bc093c2009-05-14 02:03:51 +0000319 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Pateld58562e2010-03-09 22:49:11 +0000320 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Mike Stump9bc093c2009-05-14 02:03:51 +0000323 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000324 uint64_t Size = CGM.getContext().getTypeSize(Ty);
325 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000328 Unit, "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000329 0, Size, Align, 0, 0, EltTy);
330
331 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000332 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000334 FieldSize = CGM.getContext().getTypeSize(FType);
335 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000336 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000337 "__isa", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000338 0, FieldSize, FieldAlign,
339 FieldOffset, 0, FieldTy);
340 EltTys.push_back(FieldTy);
341
342 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000343 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000344 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000345 FieldSize = CGM.getContext().getTypeSize(FType);
346 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000347 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000348 "__flags", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000349 0, FieldSize, FieldAlign,
350 FieldOffset, 0, FieldTy);
351 EltTys.push_back(FieldTy);
352
353 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000355 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 FieldSize = CGM.getContext().getTypeSize(FType);
357 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000359 "__reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000360 0, FieldSize, FieldAlign,
361 FieldOffset, 0, FieldTy);
362 EltTys.push_back(FieldTy);
363
364 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000365 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000367 FieldSize = CGM.getContext().getTypeSize(FType);
368 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000369 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000370 "__FuncPtr", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000371 0, FieldSize, FieldAlign,
372 FieldOffset, 0, FieldTy);
373 EltTys.push_back(FieldTy);
374
375 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000376 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000377 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000378 FieldSize = CGM.getContext().getTypeSize(Ty);
379 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000381 "__descriptor", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000382 0, FieldSize, FieldAlign,
383 FieldOffset, 0, FieldTy);
384 EltTys.push_back(FieldTy);
385
386 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000387 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000388
389 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Pateld58562e2010-03-09 22:49:11 +0000390 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000391 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Mike Stump9bc093c2009-05-14 02:03:51 +0000393 BlockLiteralGenericSet = true;
394 BlockLiteralGeneric
395 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000396 "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000397 0, Size, Align, 0, 0, EltTy);
398 return BlockLiteralGeneric;
399}
400
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000402 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000403 // Typedefs are derived from some other type. If we have a typedef of a
404 // typedef, make sure to emit the whole chain.
405 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407 // We don't set size information, but do specify where the typedef was
408 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000409 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000410 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000411 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000412
Devang Pateleb6d79b2010-02-01 21:34:11 +0000413 llvm::DIDescriptor TyContext
414 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
415 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000416 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000417 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000418 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000419 Ty->getDecl()->getName(), Unit,
420 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000421 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000422}
423
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000425 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000426 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000427
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 // Add the result type at least.
429 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431 // Set up remainder of arguments if there is a prototype.
432 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000433 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000434 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
435 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
436 } else {
437 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000438 }
439
Chris Lattner9c85ba32008-11-10 06:08:34 +0000440 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000441 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Devang Patelca80a5f2009-10-20 19:55:01 +0000443 llvm::DIType DbgTy =
444 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000445 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000446 0, 0, 0, 0, 0,
447 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000448 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000449}
450
Devang Patel428deb52010-01-19 00:00:59 +0000451/// CollectRecordFields - A helper function to collect debug info for
452/// record fields. This is used while creating debug info entry for a Record.
453void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000454CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000455 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
456 unsigned FieldNo = 0;
457 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000458 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
459 for (RecordDecl::field_iterator I = RD->field_begin(),
460 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000461 I != E; ++I, ++FieldNo) {
462 FieldDecl *Field = *I;
463 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
464
465 llvm::StringRef FieldName = Field->getName();
466
Devang Patel4835fdd2010-02-12 01:31:06 +0000467 // Ignore unnamed fields. Do not ignore unnamed records.
468 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000469 continue;
470
471 // Get the location for the field.
472 SourceLocation FieldDefLoc = Field->getLocation();
473 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000474 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000475 unsigned FieldLine = 0;
476
477 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000478 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000479 FieldLine = PLoc.getLine();
480 }
481
482 QualType FType = Field->getType();
483 uint64_t FieldSize = 0;
484 unsigned FieldAlign = 0;
485 if (!FType->isIncompleteArrayType()) {
486
487 // Bit size, align and offset of the type.
488 FieldSize = CGM.getContext().getTypeSize(FType);
489 Expr *BitWidth = Field->getBitWidth();
490 if (BitWidth)
491 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
492
493 FieldAlign = CGM.getContext().getTypeAlign(FType);
494 }
495
496 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
497
498 // Create a DW_TAG_member node to remember the offset of this field in the
499 // struct. FIXME: This is an absolutely insane way to capture this
500 // information. When we gut debug info, this should be fixed.
501 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
502 FieldName, FieldDefUnit,
503 FieldLine, FieldSize, FieldAlign,
504 FieldOffset, 0, FieldTy);
505 EltTys.push_back(FieldTy);
506 }
507}
508
Devang Patela6da1922010-01-28 00:28:01 +0000509/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
510/// function type is not updated to include implicit "this" pointer. Use this
511/// routine to get a method type which includes "this" pointer.
512llvm::DIType
513CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000514 llvm::DIFile Unit) {
Devang Patela6da1922010-01-28 00:28:01 +0000515 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000516
517 // Static methods do not need "this" pointer argument.
518 if (Method->isStatic())
519 return FnTy;
520
Devang Patela6da1922010-01-28 00:28:01 +0000521 // Add "this" pointer.
522
523 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
524 assert (Args.getNumElements() && "Invalid number of arguments!");
525
526 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
527
528 // First element is always return type. For 'void' functions it is NULL.
529 Elts.push_back(Args.getElement(0));
530
531 // "this" pointer is always first argument.
532 ASTContext &Context = CGM.getContext();
533 QualType ThisPtr =
534 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000535 llvm::DIType ThisPtrType =
536 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
537 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
538 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000539
540 // Copy rest of the arguments.
541 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
542 Elts.push_back(Args.getElement(i));
543
544 llvm::DIArray EltTypeArray =
545 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
546
547 return
548 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000549 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000550 0, 0, 0, 0, 0,
551 llvm::DIType(), EltTypeArray);
552}
553
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000554/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
555/// a single member function GlobalDecl.
556llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000557CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000558 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000559 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000560 bool IsCtorOrDtor =
561 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
562
563 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000564 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000565 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000566
567 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
568 // make sense to give a single ctor/dtor a linkage name.
569 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000570 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000572 SourceManager &SM = CGM.getContext().getSourceManager();
573
574 // Get the location for the method.
575 SourceLocation MethodDefLoc = Method->getLocation();
576 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000577 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000578 unsigned MethodLine = 0;
579
580 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000581 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000582 MethodLine = PLoc.getLine();
583 }
584
585 // Collect virtual method info.
586 llvm::DIType ContainingType;
587 unsigned Virtuality = 0;
588 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000589
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000590 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000591 if (Method->isPure())
592 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
593 else
594 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
595
596 // It doesn't make sense to give a virtual destructor a vtable index,
597 // since a single destructor has two entries in the vtable.
598 if (!isa<CXXDestructorDecl>(Method))
599 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000600 ContainingType = RecordTy;
601 }
602
603 llvm::DISubprogram SP =
604 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
605 MethodLinkageName,
606 MethodDefUnit, MethodLine,
607 MethodTy, /*isLocalToUnit=*/false,
608 Method->isThisDeclarationADefinition(),
609 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000610
611 // Don't cache ctors or dtors since we have to emit multiple functions for
612 // a single ctor or dtor.
613 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
614 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000615
616 return SP;
617}
618
Devang Patel4125fd22010-01-19 01:54:44 +0000619/// CollectCXXMemberFunctions - A helper function to collect debug info for
620/// C++ member functions.This is used while creating debug info entry for
621/// a Record.
622void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000623CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000624 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
625 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000626 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
627 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000628 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000629
Devang Pateld5322da2010-02-09 19:09:28 +0000630 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000631 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000632
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000633 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000634 }
635}
636
Devang Patela245c5b2010-01-25 23:32:18 +0000637/// CollectCXXBases - A helper function to collect debug info for
638/// C++ base classes. This is used while creating debug info entry for
639/// a Record.
640void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000641CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000642 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
643 llvm::DICompositeType &RecordTy) {
644
Devang Patel239cec62010-02-01 21:39:52 +0000645 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
646 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
647 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000648 unsigned BFlags = 0;
649 uint64_t BaseOffset;
650
651 const CXXRecordDecl *Base =
652 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
653
654 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000655 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000656 // expression where it expects +ve number.
Anders Carlssonbba16072010-03-11 07:15:17 +0000657 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000658 BFlags = llvm::DIType::FlagVirtual;
659 } else
660 BaseOffset = RL.getBaseClassOffset(Base);
661
662 AccessSpecifier Access = BI->getAccessSpecifier();
663 if (Access == clang::AS_private)
664 BFlags |= llvm::DIType::FlagPrivate;
665 else if (Access == clang::AS_protected)
666 BFlags |= llvm::DIType::FlagProtected;
667
668 llvm::DIType DTy =
669 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
670 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000671 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000672 BaseOffset, BFlags,
673 getOrCreateType(BI->getType(),
674 Unit));
675 EltTys.push_back(DTy);
676 }
Devang Patela245c5b2010-01-25 23:32:18 +0000677}
678
Devang Patel4ce3f202010-01-28 18:11:52 +0000679/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000680llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000681 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000682 return VTablePtrType;
683
684 ASTContext &Context = CGM.getContext();
685
686 /* Function type */
687 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
688 STys.push_back(getOrCreateType(Context.IntTy, Unit));
689 llvm::DIArray SElements =
690 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
691 llvm::DIType SubTy =
692 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000693 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000694 0, 0, 0, 0, 0, llvm::DIType(), SElements);
695
696 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
697 llvm::DIType vtbl_ptr_type
698 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000699 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000700 0, Size, 0, 0, 0, SubTy);
701
Devang Pateld58562e2010-03-09 22:49:11 +0000702 VTablePtrType =
703 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
704 Unit, "", Unit,
705 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000706 return VTablePtrType;
707}
708
709/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000710llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000711 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000712 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000713
714 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000715 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000716 memcpy(StrPtr, Name.data(), Name.length());
717 return llvm::StringRef(StrPtr, Name.length());
718}
719
720
721/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
722/// debug info entry in EltTys vector.
723void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000724CollectVtableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000725 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000726 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000727
728 // If there is a primary base then it will hold vtable info.
729 if (RL.getPrimaryBase())
730 return;
731
732 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000733 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000734 return;
735
736 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
737 llvm::DIType VPTR
738 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000739 getVtableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000740 0, Size, 0, 0, 0,
741 getOrCreateVTablePtrType(Unit));
742 EltTys.push_back(VPTR);
743}
744
Devang Patel65e99f22009-02-25 01:36:11 +0000745/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000746llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000747 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000748 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Chris Lattner9c85ba32008-11-10 06:08:34 +0000750 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000751 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000752 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000753 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754 Tag = llvm::dwarf::DW_TAG_union_type;
755 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000756 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000757 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000758 }
759
Anders Carlsson20f12a22009-12-06 18:00:51 +0000760 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000761
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000763 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000764 llvm::DIFile DefUnit;
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000765 unsigned Line = 0;
766 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000767 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000768 Line = PLoc.getLine();
769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 // Records and classes and unions can all be recursive. To handle them, we
772 // first generate a debug descriptor for the struct as a forward declaration.
773 // Then (if it is a definition) we go through and get debug info for all of
774 // its members. Finally, we create a descriptor for the complete type (which
775 // may refer to the forward decl if the struct is recursive) and replace all
776 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000777
Devang Pateld6c5a262010-02-01 21:52:22 +0000778 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000779 // are uniqued so use type name to ensure uniquness.
Devang Patelf3383702010-03-10 00:19:43 +0000780 llvm::SmallString<256> FwdDeclName;
781 FwdDeclName.resize(256);
782 sprintf(&FwdDeclName[0], "fwd.type.%d", FwdDeclCount++);
Devang Patel411894b2010-02-01 22:40:08 +0000783 llvm::DIDescriptor FDContext =
784 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000785 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000786 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000787 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000788 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner9c85ba32008-11-10 06:08:34 +0000790 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000791 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000793
Eli Friedman14d63652009-11-16 21:04:30 +0000794 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 // Otherwise, insert it into the TypeCache so that recursive uses will find
796 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000797 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000798 // Push the struct on region stack.
799 RegionStack.push_back(FwdDecl.getNode());
800 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801
802 // Convert all the elements.
803 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
804
Devang Pateld6c5a262010-02-01 21:52:22 +0000805 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000806 if (CXXDecl) {
807 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000808 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000809 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000810 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000811 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000812 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000813 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000814
815 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000816 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000817 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
818 ContainingType =
819 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
820 else if (CXXDecl->isDynamicClass())
821 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattner9c85ba32008-11-10 06:08:34 +0000824 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000825 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000826
827 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000828 uint64_t Size = CGM.getContext().getTypeSize(Ty);
829 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Devang Patele4c1ea02010-03-11 20:01:48 +0000831 RegionStack.pop_back();
832 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
833 RegionMap.find(Ty->getDecl());
834 if (RI != RegionMap.end())
835 RegionMap.erase(RI);
836
Devang Patel411894b2010-02-01 22:40:08 +0000837 llvm::DIDescriptor RDContext =
838 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000839 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000840 DebugFactory.CreateCompositeType(Tag, RDContext,
841 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000842 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000843 llvm::DIType(), Elements,
844 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000845
846 // Now that we have a real decl for the struct, replace anything using the
847 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000848 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +0000849 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000850 return RealDecl;
851}
852
Devang Patel9ca36b62009-02-26 21:10:26 +0000853/// CreateType - get objective-c interface type.
854llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000855 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000856 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Devang Patel9ca36b62009-02-26 21:10:26 +0000858 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000859 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000860
861 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000862 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000863 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000864 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
865
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Devang Patel17800552010-03-09 00:44:50 +0000867 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000868
Devang Patel9ca36b62009-02-26 21:10:26 +0000869 // To handle recursive interface, we
870 // first generate a debug descriptor for the struct as a forward declaration.
871 // Then (if it is a definition) we go through and get debug info for all of
872 // its members. Finally, we create a descriptor for the complete type (which
873 // may refer to the forward decl if the struct is recursive) and replace all
874 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000875 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000876 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000877 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000878 llvm::DIType(), llvm::DIArray(),
879 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Devang Patel9ca36b62009-02-26 21:10:26 +0000881 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000882 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000883 return FwdDecl;
884
Devang Patelffffb032009-11-16 20:09:38 +0000885 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000886 // Otherwise, insert it into the TypeCache so that recursive uses will find
887 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000888 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patele4c1ea02010-03-11 20:01:48 +0000889 // Push the struct on region stack.
890 RegionStack.push_back(FwdDecl.getNode());
891 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Devang Patel9ca36b62009-02-26 21:10:26 +0000892
893 // Convert all the elements.
894 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
895
Devang Pateld6c5a262010-02-01 21:52:22 +0000896 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000897 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000898 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000899 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000900 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000901 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000902 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000903 0 /* offset */, 0, SClassTy);
904 EltTys.push_back(InhTag);
905 }
906
Devang Pateld6c5a262010-02-01 21:52:22 +0000907 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000908
909 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000910 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
911 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000912 ObjCIvarDecl *Field = *I;
913 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
914
Devang Patel73621622009-11-25 17:37:31 +0000915 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000916
Devang Patelde135022009-04-27 22:40:36 +0000917 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000918 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000919 continue;
920
Devang Patel9ca36b62009-02-26 21:10:26 +0000921 // Get the location for the field.
922 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000923 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000924 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
925 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
926
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Devang Patel99c20eb2009-03-20 18:24:39 +0000928 QualType FType = Field->getType();
929 uint64_t FieldSize = 0;
930 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000931
Devang Patel99c20eb2009-03-20 18:24:39 +0000932 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patel99c20eb2009-03-20 18:24:39 +0000934 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000935 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000936 Expr *BitWidth = Field->getBitWidth();
937 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000938 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000939
Anders Carlsson20f12a22009-12-06 18:00:51 +0000940 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000941 }
942
Mike Stump1eb44332009-09-09 15:08:12 +0000943 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
944
Devang Patelc20482b2009-03-19 00:23:53 +0000945 unsigned Flags = 0;
946 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
947 Flags = llvm::DIType::FlagProtected;
948 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
949 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Devang Patel9ca36b62009-02-26 21:10:26 +0000951 // Create a DW_TAG_member node to remember the offset of this field in the
952 // struct. FIXME: This is an absolutely insane way to capture this
953 // information. When we gut debug info, this should be fixed.
954 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
955 FieldName, FieldDefUnit,
956 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000957 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000958 EltTys.push_back(FieldTy);
959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Devang Patel9ca36b62009-02-26 21:10:26 +0000961 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000962 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000963
Devang Patele4c1ea02010-03-11 20:01:48 +0000964 RegionStack.pop_back();
965 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
966 RegionMap.find(Ty->getDecl());
967 if (RI != RegionMap.end())
968 RegionMap.erase(RI);
969
Devang Patel9ca36b62009-02-26 21:10:26 +0000970 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000971 uint64_t Size = CGM.getContext().getTypeSize(Ty);
972 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Devang Patel6c1fddf2009-07-27 18:42:03 +0000974 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000975 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000976 Line, Size, Align, 0, 0, llvm::DIType(),
977 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000978
979 // Now that we have a real decl for the struct, replace anything using the
980 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000981 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patele4c1ea02010-03-11 20:01:48 +0000982 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode());
Devang Patelfe09eab2009-07-13 17:03:14 +0000983
Devang Patel9ca36b62009-02-26 21:10:26 +0000984 return RealDecl;
985}
986
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000988 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000989 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990
991 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
992
993 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000994 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000995 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000996 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000997 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000998 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001 // Return a CompositeType for the enum itself.
1002 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001003 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004
Devang Pateld6c5a262010-02-01 21:52:22 +00001005 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +00001006 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001007 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001008 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1009 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1010
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner9c85ba32008-11-10 06:08:34 +00001012 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001013 uint64_t Size = 0;
1014 unsigned Align = 0;
1015 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001016 Size = CGM.getContext().getTypeSize(Ty);
1017 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Devang Patelca80a5f2009-10-20 19:55:01 +00001020 llvm::DIType DbgTy =
1021 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001023 Size, Align, 0, 0,
1024 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001025 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001026}
1027
1028llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001029 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001030 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1031 return CreateType(RT, Unit);
1032 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1033 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattner9c85ba32008-11-10 06:08:34 +00001035 return llvm::DIType();
1036}
1037
Devang Patel70c23cd2010-02-23 22:59:39 +00001038llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001039 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001040 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1041 uint64_t NumElems = Ty->getNumElements();
1042 if (NumElems > 0)
1043 --NumElems;
1044 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1045 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1046
1047 llvm::DIArray SubscriptArray =
1048 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1049
1050 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1051 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1052
1053 return
1054 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001055 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001056 0, Size, Align, 0, 0,
1057 ElementTy, SubscriptArray);
1058}
1059
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001061 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001062 uint64_t Size;
1063 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001064
1065
Nuno Lopes010d5142009-01-28 00:35:17 +00001066 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001067 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001068 Size = 0;
1069 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001070 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001071 } else if (Ty->isIncompleteArrayType()) {
1072 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001073 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001074 } else {
1075 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001076 Size = CGM.getContext().getTypeSize(Ty);
1077 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner9c85ba32008-11-10 06:08:34 +00001080 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1081 // interior arrays, do we care? Why aren't nested arrays represented the
1082 // obvious/recursive way?
1083 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1084 QualType EltTy(Ty, 0);
1085 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001086 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001087 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001088 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001089 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 // FIXME: Verify this is right for VLAs.
1091 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1092 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner9c85ba32008-11-10 06:08:34 +00001095 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001096 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001097
Devang Patelca80a5f2009-10-20 19:55:01 +00001098 llvm::DIType DbgTy =
1099 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001100 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001101 0, Size, Align, 0, 0,
1102 getOrCreateType(EltTy, Unit),
1103 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001104 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001105}
1106
Anders Carlssona031b352009-11-06 19:19:55 +00001107llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001108 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001109 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1110 Ty, Ty->getPointeeType(), Unit);
1111}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001112
Anders Carlsson20f12a22009-12-06 18:00:51 +00001113llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001114 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001115 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1116 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1117
1118 if (!Ty->getPointeeType()->isFunctionType()) {
1119 // We have a data member pointer type.
1120 return PointerDiffDITy;
1121 }
1122
1123 // We have a member function pointer type. Treat it as a struct with two
1124 // ptrdiff_t members.
1125 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1126
1127 uint64_t FieldOffset = 0;
1128 llvm::DIDescriptor ElementTypes[2];
1129
1130 // FIXME: This should probably be a function type instead.
1131 ElementTypes[0] =
1132 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001133 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001134 Info.first, Info.second, FieldOffset, 0,
1135 PointerDiffDITy);
1136 FieldOffset += Info.first;
1137
1138 ElementTypes[1] =
1139 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001140 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001141 Info.first, Info.second, FieldOffset, 0,
1142 PointerDiffDITy);
1143
1144 llvm::DIArray Elements =
1145 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1146 llvm::array_lengthof(ElementTypes));
1147
1148 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1149 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001150 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001151 0, 0, 0, llvm::DIType(), Elements);
1152}
1153
Douglas Gregor840943d2009-12-21 20:18:30 +00001154static QualType UnwrapTypeForDebugInfo(QualType T) {
1155 do {
1156 QualType LastT = T;
1157 switch (T->getTypeClass()) {
1158 default:
1159 return T;
1160 case Type::TemplateSpecialization:
1161 T = cast<TemplateSpecializationType>(T)->desugar();
1162 break;
1163 case Type::TypeOfExpr: {
1164 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1165 T = Ty->getUnderlyingExpr()->getType();
1166 break;
1167 }
1168 case Type::TypeOf:
1169 T = cast<TypeOfType>(T)->getUnderlyingType();
1170 break;
1171 case Type::Decltype:
1172 T = cast<DecltypeType>(T)->getUnderlyingType();
1173 break;
1174 case Type::QualifiedName:
1175 T = cast<QualifiedNameType>(T)->getNamedType();
1176 break;
1177 case Type::SubstTemplateTypeParm:
1178 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1179 break;
1180 case Type::Elaborated:
1181 T = cast<ElaboratedType>(T)->getUnderlyingType();
1182 break;
1183 }
1184
1185 assert(T != LastT && "Type unwrapping failed to unwrap!");
1186 if (T == LastT)
1187 return T;
1188 } while (true);
1189
1190 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001191}
1192
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001193/// getOrCreateType - Get the type from the cache or create a new
1194/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001195llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001196 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001197 if (Ty.isNull())
1198 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregor840943d2009-12-21 20:18:30 +00001200 // Unwrap the type as needed for debug information.
1201 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001202
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001203 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001204 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001205 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001206 if (it != TypeCache.end()) {
1207 // Verify that the debug info still exists.
1208 if (&*it->second)
1209 return llvm::DIType(cast<llvm::MDNode>(it->second));
1210 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001211
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001212 // Otherwise create the type.
1213 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001214
1215 // And update the type cache.
1216 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001217 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001218}
1219
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001220/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001221llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001222 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001223 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001224 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001225 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001226
Douglas Gregor2101a822009-12-21 19:57:21 +00001227 const char *Diag = 0;
1228
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001229 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001230 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001231#define TYPE(Class, Base)
1232#define ABSTRACT_TYPE(Class, Base)
1233#define NON_CANONICAL_TYPE(Class, Base)
1234#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1235#include "clang/AST/TypeNodes.def"
1236 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001237
Anders Carlssonbfe69952009-11-06 18:24:04 +00001238 // FIXME: Handle these.
1239 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001240 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001241
1242 case Type::Vector:
1243 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001244 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001245 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001246 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001247 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1248 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1249 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1250 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001251 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001252 return CreateType(cast<BlockPointerType>(Ty), Unit);
1253 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001255 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001256 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001257 case Type::FunctionProto:
1258 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001259 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001260 case Type::ConstantArray:
1261 case Type::VariableArray:
1262 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001263 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001264
1265 case Type::LValueReference:
1266 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1267
Anders Carlsson20f12a22009-12-06 18:00:51 +00001268 case Type::MemberPointer:
1269 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001270
John McCall3cb0ebd2010-03-10 03:28:59 +00001271 case Type::InjectedClassName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001272 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001273 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001274 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001275 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001276 case Type::TypeOfExpr:
1277 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001278 case Type::Decltype:
1279 llvm_unreachable("type should have been unwrapped!");
1280 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001281
1282 case Type::RValueReference:
1283 // FIXME: Implement!
1284 Diag = "rvalue references";
1285 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001286 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001287
1288 assert(Diag && "Fall through without a diagnostic?");
1289 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1290 "debug information for %0 is not yet supported");
1291 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1292 << Diag;
1293 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001294}
1295
1296/// EmitFunctionStart - Constructs the debug code for entering a function -
1297/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001298void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001299 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Devang Patel9c6c3a02010-01-14 00:36:21 +00001302 llvm::StringRef Name;
1303 llvm::StringRef LinkageName;
1304
1305 const Decl *D = GD.getDecl();
1306 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001307 // If there is a DISubprogram for this function available then use it.
1308 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1309 FI = SPCache.find(FD);
1310 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001311 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1312 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001313 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001314 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001315 return;
1316 }
1317 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001318 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001319 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001320 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001321 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001322 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001323 } else {
1324 // Use llvm function name as linkage name.
1325 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001326 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001327 if (!Name.empty() && Name[0] == '\01')
1328 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Devang Patel98a200b2010-01-14 18:06:13 +00001331 // It is expected that CurLoc is set before using EmitFunctionStart.
1332 // Usually, CurLoc points to the left bracket location of compound
1333 // statement representing function body.
Devang Patel17800552010-03-09 00:44:50 +00001334 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001335 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001336 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattner9c85ba32008-11-10 06:08:34 +00001338 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001339 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001340 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001341 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001343 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001344 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001345 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001346}
1347
1348
Chris Lattner9c85ba32008-11-10 06:08:34 +00001349void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001350 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001352 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001353 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001354 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001355 || (SM.getInstantiationLineNumber(CurLoc) ==
1356 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001357 && SM.isFromSameFile(CurLoc, PrevLoc)))
1358 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001359
1360 // Update last state.
1361 PrevLoc = CurLoc;
1362
1363 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001364 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001365 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001366
Devang Patel8fae0602009-11-13 19:10:24 +00001367 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001368 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1369 llvm::DILocation DO(NULL);
1370 llvm::DILocation DL =
1371 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1372 DS, DO);
1373 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001374}
1375
1376/// EmitRegionStart- Constructs the debug code for entering a declarative
1377/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001378void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001379 SourceManager &SM = CGM.getContext().getSourceManager();
1380 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001381 llvm::DIDescriptor D =
1382 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1383 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001384 llvm::DIDescriptor(RegionStack.back()),
1385 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001386 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001387}
1388
1389/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1390/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001391void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001392 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1393
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001394 // Provide an region stop point.
1395 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001397 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001398}
1399
Devang Patel809b9bb2010-02-10 18:49:08 +00001400// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1401// See BuildByRefType.
1402llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1403 uint64_t *XOffset) {
1404
1405 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1406
1407 QualType FType;
1408 uint64_t FieldSize, FieldOffset;
1409 unsigned FieldAlign;
1410
Devang Patel17800552010-03-09 00:44:50 +00001411 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001412 QualType Type = VD->getType();
1413
1414 FieldOffset = 0;
1415 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1416 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1417 FieldSize = CGM.getContext().getTypeSize(FType);
1418 FieldAlign = CGM.getContext().getTypeAlign(FType);
1419 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001420 "__isa", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001421 0, FieldSize, FieldAlign,
1422 FieldOffset, 0, FieldTy);
1423 EltTys.push_back(FieldTy);
1424 FieldOffset += FieldSize;
1425
1426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1427 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 "__forwarding", 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().IntTy;
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 "__flags", 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 "__size", 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 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1460 if (HasCopyAndDispose) {
1461 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1462 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1463 FieldSize = CGM.getContext().getTypeSize(FType);
1464 FieldAlign = CGM.getContext().getTypeAlign(FType);
1465 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001466 "__copy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001467 0, FieldSize, FieldAlign,
1468 FieldOffset, 0, FieldTy);
1469 EltTys.push_back(FieldTy);
1470 FieldOffset += FieldSize;
1471
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 "__destroy_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
1484 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1485 if (Align > CharUnits::fromQuantity(
1486 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1487 unsigned AlignedOffsetInBytes
1488 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1489 unsigned NumPaddingBytes
1490 = AlignedOffsetInBytes - FieldOffset/8;
1491
1492 if (NumPaddingBytes > 0) {
1493 llvm::APInt pad(32, NumPaddingBytes);
1494 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1495 pad, ArrayType::Normal, 0);
1496 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1497 FieldSize = CGM.getContext().getTypeSize(FType);
1498 FieldAlign = CGM.getContext().getTypeAlign(FType);
1499 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
Devang Pateld58562e2010-03-09 22:49:11 +00001500 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001501 0, FieldSize, FieldAlign,
1502 FieldOffset, 0, FieldTy);
1503 EltTys.push_back(FieldTy);
1504 FieldOffset += FieldSize;
1505 }
1506 }
1507
1508 FType = Type;
1509 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1510 FieldSize = CGM.getContext().getTypeSize(FType);
1511 FieldAlign = Align.getQuantity()*8;
1512
1513 *XOffset = FieldOffset;
1514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001515 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001516 0, FieldSize, FieldAlign,
1517 FieldOffset, 0, FieldTy);
1518 EltTys.push_back(FieldTy);
1519 FieldOffset += FieldSize;
1520
1521 llvm::DIArray Elements =
1522 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1523
1524 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1525
1526 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001527 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001528 0, FieldOffset, 0, 0, Flags,
1529 llvm::DIType(), Elements);
1530
1531}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001532/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001533void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001534 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001535 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1536
Devang Patel07739032009-03-27 23:16:32 +00001537 // Do not emit variable debug information while generating optimized code.
1538 // The llvm optimizer and code generator are not yet ready to support
1539 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001540 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001541 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001542 return;
1543
Devang Patel17800552010-03-09 00:44:50 +00001544 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001545 llvm::DIType Ty;
1546 uint64_t XOffset = 0;
1547 if (VD->hasAttr<BlocksAttr>())
1548 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1549 else
1550 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001551
Chris Lattner9c85ba32008-11-10 06:08:34 +00001552 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001553 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001554 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001555 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001556 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001557 if (PLoc.isInvalid())
1558 PLoc = SM.getPresumedLoc(CurLoc);
1559 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001560 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001561 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001562 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001563 } else {
Devang Patel17800552010-03-09 00:44:50 +00001564 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Chris Lattner9c85ba32008-11-10 06:08:34 +00001567 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001568 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001569 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001570 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001571 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001572 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001573 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001574 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001575
Devang Patel8fae0602009-11-13 19:10:24 +00001576 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001577 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001578 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1579
Chris Lattner23e92c02009-12-28 23:41:39 +00001580 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001581}
1582
Mike Stumpb1a6e682009-09-30 02:43:10 +00001583/// EmitDeclare - Emit local variable declaration debug info.
1584void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1585 llvm::Value *Storage, CGBuilderTy &Builder,
1586 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001587 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001588 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1589
1590 // Do not emit variable debug information while generating optimized code.
1591 // The llvm optimizer and code generator are not yet ready to support
1592 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001593 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001594 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001595 return;
1596
1597 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001598 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001599 llvm::DIType Ty;
1600 if (VD->hasAttr<BlocksAttr>())
1601 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1602 else
1603 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001604
1605 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001606 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001607 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001608 unsigned Line = 0;
1609 if (!PLoc.isInvalid())
1610 Line = PLoc.getLine();
1611 else
Devang Patel17800552010-03-09 00:44:50 +00001612 Unit = llvm::DIFile();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001613
Devang Pateld6c5a262010-02-01 21:52:22 +00001614 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001615 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001616 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1617 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1618 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1619 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001620 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001621 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1622 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001623 // offset of __forwarding field
1624 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001625 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1626 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1627 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001628 // offset of x field
1629 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001630 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001631 }
1632
1633 // Create the descriptor for the variable.
1634 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001635 DebugFactory.CreateComplexVariable(Tag,
1636 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001637 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001638 addr);
1639 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001640 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001641 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001642
Devang Patel8fae0602009-11-13 19:10:24 +00001643 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001644 llvm::DILocation DO(NULL);
1645 llvm::DILocation DL =
1646 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001647
Chris Lattner23e92c02009-12-28 23:41:39 +00001648 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001649}
1650
Devang Pateld6c5a262010-02-01 21:52:22 +00001651void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001652 llvm::Value *Storage,
1653 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001654 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001655}
1656
Mike Stumpb1a6e682009-09-30 02:43:10 +00001657void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1658 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1659 CodeGenFunction *CGF) {
1660 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1661}
1662
Chris Lattner9c85ba32008-11-10 06:08:34 +00001663/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1664/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001665void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001666 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001667 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001668}
1669
1670
1671
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001672/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001673void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001674 const VarDecl *D) {
1675
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001676 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001677 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001678 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001679 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001680 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001681
Devang Pateleb6d79b2010-02-01 21:34:11 +00001682 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001683 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001685 // CodeGen turns int[] into int[1] so we'll do the same here.
1686 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001688 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001689 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Anders Carlsson20f12a22009-12-06 18:00:51 +00001691 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001692 ArrayType::Normal, 0);
1693 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001694 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001695 llvm::DIDescriptor DContext =
1696 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1697 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001698 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001699 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001700 Var->hasInternalLinkage(),
1701 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001702}
1703
Devang Patel9ca36b62009-02-26 21:10:26 +00001704/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001705void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001706 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001707 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001708 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001709 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001710 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001711 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001712
Devang Pateld6c5a262010-02-01 21:52:22 +00001713 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001714
Devang Pateld6c5a262010-02-01 21:52:22 +00001715 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001716 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Devang Patel9ca36b62009-02-26 21:10:26 +00001718 // CodeGen turns int[] into int[1] so we'll do the same here.
1719 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Devang Patel9ca36b62009-02-26 21:10:26 +00001721 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001722 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Anders Carlsson20f12a22009-12-06 18:00:51 +00001724 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001725 ArrayType::Normal, 0);
1726 }
1727
Devang Patelf6a39b72009-10-20 18:26:30 +00001728 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001729 getOrCreateType(T, Unit),
1730 Var->hasInternalLinkage(),
1731 true/*definition*/, Var);
1732}
Devang Patelabb485f2010-02-01 19:16:32 +00001733
1734/// getOrCreateNamesSpace - Return namespace descriptor for the given
1735/// namespace decl.
1736llvm::DINameSpace
1737CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1738 llvm::DIDescriptor Unit) {
1739 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1740 NameSpaceCache.find(NSDecl);
1741 if (I != NameSpaceCache.end())
1742 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1743
1744 SourceManager &SM = CGM.getContext().getSourceManager();
1745 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1746 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1747
1748 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001749 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001750 llvm::DINameSpace NS =
1751 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel17800552010-03-09 00:44:50 +00001752 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patelabb485f2010-02-01 19:16:32 +00001753 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1754 return NS;
1755}