blob: 323c4588428f032a171680cac92e9e48a0446d4e [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruthbc55fe22009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson3efc6e62009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel408dcf62010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel4f262052010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000042}
43
Chris Lattneraffb3732008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000046}
47
Chris Lattneraffb3732008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000051}
52
Devang Patel7bfc5962010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Patel7b7f46f2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel7bfc5962010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Patel7b7f46f2010-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 Patele8fb4b72010-02-01 22:40:08 +000063
Devang Patel7b7f46f2010-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 Patelfaf7e9a2009-10-06 00:35:31 +000068 return CompileUnit;
69}
70
Devang Patel934661e2010-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 Patel0d61eeb2010-01-28 18:21:00 +000084 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000085 memcpy(StrPtr, NS.data(), NS.length());
86 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000087}
88
Devang Patel408dcf62010-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 Carlsson3efc6e62009-12-06 18:00:51 +000095 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
97 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer1d205642009-12-08 11:02:29 +000098 AbsFileName.makeAbsolute();
Devang Patel75009452009-04-17 21:06:59 +000099
Devang Patelf2aa7072010-03-09 19:14:07 +0000100 return DebugFactory.CreateFile(AbsFileName.getLast(),
Devang Patel408dcf62010-03-09 00:44:50 +0000101 AbsFileName.getDirname(), TheCU);
102}
103/// CreateCompileUnit - Create new compile unit.
104void CGDebugInfo::CreateCompileUnit() {
105
106 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000107 SourceManager &SM = CGM.getContext().getSourceManager();
108 std::string MainFileName;
109 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
110 MainFileName = MainFile->getName();
111 else if (CGM.getCodeGenOpts().MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000112 MainFileName = "<unknown>";
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000113 else
114 MainFileName = CGM.getCodeGenOpts().MainFileName;
Devang Patela42d3ea2010-03-12 21:04:27 +0000115 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel408dcf62010-03-09 00:44:50 +0000116 AbsFileName.makeAbsolute();
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000117
Chris Lattner8c37df42009-03-25 03:28:08 +0000118 unsigned LangTag;
Devang Patel408dcf62010-03-09 00:44:50 +0000119 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner8c37df42009-03-25 03:28:08 +0000120 if (LO.CPlusPlus) {
121 if (LO.ObjC1)
122 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
123 else
124 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
125 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000126 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000127 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000128 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000129 } else {
130 LangTag = llvm::dwarf::DW_LANG_C89;
131 }
Devang Patel75009452009-04-17 21:06:59 +0000132
Benjamin Kramer1d205642009-12-08 11:02:29 +0000133 const char *Producer =
Mike Stumpfc8ff632009-10-09 18:38:12 +0000134#ifdef CLANG_VENDOR
135 CLANG_VENDOR
136#endif
137 "clang " CLANG_VERSION_STRING;
Chris Lattner5912de12009-05-02 01:00:04 +0000138
139 // Figure out which version of the ObjC runtime we have.
140 unsigned RuntimeVers = 0;
141 if (LO.ObjC1)
142 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000144 // Create new compile unit.
Devang Patel408dcf62010-03-09 00:44:50 +0000145 TheCU = DebugFactory.CreateCompileUnit(
146 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, true,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000147 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000148}
149
Devang Patel410dc002009-02-25 01:36:11 +0000150/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000151/// one if necessary.
152llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel408dcf62010-03-09 00:44:50 +0000153 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000154 unsigned Encoding = 0;
155 switch (BT->getKind()) {
156 default:
157 case BuiltinType::Void:
158 return llvm::DIType();
159 case BuiltinType::UChar:
160 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
161 case BuiltinType::Char_S:
162 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
163 case BuiltinType::UShort:
164 case BuiltinType::UInt:
165 case BuiltinType::ULong:
166 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
167 case BuiltinType::Short:
168 case BuiltinType::Int:
169 case BuiltinType::Long:
170 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
171 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
172 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000173 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000174 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000175 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000176 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000177 uint64_t Size = CGM.getContext().getTypeSize(BT);
178 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000179 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Devang Patele21912d2009-10-20 19:55:01 +0000181 llvm::DIType DbgTy =
182 DebugFactory.CreateBasicType(Unit,
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000183 BT->getName(CGM.getContext().getLangOptions()),
Devang Patele21912d2009-10-20 19:55:01 +0000184 Unit, 0, Size, Align,
185 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000186 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000187}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000188
Chris Lattner7b0344f2009-04-23 06:13:01 +0000189llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000190 llvm::DIFile Unit) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000191 // Bit size, align and offset of the type.
192 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
193 if (Ty->isComplexIntegerType())
194 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000196 uint64_t Size = CGM.getContext().getTypeSize(Ty);
197 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000198 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000199
Devang Patele21912d2009-10-20 19:55:01 +0000200 llvm::DIType DbgTy =
201 DebugFactory.CreateBasicType(Unit, "complex",
202 Unit, 0, Size, Align,
203 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000204 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000205}
206
John McCall0cf15512009-09-25 01:40:47 +0000207/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000208/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000209llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000210 QualifierCollector Qc;
211 const Type *T = Qc.strip(Ty);
212
213 // Ignore these qualifiers for now.
214 Qc.removeObjCGCAttr();
215 Qc.removeAddressSpace();
216
Chris Lattneraffb3732008-11-10 06:08:34 +0000217 // We will create one Derived type for one qualifier and recurse to handle any
218 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000219 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000220 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000221 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000222 Qc.removeConst();
223 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000224 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000225 Qc.removeVolatile();
226 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000227 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000228 Qc.removeRestrict();
229 } else {
230 assert(Qc.empty() && "Unknown type qualifier for debug info");
231 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000232 }
Mike Stump11289f42009-09-09 15:08:12 +0000233
John McCall0cf15512009-09-25 01:40:47 +0000234 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
235
Daniel Dunbara290ded2008-10-31 03:54:29 +0000236 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
237 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000238 llvm::DIType DbgTy =
Devang Patel93f274c2010-03-09 22:49:11 +0000239 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000240 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000241 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000242}
243
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000244llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000245 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000246 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000247 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
248 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000249 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000250}
251
Chris Lattneraffb3732008-11-10 06:08:34 +0000252llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000253 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000254 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
255 Ty->getPointeeType(), Unit);
256}
257
258llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
259 const Type *Ty,
260 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000261 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000262 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000263
Sanjiv Gupta98070572008-05-25 05:15:42 +0000264 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000265
266 // Size is always the size of a pointer. We can't use getTypeSize here
267 // because that does not return the correct value for references.
268 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000269 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
270 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Devang Patele21912d2009-10-20 19:55:01 +0000272 return
Devang Patel93f274c2010-03-09 22:49:11 +0000273 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000274 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000275
Sanjiv Gupta98070572008-05-25 05:15:42 +0000276}
277
Mike Stump31f099c2009-05-14 02:03:51 +0000278llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000279 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000280 if (BlockLiteralGenericSet)
281 return BlockLiteralGeneric;
282
Mike Stump31f099c2009-05-14 02:03:51 +0000283 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
284
285 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
286
287 llvm::DIType FieldTy;
288
289 QualType FType;
290 uint64_t FieldSize, FieldOffset;
291 unsigned FieldAlign;
292
293 llvm::DIArray Elements;
294 llvm::DIType EltTy, DescTy;
295
296 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000297 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000298 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000299 FieldSize = CGM.getContext().getTypeSize(FType);
300 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000301 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000302 "reserved", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000303 0, FieldSize, FieldAlign,
304 FieldOffset, 0, FieldTy);
305 EltTys.push_back(FieldTy);
306
307 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000308 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000309 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000310 FieldSize = CGM.getContext().getTypeSize(FType);
311 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000312 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000313 "Size", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000314 0, FieldSize, FieldAlign,
315 FieldOffset, 0, FieldTy);
316 EltTys.push_back(FieldTy);
317
318 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000319 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000320 EltTys.clear();
321
Mike Stump581b9ad2009-10-02 02:30:50 +0000322 unsigned Flags = llvm::DIType::FlagAppleBlock;
323
Mike Stump31f099c2009-05-14 02:03:51 +0000324 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel93f274c2010-03-09 22:49:11 +0000325 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000326 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000327
Mike Stump31f099c2009-05-14 02:03:51 +0000328 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000329 uint64_t Size = CGM.getContext().getTypeSize(Ty);
330 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Mike Stump31f099c2009-05-14 02:03:51 +0000332 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000333 Unit, "", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000334 0, Size, Align, 0, 0, EltTy);
335
336 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000337 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000338 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000339 FieldSize = CGM.getContext().getTypeSize(FType);
340 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000341 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000342 "__isa", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000343 0, FieldSize, FieldAlign,
344 FieldOffset, 0, FieldTy);
345 EltTys.push_back(FieldTy);
346
347 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000348 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000349 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000350 FieldSize = CGM.getContext().getTypeSize(FType);
351 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000352 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000353 "__flags", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000354 0, FieldSize, FieldAlign,
355 FieldOffset, 0, FieldTy);
356 EltTys.push_back(FieldTy);
357
358 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000359 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000360 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000361 FieldSize = CGM.getContext().getTypeSize(FType);
362 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000363 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000364 "__reserved", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000365 0, FieldSize, FieldAlign,
366 FieldOffset, 0, FieldTy);
367 EltTys.push_back(FieldTy);
368
369 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000370 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000371 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000372 FieldSize = CGM.getContext().getTypeSize(FType);
373 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000374 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000375 "__FuncPtr", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000376 0, FieldSize, FieldAlign,
377 FieldOffset, 0, FieldTy);
378 EltTys.push_back(FieldTy);
379
380 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000381 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000382 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000383 FieldSize = CGM.getContext().getTypeSize(Ty);
384 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000385 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000386 "__descriptor", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000387 0, FieldSize, FieldAlign,
388 FieldOffset, 0, FieldTy);
389 EltTys.push_back(FieldTy);
390
391 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000392 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000393
394 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel93f274c2010-03-09 22:49:11 +0000395 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000396 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000397
Mike Stump31f099c2009-05-14 02:03:51 +0000398 BlockLiteralGenericSet = true;
399 BlockLiteralGeneric
400 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000401 "", Unit,
Mike Stump31f099c2009-05-14 02:03:51 +0000402 0, Size, Align, 0, 0, EltTy);
403 return BlockLiteralGeneric;
404}
405
Chris Lattneraffb3732008-11-10 06:08:34 +0000406llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000407 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000408 // Typedefs are derived from some other type. If we have a typedef of a
409 // typedef, make sure to emit the whole chain.
410 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattneraffb3732008-11-10 06:08:34 +0000412 // We don't set size information, but do specify where the typedef was
413 // declared.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000414 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelbb4820d2010-01-29 22:29:31 +0000415 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000416 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta98070572008-05-25 05:15:42 +0000417
Devang Patel7b7f46f2010-02-01 21:34:11 +0000418 llvm::DIDescriptor TyContext
419 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
420 Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000421 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000422 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Patel7b7f46f2010-02-01 21:34:11 +0000423 TyContext,
Devang Patelbb4820d2010-01-29 22:29:31 +0000424 Ty->getDecl()->getName(), Unit,
425 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000426 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000427}
428
Chris Lattneraffb3732008-11-10 06:08:34 +0000429llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000430 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000431 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000432
Chris Lattneraffb3732008-11-10 06:08:34 +0000433 // Add the result type at least.
434 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattneraffb3732008-11-10 06:08:34 +0000436 // Set up remainder of arguments if there is a prototype.
437 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000438 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000439 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
440 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
441 } else {
442 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000443 }
444
Chris Lattneraffb3732008-11-10 06:08:34 +0000445 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000446 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000447
Devang Patele21912d2009-10-20 19:55:01 +0000448 llvm::DIType DbgTy =
449 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000450 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000451 0, 0, 0, 0, 0,
452 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000453 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000454}
455
Devang Patel889ce762010-01-19 00:00:59 +0000456/// CollectRecordFields - A helper function to collect debug info for
457/// record fields. This is used while creating debug info entry for a Record.
458void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000459CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel889ce762010-01-19 00:00:59 +0000460 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
461 unsigned FieldNo = 0;
462 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel1c0954c2010-02-01 21:39:52 +0000463 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
464 for (RecordDecl::field_iterator I = RD->field_begin(),
465 E = RD->field_end();
Devang Patel889ce762010-01-19 00:00:59 +0000466 I != E; ++I, ++FieldNo) {
467 FieldDecl *Field = *I;
468 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
469
470 llvm::StringRef FieldName = Field->getName();
471
Devang Patelf4df65c2010-02-12 01:31:06 +0000472 // Ignore unnamed fields. Do not ignore unnamed records.
473 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel889ce762010-01-19 00:00:59 +0000474 continue;
475
476 // Get the location for the field.
477 SourceLocation FieldDefLoc = Field->getLocation();
478 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel408dcf62010-03-09 00:44:50 +0000479 llvm::DIFile FieldDefUnit;
Devang Patel889ce762010-01-19 00:00:59 +0000480 unsigned FieldLine = 0;
481
482 if (!PLoc.isInvalid()) {
Devang Patel408dcf62010-03-09 00:44:50 +0000483 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel889ce762010-01-19 00:00:59 +0000484 FieldLine = PLoc.getLine();
485 }
486
487 QualType FType = Field->getType();
488 uint64_t FieldSize = 0;
489 unsigned FieldAlign = 0;
490 if (!FType->isIncompleteArrayType()) {
491
492 // Bit size, align and offset of the type.
493 FieldSize = CGM.getContext().getTypeSize(FType);
494 Expr *BitWidth = Field->getBitWidth();
495 if (BitWidth)
496 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
497
498 FieldAlign = CGM.getContext().getTypeAlign(FType);
499 }
500
501 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
502
503 // Create a DW_TAG_member node to remember the offset of this field in the
504 // struct. FIXME: This is an absolutely insane way to capture this
505 // information. When we gut debug info, this should be fixed.
506 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
507 FieldName, FieldDefUnit,
508 FieldLine, FieldSize, FieldAlign,
509 FieldOffset, 0, FieldTy);
510 EltTys.push_back(FieldTy);
511 }
512}
513
Devang Patel3d4e6d92010-01-28 00:28:01 +0000514/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
515/// function type is not updated to include implicit "this" pointer. Use this
516/// routine to get a method type which includes "this" pointer.
517llvm::DIType
518CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000519 llvm::DIFile Unit) {
Devang Patel3d4e6d92010-01-28 00:28:01 +0000520 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000521
522 // Static methods do not need "this" pointer argument.
523 if (Method->isStatic())
524 return FnTy;
525
Devang Patel3d4e6d92010-01-28 00:28:01 +0000526 // Add "this" pointer.
527
528 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
529 assert (Args.getNumElements() && "Invalid number of arguments!");
530
531 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
532
533 // First element is always return type. For 'void' functions it is NULL.
534 Elts.push_back(Args.getElement(0));
535
536 // "this" pointer is always first argument.
537 ASTContext &Context = CGM.getContext();
538 QualType ThisPtr =
539 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patelcce7e852010-02-09 17:57:50 +0000540 llvm::DIType ThisPtrType =
541 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
542 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
543 Elts.push_back(ThisPtrType);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000544
545 // Copy rest of the arguments.
546 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
547 Elts.push_back(Args.getElement(i));
548
549 llvm::DIArray EltTypeArray =
550 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
551
552 return
553 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000554 Unit, "", Unit,
Devang Patel3d4e6d92010-01-28 00:28:01 +0000555 0, 0, 0, 0, 0,
556 llvm::DIType(), EltTypeArray);
557}
558
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000559/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
560/// a single member function GlobalDecl.
561llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000562CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000563 llvm::DIFile Unit,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000564 llvm::DICompositeType &RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000565 bool IsCtorOrDtor =
566 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
567
568 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000569 llvm::StringRef MethodLinkageName;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000570 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000571
572 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
573 // make sense to give a single ctor/dtor a linkage name.
574 if (!IsCtorOrDtor)
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000575 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000576
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000577 SourceManager &SM = CGM.getContext().getSourceManager();
578
579 // Get the location for the method.
580 SourceLocation MethodDefLoc = Method->getLocation();
581 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel408dcf62010-03-09 00:44:50 +0000582 llvm::DIFile MethodDefUnit;
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000583 unsigned MethodLine = 0;
584
585 if (!PLoc.isInvalid()) {
Devang Patel408dcf62010-03-09 00:44:50 +0000586 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000587 MethodLine = PLoc.getLine();
588 }
589
590 // Collect virtual method info.
591 llvm::DIType ContainingType;
592 unsigned Virtuality = 0;
593 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000594
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000595 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000596 if (Method->isPure())
597 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
598 else
599 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
600
601 // It doesn't make sense to give a virtual destructor a vtable index,
602 // since a single destructor has two entries in the vtable.
603 if (!isa<CXXDestructorDecl>(Method))
604 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000605 ContainingType = RecordTy;
606 }
607
608 llvm::DISubprogram SP =
609 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
610 MethodLinkageName,
611 MethodDefUnit, MethodLine,
612 MethodTy, /*isLocalToUnit=*/false,
613 Method->isThisDeclarationADefinition(),
614 Virtuality, VIndex, ContainingType);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000615
616 // Don't cache ctors or dtors since we have to emit multiple functions for
617 // a single ctor or dtor.
618 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
619 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000620
621 return SP;
622}
623
Devang Patel7a12ad02010-01-19 01:54:44 +0000624/// CollectCXXMemberFunctions - A helper function to collect debug info for
625/// C++ member functions.This is used while creating debug info entry for
626/// a Record.
627void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000628CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel7a12ad02010-01-19 01:54:44 +0000629 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
630 llvm::DICompositeType &RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000631 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
632 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000633 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000634
Devang Patel0ae70d12010-02-09 19:09:28 +0000635 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000636 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000637
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000638 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000639 }
640}
641
Devang Patelc54353d2010-01-25 23:32:18 +0000642/// CollectCXXBases - A helper function to collect debug info for
643/// C++ base classes. This is used while creating debug info entry for
644/// a Record.
645void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000646CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patelc54353d2010-01-25 23:32:18 +0000647 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
648 llvm::DICompositeType &RecordTy) {
649
Devang Patel1c0954c2010-02-01 21:39:52 +0000650 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
651 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
652 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000653 unsigned BFlags = 0;
654 uint64_t BaseOffset;
655
656 const CXXRecordDecl *Base =
657 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
658
659 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000660 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000661 // expression where it expects +ve number.
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000662 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetOffset(RD, Base);
Devang Patel128aa9d2010-01-28 21:54:15 +0000663 BFlags = llvm::DIType::FlagVirtual;
664 } else
665 BaseOffset = RL.getBaseClassOffset(Base);
666
667 AccessSpecifier Access = BI->getAccessSpecifier();
668 if (Access == clang::AS_private)
669 BFlags |= llvm::DIType::FlagPrivate;
670 else if (Access == clang::AS_protected)
671 BFlags |= llvm::DIType::FlagProtected;
672
673 llvm::DIType DTy =
674 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
675 RecordTy, llvm::StringRef(),
Devang Patel93f274c2010-03-09 22:49:11 +0000676 Unit, 0, 0, 0,
Devang Patel128aa9d2010-01-28 21:54:15 +0000677 BaseOffset, BFlags,
678 getOrCreateType(BI->getType(),
679 Unit));
680 EltTys.push_back(DTy);
681 }
Devang Patelc54353d2010-01-25 23:32:18 +0000682}
683
Devang Patel84033fb2010-01-28 18:11:52 +0000684/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +0000685llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +0000686 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +0000687 return VTablePtrType;
688
689 ASTContext &Context = CGM.getContext();
690
691 /* Function type */
692 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
693 STys.push_back(getOrCreateType(Context.IntTy, Unit));
694 llvm::DIArray SElements =
695 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
696 llvm::DIType SubTy =
697 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000698 Unit, "", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000699 0, 0, 0, 0, 0, llvm::DIType(), SElements);
700
701 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
702 llvm::DIType vtbl_ptr_type
703 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000704 Unit, "__vtbl_ptr_type", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000705 0, Size, 0, 0, 0, SubTy);
706
Devang Patel93f274c2010-03-09 22:49:11 +0000707 VTablePtrType =
708 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
709 Unit, "", Unit,
710 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel84033fb2010-01-28 18:11:52 +0000711 return VTablePtrType;
712}
713
714/// getVtableName - Get vtable name for the given Class.
Devang Patel1c0954c2010-02-01 21:39:52 +0000715llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000716 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000717 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000718
719 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000720 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000721 memcpy(StrPtr, Name.data(), Name.length());
722 return llvm::StringRef(StrPtr, Name.length());
723}
724
725
726/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
727/// debug info entry in EltTys vector.
728void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000729CollectVtableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000730 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000731 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000732
733 // If there is a primary base then it will hold vtable info.
734 if (RL.getPrimaryBase())
735 return;
736
737 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000738 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000739 return;
740
741 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
742 llvm::DIType VPTR
743 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000744 getVtableName(RD), Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000745 0, Size, 0, 0, 0,
746 getOrCreateVTablePtrType(Unit));
747 EltTys.push_back(VPTR);
748}
749
Devang Patel410dc002009-02-25 01:36:11 +0000750/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000751llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000752 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000753 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000754
Chris Lattneraffb3732008-11-10 06:08:34 +0000755 unsigned Tag;
Devang Patel3efd1472010-02-01 21:52:22 +0000756 if (RD->isStruct())
Chris Lattneraffb3732008-11-10 06:08:34 +0000757 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel3efd1472010-02-01 21:52:22 +0000758 else if (RD->isUnion())
Chris Lattneraffb3732008-11-10 06:08:34 +0000759 Tag = llvm::dwarf::DW_TAG_union_type;
760 else {
Devang Patel3efd1472010-02-01 21:52:22 +0000761 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattneraffb3732008-11-10 06:08:34 +0000762 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000763 }
764
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000765 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000766
Chris Lattneraffb3732008-11-10 06:08:34 +0000767 // Get overall information about the record type for the debug info.
Devang Patel3efd1472010-02-01 21:52:22 +0000768 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +0000769 llvm::DIFile DefUnit;
Chris Lattner448a2282009-05-05 05:16:17 +0000770 unsigned Line = 0;
771 if (!PLoc.isInvalid()) {
Devang Patel408dcf62010-03-09 00:44:50 +0000772 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattner448a2282009-05-05 05:16:17 +0000773 Line = PLoc.getLine();
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattneraffb3732008-11-10 06:08:34 +0000776 // Records and classes and unions can all be recursive. To handle them, we
777 // first generate a debug descriptor for the struct as a forward declaration.
778 // Then (if it is a definition) we go through and get debug info for all of
779 // its members. Finally, we create a descriptor for the complete type (which
780 // may refer to the forward decl if the struct is recursive) and replace all
781 // uses of the forward declaration with the final definition.
Devang Patel3f4a77e2010-01-20 23:56:40 +0000782
Devang Patel3efd1472010-02-01 21:52:22 +0000783 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelab793232010-02-01 22:51:29 +0000784 // are uniqued so use type name to ensure uniquness.
Benjamin Kramer69b3c432010-03-13 12:06:51 +0000785 llvm::SmallString<128> FwdDeclName;
786 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patele8fb4b72010-02-01 22:40:08 +0000787 llvm::DIDescriptor FDContext =
788 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel06cceef2009-07-22 18:57:00 +0000789 llvm::DICompositeType FwdDecl =
Devang Patel4f262052010-03-09 21:32:27 +0000790 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patel7bdf0962009-11-12 00:51:46 +0000791 DefUnit, Line, 0, 0, 0, 0,
Chris Lattneraffb3732008-11-10 06:08:34 +0000792 llvm::DIType(), llvm::DIArray());
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattneraffb3732008-11-10 06:08:34 +0000794 // If this is just a forward declaration, return it.
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000795 if (!RD->getDefinition())
Chris Lattneraffb3732008-11-10 06:08:34 +0000796 return FwdDecl;
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000797
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000798 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000799 // Otherwise, insert it into the TypeCache so that recursive uses will find
800 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000801 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel01bb5ce2010-03-11 20:01:48 +0000802 // Push the struct on region stack.
803 RegionStack.push_back(FwdDecl.getNode());
804 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Chris Lattneraffb3732008-11-10 06:08:34 +0000805
806 // Convert all the elements.
807 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
808
Devang Patel3efd1472010-02-01 21:52:22 +0000809 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +0000810 if (CXXDecl) {
811 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel84033fb2010-01-28 18:11:52 +0000812 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000813 }
Devang Patel3efd1472010-02-01 21:52:22 +0000814 CollectRecordFields(RD, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000815 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000816 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000817 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000818
819 // A class's primary base or the class itself contains the vtable.
Devang Patel3efd1472010-02-01 21:52:22 +0000820 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patelabb44132010-01-28 00:54:21 +0000821 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
822 ContainingType =
823 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
824 else if (CXXDecl->isDynamicClass())
825 ContainingType = FwdDecl.getNode();
Devang Patelc54353d2010-01-25 23:32:18 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattneraffb3732008-11-10 06:08:34 +0000828 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000829 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000830
831 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000832 uint64_t Size = CGM.getContext().getTypeSize(Ty);
833 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000834
Devang Patel01bb5ce2010-03-11 20:01:48 +0000835 RegionStack.pop_back();
836 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
837 RegionMap.find(Ty->getDecl());
838 if (RI != RegionMap.end())
839 RegionMap.erase(RI);
840
Devang Patele8fb4b72010-02-01 22:40:08 +0000841 llvm::DIDescriptor RDContext =
842 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel06cceef2009-07-22 18:57:00 +0000843 llvm::DICompositeType RealDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000844 DebugFactory.CreateCompositeType(Tag, RDContext,
845 RD->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000846 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000847 llvm::DIType(), Elements,
848 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000849
850 // Now that we have a real decl for the struct, replace anything using the
851 // old decl with the new one. This will recursively update the debug info.
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000852 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel01bb5ce2010-03-11 20:01:48 +0000853 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode());
Chris Lattneraffb3732008-11-10 06:08:34 +0000854 return RealDecl;
855}
856
Devang Patelf4c205b2009-02-26 21:10:26 +0000857/// CreateType - get objective-c interface type.
858llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000859 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000860 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000861
Devang Patelf4c205b2009-02-26 21:10:26 +0000862 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000863 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelf4c205b2009-02-26 21:10:26 +0000864
865 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +0000866 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel3efd1472010-02-01 21:52:22 +0000867 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000868 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
869
Mike Stump11289f42009-09-09 15:08:12 +0000870
Devang Patel408dcf62010-03-09 00:44:50 +0000871 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000872
Devang Patelf4c205b2009-02-26 21:10:26 +0000873 // To handle recursive interface, we
874 // first generate a debug descriptor for the struct as a forward declaration.
875 // Then (if it is a definition) we go through and get debug info for all of
876 // its members. Finally, we create a descriptor for the complete type (which
877 // may refer to the forward decl if the struct is recursive) and replace all
878 // uses of the forward declaration with the final definition.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000879 llvm::DICompositeType FwdDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000880 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000881 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000882 llvm::DIType(), llvm::DIArray(),
883 RuntimeLang);
Mike Stump11289f42009-09-09 15:08:12 +0000884
Devang Patelf4c205b2009-02-26 21:10:26 +0000885 // If this is just a forward declaration, return it.
Devang Patel3efd1472010-02-01 21:52:22 +0000886 if (ID->isForwardDecl())
Devang Patelf4c205b2009-02-26 21:10:26 +0000887 return FwdDecl;
888
Devang Patel10909d52009-11-16 20:09:38 +0000889 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000890 // Otherwise, insert it into the TypeCache so that recursive uses will find
891 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000892 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel01bb5ce2010-03-11 20:01:48 +0000893 // Push the struct on region stack.
894 RegionStack.push_back(FwdDecl.getNode());
895 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode());
Devang Patelf4c205b2009-02-26 21:10:26 +0000896
897 // Convert all the elements.
898 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
899
Devang Patel3efd1472010-02-01 21:52:22 +0000900 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +0000901 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +0000902 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000903 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000904 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000905 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Patel93f274c2010-03-09 22:49:11 +0000906 Unit, "", Unit, 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +0000907 0 /* offset */, 0, SClassTy);
908 EltTys.push_back(InhTag);
909 }
910
Devang Patel3efd1472010-02-01 21:52:22 +0000911 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +0000912
913 unsigned FieldNo = 0;
Devang Patel3efd1472010-02-01 21:52:22 +0000914 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
915 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +0000916 ObjCIvarDecl *Field = *I;
917 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
918
Devang Patel58bf6e12009-11-25 17:37:31 +0000919 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +0000920
Devang Pateldf348f12009-04-27 22:40:36 +0000921 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +0000922 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +0000923 continue;
924
Devang Patelf4c205b2009-02-26 21:10:26 +0000925 // Get the location for the field.
926 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel408dcf62010-03-09 00:44:50 +0000927 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel12f0dea2009-04-17 21:35:15 +0000928 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
929 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
930
Mike Stump11289f42009-09-09 15:08:12 +0000931
Devang Patel9f804932009-03-20 18:24:39 +0000932 QualType FType = Field->getType();
933 uint64_t FieldSize = 0;
934 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +0000935
Devang Patel9f804932009-03-20 18:24:39 +0000936 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000937
Devang Patel9f804932009-03-20 18:24:39 +0000938 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000939 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000940 Expr *BitWidth = Field->getBitWidth();
941 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000942 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000943
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000944 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000945 }
946
Mike Stump11289f42009-09-09 15:08:12 +0000947 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
948
Devang Patelec4bad52009-03-19 00:23:53 +0000949 unsigned Flags = 0;
950 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
951 Flags = llvm::DIType::FlagProtected;
952 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
953 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Devang Patelf4c205b2009-02-26 21:10:26 +0000955 // Create a DW_TAG_member node to remember the offset of this field in the
956 // struct. FIXME: This is an absolutely insane way to capture this
957 // information. When we gut debug info, this should be fixed.
958 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
959 FieldName, FieldDefUnit,
960 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +0000961 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +0000962 EltTys.push_back(FieldTy);
963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Devang Patelf4c205b2009-02-26 21:10:26 +0000965 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +0000966 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +0000967
Devang Patel01bb5ce2010-03-11 20:01:48 +0000968 RegionStack.pop_back();
969 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
970 RegionMap.find(Ty->getDecl());
971 if (RI != RegionMap.end())
972 RegionMap.erase(RI);
973
Devang Patelf4c205b2009-02-26 21:10:26 +0000974 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000975 uint64_t Size = CGM.getContext().getTypeSize(Ty);
976 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000977
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000978 llvm::DICompositeType RealDecl =
Devang Patel3efd1472010-02-01 21:52:22 +0000979 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +0000980 Line, Size, Align, 0, 0, llvm::DIType(),
981 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +0000982
983 // Now that we have a real decl for the struct, replace anything using the
984 // old decl with the new one. This will recursively update the debug info.
Devang Patel10909d52009-11-16 20:09:38 +0000985 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel01bb5ce2010-03-11 20:01:48 +0000986 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode());
Devang Patel9c3a0182009-07-13 17:03:14 +0000987
Devang Patelf4c205b2009-02-26 21:10:26 +0000988 return RealDecl;
989}
990
Chris Lattneraffb3732008-11-10 06:08:34 +0000991llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000992 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000993 EnumDecl *ED = Ty->getDecl();
Chris Lattneraffb3732008-11-10 06:08:34 +0000994
995 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
996
997 // Create DIEnumerator elements for each enumerator.
Mike Stump11289f42009-09-09 15:08:12 +0000998 for (EnumDecl::enumerator_iterator
Devang Patel3efd1472010-02-01 21:52:22 +0000999 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00001000 Enum != EnumEnd; ++Enum) {
Devang Patel58bf6e12009-11-25 17:37:31 +00001001 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001002 Enum->getInitVal().getZExtValue()));
Chris Lattneraffb3732008-11-10 06:08:34 +00001003 }
Mike Stump11289f42009-09-09 15:08:12 +00001004
Chris Lattneraffb3732008-11-10 06:08:34 +00001005 // Return a CompositeType for the enum itself.
1006 llvm::DIArray EltArray =
Jay Foad7d0479f2009-05-21 09:52:38 +00001007 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001008
Devang Patel3efd1472010-02-01 21:52:22 +00001009 SourceLocation DefLoc = ED->getLocation();
Devang Patel408dcf62010-03-09 00:44:50 +00001010 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001011 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001012 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1013 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1014
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattneraffb3732008-11-10 06:08:34 +00001016 // Size and align of the type.
Eli Friedman2ad7e172009-05-04 04:39:55 +00001017 uint64_t Size = 0;
1018 unsigned Align = 0;
1019 if (!Ty->isIncompleteType()) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001020 Size = CGM.getContext().getTypeSize(Ty);
1021 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman2ad7e172009-05-04 04:39:55 +00001022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Devang Patele21912d2009-10-20 19:55:01 +00001024 llvm::DIType DbgTy =
1025 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel3efd1472010-02-01 21:52:22 +00001026 Unit, ED->getName(), DefUnit, Line,
Devang Patele21912d2009-10-20 19:55:01 +00001027 Size, Align, 0, 0,
1028 llvm::DIType(), EltArray);
Devang Patele21912d2009-10-20 19:55:01 +00001029 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001030}
1031
1032llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001033 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001034 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1035 return CreateType(RT, Unit);
1036 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1037 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattneraffb3732008-11-10 06:08:34 +00001039 return llvm::DIType();
1040}
1041
Devang Patelb4073382010-02-23 22:59:39 +00001042llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001043 llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001044 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1045 uint64_t NumElems = Ty->getNumElements();
1046 if (NumElems > 0)
1047 --NumElems;
1048 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1049 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1050
1051 llvm::DIArray SubscriptArray =
1052 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1053
1054 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1055 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1056
1057 return
1058 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001059 Unit, "", Unit,
Devang Patelb4073382010-02-23 22:59:39 +00001060 0, Size, Align, 0, 0,
1061 ElementTy, SubscriptArray);
1062}
1063
Chris Lattneraffb3732008-11-10 06:08:34 +00001064llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001065 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001066 uint64_t Size;
1067 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001068
1069
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001070 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001071 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001072 Size = 0;
1073 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001074 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001075 } else if (Ty->isIncompleteArrayType()) {
1076 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001077 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001078 } else {
1079 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001080 Size = CGM.getContext().getTypeSize(Ty);
1081 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattneraffb3732008-11-10 06:08:34 +00001084 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1085 // interior arrays, do we care? Why aren't nested arrays represented the
1086 // obvious/recursive way?
1087 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1088 QualType EltTy(Ty, 0);
1089 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001090 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001091 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001092 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001093 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001094 // FIXME: Verify this is right for VLAs.
1095 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1096 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001097 }
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattneraffb3732008-11-10 06:08:34 +00001099 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001100 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001101
Devang Patele21912d2009-10-20 19:55:01 +00001102 llvm::DIType DbgTy =
1103 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001104 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +00001105 0, Size, Align, 0, 0,
1106 getOrCreateType(EltTy, Unit),
1107 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001108 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001109}
1110
Anders Carlsson443f6772009-11-06 19:19:55 +00001111llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001112 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001113 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1114 Ty, Ty->getPointeeType(), Unit);
1115}
Chris Lattneraffb3732008-11-10 06:08:34 +00001116
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001117llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001118 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001119 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1120 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1121
1122 if (!Ty->getPointeeType()->isFunctionType()) {
1123 // We have a data member pointer type.
1124 return PointerDiffDITy;
1125 }
1126
1127 // We have a member function pointer type. Treat it as a struct with two
1128 // ptrdiff_t members.
1129 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1130
1131 uint64_t FieldOffset = 0;
1132 llvm::DIDescriptor ElementTypes[2];
1133
1134 // FIXME: This should probably be a function type instead.
1135 ElementTypes[0] =
1136 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001137 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001138 Info.first, Info.second, FieldOffset, 0,
1139 PointerDiffDITy);
1140 FieldOffset += Info.first;
1141
1142 ElementTypes[1] =
1143 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001144 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001145 Info.first, Info.second, FieldOffset, 0,
1146 PointerDiffDITy);
1147
1148 llvm::DIArray Elements =
1149 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1150 llvm::array_lengthof(ElementTypes));
1151
1152 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1153 U, llvm::StringRef("test"),
Devang Patel93f274c2010-03-09 22:49:11 +00001154 U, 0, FieldOffset,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001155 0, 0, 0, llvm::DIType(), Elements);
1156}
1157
Douglas Gregor0f139a12009-12-21 20:18:30 +00001158static QualType UnwrapTypeForDebugInfo(QualType T) {
1159 do {
1160 QualType LastT = T;
1161 switch (T->getTypeClass()) {
1162 default:
1163 return T;
1164 case Type::TemplateSpecialization:
1165 T = cast<TemplateSpecializationType>(T)->desugar();
1166 break;
1167 case Type::TypeOfExpr: {
1168 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1169 T = Ty->getUnderlyingExpr()->getType();
1170 break;
1171 }
1172 case Type::TypeOf:
1173 T = cast<TypeOfType>(T)->getUnderlyingType();
1174 break;
1175 case Type::Decltype:
1176 T = cast<DecltypeType>(T)->getUnderlyingType();
1177 break;
1178 case Type::QualifiedName:
1179 T = cast<QualifiedNameType>(T)->getNamedType();
1180 break;
1181 case Type::SubstTemplateTypeParm:
1182 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1183 break;
1184 case Type::Elaborated:
1185 T = cast<ElaboratedType>(T)->getUnderlyingType();
1186 break;
1187 }
1188
1189 assert(T != LastT && "Type unwrapping failed to unwrap!");
1190 if (T == LastT)
1191 return T;
1192 } while (true);
1193
1194 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001195}
1196
Sanjiv Gupta98070572008-05-25 05:15:42 +00001197/// getOrCreateType - Get the type from the cache or create a new
1198/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001199llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001200 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001201 if (Ty.isNull())
1202 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001203
Douglas Gregor0f139a12009-12-21 20:18:30 +00001204 // Unwrap the type as needed for debug information.
1205 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001206
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001207 // Check for existing entry.
Daniel Dunbar99961382009-09-19 20:17:48 +00001208 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001209 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001210 if (it != TypeCache.end()) {
1211 // Verify that the debug info still exists.
1212 if (&*it->second)
1213 return llvm::DIType(cast<llvm::MDNode>(it->second));
1214 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001215
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001216 // Otherwise create the type.
1217 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001218
1219 // And update the type cache.
1220 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001221 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001222}
1223
Anders Carlsson6037e782009-11-14 20:52:05 +00001224/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001225llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001226 llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001227 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001228 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001229 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001230
Douglas Gregor0915b432009-12-21 19:57:21 +00001231 const char *Diag = 0;
1232
Sanjiv Gupta98070572008-05-25 05:15:42 +00001233 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001234 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001235#define TYPE(Class, Base)
1236#define ABSTRACT_TYPE(Class, Base)
1237#define NON_CANONICAL_TYPE(Class, Base)
1238#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1239#include "clang/AST/TypeNodes.def"
1240 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001241
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001242 // FIXME: Handle these.
1243 case Type::ExtVector:
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001244 return llvm::DIType();
Devang Patelb4073382010-02-23 22:59:39 +00001245
1246 case Type::Vector:
1247 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001248 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001249 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001250 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001251 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1252 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1253 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1254 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001255 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001256 return CreateType(cast<BlockPointerType>(Ty), Unit);
1257 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001258 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001259 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001260 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001261 case Type::FunctionProto:
1262 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001263 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001264 case Type::ConstantArray:
1265 case Type::VariableArray:
1266 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001267 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001268
1269 case Type::LValueReference:
1270 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1271
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001272 case Type::MemberPointer:
1273 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001274
John McCalle78aac42010-03-10 03:28:59 +00001275 case Type::InjectedClassName:
Douglas Gregor0915b432009-12-21 19:57:21 +00001276 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001277 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001278 case Type::QualifiedName:
Douglas Gregor0915b432009-12-21 19:57:21 +00001279 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001280 case Type::TypeOfExpr:
1281 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001282 case Type::Decltype:
1283 llvm_unreachable("type should have been unwrapped!");
1284 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001285
1286 case Type::RValueReference:
1287 // FIXME: Implement!
1288 Diag = "rvalue references";
1289 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001290 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001291
1292 assert(Diag && "Fall through without a diagnostic?");
1293 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1294 "debug information for %0 is not yet supported");
1295 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1296 << Diag;
1297 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001298}
1299
1300/// EmitFunctionStart - Constructs the debug code for entering a function -
1301/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001302void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001303 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001304 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001305
Devang Patel934661e2010-01-14 00:36:21 +00001306 llvm::StringRef Name;
1307 llvm::StringRef LinkageName;
1308
1309 const Decl *D = GD.getDecl();
1310 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001311 // If there is a DISubprogram for this function available then use it.
1312 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1313 FI = SPCache.find(FD);
1314 if (FI != SPCache.end()) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001315 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1316 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001317 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001318 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel7a12ad02010-01-19 01:54:44 +00001319 return;
1320 }
1321 }
Devang Patel934661e2010-01-14 00:36:21 +00001322 Name = getFunctionName(FD);
Eli Friedman8fdc2cb2010-01-16 00:43:13 +00001323 if (!Name.empty() && Name[0] == '\01')
Devang Patel2d630102010-01-14 21:46:57 +00001324 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001325 // Use mangled name as linkage name for c/c++ functions.
Devang Patel2d630102010-01-14 21:46:57 +00001326 LinkageName = CGM.getMangledName(GD);
Devang Patel934661e2010-01-14 00:36:21 +00001327 } else {
1328 // Use llvm function name as linkage name.
1329 Name = Fn->getName();
Devang Patel934661e2010-01-14 00:36:21 +00001330 LinkageName = Name;
Devang Patel757daca2010-01-19 00:25:12 +00001331 if (!Name.empty() && Name[0] == '\01')
1332 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Devang Patelea84f592010-01-14 18:06:13 +00001335 // It is expected that CurLoc is set before using EmitFunctionStart.
1336 // Usually, CurLoc points to the left bracket location of compound
1337 // statement representing function body.
Devang Patel408dcf62010-03-09 00:44:50 +00001338 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001339 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel2ac33a02009-04-08 19:47:04 +00001340 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattneraffb3732008-11-10 06:08:34 +00001342 llvm::DISubprogram SP =
Devang Patel9be7b202009-07-14 21:31:22 +00001343 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001344 getOrCreateType(FnType, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001345 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump11289f42009-09-09 15:08:12 +00001346
Sanjiv Gupta98070572008-05-25 05:15:42 +00001347 // Push function on region stack.
Devang Patelb40f2952009-11-13 19:10:24 +00001348 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001349 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta98070572008-05-25 05:15:42 +00001350}
1351
1352
Chris Lattneraffb3732008-11-10 06:08:34 +00001353void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001354 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001355
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001356 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001357 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001358 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001359 || (SM.getInstantiationLineNumber(CurLoc) ==
1360 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001361 && SM.isFromSameFile(CurLoc, PrevLoc)))
1362 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001363
1364 // Update last state.
1365 PrevLoc = CurLoc;
1366
1367 // Get the appropriate compile unit.
Devang Patel408dcf62010-03-09 00:44:50 +00001368 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel2ac33a02009-04-08 19:47:04 +00001369 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel5d90d622009-10-06 18:36:08 +00001370
Devang Patelb40f2952009-11-13 19:10:24 +00001371 llvm::DIDescriptor DR(RegionStack.back());
Devang Patel5d90d622009-10-06 18:36:08 +00001372 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1373 llvm::DILocation DO(NULL);
1374 llvm::DILocation DL =
1375 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1376 DS, DO);
1377 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001378}
1379
1380/// EmitRegionStart- Constructs the debug code for entering a declarative
1381/// region - "llvm.dbg.region.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +00001382void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel75855802010-02-16 21:41:20 +00001383 SourceManager &SM = CGM.getContext().getSourceManager();
1384 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelb40f2952009-11-13 19:10:24 +00001385 llvm::DIDescriptor D =
1386 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1387 llvm::DIDescriptor() :
Devang Patel75855802010-02-16 21:41:20 +00001388 llvm::DIDescriptor(RegionStack.back()),
1389 PLoc.getLine(), PLoc.getColumn());
Devang Patelb40f2952009-11-13 19:10:24 +00001390 RegionStack.push_back(D.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001391}
1392
1393/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1394/// region - "llvm.dbg.region.end."
Chris Lattneraffb3732008-11-10 06:08:34 +00001395void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001396 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1397
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001398 // Provide an region stop point.
1399 EmitStopPoint(Fn, Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001400
Sanjiv Gupta98070572008-05-25 05:15:42 +00001401 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001402}
1403
Devang Patel535fdaf2010-02-10 18:49:08 +00001404// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1405// See BuildByRefType.
1406llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1407 uint64_t *XOffset) {
1408
1409 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1410
1411 QualType FType;
1412 uint64_t FieldSize, FieldOffset;
1413 unsigned FieldAlign;
1414
Devang Patel408dcf62010-03-09 00:44:50 +00001415 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001416 QualType Type = VD->getType();
1417
1418 FieldOffset = 0;
1419 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1420 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1421 FieldSize = CGM.getContext().getTypeSize(FType);
1422 FieldAlign = CGM.getContext().getTypeAlign(FType);
1423 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001424 "__isa", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001425 0, FieldSize, FieldAlign,
1426 FieldOffset, 0, FieldTy);
1427 EltTys.push_back(FieldTy);
1428 FieldOffset += FieldSize;
1429
1430 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1431 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1432 FieldSize = CGM.getContext().getTypeSize(FType);
1433 FieldAlign = CGM.getContext().getTypeAlign(FType);
1434 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001435 "__forwarding", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001436 0, FieldSize, FieldAlign,
1437 FieldOffset, 0, FieldTy);
1438 EltTys.push_back(FieldTy);
1439 FieldOffset += FieldSize;
1440
1441 FType = CGM.getContext().IntTy;
1442 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1443 FieldSize = CGM.getContext().getTypeSize(FType);
1444 FieldAlign = CGM.getContext().getTypeAlign(FType);
1445 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001446 "__flags", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001447 0, FieldSize, FieldAlign,
1448 FieldOffset, 0, FieldTy);
1449 EltTys.push_back(FieldTy);
1450 FieldOffset += FieldSize;
1451
1452 FType = CGM.getContext().IntTy;
1453 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1454 FieldSize = CGM.getContext().getTypeSize(FType);
1455 FieldAlign = CGM.getContext().getTypeAlign(FType);
1456 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001457 "__size", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001458 0, FieldSize, FieldAlign,
1459 FieldOffset, 0, FieldTy);
1460 EltTys.push_back(FieldTy);
1461 FieldOffset += FieldSize;
1462
1463 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1464 if (HasCopyAndDispose) {
1465 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1466 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1467 FieldSize = CGM.getContext().getTypeSize(FType);
1468 FieldAlign = CGM.getContext().getTypeAlign(FType);
1469 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001470 "__copy_helper", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001471 0, FieldSize, FieldAlign,
1472 FieldOffset, 0, FieldTy);
1473 EltTys.push_back(FieldTy);
1474 FieldOffset += FieldSize;
1475
1476 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1477 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1478 FieldSize = CGM.getContext().getTypeSize(FType);
1479 FieldAlign = CGM.getContext().getTypeAlign(FType);
1480 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001481 "__destroy_helper", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001482 0, FieldSize, FieldAlign,
1483 FieldOffset, 0, FieldTy);
1484 EltTys.push_back(FieldTy);
1485 FieldOffset += FieldSize;
1486 }
1487
1488 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1489 if (Align > CharUnits::fromQuantity(
1490 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1491 unsigned AlignedOffsetInBytes
1492 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1493 unsigned NumPaddingBytes
1494 = AlignedOffsetInBytes - FieldOffset/8;
1495
1496 if (NumPaddingBytes > 0) {
1497 llvm::APInt pad(32, NumPaddingBytes);
1498 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1499 pad, ArrayType::Normal, 0);
1500 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1501 FieldSize = CGM.getContext().getTypeSize(FType);
1502 FieldAlign = CGM.getContext().getTypeAlign(FType);
1503 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
Devang Patel93f274c2010-03-09 22:49:11 +00001504 Unit, "", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001505 0, FieldSize, FieldAlign,
1506 FieldOffset, 0, FieldTy);
1507 EltTys.push_back(FieldTy);
1508 FieldOffset += FieldSize;
1509 }
1510 }
1511
1512 FType = Type;
1513 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1514 FieldSize = CGM.getContext().getTypeSize(FType);
1515 FieldAlign = Align.getQuantity()*8;
1516
1517 *XOffset = FieldOffset;
1518 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001519 VD->getName(), Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001520 0, FieldSize, FieldAlign,
1521 FieldOffset, 0, FieldTy);
1522 EltTys.push_back(FieldTy);
1523 FieldOffset += FieldSize;
1524
1525 llvm::DIArray Elements =
1526 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1527
1528 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1529
1530 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001531 Unit, "", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001532 0, FieldOffset, 0, 0, Flags,
1533 llvm::DIType(), Elements);
1534
1535}
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001536/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001537void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattneraffb3732008-11-10 06:08:34 +00001538 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001539 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1540
Devang Patelafc1c1d2009-03-27 23:16:32 +00001541 // Do not emit variable debug information while generating optimized code.
1542 // The llvm optimizer and code generator are not yet ready to support
1543 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001544 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001545 if (CGO.OptimizationLevel)
Devang Patelafc1c1d2009-03-27 23:16:32 +00001546 return;
1547
Devang Patel408dcf62010-03-09 00:44:50 +00001548 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001549 llvm::DIType Ty;
1550 uint64_t XOffset = 0;
1551 if (VD->hasAttr<BlocksAttr>())
1552 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1553 else
1554 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00001555
Chris Lattneraffb3732008-11-10 06:08:34 +00001556 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001557 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel1c0954c2010-02-01 21:39:52 +00001558 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner362d8ae2009-05-05 04:57:08 +00001559 unsigned Line = 0;
Eli Friedmanb05d0822009-11-16 20:33:31 +00001560 unsigned Column = 0;
Devang Patel1b5330a2010-02-10 01:09:50 +00001561 if (PLoc.isInvalid())
1562 PLoc = SM.getPresumedLoc(CurLoc);
1563 if (PLoc.isValid()) {
Chris Lattner362d8ae2009-05-05 04:57:08 +00001564 Line = PLoc.getLine();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001565 Column = PLoc.getColumn();
Devang Patel408dcf62010-03-09 00:44:50 +00001566 Unit = getOrCreateFile(CurLoc);
Eli Friedmanb05d0822009-11-16 20:33:31 +00001567 } else {
Devang Patel408dcf62010-03-09 00:44:50 +00001568 Unit = llvm::DIFile();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001569 }
Mike Stump11289f42009-09-09 15:08:12 +00001570
Chris Lattneraffb3732008-11-10 06:08:34 +00001571 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001572 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001573 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel1c0954c2010-02-01 21:39:52 +00001574 VD->getName(),
Chris Lattner362d8ae2009-05-05 04:57:08 +00001575 Unit, Line, Ty);
Chris Lattneraffb3732008-11-10 06:08:34 +00001576 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001577 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001578 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001579
Devang Patelb40f2952009-11-13 19:10:24 +00001580 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001581 llvm::DILocation DO(NULL);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001582 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1583
Chris Lattner9f021fd2009-12-28 23:41:39 +00001584 Call->setMetadata("dbg", DL.getNode());
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001585}
1586
Mike Stump2e722b92009-09-30 02:43:10 +00001587/// EmitDeclare - Emit local variable declaration debug info.
1588void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1589 llvm::Value *Storage, CGBuilderTy &Builder,
1590 CodeGenFunction *CGF) {
Devang Patel3efd1472010-02-01 21:52:22 +00001591 const ValueDecl *VD = BDRE->getDecl();
Mike Stump2e722b92009-09-30 02:43:10 +00001592 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1593
1594 // Do not emit variable debug information while generating optimized code.
1595 // The llvm optimizer and code generator are not yet ready to support
1596 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001597 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001598 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001599 return;
1600
1601 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00001602 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001603 llvm::DIType Ty;
1604 if (VD->hasAttr<BlocksAttr>())
1605 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1606 else
1607 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00001608
1609 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001610 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel3efd1472010-02-01 21:52:22 +00001611 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00001612 unsigned Line = 0;
1613 if (!PLoc.isInvalid())
1614 Line = PLoc.getLine();
1615 else
Devang Patel408dcf62010-03-09 00:44:50 +00001616 Unit = llvm::DIFile();
Mike Stump2e722b92009-09-30 02:43:10 +00001617
Devang Patel3efd1472010-02-01 21:52:22 +00001618 CharUnits offset = CGF->BlockDecls[VD];
Mike Stump2e722b92009-09-30 02:43:10 +00001619 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001620 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1621 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1622 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1623 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001624 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001625 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1626 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001627 // offset of __forwarding field
1628 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001629 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1630 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1631 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001632 // offset of x field
1633 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001634 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001635 }
1636
1637 // Create the descriptor for the variable.
1638 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001639 DebugFactory.CreateComplexVariable(Tag,
1640 llvm::DIDescriptor(RegionStack.back()),
Devang Patel3efd1472010-02-01 21:52:22 +00001641 VD->getName(), Unit, Line, Ty,
Mike Stump2e722b92009-09-30 02:43:10 +00001642 addr);
1643 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001644 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001645 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001646
Devang Patelb40f2952009-11-13 19:10:24 +00001647 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001648 llvm::DILocation DO(NULL);
1649 llvm::DILocation DL =
1650 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001651
Chris Lattner9f021fd2009-12-28 23:41:39 +00001652 Call->setMetadata("dbg", DL.getNode());
Mike Stump2e722b92009-09-30 02:43:10 +00001653}
1654
Devang Patel3efd1472010-02-01 21:52:22 +00001655void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattneraffb3732008-11-10 06:08:34 +00001656 llvm::Value *Storage,
1657 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001658 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001659}
1660
Mike Stump2e722b92009-09-30 02:43:10 +00001661void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1662 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1663 CodeGenFunction *CGF) {
1664 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1665}
1666
Chris Lattneraffb3732008-11-10 06:08:34 +00001667/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1668/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00001669void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattneraffb3732008-11-10 06:08:34 +00001670 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001671 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001672}
1673
1674
1675
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001676/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001677void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00001678 const VarDecl *D) {
1679
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001680 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001681 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001682 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001683 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +00001684 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner86d7d912008-11-24 03:54:41 +00001685
Devang Patel7b7f46f2010-02-01 21:34:11 +00001686 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001687 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001688
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001689 // CodeGen turns int[] into int[1] so we'll do the same here.
1690 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001691
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001692 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001693 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001694
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001695 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001696 ArrayType::Normal, 0);
1697 }
Sanjiv Gupta9ac30922010-02-25 05:20:44 +00001698 llvm::StringRef DeclName = Var->getName();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001699 llvm::DIDescriptor DContext =
1700 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1701 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel7bfc5962010-01-28 23:15:27 +00001702 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001703 getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001704 Var->hasInternalLinkage(),
1705 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001706}
1707
Devang Patelf4c205b2009-02-26 21:10:26 +00001708/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001709void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00001710 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001711 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001712 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001713 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel3efd1472010-02-01 21:52:22 +00001714 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +00001715 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patelf4c205b2009-02-26 21:10:26 +00001716
Devang Patel3efd1472010-02-01 21:52:22 +00001717 llvm::StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001718
Devang Patel3efd1472010-02-01 21:52:22 +00001719 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001720 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001721
Devang Patelf4c205b2009-02-26 21:10:26 +00001722 // CodeGen turns int[] into int[1] so we'll do the same here.
1723 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001724
Devang Patelf4c205b2009-02-26 21:10:26 +00001725 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001726 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001727
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001728 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001729 ArrayType::Normal, 0);
1730 }
1731
Devang Patele4f2b2a2009-10-20 18:26:30 +00001732 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001733 getOrCreateType(T, Unit),
1734 Var->hasInternalLinkage(),
1735 true/*definition*/, Var);
1736}
Devang Patel973f2eb2010-02-01 19:16:32 +00001737
1738/// getOrCreateNamesSpace - Return namespace descriptor for the given
1739/// namespace decl.
1740llvm::DINameSpace
1741CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1742 llvm::DIDescriptor Unit) {
1743 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1744 NameSpaceCache.find(NSDecl);
1745 if (I != NameSpaceCache.end())
1746 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1747
1748 SourceManager &SM = CGM.getContext().getSourceManager();
1749 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1750 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1751
1752 llvm::DIDescriptor Context =
Devang Patel7b7f46f2010-02-01 21:34:11 +00001753 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patel973f2eb2010-02-01 19:16:32 +00001754 llvm::DINameSpace NS =
1755 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel408dcf62010-03-09 00:44:50 +00001756 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patel973f2eb2010-02-01 19:16:32 +00001757 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1758 return NS;
1759}