blob: c469e9c3b9e999d7ad44f9a4866ee05a03f48bf0 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051}
52
Devang Patel33583052010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000056 if (!Context)
57 return CompileUnit;
58
59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
60 I = RegionMap.find(Context);
61 if (I != RegionMap.end())
62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000063
Devang Pateleb6d79b2010-02-01 21:34:11 +000064 // Check namespace.
65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
67
Devang Patel979ec2e2009-10-06 00:35:31 +000068 return CompileUnit;
69}
70
Devang Patel9c6c3a02010-01-14 00:36:21 +000071/// getFunctionName - Get function name for the given FunctionDecl. If the
72/// name is constructred on demand (e.g. C++ destructor) then the name
73/// is stored on the side.
74llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
75 assert (FD && "Invalid FunctionDecl!");
76 IdentifierInfo *FII = FD->getIdentifier();
77 if (FII)
78 return FII->getName();
79
80 // Otherwise construct human readable name for debug info.
81 std::string NS = FD->getNameAsString();
82
83 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000084 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000085 memcpy(StrPtr, NS.data(), NS.length());
86 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000087}
88
Devang Patel17800552010-03-09 00:44:50 +000089/// getOrCreateFile - Get the file debug info descriptor for the input location.
90llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
91 if (!Loc.isValid())
92 // If Location is not valid then use main input file.
93 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
94 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +000095 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +000096 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
97 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +000098 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +000099
Devang Patel683f6db2010-03-09 19:14:07 +0000100 return DebugFactory.CreateFile(AbsFileName.getLast(),
Devang Patel17800552010-03-09 00:44:50 +0000101 AbsFileName.getDirname(), TheCU);
102}
103/// CreateCompileUnit - Create new compile unit.
104void CGDebugInfo::CreateCompileUnit() {
105
106 // Get absolute path name.
107 llvm::sys::Path AbsFileName(CGM.getCodeGenOpts().MainFileName);
108 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000109
Chris Lattner515455a2009-03-25 03:28:08 +0000110 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000111 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000112 if (LO.CPlusPlus) {
113 if (LO.ObjC1)
114 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
115 else
116 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
117 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000118 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000119 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000120 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000121 } else {
122 LangTag = llvm::dwarf::DW_LANG_C89;
123 }
Devang Patel446c6192009-04-17 21:06:59 +0000124
Benjamin Kramer47daf682009-12-08 11:02:29 +0000125 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000126#ifdef CLANG_VENDOR
127 CLANG_VENDOR
128#endif
129 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000130
131 // Figure out which version of the ObjC runtime we have.
132 unsigned RuntimeVers = 0;
133 if (LO.ObjC1)
134 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000136 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000137 TheCU = DebugFactory.CreateCompileUnit(
138 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000139 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000140}
141
Devang Patel65e99f22009-02-25 01:36:11 +0000142/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000143/// one if necessary.
144llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000145 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000146 unsigned Encoding = 0;
147 switch (BT->getKind()) {
148 default:
149 case BuiltinType::Void:
150 return llvm::DIType();
151 case BuiltinType::UChar:
152 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
153 case BuiltinType::Char_S:
154 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
155 case BuiltinType::UShort:
156 case BuiltinType::UInt:
157 case BuiltinType::ULong:
158 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
159 case BuiltinType::Short:
160 case BuiltinType::Int:
161 case BuiltinType::Long:
162 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
163 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
164 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000165 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000166 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000167 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000168 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000169 uint64_t Size = CGM.getContext().getTypeSize(BT);
170 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000171 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Devang Patelca80a5f2009-10-20 19:55:01 +0000173 llvm::DIType DbgTy =
174 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000175 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000176 Unit, 0, Size, Align,
177 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000178 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000179}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000180
Chris Lattnerb7003772009-04-23 06:13:01 +0000181llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000182 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000183 // Bit size, align and offset of the type.
184 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
185 if (Ty->isComplexIntegerType())
186 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlsson20f12a22009-12-06 18:00:51 +0000188 uint64_t Size = CGM.getContext().getTypeSize(Ty);
189 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000190 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Devang Patelca80a5f2009-10-20 19:55:01 +0000192 llvm::DIType DbgTy =
193 DebugFactory.CreateBasicType(Unit, "complex",
194 Unit, 0, Size, Align,
195 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000196 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000197}
198
John McCalla1805292009-09-25 01:40:47 +0000199/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000200/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000201llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000202 QualifierCollector Qc;
203 const Type *T = Qc.strip(Ty);
204
205 // Ignore these qualifiers for now.
206 Qc.removeObjCGCAttr();
207 Qc.removeAddressSpace();
208
Chris Lattner9c85ba32008-11-10 06:08:34 +0000209 // We will create one Derived type for one qualifier and recurse to handle any
210 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000211 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000212 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000213 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000214 Qc.removeConst();
215 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000216 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000217 Qc.removeVolatile();
218 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000219 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000220 Qc.removeRestrict();
221 } else {
222 assert(Qc.empty() && "Unknown type qualifier for debug info");
223 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
John McCalla1805292009-09-25 01:40:47 +0000226 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
227
Daniel Dunbar3845f862008-10-31 03:54:29 +0000228 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
229 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000230 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000231 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000232 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000233 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000234}
235
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000236llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000237 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000238 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000239 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
240 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000241 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000242}
243
Chris Lattner9c85ba32008-11-10 06:08:34 +0000244llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000245 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000246 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
247 Ty->getPointeeType(), Unit);
248}
249
250llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
251 const Type *Ty,
252 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000253 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000254 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000256 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000257
258 // Size is always the size of a pointer. We can't use getTypeSize here
259 // because that does not return the correct value for references.
260 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000261 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
262 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Devang Patelca80a5f2009-10-20 19:55:01 +0000264 return
Devang Pateld58562e2010-03-09 22:49:11 +0000265 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000266 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000267
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000268}
269
Mike Stump9bc093c2009-05-14 02:03:51 +0000270llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000271 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000272 if (BlockLiteralGenericSet)
273 return BlockLiteralGeneric;
274
Mike Stump9bc093c2009-05-14 02:03:51 +0000275 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
276
277 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
278
279 llvm::DIType FieldTy;
280
281 QualType FType;
282 uint64_t FieldSize, FieldOffset;
283 unsigned FieldAlign;
284
285 llvm::DIArray Elements;
286 llvm::DIType EltTy, DescTy;
287
288 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000289 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000290 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000291 FieldSize = CGM.getContext().getTypeSize(FType);
292 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000293 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000294 "reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000295 0, FieldSize, FieldAlign,
296 FieldOffset, 0, FieldTy);
297 EltTys.push_back(FieldTy);
298
299 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000300 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000301 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000302 FieldSize = CGM.getContext().getTypeSize(FType);
303 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000304 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000305 "Size", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000306 0, FieldSize, FieldAlign,
307 FieldOffset, 0, FieldTy);
308 EltTys.push_back(FieldTy);
309
310 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000311 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000312 EltTys.clear();
313
Mike Stump3d363c52009-10-02 02:30:50 +0000314 unsigned Flags = llvm::DIType::FlagAppleBlock;
315
Mike Stump9bc093c2009-05-14 02:03:51 +0000316 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Pateld58562e2010-03-09 22:49:11 +0000317 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000318 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Mike Stump9bc093c2009-05-14 02:03:51 +0000320 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000321 uint64_t Size = CGM.getContext().getTypeSize(Ty);
322 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Mike Stump9bc093c2009-05-14 02:03:51 +0000324 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000325 Unit, "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000326 0, Size, Align, 0, 0, EltTy);
327
328 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000329 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000330 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000331 FieldSize = CGM.getContext().getTypeSize(FType);
332 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000334 "__isa", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000335 0, FieldSize, FieldAlign,
336 FieldOffset, 0, FieldTy);
337 EltTys.push_back(FieldTy);
338
339 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000340 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000341 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000342 FieldSize = CGM.getContext().getTypeSize(FType);
343 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000344 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000345 "__flags", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000346 0, FieldSize, FieldAlign,
347 FieldOffset, 0, FieldTy);
348 EltTys.push_back(FieldTy);
349
350 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000351 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000352 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000353 FieldSize = CGM.getContext().getTypeSize(FType);
354 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000355 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000356 "__reserved", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 0, FieldSize, FieldAlign,
358 FieldOffset, 0, FieldTy);
359 EltTys.push_back(FieldTy);
360
361 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000362 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000363 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000364 FieldSize = CGM.getContext().getTypeSize(FType);
365 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000367 "__FuncPtr", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000368 0, FieldSize, FieldAlign,
369 FieldOffset, 0, FieldTy);
370 EltTys.push_back(FieldTy);
371
372 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000373 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000374 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000375 FieldSize = CGM.getContext().getTypeSize(Ty);
376 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000377 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000378 "__descriptor", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 0, FieldSize, FieldAlign,
380 FieldOffset, 0, FieldTy);
381 EltTys.push_back(FieldTy);
382
383 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000384 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000385
386 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Pateld58562e2010-03-09 22:49:11 +0000387 Unit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000388 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 BlockLiteralGenericSet = true;
391 BlockLiteralGeneric
392 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000393 "", Unit,
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 0, Size, Align, 0, 0, EltTy);
395 return BlockLiteralGeneric;
396}
397
Chris Lattner9c85ba32008-11-10 06:08:34 +0000398llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000399 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000400 // Typedefs are derived from some other type. If we have a typedef of a
401 // typedef, make sure to emit the whole chain.
402 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404 // We don't set size information, but do specify where the typedef was
405 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000406 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000407 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000408 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409
Devang Pateleb6d79b2010-02-01 21:34:11 +0000410 llvm::DIDescriptor TyContext
411 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
412 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000413 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000414 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000415 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000416 Ty->getDecl()->getName(), Unit,
417 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000418 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000419}
420
Chris Lattner9c85ba32008-11-10 06:08:34 +0000421llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000422 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000423 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425 // Add the result type at least.
426 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner9c85ba32008-11-10 06:08:34 +0000428 // Set up remainder of arguments if there is a prototype.
429 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000430 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
432 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
433 } else {
434 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000435 }
436
Chris Lattner9c85ba32008-11-10 06:08:34 +0000437 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000438 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Devang Patelca80a5f2009-10-20 19:55:01 +0000440 llvm::DIType DbgTy =
441 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000442 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000443 0, 0, 0, 0, 0,
444 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000445 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000446}
447
Devang Patel428deb52010-01-19 00:00:59 +0000448/// CollectRecordFields - A helper function to collect debug info for
449/// record fields. This is used while creating debug info entry for a Record.
450void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000451CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000452 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
453 unsigned FieldNo = 0;
454 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000455 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
456 for (RecordDecl::field_iterator I = RD->field_begin(),
457 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000458 I != E; ++I, ++FieldNo) {
459 FieldDecl *Field = *I;
460 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
461
462 llvm::StringRef FieldName = Field->getName();
463
Devang Patel4835fdd2010-02-12 01:31:06 +0000464 // Ignore unnamed fields. Do not ignore unnamed records.
465 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000466 continue;
467
468 // Get the location for the field.
469 SourceLocation FieldDefLoc = Field->getLocation();
470 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000471 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000472 unsigned FieldLine = 0;
473
474 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000475 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000476 FieldLine = PLoc.getLine();
477 }
478
479 QualType FType = Field->getType();
480 uint64_t FieldSize = 0;
481 unsigned FieldAlign = 0;
482 if (!FType->isIncompleteArrayType()) {
483
484 // Bit size, align and offset of the type.
485 FieldSize = CGM.getContext().getTypeSize(FType);
486 Expr *BitWidth = Field->getBitWidth();
487 if (BitWidth)
488 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
489
490 FieldAlign = CGM.getContext().getTypeAlign(FType);
491 }
492
493 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
494
495 // Create a DW_TAG_member node to remember the offset of this field in the
496 // struct. FIXME: This is an absolutely insane way to capture this
497 // information. When we gut debug info, this should be fixed.
498 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
499 FieldName, FieldDefUnit,
500 FieldLine, FieldSize, FieldAlign,
501 FieldOffset, 0, FieldTy);
502 EltTys.push_back(FieldTy);
503 }
504}
505
Devang Patela6da1922010-01-28 00:28:01 +0000506/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
507/// function type is not updated to include implicit "this" pointer. Use this
508/// routine to get a method type which includes "this" pointer.
509llvm::DIType
510CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000511 llvm::DIFile Unit) {
Devang Patela6da1922010-01-28 00:28:01 +0000512 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000513
514 // Static methods do not need "this" pointer argument.
515 if (Method->isStatic())
516 return FnTy;
517
Devang Patela6da1922010-01-28 00:28:01 +0000518 // Add "this" pointer.
519
520 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
521 assert (Args.getNumElements() && "Invalid number of arguments!");
522
523 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
524
525 // First element is always return type. For 'void' functions it is NULL.
526 Elts.push_back(Args.getElement(0));
527
528 // "this" pointer is always first argument.
529 ASTContext &Context = CGM.getContext();
530 QualType ThisPtr =
531 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000532 llvm::DIType ThisPtrType =
533 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
534 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
535 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000536
537 // Copy rest of the arguments.
538 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
539 Elts.push_back(Args.getElement(i));
540
541 llvm::DIArray EltTypeArray =
542 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
543
544 return
545 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000546 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000547 0, 0, 0, 0, 0,
548 llvm::DIType(), EltTypeArray);
549}
550
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000551/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
552/// a single member function GlobalDecl.
553llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000554CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000555 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000556 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000557 bool IsCtorOrDtor =
558 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
559
560 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000561 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000562 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000563
564 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
565 // make sense to give a single ctor/dtor a linkage name.
566 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000567 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000568
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000569 SourceManager &SM = CGM.getContext().getSourceManager();
570
571 // Get the location for the method.
572 SourceLocation MethodDefLoc = Method->getLocation();
573 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000574 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000575 unsigned MethodLine = 0;
576
577 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000578 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000579 MethodLine = PLoc.getLine();
580 }
581
582 // Collect virtual method info.
583 llvm::DIType ContainingType;
584 unsigned Virtuality = 0;
585 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000586
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000587 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000588 if (Method->isPure())
589 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
590 else
591 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
592
593 // It doesn't make sense to give a virtual destructor a vtable index,
594 // since a single destructor has two entries in the vtable.
595 if (!isa<CXXDestructorDecl>(Method))
596 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000597 ContainingType = RecordTy;
598 }
599
600 llvm::DISubprogram SP =
601 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
602 MethodLinkageName,
603 MethodDefUnit, MethodLine,
604 MethodTy, /*isLocalToUnit=*/false,
605 Method->isThisDeclarationADefinition(),
606 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000607
608 // Don't cache ctors or dtors since we have to emit multiple functions for
609 // a single ctor or dtor.
610 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
611 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000612
613 return SP;
614}
615
Devang Patel4125fd22010-01-19 01:54:44 +0000616/// CollectCXXMemberFunctions - A helper function to collect debug info for
617/// C++ member functions.This is used while creating debug info entry for
618/// a Record.
619void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000620CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000621 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
622 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000623 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
624 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000625 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000626
Devang Pateld5322da2010-02-09 19:09:28 +0000627 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000628 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000629
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000630 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000631 }
632}
633
Devang Patela245c5b2010-01-25 23:32:18 +0000634/// CollectCXXBases - A helper function to collect debug info for
635/// C++ base classes. This is used while creating debug info entry for
636/// a Record.
637void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000638CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000639 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
640 llvm::DICompositeType &RecordTy) {
641
Devang Patel239cec62010-02-01 21:39:52 +0000642 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
643 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
644 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000645 unsigned BFlags = 0;
646 uint64_t BaseOffset;
647
648 const CXXRecordDecl *Base =
649 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
650
651 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000652 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000653 // expression where it expects +ve number.
Anders Carlssonbba16072010-03-11 07:15:17 +0000654 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000655 BFlags = llvm::DIType::FlagVirtual;
656 } else
657 BaseOffset = RL.getBaseClassOffset(Base);
658
659 AccessSpecifier Access = BI->getAccessSpecifier();
660 if (Access == clang::AS_private)
661 BFlags |= llvm::DIType::FlagPrivate;
662 else if (Access == clang::AS_protected)
663 BFlags |= llvm::DIType::FlagProtected;
664
665 llvm::DIType DTy =
666 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
667 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000668 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000669 BaseOffset, BFlags,
670 getOrCreateType(BI->getType(),
671 Unit));
672 EltTys.push_back(DTy);
673 }
Devang Patela245c5b2010-01-25 23:32:18 +0000674}
675
Devang Patel4ce3f202010-01-28 18:11:52 +0000676/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000677llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000678 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000679 return VTablePtrType;
680
681 ASTContext &Context = CGM.getContext();
682
683 /* Function type */
684 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
685 STys.push_back(getOrCreateType(Context.IntTy, Unit));
686 llvm::DIArray SElements =
687 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
688 llvm::DIType SubTy =
689 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000690 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000691 0, 0, 0, 0, 0, llvm::DIType(), SElements);
692
693 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
694 llvm::DIType vtbl_ptr_type
695 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000696 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000697 0, Size, 0, 0, 0, SubTy);
698
Devang Pateld58562e2010-03-09 22:49:11 +0000699 VTablePtrType =
700 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
701 Unit, "", Unit,
702 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000703 return VTablePtrType;
704}
705
706/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000707llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000708 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000709 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000710
711 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000712 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000713 memcpy(StrPtr, Name.data(), Name.length());
714 return llvm::StringRef(StrPtr, Name.length());
715}
716
717
718/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
719/// debug info entry in EltTys vector.
720void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000721CollectVtableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000722 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000723 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000724
725 // If there is a primary base then it will hold vtable info.
726 if (RL.getPrimaryBase())
727 return;
728
729 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000730 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000731 return;
732
733 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
734 llvm::DIType VPTR
735 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000736 getVtableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000737 0, Size, 0, 0, 0,
738 getOrCreateVTablePtrType(Unit));
739 EltTys.push_back(VPTR);
740}
741
Devang Patel65e99f22009-02-25 01:36:11 +0000742/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000744 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000745 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattner9c85ba32008-11-10 06:08:34 +0000747 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000748 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000749 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000750 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751 Tag = llvm::dwarf::DW_TAG_union_type;
752 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000753 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000754 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000755 }
756
Anders Carlsson20f12a22009-12-06 18:00:51 +0000757 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000758
Chris Lattner9c85ba32008-11-10 06:08:34 +0000759 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000760 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000761 llvm::DIFile DefUnit;
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000762 unsigned Line = 0;
763 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000764 DefUnit = getOrCreateFile(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000765 Line = PLoc.getLine();
766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattner9c85ba32008-11-10 06:08:34 +0000768 // Records and classes and unions can all be recursive. To handle them, we
769 // first generate a debug descriptor for the struct as a forward declaration.
770 // Then (if it is a definition) we go through and get debug info for all of
771 // its members. Finally, we create a descriptor for the complete type (which
772 // may refer to the forward decl if the struct is recursive) and replace all
773 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000774
Devang Pateld6c5a262010-02-01 21:52:22 +0000775 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000776 // are uniqued so use type name to ensure uniquness.
Devang Patelf3383702010-03-10 00:19:43 +0000777 llvm::SmallString<256> FwdDeclName;
778 FwdDeclName.resize(256);
779 sprintf(&FwdDeclName[0], "fwd.type.%d", FwdDeclCount++);
Devang Patel411894b2010-02-01 22:40:08 +0000780 llvm::DIDescriptor FDContext =
781 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000782 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000783 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000784 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattner9c85ba32008-11-10 06:08:34 +0000787 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000788 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000789 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000790
Eli Friedman14d63652009-11-16 21:04:30 +0000791 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 // Otherwise, insert it into the TypeCache so that recursive uses will find
793 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000794 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795
796 // Convert all the elements.
797 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
798
Devang Pateld6c5a262010-02-01 21:52:22 +0000799 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000800 if (CXXDecl) {
801 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000802 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000803 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000804 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000805 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000806 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000807 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000808
809 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000810 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000811 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
812 ContainingType =
813 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
814 else if (CXXDecl->isDynamicClass())
815 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattner9c85ba32008-11-10 06:08:34 +0000818 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000819 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000820
821 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000822 uint64_t Size = CGM.getContext().getTypeSize(Ty);
823 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Devang Patel411894b2010-02-01 22:40:08 +0000825 llvm::DIDescriptor RDContext =
826 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000827 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000828 DebugFactory.CreateCompositeType(Tag, RDContext,
829 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000830 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000831 llvm::DIType(), Elements,
832 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833
834 // Now that we have a real decl for the struct, replace anything using the
835 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000836 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000837
Chris Lattner9c85ba32008-11-10 06:08:34 +0000838 return RealDecl;
839}
840
Devang Patel9ca36b62009-02-26 21:10:26 +0000841/// CreateType - get objective-c interface type.
842llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000843 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000844 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Devang Patel9ca36b62009-02-26 21:10:26 +0000846 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000847 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000848
849 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000850 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000851 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000852 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Devang Patel17800552010-03-09 00:44:50 +0000855 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000856
Devang Patel9ca36b62009-02-26 21:10:26 +0000857 // To handle recursive interface, we
858 // first generate a debug descriptor for the struct as a forward declaration.
859 // Then (if it is a definition) we go through and get debug info for all of
860 // its members. Finally, we create a descriptor for the complete type (which
861 // may refer to the forward decl if the struct is recursive) and replace all
862 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000863 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000864 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000865 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000866 llvm::DIType(), llvm::DIArray(),
867 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Devang Patel9ca36b62009-02-26 21:10:26 +0000869 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000870 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000871 return FwdDecl;
872
Devang Patelffffb032009-11-16 20:09:38 +0000873 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000874 // Otherwise, insert it into the TypeCache so that recursive uses will find
875 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000876 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000877
878 // Convert all the elements.
879 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
880
Devang Pateld6c5a262010-02-01 21:52:22 +0000881 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000882 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000883 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000884 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000886 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000887 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000888 0 /* offset */, 0, SClassTy);
889 EltTys.push_back(InhTag);
890 }
891
Devang Pateld6c5a262010-02-01 21:52:22 +0000892 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000893
894 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000895 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
896 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000897 ObjCIvarDecl *Field = *I;
898 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
899
Devang Patel73621622009-11-25 17:37:31 +0000900 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000901
Devang Patelde135022009-04-27 22:40:36 +0000902 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000903 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000904 continue;
905
Devang Patel9ca36b62009-02-26 21:10:26 +0000906 // Get the location for the field.
907 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000908 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000909 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
910 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
911
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Devang Patel99c20eb2009-03-20 18:24:39 +0000913 QualType FType = Field->getType();
914 uint64_t FieldSize = 0;
915 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000916
Devang Patel99c20eb2009-03-20 18:24:39 +0000917 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Devang Patel99c20eb2009-03-20 18:24:39 +0000919 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000920 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000921 Expr *BitWidth = Field->getBitWidth();
922 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000923 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000924
Anders Carlsson20f12a22009-12-06 18:00:51 +0000925 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000926 }
927
Mike Stump1eb44332009-09-09 15:08:12 +0000928 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
929
Devang Patelc20482b2009-03-19 00:23:53 +0000930 unsigned Flags = 0;
931 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
932 Flags = llvm::DIType::FlagProtected;
933 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
934 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Devang Patel9ca36b62009-02-26 21:10:26 +0000936 // Create a DW_TAG_member node to remember the offset of this field in the
937 // struct. FIXME: This is an absolutely insane way to capture this
938 // information. When we gut debug info, this should be fixed.
939 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
940 FieldName, FieldDefUnit,
941 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000942 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000943 EltTys.push_back(FieldTy);
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Devang Patel9ca36b62009-02-26 21:10:26 +0000946 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000947 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000948
949 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000950 uint64_t Size = CGM.getContext().getTypeSize(Ty);
951 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Devang Patel6c1fddf2009-07-27 18:42:03 +0000953 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000954 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000955 Line, Size, Align, 0, 0, llvm::DIType(),
956 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000957
958 // Now that we have a real decl for the struct, replace anything using the
959 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000960 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000961
Devang Patel9ca36b62009-02-26 21:10:26 +0000962 return RealDecl;
963}
964
Chris Lattner9c85ba32008-11-10 06:08:34 +0000965llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000966 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000967 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968
969 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
970
971 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000972 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000973 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000974 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000975 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000976 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979 // Return a CompositeType for the enum itself.
980 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000981 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000982
Devang Pateld6c5a262010-02-01 21:52:22 +0000983 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000984 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000985 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000986 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
987 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
988
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattner9c85ba32008-11-10 06:08:34 +0000990 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000991 uint64_t Size = 0;
992 unsigned Align = 0;
993 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000994 Size = CGM.getContext().getTypeSize(Ty);
995 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Devang Patelca80a5f2009-10-20 19:55:01 +0000998 llvm::DIType DbgTy =
999 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001000 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001001 Size, Align, 0, 0,
1002 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001003 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004}
1005
1006llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001007 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001008 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1009 return CreateType(RT, Unit);
1010 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1011 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Chris Lattner9c85ba32008-11-10 06:08:34 +00001013 return llvm::DIType();
1014}
1015
Devang Patel70c23cd2010-02-23 22:59:39 +00001016llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001017 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001018 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1019 uint64_t NumElems = Ty->getNumElements();
1020 if (NumElems > 0)
1021 --NumElems;
1022 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1023 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1024
1025 llvm::DIArray SubscriptArray =
1026 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1027
1028 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1029 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1030
1031 return
1032 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001033 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001034 0, Size, Align, 0, 0,
1035 ElementTy, SubscriptArray);
1036}
1037
Chris Lattner9c85ba32008-11-10 06:08:34 +00001038llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001039 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001040 uint64_t Size;
1041 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
1043
Nuno Lopes010d5142009-01-28 00:35:17 +00001044 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001046 Size = 0;
1047 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001048 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001049 } else if (Ty->isIncompleteArrayType()) {
1050 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001051 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001052 } else {
1053 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001054 Size = CGM.getContext().getTypeSize(Ty);
1055 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1059 // interior arrays, do we care? Why aren't nested arrays represented the
1060 // obvious/recursive way?
1061 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1062 QualType EltTy(Ty, 0);
1063 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001064 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001065 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001066 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001067 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001068 // FIXME: Verify this is right for VLAs.
1069 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1070 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattner9c85ba32008-11-10 06:08:34 +00001073 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001074 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075
Devang Patelca80a5f2009-10-20 19:55:01 +00001076 llvm::DIType DbgTy =
1077 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001078 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001079 0, Size, Align, 0, 0,
1080 getOrCreateType(EltTy, Unit),
1081 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001082 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001083}
1084
Anders Carlssona031b352009-11-06 19:19:55 +00001085llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001086 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001087 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1088 Ty, Ty->getPointeeType(), Unit);
1089}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090
Anders Carlsson20f12a22009-12-06 18:00:51 +00001091llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001092 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001093 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1094 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1095
1096 if (!Ty->getPointeeType()->isFunctionType()) {
1097 // We have a data member pointer type.
1098 return PointerDiffDITy;
1099 }
1100
1101 // We have a member function pointer type. Treat it as a struct with two
1102 // ptrdiff_t members.
1103 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1104
1105 uint64_t FieldOffset = 0;
1106 llvm::DIDescriptor ElementTypes[2];
1107
1108 // FIXME: This should probably be a function type instead.
1109 ElementTypes[0] =
1110 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001111 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001112 Info.first, Info.second, FieldOffset, 0,
1113 PointerDiffDITy);
1114 FieldOffset += Info.first;
1115
1116 ElementTypes[1] =
1117 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001118 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001119 Info.first, Info.second, FieldOffset, 0,
1120 PointerDiffDITy);
1121
1122 llvm::DIArray Elements =
1123 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1124 llvm::array_lengthof(ElementTypes));
1125
1126 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1127 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001128 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001129 0, 0, 0, llvm::DIType(), Elements);
1130}
1131
Douglas Gregor840943d2009-12-21 20:18:30 +00001132static QualType UnwrapTypeForDebugInfo(QualType T) {
1133 do {
1134 QualType LastT = T;
1135 switch (T->getTypeClass()) {
1136 default:
1137 return T;
1138 case Type::TemplateSpecialization:
1139 T = cast<TemplateSpecializationType>(T)->desugar();
1140 break;
1141 case Type::TypeOfExpr: {
1142 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1143 T = Ty->getUnderlyingExpr()->getType();
1144 break;
1145 }
1146 case Type::TypeOf:
1147 T = cast<TypeOfType>(T)->getUnderlyingType();
1148 break;
1149 case Type::Decltype:
1150 T = cast<DecltypeType>(T)->getUnderlyingType();
1151 break;
1152 case Type::QualifiedName:
1153 T = cast<QualifiedNameType>(T)->getNamedType();
1154 break;
1155 case Type::SubstTemplateTypeParm:
1156 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1157 break;
1158 case Type::Elaborated:
1159 T = cast<ElaboratedType>(T)->getUnderlyingType();
1160 break;
1161 }
1162
1163 assert(T != LastT && "Type unwrapping failed to unwrap!");
1164 if (T == LastT)
1165 return T;
1166 } while (true);
1167
1168 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001169}
1170
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001171/// getOrCreateType - Get the type from the cache or create a new
1172/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001173llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001174 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001175 if (Ty.isNull())
1176 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor840943d2009-12-21 20:18:30 +00001178 // Unwrap the type as needed for debug information.
1179 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001180
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001181 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001182 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001183 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001184 if (it != TypeCache.end()) {
1185 // Verify that the debug info still exists.
1186 if (&*it->second)
1187 return llvm::DIType(cast<llvm::MDNode>(it->second));
1188 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001189
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001190 // Otherwise create the type.
1191 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001192
1193 // And update the type cache.
1194 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001195 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001196}
1197
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001198/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001199llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001200 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001201 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001202 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001203 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001204
Douglas Gregor2101a822009-12-21 19:57:21 +00001205 const char *Diag = 0;
1206
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001207 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001208 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001209#define TYPE(Class, Base)
1210#define ABSTRACT_TYPE(Class, Base)
1211#define NON_CANONICAL_TYPE(Class, Base)
1212#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1213#include "clang/AST/TypeNodes.def"
1214 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001215
Anders Carlssonbfe69952009-11-06 18:24:04 +00001216 // FIXME: Handle these.
1217 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001218 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001219
1220 case Type::Vector:
1221 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001222 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001223 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001224 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001225 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1226 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1227 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1228 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001229 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001230 return CreateType(cast<BlockPointerType>(Ty), Unit);
1231 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001232 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001233 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001234 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001235 case Type::FunctionProto:
1236 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001237 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001238 case Type::ConstantArray:
1239 case Type::VariableArray:
1240 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001241 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001242
1243 case Type::LValueReference:
1244 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1245
Anders Carlsson20f12a22009-12-06 18:00:51 +00001246 case Type::MemberPointer:
1247 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001248
John McCall3cb0ebd2010-03-10 03:28:59 +00001249 case Type::InjectedClassName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001250 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001251 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001252 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001253 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001254 case Type::TypeOfExpr:
1255 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001256 case Type::Decltype:
1257 llvm_unreachable("type should have been unwrapped!");
1258 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001259
1260 case Type::RValueReference:
1261 // FIXME: Implement!
1262 Diag = "rvalue references";
1263 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001264 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001265
1266 assert(Diag && "Fall through without a diagnostic?");
1267 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1268 "debug information for %0 is not yet supported");
1269 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1270 << Diag;
1271 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001272}
1273
1274/// EmitFunctionStart - Constructs the debug code for entering a function -
1275/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001276void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001277 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001278 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Devang Patel9c6c3a02010-01-14 00:36:21 +00001280 llvm::StringRef Name;
1281 llvm::StringRef LinkageName;
1282
1283 const Decl *D = GD.getDecl();
1284 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001285 // If there is a DISubprogram for this function available then use it.
1286 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1287 FI = SPCache.find(FD);
1288 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001289 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1290 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001291 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001292 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001293 return;
1294 }
1295 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001296 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001297 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001298 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001299 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001300 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001301 } else {
1302 // Use llvm function name as linkage name.
1303 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001304 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001305 if (!Name.empty() && Name[0] == '\01')
1306 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001307 }
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Devang Patel98a200b2010-01-14 18:06:13 +00001309 // It is expected that CurLoc is set before using EmitFunctionStart.
1310 // Usually, CurLoc points to the left bracket location of compound
1311 // statement representing function body.
Devang Patel17800552010-03-09 00:44:50 +00001312 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001313 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001314 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattner9c85ba32008-11-10 06:08:34 +00001316 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001317 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001318 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001319 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001321 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001322 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001323 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001324}
1325
1326
Chris Lattner9c85ba32008-11-10 06:08:34 +00001327void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001328 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001330 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001331 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001332 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001333 || (SM.getInstantiationLineNumber(CurLoc) ==
1334 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001335 && SM.isFromSameFile(CurLoc, PrevLoc)))
1336 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001337
1338 // Update last state.
1339 PrevLoc = CurLoc;
1340
1341 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001342 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001343 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001344
Devang Patel8fae0602009-11-13 19:10:24 +00001345 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001346 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1347 llvm::DILocation DO(NULL);
1348 llvm::DILocation DL =
1349 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1350 DS, DO);
1351 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001352}
1353
1354/// EmitRegionStart- Constructs the debug code for entering a declarative
1355/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001356void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001357 SourceManager &SM = CGM.getContext().getSourceManager();
1358 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001359 llvm::DIDescriptor D =
1360 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1361 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001362 llvm::DIDescriptor(RegionStack.back()),
1363 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001364 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001365}
1366
1367/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1368/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001369void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001370 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1371
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001372 // Provide an region stop point.
1373 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001375 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001376}
1377
Devang Patel809b9bb2010-02-10 18:49:08 +00001378// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1379// See BuildByRefType.
1380llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1381 uint64_t *XOffset) {
1382
1383 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1384
1385 QualType FType;
1386 uint64_t FieldSize, FieldOffset;
1387 unsigned FieldAlign;
1388
Devang Patel17800552010-03-09 00:44:50 +00001389 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001390 QualType Type = VD->getType();
1391
1392 FieldOffset = 0;
1393 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1394 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1395 FieldSize = CGM.getContext().getTypeSize(FType);
1396 FieldAlign = CGM.getContext().getTypeAlign(FType);
1397 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001398 "__isa", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001399 0, FieldSize, FieldAlign,
1400 FieldOffset, 0, FieldTy);
1401 EltTys.push_back(FieldTy);
1402 FieldOffset += FieldSize;
1403
1404 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1405 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1406 FieldSize = CGM.getContext().getTypeSize(FType);
1407 FieldAlign = CGM.getContext().getTypeAlign(FType);
1408 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001409 "__forwarding", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001410 0, FieldSize, FieldAlign,
1411 FieldOffset, 0, FieldTy);
1412 EltTys.push_back(FieldTy);
1413 FieldOffset += FieldSize;
1414
1415 FType = CGM.getContext().IntTy;
1416 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1417 FieldSize = CGM.getContext().getTypeSize(FType);
1418 FieldAlign = CGM.getContext().getTypeAlign(FType);
1419 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001420 "__flags", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001421 0, FieldSize, FieldAlign,
1422 FieldOffset, 0, FieldTy);
1423 EltTys.push_back(FieldTy);
1424 FieldOffset += FieldSize;
1425
1426 FType = CGM.getContext().IntTy;
1427 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1428 FieldSize = CGM.getContext().getTypeSize(FType);
1429 FieldAlign = CGM.getContext().getTypeAlign(FType);
1430 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001431 "__size", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001432 0, FieldSize, FieldAlign,
1433 FieldOffset, 0, FieldTy);
1434 EltTys.push_back(FieldTy);
1435 FieldOffset += FieldSize;
1436
1437 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1438 if (HasCopyAndDispose) {
1439 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1440 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1441 FieldSize = CGM.getContext().getTypeSize(FType);
1442 FieldAlign = CGM.getContext().getTypeAlign(FType);
1443 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001444 "__copy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001445 0, FieldSize, FieldAlign,
1446 FieldOffset, 0, FieldTy);
1447 EltTys.push_back(FieldTy);
1448 FieldOffset += FieldSize;
1449
1450 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1451 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1452 FieldSize = CGM.getContext().getTypeSize(FType);
1453 FieldAlign = CGM.getContext().getTypeAlign(FType);
1454 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001455 "__destroy_helper", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001456 0, FieldSize, FieldAlign,
1457 FieldOffset, 0, FieldTy);
1458 EltTys.push_back(FieldTy);
1459 FieldOffset += FieldSize;
1460 }
1461
1462 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1463 if (Align > CharUnits::fromQuantity(
1464 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1465 unsigned AlignedOffsetInBytes
1466 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1467 unsigned NumPaddingBytes
1468 = AlignedOffsetInBytes - FieldOffset/8;
1469
1470 if (NumPaddingBytes > 0) {
1471 llvm::APInt pad(32, NumPaddingBytes);
1472 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1473 pad, ArrayType::Normal, 0);
1474 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1475 FieldSize = CGM.getContext().getTypeSize(FType);
1476 FieldAlign = CGM.getContext().getTypeAlign(FType);
1477 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
Devang Pateld58562e2010-03-09 22:49:11 +00001478 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001479 0, FieldSize, FieldAlign,
1480 FieldOffset, 0, FieldTy);
1481 EltTys.push_back(FieldTy);
1482 FieldOffset += FieldSize;
1483 }
1484 }
1485
1486 FType = Type;
1487 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1488 FieldSize = CGM.getContext().getTypeSize(FType);
1489 FieldAlign = Align.getQuantity()*8;
1490
1491 *XOffset = FieldOffset;
1492 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001493 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001494 0, FieldSize, FieldAlign,
1495 FieldOffset, 0, FieldTy);
1496 EltTys.push_back(FieldTy);
1497 FieldOffset += FieldSize;
1498
1499 llvm::DIArray Elements =
1500 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1501
1502 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1503
1504 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001505 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001506 0, FieldOffset, 0, 0, Flags,
1507 llvm::DIType(), Elements);
1508
1509}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001510/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001511void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001512 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001513 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1514
Devang Patel07739032009-03-27 23:16:32 +00001515 // Do not emit variable debug information while generating optimized code.
1516 // The llvm optimizer and code generator are not yet ready to support
1517 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001518 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001519 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001520 return;
1521
Devang Patel17800552010-03-09 00:44:50 +00001522 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001523 llvm::DIType Ty;
1524 uint64_t XOffset = 0;
1525 if (VD->hasAttr<BlocksAttr>())
1526 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1527 else
1528 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001529
Chris Lattner9c85ba32008-11-10 06:08:34 +00001530 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001531 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001532 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001533 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001534 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001535 if (PLoc.isInvalid())
1536 PLoc = SM.getPresumedLoc(CurLoc);
1537 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001538 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001539 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001540 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001541 } else {
Devang Patel17800552010-03-09 00:44:50 +00001542 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Chris Lattner9c85ba32008-11-10 06:08:34 +00001545 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001546 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001547 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001548 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001549 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001550 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001551 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001552 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001553
Devang Patel8fae0602009-11-13 19:10:24 +00001554 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001555 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001556 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1557
Chris Lattner23e92c02009-12-28 23:41:39 +00001558 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001559}
1560
Mike Stumpb1a6e682009-09-30 02:43:10 +00001561/// EmitDeclare - Emit local variable declaration debug info.
1562void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1563 llvm::Value *Storage, CGBuilderTy &Builder,
1564 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001565 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001566 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1567
1568 // Do not emit variable debug information while generating optimized code.
1569 // The llvm optimizer and code generator are not yet ready to support
1570 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001571 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001572 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001573 return;
1574
1575 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001576 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001577 llvm::DIType Ty;
1578 if (VD->hasAttr<BlocksAttr>())
1579 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1580 else
1581 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001582
1583 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001584 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001585 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001586 unsigned Line = 0;
1587 if (!PLoc.isInvalid())
1588 Line = PLoc.getLine();
1589 else
Devang Patel17800552010-03-09 00:44:50 +00001590 Unit = llvm::DIFile();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001591
Devang Pateld6c5a262010-02-01 21:52:22 +00001592 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001593 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001594 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1595 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1596 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1597 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001598 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001599 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1600 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001601 // offset of __forwarding field
1602 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001603 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1604 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1605 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001606 // offset of x field
1607 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001608 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001609 }
1610
1611 // Create the descriptor for the variable.
1612 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001613 DebugFactory.CreateComplexVariable(Tag,
1614 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001615 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001616 addr);
1617 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001618 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001619 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001620
Devang Patel8fae0602009-11-13 19:10:24 +00001621 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001622 llvm::DILocation DO(NULL);
1623 llvm::DILocation DL =
1624 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001625
Chris Lattner23e92c02009-12-28 23:41:39 +00001626 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001627}
1628
Devang Pateld6c5a262010-02-01 21:52:22 +00001629void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001630 llvm::Value *Storage,
1631 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001632 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001633}
1634
Mike Stumpb1a6e682009-09-30 02:43:10 +00001635void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1636 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1637 CodeGenFunction *CGF) {
1638 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1639}
1640
Chris Lattner9c85ba32008-11-10 06:08:34 +00001641/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1642/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001643void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001644 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001645 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001646}
1647
1648
1649
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001650/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001651void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001652 const VarDecl *D) {
1653
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001654 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001655 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001656 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001657 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001658 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001659
Devang Pateleb6d79b2010-02-01 21:34:11 +00001660 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001661 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001663 // CodeGen turns int[] into int[1] so we'll do the same here.
1664 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001666 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001667 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Anders Carlsson20f12a22009-12-06 18:00:51 +00001669 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001670 ArrayType::Normal, 0);
1671 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001672 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001673 llvm::DIDescriptor DContext =
1674 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1675 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001676 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001677 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001678 Var->hasInternalLinkage(),
1679 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001680}
1681
Devang Patel9ca36b62009-02-26 21:10:26 +00001682/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001683void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001684 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001685 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001686 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001687 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001688 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001689 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001690
Devang Pateld6c5a262010-02-01 21:52:22 +00001691 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001692
Devang Pateld6c5a262010-02-01 21:52:22 +00001693 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001694 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Devang Patel9ca36b62009-02-26 21:10:26 +00001696 // CodeGen turns int[] into int[1] so we'll do the same here.
1697 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Devang Patel9ca36b62009-02-26 21:10:26 +00001699 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001700 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Anders Carlsson20f12a22009-12-06 18:00:51 +00001702 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001703 ArrayType::Normal, 0);
1704 }
1705
Devang Patelf6a39b72009-10-20 18:26:30 +00001706 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001707 getOrCreateType(T, Unit),
1708 Var->hasInternalLinkage(),
1709 true/*definition*/, Var);
1710}
Devang Patelabb485f2010-02-01 19:16:32 +00001711
1712/// getOrCreateNamesSpace - Return namespace descriptor for the given
1713/// namespace decl.
1714llvm::DINameSpace
1715CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1716 llvm::DIDescriptor Unit) {
1717 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1718 NameSpaceCache.find(NSDecl);
1719 if (I != NameSpaceCache.end())
1720 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1721
1722 SourceManager &SM = CGM.getContext().getSourceManager();
1723 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1724 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1725
1726 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001727 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001728 llvm::DINameSpace NS =
1729 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel17800552010-03-09 00:44:50 +00001730 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patelabb485f2010-02-01 19:16:32 +00001731 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1732 return NS;
1733}